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 set_got2_shndx(unsigned int shndx
)
82 this->got2_section_
= shndx
;
88 do_find_special_sections(Read_symbols_data
* sd
);
91 unsigned int got2_section_
;
94 template<int size
, bool big_endian
>
95 class Target_powerpc
: public Sized_target
<size
, big_endian
>
99 Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
102 : Sized_target
<size
, big_endian
>(&powerpc_info
),
103 got_(NULL
), plt_(NULL
), glink_(NULL
), rela_dyn_(NULL
),
104 copy_relocs_(elfcpp::R_POWERPC_COPY
),
105 dynbss_(NULL
), got_mod_index_offset_(-1U)
109 // Process the relocations to determine unreferenced sections for
110 // garbage collection.
112 gc_process_relocs(Symbol_table
* symtab
,
114 Sized_relobj_file
<size
, big_endian
>* object
,
115 unsigned int data_shndx
,
116 unsigned int sh_type
,
117 const unsigned char* prelocs
,
119 Output_section
* output_section
,
120 bool needs_special_offset_handling
,
121 size_t local_symbol_count
,
122 const unsigned char* plocal_symbols
);
124 // Scan the relocations to look for symbol adjustments.
126 scan_relocs(Symbol_table
* symtab
,
128 Sized_relobj_file
<size
, big_endian
>* object
,
129 unsigned int data_shndx
,
130 unsigned int sh_type
,
131 const unsigned char* prelocs
,
133 Output_section
* output_section
,
134 bool needs_special_offset_handling
,
135 size_t local_symbol_count
,
136 const unsigned char* plocal_symbols
);
137 // Finalize the sections.
139 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
141 // Return the value to use for a dynamic which requires special
144 do_dynsym_value(const Symbol
*) const;
146 // Relocate a section.
148 relocate_section(const Relocate_info
<size
, big_endian
>*,
149 unsigned int sh_type
,
150 const unsigned char* prelocs
,
152 Output_section
* output_section
,
153 bool needs_special_offset_handling
,
155 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
156 section_size_type view_size
,
157 const Reloc_symbol_changes
*);
159 // Scan the relocs during a relocatable link.
161 scan_relocatable_relocs(Symbol_table
* symtab
,
163 Sized_relobj_file
<size
, big_endian
>* object
,
164 unsigned int data_shndx
,
165 unsigned int sh_type
,
166 const unsigned char* prelocs
,
168 Output_section
* output_section
,
169 bool needs_special_offset_handling
,
170 size_t local_symbol_count
,
171 const unsigned char* plocal_symbols
,
172 Relocatable_relocs
*);
174 // Relocate a section during a relocatable link.
176 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
177 unsigned int sh_type
,
178 const unsigned char* prelocs
,
180 Output_section
* output_section
,
181 off_t offset_in_output_section
,
182 const Relocatable_relocs
*,
184 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
186 unsigned char* reloc_view
,
187 section_size_type reloc_view_size
);
189 // Return whether SYM is defined by the ABI.
191 do_is_defined_by_abi(const Symbol
* sym
) const
193 return strcmp(sym
->name(), "__tls_get_addr") == 0;
196 // Return the size of the GOT section.
200 gold_assert(this->got_
!= NULL
);
201 return this->got_
->data_size();
204 // Get the PLT section.
205 const Output_data_plt_powerpc
<size
, big_endian
>*
208 gold_assert(this->plt_
!= NULL
);
212 // Get the .glink section.
213 const Output_data_glink
<size
, big_endian
>*
214 glink_section() const
216 gold_assert(this->glink_
!= NULL
);
220 // Get the GOT section.
221 const Output_data_got_powerpc
<size
, big_endian
>*
224 gold_assert(this->got_
!= NULL
);
230 do_make_elf_object(const std::string
&, Input_file
*, off_t
,
231 const elfcpp::Ehdr
<size
, big_endian
>&);
233 // Return the number of entries in the GOT.
235 got_entry_count() const
237 if (this->got_
== NULL
)
239 return this->got_size() / (size
/ 8);
242 // Return the number of entries in the PLT.
244 plt_entry_count() const;
246 // Return the offset of the first non-reserved PLT entry.
248 first_plt_entry_offset() const;
250 // Return the size of each PLT entry.
252 plt_entry_size() const;
256 // The class which scans relocations.
261 : issued_non_pic_error_(false)
265 get_reference_flags(unsigned int r_type
);
268 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
269 Sized_relobj_file
<size
, big_endian
>* object
,
270 unsigned int data_shndx
,
271 Output_section
* output_section
,
272 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
273 const elfcpp::Sym
<size
, big_endian
>& lsym
);
276 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
277 Sized_relobj_file
<size
, big_endian
>* object
,
278 unsigned int data_shndx
,
279 Output_section
* output_section
,
280 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
284 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
286 Sized_relobj_file
<size
, big_endian
>* ,
289 const elfcpp::Rela
<size
, big_endian
>& ,
291 const elfcpp::Sym
<size
, big_endian
>&)
295 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
297 Sized_relobj_file
<size
, big_endian
>* ,
300 const elfcpp::Rela
<size
,
302 unsigned int , Symbol
*)
307 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
308 unsigned int r_type
);
311 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
312 unsigned int r_type
, Symbol
*);
315 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
316 Target_powerpc
* target
);
319 check_non_pic(Relobj
*, unsigned int r_type
);
321 // Whether we have issued an error about a non-PIC compilation.
322 bool issued_non_pic_error_
;
325 // The class which implements relocation.
329 // Do a relocation. Return false if the caller should not issue
330 // any warnings about this relocation.
332 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
333 Output_section
*, size_t relnum
,
334 const elfcpp::Rela
<size
, big_endian
>&,
335 unsigned int r_type
, const Sized_symbol
<size
>*,
336 const Symbol_value
<size
>*,
338 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
342 // Do a TLS relocation.
344 relocate_tls(const Relocate_info
<size
, big_endian
>*,
345 Target_powerpc
* target
,
346 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
347 unsigned int r_type
, const Sized_symbol
<size
>*,
348 const Symbol_value
<size
>*,
350 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
354 // A class which returns the size required for a relocation type,
355 // used while scanning relocs during a relocatable link.
356 class Relocatable_size_for_reloc
360 get_size_for_reloc(unsigned int, Relobj
*)
367 // Adjust TLS relocation type based on the options and whether this
368 // is a local symbol.
369 static tls::Tls_optimization
370 optimize_tls_reloc(bool is_final
, int r_type
);
372 // Get the GOT section, creating it if necessary.
373 Output_data_got_powerpc
<size
, big_endian
>*
374 got_section(Symbol_table
*, Layout
*);
378 make_glink_section(Layout
*);
380 // Create the PLT section.
382 make_plt_section(Layout
*);
384 // Create a PLT entry for a global symbol.
386 make_plt_entry(Layout
*, Symbol
*,
387 const elfcpp::Rela
<size
, big_endian
>&,
388 const Sized_relobj
<size
, big_endian
>* object
);
390 // Create a GOT entry for the TLS module index.
392 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
393 Sized_relobj_file
<size
, big_endian
>* object
);
395 // Get the dynamic reloc section, creating it if necessary.
397 rela_dyn_section(Layout
*);
399 // Copy a relocation against a global symbol.
401 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
402 Sized_relobj_file
<size
, big_endian
>* object
,
403 unsigned int shndx
, Output_section
* output_section
,
404 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
406 this->copy_relocs_
.copy_reloc(symtab
, layout
,
407 symtab
->get_sized_symbol
<size
>(sym
),
408 object
, shndx
, output_section
,
409 reloc
, this->rela_dyn_section(layout
));
412 // Information about this specific target which we pass to the
413 // general Target structure.
414 static Target::Target_info powerpc_info
;
416 // The types of GOT entries needed for this platform.
417 // These values are exposed to the ABI in an incremental link.
418 // Do not renumber existing values without changing the version
419 // number of the .gnu_incremental_inputs section.
422 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
423 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
424 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
427 // The GOT output section.
428 Output_data_got_powerpc
<size
, big_endian
>* got_
;
429 // The PLT output section.
430 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
431 // The .glink output section.
432 Output_data_glink
<size
, big_endian
>* glink_
;
433 // The dynamic reloc output section.
434 Reloc_section
* rela_dyn_
;
435 // Relocs saved to avoid a COPY reloc.
436 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
437 // Space for variables copied with a COPY reloc.
438 Output_data_space
* dynbss_
;
439 // Offset of the GOT entry for the TLS module index;
440 unsigned int got_mod_index_offset_
;
444 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
447 true, // is_big_endian
448 elfcpp::EM_PPC
, // machine_code
449 false, // has_make_symbol
450 false, // has_resolve
451 false, // has_code_fill
452 true, // is_default_stack_executable
453 false, // can_icf_inline_merge_sections
455 "/usr/lib/ld.so.1", // dynamic_linker
456 0x10000000, // default_text_segment_address
457 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
458 4 * 1024, // common_pagesize (overridable by -z common-page-size)
459 false, // isolate_execinstr
461 elfcpp::SHN_UNDEF
, // small_common_shndx
462 elfcpp::SHN_UNDEF
, // large_common_shndx
463 0, // small_common_section_flags
464 0, // large_common_section_flags
465 NULL
, // attributes_section
466 NULL
// attributes_vendor
470 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
473 false, // is_big_endian
474 elfcpp::EM_PPC
, // machine_code
475 false, // has_make_symbol
476 false, // has_resolve
477 false, // has_code_fill
478 true, // is_default_stack_executable
479 false, // can_icf_inline_merge_sections
481 "/usr/lib/ld.so.1", // dynamic_linker
482 0x10000000, // default_text_segment_address
483 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
484 4 * 1024, // common_pagesize (overridable by -z common-page-size)
485 false, // isolate_execinstr
487 elfcpp::SHN_UNDEF
, // small_common_shndx
488 elfcpp::SHN_UNDEF
, // large_common_shndx
489 0, // small_common_section_flags
490 0, // large_common_section_flags
491 NULL
, // attributes_section
492 NULL
// attributes_vendor
496 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
499 true, // is_big_endian
500 elfcpp::EM_PPC64
, // machine_code
501 false, // has_make_symbol
502 false, // has_resolve
503 false, // has_code_fill
504 true, // is_default_stack_executable
505 false, // can_icf_inline_merge_sections
507 "/usr/lib/ld.so.1", // dynamic_linker
508 0x10000000, // default_text_segment_address
509 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
510 8 * 1024, // common_pagesize (overridable by -z common-page-size)
511 false, // isolate_execinstr
513 elfcpp::SHN_UNDEF
, // small_common_shndx
514 elfcpp::SHN_UNDEF
, // large_common_shndx
515 0, // small_common_section_flags
516 0, // large_common_section_flags
517 NULL
, // attributes_section
518 NULL
// attributes_vendor
522 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
525 false, // is_big_endian
526 elfcpp::EM_PPC64
, // machine_code
527 false, // has_make_symbol
528 false, // has_resolve
529 false, // has_code_fill
530 true, // is_default_stack_executable
531 false, // can_icf_inline_merge_sections
533 "/usr/lib/ld.so.1", // dynamic_linker
534 0x10000000, // default_text_segment_address
535 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
536 8 * 1024, // common_pagesize (overridable by -z common-page-size)
537 false, // isolate_execinstr
539 elfcpp::SHN_UNDEF
, // small_common_shndx
540 elfcpp::SHN_UNDEF
, // large_common_shndx
541 0, // small_common_section_flags
542 0, // large_common_section_flags
543 NULL
, // attributes_section
544 NULL
// attributes_vendor
547 template<int size
, bool big_endian
>
548 class Powerpc_relocate_functions
551 // Do a simple RELA relocation
552 template<int valsize
>
554 rela(unsigned char* view
,
555 unsigned int right_shift
,
556 elfcpp::Elf_Xword dst_mask
,
557 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
558 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
560 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
561 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
562 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
563 Valtype reloc
= (value
+ addend
) >> right_shift
;
568 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
571 // Do a simple RELA relocation, unaligned.
572 template<int valsize
>
574 rela_ua(unsigned char* view
,
575 unsigned int right_shift
,
576 elfcpp::Elf_Xword dst_mask
,
577 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
578 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
580 typedef typename
elfcpp::Swap_unaligned
<valsize
,
581 big_endian
>::Valtype Valtype
;
582 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
583 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
584 Valtype reloc
= (value
+ addend
) >> right_shift
;
589 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
592 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
593 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
595 // R_POWERPC_REL32: (Symbol + Addend - Address)
597 rel32(unsigned char* view
,
598 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
599 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
600 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
601 { This_reloc::pcrela32(view
, value
, addend
, address
); }
603 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
605 rel24(unsigned char* view
,
606 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
607 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
608 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
610 This::template rela
<32>(view
, 0, 0x03fffffc, value
- address
, addend
);
613 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
615 rel14(unsigned char* view
,
616 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
617 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
618 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
620 This::template rela
<32>(view
, 0, 0xfffc, value
- address
, addend
);
623 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
625 addr16(unsigned char* view
,
626 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
627 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
628 { This_reloc::rela16(view
, value
, addend
); }
630 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
632 addr16_ds(unsigned char* view
,
633 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
634 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
636 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
639 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
641 addr16_lo(unsigned char* view
,
642 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
643 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
644 { This_reloc::rela16(view
, value
, addend
); }
646 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
648 addr16_hi(unsigned char* view
,
649 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
650 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
652 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
655 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
656 // final value of the low 16 bits of the
657 // relocation is negative, add one.
659 addr16_ha(unsigned char* view
,
660 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
661 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
663 This::addr16_hi(view
, value
+ 0x8000, addend
);
666 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
668 rel16(unsigned char* view
,
669 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
670 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
671 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
672 { This_reloc::pcrela16(view
, value
, addend
, address
); }
674 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
676 rel16_lo(unsigned char* view
,
677 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
678 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
679 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
680 { This_reloc::pcrela16(view
, value
, addend
, address
); }
682 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
684 rel16_hi(unsigned char* view
,
685 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
686 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
687 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
689 This::template rela
<16>(view
, 16, 0xffff, value
- address
, addend
);
692 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
693 // final value of the low 16 bits of the
694 // relocation is negative, add one.
696 rel16_ha(unsigned char* view
,
697 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
698 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
699 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
701 This::rel16_hi(view
, value
+ 0x8000, addend
, address
);
705 // Stash away the index of .got2 in a relocatable object, if such
708 template<int size
, bool big_endian
>
710 Powerpc_relobj
<size
, big_endian
>::do_find_special_sections(
711 Read_symbols_data
* sd
)
715 const unsigned char* const pshdrs
= sd
->section_headers
->data();
716 const unsigned char* namesu
= sd
->section_names
->data();
717 const char* names
= reinterpret_cast<const char*>(namesu
);
718 section_size_type names_size
= sd
->section_names_size
;
719 const unsigned char* s
;
721 s
= this->find_shdr(pshdrs
, ".got2", names
, names_size
, NULL
);
724 unsigned int ndx
= (s
- pshdrs
) / elfcpp::Elf_sizes
<size
>::shdr_size
;
725 this->set_got2_shndx(ndx
);
728 return Sized_relobj_file
<size
, big_endian
>::do_find_special_sections(sd
);
731 // Set up PowerPC target specific relobj.
733 template<int size
, bool big_endian
>
735 Target_powerpc
<size
, big_endian
>::do_make_elf_object(
736 const std::string
& name
,
737 Input_file
* input_file
,
738 off_t offset
, const elfcpp::Ehdr
<size
, big_endian
>& ehdr
)
740 int et
= ehdr
.get_e_type();
741 if (et
== elfcpp::ET_REL
)
743 Powerpc_relobj
<size
, big_endian
>* obj
=
744 new Powerpc_relobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
748 else if (et
== elfcpp::ET_DYN
)
750 Sized_dynobj
<size
, big_endian
>* obj
=
751 new Sized_dynobj
<size
, big_endian
>(name
, input_file
, offset
, ehdr
);
757 gold_error(_("%s: unsupported ELF file type %d"),
763 template<int size
, bool big_endian
>
764 class Output_data_got_powerpc
: public Output_data_got
<size
, big_endian
>
767 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Valtype
;
768 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Rela_dyn
;
770 Output_data_got_powerpc(Symbol_table
* symtab
, Layout
* layout
)
771 : Output_data_got
<size
, big_endian
>(),
772 symtab_(symtab
), layout_(layout
),
773 header_ent_cnt_(size
== 32 ? 3 : 1),
774 header_index_(size
== 32 ? 0x2000 : 0)
779 // Create a new GOT entry and return its offset.
781 add_got_entry(Got_entry got_entry
)
784 return Output_data_got
<size
, big_endian
>::add_got_entry(got_entry
);
787 // Create a pair of new GOT entries and return the offset of the first.
789 add_got_entry_pair(Got_entry got_entry_1
, Got_entry got_entry_2
)
791 this->reserve_ent(2);
792 return Output_data_got
<size
, big_endian
>::add_got_entry_pair(got_entry_1
,
796 // Value of _GLOBAL_OFFSET_TABLE_
800 return this->got_offset(this->header_index_
);
803 // Ensure our GOT has a header.
805 set_final_data_size()
807 if (this->header_ent_cnt_
!= 0)
809 Output_data_got
<size
, big_endian
>::set_final_data_size();
812 // First word of GOT header needs some values that are not
813 // handled by Output_data_got so poke them in here.
814 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
816 do_write(Output_file
* of
)
818 replace_constant(this->header_index_
,
820 ? this->layout_
->dynamic_section()->address()
821 : this->address() + 0x8000));
823 Output_data_got
<size
, big_endian
>::do_write(of
);
828 reserve_ent(unsigned int cnt
= 1)
830 if (this->header_ent_cnt_
== 0)
832 if (this->num_entries() + cnt
> this->header_index_
)
839 this->header_ent_cnt_
= 0;
840 this->header_index_
= this->num_entries();
843 Output_data_got
<size
, big_endian
>::add_constant(0);
844 Output_data_got
<size
, big_endian
>::add_constant(0);
845 Output_data_got
<size
, big_endian
>::add_constant(0);
847 // Define _GLOBAL_OFFSET_TABLE_ at the header
848 this->symtab_
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
849 Symbol_table::PREDEFINED
,
850 this, this->g_o_t(), 0,
857 Output_data_got
<size
, big_endian
>::add_constant(0);
861 Symbol_table
* symtab_
;
865 unsigned int header_ent_cnt_
;
867 unsigned int header_index_
;
870 // Get the GOT section, creating it if necessary.
872 template<int size
, bool big_endian
>
873 Output_data_got_powerpc
<size
, big_endian
>*
874 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
877 if (this->got_
== NULL
)
879 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
882 = new Output_data_got_powerpc
<size
, big_endian
>(symtab
, layout
);
884 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
885 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
886 this->got_
, ORDER_DATA
, false);
892 // Get the dynamic reloc section, creating it if necessary.
894 template<int size
, bool big_endian
>
895 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
896 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
898 if (this->rela_dyn_
== NULL
)
900 gold_assert(layout
!= NULL
);
901 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
902 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
903 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
904 ORDER_DYNAMIC_RELOCS
, false);
906 return this->rela_dyn_
;
909 // A class to handle the PLT data.
911 template<int size
, bool big_endian
>
912 class Output_data_plt_powerpc
: public Output_section_data_build
915 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
916 size
, big_endian
> Reloc_section
;
918 Output_data_plt_powerpc(Layout
*, Target_powerpc
<size
, big_endian
>*);
920 // Add an entry to the PLT.
924 // Return the .rela.plt section data.
931 // Return the number of PLT entries.
935 return ((this->current_data_size() - initial_plt_entry_size
)
939 // Return the offset of the first non-reserved PLT entry.
941 first_plt_entry_offset()
942 { return initial_plt_entry_size
; }
944 // Return the size of a PLT entry.
947 { return plt_entry_size
; }
951 do_adjust_output_section(Output_section
* os
)
957 // The size of an entry in the PLT.
958 static const int plt_entry_size
= size
== 32 ? 4 : 24;
959 // The size of the first reserved entry.
960 static const int initial_plt_entry_size
= size
== 32 ? 0 : 24;
962 // Write out the PLT data.
964 do_write(Output_file
*);
966 // The reloc section.
968 // Allows access to .glink for do_write.
969 Target_powerpc
<size
, big_endian
>* targ_
;
972 // Create the PLT section.
974 template<int size
, bool big_endian
>
975 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(
977 Target_powerpc
<size
, big_endian
>* targ
)
978 : Output_section_data_build(size
== 32 ? 4 : 8),
981 this->rel_
= new Reloc_section(false);
982 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
983 elfcpp::SHF_ALLOC
, this->rel_
,
984 ORDER_DYNAMIC_PLT_RELOCS
, false);
987 // Add an entry to the PLT.
989 template<int size
, bool big_endian
>
991 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
993 if (!gsym
->has_plt_offset())
995 off_t off
= this->current_data_size();
998 off
+= initial_plt_entry_size
;
999 gsym
->set_plt_offset(off
);
1000 gsym
->set_needs_dynsym_entry();
1001 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this, off
, 0);
1002 off
+= plt_entry_size
;
1003 this->set_current_data_size(off
);
1007 static const uint32_t addis_11_11
= 0x3d6b0000;
1008 static const uint32_t addis_11_30
= 0x3d7e0000;
1009 static const uint32_t addis_12_12
= 0x3d8c0000;
1010 static const uint32_t addi_11_11
= 0x396b0000;
1011 static const uint32_t add_0_11_11
= 0x7c0b5a14;
1012 static const uint32_t add_11_0_11
= 0x7d605a14;
1013 static const uint32_t b
= 0x48000000;
1014 static const uint32_t bcl_20_31
= 0x429f0005;
1015 static const uint32_t bctr
= 0x4e800420;
1016 static const uint32_t blrl
= 0x4e800021;
1017 static const uint32_t lis_11
= 0x3d600000;
1018 static const uint32_t lis_12
= 0x3d800000;
1019 static const uint32_t lwzu_0_12
= 0x840c0000;
1020 static const uint32_t lwz_0_12
= 0x800c0000;
1021 static const uint32_t lwz_11_11
= 0x816b0000;
1022 static const uint32_t lwz_11_30
= 0x817e0000;
1023 static const uint32_t lwz_12_12
= 0x818c0000;
1024 static const uint32_t mflr_0
= 0x7c0802a6;
1025 static const uint32_t mflr_12
= 0x7d8802a6;
1026 static const uint32_t mtctr_0
= 0x7c0903a6;
1027 static const uint32_t mtctr_11
= 0x7d6903a6;
1028 static const uint32_t mtlr_0
= 0x7c0803a6;
1029 static const uint32_t nop
= 0x60000000;
1030 static const uint32_t sub_11_11_12
= 0x7d6c5850;
1031 static const uint32_t addis_12_2
= 0x3d820000;
1032 static const uint32_t std_2_1
= 0xf8410000;
1033 static const uint32_t ld_11_12
= 0xe96c0000;
1034 static const uint32_t ld_2_12
= 0xe84c0000;
1035 static const uint32_t addi_12_12
= 0x398c0000;
1036 static const uint32_t ld_11_2
= 0xe9620000;
1037 static const uint32_t addi_2_2
= 0x38420000;
1038 static const uint32_t ld_2_2
= 0xe8420000;
1039 static const uint32_t mflr_11
= 0x7d6802a6;
1040 static const uint32_t ld_2_11
= 0xe84b0000;
1041 static const uint32_t mtlr_12
= 0x7d8803a6;
1042 static const uint32_t add_12_2_11
= 0x7d825a14;
1043 static const uint32_t li_0_0
= 0x38000000;
1044 static const uint32_t lis_0_0
= 0x3c000000;
1045 static const uint32_t ori_0_0_0
= 0x60000000;
1047 // Write out the PLT.
1049 template<int size
, bool big_endian
>
1051 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
1055 const off_t offset
= this->offset();
1056 const section_size_type oview_size
1057 = convert_to_section_size_type(this->data_size());
1058 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1059 unsigned char* pov
= oview
;
1060 unsigned char* endpov
= oview
+ oview_size
;
1062 // The address the .glink branch table
1063 const Output_data_glink
<size
, big_endian
>* glink
1064 = this->targ_
->glink_section();
1065 elfcpp::Elf_types
<32>::Elf_Addr branch_tab
1066 = glink
->address() + glink
->pltresolve();
1068 while (pov
< endpov
)
1070 elfcpp::Swap
<32, big_endian
>::writeval(pov
, branch_tab
);
1075 of
->write_output_view(offset
, oview_size
, oview
);
1079 // Create the PLT section.
1081 template<int size
, bool big_endian
>
1083 Target_powerpc
<size
, big_endian
>::make_plt_section(Layout
* layout
)
1085 if (this->plt_
== NULL
)
1087 if (this->glink_
== NULL
)
1088 make_glink_section(layout
);
1090 // Ensure that .rela.dyn always appears before .rela.plt This is
1091 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1092 // needs to include .rela.plt in it's range.
1093 this->rela_dyn_section(layout
);
1095 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
, this);
1096 layout
->add_output_section_data(".plt",
1098 ? elfcpp::SHT_PROGBITS
1099 : elfcpp::SHT_NOBITS
),
1100 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1109 // A class to handle .glink.
1111 template<int size
, bool big_endian
>
1112 class Output_data_glink
: public Output_section_data
1115 Output_data_glink(Target_powerpc
<size
, big_endian
>*);
1119 add_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1120 const Sized_relobj
<size
, big_endian
>*, unsigned int);
1123 find_entry(const Symbol
*, const elfcpp::Rela
<size
, big_endian
>&,
1124 const Sized_relobj
<size
, big_endian
>*, unsigned int) const;
1127 glink_entry_size() const
1132 // FIXME: We should be using multiple glink sections for
1133 // stubs to support > 33M applications.
1140 return this->pltresolve_
;
1144 static const int pltresolve_size
= 16*4;
1147 set_final_data_size();
1151 do_write(Output_file
*);
1153 struct Glink_sym_ent
1155 Glink_sym_ent(const Symbol
*sym
,
1156 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1157 const Sized_relobj
<size
, big_endian
>* object
,
1159 : sym_(sym
), object_(0), shndx_(0), addend_(0)
1162 this->addend_
= reloc
.get_r_addend();
1163 else if (parameters
->options().output_is_position_independent())
1165 if (object
!= NULL
&& shndx
!= 0)
1166 this->addend_
= reloc
.get_r_addend();
1167 if (this->addend_
!= 0)
1169 this->object_
= object
;
1170 this->shndx_
= shndx
;
1176 const Sized_relobj
<size
, big_endian
>* object_
;
1177 unsigned int shndx_
;
1178 unsigned int addend_
;
1180 bool operator==(const Glink_sym_ent
& that
) const
1182 return (this->sym_
== that
.sym_
1183 && this->object_
== that
.object_
1184 && this->shndx_
== that
.shndx_
1185 && this->addend_
== that
.addend_
);
1189 struct Glink_sym_ent_hash
1191 size_t operator()(const Glink_sym_ent
& ent
) const
1193 return (reinterpret_cast<uintptr_t>(ent
.sym_
)
1194 ^ reinterpret_cast<uintptr_t>(ent
.object_
)
1200 // Set of sym/shndx/addend entries.
1201 typedef Unordered_map
<Glink_sym_ent
, unsigned int,
1202 Glink_sym_ent_hash
> Glink_entries
;
1203 Glink_entries glink_entries_
;
1205 // Offset of pltresolve stub (actually, branch table for 32-bit)
1208 // Allows access to .got and .plt for do_write.
1209 Target_powerpc
<size
, big_endian
>* targ_
;
1212 // Create the glink section.
1214 template<int size
, bool big_endian
>
1215 Output_data_glink
<size
, big_endian
>::Output_data_glink(
1216 Target_powerpc
<size
, big_endian
>* targ
)
1217 : Output_section_data(16),
1218 pltresolve_(0), targ_(targ
)
1222 // Add an entry to glink, if we do not already have one for this
1223 // sym/addend/shndx combo.
1225 template<int size
, bool big_endian
>
1227 Output_data_glink
<size
, big_endian
>::add_entry(
1229 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1230 const Sized_relobj
<size
, big_endian
>* object
,
1233 Glink_sym_ent
ent(gsym
, reloc
, object
, shndx
);
1234 unsigned int indx
= this->glink_entries_
.size();
1235 this->glink_entries_
.insert(std::make_pair(ent
, indx
));
1238 template<int size
, bool big_endian
>
1240 Output_data_glink
<size
, big_endian
>::find_entry(
1242 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1243 const Sized_relobj
<size
, big_endian
>* object
,
1244 unsigned int shndx
) const
1246 Glink_sym_ent
ent(gsym
, reloc
, object
, shndx
);
1247 typename
Glink_entries::const_iterator p
= this->glink_entries_
.find(ent
);
1248 gold_assert(p
!= this->glink_entries_
.end());
1252 template<int size
, bool big_endian
>
1254 Output_data_glink
<size
, big_endian
>::set_final_data_size()
1256 unsigned int count
= this->glink_entries_
.size();
1257 off_t total
= count
;
1264 this->pltresolve_
= total
;
1266 // space for branch table
1267 total
+= 4 * (count
- 1);
1269 total
+= -total
& 15;
1270 total
+= this->pltresolve_size
;
1275 this->pltresolve_
= total
;
1276 total
+= this->pltresolve_size
;
1278 // space for branch table
1281 total
+= 4 * (count
- 0x8000);
1285 this->set_data_size(total
);
1288 static inline uint32_t
1294 static inline uint32_t
1300 static inline uint32_t
1303 return hi(a
+ 0x8000);
1306 template<bool big_endian
>
1308 write_insn(unsigned char *p
, uint32_t v
)
1310 elfcpp::Swap
<32, big_endian
>::writeval(p
, v
);
1313 // Write out .glink.
1315 template<int size
, bool big_endian
>
1317 Output_data_glink
<size
, big_endian
>::do_write(Output_file
* of
)
1319 const off_t off
= this->offset();
1320 const section_size_type oview_size
=
1321 convert_to_section_size_type(this->data_size());
1322 unsigned char* const oview
= of
->get_output_view(off
, oview_size
);
1325 // The base address of the .plt section.
1326 uint32_t plt_base
= this->targ_
->plt_section()->address();
1328 // The address of _GLOBAL_OFFSET_TABLE_.
1329 const Output_data_got_powerpc
<size
, big_endian
> *got
;
1330 typename
elfcpp::Elf_types
<size
>::Elf_Addr g_o_t
;
1331 got
= this->targ_
->got_section();
1332 g_o_t
= got
->address() + got
->g_o_t();
1336 // Write out call stubs.
1337 typename
Glink_entries::const_iterator g
;
1338 for (g
= this->glink_entries_
.begin();
1339 g
!= this->glink_entries_
.end();
1342 uint64_t plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1343 uint64_t got_addr
= g_o_t
;
1344 uint64_t pltoff
= plt_addr
- got_addr
;
1346 if (pltoff
+ 0x80008000 > 0xffffffff || (pltoff
& 7) != 0)
1347 gold_error(_("%s: linkage table error against `%s'"),
1348 g
->first
.object_
->name().c_str(),
1349 g
->first
.sym_
->demangled_name().c_str());
1351 p
= oview
+ g
->second
* this->glink_entry_size();
1352 if (ha(pltoff
) != 0)
1354 write_insn
<big_endian
>(p
, addis_12_2
+ ha(pltoff
)), p
+= 4;
1355 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1356 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
)), p
+= 4;
1357 if (ha(pltoff
+ 16) != ha(pltoff
))
1359 write_insn
<big_endian
>(p
, addi_12_12
+ l(pltoff
)), p
+= 4;
1362 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1363 write_insn
<big_endian
>(p
, ld_2_12
+ l(pltoff
+ 8)), p
+= 4;
1364 write_insn
<big_endian
>(p
, ld_11_12
+ l(pltoff
+ 16)), p
+= 4;
1365 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1369 write_insn
<big_endian
>(p
, std_2_1
+ 40), p
+= 4;
1370 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
)), p
+= 4;
1371 if (ha(pltoff
+ 16) != ha(pltoff
))
1373 write_insn
<big_endian
>(p
, addi_2_2
+ l(pltoff
)), p
+= 4;
1376 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1377 write_insn
<big_endian
>(p
, ld_11_2
+ l(pltoff
+ 16)), p
+= 4;
1378 write_insn
<big_endian
>(p
, ld_2_2
+ l(pltoff
+ 8)), p
+= 4;
1379 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1383 // Write pltresolve stub.
1384 p
= oview
+ this->pltresolve_
;
1385 uint64_t after_bcl
= this->address() + this->pltresolve_
+ 16;
1386 uint64_t pltoff
= plt_base
- after_bcl
;
1388 elfcpp::Swap
<64, big_endian
>::writeval(p
, pltoff
), p
+= 8;
1390 write_insn
<big_endian
>(p
, mflr_12
), p
+= 4;
1391 write_insn
<big_endian
>(p
, bcl_20_31
), p
+= 4;
1392 write_insn
<big_endian
>(p
, mflr_11
), p
+= 4;
1393 write_insn
<big_endian
>(p
, ld_2_11
+ l(-16)), p
+= 4;
1394 write_insn
<big_endian
>(p
, mtlr_12
), p
+= 4;
1395 write_insn
<big_endian
>(p
, add_12_2_11
), p
+= 4;
1396 write_insn
<big_endian
>(p
, ld_11_12
+ 0), p
+= 4;
1397 write_insn
<big_endian
>(p
, ld_2_12
+ 8), p
+= 4;
1398 write_insn
<big_endian
>(p
, mtctr_11
), p
+= 4;
1399 write_insn
<big_endian
>(p
, ld_11_12
+ 16), p
+= 4;
1400 write_insn
<big_endian
>(p
, bctr
), p
+= 4;
1401 while (p
< oview
+ this->pltresolve_
+ this->pltresolve_size
)
1402 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1404 // Write lazy link call stubs.
1406 while (p
< oview
+ oview_size
)
1410 write_insn
<big_endian
>(p
, li_0_0
+ indx
), p
+= 4;
1414 write_insn
<big_endian
>(p
, lis_0_0
+ hi(indx
)), p
+= 4;
1415 write_insn
<big_endian
>(p
, ori_0_0_0
+ l(indx
)), p
+= 4;
1417 uint16_t branch_off
= this->pltresolve_
+ 8 - (p
- oview
);
1418 write_insn
<big_endian
>(p
, b
+ (branch_off
& 0x3fffffc)), p
+= 4;
1424 // Write out call stubs.
1425 typename
Glink_entries::const_iterator g
;
1426 for (g
= this->glink_entries_
.begin();
1427 g
!= this->glink_entries_
.end();
1430 uint32_t plt_addr
= plt_base
+ g
->first
.sym_
->plt_offset();
1433 p
= oview
+ g
->second
* this->glink_entry_size();
1434 if (parameters
->options().output_is_position_independent())
1436 unsigned int got2
= g
->first
.shndx_
;
1438 got_addr
= (g
->first
.object_
->output_section(got2
)->address()
1439 + g
->first
.object_
->output_section_offset(got2
)
1440 + g
->first
.addend_
);
1444 uint32_t pltoff
= plt_addr
- got_addr
;
1445 if (ha(pltoff
) == 0)
1447 write_insn
<big_endian
>(p
+ 0, lwz_11_30
+ l(pltoff
));
1448 write_insn
<big_endian
>(p
+ 4, mtctr_11
);
1449 write_insn
<big_endian
>(p
+ 8, bctr
);
1453 write_insn
<big_endian
>(p
+ 0, addis_11_30
+ ha(pltoff
));
1454 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(pltoff
));
1455 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1456 write_insn
<big_endian
>(p
+ 12, bctr
);
1461 write_insn
<big_endian
>(p
+ 0, lis_11
+ ha(plt_addr
));
1462 write_insn
<big_endian
>(p
+ 4, lwz_11_11
+ l(plt_addr
));
1463 write_insn
<big_endian
>(p
+ 8, mtctr_11
);
1464 write_insn
<big_endian
>(p
+ 12, bctr
);
1468 // Write out pltresolve branch table.
1469 p
= oview
+ this->pltresolve_
;
1470 unsigned int the_end
= oview_size
- this->pltresolve_size
;
1471 unsigned char *end_p
= oview
+ the_end
;
1472 while (p
< end_p
- 8 * 4)
1473 write_insn
<big_endian
>(p
, b
+ end_p
- p
), p
+= 4;
1475 write_insn
<big_endian
>(p
, nop
), p
+= 4;
1477 // Write out pltresolve call stub.
1478 if (parameters
->options().output_is_position_independent())
1480 uint32_t res0_off
= this->pltresolve_
;
1481 uint32_t after_bcl_off
= the_end
+ 12;
1482 uint32_t bcl_res0
= after_bcl_off
- res0_off
;
1484 write_insn
<big_endian
>(p
+ 0, addis_11_11
+ ha(bcl_res0
));
1485 write_insn
<big_endian
>(p
+ 4, mflr_0
);
1486 write_insn
<big_endian
>(p
+ 8, bcl_20_31
);
1487 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(bcl_res0
));
1488 write_insn
<big_endian
>(p
+ 16, mflr_12
);
1489 write_insn
<big_endian
>(p
+ 20, mtlr_0
);
1490 write_insn
<big_endian
>(p
+ 24, sub_11_11_12
);
1492 uint32_t got_bcl
= g_o_t
+ 4 - (after_bcl_off
+ this->address());
1494 write_insn
<big_endian
>(p
+ 28, addis_12_12
+ ha(got_bcl
));
1495 if (ha(got_bcl
) == ha(got_bcl
+ 4))
1497 write_insn
<big_endian
>(p
+ 32, lwz_0_12
+ l(got_bcl
));
1498 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ l(got_bcl
+ 4));
1502 write_insn
<big_endian
>(p
+ 32, lwzu_0_12
+ l(got_bcl
));
1503 write_insn
<big_endian
>(p
+ 36, lwz_12_12
+ 4);
1505 write_insn
<big_endian
>(p
+ 40, mtctr_0
);
1506 write_insn
<big_endian
>(p
+ 44, add_0_11_11
);
1507 write_insn
<big_endian
>(p
+ 48, add_11_0_11
);
1508 write_insn
<big_endian
>(p
+ 52, bctr
);
1509 write_insn
<big_endian
>(p
+ 56, nop
);
1510 write_insn
<big_endian
>(p
+ 60, nop
);
1514 uint32_t res0
= this->pltresolve_
+ this->address();
1516 write_insn
<big_endian
>(p
+ 0, lis_12
+ ha(g_o_t
+ 4));
1517 write_insn
<big_endian
>(p
+ 4, addis_11_11
+ ha(-res0
));
1518 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1519 write_insn
<big_endian
>(p
+ 8, lwz_0_12
+ l(g_o_t
+ 4));
1521 write_insn
<big_endian
>(p
+ 8, lwzu_0_12
+ l(g_o_t
+ 4));
1522 write_insn
<big_endian
>(p
+ 12, addi_11_11
+ l(-res0
));
1523 write_insn
<big_endian
>(p
+ 16, mtctr_0
);
1524 write_insn
<big_endian
>(p
+ 20, add_0_11_11
);
1525 if (ha(g_o_t
+ 4) == ha(g_o_t
+ 8))
1526 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ l(g_o_t
+ 8));
1528 write_insn
<big_endian
>(p
+ 24, lwz_12_12
+ 4);
1529 write_insn
<big_endian
>(p
+ 28, add_11_0_11
);
1530 write_insn
<big_endian
>(p
+ 32, bctr
);
1531 write_insn
<big_endian
>(p
+ 36, nop
);
1532 write_insn
<big_endian
>(p
+ 40, nop
);
1533 write_insn
<big_endian
>(p
+ 44, nop
);
1534 write_insn
<big_endian
>(p
+ 48, nop
);
1535 write_insn
<big_endian
>(p
+ 52, nop
);
1536 write_insn
<big_endian
>(p
+ 56, nop
);
1537 write_insn
<big_endian
>(p
+ 60, nop
);
1542 of
->write_output_view(off
, oview_size
, oview
);
1545 // Create the glink section.
1547 template<int size
, bool big_endian
>
1549 Target_powerpc
<size
, big_endian
>::make_glink_section(Layout
* layout
)
1551 if (this->glink_
== NULL
)
1553 this->glink_
= new Output_data_glink
<size
, big_endian
>(this);
1554 layout
->add_output_section_data(".text", elfcpp::SHT_PROGBITS
,
1555 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1556 this->glink_
, ORDER_TEXT
, false);
1560 // Create a PLT entry for a global symbol.
1562 template<int size
, bool big_endian
>
1564 Target_powerpc
<size
, big_endian
>::make_plt_entry(
1567 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1568 const Sized_relobj
<size
, big_endian
>* object
)
1570 if (this->plt_
== NULL
)
1571 this->make_plt_section(layout
);
1573 this->plt_
->add_entry(gsym
);
1575 unsigned int got2_shndx
= 0;
1576 if (size
== 32 && object
!= NULL
)
1578 const Powerpc_relobj
<size
, big_endian
>* ppc_obj
1579 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(object
);
1580 got2_shndx
= ppc_obj
->got2_shndx();
1582 this->glink_
->add_entry(gsym
, reloc
, object
, got2_shndx
);
1585 // Return the number of entries in the PLT.
1587 template<int size
, bool big_endian
>
1589 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
1591 if (this->plt_
== NULL
)
1593 return this->plt_
->entry_count();
1596 // Return the offset of the first non-reserved PLT entry.
1598 template<int size
, bool big_endian
>
1600 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
1602 return Output_data_plt_powerpc
<size
, big_endian
>::first_plt_entry_offset();
1605 // Return the size of each PLT entry.
1607 template<int size
, bool big_endian
>
1609 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
1611 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
1614 // Create a GOT entry for the TLS module index.
1616 template<int size
, bool big_endian
>
1618 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(
1619 Symbol_table
* symtab
,
1621 Sized_relobj_file
<size
, big_endian
>* object
)
1623 if (this->got_mod_index_offset_
== -1U)
1625 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1626 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1627 Output_data_got_powerpc
<size
, big_endian
>* got
;
1628 unsigned int got_offset
;
1630 got
= this->got_section(symtab
, layout
);
1631 got
->reserve_ent(2);
1632 got_offset
= got
->add_constant(0);
1633 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
1635 got
->add_constant(0);
1636 this->got_mod_index_offset_
= got_offset
;
1638 return this->got_mod_index_offset_
;
1641 // Optimize the TLS relocation type based on what we know about the
1642 // symbol. IS_FINAL is true if the final address of this symbol is
1643 // known at link time.
1645 template<int size
, bool big_endian
>
1646 tls::Tls_optimization
1647 Target_powerpc
<size
, big_endian
>::optimize_tls_reloc(bool, int)
1649 // If we are generating a shared library, then we can't do anything
1651 if (parameters
->options().shared())
1652 return tls::TLSOPT_NONE
;
1654 return tls::TLSOPT_NONE
;
1657 // Get the Reference_flags for a particular relocation.
1659 template<int size
, bool big_endian
>
1661 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(unsigned int r_type
)
1665 case elfcpp::R_POWERPC_NONE
:
1666 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1667 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1668 case elfcpp::R_PPC64_TOC
:
1669 // No symbol reference.
1672 case elfcpp::R_POWERPC_ADDR16
:
1673 case elfcpp::R_POWERPC_ADDR16_LO
:
1674 case elfcpp::R_POWERPC_ADDR16_HI
:
1675 case elfcpp::R_POWERPC_ADDR16_HA
:
1676 case elfcpp::R_POWERPC_ADDR32
:
1677 case elfcpp::R_PPC64_ADDR64
:
1678 return Symbol::ABSOLUTE_REF
;
1680 case elfcpp::R_POWERPC_REL24
:
1681 case elfcpp::R_PPC_LOCAL24PC
:
1682 case elfcpp::R_PPC_REL16
:
1683 case elfcpp::R_PPC_REL16_LO
:
1684 case elfcpp::R_PPC_REL16_HI
:
1685 case elfcpp::R_PPC_REL16_HA
:
1686 return Symbol::RELATIVE_REF
;
1688 case elfcpp::R_PPC_PLTREL24
:
1689 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1691 case elfcpp::R_POWERPC_GOT16
:
1692 case elfcpp::R_POWERPC_GOT16_LO
:
1693 case elfcpp::R_POWERPC_GOT16_HI
:
1694 case elfcpp::R_POWERPC_GOT16_HA
:
1695 case elfcpp::R_PPC64_TOC16
:
1696 case elfcpp::R_PPC64_TOC16_LO
:
1697 case elfcpp::R_PPC64_TOC16_HI
:
1698 case elfcpp::R_PPC64_TOC16_HA
:
1699 case elfcpp::R_PPC64_TOC16_DS
:
1700 case elfcpp::R_PPC64_TOC16_LO_DS
:
1702 return Symbol::ABSOLUTE_REF
;
1704 case elfcpp::R_POWERPC_GOT_TPREL16
:
1705 case elfcpp::R_POWERPC_TLS
:
1706 return Symbol::TLS_REF
;
1708 case elfcpp::R_POWERPC_COPY
:
1709 case elfcpp::R_POWERPC_GLOB_DAT
:
1710 case elfcpp::R_POWERPC_JMP_SLOT
:
1711 case elfcpp::R_POWERPC_RELATIVE
:
1712 case elfcpp::R_POWERPC_DTPMOD
:
1714 // Not expected. We will give an error later.
1719 // Report an unsupported relocation against a local symbol.
1721 template<int size
, bool big_endian
>
1723 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1724 Sized_relobj_file
<size
, big_endian
>* object
,
1725 unsigned int r_type
)
1727 gold_error(_("%s: unsupported reloc %u against local symbol"),
1728 object
->name().c_str(), r_type
);
1731 // We are about to emit a dynamic relocation of type R_TYPE. If the
1732 // dynamic linker does not support it, issue an error.
1734 template<int size
, bool big_endian
>
1736 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1737 unsigned int r_type
)
1739 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1741 // These are the relocation types supported by glibc for both 32-bit
1742 // and 64-bit powerpc.
1745 case elfcpp::R_POWERPC_RELATIVE
:
1746 case elfcpp::R_POWERPC_GLOB_DAT
:
1747 case elfcpp::R_POWERPC_DTPMOD
:
1748 case elfcpp::R_POWERPC_DTPREL
:
1749 case elfcpp::R_POWERPC_TPREL
:
1750 case elfcpp::R_POWERPC_JMP_SLOT
:
1751 case elfcpp::R_POWERPC_COPY
:
1752 case elfcpp::R_POWERPC_ADDR32
:
1753 case elfcpp::R_POWERPC_ADDR24
:
1754 case elfcpp::R_POWERPC_REL24
:
1765 // These are the relocation types supported only on 64-bit.
1766 case elfcpp::R_PPC64_ADDR64
:
1767 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1768 case elfcpp::R_PPC64_TPREL16_DS
:
1769 case elfcpp::R_POWERPC_TPREL16
:
1770 case elfcpp::R_POWERPC_TPREL16_LO
:
1771 case elfcpp::R_POWERPC_TPREL16_HI
:
1772 case elfcpp::R_POWERPC_TPREL16_HA
:
1773 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1774 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1775 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1776 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1777 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1778 case elfcpp::R_POWERPC_ADDR16_LO
:
1779 case elfcpp::R_POWERPC_ADDR16_HI
:
1780 case elfcpp::R_POWERPC_ADDR16_HA
:
1781 case elfcpp::R_POWERPC_ADDR30
:
1782 case elfcpp::R_PPC64_UADDR64
:
1783 case elfcpp::R_POWERPC_UADDR32
:
1784 case elfcpp::R_POWERPC_ADDR16
:
1785 case elfcpp::R_POWERPC_UADDR16
:
1786 case elfcpp::R_PPC64_ADDR16_DS
:
1787 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1788 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1789 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1790 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1791 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1792 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1793 case elfcpp::R_POWERPC_REL32
:
1794 case elfcpp::R_PPC64_REL64
:
1805 // These are the relocation types supported only on 32-bit.
1812 // This prevents us from issuing more than one error per reloc
1813 // section. But we can still wind up issuing more than one
1814 // error per object file.
1815 if (this->issued_non_pic_error_
)
1817 gold_assert(parameters
->options().output_is_position_independent());
1818 object
->error(_("requires unsupported dynamic reloc; "
1819 "recompile with -fPIC"));
1820 this->issued_non_pic_error_
= true;
1824 // Scan a relocation for a local symbol.
1826 template<int size
, bool big_endian
>
1828 Target_powerpc
<size
, big_endian
>::Scan::local(
1829 Symbol_table
* symtab
,
1831 Target_powerpc
<size
, big_endian
>* target
,
1832 Sized_relobj_file
<size
, big_endian
>* object
,
1833 unsigned int data_shndx
,
1834 Output_section
* output_section
,
1835 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1836 unsigned int r_type
,
1837 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1841 case elfcpp::R_POWERPC_NONE
:
1842 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1843 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1846 case elfcpp::R_PPC64_ADDR64
:
1847 case elfcpp::R_POWERPC_ADDR32
:
1848 case elfcpp::R_POWERPC_ADDR16_HA
:
1849 case elfcpp::R_POWERPC_ADDR16_LO
:
1850 // If building a shared library (or a position-independent
1851 // executable), we need to create a dynamic relocation for
1853 if (parameters
->options().output_is_position_independent())
1855 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1857 check_non_pic(object
, r_type
);
1858 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1860 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1861 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1862 data_shndx
, reloc
.get_r_offset(),
1863 reloc
.get_r_addend());
1867 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1868 gold_assert(lsym
.get_st_value() == 0);
1869 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1870 output_section
, data_shndx
,
1871 reloc
.get_r_offset(),
1872 reloc
.get_r_addend(), false);
1877 case elfcpp::R_POWERPC_REL24
:
1878 case elfcpp::R_PPC_LOCAL24PC
:
1879 case elfcpp::R_POWERPC_REL32
:
1880 case elfcpp::R_PPC_REL16_LO
:
1881 case elfcpp::R_PPC_REL16_HA
:
1884 case elfcpp::R_POWERPC_GOT16
:
1885 case elfcpp::R_POWERPC_GOT16_LO
:
1886 case elfcpp::R_POWERPC_GOT16_HI
:
1887 case elfcpp::R_POWERPC_GOT16_HA
:
1889 // The symbol requires a GOT entry.
1890 Output_data_got_powerpc
<size
, big_endian
>* got
;
1893 got
= target
->got_section(symtab
, layout
);
1894 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1896 // If we are generating a shared object, we need to add a
1897 // dynamic relocation for this symbol's GOT entry.
1898 if (parameters
->options().output_is_position_independent())
1900 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1902 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1905 off
= got
->add_constant(0);
1906 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1907 rela_dyn
->add_local_relative(object
, r_sym
,
1908 elfcpp::R_POWERPC_RELATIVE
,
1909 got
, off
, 0, false);
1913 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1917 case elfcpp::R_PPC64_TOC16
:
1918 case elfcpp::R_PPC64_TOC16_LO
:
1919 case elfcpp::R_PPC64_TOC16_HI
:
1920 case elfcpp::R_PPC64_TOC16_HA
:
1921 case elfcpp::R_PPC64_TOC16_DS
:
1922 case elfcpp::R_PPC64_TOC16_LO_DS
:
1923 case elfcpp::R_PPC64_TOC
:
1924 // We need a GOT section.
1925 target
->got_section(symtab
, layout
);
1928 // These are relocations which should only be seen by the
1929 // dynamic linker, and should never be seen here.
1930 case elfcpp::R_POWERPC_COPY
:
1931 case elfcpp::R_POWERPC_GLOB_DAT
:
1932 case elfcpp::R_POWERPC_JMP_SLOT
:
1933 case elfcpp::R_POWERPC_RELATIVE
:
1934 case elfcpp::R_POWERPC_DTPMOD
:
1935 gold_error(_("%s: unexpected reloc %u in object file"),
1936 object
->name().c_str(), r_type
);
1940 unsupported_reloc_local(object
, r_type
);
1945 // Report an unsupported relocation against a global symbol.
1947 template<int size
, bool big_endian
>
1949 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1950 Sized_relobj_file
<size
, big_endian
>* object
,
1951 unsigned int r_type
,
1954 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1955 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1958 // Scan a relocation for a global symbol.
1960 template<int size
, bool big_endian
>
1962 Target_powerpc
<size
, big_endian
>::Scan::global(
1963 Symbol_table
* symtab
,
1965 Target_powerpc
<size
, big_endian
>* target
,
1966 Sized_relobj_file
<size
, big_endian
>* object
,
1967 unsigned int data_shndx
,
1968 Output_section
* output_section
,
1969 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1970 unsigned int r_type
,
1975 case elfcpp::R_POWERPC_NONE
:
1976 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1977 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1978 case elfcpp::R_PPC_LOCAL24PC
:
1981 case elfcpp::R_POWERPC_ADDR16
:
1982 case elfcpp::R_POWERPC_ADDR16_LO
:
1983 case elfcpp::R_POWERPC_ADDR16_HI
:
1984 case elfcpp::R_POWERPC_ADDR16_HA
:
1985 case elfcpp::R_POWERPC_ADDR32
:
1986 case elfcpp::R_PPC64_ADDR64
:
1988 // Make a PLT entry if necessary.
1989 if (gsym
->needs_plt_entry())
1991 target
->make_plt_entry(layout
, gsym
, reloc
, 0);
1992 // Since this is not a PC-relative relocation, we may be
1993 // taking the address of a function. In that case we need to
1994 // set the entry in the dynamic symbol table to the address of
1997 && gsym
->is_from_dynobj() && !parameters
->options().shared())
1998 gsym
->set_needs_dynsym_value();
2000 // Make a dynamic relocation if necessary.
2001 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2003 if (gsym
->may_need_copy_reloc())
2005 target
->copy_reloc(symtab
, layout
, object
,
2006 data_shndx
, output_section
, gsym
, reloc
);
2008 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
2009 || r_type
== elfcpp::R_PPC64_ADDR64
)
2010 && gsym
->can_use_relative_reloc(false))
2012 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2013 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2014 output_section
, object
,
2015 data_shndx
, reloc
.get_r_offset(),
2016 reloc
.get_r_addend(), false);
2020 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2022 check_non_pic(object
, r_type
);
2023 if (gsym
->is_from_dynobj()
2024 || gsym
->is_undefined()
2025 || gsym
->is_preemptible())
2026 rela_dyn
->add_global(gsym
, r_type
, output_section
,
2028 reloc
.get_r_offset(),
2029 reloc
.get_r_addend());
2031 rela_dyn
->add_global_relative(gsym
, r_type
,
2032 output_section
, object
,
2034 reloc
.get_r_offset(),
2035 reloc
.get_r_addend(), false);
2041 case elfcpp::R_PPC_PLTREL24
:
2042 case elfcpp::R_POWERPC_REL24
:
2044 if (gsym
->needs_plt_entry()
2045 || (!gsym
->final_value_is_known()
2046 && !(gsym
->is_defined()
2047 && !gsym
->is_from_dynobj()
2048 && !gsym
->is_preemptible())))
2050 if (r_type
== elfcpp::R_PPC_PLTREL24
)
2051 target
->make_plt_entry(layout
, gsym
, reloc
, object
);
2053 target
->make_plt_entry(layout
, gsym
, reloc
, 0);
2055 // Make a dynamic relocation if necessary.
2056 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2058 if (gsym
->may_need_copy_reloc())
2060 target
->copy_reloc(symtab
, layout
, object
,
2061 data_shndx
, output_section
, gsym
,
2066 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2067 check_non_pic(object
, r_type
);
2068 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2069 data_shndx
, reloc
.get_r_offset(),
2070 reloc
.get_r_addend());
2076 case elfcpp::R_PPC_REL16
:
2077 case elfcpp::R_PPC_REL16_LO
:
2078 case elfcpp::R_PPC_REL16_HI
:
2079 case elfcpp::R_PPC_REL16_HA
:
2082 case elfcpp::R_POWERPC_GOT16
:
2083 case elfcpp::R_POWERPC_GOT16_LO
:
2084 case elfcpp::R_POWERPC_GOT16_HI
:
2085 case elfcpp::R_POWERPC_GOT16_HA
:
2087 // The symbol requires a GOT entry.
2088 Output_data_got_powerpc
<size
, big_endian
>* got
;
2090 got
= target
->got_section(symtab
, layout
);
2091 if (gsym
->final_value_is_known())
2092 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2095 // If this symbol is not fully resolved, we need to add a
2096 // dynamic relocation for it.
2097 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2098 if (gsym
->is_from_dynobj()
2099 || gsym
->is_undefined()
2100 || gsym
->is_preemptible())
2101 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2102 elfcpp::R_POWERPC_GLOB_DAT
);
2103 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
2105 unsigned int off
= got
->add_constant(0);
2107 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
2108 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
2109 got
, off
, 0, false);
2115 case elfcpp::R_PPC64_TOC
:
2116 case elfcpp::R_PPC64_TOC16
:
2117 case elfcpp::R_PPC64_TOC16_LO
:
2118 case elfcpp::R_PPC64_TOC16_HI
:
2119 case elfcpp::R_PPC64_TOC16_HA
:
2120 case elfcpp::R_PPC64_TOC16_DS
:
2121 case elfcpp::R_PPC64_TOC16_LO_DS
:
2122 // We need a GOT section.
2123 target
->got_section(symtab
, layout
);
2126 case elfcpp::R_POWERPC_GOT_TPREL16
:
2127 case elfcpp::R_POWERPC_TLS
:
2131 // These are relocations which should only be seen by the
2132 // dynamic linker, and should never be seen here.
2133 case elfcpp::R_POWERPC_COPY
:
2134 case elfcpp::R_POWERPC_GLOB_DAT
:
2135 case elfcpp::R_POWERPC_JMP_SLOT
:
2136 case elfcpp::R_POWERPC_RELATIVE
:
2137 case elfcpp::R_POWERPC_DTPMOD
:
2138 gold_error(_("%s: unexpected reloc %u in object file"),
2139 object
->name().c_str(), r_type
);
2143 unsupported_reloc_global(object
, r_type
, gsym
);
2148 // Process relocations for gc.
2150 template<int size
, bool big_endian
>
2152 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
2153 Symbol_table
* symtab
,
2155 Sized_relobj_file
<size
, big_endian
>* object
,
2156 unsigned int data_shndx
,
2158 const unsigned char* prelocs
,
2160 Output_section
* output_section
,
2161 bool needs_special_offset_handling
,
2162 size_t local_symbol_count
,
2163 const unsigned char* plocal_symbols
)
2165 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2166 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2168 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
2169 typename
Target_powerpc::Relocatable_size_for_reloc
>(
2178 needs_special_offset_handling
,
2183 // Scan relocations for a section.
2185 template<int size
, bool big_endian
>
2187 Target_powerpc
<size
, big_endian
>::scan_relocs(
2188 Symbol_table
* symtab
,
2190 Sized_relobj_file
<size
, big_endian
>* object
,
2191 unsigned int data_shndx
,
2192 unsigned int sh_type
,
2193 const unsigned char* prelocs
,
2195 Output_section
* output_section
,
2196 bool needs_special_offset_handling
,
2197 size_t local_symbol_count
,
2198 const unsigned char* plocal_symbols
)
2200 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2201 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
2203 if (sh_type
== elfcpp::SHT_REL
)
2205 gold_error(_("%s: unsupported REL reloc section"),
2206 object
->name().c_str());
2212 static Output_data_space
* sdata
;
2214 // Define _SDA_BASE_ at the start of the .sdata section.
2217 // layout->find_output_section(".sdata") == NULL
2218 sdata
= new Output_data_space(4, "** sdata");
2220 = layout
->add_output_section_data(".sdata", 0,
2222 | elfcpp::SHF_WRITE
,
2223 sdata
, ORDER_SMALL_DATA
, false);
2224 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
2225 Symbol_table::PREDEFINED
,
2226 os
, 32768, 0, elfcpp::STT_OBJECT
,
2227 elfcpp::STB_LOCAL
, elfcpp::STV_HIDDEN
,
2232 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
2241 needs_special_offset_handling
,
2246 // Finalize the sections.
2248 template<int size
, bool big_endian
>
2250 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
2252 const Input_objects
*,
2255 // Fill in some more dynamic tags.
2256 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2258 : this->plt_
->rel_plt());
2259 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
2260 this->rela_dyn_
, true, size
== 32);
2264 this->got_
->finalize_data_size();
2265 Output_data_dynamic
* odyn
= layout
->dynamic_data();
2266 odyn
->add_section_plus_offset(elfcpp::DT_PPC_GOT
,
2267 this->got_
, this->got_
->g_o_t());
2270 // Emit any relocs we saved in an attempt to avoid generating COPY
2272 if (this->copy_relocs_
.any_saved_relocs())
2273 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2276 // Perform a relocation.
2278 template<int size
, bool big_endian
>
2280 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
2281 const Relocate_info
<size
, big_endian
>* relinfo
,
2282 Target_powerpc
* target
,
2285 const elfcpp::Rela
<size
, big_endian
>& rela
,
2286 unsigned int r_type
,
2287 const Sized_symbol
<size
>* gsym
,
2288 const Symbol_value
<size
>* psymval
,
2289 unsigned char* view
,
2290 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2291 section_size_type
/* view_size */)
2293 const unsigned int toc_base_offset
= 0x8000;
2294 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
2295 const Powerpc_relobj
<size
, big_endian
>* const object
2296 = static_cast<const Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
2297 elfcpp::Elf_Xword value
;
2299 if (r_type
== elfcpp::R_POWERPC_GOT16
2300 || r_type
== elfcpp::R_POWERPC_GOT16_LO
2301 || r_type
== elfcpp::R_POWERPC_GOT16_HI
2302 || r_type
== elfcpp::R_POWERPC_GOT16_HA
2303 || r_type
== elfcpp::R_PPC64_GOT16_DS
2304 || r_type
== elfcpp::R_PPC64_GOT16_LO_DS
)
2308 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2309 value
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2313 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
2314 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2315 value
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2317 value
-= target
->got_section()->g_o_t();
2319 else if (r_type
== elfcpp::R_PPC64_TOC
)
2321 value
= target
->got_section()->address() + toc_base_offset
;
2323 else if (gsym
!= NULL
2324 && (r_type
== elfcpp::R_POWERPC_REL24
2325 || r_type
== elfcpp::R_PPC_PLTREL24
)
2326 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2328 const Output_data_glink
<size
, big_endian
>* glink
;
2330 glink
= target
->glink_section();
2331 unsigned int shndx
= 0;
2332 if (size
== 32 && r_type
== elfcpp::R_PPC_PLTREL24
)
2333 shndx
= object
->got2_shndx();
2334 unsigned int glink_index
= glink
->find_entry(gsym
, rela
, object
, shndx
);
2335 value
= glink
->address() + glink_index
* glink
->glink_entry_size();
2339 elfcpp::Elf_Xword addend
= 0;
2340 if (r_type
!= elfcpp::R_PPC_PLTREL24
)
2341 addend
= rela
.get_r_addend();
2342 value
= psymval
->value(object
, addend
);
2347 case elfcpp::R_PPC64_TOC16
:
2348 case elfcpp::R_PPC64_TOC16_LO
:
2349 case elfcpp::R_PPC64_TOC16_HI
:
2350 case elfcpp::R_PPC64_TOC16_HA
:
2351 case elfcpp::R_PPC64_TOC16_DS
:
2352 case elfcpp::R_PPC64_TOC16_LO_DS
:
2353 // Subtract the TOC base address.
2354 value
-= target
->got_section()->address() + toc_base_offset
;
2357 case elfcpp::R_POWERPC_SECTOFF
:
2358 case elfcpp::R_POWERPC_SECTOFF_LO
:
2359 case elfcpp::R_POWERPC_SECTOFF_HI
:
2360 case elfcpp::R_POWERPC_SECTOFF_HA
:
2361 case elfcpp::R_PPC64_SECTOFF_DS
:
2362 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2364 value
-= os
->address();
2373 case elfcpp::R_POWERPC_NONE
:
2374 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
2375 case elfcpp::R_POWERPC_GNU_VTENTRY
:
2378 case elfcpp::R_POWERPC_REL32
:
2379 Reloc::rel32(view
, value
, 0, address
);
2382 case elfcpp::R_POWERPC_REL24
:
2383 case elfcpp::R_PPC_PLTREL24
:
2384 case elfcpp::R_PPC_LOCAL24PC
:
2385 Reloc::rel24(view
, value
, 0, address
);
2388 case elfcpp::R_POWERPC_REL14
:
2389 Reloc::rel14(view
, value
, 0, address
);
2392 case elfcpp::R_PPC64_ADDR64
:
2393 case elfcpp::R_PPC64_TOC
:
2394 Relocate_functions
<size
, big_endian
>::rela64(view
, value
, 0);
2397 case elfcpp::R_POWERPC_ADDR32
:
2398 Relocate_functions
<size
, big_endian
>::rela32(view
, value
, 0);
2401 case elfcpp::R_POWERPC_ADDR16
:
2402 case elfcpp::R_PPC64_TOC16
:
2403 case elfcpp::R_POWERPC_GOT16
:
2404 case elfcpp::R_POWERPC_SECTOFF
:
2405 Reloc::addr16(view
, value
, 0);
2408 case elfcpp::R_POWERPC_ADDR16_LO
:
2409 case elfcpp::R_PPC64_TOC16_LO
:
2410 case elfcpp::R_POWERPC_GOT16_LO
:
2411 case elfcpp::R_POWERPC_SECTOFF_LO
:
2412 Reloc::addr16_lo(view
, value
, 0);
2415 case elfcpp::R_POWERPC_ADDR16_HI
:
2416 case elfcpp::R_PPC64_TOC16_HI
:
2417 case elfcpp::R_POWERPC_GOT16_HI
:
2418 case elfcpp::R_POWERPC_SECTOFF_HI
:
2419 Reloc::addr16_hi(view
, value
, 0);
2422 case elfcpp::R_POWERPC_ADDR16_HA
:
2423 case elfcpp::R_PPC64_TOC16_HA
:
2424 case elfcpp::R_POWERPC_GOT16_HA
:
2425 case elfcpp::R_POWERPC_SECTOFF_HA
:
2426 Reloc::addr16_ha(view
, value
, 0);
2429 case elfcpp::R_PPC_REL16_LO
:
2430 Reloc::rel16_lo(view
, value
, 0, address
);
2433 case elfcpp::R_PPC_REL16_HI
:
2434 Reloc::rel16_hi(view
, value
, 0, address
);
2437 case elfcpp::R_PPC_REL16_HA
:
2438 Reloc::rel16_ha(view
, value
, 0, address
);
2441 case elfcpp::R_PPC64_ADDR16_DS
:
2442 case elfcpp::R_PPC64_ADDR16_LO_DS
:
2443 case elfcpp::R_PPC64_TOC16_DS
:
2444 case elfcpp::R_PPC64_TOC16_LO_DS
:
2445 case elfcpp::R_PPC64_GOT16_DS
:
2446 case elfcpp::R_PPC64_GOT16_LO_DS
:
2447 case elfcpp::R_PPC64_SECTOFF_DS
:
2448 case elfcpp::R_PPC64_SECTOFF_LO_DS
:
2449 Reloc::addr16_ds(view
, value
, 0);
2452 case elfcpp::R_POWERPC_COPY
:
2453 case elfcpp::R_POWERPC_GLOB_DAT
:
2454 case elfcpp::R_POWERPC_JMP_SLOT
:
2455 case elfcpp::R_POWERPC_RELATIVE
:
2456 // This is an outstanding tls reloc, which is unexpected when
2458 case elfcpp::R_POWERPC_DTPMOD
:
2459 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2460 _("unexpected reloc %u in object file"),
2465 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2466 _("unsupported reloc %u"),
2474 // Perform a TLS relocation.
2476 template<int size
, bool big_endian
>
2478 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
2479 const Relocate_info
<size
, big_endian
>* relinfo
,
2480 Target_powerpc
<size
, big_endian
>* target
,
2482 const elfcpp::Rela
<size
, big_endian
>& rela
,
2483 unsigned int r_type
,
2484 const Sized_symbol
<size
>* gsym
,
2485 const Symbol_value
<size
>* psymval
,
2486 unsigned char* view
,
2487 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2490 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2491 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
2493 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2494 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
2496 const bool is_final
=
2498 ? !parameters
->options().output_is_position_independent()
2499 : gsym
->final_value_is_known());
2507 // Relocate section data.
2509 template<int size
, bool big_endian
>
2511 Target_powerpc
<size
, big_endian
>::relocate_section(
2512 const Relocate_info
<size
, big_endian
>* relinfo
,
2513 unsigned int sh_type
,
2514 const unsigned char* prelocs
,
2516 Output_section
* output_section
,
2517 bool needs_special_offset_handling
,
2518 unsigned char* view
,
2519 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2520 section_size_type view_size
,
2521 const Reloc_symbol_changes
* reloc_symbol_changes
)
2523 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2524 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
2526 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2528 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
2535 needs_special_offset_handling
,
2539 reloc_symbol_changes
);
2542 class Powerpc_scan_relocatable_reloc
2545 // Return the strategy to use for a local symbol which is not a
2546 // section symbol, given the relocation type.
2547 inline Relocatable_relocs::Reloc_strategy
2548 local_non_section_strategy(unsigned int r_type
, Relobj
*, unsigned int r_sym
)
2550 if (r_type
== 0 && r_sym
== 0)
2551 return Relocatable_relocs::RELOC_DISCARD
;
2552 return Relocatable_relocs::RELOC_COPY
;
2555 // Return the strategy to use for a local symbol which is a section
2556 // symbol, given the relocation type.
2557 inline Relocatable_relocs::Reloc_strategy
2558 local_section_strategy(unsigned int, Relobj
*)
2560 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
;
2563 // Return the strategy to use for a global symbol, given the
2564 // relocation type, the object, and the symbol index.
2565 inline Relocatable_relocs::Reloc_strategy
2566 global_strategy(unsigned int r_type
, Relobj
*, unsigned int)
2568 if (r_type
== elfcpp::R_PPC_PLTREL24
)
2569 return Relocatable_relocs::RELOC_SPECIAL
;
2570 return Relocatable_relocs::RELOC_COPY
;
2574 // Scan the relocs during a relocatable link.
2576 template<int size
, bool big_endian
>
2578 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
2579 Symbol_table
* symtab
,
2581 Sized_relobj_file
<size
, big_endian
>* object
,
2582 unsigned int data_shndx
,
2583 unsigned int sh_type
,
2584 const unsigned char* prelocs
,
2586 Output_section
* output_section
,
2587 bool needs_special_offset_handling
,
2588 size_t local_symbol_count
,
2589 const unsigned char* plocal_symbols
,
2590 Relocatable_relocs
* rr
)
2592 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2594 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
2595 Powerpc_scan_relocatable_reloc
>(
2603 needs_special_offset_handling
,
2609 // Relocate a section during a relocatable link.
2611 template<int size
, bool big_endian
>
2613 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
2614 const Relocate_info
<size
, big_endian
>* relinfo
,
2615 unsigned int sh_type
,
2616 const unsigned char* prelocs
,
2618 Output_section
* output_section
,
2619 off_t offset_in_output_section
,
2620 const Relocatable_relocs
* rr
,
2622 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2624 unsigned char* reloc_view
,
2625 section_size_type reloc_view_size
)
2627 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2629 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Address
;
2630 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc
2632 typedef typename Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::Reloc_write
2634 const int reloc_size
2635 = Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::reloc_size
;
2636 const Address invalid_address
= static_cast<Address
>(0) - 1;
2638 Powerpc_relobj
<size
, big_endian
>* const object
2639 = static_cast<Powerpc_relobj
<size
, big_endian
>*>(relinfo
->object
);
2640 const unsigned int local_count
= object
->local_symbol_count();
2641 unsigned int got2_shndx
= object
->got2_shndx();
2642 typename
elfcpp::Elf_types
<size
>::Elf_Swxword got2_addend
= 0;
2643 if (got2_shndx
!= 0)
2644 got2_addend
= object
->get_output_section_offset(got2_shndx
);
2646 unsigned char* pwrite
= reloc_view
;
2648 for (size_t i
= 0; i
< reloc_count
; ++i
, prelocs
+= reloc_size
)
2650 Relocatable_relocs::Reloc_strategy strategy
= rr
->strategy(i
);
2651 if (strategy
== Relocatable_relocs::RELOC_DISCARD
)
2654 Reltype
reloc(prelocs
);
2655 Reltype_write
reloc_write(pwrite
);
2657 typename
elfcpp::Elf_types
<size
>::Elf_WXword r_info
= reloc
.get_r_info();
2658 const unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(r_info
);
2659 const unsigned int r_type
= elfcpp::elf_r_type
<size
>(r_info
);
2661 // Get the new symbol index.
2663 unsigned int new_symndx
;
2664 if (r_sym
< local_count
)
2668 case Relocatable_relocs::RELOC_COPY
:
2669 case Relocatable_relocs::RELOC_SPECIAL
:
2670 new_symndx
= object
->symtab_index(r_sym
);
2671 gold_assert(new_symndx
!= -1U);
2674 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
:
2676 // We are adjusting a section symbol. We need to find
2677 // the symbol table index of the section symbol for
2678 // the output section corresponding to input section
2679 // in which this symbol is defined.
2680 gold_assert(r_sym
< local_count
);
2682 unsigned int shndx
=
2683 object
->local_symbol_input_shndx(r_sym
, &is_ordinary
);
2684 gold_assert(is_ordinary
);
2685 Output_section
* os
= object
->output_section(shndx
);
2686 gold_assert(os
!= NULL
);
2687 gold_assert(os
->needs_symtab_index());
2688 new_symndx
= os
->symtab_index();
2698 const Symbol
* gsym
= object
->global_symbol(r_sym
);
2699 gold_assert(gsym
!= NULL
);
2700 if (gsym
->is_forwarder())
2701 gsym
= relinfo
->symtab
->resolve_forwards(gsym
);
2703 gold_assert(gsym
->has_symtab_index());
2704 new_symndx
= gsym
->symtab_index();
2707 // Get the new offset--the location in the output section where
2708 // this relocation should be applied.
2710 Address offset
= reloc
.get_r_offset();
2712 if (static_cast<Address
>(offset_in_output_section
) != invalid_address
)
2713 new_offset
= offset
+ offset_in_output_section
;
2716 section_offset_type sot_offset
=
2717 convert_types
<section_offset_type
, Address
>(offset
);
2718 section_offset_type new_sot_offset
=
2719 output_section
->output_offset(object
, relinfo
->data_shndx
,
2721 gold_assert(new_sot_offset
!= -1);
2722 new_offset
= new_sot_offset
;
2725 reloc_write
.put_r_offset(new_offset
);
2726 reloc_write
.put_r_info(elfcpp::elf_r_info
<size
>(new_symndx
, r_type
));
2728 // Handle the reloc addend based on the strategy.
2729 typename
elfcpp::Elf_types
<size
>::Elf_Swxword addend
;
2730 addend
= Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::
2731 get_reloc_addend(&reloc
);
2733 if (strategy
== Relocatable_relocs::RELOC_COPY
)
2735 else if (strategy
== Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA
)
2737 const Symbol_value
<size
>* psymval
= object
->local_symbol(r_sym
);
2739 addend
= psymval
->value(object
, addend
);
2741 else if (strategy
== Relocatable_relocs::RELOC_SPECIAL
)
2743 if (addend
>= 32768)
2744 addend
+= got2_addend
;
2749 Reloc_types
<elfcpp::SHT_RELA
, size
, big_endian
>::
2750 set_reloc_addend(&reloc_write
, addend
);
2752 pwrite
+= reloc_size
;
2755 gold_assert(static_cast<section_size_type
>(pwrite
- reloc_view
)
2756 == reloc_view_size
);
2759 // Return the value to use for a dynamic which requires special
2760 // treatment. This is how we support equality comparisons of function
2761 // pointers across shared library boundaries, as described in the
2762 // processor specific ABI supplement.
2764 template<int size
, bool big_endian
>
2766 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
2770 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2771 return this->plt_section()->address() + gsym
->plt_offset();
2777 // The selector for powerpc object files.
2779 template<int size
, bool big_endian
>
2780 class Target_selector_powerpc
: public Target_selector
2783 Target_selector_powerpc()
2784 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
2786 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
2787 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
2789 ? (big_endian
? "elf64ppc" : "elf64lppc")
2790 : (big_endian
? "elf32ppc" : "elf32lppc")))
2794 do_recognize(Input_file
*, off_t
, int machine
, int, int)
2799 if (machine
!= elfcpp::EM_PPC64
)
2804 if (machine
!= elfcpp::EM_PPC
)
2812 return this->instantiate_target();
2816 do_instantiate_target()
2817 { return new Target_powerpc
<size
, big_endian
>(); }
2820 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2821 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2822 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2823 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2825 } // End anonymous namespace.