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 typename elfcpp::Elf_types<size>::Elf_Off
138 offset_in_output_section,
139 const Relocatable_relocs*,
141 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
142 section_size_type view_size,
143 unsigned char* reloc_view,
144 section_size_type reloc_view_size);
146 // Return whether SYM is defined by the ABI.
148 do_is_defined_by_abi(const Symbol* sym) const
150 return strcmp(sym->name(), "___tls_get_addr") == 0;
153 // Return the size of the GOT section.
157 gold_assert(this->got_ != NULL);
158 return this->got_->data_size();
161 // Return the number of entries in the GOT.
163 got_entry_count() const
165 if (this->got_ == NULL)
167 return this->got_size() / (size / 8);
170 // Return the number of entries in the PLT.
172 plt_entry_count() const;
174 // Return the offset of the first non-reserved PLT entry.
176 first_plt_entry_offset() const;
178 // Return the size of each PLT entry.
180 plt_entry_size() const;
184 // The class which scans relocations.
189 : issued_non_pic_error_(false)
193 get_reference_flags(unsigned int r_type);
196 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
197 Sized_relobj_file<size, big_endian>* object,
198 unsigned int data_shndx,
199 Output_section* output_section,
200 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
201 const elfcpp::Sym<size, big_endian>& lsym);
204 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
205 Sized_relobj_file<size, big_endian>* object,
206 unsigned int data_shndx,
207 Output_section* output_section,
208 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
212 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
214 Sized_relobj_file<size, big_endian>* ,
217 const elfcpp::Rela<size, big_endian>& ,
219 const elfcpp::Sym<size, big_endian>&)
223 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
225 Sized_relobj_file<size, big_endian>* ,
228 const elfcpp::Rela<size,
230 unsigned int , Symbol*)
235 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
236 unsigned int r_type);
239 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
240 unsigned int r_type, Symbol*);
243 generate_tls_call(Symbol_table* symtab, Layout* layout,
244 Target_powerpc* target);
247 check_non_pic(Relobj*, unsigned int r_type);
249 // Whether we have issued an error about a non-PIC compilation.
250 bool issued_non_pic_error_;
253 // The class which implements relocation.
257 // Do a relocation. Return false if the caller should not issue
258 // any warnings about this relocation.
260 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
261 Output_section*, size_t relnum,
262 const elfcpp::Rela<size, big_endian>&,
263 unsigned int r_type, const Sized_symbol<size>*,
264 const Symbol_value<size>*,
266 typename elfcpp::Elf_types<size>::Elf_Addr,
270 // Do a TLS relocation.
272 relocate_tls(const Relocate_info<size, big_endian>*,
273 Target_powerpc* target,
274 size_t relnum, const elfcpp::Rela<size, big_endian>&,
275 unsigned int r_type, const Sized_symbol<size>*,
276 const Symbol_value<size>*,
278 typename elfcpp::Elf_types<size>::Elf_Addr,
282 // A class which returns the size required for a relocation type,
283 // used while scanning relocs during a relocatable link.
284 class Relocatable_size_for_reloc
288 get_size_for_reloc(unsigned int, Relobj*);
291 // Get the GOT section, creating it if necessary.
292 Output_data_got<size, big_endian>*
293 got_section(Symbol_table*, Layout*);
298 gold_assert(this->got2_ != NULL);
302 // Get the TOC section.
306 gold_assert(this->toc_ != NULL);
310 // Create a PLT entry for a global symbol.
312 make_plt_entry(Symbol_table*, Layout*, Symbol*);
314 // Create a GOT entry for the TLS module index.
316 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
317 Sized_relobj_file<size, big_endian>* object);
319 // Get the PLT section.
320 const Output_data_plt_powerpc<size, big_endian>*
323 gold_assert(this->plt_ != NULL);
327 // Get the dynamic reloc section, creating it if necessary.
329 rela_dyn_section(Layout*);
331 // Copy a relocation against a global symbol.
333 copy_reloc(Symbol_table* symtab, Layout* layout,
334 Sized_relobj_file<size, big_endian>* object,
335 unsigned int shndx, Output_section* output_section,
336 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
338 this->copy_relocs_.copy_reloc(symtab, layout,
339 symtab->get_sized_symbol<size>(sym),
340 object, shndx, output_section,
341 reloc, this->rela_dyn_section(layout));
344 // Information about this specific target which we pass to the
345 // general Target structure.
346 static Target::Target_info powerpc_info;
348 // The types of GOT entries needed for this platform.
349 // These values are exposed to the ABI in an incremental link.
350 // Do not renumber existing values without changing the version
351 // number of the .gnu_incremental_inputs section.
354 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
355 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
356 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
360 Output_data_got<size, big_endian>* got_;
362 Output_data_space* got2_;
364 Output_data_space* toc_;
366 Output_data_plt_powerpc<size, big_endian>* plt_;
367 // The dynamic reloc section.
368 Reloc_section* rela_dyn_;
369 // Relocs saved to avoid a COPY reloc.
370 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
371 // Space for variables copied with a COPY reloc.
372 Output_data_space* dynbss_;
373 // Offset of the GOT entry for the TLS module index;
374 unsigned int got_mod_index_offset_;
378 Target::Target_info Target_powerpc<32, true>::powerpc_info =
381 true, // is_big_endian
382 elfcpp::EM_PPC, // machine_code
383 false, // has_make_symbol
384 false, // has_resolve
385 false, // has_code_fill
386 true, // is_default_stack_executable
387 false, // can_icf_inline_merge_sections
389 "/usr/lib/ld.so.1", // dynamic_linker
390 0x10000000, // default_text_segment_address
391 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
392 4 * 1024, // common_pagesize (overridable by -z common-page-size)
393 false, // isolate_execinstr
395 elfcpp::SHN_UNDEF, // small_common_shndx
396 elfcpp::SHN_UNDEF, // large_common_shndx
397 0, // small_common_section_flags
398 0, // large_common_section_flags
399 NULL, // attributes_section
400 NULL // attributes_vendor
404 Target::Target_info Target_powerpc<32, false>::powerpc_info =
407 false, // is_big_endian
408 elfcpp::EM_PPC, // machine_code
409 false, // has_make_symbol
410 false, // has_resolve
411 false, // has_code_fill
412 true, // is_default_stack_executable
413 false, // can_icf_inline_merge_sections
415 "/usr/lib/ld.so.1", // dynamic_linker
416 0x10000000, // default_text_segment_address
417 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
418 4 * 1024, // common_pagesize (overridable by -z common-page-size)
419 false, // isolate_execinstr
421 elfcpp::SHN_UNDEF, // small_common_shndx
422 elfcpp::SHN_UNDEF, // large_common_shndx
423 0, // small_common_section_flags
424 0, // large_common_section_flags
425 NULL, // attributes_section
426 NULL // attributes_vendor
430 Target::Target_info Target_powerpc<64, true>::powerpc_info =
433 true, // is_big_endian
434 elfcpp::EM_PPC64, // machine_code
435 false, // has_make_symbol
436 false, // has_resolve
437 false, // has_code_fill
438 true, // is_default_stack_executable
439 false, // can_icf_inline_merge_sections
441 "/usr/lib/ld.so.1", // dynamic_linker
442 0x10000000, // default_text_segment_address
443 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
444 8 * 1024, // common_pagesize (overridable by -z common-page-size)
445 false, // isolate_execinstr
447 elfcpp::SHN_UNDEF, // small_common_shndx
448 elfcpp::SHN_UNDEF, // large_common_shndx
449 0, // small_common_section_flags
450 0, // large_common_section_flags
451 NULL, // attributes_section
452 NULL // attributes_vendor
456 Target::Target_info Target_powerpc<64, false>::powerpc_info =
459 false, // is_big_endian
460 elfcpp::EM_PPC64, // machine_code
461 false, // has_make_symbol
462 false, // has_resolve
463 false, // has_code_fill
464 true, // is_default_stack_executable
465 false, // can_icf_inline_merge_sections
467 "/usr/lib/ld.so.1", // dynamic_linker
468 0x10000000, // default_text_segment_address
469 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
470 8 * 1024, // common_pagesize (overridable by -z common-page-size)
471 false, // isolate_execinstr
473 elfcpp::SHN_UNDEF, // small_common_shndx
474 elfcpp::SHN_UNDEF, // large_common_shndx
475 0, // small_common_section_flags
476 0, // large_common_section_flags
477 NULL, // attributes_section
478 NULL // attributes_vendor
481 template<int size, bool big_endian>
482 class Powerpc_relocate_functions
485 // Do a simple relocation with the addend in the relocation.
486 template<int valsize>
488 rela(unsigned char* view,
489 unsigned int right_shift,
490 elfcpp::Elf_Xword dst_mask,
491 typename elfcpp::Swap<size, big_endian>::Valtype value,
492 typename elfcpp::Swap<size, big_endian>::Valtype addend)
494 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
495 Valtype* wv = reinterpret_cast<Valtype*>(view);
496 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
497 Valtype reloc = ((value + addend) >> right_shift);
502 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
505 // Do a simple relocation using a symbol value with the addend in
507 template<int valsize>
509 rela(unsigned char* view,
510 unsigned int right_shift,
511 elfcpp::Elf_Xword dst_mask,
512 const Sized_relobj_file<size, big_endian>* object,
513 const Symbol_value<size>* psymval,
514 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
516 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
517 Valtype* wv = reinterpret_cast<Valtype*>(view);
518 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
519 Valtype reloc = (psymval->value(object, addend) >> right_shift);
524 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
527 // Do a simple relocation using a symbol value with the addend in
528 // the relocation, unaligned.
529 template<int valsize>
531 rela_ua(unsigned char* view, unsigned int right_shift,
532 elfcpp::Elf_Xword dst_mask,
533 const Sized_relobj_file<size, big_endian>* object,
534 const Symbol_value<size>* psymval,
535 typename elfcpp::Swap<size, big_endian>::Valtype addend)
537 typedef typename elfcpp::Swap_unaligned<valsize,
538 big_endian>::Valtype Valtype;
539 unsigned char* wv = view;
540 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
541 Valtype reloc = (psymval->value(object, addend) >> right_shift);
546 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
549 // Do a simple PC relative relocation with a Symbol_value with the
550 // addend in the relocation.
551 template<int valsize>
553 pcrela(unsigned char* view, unsigned int right_shift,
554 elfcpp::Elf_Xword dst_mask,
555 const Sized_relobj_file<size, big_endian>* object,
556 const Symbol_value<size>* psymval,
557 typename elfcpp::Swap<size, big_endian>::Valtype addend,
558 typename elfcpp::Elf_types<size>::Elf_Addr address)
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 = ((psymval->value(object, addend) - address)
569 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
572 template<int valsize>
574 pcrela_unaligned(unsigned char* view,
575 const Sized_relobj_file<size, big_endian>* object,
576 const Symbol_value<size>* psymval,
577 typename elfcpp::Swap<size, big_endian>::Valtype addend,
578 typename elfcpp::Elf_types<size>::Elf_Addr address)
580 typedef typename elfcpp::Swap_unaligned<valsize,
581 big_endian>::Valtype Valtype;
582 unsigned char* wv = view;
583 Valtype reloc = (psymval->value(object, addend) - address);
585 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
588 typedef Powerpc_relocate_functions<size, big_endian> This;
589 typedef Relocate_functions<size, big_endian> This_reloc;
591 // R_POWERPC_REL32: (Symbol + Addend - Address)
593 rel32(unsigned char* view,
594 const Sized_relobj_file<size, big_endian>* object,
595 const Symbol_value<size>* psymval,
596 typename elfcpp::Elf_types<size>::Elf_Addr addend,
597 typename elfcpp::Elf_types<size>::Elf_Addr address)
598 { This_reloc::pcrela32(view, object, psymval, addend, address); }
600 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
602 rel24(unsigned char* view,
603 const Sized_relobj_file<size, big_endian>* object,
604 const Symbol_value<size>* psymval,
605 typename elfcpp::Elf_types<size>::Elf_Addr addend,
606 typename elfcpp::Elf_types<size>::Elf_Addr address)
608 This::template pcrela<32>(view, 0, 0x03fffffc, object,
609 psymval, addend, address);
612 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
614 rel14(unsigned char* view,
615 const Sized_relobj_file<size, big_endian>* object,
616 const Symbol_value<size>* psymval,
617 typename elfcpp::Elf_types<size>::Elf_Addr addend,
618 typename elfcpp::Elf_types<size>::Elf_Addr address)
620 This::template pcrela<32>(view, 0, 0x0000fffc, object,
621 psymval, addend, address);
624 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
626 addr16(unsigned char* view,
627 typename elfcpp::Elf_types<size>::Elf_Addr value,
628 typename elfcpp::Elf_types<size>::Elf_Addr addend)
629 { This_reloc::rela16(view, value, addend); }
632 addr16(unsigned char* view,
633 const Sized_relobj_file<size, big_endian>* object,
634 const Symbol_value<size>* psymval,
635 typename elfcpp::Elf_types<size>::Elf_Addr addend)
636 { This_reloc::rela16(view, object, psymval, addend); }
638 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
640 addr16_ds(unsigned char* view,
641 typename elfcpp::Elf_types<size>::Elf_Addr value,
642 typename elfcpp::Elf_types<size>::Elf_Addr addend)
644 This::template rela<16>(view, 0, 0xfffc, value, addend);
647 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
649 addr16_lo(unsigned char* view,
650 typename elfcpp::Elf_types<size>::Elf_Addr value,
651 typename elfcpp::Elf_types<size>::Elf_Addr addend)
652 { This_reloc::rela16(view, value, addend); }
655 addr16_lo(unsigned char* view,
656 const Sized_relobj_file<size, big_endian>* object,
657 const Symbol_value<size>* psymval,
658 typename elfcpp::Elf_types<size>::Elf_Addr addend)
659 { This_reloc::rela16(view, object, psymval, addend); }
661 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
663 addr16_hi(unsigned char* view,
664 typename elfcpp::Elf_types<size>::Elf_Addr value,
665 typename elfcpp::Elf_types<size>::Elf_Addr addend)
667 This::template rela<16>(view, 16, 0xffff, value, addend);
671 addr16_hi(unsigned char* view,
672 const Sized_relobj_file<size, big_endian>* object,
673 const Symbol_value<size>* psymval,
674 typename elfcpp::Elf_types<size>::Elf_Addr addend)
676 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
679 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
680 // final value of the low 16 bits of the
681 // relocation is negative, add one.
683 addr16_ha(unsigned char* view,
684 typename elfcpp::Elf_types<size>::Elf_Addr value,
685 typename elfcpp::Elf_types<size>::Elf_Addr addend)
687 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
689 reloc = value + addend;
695 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
699 addr16_ha(unsigned char* view,
700 const Sized_relobj_file<size, big_endian>* object,
701 const Symbol_value<size>* psymval,
702 typename elfcpp::Elf_types<size>::Elf_Addr addend)
704 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
706 reloc = psymval->value(object, addend);
712 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
715 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
717 rel16(unsigned char* view,
718 const Sized_relobj_file<size, big_endian>* object,
719 const Symbol_value<size>* psymval,
720 typename elfcpp::Elf_types<size>::Elf_Addr addend,
721 typename elfcpp::Elf_types<size>::Elf_Addr address)
722 { This_reloc::pcrela16(view, object, psymval, addend, address); }
724 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
726 rel16_lo(unsigned char* view,
727 const Sized_relobj_file<size, big_endian>* object,
728 const Symbol_value<size>* psymval,
729 typename elfcpp::Elf_types<size>::Elf_Addr addend,
730 typename elfcpp::Elf_types<size>::Elf_Addr address)
731 { This_reloc::pcrela16(view, object, psymval, addend, address); }
733 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
735 rel16_hi(unsigned char* view,
736 const Sized_relobj_file<size, big_endian>* object,
737 const Symbol_value<size>* psymval,
738 typename elfcpp::Elf_types<size>::Elf_Addr addend,
739 typename elfcpp::Elf_types<size>::Elf_Addr address)
741 This::template pcrela<16>(view, 16, 0xffff, object,
742 psymval, addend, address);
745 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
746 // final value of the low 16 bits of the
747 // relocation is negative, add one.
749 rel16_ha(unsigned char* view,
750 const Sized_relobj_file<size, big_endian>* object,
751 const Symbol_value<size>* psymval,
752 typename elfcpp::Elf_types<size>::Elf_Addr addend,
753 typename elfcpp::Elf_types<size>::Elf_Addr address)
755 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
757 reloc = (psymval->value(object, addend) - address);
762 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
766 // Get the GOT section, creating it if necessary.
768 template<int size, bool big_endian>
769 Output_data_got<size, big_endian>*
770 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
773 if (this->got_ == NULL)
775 gold_assert(symtab != NULL && layout != NULL);
777 this->got_ = new Output_data_got<size, big_endian>();
779 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
780 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
781 this->got_, ORDER_DATA, false);
783 // Create the GOT2 or TOC in the .got section.
786 this->got2_ = new Output_data_space(4, "** GOT2");
787 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
790 this->got2_, ORDER_DATA, false);
794 this->toc_ = new Output_data_space(8, "** TOC");
795 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
798 this->toc_, ORDER_DATA, false);
801 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
802 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
803 Symbol_table::PREDEFINED,
805 0, 0, elfcpp::STT_OBJECT,
807 elfcpp::STV_HIDDEN, 0,
814 // Get the dynamic reloc section, creating it if necessary.
816 template<int size, bool big_endian>
817 typename Target_powerpc<size, big_endian>::Reloc_section*
818 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
820 if (this->rela_dyn_ == NULL)
822 gold_assert(layout != NULL);
823 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
824 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
825 elfcpp::SHF_ALLOC, this->rela_dyn_,
826 ORDER_DYNAMIC_RELOCS, false);
828 return this->rela_dyn_;
831 // A class to handle the PLT data.
833 template<int size, bool big_endian>
834 class Output_data_plt_powerpc : public Output_section_data
837 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
838 size, big_endian> Reloc_section;
840 Output_data_plt_powerpc(Layout*);
842 // Add an entry to the PLT.
843 void add_entry(Symbol* gsym);
845 // Return the .rela.plt section data.
846 const Reloc_section* rel_plt() const
851 // Return the number of PLT entries.
854 { return this->count_; }
856 // Return the offset of the first non-reserved PLT entry.
858 first_plt_entry_offset()
859 { return 4 * base_plt_entry_size; }
861 // Return the size of a PLT entry.
864 { return base_plt_entry_size; }
867 void do_adjust_output_section(Output_section* os);
870 // The size of an entry in the PLT.
871 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
873 // Set the final size.
875 set_final_data_size()
877 unsigned int full_count = this->count_ + 4;
879 this->set_data_size(full_count * base_plt_entry_size);
882 // Write out the PLT data.
884 do_write(Output_file*);
886 // The reloc section.
888 // The number of PLT entries.
892 // Create the PLT section. The ordinary .got section is an argument,
893 // since we need to refer to the start.
895 template<int size, bool big_endian>
896 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
897 : Output_section_data(size == 32 ? 4 : 8), count_(0)
899 this->rel_ = new Reloc_section(false);
900 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
901 elfcpp::SHF_ALLOC, this->rel_,
902 ORDER_DYNAMIC_PLT_RELOCS, false);
905 template<int size, bool big_endian>
907 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
912 // Add an entry to the PLT.
914 template<int size, bool big_endian>
916 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
918 gold_assert(!gsym->has_plt_offset());
919 unsigned int index = this->count_+ + 4;
920 section_offset_type plt_offset;
923 plt_offset = index * base_plt_entry_size;
927 gsym->set_plt_offset(plt_offset);
931 gsym->set_needs_dynsym_entry();
932 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
936 static const unsigned int addis_11_11 = 0x3d6b0000;
937 static const unsigned int addis_11_30 = 0x3d7e0000;
938 static const unsigned int addis_12_12 = 0x3d8c0000;
939 static const unsigned int addi_11_11 = 0x396b0000;
940 static const unsigned int add_0_11_11 = 0x7c0b5a14;
941 static const unsigned int add_11_0_11 = 0x7d605a14;
942 static const unsigned int b = 0x48000000;
943 static const unsigned int bcl_20_31 = 0x429f0005;
944 static const unsigned int bctr = 0x4e800420;
945 static const unsigned int lis_11 = 0x3d600000;
946 static const unsigned int lis_12 = 0x3d800000;
947 static const unsigned int lwzu_0_12 = 0x840c0000;
948 static const unsigned int lwz_0_12 = 0x800c0000;
949 static const unsigned int lwz_11_11 = 0x816b0000;
950 static const unsigned int lwz_11_30 = 0x817e0000;
951 static const unsigned int lwz_12_12 = 0x818c0000;
952 static const unsigned int mflr_0 = 0x7c0802a6;
953 static const unsigned int mflr_12 = 0x7d8802a6;
954 static const unsigned int mtctr_0 = 0x7c0903a6;
955 static const unsigned int mtctr_11 = 0x7d6903a6;
956 static const unsigned int mtlr_0 = 0x7c0803a6;
957 static const unsigned int nop = 0x60000000;
958 static const unsigned int sub_11_11_12 = 0x7d6c5850;
960 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
961 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
962 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
963 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
964 /* ld %r11,xxx+16@l(%r12) */
967 // Write out the PLT.
969 template<int size, bool big_endian>
971 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
973 const off_t offset = this->offset();
974 const section_size_type oview_size =
975 convert_to_section_size_type(this->data_size());
976 unsigned char* const oview = of->get_output_view(offset, oview_size);
977 unsigned char* pov = oview;
979 memset(pov, 0, base_plt_entry_size * 4);
980 pov += base_plt_entry_size * 4;
982 unsigned int plt_offset = base_plt_entry_size * 4;
983 const unsigned int count = this->count_;
987 for (unsigned int i = 0; i < count; i++)
993 for (unsigned int i = 0; i < count; i++)
995 elfcpp::Swap<32, true>::writeval(pov + 0x00,
996 lwz_11_30 + plt_offset);
997 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
998 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
999 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
1000 pov += base_plt_entry_size;
1001 plt_offset += base_plt_entry_size;
1005 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1007 of->write_output_view(offset, oview_size, oview);
1010 // Create a PLT entry for a global symbol.
1012 template<int size, bool big_endian>
1014 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1018 if (gsym->has_plt_offset())
1021 if (this->plt_ == NULL)
1023 // Create the GOT section first.
1024 this->got_section(symtab, layout);
1026 // Ensure that .rela.dyn always appears before .rela.plt This is
1027 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1028 // needs to include .rela.plt in it's range.
1029 this->rela_dyn_section(layout);
1031 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
1032 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1034 | elfcpp::SHF_EXECINSTR
1035 | elfcpp::SHF_WRITE),
1036 this->plt_, ORDER_PLT, false);
1038 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1039 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1040 Symbol_table::PREDEFINED,
1042 0, 0, elfcpp::STT_OBJECT,
1044 elfcpp::STV_HIDDEN, 0,
1048 this->plt_->add_entry(gsym);
1051 // Return the number of entries in the PLT.
1053 template<int size, bool big_endian>
1055 Target_powerpc<size, big_endian>::plt_entry_count() const
1057 if (this->plt_ == NULL)
1059 return this->plt_->entry_count();
1062 // Return the offset of the first non-reserved PLT entry.
1064 template<int size, bool big_endian>
1066 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1068 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1071 // Return the size of each PLT entry.
1073 template<int size, bool big_endian>
1075 Target_powerpc<size, big_endian>::plt_entry_size() const
1077 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1080 // Create a GOT entry for the TLS module index.
1082 template<int size, bool big_endian>
1084 Target_powerpc<size, big_endian>::got_mod_index_entry(
1085 Symbol_table* symtab,
1087 Sized_relobj_file<size, big_endian>* object)
1089 if (this->got_mod_index_offset_ == -1U)
1091 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1092 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1093 Output_data_got<size, big_endian>* got;
1094 unsigned int got_offset;
1096 got = this->got_section(symtab, layout);
1097 got_offset = got->add_constant(0);
1098 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1100 got->add_constant(0);
1101 this->got_mod_index_offset_ = got_offset;
1103 return this->got_mod_index_offset_;
1106 // Optimize the TLS relocation type based on what we know about the
1107 // symbol. IS_FINAL is true if the final address of this symbol is
1108 // known at link time.
1110 static tls::Tls_optimization
1111 optimize_tls_reloc(bool /* is_final */, int r_type)
1113 // If we are generating a shared library, then we can't do anything
1115 if (parameters->options().shared())
1116 return tls::TLSOPT_NONE;
1125 // Get the Reference_flags for a particular relocation.
1127 template<int size, bool big_endian>
1129 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
1130 unsigned int r_type)
1134 case elfcpp::R_POWERPC_NONE:
1135 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1136 case elfcpp::R_POWERPC_GNU_VTENTRY:
1137 case elfcpp::R_PPC64_TOC:
1138 // No symbol reference.
1141 case elfcpp::R_POWERPC_ADDR16:
1142 case elfcpp::R_POWERPC_ADDR16_LO:
1143 case elfcpp::R_POWERPC_ADDR16_HI:
1144 case elfcpp::R_POWERPC_ADDR16_HA:
1145 case elfcpp::R_POWERPC_ADDR32:
1146 case elfcpp::R_PPC64_ADDR64:
1147 return Symbol::ABSOLUTE_REF;
1149 case elfcpp::R_POWERPC_REL24:
1150 case elfcpp::R_PPC_LOCAL24PC:
1151 case elfcpp::R_PPC_REL16:
1152 case elfcpp::R_PPC_REL16_LO:
1153 case elfcpp::R_PPC_REL16_HI:
1154 case elfcpp::R_PPC_REL16_HA:
1155 return Symbol::RELATIVE_REF;
1157 case elfcpp::R_PPC_PLTREL24:
1158 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1160 case elfcpp::R_POWERPC_GOT16:
1161 case elfcpp::R_POWERPC_GOT16_LO:
1162 case elfcpp::R_POWERPC_GOT16_HI:
1163 case elfcpp::R_POWERPC_GOT16_HA:
1164 case elfcpp::R_PPC64_TOC16:
1165 case elfcpp::R_PPC64_TOC16_LO:
1166 case elfcpp::R_PPC64_TOC16_HI:
1167 case elfcpp::R_PPC64_TOC16_HA:
1168 case elfcpp::R_PPC64_TOC16_DS:
1169 case elfcpp::R_PPC64_TOC16_LO_DS:
1171 return Symbol::ABSOLUTE_REF;
1173 case elfcpp::R_POWERPC_GOT_TPREL16:
1174 case elfcpp::R_POWERPC_TLS:
1175 return Symbol::TLS_REF;
1177 case elfcpp::R_POWERPC_COPY:
1178 case elfcpp::R_POWERPC_GLOB_DAT:
1179 case elfcpp::R_POWERPC_JMP_SLOT:
1180 case elfcpp::R_POWERPC_RELATIVE:
1181 case elfcpp::R_POWERPC_DTPMOD:
1183 // Not expected. We will give an error later.
1188 // Report an unsupported relocation against a local symbol.
1190 template<int size, bool big_endian>
1192 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1193 Sized_relobj_file<size, big_endian>* object,
1194 unsigned int r_type)
1196 gold_error(_("%s: unsupported reloc %u against local symbol"),
1197 object->name().c_str(), r_type);
1200 // We are about to emit a dynamic relocation of type R_TYPE. If the
1201 // dynamic linker does not support it, issue an error.
1203 template<int size, bool big_endian>
1205 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1206 unsigned int r_type)
1208 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1210 // These are the relocation types supported by glibc for both 32-bit
1211 // and 64-bit powerpc.
1214 case elfcpp::R_POWERPC_RELATIVE:
1215 case elfcpp::R_POWERPC_GLOB_DAT:
1216 case elfcpp::R_POWERPC_DTPMOD:
1217 case elfcpp::R_POWERPC_DTPREL:
1218 case elfcpp::R_POWERPC_TPREL:
1219 case elfcpp::R_POWERPC_JMP_SLOT:
1220 case elfcpp::R_POWERPC_COPY:
1221 case elfcpp::R_POWERPC_ADDR32:
1222 case elfcpp::R_POWERPC_ADDR24:
1223 case elfcpp::R_POWERPC_REL24:
1234 // These are the relocation types supported only on 64-bit.
1235 case elfcpp::R_PPC64_ADDR64:
1236 case elfcpp::R_PPC64_TPREL16_LO_DS:
1237 case elfcpp::R_PPC64_TPREL16_DS:
1238 case elfcpp::R_POWERPC_TPREL16:
1239 case elfcpp::R_POWERPC_TPREL16_LO:
1240 case elfcpp::R_POWERPC_TPREL16_HI:
1241 case elfcpp::R_POWERPC_TPREL16_HA:
1242 case elfcpp::R_PPC64_TPREL16_HIGHER:
1243 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1244 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1245 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1246 case elfcpp::R_PPC64_ADDR16_LO_DS:
1247 case elfcpp::R_POWERPC_ADDR16_LO:
1248 case elfcpp::R_POWERPC_ADDR16_HI:
1249 case elfcpp::R_POWERPC_ADDR16_HA:
1250 case elfcpp::R_POWERPC_ADDR30:
1251 case elfcpp::R_PPC64_UADDR64:
1252 case elfcpp::R_POWERPC_UADDR32:
1253 case elfcpp::R_POWERPC_ADDR16:
1254 case elfcpp::R_POWERPC_UADDR16:
1255 case elfcpp::R_PPC64_ADDR16_DS:
1256 case elfcpp::R_PPC64_ADDR16_HIGHER:
1257 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1258 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1259 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1260 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1261 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1262 case elfcpp::R_POWERPC_REL32:
1263 case elfcpp::R_PPC64_REL64:
1274 // These are the relocation types supported only on 32-bit.
1281 // This prevents us from issuing more than one error per reloc
1282 // section. But we can still wind up issuing more than one
1283 // error per object file.
1284 if (this->issued_non_pic_error_)
1286 gold_assert(parameters->options().output_is_position_independent());
1287 object->error(_("requires unsupported dynamic reloc; "
1288 "recompile with -fPIC"));
1289 this->issued_non_pic_error_ = true;
1293 // Scan a relocation for a local symbol.
1295 template<int size, bool big_endian>
1297 Target_powerpc<size, big_endian>::Scan::local(
1298 Symbol_table* symtab,
1300 Target_powerpc<size, big_endian>* target,
1301 Sized_relobj_file<size, big_endian>* object,
1302 unsigned int data_shndx,
1303 Output_section* output_section,
1304 const elfcpp::Rela<size, big_endian>& reloc,
1305 unsigned int r_type,
1306 const elfcpp::Sym<size, big_endian>& lsym)
1310 case elfcpp::R_POWERPC_NONE:
1311 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1312 case elfcpp::R_POWERPC_GNU_VTENTRY:
1315 case elfcpp::R_PPC64_ADDR64:
1316 case elfcpp::R_POWERPC_ADDR32:
1317 case elfcpp::R_POWERPC_ADDR16_HA:
1318 case elfcpp::R_POWERPC_ADDR16_LO:
1319 // If building a shared library (or a position-independent
1320 // executable), we need to create a dynamic relocation for
1322 if (parameters->options().output_is_position_independent())
1324 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1326 check_non_pic(object, r_type);
1327 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1329 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1330 rela_dyn->add_local(object, r_sym, r_type, output_section,
1331 data_shndx, reloc.get_r_offset(),
1332 reloc.get_r_addend());
1336 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1337 gold_assert(lsym.get_st_value() == 0);
1338 rela_dyn->add_local_relative(object, r_sym, r_type,
1339 output_section, data_shndx,
1340 reloc.get_r_offset(),
1341 reloc.get_r_addend(), false);
1346 case elfcpp::R_POWERPC_REL24:
1347 case elfcpp::R_PPC_LOCAL24PC:
1348 case elfcpp::R_POWERPC_REL32:
1349 case elfcpp::R_PPC_REL16_LO:
1350 case elfcpp::R_PPC_REL16_HA:
1353 case elfcpp::R_POWERPC_GOT16:
1354 case elfcpp::R_POWERPC_GOT16_LO:
1355 case elfcpp::R_POWERPC_GOT16_HI:
1356 case elfcpp::R_POWERPC_GOT16_HA:
1357 case elfcpp::R_PPC64_TOC16:
1358 case elfcpp::R_PPC64_TOC16_LO:
1359 case elfcpp::R_PPC64_TOC16_HI:
1360 case elfcpp::R_PPC64_TOC16_HA:
1361 case elfcpp::R_PPC64_TOC16_DS:
1362 case elfcpp::R_PPC64_TOC16_LO_DS:
1364 // The symbol requires a GOT entry.
1365 Output_data_got<size, big_endian>* got;
1368 got = target->got_section(symtab, layout);
1369 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1371 // If we are generating a shared object, we need to add a
1372 // dynamic relocation for this symbol's GOT entry.
1373 if (parameters->options().output_is_position_independent())
1375 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1377 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1380 off = got->add_constant(0);
1381 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1382 rela_dyn->add_local_relative(object, r_sym,
1383 elfcpp::R_POWERPC_RELATIVE,
1384 got, off, 0, false);
1388 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1392 case elfcpp::R_PPC64_TOC:
1393 // We need a GOT section.
1394 target->got_section(symtab, layout);
1397 // These are relocations which should only be seen by the
1398 // dynamic linker, and should never be seen here.
1399 case elfcpp::R_POWERPC_COPY:
1400 case elfcpp::R_POWERPC_GLOB_DAT:
1401 case elfcpp::R_POWERPC_JMP_SLOT:
1402 case elfcpp::R_POWERPC_RELATIVE:
1403 case elfcpp::R_POWERPC_DTPMOD:
1404 gold_error(_("%s: unexpected reloc %u in object file"),
1405 object->name().c_str(), r_type);
1409 unsupported_reloc_local(object, r_type);
1414 // Report an unsupported relocation against a global symbol.
1416 template<int size, bool big_endian>
1418 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1419 Sized_relobj_file<size, big_endian>* object,
1420 unsigned int r_type,
1423 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1424 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1427 // Scan a relocation for a global symbol.
1429 template<int size, bool big_endian>
1431 Target_powerpc<size, big_endian>::Scan::global(
1432 Symbol_table* symtab,
1434 Target_powerpc<size, big_endian>* target,
1435 Sized_relobj_file<size, big_endian>* object,
1436 unsigned int data_shndx,
1437 Output_section* output_section,
1438 const elfcpp::Rela<size, big_endian>& reloc,
1439 unsigned int r_type,
1444 case elfcpp::R_POWERPC_NONE:
1445 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1446 case elfcpp::R_POWERPC_GNU_VTENTRY:
1449 case elfcpp::R_PPC_PLTREL24:
1450 // If the symbol is fully resolved, this is just a PC32 reloc.
1451 // Otherwise we need a PLT entry.
1452 if (gsym->final_value_is_known())
1454 // If building a shared library, we can also skip the PLT entry
1455 // if the symbol is defined in the output file and is protected
1457 if (gsym->is_defined()
1458 && !gsym->is_from_dynobj()
1459 && !gsym->is_preemptible())
1461 target->make_plt_entry(symtab, layout, gsym);
1464 case elfcpp::R_POWERPC_ADDR16:
1465 case elfcpp::R_POWERPC_ADDR16_LO:
1466 case elfcpp::R_POWERPC_ADDR16_HI:
1467 case elfcpp::R_POWERPC_ADDR16_HA:
1468 case elfcpp::R_POWERPC_ADDR32:
1469 case elfcpp::R_PPC64_ADDR64:
1471 // Make a PLT entry if necessary.
1472 if (gsym->needs_plt_entry())
1474 target->make_plt_entry(symtab, layout, gsym);
1475 // Since this is not a PC-relative relocation, we may be
1476 // taking the address of a function. In that case we need to
1477 // set the entry in the dynamic symbol table to the address of
1479 if (gsym->is_from_dynobj() && !parameters->options().shared())
1480 gsym->set_needs_dynsym_value();
1482 // Make a dynamic relocation if necessary.
1483 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1485 if (gsym->may_need_copy_reloc())
1487 target->copy_reloc(symtab, layout, object,
1488 data_shndx, output_section, gsym, reloc);
1490 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1491 || r_type == elfcpp::R_PPC64_ADDR64)
1492 && gsym->can_use_relative_reloc(false))
1494 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1495 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1496 output_section, object,
1497 data_shndx, reloc.get_r_offset(),
1498 reloc.get_r_addend(), false);
1502 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1504 check_non_pic(object, r_type);
1505 if (gsym->is_from_dynobj()
1506 || gsym->is_undefined()
1507 || gsym->is_preemptible())
1508 rela_dyn->add_global(gsym, r_type, output_section,
1510 reloc.get_r_offset(),
1511 reloc.get_r_addend());
1513 rela_dyn->add_global_relative(gsym, r_type,
1514 output_section, object,
1516 reloc.get_r_offset(),
1517 reloc.get_r_addend(), false);
1523 case elfcpp::R_POWERPC_REL24:
1524 case elfcpp::R_PPC_LOCAL24PC:
1525 case elfcpp::R_PPC_REL16:
1526 case elfcpp::R_PPC_REL16_LO:
1527 case elfcpp::R_PPC_REL16_HI:
1528 case elfcpp::R_PPC_REL16_HA:
1530 if (gsym->needs_plt_entry())
1531 target->make_plt_entry(symtab, layout, gsym);
1532 // Make a dynamic relocation if necessary.
1533 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1535 if (gsym->may_need_copy_reloc())
1537 target->copy_reloc(symtab, layout, object,
1538 data_shndx, output_section, gsym,
1543 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1544 check_non_pic(object, r_type);
1545 rela_dyn->add_global(gsym, r_type, output_section, object,
1546 data_shndx, reloc.get_r_offset(),
1547 reloc.get_r_addend());
1553 case elfcpp::R_POWERPC_GOT16:
1554 case elfcpp::R_POWERPC_GOT16_LO:
1555 case elfcpp::R_POWERPC_GOT16_HI:
1556 case elfcpp::R_POWERPC_GOT16_HA:
1557 case elfcpp::R_PPC64_TOC16:
1558 case elfcpp::R_PPC64_TOC16_LO:
1559 case elfcpp::R_PPC64_TOC16_HI:
1560 case elfcpp::R_PPC64_TOC16_HA:
1561 case elfcpp::R_PPC64_TOC16_DS:
1562 case elfcpp::R_PPC64_TOC16_LO_DS:
1564 // The symbol requires a GOT entry.
1565 Output_data_got<size, big_endian>* got;
1567 got = target->got_section(symtab, layout);
1568 if (gsym->final_value_is_known())
1569 got->add_global(gsym, GOT_TYPE_STANDARD);
1572 // If this symbol is not fully resolved, we need to add a
1573 // dynamic relocation for it.
1574 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1575 if (gsym->is_from_dynobj()
1576 || gsym->is_undefined()
1577 || gsym->is_preemptible())
1578 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
1579 elfcpp::R_POWERPC_GLOB_DAT);
1580 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1582 unsigned int off = got->add_constant(0);
1584 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1585 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1586 got, off, 0, false);
1592 case elfcpp::R_PPC64_TOC:
1593 // We need a GOT section.
1594 target->got_section(symtab, layout);
1597 case elfcpp::R_POWERPC_GOT_TPREL16:
1598 case elfcpp::R_POWERPC_TLS:
1602 // These are relocations which should only be seen by the
1603 // dynamic linker, and should never be seen here.
1604 case elfcpp::R_POWERPC_COPY:
1605 case elfcpp::R_POWERPC_GLOB_DAT:
1606 case elfcpp::R_POWERPC_JMP_SLOT:
1607 case elfcpp::R_POWERPC_RELATIVE:
1608 case elfcpp::R_POWERPC_DTPMOD:
1609 gold_error(_("%s: unexpected reloc %u in object file"),
1610 object->name().c_str(), r_type);
1614 unsupported_reloc_global(object, r_type, gsym);
1619 // Process relocations for gc.
1621 template<int size, bool big_endian>
1623 Target_powerpc<size, big_endian>::gc_process_relocs(
1624 Symbol_table* symtab,
1626 Sized_relobj_file<size, big_endian>* object,
1627 unsigned int data_shndx,
1629 const unsigned char* prelocs,
1631 Output_section* output_section,
1632 bool needs_special_offset_handling,
1633 size_t local_symbol_count,
1634 const unsigned char* plocal_symbols)
1636 typedef Target_powerpc<size, big_endian> Powerpc;
1637 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1639 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
1640 typename Target_powerpc::Relocatable_size_for_reloc>(
1649 needs_special_offset_handling,
1654 // Scan relocations for a section.
1656 template<int size, bool big_endian>
1658 Target_powerpc<size, big_endian>::scan_relocs(
1659 Symbol_table* symtab,
1661 Sized_relobj_file<size, big_endian>* object,
1662 unsigned int data_shndx,
1663 unsigned int sh_type,
1664 const unsigned char* prelocs,
1666 Output_section* output_section,
1667 bool needs_special_offset_handling,
1668 size_t local_symbol_count,
1669 const unsigned char* plocal_symbols)
1671 typedef Target_powerpc<size, big_endian> Powerpc;
1672 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1673 static Output_data_space* sdata;
1675 if (sh_type == elfcpp::SHT_REL)
1677 gold_error(_("%s: unsupported REL reloc section"),
1678 object->name().c_str());
1682 // Define _SDA_BASE_ at the start of the .sdata section.
1685 // layout->find_output_section(".sdata") == NULL
1686 sdata = new Output_data_space(4, "** sdata");
1687 Output_section* os = layout->add_output_section_data(".sdata", 0,
1689 | elfcpp::SHF_WRITE,
1693 symtab->define_in_output_data("_SDA_BASE_", NULL,
1694 Symbol_table::PREDEFINED,
1699 elfcpp::STV_HIDDEN, 0,
1703 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1712 needs_special_offset_handling,
1717 // Finalize the sections.
1719 template<int size, bool big_endian>
1721 Target_powerpc<size, big_endian>::do_finalize_sections(
1723 const Input_objects*,
1726 // Fill in some more dynamic tags.
1727 const Reloc_section* rel_plt = (this->plt_ == NULL
1729 : this->plt_->rel_plt());
1730 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
1731 this->rela_dyn_, true, size == 32);
1733 // Emit any relocs we saved in an attempt to avoid generating COPY
1735 if (this->copy_relocs_.any_saved_relocs())
1736 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1739 // Perform a relocation.
1741 template<int size, bool big_endian>
1743 Target_powerpc<size, big_endian>::Relocate::relocate(
1744 const Relocate_info<size, big_endian>* relinfo,
1745 Target_powerpc* target,
1748 const elfcpp::Rela<size, big_endian>& rela,
1749 unsigned int r_type,
1750 const Sized_symbol<size>* gsym,
1751 const Symbol_value<size>* psymval,
1752 unsigned char* view,
1753 typename elfcpp::Elf_types<size>::Elf_Addr address,
1754 section_size_type /* view_size */)
1756 const unsigned int toc_base_offset = 0x8000;
1757 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1759 // Pick the value to use for symbols defined in shared objects.
1760 Symbol_value<size> symval;
1762 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
1764 elfcpp::Elf_Xword value;
1766 value = target->plt_section()->address() + gsym->plt_offset();
1768 symval.set_output_value(value);
1773 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1774 elfcpp::Elf_Xword addend = rela.get_r_addend();
1776 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1777 // pointer points to the beginning, not the end, of the table.
1778 // So we just use the plain offset.
1779 unsigned int got_offset = 0;
1780 unsigned int got2_offset = 0;
1783 case elfcpp::R_PPC64_TOC16:
1784 case elfcpp::R_PPC64_TOC16_LO:
1785 case elfcpp::R_PPC64_TOC16_HI:
1786 case elfcpp::R_PPC64_TOC16_HA:
1787 case elfcpp::R_PPC64_TOC16_DS:
1788 case elfcpp::R_PPC64_TOC16_LO_DS:
1789 // Subtract the TOC base address.
1790 addend -= target->toc_section()->address() + toc_base_offset;
1793 case elfcpp::R_POWERPC_GOT16:
1794 case elfcpp::R_POWERPC_GOT16_LO:
1795 case elfcpp::R_POWERPC_GOT16_HI:
1796 case elfcpp::R_POWERPC_GOT16_HA:
1797 case elfcpp::R_PPC64_GOT16_DS:
1798 case elfcpp::R_PPC64_GOT16_LO_DS:
1801 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1802 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1806 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1807 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1808 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1812 // R_PPC_PLTREL24 is rather special. If non-zero,
1813 // the addend specifies the GOT pointer offset within .got2.
1814 case elfcpp::R_PPC_PLTREL24:
1815 if (addend >= 32768)
1817 Output_data_space* got2;
1818 got2 = target->got2_section();
1819 got2_offset = got2->offset();
1820 addend += got2_offset;
1830 case elfcpp::R_POWERPC_NONE:
1831 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1832 case elfcpp::R_POWERPC_GNU_VTENTRY:
1835 case elfcpp::R_POWERPC_REL32:
1836 Reloc::rel32(view, object, psymval, addend, address);
1839 case elfcpp::R_POWERPC_REL24:
1840 Reloc::rel24(view, object, psymval, addend, address);
1843 case elfcpp::R_POWERPC_REL14:
1844 Reloc::rel14(view, object, psymval, addend, address);
1847 case elfcpp::R_PPC_PLTREL24:
1848 Reloc::rel24(view, object, psymval, addend, address);
1851 case elfcpp::R_PPC_LOCAL24PC:
1852 Reloc::rel24(view, object, psymval, addend, address);
1855 case elfcpp::R_PPC64_ADDR64:
1856 if (!parameters->options().output_is_position_independent())
1857 Relocate_functions<size, big_endian>::rela64(view, object,
1861 case elfcpp::R_POWERPC_ADDR32:
1862 if (!parameters->options().output_is_position_independent())
1863 Relocate_functions<size, big_endian>::rela32(view, object,
1867 case elfcpp::R_POWERPC_ADDR16_LO:
1868 Reloc::addr16_lo(view, object, psymval, addend);
1871 case elfcpp::R_POWERPC_ADDR16_HI:
1872 Reloc::addr16_hi(view, object, psymval, addend);
1875 case elfcpp::R_POWERPC_ADDR16_HA:
1876 Reloc::addr16_ha(view, object, psymval, addend);
1879 case elfcpp::R_PPC_REL16_LO:
1880 Reloc::rel16_lo(view, object, psymval, addend, address);
1883 case elfcpp::R_PPC_REL16_HI:
1884 Reloc::rel16_lo(view, object, psymval, addend, address);
1887 case elfcpp::R_PPC_REL16_HA:
1888 Reloc::rel16_ha(view, object, psymval, addend, address);
1891 case elfcpp::R_POWERPC_GOT16:
1892 Reloc::addr16(view, got_offset, addend);
1895 case elfcpp::R_POWERPC_GOT16_LO:
1896 Reloc::addr16_lo(view, got_offset, addend);
1899 case elfcpp::R_POWERPC_GOT16_HI:
1900 Reloc::addr16_hi(view, got_offset, addend);
1903 case elfcpp::R_POWERPC_GOT16_HA:
1904 Reloc::addr16_ha(view, got_offset, addend);
1907 case elfcpp::R_PPC64_TOC16:
1908 Reloc::addr16(view, got_offset, addend);
1911 case elfcpp::R_PPC64_TOC16_LO:
1912 Reloc::addr16_lo(view, got_offset, addend);
1915 case elfcpp::R_PPC64_TOC16_HI:
1916 Reloc::addr16_hi(view, got_offset, addend);
1919 case elfcpp::R_PPC64_TOC16_HA:
1920 Reloc::addr16_ha(view, got_offset, addend);
1923 case elfcpp::R_PPC64_TOC16_DS:
1924 case elfcpp::R_PPC64_TOC16_LO_DS:
1925 Reloc::addr16_ds(view, got_offset, addend);
1928 case elfcpp::R_PPC64_TOC:
1930 elfcpp::Elf_types<64>::Elf_Addr value;
1931 value = target->toc_section()->address() + toc_base_offset;
1932 Relocate_functions<64, false>::rela64(view, value, addend);
1936 case elfcpp::R_POWERPC_COPY:
1937 case elfcpp::R_POWERPC_GLOB_DAT:
1938 case elfcpp::R_POWERPC_JMP_SLOT:
1939 case elfcpp::R_POWERPC_RELATIVE:
1940 // This is an outstanding tls reloc, which is unexpected when
1942 case elfcpp::R_POWERPC_DTPMOD:
1943 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1944 _("unexpected reloc %u in object file"),
1949 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1950 _("unsupported reloc %u"),
1958 // Perform a TLS relocation.
1960 template<int size, bool big_endian>
1962 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1963 const Relocate_info<size, big_endian>* relinfo,
1964 Target_powerpc<size, big_endian>* target,
1966 const elfcpp::Rela<size, big_endian>& rela,
1967 unsigned int r_type,
1968 const Sized_symbol<size>* gsym,
1969 const Symbol_value<size>* psymval,
1970 unsigned char* view,
1971 typename elfcpp::Elf_types<size>::Elf_Addr address,
1974 Output_segment* tls_segment = relinfo->layout->tls_segment();
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 typename elfcpp::Elf_types<size>::Elf_Off 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.