1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
27 #include "parameters.h"
34 #include "copy-relocs.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
47 template<int size
, bool big_endian
>
48 class Output_data_plt_powerpc
;
50 template<int size
, bool big_endian
>
51 class Output_data_got_powerpc
;
53 template<int size
, bool big_endian
>
54 class Output_data_glink
;
56 template<int size
, bool big_endian
>
57 class Powerpc_relobj
: public Sized_relobj_file
<size
, big_endian
>
60 Powerpc_relobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
61 const typename
elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
62 : Sized_relobj_file
<size
, big_endian
>(name
, input_file
, offset
, ehdr
),
73 return this->got2_section_
;
79 do_find_special_sections(Read_symbols_data
* sd
);
82 unsigned int got2_section_
;
85 template<int size
, bool big_endian
>
86 class Target_powerpc
: public Sized_target
<size
, big_endian
>
90 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
93 : Sized_target
<size
, big_endian
>(&powerpc_info
),
94 got_(NULL
), plt_(NULL
), glink_(NULL
), rela_dyn_(NULL
),
95 copy_relocs_(elfcpp::R_POWERPC_COPY
),
96 dynbss_(NULL
), got_mod_index_offset_(-1U)
100 // Process the relocations to determine unreferenced sections for
101 // garbage collection.
103 gc_process_relocs(Symbol_table
* symtab
,
105 Sized_relobj_file
<size
, big_endian
>* object
,
106 unsigned int data_shndx
,
107 unsigned int sh_type
,
108 const unsigned char* prelocs
,
110 Output_section
* output_section
,
111 bool needs_special_offset_handling
,
112 size_t local_symbol_count
,
113 const unsigned char* plocal_symbols
);
115 // Scan the relocations to look for symbol adjustments.
117 scan_relocs(Symbol_table
* symtab
,
119 Sized_relobj_file
<size
, big_endian
>* object
,
120 unsigned int data_shndx
,
121 unsigned int sh_type
,
122 const unsigned char* prelocs
,
124 Output_section
* output_section
,
125 bool needs_special_offset_handling
,
126 size_t local_symbol_count
,
127 const unsigned char* plocal_symbols
);
128 // Finalize the sections.
130 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
132 // Return the value to use for a dynamic which requires special
135 do_dynsym_value(const Symbol
*) const;
137 // Relocate a section.
139 relocate_section(const Relocate_info
<size
, big_endian
>*,
140 unsigned int sh_type
,
141 const unsigned char* prelocs
,
143 Output_section
* output_section
,
144 bool needs_special_offset_handling
,
146 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
147 section_size_type view_size
,
148 const Reloc_symbol_changes
*);
150 // Scan the relocs during a relocatable link.
152 scan_relocatable_relocs(Symbol_table
* symtab
,
154 Sized_relobj_file
<size
, big_endian
>* object
,
155 unsigned int data_shndx
,
156 unsigned int sh_type
,
157 const unsigned char* prelocs
,
159 Output_section
* output_section
,
160 bool needs_special_offset_handling
,
161 size_t local_symbol_count
,
162 const unsigned char* plocal_symbols
,
163 Relocatable_relocs
*);
165 // Relocate a section during a relocatable link.
167 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
168 unsigned int sh_type
,
169 const unsigned char* prelocs
,
171 Output_section
* output_section
,
172 off_t offset_in_output_section
,
173 const Relocatable_relocs
*,
175 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
177 unsigned char* reloc_view
,
178 section_size_type reloc_view_size
);
180 // Return whether SYM is defined by the ABI.
182 do_is_defined_by_abi(const Symbol
* sym
) const
184 return strcmp(sym
->name(), "__tls_get_addr") == 0;
187 // Return the size of the GOT section.
191 gold_assert(this->got_
!= NULL
);
192 return this->got_
->data_size();
195 // Get the PLT section.
196 const Output_data_plt_powerpc
<size
, big_endian
>*
199 gold_assert(this->plt_
!= NULL
);
203 // Get the .glink section.
204 const Output_data_glink
<size
, big_endian
>*
205 glink_section() const
207 gold_assert(this->glink_
!= NULL
);
211 // Get the GOT section.
212 const Output_data_got_powerpc
<size
, big_endian
>*
215 gold_assert(this->got_
!= NULL
);
221 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
222 const elfcpp::Ehdr
<size
, big_endian
>&);
224 // Return the number of entries in the GOT.
226 got_entry_count() const
228 if (this->got_
== NULL
)
230 return this->got_size() / (size
/ 8);
233 // Return the number of entries in the PLT.
235 plt_entry_count() const;
237 // Return the offset of the first non-reserved PLT entry.
239 first_plt_entry_offset() const;
241 // Return the size of each PLT entry.
243 plt_entry_size() const;
247 // The class which scans relocations.
252 : issued_non_pic_error_(false)
256 get_reference_flags(unsigned int r_type
);
259 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
260 Sized_relobj_file
<size
, big_endian
>* object
,
261 unsigned int data_shndx
,
262 Output_section
* output_section
,
263 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
264 const elfcpp::Sym
<size
, big_endian
>& lsym
);
267 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
268 Sized_relobj_file
<size
, big_endian
>* object
,
269 unsigned int data_shndx
,
270 Output_section
* output_section
,
271 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
275 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
277 Sized_relobj_file
<size
, big_endian
>* ,
280 const elfcpp::Rela
<size
, big_endian
>& ,
282 const elfcpp::Sym
<size
, big_endian
>&)
286 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
288 Sized_relobj_file
<size
, big_endian
>* ,
291 const elfcpp::Rela
<size
,
293 unsigned int , Symbol
*)
298 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
299 unsigned int r_type
);
302 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
303 unsigned int r_type
, Symbol
*);
306 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
307 Target_powerpc
* target
);
310 check_non_pic(Relobj
*, unsigned int r_type
);
312 // Whether we have issued an error about a non-PIC compilation.
313 bool issued_non_pic_error_
;
316 // The class which implements relocation.
320 // Do a relocation. Return false if the caller should not issue
321 // any warnings about this relocation.
323 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
324 Output_section
*, size_t relnum
,
325 const elfcpp::Rela
<size
, big_endian
>&,
326 unsigned int r_type
, const Sized_symbol
<size
>*,
327 const Symbol_value
<size
>*,
329 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
333 // Do a TLS relocation.
335 relocate_tls(const Relocate_info
<size
, big_endian
>*,
336 Target_powerpc
* target
,
337 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
338 unsigned int r_type
, const Sized_symbol
<size
>*,
339 const Symbol_value
<size
>*,
341 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
345 // A class which returns the size required for a relocation type,
346 // used while scanning relocs during a relocatable link.
347 class Relocatable_size_for_reloc
351 get_size_for_reloc(unsigned int, Relobj
*)
358 // Adjust TLS relocation type based on the options and whether this
359 // is a local symbol.
360 static tls::Tls_optimization
361 optimize_tls_reloc(bool is_final
, int r_type
);
363 // Get the GOT section, creating it if necessary.
364 Output_data_got_powerpc
<size
, big_endian
>*
365 got_section(Symbol_table
*, Layout
*);
369 make_glink_section(Layout
*);
371 // Create the PLT section.
373 make_plt_section(Layout
*);
375 // Create a PLT entry for a global symbol.
377 make_plt_entry(Layout
*, Symbol
*,
378 const elfcpp::Rela
<size
, big_endian
>&,
379 const Sized_relobj
<size
, big_endian
>* object
);
381 // Create a GOT entry for the TLS module index.
383 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
384 Sized_relobj_file
<size
, big_endian
>* object
);
386 // Get the dynamic reloc section, creating it if necessary.
388 rela_dyn_section(Layout
*);
390 // Copy a relocation against a global symbol.
392 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
393 Sized_relobj_file
<size
, big_endian
>* object
,
394 unsigned int shndx
, Output_section
* output_section
,
395 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
397 this->copy_relocs_
.copy_reloc(symtab
, layout
,
398 symtab
->get_sized_symbol
<size
>(sym
),
399 object
, shndx
, output_section
,
400 reloc
, this->rela_dyn_section(layout
));
403 // Information about this specific target which we pass to the
404 // general Target structure.
405 static Target::Target_info powerpc_info
;
407 // The types of GOT entries needed for this platform.
408 // These values are exposed to the ABI in an incremental link.
409 // Do not renumber existing values without changing the version
410 // number of the .gnu_incremental_inputs section.
413 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
414 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
415 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
418 // The GOT output section.
419 Output_data_got_powerpc
<size
, big_endian
>* got_
;
420 // The PLT output section.
421 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
422 // The .glink output section.
423 Output_data_glink
<size
, big_endian
>* glink_
;
424 // The dynamic reloc output section.
425 Reloc_section
* rela_dyn_
;
426 // Relocs saved to avoid a COPY reloc.
427 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
428 // Space for variables copied with a COPY reloc.
429 Output_data_space
* dynbss_
;
430 // Offset of the GOT entry for the TLS module index;
431 unsigned int got_mod_index_offset_
;
435 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
438 true, // is_big_endian
439 elfcpp::EM_PPC
, // machine_code
440 false, // has_make_symbol
441 false, // has_resolve
442 false, // has_code_fill
443 true, // is_default_stack_executable
444 false, // can_icf_inline_merge_sections
446 "/usr/lib/ld.so.1", // dynamic_linker
447 0x10000000, // default_text_segment_address
448 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
449 4 * 1024, // common_pagesize (overridable by -z common-page-size)
450 false, // isolate_execinstr
452 elfcpp::SHN_UNDEF
, // small_common_shndx
453 elfcpp::SHN_UNDEF
, // large_common_shndx
454 0, // small_common_section_flags
455 0, // large_common_section_flags
456 NULL
, // attributes_section
457 NULL
// attributes_vendor
461 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
464 false, // is_big_endian
465 elfcpp::EM_PPC
, // machine_code
466 false, // has_make_symbol
467 false, // has_resolve
468 false, // has_code_fill
469 true, // is_default_stack_executable
470 false, // can_icf_inline_merge_sections
472 "/usr/lib/ld.so.1", // dynamic_linker
473 0x10000000, // default_text_segment_address
474 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
475 4 * 1024, // common_pagesize (overridable by -z common-page-size)
476 false, // isolate_execinstr
478 elfcpp::SHN_UNDEF
, // small_common_shndx
479 elfcpp::SHN_UNDEF
, // large_common_shndx
480 0, // small_common_section_flags
481 0, // large_common_section_flags
482 NULL
, // attributes_section
483 NULL
// attributes_vendor
487 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
490 true, // is_big_endian
491 elfcpp::EM_PPC64
, // machine_code
492 false, // has_make_symbol
493 false, // has_resolve
494 false, // has_code_fill
495 true, // is_default_stack_executable
496 false, // can_icf_inline_merge_sections
498 "/usr/lib/ld.so.1", // dynamic_linker
499 0x10000000, // default_text_segment_address
500 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
501 8 * 1024, // common_pagesize (overridable by -z common-page-size)
502 false, // isolate_execinstr
504 elfcpp::SHN_UNDEF
, // small_common_shndx
505 elfcpp::SHN_UNDEF
, // large_common_shndx
506 0, // small_common_section_flags
507 0, // large_common_section_flags
508 NULL
, // attributes_section
509 NULL
// attributes_vendor
513 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
516 false, // is_big_endian
517 elfcpp::EM_PPC64
, // machine_code
518 false, // has_make_symbol
519 false, // has_resolve
520 false, // has_code_fill
521 true, // is_default_stack_executable
522 false, // can_icf_inline_merge_sections
524 "/usr/lib/ld.so.1", // dynamic_linker
525 0x10000000, // default_text_segment_address
526 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
527 8 * 1024, // common_pagesize (overridable by -z common-page-size)
528 false, // isolate_execinstr
530 elfcpp::SHN_UNDEF
, // small_common_shndx
531 elfcpp::SHN_UNDEF
, // large_common_shndx
532 0, // small_common_section_flags
533 0, // large_common_section_flags
534 NULL
, // attributes_section
535 NULL
// attributes_vendor
538 template<int size
, bool big_endian
>
539 class Powerpc_relocate_functions
542 // Do a simple RELA relocation
543 template<int valsize
>
545 rela(unsigned char* view
,
546 unsigned int right_shift
,
547 elfcpp::Elf_Xword dst_mask
,
548 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
549 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
551 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
552 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
553 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
554 Valtype reloc
= (value
+ addend
) >> right_shift
;
559 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
562 // Do a simple RELA relocation, unaligned.
563 template<int valsize
>
565 rela_ua(unsigned char* view
,
566 unsigned int right_shift
,
567 elfcpp::Elf_Xword dst_mask
,
568 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
569 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
571 typedef typename
elfcpp::Swap_unaligned
<valsize
,
572 big_endian
>::Valtype Valtype
;
573 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
574 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
575 Valtype reloc
= (value
+ addend
) >> right_shift
;
580 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
583 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
584 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
586 // R_POWERPC_REL32: (Symbol + Addend - Address)
588 rel32(unsigned char* view
,
589 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
590 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
591 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
592 { This_reloc::pcrela32(view
, value
, addend
, address
); }
594 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
596 rel24(unsigned char* view
,
597 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
599 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
601 This::template rela
<32>(view
, 0, 0x03fffffc, value
- address
, addend
);
604 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
606 rel14(unsigned char* view
,
607 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
608 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
609 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
611 This::template rela
<32>(view
, 0, 0xfffc, value
- address
, addend
);
614 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
616 addr16(unsigned char* view
,
617 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
618 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
619 { This_reloc::rela16(view
, value
, addend
); }
621 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
623 addr16_ds(unsigned char* view
,
624 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
625 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
627 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
630 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
632 addr16_lo(unsigned char* view
,
633 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
634 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
635 { This_reloc::rela16(view
, value
, addend
); }
637 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
639 addr16_hi(unsigned char* view
,
640 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
641 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
643 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
646 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
647 // final value of the low 16 bits of the
648 // relocation is negative, add one.
650 addr16_ha(unsigned char* view
,
651 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
652 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
654 This::addr16_hi(view
, value
+ 0x8000, addend
);
657 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
659 rel16(unsigned char* view
,
660 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
661 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
662 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
663 { This_reloc::pcrela16(view
, value
, addend
, address
); }
665 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
667 rel16_lo(unsigned char* view
,
668 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
669 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
670 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
671 { This_reloc::pcrela16(view
, value
, addend
, address
); }
673 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
675 rel16_hi(unsigned char* view
,
676 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
677 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
678 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
680 This::template rela
<16>(view
, 16, 0xffff, value
- address
, addend
);
683 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
684 // final value of the low 16 bits of the
685 // relocation is negative, add one.
687 rel16_ha(unsigned char* view
,
688 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
689 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
690 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
692 This::rel16_hi(view
, value
+ 0x8000, addend
, address
);
696 // Stash away the index of .got2 in a relocatable object, if such
699 template<int size
, bool big_endian
>
701 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
702 Read_symbols_data
* sd
)
706 const unsigned char* const pshdrs
= sd
->section_headers
->data();
707 const unsigned char* namesu
= sd
->section_names
->data();
708 const char* names
= reinterpret_cast<const char*>(namesu
);
709 section_size_type names_size
= sd
->section_names_size
;
710 const unsigned char* s
;
712 s
= this->find_shdr(pshdrs
, ".got2", names
, names_size
, NULL
);
715 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
716 this->got2_section_
= ndx
;
719 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
722 // Set up PowerPC target specific relobj.
724 template<int size
, bool big_endian
>
726 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
727 const std::string
& name
,
728 Input_file
* input_file
,
729 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
731 int et
= ehdr
.get_e_type();
732 if (et
== elfcpp::ET_REL
)
734 Powerpc_relobj
<size
, big_endian
>* obj
=
735 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
739 else if (et
== elfcpp::ET_DYN
)
741 Sized_dynobj
<size
, big_endian
>* obj
=
742 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
748 gold_error(_("%s: unsupported ELF file type %d"),
754 template<int size
, bool big_endian
>
755 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
758 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
759 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
761 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
762 : Output_data_got
<size
, big_endian
>(),
763 symtab_(symtab
), layout_(layout
),
764 header_ent_cnt_(size
== 32 ? 3 : 1),
765 header_index_(size
== 32 ? 0x2000 : 0)
770 // Create a new GOT entry and return its offset.
772 add_got_entry(Got_entry got_entry
)
775 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
778 // Create a pair of new GOT entries and return the offset of the first.
780 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
782 this->reserve_ent(2);
783 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
787 // Value of _GLOBAL_OFFSET_TABLE_
791 return this->got_offset(this->header_index_
);
794 // Ensure our GOT has a header.
796 set_final_data_size()
798 if (this->header_ent_cnt_
!= 0)
800 Output_data_got
<size
, big_endian
>::set_final_data_size();
803 // First word of GOT header needs some values that are not
804 // handled by Output_data_got so poke them in here.
805 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
807 do_write(Output_file
* of
)
809 replace_constant(this->header_index_
,
811 ? this->layout_
->dynamic_section()->address()
812 : this->address() + 0x8000));
814 Output_data_got
<size
, big_endian
>::do_write(of
);
819 reserve_ent(unsigned int cnt
= 1)
821 if (this->header_ent_cnt_
== 0)
823 if (this->num_entries() + cnt
> this->header_index_
)
830 this->header_ent_cnt_
= 0;
831 this->header_index_
= this->num_entries();
834 Output_data_got
<size
, big_endian
>::add_constant(0);
835 Output_data_got
<size
, big_endian
>::add_constant(0);
836 Output_data_got
<size
, big_endian
>::add_constant(0);
838 // Define _GLOBAL_OFFSET_TABLE_ at the header
839 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
840 Symbol_table::PREDEFINED
,
841 this, this->g_o_t(), 0,
848 Output_data_got
<size
, big_endian
>::add_constant(0);
852 Symbol_table
* symtab_
;
856 unsigned int header_ent_cnt_
;
858 unsigned int header_index_
;
861 // Get the GOT section, creating it if necessary.
863 template<int size
, bool big_endian
>
864 Output_data_got_powerpc
<size
, big_endian
>*
865 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
868 if (this->got_
== NULL
)
870 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
873 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
875 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
876 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
877 this->got_
, ORDER_DATA
, false);
883 // Get the dynamic reloc section, creating it if necessary.
885 template<int size
, bool big_endian
>
886 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
887 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
889 if (this->rela_dyn_
== NULL
)
891 gold_assert(layout
!= NULL
);
892 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
893 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
894 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
895 ORDER_DYNAMIC_RELOCS
, false);
897 return this->rela_dyn_
;
900 // A class to handle the PLT data.
902 template<int size
, bool big_endian
>
903 class Output_data_plt_powerpc
: public Output_section_data_build
906 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
907 size
, big_endian
> Reloc_section
;
909 Output_data_plt_powerpc(Layout
*, Target_powerpc
<size
, big_endian
>*);
911 // Add an entry to the PLT.
915 // Return the .rela.plt section data.
922 // Return the number of PLT entries.
926 return ((this->current_data_size() - initial_plt_entry_size
)
930 // Return the offset of the first non-reserved PLT entry.
932 first_plt_entry_offset()
933 { return initial_plt_entry_size
; }
935 // Return the size of a PLT entry.
938 { return plt_entry_size
; }
942 do_adjust_output_section(Output_section
* os
)
948 // The size of an entry in the PLT.
949 static const int plt_entry_size
= size
== 32 ? 4 : 24;
950 // The size of the first reserved entry.
951 static const int initial_plt_entry_size
= size
== 32 ? 0 : 24;
953 // Write out the PLT data.
955 do_write(Output_file
*);
957 // The reloc section.
959 // Allows access to .glink for do_write.
960 Target_powerpc
<size
, big_endian
>* targ_
;
963 // Create the PLT section.
965 template<int size
, bool big_endian
>
966 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(
968 Target_powerpc
<size
, big_endian
>* targ
)
969 : Output_section_data_build(size
== 32 ? 4 : 8),
972 this->rel_
= new Reloc_section(false);
973 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
974 elfcpp::SHF_ALLOC
, this->rel_
,
975 ORDER_DYNAMIC_PLT_RELOCS
, false);
978 // Add an entry to the PLT.
980 template<int size
, bool big_endian
>
982 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
984 if (!gsym
->has_plt_offset())
986 off_t off
= this->current_data_size();
989 off
+= initial_plt_entry_size
;
990 gsym
->set_plt_offset(off
);
991 gsym
->set_needs_dynsym_entry();
992 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this, off
, 0);
993 off
+= plt_entry_size
;
994 this->set_current_data_size(off
);
998 static const uint32_t addis_11_11
= 0x3d6b0000;
999 static const uint32_t addis_11_30
= 0x3d7e0000;
1000 static const uint32_t addis_12_12
= 0x3d8c0000;
1001 static const uint32_t addi_11_11
= 0x396b0000;
1002 static const uint32_t add_0_11_11
= 0x7c0b5a14;
1003 static const uint32_t add_11_0_11
= 0x7d605a14;
1004 static const uint32_t b
= 0x48000000;
1005 static const uint32_t bcl_20_31
= 0x429f0005;
1006 static const uint32_t bctr
= 0x4e800420;
1007 static const uint32_t blrl
= 0x4e800021;
1008 static const uint32_t lis_11
= 0x3d600000;
1009 static const uint32_t lis_12
= 0x3d800000;
1010 static const uint32_t lwzu_0_12
= 0x840c0000;
1011 static const uint32_t lwz_0_12
= 0x800c0000;
1012 static const uint32_t lwz_11_11
= 0x816b0000;
1013 static const uint32_t lwz_11_30
= 0x817e0000;
1014 static const uint32_t lwz_12_12
= 0x818c0000;
1015 static const uint32_t mflr_0
= 0x7c0802a6;
1016 static const uint32_t mflr_12
= 0x7d8802a6;
1017 static const uint32_t mtctr_0
= 0x7c0903a6;
1018 static const uint32_t mtctr_11
= 0x7d6903a6;
1019 static const uint32_t mtlr_0
= 0x7c0803a6;
1020 static const uint32_t nop
= 0x60000000;
1021 static const uint32_t sub_11_11_12
= 0x7d6c5850;
1022 static const uint32_t addis_12_2
= 0x3d820000;
1023 static const uint32_t std_2_1
= 0xf8410000;
1024 static const uint32_t ld_11_12
= 0xe96c0000;
1025 static const uint32_t ld_2_12
= 0xe84c0000;
1026 static const uint32_t addi_12_12
= 0x398c0000;
1027 static const uint32_t ld_11_2
= 0xe9620000;
1028 static const uint32_t addi_2_2
= 0x38420000;
1029 static const uint32_t ld_2_2
= 0xe8420000;
1030 static const uint32_t mflr_11
= 0x7d6802a6;
1031 static const uint32_t ld_2_11
= 0xe84b0000;
1032 static const uint32_t mtlr_12
= 0x7d8803a6;
1033 static const uint32_t add_12_2_11
= 0x7d825a14;
1034 static const uint32_t li_0_0
= 0x38000000;
1035 static const uint32_t lis_0_0
= 0x3c000000;
1036 static const uint32_t ori_0_0_0
= 0x60000000;
1038 // Write out the PLT.
1040 template<int size
, bool big_endian
>
1042 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
1046 const off_t offset
= this->offset();
1047 const section_size_type oview_size
1048 = convert_to_section_size_type(this->data_size());
1049 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1050 unsigned char* pov
= oview
;
1051 unsigned char* endpov
= oview
+ oview_size
;
1053 // The address the .glink branch table
1054 const Output_data_glink
<size
, big_endian
>* glink
1055 = this->targ_
->glink_section();
1056 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
1057 = glink
->address() + glink
->pltresolve();
1059 while (pov
< endpov
)
1061 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
1066 of
->write_output_view(offset
, oview_size
, oview
);
1070 // Create the PLT section.
1072 template<int size
, bool big_endian
>
1074 Target_powerpc
<size
, big_endian
>::make_plt_section(Layout
* layout
)
1076 if (this->plt_
== NULL
)
1078 if (this->glink_
== NULL
)
1079 make_glink_section(layout
);
1081 // Ensure that .rela.dyn always appears before .rela.plt This is
1082 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1083 // needs to include .rela.plt in it's range.
1084 this->rela_dyn_section(layout
);
1086 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
, this);
1087 layout
->add_output_section_data(".plt",
1089 ? elfcpp::SHT_PROGBITS
1090 : elfcpp::SHT_NOBITS
),
1091 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1100 // A class to handle .glink.
1102 template<int size
, bool big_endian
>
1103 class Output_data_glink
: public Output_section_data
1106 Output_data_glink(Target_powerpc
<size
, big_endian
>*);
1110 add_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1111 const Sized_relobj
<size
, big_endian
>*);
1114 find_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1115 const Sized_relobj
<size
, big_endian
>*) const;
1118 glink_entry_size() const
1123 // FIXME: We should be using multiple glink sections for
1124 // stubs to support > 33M applications.
1131 return this->pltresolve_
;
1135 static const int pltresolve_size
= 16*4;
1138 set_final_data_size();
1142 do_write(Output_file
*);
1147 Glink_sym_ent(const Symbol
* sym
,
1148 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1149 const Sized_relobj
<size
, big_endian
>* object
)
1150 : sym_(sym
), object_(0), addend_(0)
1153 this->addend_
= reloc
.get_r_addend();
1154 else if (parameters
->options().output_is_position_independent()
1155 && (elfcpp::elf_r_type
<size
>(reloc
.get_r_info())
1156 == elfcpp::R_PPC_PLTREL24
))
1158 this->addend_
= reloc
.get_r_addend();
1159 if (this->addend_
!= 0)
1160 this->object_
= object
;
1165 const Sized_relobj
<size
, big_endian
>* object_
;
1166 unsigned int addend_
;
1168 bool operator==(const Glink_sym_ent
& that
) const
1170 return (this->sym_
== that
.sym_
1171 && this->object_
== that
.object_
1172 && this->addend_
== that
.addend_
);
1176 class Glink_sym_ent_hash
1179 size_t operator()(const Glink_sym_ent
& ent
) const
1181 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
1182 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
1187 // Map sym/object/addend to index.
1188 typedef Unordered_map
<Glink_sym_ent
, unsigned int,
1189 Glink_sym_ent_hash
> Glink_entries
;
1190 Glink_entries glink_entries_
;
1192 // Offset of pltresolve stub (actually, branch table for 32-bit)
1195 // Allows access to .got and .plt for do_write.
1196 Target_powerpc
<size
, big_endian
>* targ_
;
1199 // Create the glink section.
1201 template<int size
, bool big_endian
>
1202 Output_data_glink
<size
, big_endian
>::Output_data_glink(
1203 Target_powerpc
<size
, big_endian
>* targ
)
1204 : Output_section_data(16),
1205 pltresolve_(0), targ_(targ
)
1209 // Add an entry to glink, if we do not already have one for this
1210 // sym/object/addend combo.
1212 template<int size
, bool big_endian
>
1214 Output_data_glink
<size
, big_endian
>::add_entry(
1216 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1217 const Sized_relobj
<size
, big_endian
>* object
)
1219 Glink_sym_ent
ent(gsym
, reloc
, object
);
1220 unsigned int indx
= this->glink_entries_
.size();
1221 this->glink_entries_
.insert(std::make_pair(ent
, indx
));
1224 template<int size
, bool big_endian
>
1226 Output_data_glink
<size
, big_endian
>::find_entry(
1228 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1229 const Sized_relobj
<size
, big_endian
>* object
) const
1231 Glink_sym_ent
ent(gsym
, reloc
, object
);
1232 typename
Glink_entries::const_iterator p
= this->glink_entries_
.find(ent
);
1233 gold_assert(p
!= this->glink_entries_
.end());
1237 template<int size
, bool big_endian
>
1239 Output_data_glink
<size
, big_endian
>::set_final_data_size()
1241 unsigned int count
= this->glink_entries_
.size();
1242 off_t total
= count
;
1249 this->pltresolve_
= total
;
1251 // space for branch table
1252 total
+= 4 * (count
- 1);
1254 total
+= -total
& 15;
1255 total
+= this->pltresolve_size
;
1260 this->pltresolve_
= total
;
1261 total
+= this->pltresolve_size
;
1263 // space for branch table
1266 total
+= 4 * (count
- 0x8000);
1270 this->set_data_size(total
);
1273 static inline uint32_t
1279 static inline uint32_t
1285 static inline uint32_t
1288 return hi(a
+ 0x8000);
1291 template<bool big_endian
>
1293 write_insn(unsigned char *p
, uint32_t v
)
1295 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
1298 // Write out .glink.
1300 template<int size
, bool big_endian
>
1302 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
1304 const off_t off
= this->offset();
1305 const section_size_type oview_size
=
1306 convert_to_section_size_type(this->data_size());
1307 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1310 // The base address of the .plt section.
1311 uint32_t plt_base
= this->targ_
->plt_section()->address();
1313 // The address of _GLOBAL_OFFSET_TABLE_.
1314 const Output_data_got_powerpc
<size
, big_endian
> *got
;
1315 typename
elfcpp::Elf_types
<size
>::Elf_Addr g_o_t
;
1316 got
= this->targ_
->got_section();
1317 g_o_t
= got
->address() + got
->g_o_t();
1321 // Write out call stubs.
1322 typename
Glink_entries::const_iterator g
;
1323 for (g
= this->glink_entries_
.begin();
1324 g
!= this->glink_entries_
.end();
1327 uint64_t plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1328 uint64_t got_addr
= g_o_t
;
1329 uint64_t pltoff
= plt_addr
- got_addr
;
1331 if (pltoff
+ 0x80008000 > 0xffffffff || (pltoff
& 7) != 0)
1332 gold_error(_("%s: linkage table error against `%s'"),
1333 g
->first
.object_
->name().c_str(),
1334 g
->first
.sym_
->demangled_name().c_str());
1336 p
= oview
+ g
->second
* this->glink_entry_size();
1337 if (ha(pltoff
) != 0)
1339 write_insn
<big_endian
>(p
, addis_12_2
+ ha(pltoff
)), p
+= 4;
1340 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1341 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
)), p
+= 4;
1342 if (ha(pltoff
+ 16) != ha(pltoff
))
1344 write_insn
<big_endian
>(p
, addi_12_12
+ l(pltoff
)), p
+= 4;
1347 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1348 write_insn
<big_endian
>(p
, ld_2_12
+ l(pltoff
+ 8)), p
+= 4;
1349 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
+ 16)), p
+= 4;
1350 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1354 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1355 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
)), p
+= 4;
1356 if (ha(pltoff
+ 16) != ha(pltoff
))
1358 write_insn
<big_endian
>(p
, addi_2_2
+ l(pltoff
)), p
+= 4;
1361 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1362 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
+ 16)), p
+= 4;
1363 write_insn
<big_endian
>(p
, ld_2_2
+ l(pltoff
+ 8)), p
+= 4;
1364 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1368 // Write pltresolve stub.
1369 p
= oview
+ this->pltresolve_
;
1370 uint64_t after_bcl
= this->address() + this->pltresolve_
+ 16;
1371 uint64_t pltoff
= plt_base
- after_bcl
;
1373 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
1375 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
1376 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
1377 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
1378 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
1379 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
1380 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
1381 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
1382 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
1383 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1384 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
1385 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1386 while (p
< oview
+ this->pltresolve_
+ this->pltresolve_size
)
1387 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1389 // Write lazy link call stubs.
1391 while (p
< oview
+ oview_size
)
1395 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
1399 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
1400 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
1402 uint16_t branch_off
= this->pltresolve_
+ 8 - (p
- oview
);
1403 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
1409 // Write out call stubs.
1410 typename
Glink_entries::const_iterator g
;
1411 for (g
= this->glink_entries_
.begin();
1412 g
!= this->glink_entries_
.end();
1415 uint32_t plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1418 p
= oview
+ g
->second
* this->glink_entry_size();
1419 if (parameters
->options().output_is_position_independent())
1421 const Powerpc_relobj
<size
, big_endian
>* object
= static_cast
1422 <const Powerpc_relobj
<size
, big_endian
>*>(g
->first
.object_
);
1425 unsigned int got2
= object
->got2_shndx();
1426 got_addr
= (g
->first
.object_
->output_section(got2
)->address()
1427 + g
->first
.object_
->output_section_offset(got2
)
1428 + g
->first
.addend_
);
1433 uint32_t pltoff
= plt_addr
- got_addr
;
1434 if (ha(pltoff
) == 0)
1436 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(pltoff
));
1437 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
1438 write_insn
<big_endian
>(p
+ 8, bctr
);
1442 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(pltoff
));
1443 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(pltoff
));
1444 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1445 write_insn
<big_endian
>(p
+ 12, bctr
);
1450 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
1451 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
1452 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1453 write_insn
<big_endian
>(p
+ 12, bctr
);
1457 // Write out pltresolve branch table.
1458 p
= oview
+ this->pltresolve_
;
1459 unsigned int the_end
= oview_size
- this->pltresolve_size
;
1460 unsigned char *end_p
= oview
+ the_end
;
1461 while (p
< end_p
- 8 * 4)
1462 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
1464 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1466 // Write out pltresolve call stub.
1467 if (parameters
->options().output_is_position_independent())
1469 uint32_t res0_off
= this->pltresolve_
;
1470 uint32_t after_bcl_off
= the_end
+ 12;
1471 uint32_t bcl_res0
= after_bcl_off
- res0_off
;
1473 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
1474 write_insn
<big_endian
>(p
+ 4, mflr_0
);
1475 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
1476 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
1477 write_insn
<big_endian
>(p
+ 16, mflr_12
);
1478 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
1479 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
1481 uint32_t got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
1483 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
1484 if (ha(got_bcl
) == ha(got_bcl
+ 4))
1486 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
1487 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
1491 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
1492 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
1494 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
1495 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
1496 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
1497 write_insn
<big_endian
>(p
+ 52, bctr
);
1498 write_insn
<big_endian
>(p
+ 56, nop
);
1499 write_insn
<big_endian
>(p
+ 60, nop
);
1503 uint32_t res0
= this->pltresolve_
+ this->address();
1505 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
1506 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
1507 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1508 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
1510 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
1511 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
1512 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
1513 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
1514 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1515 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
1517 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
1518 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
1519 write_insn
<big_endian
>(p
+ 32, bctr
);
1520 write_insn
<big_endian
>(p
+ 36, nop
);
1521 write_insn
<big_endian
>(p
+ 40, nop
);
1522 write_insn
<big_endian
>(p
+ 44, nop
);
1523 write_insn
<big_endian
>(p
+ 48, nop
);
1524 write_insn
<big_endian
>(p
+ 52, nop
);
1525 write_insn
<big_endian
>(p
+ 56, nop
);
1526 write_insn
<big_endian
>(p
+ 60, nop
);
1531 of
->write_output_view(off
, oview_size
, oview
);
1534 // Create the glink section.
1536 template<int size
, bool big_endian
>
1538 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
1540 if (this->glink_
== NULL
)
1542 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
1543 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
1544 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1545 this->glink_
, ORDER_TEXT
, false);
1549 // Create a PLT entry for a global symbol.
1551 template<int size
, bool big_endian
>
1553 Target_powerpc
<size
, big_endian
>::make_plt_entry(
1556 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1557 const Sized_relobj
<size
, big_endian
>* object
)
1559 if (this->plt_
== NULL
)
1560 this->make_plt_section(layout
);
1562 this->plt_
->add_entry(gsym
);
1564 this->glink_
->add_entry(gsym
, reloc
, object
);
1567 // Return the number of entries in the PLT.
1569 template<int size
, bool big_endian
>
1571 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
1573 if (this->plt_
== NULL
)
1575 return this->plt_
->entry_count();
1578 // Return the offset of the first non-reserved PLT entry.
1580 template<int size
, bool big_endian
>
1582 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
1584 return Output_data_plt_powerpc
<size
, big_endian
>::first_plt_entry_offset();
1587 // Return the size of each PLT entry.
1589 template<int size
, bool big_endian
>
1591 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
1593 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
1596 // Create a GOT entry for the TLS module index.
1598 template<int size
, bool big_endian
>
1600 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(
1601 Symbol_table
* symtab
,
1603 Sized_relobj_file
<size
, big_endian
>* object
)
1605 if (this->got_mod_index_offset_
== -1U)
1607 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1608 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1609 Output_data_got_powerpc
<size
, big_endian
>* got
;
1610 unsigned int got_offset
;
1612 got
= this->got_section(symtab
, layout
);
1613 got
->reserve_ent(2);
1614 got_offset
= got
->add_constant(0);
1615 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
1617 got
->add_constant(0);
1618 this->got_mod_index_offset_
= got_offset
;
1620 return this->got_mod_index_offset_
;
1623 // Optimize the TLS relocation type based on what we know about the
1624 // symbol. IS_FINAL is true if the final address of this symbol is
1625 // known at link time.
1627 template<int size
, bool big_endian
>
1628 tls::Tls_optimization
1629 Target_powerpc
<size
, big_endian
>::optimize_tls_reloc(bool, int)
1631 // If we are generating a shared library, then we can't do anything
1633 if (parameters
->options().shared())
1634 return tls::TLSOPT_NONE
;
1636 return tls::TLSOPT_NONE
;
1639 // Get the Reference_flags for a particular relocation.
1641 template<int size
, bool big_endian
>
1643 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
1647 case elfcpp::R_POWERPC_NONE
:
1648 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1649 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1650 case elfcpp::R_PPC64_TOC
:
1651 // No symbol reference.
1654 case elfcpp::R_POWERPC_ADDR16
:
1655 case elfcpp::R_POWERPC_ADDR16_LO
:
1656 case elfcpp::R_POWERPC_ADDR16_HI
:
1657 case elfcpp::R_POWERPC_ADDR16_HA
:
1658 case elfcpp::R_POWERPC_ADDR32
:
1659 case elfcpp::R_PPC64_ADDR64
:
1660 return Symbol::ABSOLUTE_REF
;
1662 case elfcpp::R_POWERPC_REL24
:
1663 case elfcpp::R_PPC_LOCAL24PC
:
1664 case elfcpp::R_PPC_REL16
:
1665 case elfcpp::R_PPC_REL16_LO
:
1666 case elfcpp::R_PPC_REL16_HI
:
1667 case elfcpp::R_PPC_REL16_HA
:
1668 return Symbol::RELATIVE_REF
;
1670 case elfcpp::R_PPC_PLTREL24
:
1671 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1673 case elfcpp::R_POWERPC_GOT16
:
1674 case elfcpp::R_POWERPC_GOT16_LO
:
1675 case elfcpp::R_POWERPC_GOT16_HI
:
1676 case elfcpp::R_POWERPC_GOT16_HA
:
1677 case elfcpp::R_PPC64_TOC16
:
1678 case elfcpp::R_PPC64_TOC16_LO
:
1679 case elfcpp::R_PPC64_TOC16_HI
:
1680 case elfcpp::R_PPC64_TOC16_HA
:
1681 case elfcpp::R_PPC64_TOC16_DS
:
1682 case elfcpp::R_PPC64_TOC16_LO_DS
:
1684 return Symbol::ABSOLUTE_REF
;
1686 case elfcpp::R_POWERPC_GOT_TPREL16
:
1687 case elfcpp::R_POWERPC_TLS
:
1688 return Symbol::TLS_REF
;
1690 case elfcpp::R_POWERPC_COPY
:
1691 case elfcpp::R_POWERPC_GLOB_DAT
:
1692 case elfcpp::R_POWERPC_JMP_SLOT
:
1693 case elfcpp::R_POWERPC_RELATIVE
:
1694 case elfcpp::R_POWERPC_DTPMOD
:
1696 // Not expected. We will give an error later.
1701 // Report an unsupported relocation against a local symbol.
1703 template<int size
, bool big_endian
>
1705 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1706 Sized_relobj_file
<size
, big_endian
>* object
,
1707 unsigned int r_type
)
1709 gold_error(_("%s: unsupported reloc %u against local symbol"),
1710 object
->name().c_str(), r_type
);
1713 // We are about to emit a dynamic relocation of type R_TYPE. If the
1714 // dynamic linker does not support it, issue an error.
1716 template<int size
, bool big_endian
>
1718 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1719 unsigned int r_type
)
1721 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1723 // These are the relocation types supported by glibc for both 32-bit
1724 // and 64-bit powerpc.
1727 case elfcpp::R_POWERPC_RELATIVE
:
1728 case elfcpp::R_POWERPC_GLOB_DAT
:
1729 case elfcpp::R_POWERPC_DTPMOD
:
1730 case elfcpp::R_POWERPC_DTPREL
:
1731 case elfcpp::R_POWERPC_TPREL
:
1732 case elfcpp::R_POWERPC_JMP_SLOT
:
1733 case elfcpp::R_POWERPC_COPY
:
1734 case elfcpp::R_POWERPC_ADDR32
:
1735 case elfcpp::R_POWERPC_ADDR24
:
1736 case elfcpp::R_POWERPC_REL24
:
1747 // These are the relocation types supported only on 64-bit.
1748 case elfcpp::R_PPC64_ADDR64
:
1749 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1750 case elfcpp::R_PPC64_TPREL16_DS
:
1751 case elfcpp::R_POWERPC_TPREL16
:
1752 case elfcpp::R_POWERPC_TPREL16_LO
:
1753 case elfcpp::R_POWERPC_TPREL16_HI
:
1754 case elfcpp::R_POWERPC_TPREL16_HA
:
1755 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1756 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1757 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1758 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1759 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1760 case elfcpp::R_POWERPC_ADDR16_LO
:
1761 case elfcpp::R_POWERPC_ADDR16_HI
:
1762 case elfcpp::R_POWERPC_ADDR16_HA
:
1763 case elfcpp::R_POWERPC_ADDR30
:
1764 case elfcpp::R_PPC64_UADDR64
:
1765 case elfcpp::R_POWERPC_UADDR32
:
1766 case elfcpp::R_POWERPC_ADDR16
:
1767 case elfcpp::R_POWERPC_UADDR16
:
1768 case elfcpp::R_PPC64_ADDR16_DS
:
1769 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1770 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1771 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1772 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1773 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1774 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1775 case elfcpp::R_POWERPC_REL32
:
1776 case elfcpp::R_PPC64_REL64
:
1787 // These are the relocation types supported only on 32-bit.
1794 // This prevents us from issuing more than one error per reloc
1795 // section. But we can still wind up issuing more than one
1796 // error per object file.
1797 if (this->issued_non_pic_error_
)
1799 gold_assert(parameters
->options().output_is_position_independent());
1800 object
->error(_("requires unsupported dynamic reloc; "
1801 "recompile with -fPIC"));
1802 this->issued_non_pic_error_
= true;
1806 // Scan a relocation for a local symbol.
1808 template<int size
, bool big_endian
>
1810 Target_powerpc
<size
, big_endian
>::Scan::local(
1811 Symbol_table
* symtab
,
1813 Target_powerpc
<size
, big_endian
>* target
,
1814 Sized_relobj_file
<size
, big_endian
>* object
,
1815 unsigned int data_shndx
,
1816 Output_section
* output_section
,
1817 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1818 unsigned int r_type
,
1819 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1823 case elfcpp::R_POWERPC_NONE
:
1824 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1825 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1828 case elfcpp::R_PPC64_ADDR64
:
1829 case elfcpp::R_POWERPC_ADDR32
:
1830 case elfcpp::R_POWERPC_ADDR16_HA
:
1831 case elfcpp::R_POWERPC_ADDR16_LO
:
1832 // If building a shared library (or a position-independent
1833 // executable), we need to create a dynamic relocation for
1835 if (parameters
->options().output_is_position_independent())
1837 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1839 check_non_pic(object
, r_type
);
1840 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1842 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1843 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1844 data_shndx
, reloc
.get_r_offset(),
1845 reloc
.get_r_addend());
1849 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1850 gold_assert(lsym
.get_st_value() == 0);
1851 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1852 output_section
, data_shndx
,
1853 reloc
.get_r_offset(),
1854 reloc
.get_r_addend(), false);
1859 case elfcpp::R_POWERPC_REL24
:
1860 case elfcpp::R_PPC_LOCAL24PC
:
1861 case elfcpp::R_POWERPC_REL32
:
1862 case elfcpp::R_PPC_REL16_LO
:
1863 case elfcpp::R_PPC_REL16_HA
:
1866 case elfcpp::R_POWERPC_GOT16
:
1867 case elfcpp::R_POWERPC_GOT16_LO
:
1868 case elfcpp::R_POWERPC_GOT16_HI
:
1869 case elfcpp::R_POWERPC_GOT16_HA
:
1871 // The symbol requires a GOT entry.
1872 Output_data_got_powerpc
<size
, big_endian
>* got
;
1875 got
= target
->got_section(symtab
, layout
);
1876 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1878 // If we are generating a shared object, we need to add a
1879 // dynamic relocation for this symbol's GOT entry.
1880 if (parameters
->options().output_is_position_independent())
1882 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1884 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1887 off
= got
->add_constant(0);
1888 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1889 rela_dyn
->add_local_relative(object
, r_sym
,
1890 elfcpp::R_POWERPC_RELATIVE
,
1891 got
, off
, 0, false);
1895 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1899 case elfcpp::R_PPC64_TOC16
:
1900 case elfcpp::R_PPC64_TOC16_LO
:
1901 case elfcpp::R_PPC64_TOC16_HI
:
1902 case elfcpp::R_PPC64_TOC16_HA
:
1903 case elfcpp::R_PPC64_TOC16_DS
:
1904 case elfcpp::R_PPC64_TOC16_LO_DS
:
1905 case elfcpp::R_PPC64_TOC
:
1906 // We need a GOT section.
1907 target
->got_section(symtab
, layout
);
1910 // These are relocations which should only be seen by the
1911 // dynamic linker, and should never be seen here.
1912 case elfcpp::R_POWERPC_COPY
:
1913 case elfcpp::R_POWERPC_GLOB_DAT
:
1914 case elfcpp::R_POWERPC_JMP_SLOT
:
1915 case elfcpp::R_POWERPC_RELATIVE
:
1916 case elfcpp::R_POWERPC_DTPMOD
:
1917 gold_error(_("%s: unexpected reloc %u in object file"),
1918 object
->name().c_str(), r_type
);
1922 unsupported_reloc_local(object
, r_type
);
1927 // Report an unsupported relocation against a global symbol.
1929 template<int size
, bool big_endian
>
1931 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1932 Sized_relobj_file
<size
, big_endian
>* object
,
1933 unsigned int r_type
,
1936 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1937 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1940 // Scan a relocation for a global symbol.
1942 template<int size
, bool big_endian
>
1944 Target_powerpc
<size
, big_endian
>::Scan::global(
1945 Symbol_table
* symtab
,
1947 Target_powerpc
<size
, big_endian
>* target
,
1948 Sized_relobj_file
<size
, big_endian
>* object
,
1949 unsigned int data_shndx
,
1950 Output_section
* output_section
,
1951 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1952 unsigned int r_type
,
1957 case elfcpp::R_POWERPC_NONE
:
1958 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1959 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1960 case elfcpp::R_PPC_LOCAL24PC
:
1963 case elfcpp::R_POWERPC_ADDR16
:
1964 case elfcpp::R_POWERPC_ADDR16_LO
:
1965 case elfcpp::R_POWERPC_ADDR16_HI
:
1966 case elfcpp::R_POWERPC_ADDR16_HA
:
1967 case elfcpp::R_POWERPC_ADDR32
:
1968 case elfcpp::R_PPC64_ADDR64
:
1970 // Make a PLT entry if necessary.
1971 if (gsym
->needs_plt_entry())
1973 target
->make_plt_entry(layout
, gsym
, reloc
, 0);
1974 // Since this is not a PC-relative relocation, we may be
1975 // taking the address of a function. In that case we need to
1976 // set the entry in the dynamic symbol table to the address of
1979 && gsym
->is_from_dynobj() && !parameters
->options().shared())
1980 gsym
->set_needs_dynsym_value();
1982 // Make a dynamic relocation if necessary.
1983 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1985 if (gsym
->may_need_copy_reloc())
1987 target
->copy_reloc(symtab
, layout
, object
,
1988 data_shndx
, output_section
, gsym
, reloc
);
1990 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1991 || r_type
== elfcpp::R_PPC64_ADDR64
)
1992 && gsym
->can_use_relative_reloc(false))
1994 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1995 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1996 output_section
, object
,
1997 data_shndx
, reloc
.get_r_offset(),
1998 reloc
.get_r_addend(), false);
2002 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2004 check_non_pic(object
, r_type
);
2005 if (gsym
->is_from_dynobj()
2006 || gsym
->is_undefined()
2007 || gsym
->is_preemptible())
2008 rela_dyn
->add_global(gsym
, r_type
, output_section
,
2010 reloc
.get_r_offset(),
2011 reloc
.get_r_addend());
2013 rela_dyn
->add_global_relative(gsym
, r_type
,
2014 output_section
, object
,
2016 reloc
.get_r_offset(),
2017 reloc
.get_r_addend(), false);
2023 case elfcpp::R_PPC_PLTREL24
:
2024 case elfcpp::R_POWERPC_REL24
:
2026 if (gsym
->needs_plt_entry()
2027 || (!gsym
->final_value_is_known()
2028 && !(gsym
->is_defined()
2029 && !gsym
->is_from_dynobj()
2030 && !gsym
->is_preemptible())))
2031 target
->make_plt_entry(layout
, gsym
, reloc
, object
);
2032 // Make a dynamic relocation if necessary.
2033 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2035 if (gsym
->may_need_copy_reloc())
2037 target
->copy_reloc(symtab
, layout
, object
,
2038 data_shndx
, output_section
, gsym
,
2043 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2044 check_non_pic(object
, r_type
);
2045 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2046 data_shndx
, reloc
.get_r_offset(),
2047 reloc
.get_r_addend());
2053 case elfcpp::R_PPC_REL16
:
2054 case elfcpp::R_PPC_REL16_LO
:
2055 case elfcpp::R_PPC_REL16_HI
:
2056 case elfcpp::R_PPC_REL16_HA
:
2059 case elfcpp::R_POWERPC_GOT16
:
2060 case elfcpp::R_POWERPC_GOT16_LO
:
2061 case elfcpp::R_POWERPC_GOT16_HI
:
2062 case elfcpp::R_POWERPC_GOT16_HA
:
2064 // The symbol requires a GOT entry.
2065 Output_data_got_powerpc
<size
, big_endian
>* got
;
2067 got
= target
->got_section(symtab
, layout
);
2068 if (gsym
->final_value_is_known())
2069 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2072 // If this symbol is not fully resolved, we need to add a
2073 // dynamic relocation for it.
2074 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2075 if (gsym
->is_from_dynobj()
2076 || gsym
->is_undefined()
2077 || gsym
->is_preemptible())
2078 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2079 elfcpp::R_POWERPC_GLOB_DAT
);
2080 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
2082 unsigned int off
= got
->add_constant(0);
2084 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
2085 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2086 got
, off
, 0, false);
2092 case elfcpp::R_PPC64_TOC
:
2093 case elfcpp::R_PPC64_TOC16
:
2094 case elfcpp::R_PPC64_TOC16_LO
:
2095 case elfcpp::R_PPC64_TOC16_HI
:
2096 case elfcpp::R_PPC64_TOC16_HA
:
2097 case elfcpp::R_PPC64_TOC16_DS
:
2098 case elfcpp::R_PPC64_TOC16_LO_DS
:
2099 // We need a GOT section.
2100 target
->got_section(symtab
, layout
);
2103 case elfcpp::R_POWERPC_GOT_TPREL16
:
2104 case elfcpp::R_POWERPC_TLS
:
2108 // These are relocations which should only be seen by the
2109 // dynamic linker, and should never be seen here.
2110 case elfcpp::R_POWERPC_COPY
:
2111 case elfcpp::R_POWERPC_GLOB_DAT
:
2112 case elfcpp::R_POWERPC_JMP_SLOT
:
2113 case elfcpp::R_POWERPC_RELATIVE
:
2114 case elfcpp::R_POWERPC_DTPMOD
:
2115 gold_error(_("%s: unexpected reloc %u in object file"),
2116 object
->name().c_str(), r_type
);
2120 unsupported_reloc_global(object
, r_type
, gsym
);
2125 // Process relocations for gc.
2127 template<int size
, bool big_endian
>
2129 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
2130 Symbol_table
* symtab
,
2132 Sized_relobj_file
<size
, big_endian
>* object
,
2133 unsigned int data_shndx
,
2135 const unsigned char* prelocs
,
2137 Output_section
* output_section
,
2138 bool needs_special_offset_handling
,
2139 size_t local_symbol_count
,
2140 const unsigned char* plocal_symbols
)
2142 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2143 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2145 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
2146 typename
Target_powerpc::Relocatable_size_for_reloc
>(
2155 needs_special_offset_handling
,
2160 // Scan relocations for a section.
2162 template<int size
, bool big_endian
>
2164 Target_powerpc
<size
, big_endian
>::scan_relocs(
2165 Symbol_table
* symtab
,
2167 Sized_relobj_file
<size
, big_endian
>* object
,
2168 unsigned int data_shndx
,
2169 unsigned int sh_type
,
2170 const unsigned char* prelocs
,
2172 Output_section
* output_section
,
2173 bool needs_special_offset_handling
,
2174 size_t local_symbol_count
,
2175 const unsigned char* plocal_symbols
)
2177 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2178 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2180 if (sh_type
== elfcpp::SHT_REL
)
2182 gold_error(_("%s: unsupported REL reloc section"),
2183 object
->name().c_str());
2189 static Output_data_space
* sdata
;
2191 // Define _SDA_BASE_ at the start of the .sdata section.
2194 // layout->find_output_section(".sdata") == NULL
2195 sdata
= new Output_data_space(4, "** sdata");
2197 = layout
->add_output_section_data(".sdata", 0,
2199 | elfcpp::SHF_WRITE
,
2200 sdata
, ORDER_SMALL_DATA
, false);
2201 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2202 Symbol_table::PREDEFINED
,
2203 os
, 32768, 0, elfcpp::STT_OBJECT
,
2204 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2209 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
2218 needs_special_offset_handling
,
2223 // Finalize the sections.
2225 template<int size
, bool big_endian
>
2227 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
2229 const Input_objects
*,
2232 // Fill in some more dynamic tags.
2233 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2235 : this->plt_
->rel_plt());
2236 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
2237 this->rela_dyn_
, true, size
== 32);
2241 this->got_
->finalize_data_size();
2242 Output_data_dynamic
* odyn
= layout
->dynamic_data();
2243 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
2244 this->got_
, this->got_
->g_o_t());
2247 // Emit any relocs we saved in an attempt to avoid generating COPY
2249 if (this->copy_relocs_
.any_saved_relocs())
2250 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2253 // Perform a relocation.
2255 template<int size
, bool big_endian
>
2257 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
2258 const Relocate_info
<size
, big_endian
>* relinfo
,
2259 Target_powerpc
* target
,
2262 const elfcpp::Rela
<size
, big_endian
>& rela
,
2263 unsigned int r_type
,
2264 const Sized_symbol
<size
>* gsym
,
2265 const Symbol_value
<size
>* psymval
,
2266 unsigned char* view
,
2267 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2268 section_size_type
/* view_size */)
2270 const unsigned int toc_base_offset
= 0x8000;
2271 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
2272 const Powerpc_relobj
<size
, big_endian
>* const object
2273 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
2274 elfcpp::Elf_Xword value
;
2276 if (r_type
== elfcpp::R_POWERPC_GOT16
2277 || r_type
== elfcpp::R_POWERPC_GOT16_LO
2278 || r_type
== elfcpp::R_POWERPC_GOT16_HI
2279 || r_type
== elfcpp::R_POWERPC_GOT16_HA
2280 || r_type
== elfcpp::R_PPC64_GOT16_DS
2281 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
2285 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2286 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2290 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2291 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2292 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2294 value
-= target
->got_section()->g_o_t();
2296 else if (r_type
== elfcpp::R_PPC64_TOC
)
2298 value
= target
->got_section()->address() + toc_base_offset
;
2300 else if (gsym
!= NULL
2301 && (r_type
== elfcpp::R_POWERPC_REL24
2302 || r_type
== elfcpp::R_PPC_PLTREL24
)
2303 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2305 const Output_data_glink
<size
, big_endian
>* glink
;
2307 glink
= target
->glink_section();
2308 unsigned int glink_index
= glink
->find_entry(gsym
, rela
, object
);
2309 value
= glink
->address() + glink_index
* glink
->glink_entry_size();
2313 elfcpp::Elf_Xword addend
= 0;
2314 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
2315 addend
= rela
.get_r_addend();
2316 value
= psymval
->value(object
, addend
);
2321 case elfcpp::R_PPC64_TOC16
:
2322 case elfcpp::R_PPC64_TOC16_LO
:
2323 case elfcpp::R_PPC64_TOC16_HI
:
2324 case elfcpp::R_PPC64_TOC16_HA
:
2325 case elfcpp::R_PPC64_TOC16_DS
:
2326 case elfcpp::R_PPC64_TOC16_LO_DS
:
2327 // Subtract the TOC base address.
2328 value
-= target
->got_section()->address() + toc_base_offset
;
2331 case elfcpp::R_POWERPC_SECTOFF
:
2332 case elfcpp::R_POWERPC_SECTOFF_LO
:
2333 case elfcpp::R_POWERPC_SECTOFF_HI
:
2334 case elfcpp::R_POWERPC_SECTOFF_HA
:
2335 case elfcpp::R_PPC64_SECTOFF_DS
:
2336 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2338 value
-= os
->address();
2347 case elfcpp::R_POWERPC_NONE
:
2348 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
2349 case elfcpp::R_POWERPC_GNU_VTENTRY
:
2352 case elfcpp::R_POWERPC_REL32
:
2353 Reloc::rel32(view
, value
, 0, address
);
2356 case elfcpp::R_POWERPC_REL24
:
2357 case elfcpp::R_PPC_PLTREL24
:
2358 case elfcpp::R_PPC_LOCAL24PC
:
2359 Reloc::rel24(view
, value
, 0, address
);
2362 case elfcpp::R_POWERPC_REL14
:
2363 Reloc::rel14(view
, value
, 0, address
);
2366 case elfcpp::R_PPC64_ADDR64
:
2367 case elfcpp::R_PPC64_TOC
:
2368 Relocate_functions
<size
, big_endian
>::rela64(view
, value
, 0);
2371 case elfcpp::R_POWERPC_ADDR32
:
2372 Relocate_functions
<size
, big_endian
>::rela32(view
, value
, 0);
2375 case elfcpp::R_POWERPC_ADDR16
:
2376 case elfcpp::R_PPC64_TOC16
:
2377 case elfcpp::R_POWERPC_GOT16
:
2378 case elfcpp::R_POWERPC_SECTOFF
:
2379 Reloc::addr16(view
, value
, 0);
2382 case elfcpp::R_POWERPC_ADDR16_LO
:
2383 case elfcpp::R_PPC64_TOC16_LO
:
2384 case elfcpp::R_POWERPC_GOT16_LO
:
2385 case elfcpp::R_POWERPC_SECTOFF_LO
:
2386 Reloc::addr16_lo(view
, value
, 0);
2389 case elfcpp::R_POWERPC_ADDR16_HI
:
2390 case elfcpp::R_PPC64_TOC16_HI
:
2391 case elfcpp::R_POWERPC_GOT16_HI
:
2392 case elfcpp::R_POWERPC_SECTOFF_HI
:
2393 Reloc::addr16_hi(view
, value
, 0);
2396 case elfcpp::R_POWERPC_ADDR16_HA
:
2397 case elfcpp::R_PPC64_TOC16_HA
:
2398 case elfcpp::R_POWERPC_GOT16_HA
:
2399 case elfcpp::R_POWERPC_SECTOFF_HA
:
2400 Reloc::addr16_ha(view
, value
, 0);
2403 case elfcpp::R_PPC_REL16_LO
:
2404 Reloc::rel16_lo(view
, value
, 0, address
);
2407 case elfcpp::R_PPC_REL16_HI
:
2408 Reloc::rel16_hi(view
, value
, 0, address
);
2411 case elfcpp::R_PPC_REL16_HA
:
2412 Reloc::rel16_ha(view
, value
, 0, address
);
2415 case elfcpp::R_PPC64_ADDR16_DS
:
2416 case elfcpp::R_PPC64_ADDR16_LO_DS
:
2417 case elfcpp::R_PPC64_TOC16_DS
:
2418 case elfcpp::R_PPC64_TOC16_LO_DS
:
2419 case elfcpp::R_PPC64_GOT16_DS
:
2420 case elfcpp::R_PPC64_GOT16_LO_DS
:
2421 case elfcpp::R_PPC64_SECTOFF_DS
:
2422 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2423 Reloc::addr16_ds(view
, value
, 0);
2426 case elfcpp::R_POWERPC_COPY
:
2427 case elfcpp::R_POWERPC_GLOB_DAT
:
2428 case elfcpp::R_POWERPC_JMP_SLOT
:
2429 case elfcpp::R_POWERPC_RELATIVE
:
2430 // This is an outstanding tls reloc, which is unexpected when
2432 case elfcpp::R_POWERPC_DTPMOD
:
2433 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2434 _("unexpected reloc %u in object file"),
2439 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2440 _("unsupported reloc %u"),
2448 // Perform a TLS relocation.
2450 template<int size
, bool big_endian
>
2452 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
2453 const Relocate_info
<size
, big_endian
>* relinfo
,
2454 Target_powerpc
<size
, big_endian
>* target
,
2456 const elfcpp::Rela
<size
, big_endian
>& rela
,
2457 unsigned int r_type
,
2458 const Sized_symbol
<size
>* gsym
,
2459 const Symbol_value
<size
>* psymval
,
2460 unsigned char* view
,
2461 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2464 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2465 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
2467 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2468 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
2470 const bool is_final
=
2472 ? !parameters
->options().output_is_position_independent()
2473 : gsym
->final_value_is_known());
2481 // Relocate section data.
2483 template<int size
, bool big_endian
>
2485 Target_powerpc
<size
, big_endian
>::relocate_section(
2486 const Relocate_info
<size
, big_endian
>* relinfo
,
2487 unsigned int sh_type
,
2488 const unsigned char* prelocs
,
2490 Output_section
* output_section
,
2491 bool needs_special_offset_handling
,
2492 unsigned char* view
,
2493 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2494 section_size_type view_size
,
2495 const Reloc_symbol_changes
* reloc_symbol_changes
)
2497 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2498 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
2500 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2502 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
2509 needs_special_offset_handling
,
2513 reloc_symbol_changes
);
2516 class Powerpc_scan_relocatable_reloc
2519 // Return the strategy to use for a local symbol which is not a
2520 // section symbol, given the relocation type.
2521 inline Relocatable_relocs::Reloc_strategy
2522 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
2524 if (r_type
== 0 && r_sym
== 0)
2525 return Relocatable_relocs::RELOC_DISCARD
;
2526 return Relocatable_relocs::RELOC_COPY
;
2529 // Return the strategy to use for a local symbol which is a section
2530 // symbol, given the relocation type.
2531 inline Relocatable_relocs::Reloc_strategy
2532 local_section_strategy(unsigned int, Relobj
*)
2534 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
2537 // Return the strategy to use for a global symbol, given the
2538 // relocation type, the object, and the symbol index.
2539 inline Relocatable_relocs::Reloc_strategy
2540 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
2542 if (r_type
== elfcpp::R_PPC_PLTREL24
)
2543 return Relocatable_relocs::RELOC_SPECIAL
;
2544 return Relocatable_relocs::RELOC_COPY
;
2548 // Scan the relocs during a relocatable link.
2550 template<int size
, bool big_endian
>
2552 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
2553 Symbol_table
* symtab
,
2555 Sized_relobj_file
<size
, big_endian
>* object
,
2556 unsigned int data_shndx
,
2557 unsigned int sh_type
,
2558 const unsigned char* prelocs
,
2560 Output_section
* output_section
,
2561 bool needs_special_offset_handling
,
2562 size_t local_symbol_count
,
2563 const unsigned char* plocal_symbols
,
2564 Relocatable_relocs
* rr
)
2566 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2568 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
2569 Powerpc_scan_relocatable_reloc
>(
2577 needs_special_offset_handling
,
2583 // Relocate a section during a relocatable link.
2585 template<int size
, bool big_endian
>
2587 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
2588 const Relocate_info
<size
, big_endian
>* relinfo
,
2589 unsigned int sh_type
,
2590 const unsigned char* prelocs
,
2592 Output_section
* output_section
,
2593 off_t offset_in_output_section
,
2594 const Relocatable_relocs
* rr
,
2596 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2598 unsigned char* reloc_view
,
2599 section_size_type reloc_view_size
)
2601 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2603 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2604 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
2606 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
2608 const int reloc_size
2609 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2610 const Address invalid_address
= static_cast<Address
>(0) - 1;
2612 Powerpc_relobj
<size
, big_endian
>* const object
2613 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
2614 const unsigned int local_count
= object
->local_symbol_count();
2615 unsigned int got2_shndx
= object
->got2_shndx();
2616 typename
elfcpp::Elf_types
<size
>::Elf_Swxword got2_addend
= 0;
2617 if (got2_shndx
!= 0)
2618 got2_addend
= object
->get_output_section_offset(got2_shndx
);
2620 unsigned char* pwrite
= reloc_view
;
2622 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2624 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
2625 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
2628 Reltype
reloc(prelocs
);
2629 Reltype_write
reloc_write(pwrite
);
2631 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
2632 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2633 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2635 // Get the new symbol index.
2637 unsigned int new_symndx
;
2638 if (r_sym
< local_count
)
2642 case Relocatable_relocs::RELOC_COPY
:
2643 case Relocatable_relocs::RELOC_SPECIAL
:
2644 new_symndx
= object
->symtab_index(r_sym
);
2645 gold_assert(new_symndx
!= -1U);
2648 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
2650 // We are adjusting a section symbol. We need to find
2651 // the symbol table index of the section symbol for
2652 // the output section corresponding to input section
2653 // in which this symbol is defined.
2654 gold_assert(r_sym
< local_count
);
2656 unsigned int shndx
=
2657 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
2658 gold_assert(is_ordinary
);
2659 Output_section
* os
= object
->output_section(shndx
);
2660 gold_assert(os
!= NULL
);
2661 gold_assert(os
->needs_symtab_index());
2662 new_symndx
= os
->symtab_index();
2672 const Symbol
* gsym
= object
->global_symbol(r_sym
);
2673 gold_assert(gsym
!= NULL
);
2674 if (gsym
->is_forwarder())
2675 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
2677 gold_assert(gsym
->has_symtab_index());
2678 new_symndx
= gsym
->symtab_index();
2681 // Get the new offset--the location in the output section where
2682 // this relocation should be applied.
2684 Address offset
= reloc
.get_r_offset();
2686 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
2687 new_offset
= offset
+ offset_in_output_section
;
2690 section_offset_type sot_offset
=
2691 convert_types
<section_offset_type
, Address
>(offset
);
2692 section_offset_type new_sot_offset
=
2693 output_section
->output_offset(object
, relinfo
->data_shndx
,
2695 gold_assert(new_sot_offset
!= -1);
2696 new_offset
= new_sot_offset
;
2699 reloc_write
.put_r_offset(new_offset
);
2700 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
2702 // Handle the reloc addend based on the strategy.
2703 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
2704 addend
= Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::
2705 get_reloc_addend(&reloc
);
2707 if (strategy
== Relocatable_relocs::RELOC_COPY
)
2709 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
2711 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
2713 addend
= psymval
->value(object
, addend
);
2715 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
2717 if (addend
>= 32768)
2718 addend
+= got2_addend
;
2723 Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::
2724 set_reloc_addend(&reloc_write
, addend
);
2726 pwrite
+= reloc_size
;
2729 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
2730 == reloc_view_size
);
2733 // Return the value to use for a dynamic which requires special
2734 // treatment. This is how we support equality comparisons of function
2735 // pointers across shared library boundaries, as described in the
2736 // processor specific ABI supplement.
2738 template<int size
, bool big_endian
>
2740 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
2744 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2745 return this->plt_section()->address() + gsym
->plt_offset();
2751 // The selector for powerpc object files.
2753 template<int size
, bool big_endian
>
2754 class Target_selector_powerpc
: public Target_selector
2757 Target_selector_powerpc()
2758 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
2760 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
2761 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
2763 ? (big_endian
? "elf64ppc" : "elf64lppc")
2764 : (big_endian
? "elf32ppc" : "elf32lppc")))
2768 do_recognize(Input_file
*, off_t
, int machine
, int, int)
2773 if (machine
!= elfcpp::EM_PPC64
)
2778 if (machine
!= elfcpp::EM_PPC
)
2786 return this->instantiate_target();
2790 do_instantiate_target()
2791 { return new Target_powerpc
<size
, big_endian
>(); }
2794 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2795 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2796 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2797 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2799 } // End anonymous namespace.