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
);
129 // Map input .toc section to output .got section.
131 do_output_section_name(const Relobj
*, const char* name
, size_t* plen
) const
133 if (size
== 64 && strcmp(name
, ".toc") == 0)
141 // Finalize the sections.
143 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
145 // Return the value to use for a dynamic which requires special
148 do_dynsym_value(const Symbol
*) const;
150 // Relocate a section.
152 relocate_section(const Relocate_info
<size
, big_endian
>*,
153 unsigned int sh_type
,
154 const unsigned char* prelocs
,
156 Output_section
* output_section
,
157 bool needs_special_offset_handling
,
159 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
160 section_size_type view_size
,
161 const Reloc_symbol_changes
*);
163 // Scan the relocs during a relocatable link.
165 scan_relocatable_relocs(Symbol_table
* symtab
,
167 Sized_relobj_file
<size
, big_endian
>* object
,
168 unsigned int data_shndx
,
169 unsigned int sh_type
,
170 const unsigned char* prelocs
,
172 Output_section
* output_section
,
173 bool needs_special_offset_handling
,
174 size_t local_symbol_count
,
175 const unsigned char* plocal_symbols
,
176 Relocatable_relocs
*);
178 // Relocate a section during a relocatable link.
180 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
181 unsigned int sh_type
,
182 const unsigned char* prelocs
,
184 Output_section
* output_section
,
185 off_t offset_in_output_section
,
186 const Relocatable_relocs
*,
188 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
190 unsigned char* reloc_view
,
191 section_size_type reloc_view_size
);
193 // Return whether SYM is defined by the ABI.
195 do_is_defined_by_abi(const Symbol
* sym
) const
197 return strcmp(sym
->name(), "__tls_get_addr") == 0;
200 // Return the size of the GOT section.
204 gold_assert(this->got_
!= NULL
);
205 return this->got_
->data_size();
208 // Get the PLT section.
209 const Output_data_plt_powerpc
<size
, big_endian
>*
212 gold_assert(this->plt_
!= NULL
);
216 // Get the .glink section.
217 const Output_data_glink
<size
, big_endian
>*
218 glink_section() const
220 gold_assert(this->glink_
!= NULL
);
224 // Get the GOT section.
225 const Output_data_got_powerpc
<size
, big_endian
>*
228 gold_assert(this->got_
!= NULL
);
234 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
235 const elfcpp::Ehdr
<size
, big_endian
>&);
237 // Return the number of entries in the GOT.
239 got_entry_count() const
241 if (this->got_
== NULL
)
243 return this->got_size() / (size
/ 8);
246 // Return the number of entries in the PLT.
248 plt_entry_count() const;
250 // Return the offset of the first non-reserved PLT entry.
252 first_plt_entry_offset() const;
254 // Return the size of each PLT entry.
256 plt_entry_size() const;
260 // The class which scans relocations.
265 : issued_non_pic_error_(false)
269 get_reference_flags(unsigned int r_type
);
272 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
273 Sized_relobj_file
<size
, big_endian
>* object
,
274 unsigned int data_shndx
,
275 Output_section
* output_section
,
276 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
277 const elfcpp::Sym
<size
, big_endian
>& lsym
);
280 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
281 Sized_relobj_file
<size
, big_endian
>* object
,
282 unsigned int data_shndx
,
283 Output_section
* output_section
,
284 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
288 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
290 Sized_relobj_file
<size
, big_endian
>* ,
293 const elfcpp::Rela
<size
, big_endian
>& ,
295 const elfcpp::Sym
<size
, big_endian
>&)
299 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
301 Sized_relobj_file
<size
, big_endian
>* ,
304 const elfcpp::Rela
<size
,
306 unsigned int , Symbol
*)
311 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
312 unsigned int r_type
);
315 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
316 unsigned int r_type
, Symbol
*);
319 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
320 Target_powerpc
* target
);
323 check_non_pic(Relobj
*, unsigned int r_type
);
325 // Whether we have issued an error about a non-PIC compilation.
326 bool issued_non_pic_error_
;
329 // The class which implements relocation.
333 // Do a relocation. Return false if the caller should not issue
334 // any warnings about this relocation.
336 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
337 Output_section
*, size_t relnum
,
338 const elfcpp::Rela
<size
, big_endian
>&,
339 unsigned int r_type
, const Sized_symbol
<size
>*,
340 const Symbol_value
<size
>*,
342 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
346 // Do a TLS relocation.
348 relocate_tls(const Relocate_info
<size
, big_endian
>*,
349 Target_powerpc
* target
,
350 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
351 unsigned int r_type
, const Sized_symbol
<size
>*,
352 const Symbol_value
<size
>*,
354 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
358 // A class which returns the size required for a relocation type,
359 // used while scanning relocs during a relocatable link.
360 class Relocatable_size_for_reloc
364 get_size_for_reloc(unsigned int, Relobj
*)
371 // Adjust TLS relocation type based on the options and whether this
372 // is a local symbol.
373 static tls::Tls_optimization
374 optimize_tls_reloc(bool is_final
, int r_type
);
376 // Get the GOT section, creating it if necessary.
377 Output_data_got_powerpc
<size
, big_endian
>*
378 got_section(Symbol_table
*, Layout
*);
382 make_glink_section(Layout
*);
384 // Create the PLT section.
386 make_plt_section(Layout
*);
388 // Create a PLT entry for a global symbol.
390 make_plt_entry(Layout
*, Symbol
*,
391 const elfcpp::Rela
<size
, big_endian
>&,
392 const Sized_relobj
<size
, big_endian
>* object
);
394 // Create a GOT entry for the TLS module index.
396 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
397 Sized_relobj_file
<size
, big_endian
>* object
);
399 // Get the dynamic reloc section, creating it if necessary.
401 rela_dyn_section(Layout
*);
403 // Copy a relocation against a global symbol.
405 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
406 Sized_relobj_file
<size
, big_endian
>* object
,
407 unsigned int shndx
, Output_section
* output_section
,
408 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
410 this->copy_relocs_
.copy_reloc(symtab
, layout
,
411 symtab
->get_sized_symbol
<size
>(sym
),
412 object
, shndx
, output_section
,
413 reloc
, this->rela_dyn_section(layout
));
416 // Information about this specific target which we pass to the
417 // general Target structure.
418 static Target::Target_info powerpc_info
;
420 // The types of GOT entries needed for this platform.
421 // These values are exposed to the ABI in an incremental link.
422 // Do not renumber existing values without changing the version
423 // number of the .gnu_incremental_inputs section.
426 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
427 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
428 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
431 // The GOT output section.
432 Output_data_got_powerpc
<size
, big_endian
>* got_
;
433 // The PLT output section.
434 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
435 // The .glink output section.
436 Output_data_glink
<size
, big_endian
>* glink_
;
437 // The dynamic reloc output section.
438 Reloc_section
* rela_dyn_
;
439 // Relocs saved to avoid a COPY reloc.
440 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
441 // Space for variables copied with a COPY reloc.
442 Output_data_space
* dynbss_
;
443 // Offset of the GOT entry for the TLS module index;
444 unsigned int got_mod_index_offset_
;
448 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
451 true, // is_big_endian
452 elfcpp::EM_PPC
, // machine_code
453 false, // has_make_symbol
454 false, // has_resolve
455 false, // has_code_fill
456 true, // is_default_stack_executable
457 false, // can_icf_inline_merge_sections
459 "/usr/lib/ld.so.1", // dynamic_linker
460 0x10000000, // default_text_segment_address
461 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
462 4 * 1024, // common_pagesize (overridable by -z common-page-size)
463 false, // isolate_execinstr
465 elfcpp::SHN_UNDEF
, // small_common_shndx
466 elfcpp::SHN_UNDEF
, // large_common_shndx
467 0, // small_common_section_flags
468 0, // large_common_section_flags
469 NULL
, // attributes_section
470 NULL
// attributes_vendor
474 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
477 false, // is_big_endian
478 elfcpp::EM_PPC
, // machine_code
479 false, // has_make_symbol
480 false, // has_resolve
481 false, // has_code_fill
482 true, // is_default_stack_executable
483 false, // can_icf_inline_merge_sections
485 "/usr/lib/ld.so.1", // dynamic_linker
486 0x10000000, // default_text_segment_address
487 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
488 4 * 1024, // common_pagesize (overridable by -z common-page-size)
489 false, // isolate_execinstr
491 elfcpp::SHN_UNDEF
, // small_common_shndx
492 elfcpp::SHN_UNDEF
, // large_common_shndx
493 0, // small_common_section_flags
494 0, // large_common_section_flags
495 NULL
, // attributes_section
496 NULL
// attributes_vendor
500 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
503 true, // is_big_endian
504 elfcpp::EM_PPC64
, // machine_code
505 false, // has_make_symbol
506 false, // has_resolve
507 false, // has_code_fill
508 true, // is_default_stack_executable
509 false, // can_icf_inline_merge_sections
511 "/usr/lib/ld.so.1", // dynamic_linker
512 0x10000000, // default_text_segment_address
513 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
514 8 * 1024, // common_pagesize (overridable by -z common-page-size)
515 false, // isolate_execinstr
517 elfcpp::SHN_UNDEF
, // small_common_shndx
518 elfcpp::SHN_UNDEF
, // large_common_shndx
519 0, // small_common_section_flags
520 0, // large_common_section_flags
521 NULL
, // attributes_section
522 NULL
// attributes_vendor
526 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
529 false, // is_big_endian
530 elfcpp::EM_PPC64
, // machine_code
531 false, // has_make_symbol
532 false, // has_resolve
533 false, // has_code_fill
534 true, // is_default_stack_executable
535 false, // can_icf_inline_merge_sections
537 "/usr/lib/ld.so.1", // dynamic_linker
538 0x10000000, // default_text_segment_address
539 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
540 8 * 1024, // common_pagesize (overridable by -z common-page-size)
541 false, // isolate_execinstr
543 elfcpp::SHN_UNDEF
, // small_common_shndx
544 elfcpp::SHN_UNDEF
, // large_common_shndx
545 0, // small_common_section_flags
546 0, // large_common_section_flags
547 NULL
, // attributes_section
548 NULL
// attributes_vendor
551 template<int size
, bool big_endian
>
552 class Powerpc_relocate_functions
555 // Do a simple RELA relocation
556 template<int valsize
>
558 rela(unsigned char* view
,
559 unsigned int right_shift
,
560 elfcpp::Elf_Xword dst_mask
,
561 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
562 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
564 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
565 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
566 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
567 Valtype reloc
= (value
+ addend
) >> right_shift
;
572 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
575 // Do a simple RELA relocation, unaligned.
576 template<int valsize
>
578 rela_ua(unsigned char* view
,
579 unsigned int right_shift
,
580 elfcpp::Elf_Xword dst_mask
,
581 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
582 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
584 typedef typename
elfcpp::Swap_unaligned
<valsize
,
585 big_endian
>::Valtype Valtype
;
586 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
587 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
588 Valtype reloc
= (value
+ addend
) >> right_shift
;
593 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
596 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
597 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
599 // R_POWERPC_REL32: (Symbol + Addend - Address)
601 rel32(unsigned char* view
,
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
603 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
604 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
605 { This_reloc::pcrela32(view
, value
, addend
, address
); }
607 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
609 rel24(unsigned char* view
,
610 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
611 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
612 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
614 This::template rela
<32>(view
, 0, 0x03fffffc, value
- address
, addend
);
617 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
619 rel14(unsigned char* view
,
620 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
621 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
622 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
624 This::template rela
<32>(view
, 0, 0xfffc, value
- address
, addend
);
627 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
629 addr16(unsigned char* view
,
630 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
631 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
632 { This_reloc::rela16(view
, value
, addend
); }
634 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
636 addr16_ds(unsigned char* view
,
637 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
638 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
640 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
643 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
645 addr16_lo(unsigned char* view
,
646 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
647 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
648 { This_reloc::rela16(view
, value
, addend
); }
650 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
652 addr16_hi(unsigned char* view
,
653 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
654 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
656 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
659 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
660 // final value of the low 16 bits of the
661 // relocation is negative, add one.
663 addr16_ha(unsigned char* view
,
664 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
665 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
667 This::addr16_hi(view
, value
+ 0x8000, addend
);
670 // R_POWERPC_REL16: (Symbol + Addend - Address) & 0xffff
672 rel16(unsigned char* view
,
673 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
674 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
675 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
676 { This_reloc::pcrela16(view
, value
, addend
, address
); }
678 // R_POWERPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
680 rel16_lo(unsigned char* view
,
681 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
682 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
683 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
684 { This_reloc::pcrela16(view
, value
, addend
, address
); }
686 // R_POWERPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
688 rel16_hi(unsigned char* view
,
689 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
690 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
691 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
693 This::template rela
<16>(view
, 16, 0xffff, value
- address
, addend
);
696 // R_POWERPC_REL16_HA: Same as R_POWERPC_REL16_HI except that if the
697 // final value of the low 16 bits of the
698 // relocation is negative, add one.
700 rel16_ha(unsigned char* view
,
701 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
702 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
703 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
705 This::rel16_hi(view
, value
+ 0x8000, addend
, address
);
709 // Stash away the index of .got2 in a relocatable object, if such
712 template<int size
, bool big_endian
>
714 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
715 Read_symbols_data
* sd
)
719 const unsigned char* const pshdrs
= sd
->section_headers
->data();
720 const unsigned char* namesu
= sd
->section_names
->data();
721 const char* names
= reinterpret_cast<const char*>(namesu
);
722 section_size_type names_size
= sd
->section_names_size
;
723 const unsigned char* s
;
725 s
= this->find_shdr(pshdrs
, ".got2", names
, names_size
, NULL
);
728 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
729 this->got2_section_
= ndx
;
732 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
735 // Set up PowerPC target specific relobj.
737 template<int size
, bool big_endian
>
739 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
740 const std::string
& name
,
741 Input_file
* input_file
,
742 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
744 int et
= ehdr
.get_e_type();
745 if (et
== elfcpp::ET_REL
)
747 Powerpc_relobj
<size
, big_endian
>* obj
=
748 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
752 else if (et
== elfcpp::ET_DYN
)
754 Sized_dynobj
<size
, big_endian
>* obj
=
755 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
761 gold_error(_("%s: unsupported ELF file type %d"),
767 template<int size
, bool big_endian
>
768 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
771 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
772 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
774 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
775 : Output_data_got
<size
, big_endian
>(),
776 symtab_(symtab
), layout_(layout
),
777 header_ent_cnt_(size
== 32 ? 3 : 1),
778 header_index_(size
== 32 ? 0x2000 : 0)
783 // Create a new GOT entry and return its offset.
785 add_got_entry(Got_entry got_entry
)
788 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
791 // Create a pair of new GOT entries and return the offset of the first.
793 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
795 this->reserve_ent(2);
796 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
800 // Value of _GLOBAL_OFFSET_TABLE_
804 return this->got_offset(this->header_index_
);
807 // Ensure our GOT has a header.
809 set_final_data_size()
811 if (this->header_ent_cnt_
!= 0)
813 Output_data_got
<size
, big_endian
>::set_final_data_size();
816 // First word of GOT header needs some values that are not
817 // handled by Output_data_got so poke them in here.
818 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
820 do_write(Output_file
* of
)
822 this->replace_constant(this->header_index_
,
824 ? this->layout_
->dynamic_section()->address()
825 : this->address() + 0x8000));
827 Output_data_got
<size
, big_endian
>::do_write(of
);
832 reserve_ent(unsigned int cnt
= 1)
834 if (this->header_ent_cnt_
== 0)
836 if (this->num_entries() + cnt
> this->header_index_
)
843 this->header_ent_cnt_
= 0;
844 this->header_index_
= this->num_entries();
847 Output_data_got
<size
, big_endian
>::add_constant(0);
848 Output_data_got
<size
, big_endian
>::add_constant(0);
849 Output_data_got
<size
, big_endian
>::add_constant(0);
851 // Define _GLOBAL_OFFSET_TABLE_ at the header
852 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
853 Symbol_table::PREDEFINED
,
854 this, this->g_o_t(), 0,
861 Output_data_got
<size
, big_endian
>::add_constant(0);
865 Symbol_table
* symtab_
;
869 unsigned int header_ent_cnt_
;
871 unsigned int header_index_
;
874 // Get the GOT section, creating it if necessary.
876 template<int size
, bool big_endian
>
877 Output_data_got_powerpc
<size
, big_endian
>*
878 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
881 if (this->got_
== NULL
)
883 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
886 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
888 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
889 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
890 this->got_
, ORDER_DATA
, false);
896 // Get the dynamic reloc section, creating it if necessary.
898 template<int size
, bool big_endian
>
899 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
900 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
902 if (this->rela_dyn_
== NULL
)
904 gold_assert(layout
!= NULL
);
905 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
906 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
907 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
908 ORDER_DYNAMIC_RELOCS
, false);
910 return this->rela_dyn_
;
913 // A class to handle the PLT data.
915 template<int size
, bool big_endian
>
916 class Output_data_plt_powerpc
: public Output_section_data_build
919 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
920 size
, big_endian
> Reloc_section
;
922 Output_data_plt_powerpc(Layout
*, Target_powerpc
<size
, big_endian
>*);
924 // Add an entry to the PLT.
928 // Return the .rela.plt section data.
935 // Return the number of PLT entries.
939 return ((this->current_data_size() - initial_plt_entry_size
)
943 // Return the offset of the first non-reserved PLT entry.
945 first_plt_entry_offset()
946 { return initial_plt_entry_size
; }
948 // Return the size of a PLT entry.
951 { return plt_entry_size
; }
955 do_adjust_output_section(Output_section
* os
)
960 // Write to a map file.
962 do_print_to_mapfile(Mapfile
* mapfile
) const
963 { mapfile
->print_output_data(this, _("** PLT")); }
966 // The size of an entry in the PLT.
967 static const int plt_entry_size
= size
== 32 ? 4 : 24;
968 // The size of the first reserved entry.
969 static const int initial_plt_entry_size
= size
== 32 ? 0 : 24;
971 // Write out the PLT data.
973 do_write(Output_file
*);
975 // The reloc section.
977 // Allows access to .glink for do_write.
978 Target_powerpc
<size
, big_endian
>* targ_
;
981 // Create the PLT section.
983 template<int size
, bool big_endian
>
984 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(
986 Target_powerpc
<size
, big_endian
>* targ
)
987 : Output_section_data_build(size
== 32 ? 4 : 8),
990 this->rel_
= new Reloc_section(false);
991 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
992 elfcpp::SHF_ALLOC
, this->rel_
,
993 ORDER_DYNAMIC_PLT_RELOCS
, false);
996 // Add an entry to the PLT.
998 template<int size
, bool big_endian
>
1000 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
1002 if (!gsym
->has_plt_offset())
1004 off_t off
= this->current_data_size();
1007 off
+= initial_plt_entry_size
;
1008 gsym
->set_plt_offset(off
);
1009 gsym
->set_needs_dynsym_entry();
1010 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this, off
, 0);
1011 off
+= plt_entry_size
;
1012 this->set_current_data_size(off
);
1016 static const uint32_t addis_11_11
= 0x3d6b0000;
1017 static const uint32_t addis_11_30
= 0x3d7e0000;
1018 static const uint32_t addis_12_12
= 0x3d8c0000;
1019 static const uint32_t addi_11_11
= 0x396b0000;
1020 static const uint32_t add_0_11_11
= 0x7c0b5a14;
1021 static const uint32_t add_11_0_11
= 0x7d605a14;
1022 static const uint32_t b
= 0x48000000;
1023 static const uint32_t bcl_20_31
= 0x429f0005;
1024 static const uint32_t bctr
= 0x4e800420;
1025 static const uint32_t blrl
= 0x4e800021;
1026 static const uint32_t lis_11
= 0x3d600000;
1027 static const uint32_t lis_12
= 0x3d800000;
1028 static const uint32_t lwzu_0_12
= 0x840c0000;
1029 static const uint32_t lwz_0_12
= 0x800c0000;
1030 static const uint32_t lwz_11_11
= 0x816b0000;
1031 static const uint32_t lwz_11_30
= 0x817e0000;
1032 static const uint32_t lwz_12_12
= 0x818c0000;
1033 static const uint32_t mflr_0
= 0x7c0802a6;
1034 static const uint32_t mflr_12
= 0x7d8802a6;
1035 static const uint32_t mtctr_0
= 0x7c0903a6;
1036 static const uint32_t mtctr_11
= 0x7d6903a6;
1037 static const uint32_t mtlr_0
= 0x7c0803a6;
1038 static const uint32_t nop
= 0x60000000;
1039 static const uint32_t sub_11_11_12
= 0x7d6c5850;
1040 static const uint32_t addis_12_2
= 0x3d820000;
1041 static const uint32_t std_2_1
= 0xf8410000;
1042 static const uint32_t ld_11_12
= 0xe96c0000;
1043 static const uint32_t ld_2_12
= 0xe84c0000;
1044 static const uint32_t addi_12_12
= 0x398c0000;
1045 static const uint32_t ld_11_2
= 0xe9620000;
1046 static const uint32_t addi_2_2
= 0x38420000;
1047 static const uint32_t ld_2_2
= 0xe8420000;
1048 static const uint32_t mflr_11
= 0x7d6802a6;
1049 static const uint32_t ld_2_11
= 0xe84b0000;
1050 static const uint32_t mtlr_12
= 0x7d8803a6;
1051 static const uint32_t add_12_2_11
= 0x7d825a14;
1052 static const uint32_t li_0_0
= 0x38000000;
1053 static const uint32_t lis_0_0
= 0x3c000000;
1054 static const uint32_t ori_0_0_0
= 0x60000000;
1056 // Write out the PLT.
1058 template<int size
, bool big_endian
>
1060 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
1064 const off_t offset
= this->offset();
1065 const section_size_type oview_size
1066 = convert_to_section_size_type(this->data_size());
1067 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1068 unsigned char* pov
= oview
;
1069 unsigned char* endpov
= oview
+ oview_size
;
1071 // The address the .glink branch table
1072 const Output_data_glink
<size
, big_endian
>* glink
1073 = this->targ_
->glink_section();
1074 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
1075 = glink
->address() + glink
->pltresolve();
1077 while (pov
< endpov
)
1079 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
1084 of
->write_output_view(offset
, oview_size
, oview
);
1088 // Create the PLT section.
1090 template<int size
, bool big_endian
>
1092 Target_powerpc
<size
, big_endian
>::make_plt_section(Layout
* layout
)
1094 if (this->plt_
== NULL
)
1096 if (this->glink_
== NULL
)
1097 make_glink_section(layout
);
1099 // Ensure that .rela.dyn always appears before .rela.plt This is
1100 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1101 // needs to include .rela.plt in it's range.
1102 this->rela_dyn_section(layout
);
1104 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
, this);
1105 layout
->add_output_section_data(".plt",
1107 ? elfcpp::SHT_PROGBITS
1108 : elfcpp::SHT_NOBITS
),
1109 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1118 // A class to handle .glink.
1120 template<int size
, bool big_endian
>
1121 class Output_data_glink
: public Output_section_data
1124 Output_data_glink(Target_powerpc
<size
, big_endian
>*);
1128 add_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1129 const Sized_relobj
<size
, big_endian
>*);
1132 find_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1133 const Sized_relobj
<size
, big_endian
>*) const;
1136 glink_entry_size() const
1141 // FIXME: We should be using multiple glink sections for
1142 // stubs to support > 33M applications.
1149 return this->pltresolve_
;
1153 // Write to a map file.
1155 do_print_to_mapfile(Mapfile
* mapfile
) const
1156 { mapfile
->print_output_data(this, _("** glink")); }
1159 static const int pltresolve_size
= 16*4;
1162 set_final_data_size();
1166 do_write(Output_file
*);
1171 Glink_sym_ent(const Symbol
* sym
,
1172 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1173 const Sized_relobj
<size
, big_endian
>* object
)
1174 : sym_(sym
), object_(0), addend_(0)
1177 this->addend_
= reloc
.get_r_addend();
1178 else if (parameters
->options().output_is_position_independent()
1179 && (elfcpp::elf_r_type
<size
>(reloc
.get_r_info())
1180 == elfcpp::R_PPC_PLTREL24
))
1182 this->addend_
= reloc
.get_r_addend();
1183 if (this->addend_
!= 0)
1184 this->object_
= object
;
1189 const Sized_relobj
<size
, big_endian
>* object_
;
1190 unsigned int addend_
;
1192 bool operator==(const Glink_sym_ent
& that
) const
1194 return (this->sym_
== that
.sym_
1195 && this->object_
== that
.object_
1196 && this->addend_
== that
.addend_
);
1200 class Glink_sym_ent_hash
1203 size_t operator()(const Glink_sym_ent
& ent
) const
1205 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
1206 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
1211 // Map sym/object/addend to index.
1212 typedef Unordered_map
<Glink_sym_ent
, unsigned int,
1213 Glink_sym_ent_hash
> Glink_entries
;
1214 Glink_entries glink_entries_
;
1216 // Offset of pltresolve stub (actually, branch table for 32-bit)
1219 // Allows access to .got and .plt for do_write.
1220 Target_powerpc
<size
, big_endian
>* targ_
;
1223 // Create the glink section.
1225 template<int size
, bool big_endian
>
1226 Output_data_glink
<size
, big_endian
>::Output_data_glink(
1227 Target_powerpc
<size
, big_endian
>* targ
)
1228 : Output_section_data(16),
1229 pltresolve_(0), targ_(targ
)
1233 // Add an entry to glink, if we do not already have one for this
1234 // sym/object/addend combo.
1236 template<int size
, bool big_endian
>
1238 Output_data_glink
<size
, big_endian
>::add_entry(
1240 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1241 const Sized_relobj
<size
, big_endian
>* object
)
1243 Glink_sym_ent
ent(gsym
, reloc
, object
);
1244 unsigned int indx
= this->glink_entries_
.size();
1245 this->glink_entries_
.insert(std::make_pair(ent
, indx
));
1248 template<int size
, bool big_endian
>
1250 Output_data_glink
<size
, big_endian
>::find_entry(
1252 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1253 const Sized_relobj
<size
, big_endian
>* object
) const
1255 Glink_sym_ent
ent(gsym
, reloc
, object
);
1256 typename
Glink_entries::const_iterator p
= this->glink_entries_
.find(ent
);
1257 gold_assert(p
!= this->glink_entries_
.end());
1261 template<int size
, bool big_endian
>
1263 Output_data_glink
<size
, big_endian
>::set_final_data_size()
1265 unsigned int count
= this->glink_entries_
.size();
1266 off_t total
= count
;
1273 this->pltresolve_
= total
;
1275 // space for branch table
1276 total
+= 4 * (count
- 1);
1278 total
+= -total
& 15;
1279 total
+= this->pltresolve_size
;
1284 this->pltresolve_
= total
;
1285 total
+= this->pltresolve_size
;
1287 // space for branch table
1290 total
+= 4 * (count
- 0x8000);
1294 this->set_data_size(total
);
1297 static inline uint32_t
1303 static inline uint32_t
1309 static inline uint32_t
1312 return hi(a
+ 0x8000);
1315 template<bool big_endian
>
1317 write_insn(unsigned char *p
, uint32_t v
)
1319 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
1322 // Write out .glink.
1324 template<int size
, bool big_endian
>
1326 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
1328 const off_t off
= this->offset();
1329 const section_size_type oview_size
=
1330 convert_to_section_size_type(this->data_size());
1331 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1334 // The base address of the .plt section.
1335 uint32_t plt_base
= this->targ_
->plt_section()->address();
1337 // The address of _GLOBAL_OFFSET_TABLE_.
1338 const Output_data_got_powerpc
<size
, big_endian
> *got
;
1339 typename
elfcpp::Elf_types
<size
>::Elf_Addr g_o_t
;
1340 got
= this->targ_
->got_section();
1341 g_o_t
= got
->address() + got
->g_o_t();
1345 // Write out call stubs.
1346 typename
Glink_entries::const_iterator g
;
1347 for (g
= this->glink_entries_
.begin();
1348 g
!= this->glink_entries_
.end();
1351 uint64_t plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1352 uint64_t got_addr
= g_o_t
;
1353 uint64_t pltoff
= plt_addr
- got_addr
;
1355 if (pltoff
+ 0x80008000 > 0xffffffff || (pltoff
& 7) != 0)
1356 gold_error(_("%s: linkage table error against `%s'"),
1357 g
->first
.object_
->name().c_str(),
1358 g
->first
.sym_
->demangled_name().c_str());
1360 p
= oview
+ g
->second
* this->glink_entry_size();
1361 if (ha(pltoff
) != 0)
1363 write_insn
<big_endian
>(p
, addis_12_2
+ ha(pltoff
)), p
+= 4;
1364 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1365 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
)), p
+= 4;
1366 if (ha(pltoff
+ 16) != ha(pltoff
))
1368 write_insn
<big_endian
>(p
, addi_12_12
+ l(pltoff
)), p
+= 4;
1371 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1372 write_insn
<big_endian
>(p
, ld_2_12
+ l(pltoff
+ 8)), p
+= 4;
1373 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
+ 16)), p
+= 4;
1374 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1378 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1379 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
)), p
+= 4;
1380 if (ha(pltoff
+ 16) != ha(pltoff
))
1382 write_insn
<big_endian
>(p
, addi_2_2
+ l(pltoff
)), p
+= 4;
1385 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1386 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
+ 16)), p
+= 4;
1387 write_insn
<big_endian
>(p
, ld_2_2
+ l(pltoff
+ 8)), p
+= 4;
1388 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1392 // Write pltresolve stub.
1393 p
= oview
+ this->pltresolve_
;
1394 uint64_t after_bcl
= this->address() + this->pltresolve_
+ 16;
1395 uint64_t pltoff
= plt_base
- after_bcl
;
1397 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
1399 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
1400 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
1401 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
1402 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
1403 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
1404 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
1405 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
1406 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
1407 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1408 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
1409 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1410 while (p
< oview
+ this->pltresolve_
+ this->pltresolve_size
)
1411 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1413 // Write lazy link call stubs.
1415 while (p
< oview
+ oview_size
)
1419 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
1423 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
1424 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
1426 uint16_t branch_off
= this->pltresolve_
+ 8 - (p
- oview
);
1427 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
1433 // Write out call stubs.
1434 typename
Glink_entries::const_iterator g
;
1435 for (g
= this->glink_entries_
.begin();
1436 g
!= this->glink_entries_
.end();
1439 uint32_t plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1442 p
= oview
+ g
->second
* this->glink_entry_size();
1443 if (parameters
->options().output_is_position_independent())
1445 const Powerpc_relobj
<size
, big_endian
>* object
= static_cast
1446 <const Powerpc_relobj
<size
, big_endian
>*>(g
->first
.object_
);
1449 unsigned int got2
= object
->got2_shndx();
1450 got_addr
= (g
->first
.object_
->output_section(got2
)->address()
1451 + g
->first
.object_
->output_section_offset(got2
)
1452 + g
->first
.addend_
);
1457 uint32_t pltoff
= plt_addr
- got_addr
;
1458 if (ha(pltoff
) == 0)
1460 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(pltoff
));
1461 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
1462 write_insn
<big_endian
>(p
+ 8, bctr
);
1466 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(pltoff
));
1467 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(pltoff
));
1468 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1469 write_insn
<big_endian
>(p
+ 12, bctr
);
1474 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
1475 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
1476 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1477 write_insn
<big_endian
>(p
+ 12, bctr
);
1481 // Write out pltresolve branch table.
1482 p
= oview
+ this->pltresolve_
;
1483 unsigned int the_end
= oview_size
- this->pltresolve_size
;
1484 unsigned char *end_p
= oview
+ the_end
;
1485 while (p
< end_p
- 8 * 4)
1486 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
1488 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1490 // Write out pltresolve call stub.
1491 if (parameters
->options().output_is_position_independent())
1493 uint32_t res0_off
= this->pltresolve_
;
1494 uint32_t after_bcl_off
= the_end
+ 12;
1495 uint32_t bcl_res0
= after_bcl_off
- res0_off
;
1497 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
1498 write_insn
<big_endian
>(p
+ 4, mflr_0
);
1499 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
1500 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
1501 write_insn
<big_endian
>(p
+ 16, mflr_12
);
1502 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
1503 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
1505 uint32_t got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
1507 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
1508 if (ha(got_bcl
) == ha(got_bcl
+ 4))
1510 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
1511 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
1515 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
1516 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
1518 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
1519 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
1520 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
1521 write_insn
<big_endian
>(p
+ 52, bctr
);
1522 write_insn
<big_endian
>(p
+ 56, nop
);
1523 write_insn
<big_endian
>(p
+ 60, nop
);
1527 uint32_t res0
= this->pltresolve_
+ this->address();
1529 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
1530 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
1531 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1532 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
1534 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
1535 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
1536 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
1537 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
1538 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1539 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
1541 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
1542 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
1543 write_insn
<big_endian
>(p
+ 32, bctr
);
1544 write_insn
<big_endian
>(p
+ 36, nop
);
1545 write_insn
<big_endian
>(p
+ 40, nop
);
1546 write_insn
<big_endian
>(p
+ 44, nop
);
1547 write_insn
<big_endian
>(p
+ 48, nop
);
1548 write_insn
<big_endian
>(p
+ 52, nop
);
1549 write_insn
<big_endian
>(p
+ 56, nop
);
1550 write_insn
<big_endian
>(p
+ 60, nop
);
1555 of
->write_output_view(off
, oview_size
, oview
);
1558 // Create the glink section.
1560 template<int size
, bool big_endian
>
1562 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
1564 if (this->glink_
== NULL
)
1566 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
1567 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
1568 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1569 this->glink_
, ORDER_TEXT
, false);
1573 // Create a PLT entry for a global symbol.
1575 template<int size
, bool big_endian
>
1577 Target_powerpc
<size
, big_endian
>::make_plt_entry(
1580 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1581 const Sized_relobj
<size
, big_endian
>* object
)
1583 if (this->plt_
== NULL
)
1584 this->make_plt_section(layout
);
1586 this->plt_
->add_entry(gsym
);
1588 this->glink_
->add_entry(gsym
, reloc
, object
);
1591 // Return the number of entries in the PLT.
1593 template<int size
, bool big_endian
>
1595 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
1597 if (this->plt_
== NULL
)
1599 return this->plt_
->entry_count();
1602 // Return the offset of the first non-reserved PLT entry.
1604 template<int size
, bool big_endian
>
1606 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
1608 return Output_data_plt_powerpc
<size
, big_endian
>::first_plt_entry_offset();
1611 // Return the size of each PLT entry.
1613 template<int size
, bool big_endian
>
1615 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
1617 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
1620 // Create a GOT entry for the TLS module index.
1622 template<int size
, bool big_endian
>
1624 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(
1625 Symbol_table
* symtab
,
1627 Sized_relobj_file
<size
, big_endian
>* object
)
1629 if (this->got_mod_index_offset_
== -1U)
1631 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1632 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1633 Output_data_got_powerpc
<size
, big_endian
>* got
;
1634 unsigned int got_offset
;
1636 got
= this->got_section(symtab
, layout
);
1637 got
->reserve_ent(2);
1638 got_offset
= got
->add_constant(0);
1639 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
1641 got
->add_constant(0);
1642 this->got_mod_index_offset_
= got_offset
;
1644 return this->got_mod_index_offset_
;
1647 // Optimize the TLS relocation type based on what we know about the
1648 // symbol. IS_FINAL is true if the final address of this symbol is
1649 // known at link time.
1651 template<int size
, bool big_endian
>
1652 tls::Tls_optimization
1653 Target_powerpc
<size
, big_endian
>::optimize_tls_reloc(bool, int)
1655 // If we are generating a shared library, then we can't do anything
1657 if (parameters
->options().shared())
1658 return tls::TLSOPT_NONE
;
1660 return tls::TLSOPT_NONE
;
1663 // Get the Reference_flags for a particular relocation.
1665 template<int size
, bool big_endian
>
1667 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
1671 case elfcpp::R_POWERPC_NONE
:
1672 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1673 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1674 case elfcpp::R_PPC64_TOC
:
1675 // No symbol reference.
1678 case elfcpp::R_POWERPC_ADDR16
:
1679 case elfcpp::R_POWERPC_ADDR16_LO
:
1680 case elfcpp::R_POWERPC_ADDR16_HI
:
1681 case elfcpp::R_POWERPC_ADDR16_HA
:
1682 case elfcpp::R_POWERPC_ADDR32
:
1683 case elfcpp::R_PPC64_ADDR64
:
1684 return Symbol::ABSOLUTE_REF
;
1686 case elfcpp::R_POWERPC_REL24
:
1687 case elfcpp::R_PPC_LOCAL24PC
:
1688 case elfcpp::R_POWERPC_REL16
:
1689 case elfcpp::R_POWERPC_REL16_LO
:
1690 case elfcpp::R_POWERPC_REL16_HI
:
1691 case elfcpp::R_POWERPC_REL16_HA
:
1692 return Symbol::RELATIVE_REF
;
1694 case elfcpp::R_PPC_PLTREL24
:
1695 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1697 case elfcpp::R_POWERPC_GOT16
:
1698 case elfcpp::R_POWERPC_GOT16_LO
:
1699 case elfcpp::R_POWERPC_GOT16_HI
:
1700 case elfcpp::R_POWERPC_GOT16_HA
:
1701 case elfcpp::R_PPC64_TOC16
:
1702 case elfcpp::R_PPC64_TOC16_LO
:
1703 case elfcpp::R_PPC64_TOC16_HI
:
1704 case elfcpp::R_PPC64_TOC16_HA
:
1705 case elfcpp::R_PPC64_TOC16_DS
:
1706 case elfcpp::R_PPC64_TOC16_LO_DS
:
1708 return Symbol::ABSOLUTE_REF
;
1710 case elfcpp::R_POWERPC_GOT_TPREL16
:
1711 case elfcpp::R_POWERPC_TLS
:
1712 return Symbol::TLS_REF
;
1714 case elfcpp::R_POWERPC_COPY
:
1715 case elfcpp::R_POWERPC_GLOB_DAT
:
1716 case elfcpp::R_POWERPC_JMP_SLOT
:
1717 case elfcpp::R_POWERPC_RELATIVE
:
1718 case elfcpp::R_POWERPC_DTPMOD
:
1720 // Not expected. We will give an error later.
1725 // Report an unsupported relocation against a local symbol.
1727 template<int size
, bool big_endian
>
1729 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1730 Sized_relobj_file
<size
, big_endian
>* object
,
1731 unsigned int r_type
)
1733 gold_error(_("%s: unsupported reloc %u against local symbol"),
1734 object
->name().c_str(), r_type
);
1737 // We are about to emit a dynamic relocation of type R_TYPE. If the
1738 // dynamic linker does not support it, issue an error.
1740 template<int size
, bool big_endian
>
1742 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1743 unsigned int r_type
)
1745 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1747 // These are the relocation types supported by glibc for both 32-bit
1748 // and 64-bit powerpc.
1751 case elfcpp::R_POWERPC_RELATIVE
:
1752 case elfcpp::R_POWERPC_GLOB_DAT
:
1753 case elfcpp::R_POWERPC_DTPMOD
:
1754 case elfcpp::R_POWERPC_DTPREL
:
1755 case elfcpp::R_POWERPC_TPREL
:
1756 case elfcpp::R_POWERPC_JMP_SLOT
:
1757 case elfcpp::R_POWERPC_COPY
:
1758 case elfcpp::R_POWERPC_ADDR32
:
1759 case elfcpp::R_POWERPC_ADDR24
:
1760 case elfcpp::R_POWERPC_REL24
:
1771 // These are the relocation types supported only on 64-bit.
1772 case elfcpp::R_PPC64_ADDR64
:
1773 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1774 case elfcpp::R_PPC64_TPREL16_DS
:
1775 case elfcpp::R_POWERPC_TPREL16
:
1776 case elfcpp::R_POWERPC_TPREL16_LO
:
1777 case elfcpp::R_POWERPC_TPREL16_HI
:
1778 case elfcpp::R_POWERPC_TPREL16_HA
:
1779 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1780 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1781 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1782 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1783 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1784 case elfcpp::R_POWERPC_ADDR16_LO
:
1785 case elfcpp::R_POWERPC_ADDR16_HI
:
1786 case elfcpp::R_POWERPC_ADDR16_HA
:
1787 case elfcpp::R_POWERPC_ADDR30
:
1788 case elfcpp::R_PPC64_UADDR64
:
1789 case elfcpp::R_POWERPC_UADDR32
:
1790 case elfcpp::R_POWERPC_ADDR16
:
1791 case elfcpp::R_POWERPC_UADDR16
:
1792 case elfcpp::R_PPC64_ADDR16_DS
:
1793 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1794 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1795 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1796 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1797 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1798 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1799 case elfcpp::R_POWERPC_REL32
:
1800 case elfcpp::R_PPC64_REL64
:
1811 // These are the relocation types supported only on 32-bit.
1818 // This prevents us from issuing more than one error per reloc
1819 // section. But we can still wind up issuing more than one
1820 // error per object file.
1821 if (this->issued_non_pic_error_
)
1823 gold_assert(parameters
->options().output_is_position_independent());
1824 object
->error(_("requires unsupported dynamic reloc; "
1825 "recompile with -fPIC"));
1826 this->issued_non_pic_error_
= true;
1830 // Scan a relocation for a local symbol.
1832 template<int size
, bool big_endian
>
1834 Target_powerpc
<size
, big_endian
>::Scan::local(
1835 Symbol_table
* symtab
,
1837 Target_powerpc
<size
, big_endian
>* target
,
1838 Sized_relobj_file
<size
, big_endian
>* object
,
1839 unsigned int data_shndx
,
1840 Output_section
* output_section
,
1841 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1842 unsigned int r_type
,
1843 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1847 case elfcpp::R_POWERPC_NONE
:
1848 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1849 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1850 case elfcpp::R_PPC64_TOCSAVE
:
1853 case elfcpp::R_PPC64_ADDR64
:
1854 case elfcpp::R_POWERPC_ADDR32
:
1855 case elfcpp::R_POWERPC_ADDR16_HA
:
1856 case elfcpp::R_POWERPC_ADDR16_LO
:
1857 // If building a shared library (or a position-independent
1858 // executable), we need to create a dynamic relocation for
1860 if (parameters
->options().output_is_position_independent())
1862 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1864 check_non_pic(object
, r_type
);
1865 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1867 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1868 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1869 data_shndx
, reloc
.get_r_offset(),
1870 reloc
.get_r_addend());
1874 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1875 gold_assert(lsym
.get_st_value() == 0);
1876 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1877 output_section
, data_shndx
,
1878 reloc
.get_r_offset(),
1879 reloc
.get_r_addend(), false);
1884 case elfcpp::R_POWERPC_REL24
:
1885 case elfcpp::R_PPC_LOCAL24PC
:
1886 case elfcpp::R_POWERPC_REL32
:
1887 case elfcpp::R_POWERPC_REL16_LO
:
1888 case elfcpp::R_POWERPC_REL16_HA
:
1891 case elfcpp::R_POWERPC_GOT16
:
1892 case elfcpp::R_POWERPC_GOT16_LO
:
1893 case elfcpp::R_POWERPC_GOT16_HI
:
1894 case elfcpp::R_POWERPC_GOT16_HA
:
1896 // The symbol requires a GOT entry.
1897 Output_data_got_powerpc
<size
, big_endian
>* got
;
1900 got
= target
->got_section(symtab
, layout
);
1901 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1903 // If we are generating a shared object, we need to add a
1904 // dynamic relocation for this symbol's GOT entry.
1905 if (parameters
->options().output_is_position_independent())
1907 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1909 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1912 off
= got
->add_constant(0);
1913 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1914 rela_dyn
->add_local_relative(object
, r_sym
,
1915 elfcpp::R_POWERPC_RELATIVE
,
1916 got
, off
, 0, false);
1920 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1924 case elfcpp::R_PPC64_TOC16
:
1925 case elfcpp::R_PPC64_TOC16_LO
:
1926 case elfcpp::R_PPC64_TOC16_HI
:
1927 case elfcpp::R_PPC64_TOC16_HA
:
1928 case elfcpp::R_PPC64_TOC16_DS
:
1929 case elfcpp::R_PPC64_TOC16_LO_DS
:
1930 case elfcpp::R_PPC64_TOC
:
1931 // We need a GOT section.
1932 target
->got_section(symtab
, layout
);
1935 // These are relocations which should only be seen by the
1936 // dynamic linker, and should never be seen here.
1937 case elfcpp::R_POWERPC_COPY
:
1938 case elfcpp::R_POWERPC_GLOB_DAT
:
1939 case elfcpp::R_POWERPC_JMP_SLOT
:
1940 case elfcpp::R_POWERPC_RELATIVE
:
1941 case elfcpp::R_POWERPC_DTPMOD
:
1942 gold_error(_("%s: unexpected reloc %u in object file"),
1943 object
->name().c_str(), r_type
);
1947 unsupported_reloc_local(object
, r_type
);
1952 // Report an unsupported relocation against a global symbol.
1954 template<int size
, bool big_endian
>
1956 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1957 Sized_relobj_file
<size
, big_endian
>* object
,
1958 unsigned int r_type
,
1961 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1962 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1965 // Scan a relocation for a global symbol.
1967 template<int size
, bool big_endian
>
1969 Target_powerpc
<size
, big_endian
>::Scan::global(
1970 Symbol_table
* symtab
,
1972 Target_powerpc
<size
, big_endian
>* target
,
1973 Sized_relobj_file
<size
, big_endian
>* object
,
1974 unsigned int data_shndx
,
1975 Output_section
* output_section
,
1976 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1977 unsigned int r_type
,
1982 case elfcpp::R_POWERPC_NONE
:
1983 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1984 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1985 case elfcpp::R_PPC_LOCAL24PC
:
1988 case elfcpp::R_POWERPC_ADDR16
:
1989 case elfcpp::R_POWERPC_ADDR16_LO
:
1990 case elfcpp::R_POWERPC_ADDR16_HI
:
1991 case elfcpp::R_POWERPC_ADDR16_HA
:
1992 case elfcpp::R_POWERPC_ADDR32
:
1993 case elfcpp::R_PPC64_ADDR64
:
1995 // Make a PLT entry if necessary.
1996 if (gsym
->needs_plt_entry())
1998 target
->make_plt_entry(layout
, gsym
, reloc
, 0);
1999 // Since this is not a PC-relative relocation, we may be
2000 // taking the address of a function. In that case we need to
2001 // set the entry in the dynamic symbol table to the address of
2004 && gsym
->is_from_dynobj() && !parameters
->options().shared())
2005 gsym
->set_needs_dynsym_value();
2007 // Make a dynamic relocation if necessary.
2008 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2010 if (gsym
->may_need_copy_reloc())
2012 target
->copy_reloc(symtab
, layout
, object
,
2013 data_shndx
, output_section
, gsym
, reloc
);
2015 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
2016 || r_type
== elfcpp::R_PPC64_ADDR64
)
2017 && gsym
->can_use_relative_reloc(false))
2019 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2020 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2021 output_section
, object
,
2022 data_shndx
, reloc
.get_r_offset(),
2023 reloc
.get_r_addend(), false);
2027 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2029 check_non_pic(object
, r_type
);
2030 if (gsym
->is_from_dynobj()
2031 || gsym
->is_undefined()
2032 || gsym
->is_preemptible())
2033 rela_dyn
->add_global(gsym
, r_type
, output_section
,
2035 reloc
.get_r_offset(),
2036 reloc
.get_r_addend());
2038 rela_dyn
->add_global_relative(gsym
, r_type
,
2039 output_section
, object
,
2041 reloc
.get_r_offset(),
2042 reloc
.get_r_addend(), false);
2048 case elfcpp::R_PPC_PLTREL24
:
2049 case elfcpp::R_POWERPC_REL24
:
2051 if (gsym
->needs_plt_entry()
2052 || (!gsym
->final_value_is_known()
2053 && !(gsym
->is_defined()
2054 && !gsym
->is_from_dynobj()
2055 && !gsym
->is_preemptible())))
2056 target
->make_plt_entry(layout
, gsym
, reloc
, object
);
2057 // Make a dynamic relocation if necessary.
2058 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2060 if (gsym
->may_need_copy_reloc())
2062 target
->copy_reloc(symtab
, layout
, object
,
2063 data_shndx
, output_section
, gsym
,
2068 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2069 check_non_pic(object
, r_type
);
2070 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2071 data_shndx
, reloc
.get_r_offset(),
2072 reloc
.get_r_addend());
2078 case elfcpp::R_POWERPC_REL16
:
2079 case elfcpp::R_POWERPC_REL16_LO
:
2080 case elfcpp::R_POWERPC_REL16_HI
:
2081 case elfcpp::R_POWERPC_REL16_HA
:
2084 case elfcpp::R_POWERPC_GOT16
:
2085 case elfcpp::R_POWERPC_GOT16_LO
:
2086 case elfcpp::R_POWERPC_GOT16_HI
:
2087 case elfcpp::R_POWERPC_GOT16_HA
:
2089 // The symbol requires a GOT entry.
2090 Output_data_got_powerpc
<size
, big_endian
>* got
;
2092 got
= target
->got_section(symtab
, layout
);
2093 if (gsym
->final_value_is_known())
2094 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2097 // If this symbol is not fully resolved, we need to add a
2098 // dynamic relocation for it.
2099 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2100 if (gsym
->is_from_dynobj()
2101 || gsym
->is_undefined()
2102 || gsym
->is_preemptible())
2103 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2104 elfcpp::R_POWERPC_GLOB_DAT
);
2105 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
2107 unsigned int off
= got
->add_constant(0);
2109 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
2110 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2111 got
, off
, 0, false);
2117 case elfcpp::R_PPC64_TOC
:
2118 case elfcpp::R_PPC64_TOC16
:
2119 case elfcpp::R_PPC64_TOC16_LO
:
2120 case elfcpp::R_PPC64_TOC16_HI
:
2121 case elfcpp::R_PPC64_TOC16_HA
:
2122 case elfcpp::R_PPC64_TOC16_DS
:
2123 case elfcpp::R_PPC64_TOC16_LO_DS
:
2124 // We need a GOT section.
2125 target
->got_section(symtab
, layout
);
2128 case elfcpp::R_POWERPC_GOT_TPREL16
:
2129 case elfcpp::R_POWERPC_TLS
:
2133 // These are relocations which should only be seen by the
2134 // dynamic linker, and should never be seen here.
2135 case elfcpp::R_POWERPC_COPY
:
2136 case elfcpp::R_POWERPC_GLOB_DAT
:
2137 case elfcpp::R_POWERPC_JMP_SLOT
:
2138 case elfcpp::R_POWERPC_RELATIVE
:
2139 case elfcpp::R_POWERPC_DTPMOD
:
2140 gold_error(_("%s: unexpected reloc %u in object file"),
2141 object
->name().c_str(), r_type
);
2145 unsupported_reloc_global(object
, r_type
, gsym
);
2150 // Process relocations for gc.
2152 template<int size
, bool big_endian
>
2154 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
2155 Symbol_table
* symtab
,
2157 Sized_relobj_file
<size
, big_endian
>* object
,
2158 unsigned int data_shndx
,
2160 const unsigned char* prelocs
,
2162 Output_section
* output_section
,
2163 bool needs_special_offset_handling
,
2164 size_t local_symbol_count
,
2165 const unsigned char* plocal_symbols
)
2167 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2168 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2170 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
2171 typename
Target_powerpc::Relocatable_size_for_reloc
>(
2180 needs_special_offset_handling
,
2185 // Scan relocations for a section.
2187 template<int size
, bool big_endian
>
2189 Target_powerpc
<size
, big_endian
>::scan_relocs(
2190 Symbol_table
* symtab
,
2192 Sized_relobj_file
<size
, big_endian
>* object
,
2193 unsigned int data_shndx
,
2194 unsigned int sh_type
,
2195 const unsigned char* prelocs
,
2197 Output_section
* output_section
,
2198 bool needs_special_offset_handling
,
2199 size_t local_symbol_count
,
2200 const unsigned char* plocal_symbols
)
2202 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2203 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2205 if (sh_type
== elfcpp::SHT_REL
)
2207 gold_error(_("%s: unsupported REL reloc section"),
2208 object
->name().c_str());
2214 static Output_data_space
* sdata
;
2216 // Define _SDA_BASE_ at the start of the .sdata section.
2219 // layout->find_output_section(".sdata") == NULL
2220 sdata
= new Output_data_space(4, "** sdata");
2222 = layout
->add_output_section_data(".sdata", 0,
2224 | elfcpp::SHF_WRITE
,
2225 sdata
, ORDER_SMALL_DATA
, false);
2226 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2227 Symbol_table::PREDEFINED
,
2228 os
, 32768, 0, elfcpp::STT_OBJECT
,
2229 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2234 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
2243 needs_special_offset_handling
,
2248 // Finalize the sections.
2250 template<int size
, bool big_endian
>
2252 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
2254 const Input_objects
*,
2257 // Fill in some more dynamic tags.
2258 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2260 : this->plt_
->rel_plt());
2261 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
2262 this->rela_dyn_
, true, size
== 32);
2266 this->got_
->finalize_data_size();
2267 Output_data_dynamic
* odyn
= layout
->dynamic_data();
2268 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
2269 this->got_
, this->got_
->g_o_t());
2272 // Emit any relocs we saved in an attempt to avoid generating COPY
2274 if (this->copy_relocs_
.any_saved_relocs())
2275 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2278 // Perform a relocation.
2280 template<int size
, bool big_endian
>
2282 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
2283 const Relocate_info
<size
, big_endian
>* relinfo
,
2284 Target_powerpc
* target
,
2287 const elfcpp::Rela
<size
, big_endian
>& rela
,
2288 unsigned int r_type
,
2289 const Sized_symbol
<size
>* gsym
,
2290 const Symbol_value
<size
>* psymval
,
2291 unsigned char* view
,
2292 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2293 section_size_type
/* view_size */)
2295 const unsigned int toc_base_offset
= 0x8000;
2296 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
2297 const Powerpc_relobj
<size
, big_endian
>* const object
2298 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
2299 elfcpp::Elf_Xword value
;
2301 if (r_type
== elfcpp::R_POWERPC_GOT16
2302 || r_type
== elfcpp::R_POWERPC_GOT16_LO
2303 || r_type
== elfcpp::R_POWERPC_GOT16_HI
2304 || r_type
== elfcpp::R_POWERPC_GOT16_HA
2305 || r_type
== elfcpp::R_PPC64_GOT16_DS
2306 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
2310 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2311 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2315 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2316 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2317 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2319 value
-= target
->got_section()->g_o_t();
2321 else if (r_type
== elfcpp::R_PPC64_TOC
)
2323 value
= target
->got_section()->address() + toc_base_offset
;
2325 else if (gsym
!= NULL
2326 && (r_type
== elfcpp::R_POWERPC_REL24
2327 || r_type
== elfcpp::R_PPC_PLTREL24
)
2328 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2330 const Output_data_glink
<size
, big_endian
>* glink
;
2332 glink
= target
->glink_section();
2333 unsigned int glink_index
= glink
->find_entry(gsym
, rela
, object
);
2334 value
= glink
->address() + glink_index
* glink
->glink_entry_size();
2338 elfcpp::Elf_Xword addend
= 0;
2339 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
2340 addend
= rela
.get_r_addend();
2341 value
= psymval
->value(object
, addend
);
2346 case elfcpp::R_PPC64_TOC16
:
2347 case elfcpp::R_PPC64_TOC16_LO
:
2348 case elfcpp::R_PPC64_TOC16_HI
:
2349 case elfcpp::R_PPC64_TOC16_HA
:
2350 case elfcpp::R_PPC64_TOC16_DS
:
2351 case elfcpp::R_PPC64_TOC16_LO_DS
:
2352 // Subtract the TOC base address.
2353 value
-= target
->got_section()->address() + toc_base_offset
;
2356 case elfcpp::R_POWERPC_SECTOFF
:
2357 case elfcpp::R_POWERPC_SECTOFF_LO
:
2358 case elfcpp::R_POWERPC_SECTOFF_HI
:
2359 case elfcpp::R_POWERPC_SECTOFF_HA
:
2360 case elfcpp::R_PPC64_SECTOFF_DS
:
2361 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2363 value
-= os
->address();
2372 case elfcpp::R_POWERPC_NONE
:
2373 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
2374 case elfcpp::R_POWERPC_GNU_VTENTRY
:
2377 case elfcpp::R_POWERPC_REL32
:
2378 Reloc::rel32(view
, value
, 0, address
);
2381 case elfcpp::R_POWERPC_REL24
:
2382 case elfcpp::R_PPC_PLTREL24
:
2383 case elfcpp::R_PPC_LOCAL24PC
:
2384 Reloc::rel24(view
, value
, 0, address
);
2387 case elfcpp::R_POWERPC_REL14
:
2388 Reloc::rel14(view
, value
, 0, address
);
2391 case elfcpp::R_PPC64_ADDR64
:
2392 case elfcpp::R_PPC64_TOC
:
2393 Relocate_functions
<size
, big_endian
>::rela64(view
, value
, 0);
2396 case elfcpp::R_POWERPC_ADDR32
:
2397 Relocate_functions
<size
, big_endian
>::rela32(view
, value
, 0);
2400 case elfcpp::R_POWERPC_ADDR16
:
2401 case elfcpp::R_PPC64_TOC16
:
2402 case elfcpp::R_POWERPC_GOT16
:
2403 case elfcpp::R_POWERPC_SECTOFF
:
2404 Reloc::addr16(view
, value
, 0);
2407 case elfcpp::R_POWERPC_ADDR16_LO
:
2408 case elfcpp::R_PPC64_TOC16_LO
:
2409 case elfcpp::R_POWERPC_GOT16_LO
:
2410 case elfcpp::R_POWERPC_SECTOFF_LO
:
2411 Reloc::addr16_lo(view
, value
, 0);
2414 case elfcpp::R_POWERPC_ADDR16_HI
:
2415 case elfcpp::R_PPC64_TOC16_HI
:
2416 case elfcpp::R_POWERPC_GOT16_HI
:
2417 case elfcpp::R_POWERPC_SECTOFF_HI
:
2418 Reloc::addr16_hi(view
, value
, 0);
2421 case elfcpp::R_POWERPC_ADDR16_HA
:
2422 case elfcpp::R_PPC64_TOC16_HA
:
2423 case elfcpp::R_POWERPC_GOT16_HA
:
2424 case elfcpp::R_POWERPC_SECTOFF_HA
:
2425 Reloc::addr16_ha(view
, value
, 0);
2428 case elfcpp::R_POWERPC_REL16_LO
:
2429 Reloc::rel16_lo(view
, value
, 0, address
);
2432 case elfcpp::R_POWERPC_REL16_HI
:
2433 Reloc::rel16_hi(view
, value
, 0, address
);
2436 case elfcpp::R_POWERPC_REL16_HA
:
2437 Reloc::rel16_ha(view
, value
, 0, address
);
2440 case elfcpp::R_PPC64_ADDR16_DS
:
2441 case elfcpp::R_PPC64_ADDR16_LO_DS
:
2442 case elfcpp::R_PPC64_TOC16_DS
:
2443 case elfcpp::R_PPC64_TOC16_LO_DS
:
2444 case elfcpp::R_PPC64_GOT16_DS
:
2445 case elfcpp::R_PPC64_GOT16_LO_DS
:
2446 case elfcpp::R_PPC64_SECTOFF_DS
:
2447 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2448 Reloc::addr16_ds(view
, value
, 0);
2451 case elfcpp::R_POWERPC_COPY
:
2452 case elfcpp::R_POWERPC_GLOB_DAT
:
2453 case elfcpp::R_POWERPC_JMP_SLOT
:
2454 case elfcpp::R_POWERPC_RELATIVE
:
2455 // This is an outstanding tls reloc, which is unexpected when
2457 case elfcpp::R_POWERPC_DTPMOD
:
2458 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2459 _("unexpected reloc %u in object file"),
2463 case elfcpp::R_PPC64_TOCSAVE
:
2464 // For the time being this can be ignored.
2468 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2469 _("unsupported reloc %u"),
2477 // Perform a TLS relocation.
2479 template<int size
, bool big_endian
>
2481 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
2482 const Relocate_info
<size
, big_endian
>* relinfo
,
2483 Target_powerpc
<size
, big_endian
>* target
,
2485 const elfcpp::Rela
<size
, big_endian
>& rela
,
2486 unsigned int r_type
,
2487 const Sized_symbol
<size
>* gsym
,
2488 const Symbol_value
<size
>* psymval
,
2489 unsigned char* view
,
2490 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2493 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2494 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
2496 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2497 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
2499 const bool is_final
=
2501 ? !parameters
->options().output_is_position_independent()
2502 : gsym
->final_value_is_known());
2510 // Relocate section data.
2512 template<int size
, bool big_endian
>
2514 Target_powerpc
<size
, big_endian
>::relocate_section(
2515 const Relocate_info
<size
, big_endian
>* relinfo
,
2516 unsigned int sh_type
,
2517 const unsigned char* prelocs
,
2519 Output_section
* output_section
,
2520 bool needs_special_offset_handling
,
2521 unsigned char* view
,
2522 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2523 section_size_type view_size
,
2524 const Reloc_symbol_changes
* reloc_symbol_changes
)
2526 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2527 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
2529 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2531 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
2538 needs_special_offset_handling
,
2542 reloc_symbol_changes
);
2545 class Powerpc_scan_relocatable_reloc
2548 // Return the strategy to use for a local symbol which is not a
2549 // section symbol, given the relocation type.
2550 inline Relocatable_relocs::Reloc_strategy
2551 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
2553 if (r_type
== 0 && r_sym
== 0)
2554 return Relocatable_relocs::RELOC_DISCARD
;
2555 return Relocatable_relocs::RELOC_COPY
;
2558 // Return the strategy to use for a local symbol which is a section
2559 // symbol, given the relocation type.
2560 inline Relocatable_relocs::Reloc_strategy
2561 local_section_strategy(unsigned int, Relobj
*)
2563 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
2566 // Return the strategy to use for a global symbol, given the
2567 // relocation type, the object, and the symbol index.
2568 inline Relocatable_relocs::Reloc_strategy
2569 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
2571 if (r_type
== elfcpp::R_PPC_PLTREL24
)
2572 return Relocatable_relocs::RELOC_SPECIAL
;
2573 return Relocatable_relocs::RELOC_COPY
;
2577 // Scan the relocs during a relocatable link.
2579 template<int size
, bool big_endian
>
2581 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
2582 Symbol_table
* symtab
,
2584 Sized_relobj_file
<size
, big_endian
>* object
,
2585 unsigned int data_shndx
,
2586 unsigned int sh_type
,
2587 const unsigned char* prelocs
,
2589 Output_section
* output_section
,
2590 bool needs_special_offset_handling
,
2591 size_t local_symbol_count
,
2592 const unsigned char* plocal_symbols
,
2593 Relocatable_relocs
* rr
)
2595 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2597 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
2598 Powerpc_scan_relocatable_reloc
>(
2606 needs_special_offset_handling
,
2612 // Relocate a section during a relocatable link.
2614 template<int size
, bool big_endian
>
2616 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
2617 const Relocate_info
<size
, big_endian
>* relinfo
,
2618 unsigned int sh_type
,
2619 const unsigned char* prelocs
,
2621 Output_section
* output_section
,
2622 off_t offset_in_output_section
,
2623 const Relocatable_relocs
* rr
,
2625 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2627 unsigned char* reloc_view
,
2628 section_size_type reloc_view_size
)
2630 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2632 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2633 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
2635 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
2637 const int reloc_size
2638 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2639 const Address invalid_address
= static_cast<Address
>(0) - 1;
2641 Powerpc_relobj
<size
, big_endian
>* const object
2642 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
2643 const unsigned int local_count
= object
->local_symbol_count();
2644 unsigned int got2_shndx
= object
->got2_shndx();
2645 typename
elfcpp::Elf_types
<size
>::Elf_Swxword got2_addend
= 0;
2646 if (got2_shndx
!= 0)
2647 got2_addend
= object
->get_output_section_offset(got2_shndx
);
2649 unsigned char* pwrite
= reloc_view
;
2651 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2653 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
2654 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
2657 Reltype
reloc(prelocs
);
2658 Reltype_write
reloc_write(pwrite
);
2660 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
2661 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2662 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2664 // Get the new symbol index.
2666 unsigned int new_symndx
;
2667 if (r_sym
< local_count
)
2671 case Relocatable_relocs::RELOC_COPY
:
2672 case Relocatable_relocs::RELOC_SPECIAL
:
2673 new_symndx
= object
->symtab_index(r_sym
);
2674 gold_assert(new_symndx
!= -1U);
2677 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
2679 // We are adjusting a section symbol. We need to find
2680 // the symbol table index of the section symbol for
2681 // the output section corresponding to input section
2682 // in which this symbol is defined.
2683 gold_assert(r_sym
< local_count
);
2685 unsigned int shndx
=
2686 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
2687 gold_assert(is_ordinary
);
2688 Output_section
* os
= object
->output_section(shndx
);
2689 gold_assert(os
!= NULL
);
2690 gold_assert(os
->needs_symtab_index());
2691 new_symndx
= os
->symtab_index();
2701 const Symbol
* gsym
= object
->global_symbol(r_sym
);
2702 gold_assert(gsym
!= NULL
);
2703 if (gsym
->is_forwarder())
2704 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
2706 gold_assert(gsym
->has_symtab_index());
2707 new_symndx
= gsym
->symtab_index();
2710 // Get the new offset--the location in the output section where
2711 // this relocation should be applied.
2713 Address offset
= reloc
.get_r_offset();
2715 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
2716 new_offset
= offset
+ offset_in_output_section
;
2719 section_offset_type sot_offset
=
2720 convert_types
<section_offset_type
, Address
>(offset
);
2721 section_offset_type new_sot_offset
=
2722 output_section
->output_offset(object
, relinfo
->data_shndx
,
2724 gold_assert(new_sot_offset
!= -1);
2725 new_offset
= new_sot_offset
;
2728 reloc_write
.put_r_offset(new_offset
);
2729 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
2731 // Handle the reloc addend based on the strategy.
2732 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
2733 addend
= Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::
2734 get_reloc_addend(&reloc
);
2736 if (strategy
== Relocatable_relocs::RELOC_COPY
)
2738 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
2740 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
2742 addend
= psymval
->value(object
, addend
);
2744 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
2746 if (addend
>= 32768)
2747 addend
+= got2_addend
;
2752 Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::
2753 set_reloc_addend(&reloc_write
, addend
);
2755 pwrite
+= reloc_size
;
2758 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
2759 == reloc_view_size
);
2762 // Return the value to use for a dynamic which requires special
2763 // treatment. This is how we support equality comparisons of function
2764 // pointers across shared library boundaries, as described in the
2765 // processor specific ABI supplement.
2767 template<int size
, bool big_endian
>
2769 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
2773 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2774 return this->plt_section()->address() + gsym
->plt_offset();
2780 // The selector for powerpc object files.
2782 template<int size
, bool big_endian
>
2783 class Target_selector_powerpc
: public Target_selector
2786 Target_selector_powerpc()
2787 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
2789 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
2790 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
2792 ? (big_endian
? "elf64ppc" : "elf64lppc")
2793 : (big_endian
? "elf32ppc" : "elf32lppc")))
2797 do_recognize(Input_file
*, off_t
, int machine
, int, int)
2802 if (machine
!= elfcpp::EM_PPC64
)
2807 if (machine
!= elfcpp::EM_PPC
)
2815 return this->instantiate_target();
2819 do_instantiate_target()
2820 { return new Target_powerpc
<size
, big_endian
>(); }
2823 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2824 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2825 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2826 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2828 } // End anonymous namespace.