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 Target_powerpc
: public Sized_target
<size
, big_endian
>
54 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, big_endian
> Reloc_section
;
57 : Sized_target
<size
, big_endian
>(&powerpc_info
),
58 got_(NULL
), got2_(NULL
), toc_(NULL
),
59 plt_(NULL
), rela_dyn_(NULL
),
60 copy_relocs_(elfcpp::R_POWERPC_COPY
),
61 dynbss_(NULL
), got_mod_index_offset_(-1U)
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
68 gc_process_relocs(Symbol_table
* symtab
,
70 Sized_relobj_file
<size
, big_endian
>* object
,
71 unsigned int data_shndx
,
73 const unsigned char* prelocs
,
75 Output_section
* output_section
,
76 bool needs_special_offset_handling
,
77 size_t local_symbol_count
,
78 const unsigned char* plocal_symbols
);
80 // Scan the relocations to look for symbol adjustments.
82 scan_relocs(Symbol_table
* symtab
,
84 Sized_relobj_file
<size
, big_endian
>* object
,
85 unsigned int data_shndx
,
87 const unsigned char* prelocs
,
89 Output_section
* output_section
,
90 bool needs_special_offset_handling
,
91 size_t local_symbol_count
,
92 const unsigned char* plocal_symbols
);
93 // Finalize the sections.
95 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
97 // Return the value to use for a dynamic which requires special
100 do_dynsym_value(const Symbol
*) const;
102 // Relocate a section.
104 relocate_section(const Relocate_info
<size
, big_endian
>*,
105 unsigned int sh_type
,
106 const unsigned char* prelocs
,
108 Output_section
* output_section
,
109 bool needs_special_offset_handling
,
111 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
112 section_size_type view_size
,
113 const Reloc_symbol_changes
*);
115 // Scan the relocs during a relocatable link.
117 scan_relocatable_relocs(Symbol_table
* symtab
,
119 Sized_relobj_file
<size
, big_endian
>* object
,
120 unsigned int data_shndx
,
121 unsigned int sh_type
,
122 const unsigned char* prelocs
,
124 Output_section
* output_section
,
125 bool needs_special_offset_handling
,
126 size_t local_symbol_count
,
127 const unsigned char* plocal_symbols
,
128 Relocatable_relocs
*);
130 // Relocate a section during a relocatable link.
132 relocate_for_relocatable(const Relocate_info
<size
, big_endian
>*,
133 unsigned int sh_type
,
134 const unsigned char* prelocs
,
136 Output_section
* output_section
,
137 off_t offset_in_output_section
,
138 const Relocatable_relocs
*,
140 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
141 section_size_type view_size
,
142 unsigned char* reloc_view
,
143 section_size_type reloc_view_size
);
145 // Return whether SYM is defined by the ABI.
147 do_is_defined_by_abi(const Symbol
* sym
) const
149 return strcmp(sym
->name(), "___tls_get_addr") == 0;
152 // Return the size of the GOT section.
156 gold_assert(this->got_
!= NULL
);
157 return this->got_
->data_size();
160 // Return the number of entries in the GOT.
162 got_entry_count() const
164 if (this->got_
== NULL
)
166 return this->got_size() / (size
/ 8);
169 // Return the number of entries in the PLT.
171 plt_entry_count() const;
173 // Return the offset of the first non-reserved PLT entry.
175 first_plt_entry_offset() const;
177 // Return the size of each PLT entry.
179 plt_entry_size() const;
183 // The class which scans relocations.
188 : issued_non_pic_error_(false)
192 get_reference_flags(unsigned int r_type
);
195 local(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
196 Sized_relobj_file
<size
, big_endian
>* object
,
197 unsigned int data_shndx
,
198 Output_section
* output_section
,
199 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
200 const elfcpp::Sym
<size
, big_endian
>& lsym
);
203 global(Symbol_table
* symtab
, Layout
* layout
, Target_powerpc
* target
,
204 Sized_relobj_file
<size
, big_endian
>* object
,
205 unsigned int data_shndx
,
206 Output_section
* output_section
,
207 const elfcpp::Rela
<size
, big_endian
>& reloc
, unsigned int r_type
,
211 local_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
213 Sized_relobj_file
<size
, big_endian
>* ,
216 const elfcpp::Rela
<size
, big_endian
>& ,
218 const elfcpp::Sym
<size
, big_endian
>&)
222 global_reloc_may_be_function_pointer(Symbol_table
* , Layout
* ,
224 Sized_relobj_file
<size
, big_endian
>* ,
227 const elfcpp::Rela
<size
,
229 unsigned int , Symbol
*)
234 unsupported_reloc_local(Sized_relobj_file
<size
, big_endian
>*,
235 unsigned int r_type
);
238 unsupported_reloc_global(Sized_relobj_file
<size
, big_endian
>*,
239 unsigned int r_type
, Symbol
*);
242 generate_tls_call(Symbol_table
* symtab
, Layout
* layout
,
243 Target_powerpc
* target
);
246 check_non_pic(Relobj
*, unsigned int r_type
);
248 // Whether we have issued an error about a non-PIC compilation.
249 bool issued_non_pic_error_
;
252 // The class which implements relocation.
256 // Do a relocation. Return false if the caller should not issue
257 // any warnings about this relocation.
259 relocate(const Relocate_info
<size
, big_endian
>*, Target_powerpc
*,
260 Output_section
*, size_t relnum
,
261 const elfcpp::Rela
<size
, big_endian
>&,
262 unsigned int r_type
, const Sized_symbol
<size
>*,
263 const Symbol_value
<size
>*,
265 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
269 // Do a TLS relocation.
271 relocate_tls(const Relocate_info
<size
, big_endian
>*,
272 Target_powerpc
* target
,
273 size_t relnum
, const elfcpp::Rela
<size
, big_endian
>&,
274 unsigned int r_type
, const Sized_symbol
<size
>*,
275 const Symbol_value
<size
>*,
277 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
281 // A class which returns the size required for a relocation type,
282 // used while scanning relocs during a relocatable link.
283 class Relocatable_size_for_reloc
287 get_size_for_reloc(unsigned int, Relobj
*);
290 // Get the GOT section, creating it if necessary.
291 Output_data_got
<size
, big_endian
>*
292 got_section(Symbol_table
*, Layout
*);
297 gold_assert(this->got2_
!= NULL
);
301 // Get the TOC section.
305 gold_assert(this->toc_
!= NULL
);
309 // Create a PLT entry for a global symbol.
311 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
313 // Create a GOT entry for the TLS module index.
315 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
316 Sized_relobj_file
<size
, big_endian
>* object
);
318 // Get the PLT section.
319 const Output_data_plt_powerpc
<size
, big_endian
>*
322 gold_assert(this->plt_
!= NULL
);
326 // Get the dynamic reloc section, creating it if necessary.
328 rela_dyn_section(Layout
*);
330 // Copy a relocation against a global symbol.
332 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
333 Sized_relobj_file
<size
, big_endian
>* object
,
334 unsigned int shndx
, Output_section
* output_section
,
335 Symbol
* sym
, const elfcpp::Rela
<size
, big_endian
>& reloc
)
337 this->copy_relocs_
.copy_reloc(symtab
, layout
,
338 symtab
->get_sized_symbol
<size
>(sym
),
339 object
, shndx
, output_section
,
340 reloc
, this->rela_dyn_section(layout
));
343 // Information about this specific target which we pass to the
344 // general Target structure.
345 static Target::Target_info powerpc_info
;
347 // The types of GOT entries needed for this platform.
348 // These values are exposed to the ABI in an incremental link.
349 // Do not renumber existing values without changing the version
350 // number of the .gnu_incremental_inputs section.
353 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
354 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
355 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
359 Output_data_got
<size
, big_endian
>* got_
;
361 Output_data_space
* got2_
;
363 Output_data_space
* toc_
;
365 Output_data_plt_powerpc
<size
, big_endian
>* plt_
;
366 // The dynamic reloc section.
367 Reloc_section
* rela_dyn_
;
368 // Relocs saved to avoid a COPY reloc.
369 Copy_relocs
<elfcpp::SHT_RELA
, size
, big_endian
> copy_relocs_
;
370 // Space for variables copied with a COPY reloc.
371 Output_data_space
* dynbss_
;
372 // Offset of the GOT entry for the TLS module index;
373 unsigned int got_mod_index_offset_
;
377 Target::Target_info Target_powerpc
<32, true>::powerpc_info
=
380 true, // is_big_endian
381 elfcpp::EM_PPC
, // machine_code
382 false, // has_make_symbol
383 false, // has_resolve
384 false, // has_code_fill
385 true, // is_default_stack_executable
386 false, // can_icf_inline_merge_sections
388 "/usr/lib/ld.so.1", // dynamic_linker
389 0x10000000, // default_text_segment_address
390 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
391 4 * 1024, // common_pagesize (overridable by -z common-page-size)
392 false, // isolate_execinstr
394 elfcpp::SHN_UNDEF
, // small_common_shndx
395 elfcpp::SHN_UNDEF
, // large_common_shndx
396 0, // small_common_section_flags
397 0, // large_common_section_flags
398 NULL
, // attributes_section
399 NULL
// attributes_vendor
403 Target::Target_info Target_powerpc
<32, false>::powerpc_info
=
406 false, // is_big_endian
407 elfcpp::EM_PPC
, // machine_code
408 false, // has_make_symbol
409 false, // has_resolve
410 false, // has_code_fill
411 true, // is_default_stack_executable
412 false, // can_icf_inline_merge_sections
414 "/usr/lib/ld.so.1", // dynamic_linker
415 0x10000000, // default_text_segment_address
416 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
417 4 * 1024, // common_pagesize (overridable by -z common-page-size)
418 false, // isolate_execinstr
420 elfcpp::SHN_UNDEF
, // small_common_shndx
421 elfcpp::SHN_UNDEF
, // large_common_shndx
422 0, // small_common_section_flags
423 0, // large_common_section_flags
424 NULL
, // attributes_section
425 NULL
// attributes_vendor
429 Target::Target_info Target_powerpc
<64, true>::powerpc_info
=
432 true, // is_big_endian
433 elfcpp::EM_PPC64
, // machine_code
434 false, // has_make_symbol
435 false, // has_resolve
436 false, // has_code_fill
437 true, // is_default_stack_executable
438 false, // can_icf_inline_merge_sections
440 "/usr/lib/ld.so.1", // dynamic_linker
441 0x10000000, // default_text_segment_address
442 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
443 8 * 1024, // common_pagesize (overridable by -z common-page-size)
444 false, // isolate_execinstr
446 elfcpp::SHN_UNDEF
, // small_common_shndx
447 elfcpp::SHN_UNDEF
, // large_common_shndx
448 0, // small_common_section_flags
449 0, // large_common_section_flags
450 NULL
, // attributes_section
451 NULL
// attributes_vendor
455 Target::Target_info Target_powerpc
<64, false>::powerpc_info
=
458 false, // is_big_endian
459 elfcpp::EM_PPC64
, // machine_code
460 false, // has_make_symbol
461 false, // has_resolve
462 false, // has_code_fill
463 true, // is_default_stack_executable
464 false, // can_icf_inline_merge_sections
466 "/usr/lib/ld.so.1", // dynamic_linker
467 0x10000000, // default_text_segment_address
468 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
469 8 * 1024, // common_pagesize (overridable by -z common-page-size)
470 false, // isolate_execinstr
472 elfcpp::SHN_UNDEF
, // small_common_shndx
473 elfcpp::SHN_UNDEF
, // large_common_shndx
474 0, // small_common_section_flags
475 0, // large_common_section_flags
476 NULL
, // attributes_section
477 NULL
// attributes_vendor
480 template<int size
, bool big_endian
>
481 class Powerpc_relocate_functions
484 // Do a simple relocation with the addend in the relocation.
485 template<int valsize
>
487 rela(unsigned char* view
,
488 unsigned int right_shift
,
489 elfcpp::Elf_Xword dst_mask
,
490 typename
elfcpp::Swap
<size
, big_endian
>::Valtype value
,
491 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
493 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
494 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
495 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
496 Valtype reloc
= ((value
+ addend
) >> right_shift
);
501 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
504 // Do a simple relocation using a symbol value with the addend in
506 template<int valsize
>
508 rela(unsigned char* view
,
509 unsigned int right_shift
,
510 elfcpp::Elf_Xword dst_mask
,
511 const Sized_relobj_file
<size
, big_endian
>* object
,
512 const Symbol_value
<size
>* psymval
,
513 typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype addend
)
515 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
516 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
517 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
518 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
523 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
526 // Do a simple relocation using a symbol value with the addend in
527 // the relocation, unaligned.
528 template<int valsize
>
530 rela_ua(unsigned char* view
, unsigned int right_shift
,
531 elfcpp::Elf_Xword dst_mask
,
532 const Sized_relobj_file
<size
, big_endian
>* object
,
533 const Symbol_value
<size
>* psymval
,
534 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
)
536 typedef typename
elfcpp::Swap_unaligned
<valsize
,
537 big_endian
>::Valtype Valtype
;
538 unsigned char* wv
= view
;
539 Valtype val
= elfcpp::Swap_unaligned
<valsize
, big_endian
>::readval(wv
);
540 Valtype reloc
= (psymval
->value(object
, addend
) >> right_shift
);
545 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
548 // Do a simple PC relative relocation with a Symbol_value with the
549 // addend in the relocation.
550 template<int valsize
>
552 pcrela(unsigned char* view
, unsigned int right_shift
,
553 elfcpp::Elf_Xword dst_mask
,
554 const Sized_relobj_file
<size
, big_endian
>* object
,
555 const Symbol_value
<size
>* psymval
,
556 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
557 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
559 typedef typename
elfcpp::Swap
<valsize
, big_endian
>::Valtype Valtype
;
560 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
561 Valtype val
= elfcpp::Swap
<valsize
, big_endian
>::readval(wv
);
562 Valtype reloc
= ((psymval
->value(object
, addend
) - address
)
568 elfcpp::Swap
<valsize
, big_endian
>::writeval(wv
, val
| reloc
);
571 template<int valsize
>
573 pcrela_unaligned(unsigned char* view
,
574 const Sized_relobj_file
<size
, big_endian
>* object
,
575 const Symbol_value
<size
>* psymval
,
576 typename
elfcpp::Swap
<size
, big_endian
>::Valtype addend
,
577 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
579 typedef typename
elfcpp::Swap_unaligned
<valsize
,
580 big_endian
>::Valtype Valtype
;
581 unsigned char* wv
= view
;
582 Valtype reloc
= (psymval
->value(object
, addend
) - address
);
584 elfcpp::Swap_unaligned
<valsize
, big_endian
>::writeval(wv
, reloc
);
587 typedef Powerpc_relocate_functions
<size
, big_endian
> This
;
588 typedef Relocate_functions
<size
, big_endian
> This_reloc
;
590 // R_POWERPC_REL32: (Symbol + Addend - Address)
592 rel32(unsigned char* view
,
593 const Sized_relobj_file
<size
, big_endian
>* object
,
594 const Symbol_value
<size
>* psymval
,
595 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
596 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
597 { This_reloc::pcrela32(view
, object
, psymval
, addend
, address
); }
599 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
601 rel24(unsigned char* view
,
602 const Sized_relobj_file
<size
, big_endian
>* object
,
603 const Symbol_value
<size
>* psymval
,
604 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
605 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
607 This::template pcrela
<32>(view
, 0, 0x03fffffc, object
,
608 psymval
, addend
, address
);
611 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
613 rel14(unsigned char* view
,
614 const Sized_relobj_file
<size
, big_endian
>* object
,
615 const Symbol_value
<size
>* psymval
,
616 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
617 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
619 This::template pcrela
<32>(view
, 0, 0x0000fffc, object
,
620 psymval
, addend
, address
);
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
); }
631 addr16(unsigned char* view
,
632 const Sized_relobj_file
<size
, big_endian
>* object
,
633 const Symbol_value
<size
>* psymval
,
634 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
635 { This_reloc::rela16(view
, object
, psymval
, addend
); }
637 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
639 addr16_ds(unsigned char* view
,
640 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
641 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
643 This::template rela
<16>(view
, 0, 0xfffc, value
, addend
);
646 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
648 addr16_lo(unsigned char* view
,
649 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
650 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
651 { This_reloc::rela16(view
, value
, addend
); }
654 addr16_lo(unsigned char* view
,
655 const Sized_relobj_file
<size
, big_endian
>* object
,
656 const Symbol_value
<size
>* psymval
,
657 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
658 { This_reloc::rela16(view
, object
, psymval
, addend
); }
660 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
662 addr16_hi(unsigned char* view
,
663 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
664 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
666 This::template rela
<16>(view
, 16, 0xffff, value
, addend
);
670 addr16_hi(unsigned char* view
,
671 const Sized_relobj_file
<size
, big_endian
>* object
,
672 const Symbol_value
<size
>* psymval
,
673 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
675 This::template rela
<16>(view
, 16, 0xffff, object
, psymval
, addend
);
678 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
679 // final value of the low 16 bits of the
680 // relocation is negative, add one.
682 addr16_ha(unsigned char* view
,
683 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
684 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
686 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
688 reloc
= value
+ addend
;
694 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
698 addr16_ha(unsigned char* view
,
699 const Sized_relobj_file
<size
, big_endian
>* object
,
700 const Symbol_value
<size
>* psymval
,
701 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
)
703 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
705 reloc
= psymval
->value(object
, addend
);
711 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
714 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
716 rel16(unsigned char* view
,
717 const Sized_relobj_file
<size
, big_endian
>* object
,
718 const Symbol_value
<size
>* psymval
,
719 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
720 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
721 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
723 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
725 rel16_lo(unsigned char* view
,
726 const Sized_relobj_file
<size
, big_endian
>* object
,
727 const Symbol_value
<size
>* psymval
,
728 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
729 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
730 { This_reloc::pcrela16(view
, object
, psymval
, addend
, address
); }
732 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
734 rel16_hi(unsigned char* view
,
735 const Sized_relobj_file
<size
, big_endian
>* object
,
736 const Symbol_value
<size
>* psymval
,
737 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
738 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
740 This::template pcrela
<16>(view
, 16, 0xffff, object
,
741 psymval
, addend
, address
);
744 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
745 // final value of the low 16 bits of the
746 // relocation is negative, add one.
748 rel16_ha(unsigned char* view
,
749 const Sized_relobj_file
<size
, big_endian
>* object
,
750 const Symbol_value
<size
>* psymval
,
751 typename
elfcpp::Elf_types
<size
>::Elf_Addr addend
,
752 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
)
754 typename
elfcpp::Elf_types
<size
>::Elf_Addr reloc
;
756 reloc
= (psymval
->value(object
, addend
) - address
);
761 elfcpp::Swap
<16, big_endian
>::writeval(view
, reloc
);
765 // Get the GOT section, creating it if necessary.
767 template<int size
, bool big_endian
>
768 Output_data_got
<size
, big_endian
>*
769 Target_powerpc
<size
, big_endian
>::got_section(Symbol_table
* symtab
,
772 if (this->got_
== NULL
)
774 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
776 this->got_
= new Output_data_got
<size
, big_endian
>();
778 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
779 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
780 this->got_
, ORDER_DATA
, false);
782 // Create the GOT2 or TOC in the .got section.
785 this->got2_
= new Output_data_space(4, "** GOT2");
786 layout
->add_output_section_data(".got2", elfcpp::SHT_PROGBITS
,
789 this->got2_
, ORDER_DATA
, false);
793 this->toc_
= new Output_data_space(8, "** TOC");
794 layout
->add_output_section_data(".toc", elfcpp::SHT_PROGBITS
,
797 this->toc_
, ORDER_DATA
, false);
800 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
801 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
802 Symbol_table::PREDEFINED
,
804 0, 0, elfcpp::STT_OBJECT
,
806 elfcpp::STV_HIDDEN
, 0,
813 // Get the dynamic reloc section, creating it if necessary.
815 template<int size
, bool big_endian
>
816 typename Target_powerpc
<size
, big_endian
>::Reloc_section
*
817 Target_powerpc
<size
, big_endian
>::rela_dyn_section(Layout
* layout
)
819 if (this->rela_dyn_
== NULL
)
821 gold_assert(layout
!= NULL
);
822 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
823 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
824 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
825 ORDER_DYNAMIC_RELOCS
, false);
827 return this->rela_dyn_
;
830 // A class to handle the PLT data.
832 template<int size
, bool big_endian
>
833 class Output_data_plt_powerpc
: public Output_section_data
836 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true,
837 size
, big_endian
> Reloc_section
;
839 Output_data_plt_powerpc(Layout
*);
841 // Add an entry to the PLT.
842 void add_entry(Symbol
* gsym
);
844 // Return the .rela.plt section data.
845 const Reloc_section
* rel_plt() const
850 // Return the number of PLT entries.
853 { return this->count_
; }
855 // Return the offset of the first non-reserved PLT entry.
857 first_plt_entry_offset()
858 { return 4 * base_plt_entry_size
; }
860 // Return the size of a PLT entry.
863 { return base_plt_entry_size
; }
866 void do_adjust_output_section(Output_section
* os
);
869 // The size of an entry in the PLT.
870 static const int base_plt_entry_size
= (size
== 32 ? 16 : 24);
872 // Set the final size.
874 set_final_data_size()
876 unsigned int full_count
= this->count_
+ 4;
878 this->set_data_size(full_count
* base_plt_entry_size
);
881 // Write out the PLT data.
883 do_write(Output_file
*);
885 // The reloc section.
887 // The number of PLT entries.
891 // Create the PLT section. The ordinary .got section is an argument,
892 // since we need to refer to the start.
894 template<int size
, bool big_endian
>
895 Output_data_plt_powerpc
<size
, big_endian
>::Output_data_plt_powerpc(Layout
* layout
)
896 : Output_section_data(size
== 32 ? 4 : 8), count_(0)
898 this->rel_
= new Reloc_section(false);
899 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
900 elfcpp::SHF_ALLOC
, this->rel_
,
901 ORDER_DYNAMIC_PLT_RELOCS
, false);
904 template<int size
, bool big_endian
>
906 Output_data_plt_powerpc
<size
, big_endian
>::do_adjust_output_section(Output_section
* os
)
911 // Add an entry to the PLT.
913 template<int size
, bool big_endian
>
915 Output_data_plt_powerpc
<size
, big_endian
>::add_entry(Symbol
* gsym
)
917 gold_assert(!gsym
->has_plt_offset());
918 unsigned int index
= this->count_
+ + 4;
919 section_offset_type plt_offset
;
922 plt_offset
= index
* base_plt_entry_size
;
926 gsym
->set_plt_offset(plt_offset
);
930 gsym
->set_needs_dynsym_entry();
931 this->rel_
->add_global(gsym
, elfcpp::R_POWERPC_JMP_SLOT
, this,
935 static const unsigned int addis_11_11
= 0x3d6b0000;
936 static const unsigned int addis_11_30
= 0x3d7e0000;
937 static const unsigned int addis_12_12
= 0x3d8c0000;
938 static const unsigned int addi_11_11
= 0x396b0000;
939 static const unsigned int add_0_11_11
= 0x7c0b5a14;
940 static const unsigned int add_11_0_11
= 0x7d605a14;
941 static const unsigned int b
= 0x48000000;
942 static const unsigned int bcl_20_31
= 0x429f0005;
943 static const unsigned int bctr
= 0x4e800420;
944 static const unsigned int lis_11
= 0x3d600000;
945 static const unsigned int lis_12
= 0x3d800000;
946 static const unsigned int lwzu_0_12
= 0x840c0000;
947 static const unsigned int lwz_0_12
= 0x800c0000;
948 static const unsigned int lwz_11_11
= 0x816b0000;
949 static const unsigned int lwz_11_30
= 0x817e0000;
950 static const unsigned int lwz_12_12
= 0x818c0000;
951 static const unsigned int mflr_0
= 0x7c0802a6;
952 static const unsigned int mflr_12
= 0x7d8802a6;
953 static const unsigned int mtctr_0
= 0x7c0903a6;
954 static const unsigned int mtctr_11
= 0x7d6903a6;
955 static const unsigned int mtlr_0
= 0x7c0803a6;
956 static const unsigned int nop
= 0x60000000;
957 static const unsigned int sub_11_11_12
= 0x7d6c5850;
959 static const unsigned int addis_r12_r2
= 0x3d820000; /* addis %r12,%r2,xxx@ha */
960 static const unsigned int std_r2_40r1
= 0xf8410028; /* std %r2,40(%r1) */
961 static const unsigned int ld_r11_0r12
= 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
962 static const unsigned int ld_r2_0r12
= 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
963 /* ld %r11,xxx+16@l(%r12) */
966 // Write out the PLT.
968 template<int size
, bool big_endian
>
970 Output_data_plt_powerpc
<size
, big_endian
>::do_write(Output_file
* of
)
972 const off_t offset
= this->offset();
973 const section_size_type oview_size
=
974 convert_to_section_size_type(this->data_size());
975 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
976 unsigned char* pov
= oview
;
978 memset(pov
, 0, base_plt_entry_size
* 4);
979 pov
+= base_plt_entry_size
* 4;
981 unsigned int plt_offset
= base_plt_entry_size
* 4;
982 const unsigned int count
= this->count_
;
986 for (unsigned int i
= 0; i
< count
; i
++)
992 for (unsigned int i
= 0; i
< count
; i
++)
994 elfcpp::Swap
<32, true>::writeval(pov
+ 0x00,
995 lwz_11_30
+ plt_offset
);
996 elfcpp::Swap
<32, true>::writeval(pov
+ 0x04, mtctr_11
);
997 elfcpp::Swap
<32, true>::writeval(pov
+ 0x08, bctr
);
998 elfcpp::Swap
<32, true>::writeval(pov
+ 0x0c, nop
);
999 pov
+= base_plt_entry_size
;
1000 plt_offset
+= base_plt_entry_size
;
1004 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1006 of
->write_output_view(offset
, oview_size
, oview
);
1009 // Create a PLT entry for a global symbol.
1011 template<int size
, bool big_endian
>
1013 Target_powerpc
<size
, big_endian
>::make_plt_entry(Symbol_table
* symtab
,
1017 if (gsym
->has_plt_offset())
1020 if (this->plt_
== NULL
)
1022 // Create the GOT section first.
1023 this->got_section(symtab
, layout
);
1025 // Ensure that .rela.dyn always appears before .rela.plt This is
1026 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1027 // needs to include .rela.plt in it's range.
1028 this->rela_dyn_section(layout
);
1030 this->plt_
= new Output_data_plt_powerpc
<size
, big_endian
>(layout
);
1031 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1033 | elfcpp::SHF_EXECINSTR
1034 | elfcpp::SHF_WRITE
),
1035 this->plt_
, ORDER_PLT
, false);
1037 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1038 symtab
->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL
,
1039 Symbol_table::PREDEFINED
,
1041 0, 0, elfcpp::STT_OBJECT
,
1043 elfcpp::STV_HIDDEN
, 0,
1047 this->plt_
->add_entry(gsym
);
1050 // Return the number of entries in the PLT.
1052 template<int size
, bool big_endian
>
1054 Target_powerpc
<size
, big_endian
>::plt_entry_count() const
1056 if (this->plt_
== NULL
)
1058 return this->plt_
->entry_count();
1061 // Return the offset of the first non-reserved PLT entry.
1063 template<int size
, bool big_endian
>
1065 Target_powerpc
<size
, big_endian
>::first_plt_entry_offset() const
1067 return Output_data_plt_powerpc
<size
, big_endian
>::first_plt_entry_offset();
1070 // Return the size of each PLT entry.
1072 template<int size
, bool big_endian
>
1074 Target_powerpc
<size
, big_endian
>::plt_entry_size() const
1076 return Output_data_plt_powerpc
<size
, big_endian
>::get_plt_entry_size();
1079 // Create a GOT entry for the TLS module index.
1081 template<int size
, bool big_endian
>
1083 Target_powerpc
<size
, big_endian
>::got_mod_index_entry(
1084 Symbol_table
* symtab
,
1086 Sized_relobj_file
<size
, big_endian
>* object
)
1088 if (this->got_mod_index_offset_
== -1U)
1090 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1091 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1092 Output_data_got
<size
, big_endian
>* got
;
1093 unsigned int got_offset
;
1095 got
= this->got_section(symtab
, layout
);
1096 got_offset
= got
->add_constant(0);
1097 rela_dyn
->add_local(object
, 0, elfcpp::R_POWERPC_DTPMOD
, got
,
1099 got
->add_constant(0);
1100 this->got_mod_index_offset_
= got_offset
;
1102 return this->got_mod_index_offset_
;
1105 // Optimize the TLS relocation type based on what we know about the
1106 // symbol. IS_FINAL is true if the final address of this symbol is
1107 // known at link time.
1109 static tls::Tls_optimization
1110 optimize_tls_reloc(bool /* is_final */, int r_type
)
1112 // If we are generating a shared library, then we can't do anything
1114 if (parameters
->options().shared())
1115 return tls::TLSOPT_NONE
;
1124 // Get the Reference_flags for a particular relocation.
1126 template<int size
, bool big_endian
>
1128 Target_powerpc
<size
, big_endian
>::Scan::get_reference_flags(
1129 unsigned int r_type
)
1133 case elfcpp::R_POWERPC_NONE
:
1134 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1135 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1136 case elfcpp::R_PPC64_TOC
:
1137 // No symbol reference.
1140 case elfcpp::R_POWERPC_ADDR16
:
1141 case elfcpp::R_POWERPC_ADDR16_LO
:
1142 case elfcpp::R_POWERPC_ADDR16_HI
:
1143 case elfcpp::R_POWERPC_ADDR16_HA
:
1144 case elfcpp::R_POWERPC_ADDR32
:
1145 case elfcpp::R_PPC64_ADDR64
:
1146 return Symbol::ABSOLUTE_REF
;
1148 case elfcpp::R_POWERPC_REL24
:
1149 case elfcpp::R_PPC_LOCAL24PC
:
1150 case elfcpp::R_PPC_REL16
:
1151 case elfcpp::R_PPC_REL16_LO
:
1152 case elfcpp::R_PPC_REL16_HI
:
1153 case elfcpp::R_PPC_REL16_HA
:
1154 return Symbol::RELATIVE_REF
;
1156 case elfcpp::R_PPC_PLTREL24
:
1157 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1159 case elfcpp::R_POWERPC_GOT16
:
1160 case elfcpp::R_POWERPC_GOT16_LO
:
1161 case elfcpp::R_POWERPC_GOT16_HI
:
1162 case elfcpp::R_POWERPC_GOT16_HA
:
1163 case elfcpp::R_PPC64_TOC16
:
1164 case elfcpp::R_PPC64_TOC16_LO
:
1165 case elfcpp::R_PPC64_TOC16_HI
:
1166 case elfcpp::R_PPC64_TOC16_HA
:
1167 case elfcpp::R_PPC64_TOC16_DS
:
1168 case elfcpp::R_PPC64_TOC16_LO_DS
:
1170 return Symbol::ABSOLUTE_REF
;
1172 case elfcpp::R_POWERPC_GOT_TPREL16
:
1173 case elfcpp::R_POWERPC_TLS
:
1174 return Symbol::TLS_REF
;
1176 case elfcpp::R_POWERPC_COPY
:
1177 case elfcpp::R_POWERPC_GLOB_DAT
:
1178 case elfcpp::R_POWERPC_JMP_SLOT
:
1179 case elfcpp::R_POWERPC_RELATIVE
:
1180 case elfcpp::R_POWERPC_DTPMOD
:
1182 // Not expected. We will give an error later.
1187 // Report an unsupported relocation against a local symbol.
1189 template<int size
, bool big_endian
>
1191 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_local(
1192 Sized_relobj_file
<size
, big_endian
>* object
,
1193 unsigned int r_type
)
1195 gold_error(_("%s: unsupported reloc %u against local symbol"),
1196 object
->name().c_str(), r_type
);
1199 // We are about to emit a dynamic relocation of type R_TYPE. If the
1200 // dynamic linker does not support it, issue an error.
1202 template<int size
, bool big_endian
>
1204 Target_powerpc
<size
, big_endian
>::Scan::check_non_pic(Relobj
* object
,
1205 unsigned int r_type
)
1207 gold_assert(r_type
!= elfcpp::R_POWERPC_NONE
);
1209 // These are the relocation types supported by glibc for both 32-bit
1210 // and 64-bit powerpc.
1213 case elfcpp::R_POWERPC_RELATIVE
:
1214 case elfcpp::R_POWERPC_GLOB_DAT
:
1215 case elfcpp::R_POWERPC_DTPMOD
:
1216 case elfcpp::R_POWERPC_DTPREL
:
1217 case elfcpp::R_POWERPC_TPREL
:
1218 case elfcpp::R_POWERPC_JMP_SLOT
:
1219 case elfcpp::R_POWERPC_COPY
:
1220 case elfcpp::R_POWERPC_ADDR32
:
1221 case elfcpp::R_POWERPC_ADDR24
:
1222 case elfcpp::R_POWERPC_REL24
:
1233 // These are the relocation types supported only on 64-bit.
1234 case elfcpp::R_PPC64_ADDR64
:
1235 case elfcpp::R_PPC64_TPREL16_LO_DS
:
1236 case elfcpp::R_PPC64_TPREL16_DS
:
1237 case elfcpp::R_POWERPC_TPREL16
:
1238 case elfcpp::R_POWERPC_TPREL16_LO
:
1239 case elfcpp::R_POWERPC_TPREL16_HI
:
1240 case elfcpp::R_POWERPC_TPREL16_HA
:
1241 case elfcpp::R_PPC64_TPREL16_HIGHER
:
1242 case elfcpp::R_PPC64_TPREL16_HIGHEST
:
1243 case elfcpp::R_PPC64_TPREL16_HIGHERA
:
1244 case elfcpp::R_PPC64_TPREL16_HIGHESTA
:
1245 case elfcpp::R_PPC64_ADDR16_LO_DS
:
1246 case elfcpp::R_POWERPC_ADDR16_LO
:
1247 case elfcpp::R_POWERPC_ADDR16_HI
:
1248 case elfcpp::R_POWERPC_ADDR16_HA
:
1249 case elfcpp::R_POWERPC_ADDR30
:
1250 case elfcpp::R_PPC64_UADDR64
:
1251 case elfcpp::R_POWERPC_UADDR32
:
1252 case elfcpp::R_POWERPC_ADDR16
:
1253 case elfcpp::R_POWERPC_UADDR16
:
1254 case elfcpp::R_PPC64_ADDR16_DS
:
1255 case elfcpp::R_PPC64_ADDR16_HIGHER
:
1256 case elfcpp::R_PPC64_ADDR16_HIGHEST
:
1257 case elfcpp::R_PPC64_ADDR16_HIGHERA
:
1258 case elfcpp::R_PPC64_ADDR16_HIGHESTA
:
1259 case elfcpp::R_POWERPC_ADDR14_BRTAKEN
:
1260 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN
:
1261 case elfcpp::R_POWERPC_REL32
:
1262 case elfcpp::R_PPC64_REL64
:
1273 // These are the relocation types supported only on 32-bit.
1280 // This prevents us from issuing more than one error per reloc
1281 // section. But we can still wind up issuing more than one
1282 // error per object file.
1283 if (this->issued_non_pic_error_
)
1285 gold_assert(parameters
->options().output_is_position_independent());
1286 object
->error(_("requires unsupported dynamic reloc; "
1287 "recompile with -fPIC"));
1288 this->issued_non_pic_error_
= true;
1292 // Scan a relocation for a local symbol.
1294 template<int size
, bool big_endian
>
1296 Target_powerpc
<size
, big_endian
>::Scan::local(
1297 Symbol_table
* symtab
,
1299 Target_powerpc
<size
, big_endian
>* target
,
1300 Sized_relobj_file
<size
, big_endian
>* object
,
1301 unsigned int data_shndx
,
1302 Output_section
* output_section
,
1303 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1304 unsigned int r_type
,
1305 const elfcpp::Sym
<size
, big_endian
>& lsym
)
1309 case elfcpp::R_POWERPC_NONE
:
1310 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1311 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1314 case elfcpp::R_PPC64_ADDR64
:
1315 case elfcpp::R_POWERPC_ADDR32
:
1316 case elfcpp::R_POWERPC_ADDR16_HA
:
1317 case elfcpp::R_POWERPC_ADDR16_LO
:
1318 // If building a shared library (or a position-independent
1319 // executable), we need to create a dynamic relocation for
1321 if (parameters
->options().output_is_position_independent())
1323 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1325 check_non_pic(object
, r_type
);
1326 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1328 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1329 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1330 data_shndx
, reloc
.get_r_offset(),
1331 reloc
.get_r_addend());
1335 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1336 gold_assert(lsym
.get_st_value() == 0);
1337 rela_dyn
->add_local_relative(object
, r_sym
, r_type
,
1338 output_section
, data_shndx
,
1339 reloc
.get_r_offset(),
1340 reloc
.get_r_addend(), false);
1345 case elfcpp::R_POWERPC_REL24
:
1346 case elfcpp::R_PPC_LOCAL24PC
:
1347 case elfcpp::R_POWERPC_REL32
:
1348 case elfcpp::R_PPC_REL16_LO
:
1349 case elfcpp::R_PPC_REL16_HA
:
1352 case elfcpp::R_POWERPC_GOT16
:
1353 case elfcpp::R_POWERPC_GOT16_LO
:
1354 case elfcpp::R_POWERPC_GOT16_HI
:
1355 case elfcpp::R_POWERPC_GOT16_HA
:
1356 case elfcpp::R_PPC64_TOC16
:
1357 case elfcpp::R_PPC64_TOC16_LO
:
1358 case elfcpp::R_PPC64_TOC16_HI
:
1359 case elfcpp::R_PPC64_TOC16_HA
:
1360 case elfcpp::R_PPC64_TOC16_DS
:
1361 case elfcpp::R_PPC64_TOC16_LO_DS
:
1363 // The symbol requires a GOT entry.
1364 Output_data_got
<size
, big_endian
>* got
;
1367 got
= target
->got_section(symtab
, layout
);
1368 r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
1370 // If we are generating a shared object, we need to add a
1371 // dynamic relocation for this symbol's GOT entry.
1372 if (parameters
->options().output_is_position_independent())
1374 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
))
1376 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1379 off
= got
->add_constant(0);
1380 object
->set_local_got_offset(r_sym
, GOT_TYPE_STANDARD
, off
);
1381 rela_dyn
->add_local_relative(object
, r_sym
,
1382 elfcpp::R_POWERPC_RELATIVE
,
1383 got
, off
, 0, false);
1387 got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1391 case elfcpp::R_PPC64_TOC
:
1392 // We need a GOT section.
1393 target
->got_section(symtab
, layout
);
1396 // These are relocations which should only be seen by the
1397 // dynamic linker, and should never be seen here.
1398 case elfcpp::R_POWERPC_COPY
:
1399 case elfcpp::R_POWERPC_GLOB_DAT
:
1400 case elfcpp::R_POWERPC_JMP_SLOT
:
1401 case elfcpp::R_POWERPC_RELATIVE
:
1402 case elfcpp::R_POWERPC_DTPMOD
:
1403 gold_error(_("%s: unexpected reloc %u in object file"),
1404 object
->name().c_str(), r_type
);
1408 unsupported_reloc_local(object
, r_type
);
1413 // Report an unsupported relocation against a global symbol.
1415 template<int size
, bool big_endian
>
1417 Target_powerpc
<size
, big_endian
>::Scan::unsupported_reloc_global(
1418 Sized_relobj_file
<size
, big_endian
>* object
,
1419 unsigned int r_type
,
1422 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1423 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1426 // Scan a relocation for a global symbol.
1428 template<int size
, bool big_endian
>
1430 Target_powerpc
<size
, big_endian
>::Scan::global(
1431 Symbol_table
* symtab
,
1433 Target_powerpc
<size
, big_endian
>* target
,
1434 Sized_relobj_file
<size
, big_endian
>* object
,
1435 unsigned int data_shndx
,
1436 Output_section
* output_section
,
1437 const elfcpp::Rela
<size
, big_endian
>& reloc
,
1438 unsigned int r_type
,
1443 case elfcpp::R_POWERPC_NONE
:
1444 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1445 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1448 case elfcpp::R_PPC_PLTREL24
:
1449 // If the symbol is fully resolved, this is just a PC32 reloc.
1450 // Otherwise we need a PLT entry.
1451 if (gsym
->final_value_is_known())
1453 // If building a shared library, we can also skip the PLT entry
1454 // if the symbol is defined in the output file and is protected
1456 if (gsym
->is_defined()
1457 && !gsym
->is_from_dynobj()
1458 && !gsym
->is_preemptible())
1460 target
->make_plt_entry(symtab
, layout
, gsym
);
1463 case elfcpp::R_POWERPC_ADDR16
:
1464 case elfcpp::R_POWERPC_ADDR16_LO
:
1465 case elfcpp::R_POWERPC_ADDR16_HI
:
1466 case elfcpp::R_POWERPC_ADDR16_HA
:
1467 case elfcpp::R_POWERPC_ADDR32
:
1468 case elfcpp::R_PPC64_ADDR64
:
1470 // Make a PLT entry if necessary.
1471 if (gsym
->needs_plt_entry())
1473 target
->make_plt_entry(symtab
, layout
, gsym
);
1474 // Since this is not a PC-relative relocation, we may be
1475 // taking the address of a function. In that case we need to
1476 // set the entry in the dynamic symbol table to the address of
1478 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1479 gsym
->set_needs_dynsym_value();
1481 // Make a dynamic relocation if necessary.
1482 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1484 if (gsym
->may_need_copy_reloc())
1486 target
->copy_reloc(symtab
, layout
, object
,
1487 data_shndx
, output_section
, gsym
, reloc
);
1489 else if ((r_type
== elfcpp::R_POWERPC_ADDR32
1490 || r_type
== elfcpp::R_PPC64_ADDR64
)
1491 && gsym
->can_use_relative_reloc(false))
1493 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1494 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1495 output_section
, object
,
1496 data_shndx
, reloc
.get_r_offset(),
1497 reloc
.get_r_addend(), false);
1501 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1503 check_non_pic(object
, r_type
);
1504 if (gsym
->is_from_dynobj()
1505 || gsym
->is_undefined()
1506 || gsym
->is_preemptible())
1507 rela_dyn
->add_global(gsym
, r_type
, output_section
,
1509 reloc
.get_r_offset(),
1510 reloc
.get_r_addend());
1512 rela_dyn
->add_global_relative(gsym
, r_type
,
1513 output_section
, object
,
1515 reloc
.get_r_offset(),
1516 reloc
.get_r_addend(), false);
1522 case elfcpp::R_POWERPC_REL24
:
1523 case elfcpp::R_PPC_LOCAL24PC
:
1524 case elfcpp::R_PPC_REL16
:
1525 case elfcpp::R_PPC_REL16_LO
:
1526 case elfcpp::R_PPC_REL16_HI
:
1527 case elfcpp::R_PPC_REL16_HA
:
1529 if (gsym
->needs_plt_entry())
1530 target
->make_plt_entry(symtab
, layout
, gsym
);
1531 // Make a dynamic relocation if necessary.
1532 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1534 if (gsym
->may_need_copy_reloc())
1536 target
->copy_reloc(symtab
, layout
, object
,
1537 data_shndx
, output_section
, gsym
,
1542 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1543 check_non_pic(object
, r_type
);
1544 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1545 data_shndx
, reloc
.get_r_offset(),
1546 reloc
.get_r_addend());
1552 case elfcpp::R_POWERPC_GOT16
:
1553 case elfcpp::R_POWERPC_GOT16_LO
:
1554 case elfcpp::R_POWERPC_GOT16_HI
:
1555 case elfcpp::R_POWERPC_GOT16_HA
:
1556 case elfcpp::R_PPC64_TOC16
:
1557 case elfcpp::R_PPC64_TOC16_LO
:
1558 case elfcpp::R_PPC64_TOC16_HI
:
1559 case elfcpp::R_PPC64_TOC16_HA
:
1560 case elfcpp::R_PPC64_TOC16_DS
:
1561 case elfcpp::R_PPC64_TOC16_LO_DS
:
1563 // The symbol requires a GOT entry.
1564 Output_data_got
<size
, big_endian
>* got
;
1566 got
= target
->got_section(symtab
, layout
);
1567 if (gsym
->final_value_is_known())
1568 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1571 // If this symbol is not fully resolved, we need to add a
1572 // dynamic relocation for it.
1573 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1574 if (gsym
->is_from_dynobj()
1575 || gsym
->is_undefined()
1576 || gsym
->is_preemptible())
1577 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1578 elfcpp::R_POWERPC_GLOB_DAT
);
1579 else if (!gsym
->has_got_offset(GOT_TYPE_STANDARD
))
1581 unsigned int off
= got
->add_constant(0);
1583 gsym
->set_got_offset(GOT_TYPE_STANDARD
, off
);
1584 rela_dyn
->add_global_relative(gsym
, elfcpp::R_POWERPC_RELATIVE
,
1585 got
, off
, 0, false);
1591 case elfcpp::R_PPC64_TOC
:
1592 // We need a GOT section.
1593 target
->got_section(symtab
, layout
);
1596 case elfcpp::R_POWERPC_GOT_TPREL16
:
1597 case elfcpp::R_POWERPC_TLS
:
1601 // These are relocations which should only be seen by the
1602 // dynamic linker, and should never be seen here.
1603 case elfcpp::R_POWERPC_COPY
:
1604 case elfcpp::R_POWERPC_GLOB_DAT
:
1605 case elfcpp::R_POWERPC_JMP_SLOT
:
1606 case elfcpp::R_POWERPC_RELATIVE
:
1607 case elfcpp::R_POWERPC_DTPMOD
:
1608 gold_error(_("%s: unexpected reloc %u in object file"),
1609 object
->name().c_str(), r_type
);
1613 unsupported_reloc_global(object
, r_type
, gsym
);
1618 // Process relocations for gc.
1620 template<int size
, bool big_endian
>
1622 Target_powerpc
<size
, big_endian
>::gc_process_relocs(
1623 Symbol_table
* symtab
,
1625 Sized_relobj_file
<size
, big_endian
>* object
,
1626 unsigned int data_shndx
,
1628 const unsigned char* prelocs
,
1630 Output_section
* output_section
,
1631 bool needs_special_offset_handling
,
1632 size_t local_symbol_count
,
1633 const unsigned char* plocal_symbols
)
1635 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1636 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1638 gold::gc_process_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
,
1639 typename
Target_powerpc::Relocatable_size_for_reloc
>(
1648 needs_special_offset_handling
,
1653 // Scan relocations for a section.
1655 template<int size
, bool big_endian
>
1657 Target_powerpc
<size
, big_endian
>::scan_relocs(
1658 Symbol_table
* symtab
,
1660 Sized_relobj_file
<size
, big_endian
>* object
,
1661 unsigned int data_shndx
,
1662 unsigned int sh_type
,
1663 const unsigned char* prelocs
,
1665 Output_section
* output_section
,
1666 bool needs_special_offset_handling
,
1667 size_t local_symbol_count
,
1668 const unsigned char* plocal_symbols
)
1670 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
1671 typedef typename Target_powerpc
<size
, big_endian
>::Scan Scan
;
1672 static Output_data_space
* sdata
;
1674 if (sh_type
== elfcpp::SHT_REL
)
1676 gold_error(_("%s: unsupported REL reloc section"),
1677 object
->name().c_str());
1681 // Define _SDA_BASE_ at the start of the .sdata section.
1684 // layout->find_output_section(".sdata") == NULL
1685 sdata
= new Output_data_space(4, "** sdata");
1686 Output_section
* os
= layout
->add_output_section_data(".sdata", 0,
1688 | elfcpp::SHF_WRITE
,
1692 symtab
->define_in_output_data("_SDA_BASE_", NULL
,
1693 Symbol_table::PREDEFINED
,
1698 elfcpp::STV_HIDDEN
, 0,
1702 gold::scan_relocs
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
, Scan
>(
1711 needs_special_offset_handling
,
1716 // Finalize the sections.
1718 template<int size
, bool big_endian
>
1720 Target_powerpc
<size
, big_endian
>::do_finalize_sections(
1722 const Input_objects
*,
1725 // Fill in some more dynamic tags.
1726 const Reloc_section
* rel_plt
= (this->plt_
== NULL
1728 : this->plt_
->rel_plt());
1729 layout
->add_target_dynamic_tags(false, this->plt_
, rel_plt
,
1730 this->rela_dyn_
, true, size
== 32);
1732 // Emit any relocs we saved in an attempt to avoid generating COPY
1734 if (this->copy_relocs_
.any_saved_relocs())
1735 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1738 // Perform a relocation.
1740 template<int size
, bool big_endian
>
1742 Target_powerpc
<size
, big_endian
>::Relocate::relocate(
1743 const Relocate_info
<size
, big_endian
>* relinfo
,
1744 Target_powerpc
* target
,
1747 const elfcpp::Rela
<size
, big_endian
>& rela
,
1748 unsigned int r_type
,
1749 const Sized_symbol
<size
>* gsym
,
1750 const Symbol_value
<size
>* psymval
,
1751 unsigned char* view
,
1752 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1753 section_size_type
/* view_size */)
1755 const unsigned int toc_base_offset
= 0x8000;
1756 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1758 // Pick the value to use for symbols defined in shared objects.
1759 Symbol_value
<size
> symval
;
1761 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
1763 elfcpp::Elf_Xword value
;
1765 value
= target
->plt_section()->address() + gsym
->plt_offset();
1767 symval
.set_output_value(value
);
1772 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
1773 elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1775 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1776 // pointer points to the beginning, not the end, of the table.
1777 // So we just use the plain offset.
1778 unsigned int got_offset
= 0;
1779 unsigned int got2_offset
= 0;
1782 case elfcpp::R_PPC64_TOC16
:
1783 case elfcpp::R_PPC64_TOC16_LO
:
1784 case elfcpp::R_PPC64_TOC16_HI
:
1785 case elfcpp::R_PPC64_TOC16_HA
:
1786 case elfcpp::R_PPC64_TOC16_DS
:
1787 case elfcpp::R_PPC64_TOC16_LO_DS
:
1788 // Subtract the TOC base address.
1789 addend
-= target
->toc_section()->address() + toc_base_offset
;
1792 case elfcpp::R_POWERPC_GOT16
:
1793 case elfcpp::R_POWERPC_GOT16_LO
:
1794 case elfcpp::R_POWERPC_GOT16_HI
:
1795 case elfcpp::R_POWERPC_GOT16_HA
:
1796 case elfcpp::R_PPC64_GOT16_DS
:
1797 case elfcpp::R_PPC64_GOT16_LO_DS
:
1800 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1801 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1805 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
1806 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1807 got_offset
= object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1811 // R_PPC_PLTREL24 is rather special. If non-zero,
1812 // the addend specifies the GOT pointer offset within .got2.
1813 case elfcpp::R_PPC_PLTREL24
:
1814 if (addend
>= 32768)
1816 Output_data_space
* got2
;
1817 got2
= target
->got2_section();
1818 got2_offset
= got2
->offset();
1819 addend
+= got2_offset
;
1829 case elfcpp::R_POWERPC_NONE
:
1830 case elfcpp::R_POWERPC_GNU_VTINHERIT
:
1831 case elfcpp::R_POWERPC_GNU_VTENTRY
:
1834 case elfcpp::R_POWERPC_REL32
:
1835 Reloc::rel32(view
, object
, psymval
, addend
, address
);
1838 case elfcpp::R_POWERPC_REL24
:
1839 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1842 case elfcpp::R_POWERPC_REL14
:
1843 Reloc::rel14(view
, object
, psymval
, addend
, address
);
1846 case elfcpp::R_PPC_PLTREL24
:
1847 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1850 case elfcpp::R_PPC_LOCAL24PC
:
1851 Reloc::rel24(view
, object
, psymval
, addend
, address
);
1854 case elfcpp::R_PPC64_ADDR64
:
1855 if (!parameters
->options().output_is_position_independent())
1856 Relocate_functions
<size
, big_endian
>::rela64(view
, object
,
1860 case elfcpp::R_POWERPC_ADDR32
:
1861 if (!parameters
->options().output_is_position_independent())
1862 Relocate_functions
<size
, big_endian
>::rela32(view
, object
,
1866 case elfcpp::R_POWERPC_ADDR16_LO
:
1867 Reloc::addr16_lo(view
, object
, psymval
, addend
);
1870 case elfcpp::R_POWERPC_ADDR16_HI
:
1871 Reloc::addr16_hi(view
, object
, psymval
, addend
);
1874 case elfcpp::R_POWERPC_ADDR16_HA
:
1875 Reloc::addr16_ha(view
, object
, psymval
, addend
);
1878 case elfcpp::R_PPC_REL16_LO
:
1879 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1882 case elfcpp::R_PPC_REL16_HI
:
1883 Reloc::rel16_lo(view
, object
, psymval
, addend
, address
);
1886 case elfcpp::R_PPC_REL16_HA
:
1887 Reloc::rel16_ha(view
, object
, psymval
, addend
, address
);
1890 case elfcpp::R_POWERPC_GOT16
:
1891 Reloc::addr16(view
, got_offset
, addend
);
1894 case elfcpp::R_POWERPC_GOT16_LO
:
1895 Reloc::addr16_lo(view
, got_offset
, addend
);
1898 case elfcpp::R_POWERPC_GOT16_HI
:
1899 Reloc::addr16_hi(view
, got_offset
, addend
);
1902 case elfcpp::R_POWERPC_GOT16_HA
:
1903 Reloc::addr16_ha(view
, got_offset
, addend
);
1906 case elfcpp::R_PPC64_TOC16
:
1907 Reloc::addr16(view
, got_offset
, addend
);
1910 case elfcpp::R_PPC64_TOC16_LO
:
1911 Reloc::addr16_lo(view
, got_offset
, addend
);
1914 case elfcpp::R_PPC64_TOC16_HI
:
1915 Reloc::addr16_hi(view
, got_offset
, addend
);
1918 case elfcpp::R_PPC64_TOC16_HA
:
1919 Reloc::addr16_ha(view
, got_offset
, addend
);
1922 case elfcpp::R_PPC64_TOC16_DS
:
1923 case elfcpp::R_PPC64_TOC16_LO_DS
:
1924 Reloc::addr16_ds(view
, got_offset
, addend
);
1927 case elfcpp::R_PPC64_TOC
:
1929 elfcpp::Elf_types
<64>::Elf_Addr value
;
1930 value
= target
->toc_section()->address() + toc_base_offset
;
1931 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1935 case elfcpp::R_POWERPC_COPY
:
1936 case elfcpp::R_POWERPC_GLOB_DAT
:
1937 case elfcpp::R_POWERPC_JMP_SLOT
:
1938 case elfcpp::R_POWERPC_RELATIVE
:
1939 // This is an outstanding tls reloc, which is unexpected when
1941 case elfcpp::R_POWERPC_DTPMOD
:
1942 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1943 _("unexpected reloc %u in object file"),
1948 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1949 _("unsupported reloc %u"),
1957 // Perform a TLS relocation.
1959 template<int size
, bool big_endian
>
1961 Target_powerpc
<size
, big_endian
>::Relocate::relocate_tls(
1962 const Relocate_info
<size
, big_endian
>* relinfo
,
1963 Target_powerpc
<size
, big_endian
>* target
,
1965 const elfcpp::Rela
<size
, big_endian
>& rela
,
1966 unsigned int r_type
,
1967 const Sized_symbol
<size
>* gsym
,
1968 const Symbol_value
<size
>* psymval
,
1969 unsigned char* view
,
1970 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
1973 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1974 typedef Powerpc_relocate_functions
<size
, big_endian
> Reloc
;
1975 const Sized_relobj_file
<size
, big_endian
>* object
= relinfo
->object
;
1977 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1978 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(object
, 0);
1980 const bool is_final
=
1982 ? !parameters
->options().output_is_position_independent()
1983 : gsym
->final_value_is_known());
1984 const tls::Tls_optimization optimized_type
1985 = optimize_tls_reloc(is_final
, r_type
);
1993 // Relocate section data.
1995 template<int size
, bool big_endian
>
1997 Target_powerpc
<size
, big_endian
>::relocate_section(
1998 const Relocate_info
<size
, big_endian
>* relinfo
,
1999 unsigned int sh_type
,
2000 const unsigned char* prelocs
,
2002 Output_section
* output_section
,
2003 bool needs_special_offset_handling
,
2004 unsigned char* view
,
2005 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2006 section_size_type view_size
,
2007 const Reloc_symbol_changes
* reloc_symbol_changes
)
2009 typedef Target_powerpc
<size
, big_endian
> Powerpc
;
2010 typedef typename Target_powerpc
<size
, big_endian
>::Relocate Powerpc_relocate
;
2012 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2014 gold::relocate_section
<size
, big_endian
, Powerpc
, elfcpp::SHT_RELA
,
2021 needs_special_offset_handling
,
2025 reloc_symbol_changes
);
2028 // Return the size of a relocation while scanning during a relocatable
2031 template<int size
, bool big_endian
>
2033 Target_powerpc
<size
, big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
2037 // We are always SHT_RELA, so we should never get here.
2042 // Scan the relocs during a relocatable link.
2044 template<int size
, bool big_endian
>
2046 Target_powerpc
<size
, big_endian
>::scan_relocatable_relocs(
2047 Symbol_table
* symtab
,
2049 Sized_relobj_file
<size
, big_endian
>* object
,
2050 unsigned int data_shndx
,
2051 unsigned int sh_type
,
2052 const unsigned char* prelocs
,
2054 Output_section
* output_section
,
2055 bool needs_special_offset_handling
,
2056 size_t local_symbol_count
,
2057 const unsigned char* plocal_symbols
,
2058 Relocatable_relocs
* rr
)
2060 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2062 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2063 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2065 gold::scan_relocatable_relocs
<size
, big_endian
, elfcpp::SHT_RELA
,
2066 Scan_relocatable_relocs
>(
2074 needs_special_offset_handling
,
2080 // Relocate a section during a relocatable link.
2082 template<int size
, bool big_endian
>
2084 Target_powerpc
<size
, big_endian
>::relocate_for_relocatable(
2085 const Relocate_info
<size
, big_endian
>* relinfo
,
2086 unsigned int sh_type
,
2087 const unsigned char* prelocs
,
2089 Output_section
* output_section
,
2090 off_t offset_in_output_section
,
2091 const Relocatable_relocs
* rr
,
2092 unsigned char* view
,
2093 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
2094 section_size_type view_size
,
2095 unsigned char* reloc_view
,
2096 section_size_type reloc_view_size
)
2098 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2100 gold::relocate_for_relocatable
<size
, big_endian
, elfcpp::SHT_RELA
>(
2105 offset_in_output_section
,
2114 // Return the value to use for a dynamic which requires special
2115 // treatment. This is how we support equality comparisons of function
2116 // pointers across shared library boundaries, as described in the
2117 // processor specific ABI supplement.
2119 template<int size
, bool big_endian
>
2121 Target_powerpc
<size
, big_endian
>::do_dynsym_value(const Symbol
* gsym
) const
2123 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2124 return this->plt_section()->address() + gsym
->plt_offset();
2127 // The selector for powerpc object files.
2129 template<int size
, bool big_endian
>
2130 class Target_selector_powerpc
: public Target_selector
2133 Target_selector_powerpc()
2134 : Target_selector(elfcpp::EM_NONE
, size
, big_endian
,
2136 ? (big_endian
? "elf64-powerpc" : "elf64-powerpcle")
2137 : (big_endian
? "elf32-powerpc" : "elf32-powerpcle")),
2139 ? (big_endian
? "elf64ppc" : "elf64lppc")
2140 : (big_endian
? "elf32ppc" : "elf32lppc")))
2144 do_recognize(Input_file
*, off_t
, int machine
, int, int)
2149 if (machine
!= elfcpp::EM_PPC64
)
2154 if (machine
!= elfcpp::EM_PPC
)
2162 return this->instantiate_target();
2166 do_instantiate_target()
2167 { return new Target_powerpc
<size
, big_endian
>(); }
2170 Target_selector_powerpc
<32, true> target_selector_ppc32
;
2171 Target_selector_powerpc
<32, false> target_selector_ppc32le
;
2172 Target_selector_powerpc
<64, true> target_selector_ppc64
;
2173 Target_selector_powerpc
<64, false> target_selector_ppc64le
;
2175 } // End anonymous namespace.