1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010 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
387 "/usr/lib/ld.so.1", // dynamic_linker
388 0x10000000, // default_text_segment_address
389 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
390 4 * 1024, // common_pagesize (overridable by -z common-page-size)
391 elfcpp::SHN_UNDEF, // small_common_shndx
392 elfcpp::SHN_UNDEF, // large_common_shndx
393 0, // small_common_section_flags
394 0, // large_common_section_flags
395 NULL, // attributes_section
396 NULL // attributes_vendor
400 Target::Target_info Target_powerpc<32, false>::powerpc_info =
403 false, // is_big_endian
404 elfcpp::EM_PPC, // machine_code
405 false, // has_make_symbol
406 false, // has_resolve
407 false, // has_code_fill
408 true, // is_default_stack_executable
410 "/usr/lib/ld.so.1", // dynamic_linker
411 0x10000000, // default_text_segment_address
412 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
413 4 * 1024, // common_pagesize (overridable by -z common-page-size)
414 elfcpp::SHN_UNDEF, // small_common_shndx
415 elfcpp::SHN_UNDEF, // large_common_shndx
416 0, // small_common_section_flags
417 0, // large_common_section_flags
418 NULL, // attributes_section
419 NULL // attributes_vendor
423 Target::Target_info Target_powerpc<64, true>::powerpc_info =
426 true, // is_big_endian
427 elfcpp::EM_PPC64, // machine_code
428 false, // has_make_symbol
429 false, // has_resolve
430 false, // has_code_fill
431 true, // is_default_stack_executable
433 "/usr/lib/ld.so.1", // dynamic_linker
434 0x10000000, // default_text_segment_address
435 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
436 8 * 1024, // common_pagesize (overridable by -z common-page-size)
437 elfcpp::SHN_UNDEF, // small_common_shndx
438 elfcpp::SHN_UNDEF, // large_common_shndx
439 0, // small_common_section_flags
440 0, // large_common_section_flags
441 NULL, // attributes_section
442 NULL // attributes_vendor
446 Target::Target_info Target_powerpc<64, false>::powerpc_info =
449 false, // is_big_endian
450 elfcpp::EM_PPC64, // machine_code
451 false, // has_make_symbol
452 false, // has_resolve
453 false, // has_code_fill
454 true, // is_default_stack_executable
456 "/usr/lib/ld.so.1", // dynamic_linker
457 0x10000000, // default_text_segment_address
458 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
459 8 * 1024, // common_pagesize (overridable by -z common-page-size)
460 elfcpp::SHN_UNDEF, // small_common_shndx
461 elfcpp::SHN_UNDEF, // large_common_shndx
462 0, // small_common_section_flags
463 0, // large_common_section_flags
464 NULL, // attributes_section
465 NULL // attributes_vendor
468 template<int size, bool big_endian>
469 class Powerpc_relocate_functions
472 // Do a simple relocation with the addend in the relocation.
473 template<int valsize>
475 rela(unsigned char* view,
476 unsigned int right_shift,
477 elfcpp::Elf_Xword dst_mask,
478 typename elfcpp::Swap<size, big_endian>::Valtype value,
479 typename elfcpp::Swap<size, big_endian>::Valtype addend)
481 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
482 Valtype* wv = reinterpret_cast<Valtype*>(view);
483 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
484 Valtype reloc = ((value + addend) >> right_shift);
489 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
492 // Do a simple relocation using a symbol value with the addend in
494 template<int valsize>
496 rela(unsigned char* view,
497 unsigned int right_shift,
498 elfcpp::Elf_Xword dst_mask,
499 const Sized_relobj_file<size, big_endian>* object,
500 const Symbol_value<size>* psymval,
501 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
503 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
504 Valtype* wv = reinterpret_cast<Valtype*>(view);
505 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
506 Valtype reloc = (psymval->value(object, addend) >> right_shift);
511 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
514 // Do a simple relocation using a symbol value with the addend in
515 // the relocation, unaligned.
516 template<int valsize>
518 rela_ua(unsigned char* view, unsigned int right_shift,
519 elfcpp::Elf_Xword dst_mask,
520 const Sized_relobj_file<size, big_endian>* object,
521 const Symbol_value<size>* psymval,
522 typename elfcpp::Swap<size, big_endian>::Valtype addend)
524 typedef typename elfcpp::Swap_unaligned<valsize,
525 big_endian>::Valtype Valtype;
526 unsigned char* wv = view;
527 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
528 Valtype reloc = (psymval->value(object, addend) >> right_shift);
533 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
536 // Do a simple PC relative relocation with a Symbol_value with the
537 // addend in the relocation.
538 template<int valsize>
540 pcrela(unsigned char* view, unsigned int right_shift,
541 elfcpp::Elf_Xword dst_mask,
542 const Sized_relobj_file<size, big_endian>* object,
543 const Symbol_value<size>* psymval,
544 typename elfcpp::Swap<size, big_endian>::Valtype addend,
545 typename elfcpp::Elf_types<size>::Elf_Addr address)
547 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
548 Valtype* wv = reinterpret_cast<Valtype*>(view);
549 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
550 Valtype reloc = ((psymval->value(object, addend) - address)
556 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
559 template<int valsize>
561 pcrela_unaligned(unsigned char* view,
562 const Sized_relobj_file<size, big_endian>* object,
563 const Symbol_value<size>* psymval,
564 typename elfcpp::Swap<size, big_endian>::Valtype addend,
565 typename elfcpp::Elf_types<size>::Elf_Addr address)
567 typedef typename elfcpp::Swap_unaligned<valsize,
568 big_endian>::Valtype Valtype;
569 unsigned char* wv = view;
570 Valtype reloc = (psymval->value(object, addend) - address);
572 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
575 typedef Powerpc_relocate_functions<size, big_endian> This;
576 typedef Relocate_functions<size, big_endian> This_reloc;
578 // R_POWERPC_REL32: (Symbol + Addend - Address)
580 rel32(unsigned char* view,
581 const Sized_relobj_file<size, big_endian>* object,
582 const Symbol_value<size>* psymval,
583 typename elfcpp::Elf_types<size>::Elf_Addr addend,
584 typename elfcpp::Elf_types<size>::Elf_Addr address)
585 { This_reloc::pcrela32(view, object, psymval, addend, address); }
587 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
589 rel24(unsigned char* view,
590 const Sized_relobj_file<size, big_endian>* object,
591 const Symbol_value<size>* psymval,
592 typename elfcpp::Elf_types<size>::Elf_Addr addend,
593 typename elfcpp::Elf_types<size>::Elf_Addr address)
595 This::template pcrela<32>(view, 0, 0x03fffffc, object,
596 psymval, addend, address);
599 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
601 rel14(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, 0x0000fffc, object,
608 psymval, addend, address);
611 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
613 addr16(unsigned char* view,
614 typename elfcpp::Elf_types<size>::Elf_Addr value,
615 typename elfcpp::Elf_types<size>::Elf_Addr addend)
616 { This_reloc::rela16(view, value, addend); }
619 addr16(unsigned char* view,
620 const Sized_relobj_file<size, big_endian>* object,
621 const Symbol_value<size>* psymval,
622 typename elfcpp::Elf_types<size>::Elf_Addr addend)
623 { This_reloc::rela16(view, object, psymval, addend); }
625 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
627 addr16_ds(unsigned char* view,
628 typename elfcpp::Elf_types<size>::Elf_Addr value,
629 typename elfcpp::Elf_types<size>::Elf_Addr addend)
631 This::template rela<16>(view, 0, 0xfffc, value, addend);
634 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
636 addr16_lo(unsigned char* view,
637 typename elfcpp::Elf_types<size>::Elf_Addr value,
638 typename elfcpp::Elf_types<size>::Elf_Addr addend)
639 { This_reloc::rela16(view, value, addend); }
642 addr16_lo(unsigned char* view,
643 const Sized_relobj_file<size, big_endian>* object,
644 const Symbol_value<size>* psymval,
645 typename elfcpp::Elf_types<size>::Elf_Addr addend)
646 { This_reloc::rela16(view, object, psymval, addend); }
648 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
650 addr16_hi(unsigned char* view,
651 typename elfcpp::Elf_types<size>::Elf_Addr value,
652 typename elfcpp::Elf_types<size>::Elf_Addr addend)
654 This::template rela<16>(view, 16, 0xffff, value, addend);
658 addr16_hi(unsigned char* view,
659 const Sized_relobj_file<size, big_endian>* object,
660 const Symbol_value<size>* psymval,
661 typename elfcpp::Elf_types<size>::Elf_Addr addend)
663 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
666 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
667 // final value of the low 16 bits of the
668 // relocation is negative, add one.
670 addr16_ha(unsigned char* view,
671 typename elfcpp::Elf_types<size>::Elf_Addr value,
672 typename elfcpp::Elf_types<size>::Elf_Addr addend)
674 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
676 reloc = value + addend;
682 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
686 addr16_ha(unsigned char* view,
687 const Sized_relobj_file<size, big_endian>* object,
688 const Symbol_value<size>* psymval,
689 typename elfcpp::Elf_types<size>::Elf_Addr addend)
691 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
693 reloc = psymval->value(object, addend);
699 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
702 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
704 rel16(unsigned char* view,
705 const Sized_relobj_file<size, big_endian>* object,
706 const Symbol_value<size>* psymval,
707 typename elfcpp::Elf_types<size>::Elf_Addr addend,
708 typename elfcpp::Elf_types<size>::Elf_Addr address)
709 { This_reloc::pcrela16(view, object, psymval, addend, address); }
711 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
713 rel16_lo(unsigned char* view,
714 const Sized_relobj_file<size, big_endian>* object,
715 const Symbol_value<size>* psymval,
716 typename elfcpp::Elf_types<size>::Elf_Addr addend,
717 typename elfcpp::Elf_types<size>::Elf_Addr address)
718 { This_reloc::pcrela16(view, object, psymval, addend, address); }
720 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
722 rel16_hi(unsigned char* view,
723 const Sized_relobj_file<size, big_endian>* object,
724 const Symbol_value<size>* psymval,
725 typename elfcpp::Elf_types<size>::Elf_Addr addend,
726 typename elfcpp::Elf_types<size>::Elf_Addr address)
728 This::template pcrela<16>(view, 16, 0xffff, object,
729 psymval, addend, address);
732 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
733 // final value of the low 16 bits of the
734 // relocation is negative, add one.
736 rel16_ha(unsigned char* view,
737 const Sized_relobj_file<size, big_endian>* object,
738 const Symbol_value<size>* psymval,
739 typename elfcpp::Elf_types<size>::Elf_Addr addend,
740 typename elfcpp::Elf_types<size>::Elf_Addr address)
742 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
744 reloc = (psymval->value(object, addend) - address);
749 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
753 // Get the GOT section, creating it if necessary.
755 template<int size, bool big_endian>
756 Output_data_got<size, big_endian>*
757 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
760 if (this->got_ == NULL)
762 gold_assert(symtab != NULL && layout != NULL);
764 this->got_ = new Output_data_got<size, big_endian>();
766 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
767 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
768 this->got_, ORDER_DATA, false);
770 // Create the GOT2 or TOC in the .got section.
773 this->got2_ = new Output_data_space(4, "** GOT2");
774 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
777 this->got2_, ORDER_DATA, false);
781 this->toc_ = new Output_data_space(8, "** TOC");
782 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
785 this->toc_, ORDER_DATA, false);
788 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
789 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
790 Symbol_table::PREDEFINED,
792 0, 0, elfcpp::STT_OBJECT,
794 elfcpp::STV_HIDDEN, 0,
801 // Get the dynamic reloc section, creating it if necessary.
803 template<int size, bool big_endian>
804 typename Target_powerpc<size, big_endian>::Reloc_section*
805 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
807 if (this->rela_dyn_ == NULL)
809 gold_assert(layout != NULL);
810 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
811 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
812 elfcpp::SHF_ALLOC, this->rela_dyn_,
813 ORDER_DYNAMIC_RELOCS, false);
815 return this->rela_dyn_;
818 // A class to handle the PLT data.
820 template<int size, bool big_endian>
821 class Output_data_plt_powerpc : public Output_section_data
824 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
825 size, big_endian> Reloc_section;
827 Output_data_plt_powerpc(Layout*);
829 // Add an entry to the PLT.
830 void add_entry(Symbol* gsym);
832 // Return the .rela.plt section data.
833 const Reloc_section* rel_plt() const
838 // Return the number of PLT entries.
841 { return this->count_; }
843 // Return the offset of the first non-reserved PLT entry.
845 first_plt_entry_offset()
846 { return 4 * base_plt_entry_size; }
848 // Return the size of a PLT entry.
851 { return base_plt_entry_size; }
854 void do_adjust_output_section(Output_section* os);
857 // The size of an entry in the PLT.
858 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
860 // Set the final size.
862 set_final_data_size()
864 unsigned int full_count = this->count_ + 4;
866 this->set_data_size(full_count * base_plt_entry_size);
869 // Write out the PLT data.
871 do_write(Output_file*);
873 // The reloc section.
875 // The number of PLT entries.
879 // Create the PLT section. The ordinary .got section is an argument,
880 // since we need to refer to the start.
882 template<int size, bool big_endian>
883 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
884 : Output_section_data(size == 32 ? 4 : 8), count_(0)
886 this->rel_ = new Reloc_section(false);
887 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
888 elfcpp::SHF_ALLOC, this->rel_,
889 ORDER_DYNAMIC_PLT_RELOCS, false);
892 template<int size, bool big_endian>
894 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
899 // Add an entry to the PLT.
901 template<int size, bool big_endian>
903 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
905 gold_assert(!gsym->has_plt_offset());
906 unsigned int index = this->count_+ + 4;
907 section_offset_type plt_offset;
910 plt_offset = index * base_plt_entry_size;
914 gsym->set_plt_offset(plt_offset);
918 gsym->set_needs_dynsym_entry();
919 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
923 static const unsigned int addis_11_11 = 0x3d6b0000;
924 static const unsigned int addis_11_30 = 0x3d7e0000;
925 static const unsigned int addis_12_12 = 0x3d8c0000;
926 static const unsigned int addi_11_11 = 0x396b0000;
927 static const unsigned int add_0_11_11 = 0x7c0b5a14;
928 static const unsigned int add_11_0_11 = 0x7d605a14;
929 static const unsigned int b = 0x48000000;
930 static const unsigned int bcl_20_31 = 0x429f0005;
931 static const unsigned int bctr = 0x4e800420;
932 static const unsigned int lis_11 = 0x3d600000;
933 static const unsigned int lis_12 = 0x3d800000;
934 static const unsigned int lwzu_0_12 = 0x840c0000;
935 static const unsigned int lwz_0_12 = 0x800c0000;
936 static const unsigned int lwz_11_11 = 0x816b0000;
937 static const unsigned int lwz_11_30 = 0x817e0000;
938 static const unsigned int lwz_12_12 = 0x818c0000;
939 static const unsigned int mflr_0 = 0x7c0802a6;
940 static const unsigned int mflr_12 = 0x7d8802a6;
941 static const unsigned int mtctr_0 = 0x7c0903a6;
942 static const unsigned int mtctr_11 = 0x7d6903a6;
943 static const unsigned int mtlr_0 = 0x7c0803a6;
944 static const unsigned int nop = 0x60000000;
945 static const unsigned int sub_11_11_12 = 0x7d6c5850;
947 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
948 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
949 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
950 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
951 /* ld %r11,xxx+16@l(%r12) */
954 // Write out the PLT.
956 template<int size, bool big_endian>
958 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
960 const off_t offset = this->offset();
961 const section_size_type oview_size =
962 convert_to_section_size_type(this->data_size());
963 unsigned char* const oview = of->get_output_view(offset, oview_size);
964 unsigned char* pov = oview;
966 memset(pov, 0, base_plt_entry_size * 4);
967 pov += base_plt_entry_size * 4;
969 unsigned int plt_offset = base_plt_entry_size * 4;
970 const unsigned int count = this->count_;
974 for (unsigned int i = 0; i < count; i++)
980 for (unsigned int i = 0; i < count; i++)
982 elfcpp::Swap<32, true>::writeval(pov + 0x00,
983 lwz_11_30 + plt_offset);
984 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
985 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
986 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
987 pov += base_plt_entry_size;
988 plt_offset += base_plt_entry_size;
992 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
994 of->write_output_view(offset, oview_size, oview);
997 // Create a PLT entry for a global symbol.
999 template<int size, bool big_endian>
1001 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1005 if (gsym->has_plt_offset())
1008 if (this->plt_ == NULL)
1010 // Create the GOT section first.
1011 this->got_section(symtab, layout);
1013 // Ensure that .rela.dyn always appears before .rela.plt This is
1014 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1015 // needs to include .rela.plt in it's range.
1016 this->rela_dyn_section(layout);
1018 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
1019 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1021 | elfcpp::SHF_EXECINSTR
1022 | elfcpp::SHF_WRITE),
1023 this->plt_, ORDER_PLT, false);
1025 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1026 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1027 Symbol_table::PREDEFINED,
1029 0, 0, elfcpp::STT_OBJECT,
1031 elfcpp::STV_HIDDEN, 0,
1035 this->plt_->add_entry(gsym);
1038 // Return the number of entries in the PLT.
1040 template<int size, bool big_endian>
1042 Target_powerpc<size, big_endian>::plt_entry_count() const
1044 if (this->plt_ == NULL)
1046 return this->plt_->entry_count();
1049 // Return the offset of the first non-reserved PLT entry.
1051 template<int size, bool big_endian>
1053 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1055 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1058 // Return the size of each PLT entry.
1060 template<int size, bool big_endian>
1062 Target_powerpc<size, big_endian>::plt_entry_size() const
1064 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1067 // Create a GOT entry for the TLS module index.
1069 template<int size, bool big_endian>
1071 Target_powerpc<size, big_endian>::got_mod_index_entry(
1072 Symbol_table* symtab,
1074 Sized_relobj_file<size, big_endian>* object)
1076 if (this->got_mod_index_offset_ == -1U)
1078 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1079 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1080 Output_data_got<size, big_endian>* got;
1081 unsigned int got_offset;
1083 got = this->got_section(symtab, layout);
1084 got_offset = got->add_constant(0);
1085 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1087 got->add_constant(0);
1088 this->got_mod_index_offset_ = got_offset;
1090 return this->got_mod_index_offset_;
1093 // Optimize the TLS relocation type based on what we know about the
1094 // symbol. IS_FINAL is true if the final address of this symbol is
1095 // known at link time.
1097 static tls::Tls_optimization
1098 optimize_tls_reloc(bool /* is_final */, int r_type)
1100 // If we are generating a shared library, then we can't do anything
1102 if (parameters->options().shared())
1103 return tls::TLSOPT_NONE;
1112 // Get the Reference_flags for a particular relocation.
1114 template<int size, bool big_endian>
1116 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
1117 unsigned int r_type)
1121 case elfcpp::R_POWERPC_NONE:
1122 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1123 case elfcpp::R_POWERPC_GNU_VTENTRY:
1124 case elfcpp::R_PPC64_TOC:
1125 // No symbol reference.
1128 case elfcpp::R_POWERPC_ADDR16:
1129 case elfcpp::R_POWERPC_ADDR16_LO:
1130 case elfcpp::R_POWERPC_ADDR16_HI:
1131 case elfcpp::R_POWERPC_ADDR16_HA:
1132 case elfcpp::R_POWERPC_ADDR32:
1133 case elfcpp::R_PPC64_ADDR64:
1134 return Symbol::ABSOLUTE_REF;
1136 case elfcpp::R_POWERPC_REL24:
1137 case elfcpp::R_PPC_LOCAL24PC:
1138 case elfcpp::R_PPC_REL16:
1139 case elfcpp::R_PPC_REL16_LO:
1140 case elfcpp::R_PPC_REL16_HI:
1141 case elfcpp::R_PPC_REL16_HA:
1142 return Symbol::RELATIVE_REF;
1144 case elfcpp::R_PPC_PLTREL24:
1145 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1147 case elfcpp::R_POWERPC_GOT16:
1148 case elfcpp::R_POWERPC_GOT16_LO:
1149 case elfcpp::R_POWERPC_GOT16_HI:
1150 case elfcpp::R_POWERPC_GOT16_HA:
1151 case elfcpp::R_PPC64_TOC16:
1152 case elfcpp::R_PPC64_TOC16_LO:
1153 case elfcpp::R_PPC64_TOC16_HI:
1154 case elfcpp::R_PPC64_TOC16_HA:
1155 case elfcpp::R_PPC64_TOC16_DS:
1156 case elfcpp::R_PPC64_TOC16_LO_DS:
1158 return Symbol::ABSOLUTE_REF;
1160 case elfcpp::R_POWERPC_GOT_TPREL16:
1161 case elfcpp::R_POWERPC_TLS:
1162 return Symbol::TLS_REF;
1164 case elfcpp::R_POWERPC_COPY:
1165 case elfcpp::R_POWERPC_GLOB_DAT:
1166 case elfcpp::R_POWERPC_JMP_SLOT:
1167 case elfcpp::R_POWERPC_RELATIVE:
1168 case elfcpp::R_POWERPC_DTPMOD:
1170 // Not expected. We will give an error later.
1175 // Report an unsupported relocation against a local symbol.
1177 template<int size, bool big_endian>
1179 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1180 Sized_relobj_file<size, big_endian>* object,
1181 unsigned int r_type)
1183 gold_error(_("%s: unsupported reloc %u against local symbol"),
1184 object->name().c_str(), r_type);
1187 // We are about to emit a dynamic relocation of type R_TYPE. If the
1188 // dynamic linker does not support it, issue an error.
1190 template<int size, bool big_endian>
1192 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1193 unsigned int r_type)
1195 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1197 // These are the relocation types supported by glibc for both 32-bit
1198 // and 64-bit powerpc.
1201 case elfcpp::R_POWERPC_RELATIVE:
1202 case elfcpp::R_POWERPC_GLOB_DAT:
1203 case elfcpp::R_POWERPC_DTPMOD:
1204 case elfcpp::R_POWERPC_DTPREL:
1205 case elfcpp::R_POWERPC_TPREL:
1206 case elfcpp::R_POWERPC_JMP_SLOT:
1207 case elfcpp::R_POWERPC_COPY:
1208 case elfcpp::R_POWERPC_ADDR32:
1209 case elfcpp::R_POWERPC_ADDR24:
1210 case elfcpp::R_POWERPC_REL24:
1221 // These are the relocation types supported only on 64-bit.
1222 case elfcpp::R_PPC64_ADDR64:
1223 case elfcpp::R_PPC64_TPREL16_LO_DS:
1224 case elfcpp::R_PPC64_TPREL16_DS:
1225 case elfcpp::R_POWERPC_TPREL16:
1226 case elfcpp::R_POWERPC_TPREL16_LO:
1227 case elfcpp::R_POWERPC_TPREL16_HI:
1228 case elfcpp::R_POWERPC_TPREL16_HA:
1229 case elfcpp::R_PPC64_TPREL16_HIGHER:
1230 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1231 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1232 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1233 case elfcpp::R_PPC64_ADDR16_LO_DS:
1234 case elfcpp::R_POWERPC_ADDR16_LO:
1235 case elfcpp::R_POWERPC_ADDR16_HI:
1236 case elfcpp::R_POWERPC_ADDR16_HA:
1237 case elfcpp::R_POWERPC_ADDR30:
1238 case elfcpp::R_PPC64_UADDR64:
1239 case elfcpp::R_POWERPC_UADDR32:
1240 case elfcpp::R_POWERPC_ADDR16:
1241 case elfcpp::R_POWERPC_UADDR16:
1242 case elfcpp::R_PPC64_ADDR16_DS:
1243 case elfcpp::R_PPC64_ADDR16_HIGHER:
1244 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1245 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1246 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1247 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1248 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1249 case elfcpp::R_POWERPC_REL32:
1250 case elfcpp::R_PPC64_REL64:
1261 // These are the relocation types supported only on 32-bit.
1268 // This prevents us from issuing more than one error per reloc
1269 // section. But we can still wind up issuing more than one
1270 // error per object file.
1271 if (this->issued_non_pic_error_)
1273 gold_assert(parameters->options().output_is_position_independent());
1274 object->error(_("requires unsupported dynamic reloc; "
1275 "recompile with -fPIC"));
1276 this->issued_non_pic_error_ = true;
1280 // Scan a relocation for a local symbol.
1282 template<int size, bool big_endian>
1284 Target_powerpc<size, big_endian>::Scan::local(
1285 Symbol_table* symtab,
1287 Target_powerpc<size, big_endian>* target,
1288 Sized_relobj_file<size, big_endian>* object,
1289 unsigned int data_shndx,
1290 Output_section* output_section,
1291 const elfcpp::Rela<size, big_endian>& reloc,
1292 unsigned int r_type,
1293 const elfcpp::Sym<size, big_endian>& lsym)
1297 case elfcpp::R_POWERPC_NONE:
1298 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1299 case elfcpp::R_POWERPC_GNU_VTENTRY:
1302 case elfcpp::R_PPC64_ADDR64:
1303 case elfcpp::R_POWERPC_ADDR32:
1304 case elfcpp::R_POWERPC_ADDR16_HA:
1305 case elfcpp::R_POWERPC_ADDR16_LO:
1306 // If building a shared library (or a position-independent
1307 // executable), we need to create a dynamic relocation for
1309 if (parameters->options().output_is_position_independent())
1311 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1313 check_non_pic(object, r_type);
1314 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1316 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1317 rela_dyn->add_local(object, r_sym, r_type, output_section,
1318 data_shndx, reloc.get_r_offset(),
1319 reloc.get_r_addend());
1323 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1324 gold_assert(lsym.get_st_value() == 0);
1325 rela_dyn->add_local_relative(object, r_sym, r_type,
1326 output_section, data_shndx,
1327 reloc.get_r_offset(),
1328 reloc.get_r_addend());
1333 case elfcpp::R_POWERPC_REL24:
1334 case elfcpp::R_PPC_LOCAL24PC:
1335 case elfcpp::R_POWERPC_REL32:
1336 case elfcpp::R_PPC_REL16_LO:
1337 case elfcpp::R_PPC_REL16_HA:
1340 case elfcpp::R_POWERPC_GOT16:
1341 case elfcpp::R_POWERPC_GOT16_LO:
1342 case elfcpp::R_POWERPC_GOT16_HI:
1343 case elfcpp::R_POWERPC_GOT16_HA:
1344 case elfcpp::R_PPC64_TOC16:
1345 case elfcpp::R_PPC64_TOC16_LO:
1346 case elfcpp::R_PPC64_TOC16_HI:
1347 case elfcpp::R_PPC64_TOC16_HA:
1348 case elfcpp::R_PPC64_TOC16_DS:
1349 case elfcpp::R_PPC64_TOC16_LO_DS:
1351 // The symbol requires a GOT entry.
1352 Output_data_got<size, big_endian>* got;
1355 got = target->got_section(symtab, layout);
1356 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1358 // If we are generating a shared object, we need to add a
1359 // dynamic relocation for this symbol's GOT entry.
1360 if (parameters->options().output_is_position_independent())
1362 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1364 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1367 off = got->add_constant(0);
1368 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1369 rela_dyn->add_local_relative(object, r_sym,
1370 elfcpp::R_POWERPC_RELATIVE,
1375 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1379 case elfcpp::R_PPC64_TOC:
1380 // We need a GOT section.
1381 target->got_section(symtab, layout);
1384 // These are relocations which should only be seen by the
1385 // dynamic linker, and should never be seen here.
1386 case elfcpp::R_POWERPC_COPY:
1387 case elfcpp::R_POWERPC_GLOB_DAT:
1388 case elfcpp::R_POWERPC_JMP_SLOT:
1389 case elfcpp::R_POWERPC_RELATIVE:
1390 case elfcpp::R_POWERPC_DTPMOD:
1391 gold_error(_("%s: unexpected reloc %u in object file"),
1392 object->name().c_str(), r_type);
1396 unsupported_reloc_local(object, r_type);
1401 // Report an unsupported relocation against a global symbol.
1403 template<int size, bool big_endian>
1405 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1406 Sized_relobj_file<size, big_endian>* object,
1407 unsigned int r_type,
1410 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1411 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1414 // Scan a relocation for a global symbol.
1416 template<int size, bool big_endian>
1418 Target_powerpc<size, big_endian>::Scan::global(
1419 Symbol_table* symtab,
1421 Target_powerpc<size, big_endian>* target,
1422 Sized_relobj_file<size, big_endian>* object,
1423 unsigned int data_shndx,
1424 Output_section* output_section,
1425 const elfcpp::Rela<size, big_endian>& reloc,
1426 unsigned int r_type,
1431 case elfcpp::R_POWERPC_NONE:
1432 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1433 case elfcpp::R_POWERPC_GNU_VTENTRY:
1436 case elfcpp::R_PPC_PLTREL24:
1437 // If the symbol is fully resolved, this is just a PC32 reloc.
1438 // Otherwise we need a PLT entry.
1439 if (gsym->final_value_is_known())
1441 // If building a shared library, we can also skip the PLT entry
1442 // if the symbol is defined in the output file and is protected
1444 if (gsym->is_defined()
1445 && !gsym->is_from_dynobj()
1446 && !gsym->is_preemptible())
1448 target->make_plt_entry(symtab, layout, gsym);
1451 case elfcpp::R_POWERPC_ADDR16:
1452 case elfcpp::R_POWERPC_ADDR16_LO:
1453 case elfcpp::R_POWERPC_ADDR16_HI:
1454 case elfcpp::R_POWERPC_ADDR16_HA:
1455 case elfcpp::R_POWERPC_ADDR32:
1456 case elfcpp::R_PPC64_ADDR64:
1458 // Make a PLT entry if necessary.
1459 if (gsym->needs_plt_entry())
1461 target->make_plt_entry(symtab, layout, gsym);
1462 // Since this is not a PC-relative relocation, we may be
1463 // taking the address of a function. In that case we need to
1464 // set the entry in the dynamic symbol table to the address of
1466 if (gsym->is_from_dynobj() && !parameters->options().shared())
1467 gsym->set_needs_dynsym_value();
1469 // Make a dynamic relocation if necessary.
1470 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1472 if (gsym->may_need_copy_reloc())
1474 target->copy_reloc(symtab, layout, object,
1475 data_shndx, output_section, gsym, reloc);
1477 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1478 || r_type == elfcpp::R_PPC64_ADDR64)
1479 && gsym->can_use_relative_reloc(false))
1481 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1482 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1483 output_section, object,
1484 data_shndx, reloc.get_r_offset(),
1485 reloc.get_r_addend());
1489 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1491 check_non_pic(object, r_type);
1492 if (gsym->is_from_dynobj()
1493 || gsym->is_undefined()
1494 || gsym->is_preemptible())
1495 rela_dyn->add_global(gsym, r_type, output_section,
1497 reloc.get_r_offset(),
1498 reloc.get_r_addend());
1500 rela_dyn->add_global_relative(gsym, r_type,
1501 output_section, object,
1503 reloc.get_r_offset(),
1504 reloc.get_r_addend());
1510 case elfcpp::R_POWERPC_REL24:
1511 case elfcpp::R_PPC_LOCAL24PC:
1512 case elfcpp::R_PPC_REL16:
1513 case elfcpp::R_PPC_REL16_LO:
1514 case elfcpp::R_PPC_REL16_HI:
1515 case elfcpp::R_PPC_REL16_HA:
1517 if (gsym->needs_plt_entry())
1518 target->make_plt_entry(symtab, layout, gsym);
1519 // Make a dynamic relocation if necessary.
1520 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1522 if (gsym->may_need_copy_reloc())
1524 target->copy_reloc(symtab, layout, object,
1525 data_shndx, output_section, gsym,
1530 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1531 check_non_pic(object, r_type);
1532 rela_dyn->add_global(gsym, r_type, output_section, object,
1533 data_shndx, reloc.get_r_offset(),
1534 reloc.get_r_addend());
1540 case elfcpp::R_POWERPC_GOT16:
1541 case elfcpp::R_POWERPC_GOT16_LO:
1542 case elfcpp::R_POWERPC_GOT16_HI:
1543 case elfcpp::R_POWERPC_GOT16_HA:
1544 case elfcpp::R_PPC64_TOC16:
1545 case elfcpp::R_PPC64_TOC16_LO:
1546 case elfcpp::R_PPC64_TOC16_HI:
1547 case elfcpp::R_PPC64_TOC16_HA:
1548 case elfcpp::R_PPC64_TOC16_DS:
1549 case elfcpp::R_PPC64_TOC16_LO_DS:
1551 // The symbol requires a GOT entry.
1552 Output_data_got<size, big_endian>* got;
1554 got = target->got_section(symtab, layout);
1555 if (gsym->final_value_is_known())
1556 got->add_global(gsym, GOT_TYPE_STANDARD);
1559 // If this symbol is not fully resolved, we need to add a
1560 // dynamic relocation for it.
1561 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1562 if (gsym->is_from_dynobj()
1563 || gsym->is_undefined()
1564 || gsym->is_preemptible())
1565 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1566 elfcpp::R_POWERPC_GLOB_DAT);
1567 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1569 unsigned int off = got->add_constant(0);
1571 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1572 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1579 case elfcpp::R_PPC64_TOC:
1580 // We need a GOT section.
1581 target->got_section(symtab, layout);
1584 case elfcpp::R_POWERPC_GOT_TPREL16:
1585 case elfcpp::R_POWERPC_TLS:
1589 // These are relocations which should only be seen by the
1590 // dynamic linker, and should never be seen here.
1591 case elfcpp::R_POWERPC_COPY:
1592 case elfcpp::R_POWERPC_GLOB_DAT:
1593 case elfcpp::R_POWERPC_JMP_SLOT:
1594 case elfcpp::R_POWERPC_RELATIVE:
1595 case elfcpp::R_POWERPC_DTPMOD:
1596 gold_error(_("%s: unexpected reloc %u in object file"),
1597 object->name().c_str(), r_type);
1601 unsupported_reloc_global(object, r_type, gsym);
1606 // Process relocations for gc.
1608 template<int size, bool big_endian>
1610 Target_powerpc<size, big_endian>::gc_process_relocs(
1611 Symbol_table* symtab,
1613 Sized_relobj_file<size, big_endian>* object,
1614 unsigned int data_shndx,
1616 const unsigned char* prelocs,
1618 Output_section* output_section,
1619 bool needs_special_offset_handling,
1620 size_t local_symbol_count,
1621 const unsigned char* plocal_symbols)
1623 typedef Target_powerpc<size, big_endian> Powerpc;
1624 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1626 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
1627 typename Target_powerpc::Relocatable_size_for_reloc>(
1636 needs_special_offset_handling,
1641 // Scan relocations for a section.
1643 template<int size, bool big_endian>
1645 Target_powerpc<size, big_endian>::scan_relocs(
1646 Symbol_table* symtab,
1648 Sized_relobj_file<size, big_endian>* object,
1649 unsigned int data_shndx,
1650 unsigned int sh_type,
1651 const unsigned char* prelocs,
1653 Output_section* output_section,
1654 bool needs_special_offset_handling,
1655 size_t local_symbol_count,
1656 const unsigned char* plocal_symbols)
1658 typedef Target_powerpc<size, big_endian> Powerpc;
1659 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1660 static Output_data_space* sdata;
1662 if (sh_type == elfcpp::SHT_REL)
1664 gold_error(_("%s: unsupported REL reloc section"),
1665 object->name().c_str());
1669 // Define _SDA_BASE_ at the start of the .sdata section.
1672 // layout->find_output_section(".sdata") == NULL
1673 sdata = new Output_data_space(4, "** sdata");
1674 Output_section* os = layout->add_output_section_data(".sdata", 0,
1676 | elfcpp::SHF_WRITE,
1680 symtab->define_in_output_data("_SDA_BASE_", NULL,
1681 Symbol_table::PREDEFINED,
1686 elfcpp::STV_HIDDEN, 0,
1690 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1699 needs_special_offset_handling,
1704 // Finalize the sections.
1706 template<int size, bool big_endian>
1708 Target_powerpc<size, big_endian>::do_finalize_sections(
1710 const Input_objects*,
1713 // Fill in some more dynamic tags.
1714 const Reloc_section* rel_plt = (this->plt_ == NULL
1716 : this->plt_->rel_plt());
1717 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
1718 this->rela_dyn_, true, size == 32);
1720 // Emit any relocs we saved in an attempt to avoid generating COPY
1722 if (this->copy_relocs_.any_saved_relocs())
1723 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1726 // Perform a relocation.
1728 template<int size, bool big_endian>
1730 Target_powerpc<size, big_endian>::Relocate::relocate(
1731 const Relocate_info<size, big_endian>* relinfo,
1732 Target_powerpc* target,
1735 const elfcpp::Rela<size, big_endian>& rela,
1736 unsigned int r_type,
1737 const Sized_symbol<size>* gsym,
1738 const Symbol_value<size>* psymval,
1739 unsigned char* view,
1740 typename elfcpp::Elf_types<size>::Elf_Addr address,
1741 section_size_type /* view_size */)
1743 const unsigned int toc_base_offset = 0x8000;
1744 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1746 // Pick the value to use for symbols defined in shared objects.
1747 Symbol_value<size> symval;
1749 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
1751 elfcpp::Elf_Xword value;
1753 value = target->plt_section()->address() + gsym->plt_offset();
1755 symval.set_output_value(value);
1760 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1761 elfcpp::Elf_Xword addend = rela.get_r_addend();
1763 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1764 // pointer points to the beginning, not the end, of the table.
1765 // So we just use the plain offset.
1766 unsigned int got_offset = 0;
1767 unsigned int got2_offset = 0;
1770 case elfcpp::R_PPC64_TOC16:
1771 case elfcpp::R_PPC64_TOC16_LO:
1772 case elfcpp::R_PPC64_TOC16_HI:
1773 case elfcpp::R_PPC64_TOC16_HA:
1774 case elfcpp::R_PPC64_TOC16_DS:
1775 case elfcpp::R_PPC64_TOC16_LO_DS:
1776 // Subtract the TOC base address.
1777 addend -= target->toc_section()->address() + toc_base_offset;
1780 case elfcpp::R_POWERPC_GOT16:
1781 case elfcpp::R_POWERPC_GOT16_LO:
1782 case elfcpp::R_POWERPC_GOT16_HI:
1783 case elfcpp::R_POWERPC_GOT16_HA:
1784 case elfcpp::R_PPC64_GOT16_DS:
1785 case elfcpp::R_PPC64_GOT16_LO_DS:
1788 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1789 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1793 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1794 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1795 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1799 // R_PPC_PLTREL24 is rather special. If non-zero,
1800 // the addend specifies the GOT pointer offset within .got2.
1801 case elfcpp::R_PPC_PLTREL24:
1802 if (addend >= 32768)
1804 Output_data_space* got2;
1805 got2 = target->got2_section();
1806 got2_offset = got2->offset();
1807 addend += got2_offset;
1817 case elfcpp::R_POWERPC_NONE:
1818 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1819 case elfcpp::R_POWERPC_GNU_VTENTRY:
1822 case elfcpp::R_POWERPC_REL32:
1823 Reloc::rel32(view, object, psymval, addend, address);
1826 case elfcpp::R_POWERPC_REL24:
1827 Reloc::rel24(view, object, psymval, addend, address);
1830 case elfcpp::R_POWERPC_REL14:
1831 Reloc::rel14(view, object, psymval, addend, address);
1834 case elfcpp::R_PPC_PLTREL24:
1835 Reloc::rel24(view, object, psymval, addend, address);
1838 case elfcpp::R_PPC_LOCAL24PC:
1839 Reloc::rel24(view, object, psymval, addend, address);
1842 case elfcpp::R_PPC64_ADDR64:
1843 if (!parameters->options().output_is_position_independent())
1844 Relocate_functions<size, big_endian>::rela64(view, object,
1848 case elfcpp::R_POWERPC_ADDR32:
1849 if (!parameters->options().output_is_position_independent())
1850 Relocate_functions<size, big_endian>::rela32(view, object,
1854 case elfcpp::R_POWERPC_ADDR16_LO:
1855 Reloc::addr16_lo(view, object, psymval, addend);
1858 case elfcpp::R_POWERPC_ADDR16_HI:
1859 Reloc::addr16_hi(view, object, psymval, addend);
1862 case elfcpp::R_POWERPC_ADDR16_HA:
1863 Reloc::addr16_ha(view, object, psymval, addend);
1866 case elfcpp::R_PPC_REL16_LO:
1867 Reloc::rel16_lo(view, object, psymval, addend, address);
1870 case elfcpp::R_PPC_REL16_HI:
1871 Reloc::rel16_lo(view, object, psymval, addend, address);
1874 case elfcpp::R_PPC_REL16_HA:
1875 Reloc::rel16_ha(view, object, psymval, addend, address);
1878 case elfcpp::R_POWERPC_GOT16:
1879 Reloc::addr16(view, got_offset, addend);
1882 case elfcpp::R_POWERPC_GOT16_LO:
1883 Reloc::addr16_lo(view, got_offset, addend);
1886 case elfcpp::R_POWERPC_GOT16_HI:
1887 Reloc::addr16_hi(view, got_offset, addend);
1890 case elfcpp::R_POWERPC_GOT16_HA:
1891 Reloc::addr16_ha(view, got_offset, addend);
1894 case elfcpp::R_PPC64_TOC16:
1895 Reloc::addr16(view, got_offset, addend);
1898 case elfcpp::R_PPC64_TOC16_LO:
1899 Reloc::addr16_lo(view, got_offset, addend);
1902 case elfcpp::R_PPC64_TOC16_HI:
1903 Reloc::addr16_hi(view, got_offset, addend);
1906 case elfcpp::R_PPC64_TOC16_HA:
1907 Reloc::addr16_ha(view, got_offset, addend);
1910 case elfcpp::R_PPC64_TOC16_DS:
1911 case elfcpp::R_PPC64_TOC16_LO_DS:
1912 Reloc::addr16_ds(view, got_offset, addend);
1915 case elfcpp::R_PPC64_TOC:
1917 elfcpp::Elf_types<64>::Elf_Addr value;
1918 value = target->toc_section()->address() + toc_base_offset;
1919 Relocate_functions<64, false>::rela64(view, value, addend);
1923 case elfcpp::R_POWERPC_COPY:
1924 case elfcpp::R_POWERPC_GLOB_DAT:
1925 case elfcpp::R_POWERPC_JMP_SLOT:
1926 case elfcpp::R_POWERPC_RELATIVE:
1927 // This is an outstanding tls reloc, which is unexpected when
1929 case elfcpp::R_POWERPC_DTPMOD:
1930 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1931 _("unexpected reloc %u in object file"),
1936 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1937 _("unsupported reloc %u"),
1945 // Perform a TLS relocation.
1947 template<int size, bool big_endian>
1949 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1950 const Relocate_info<size, big_endian>* relinfo,
1951 Target_powerpc<size, big_endian>* target,
1953 const elfcpp::Rela<size, big_endian>& rela,
1954 unsigned int r_type,
1955 const Sized_symbol<size>* gsym,
1956 const Symbol_value<size>* psymval,
1957 unsigned char* view,
1958 typename elfcpp::Elf_types<size>::Elf_Addr address,
1961 Output_segment* tls_segment = relinfo->layout->tls_segment();
1962 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1963 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1965 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1966 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1968 const bool is_final =
1970 ? !parameters->options().output_is_position_independent()
1971 : gsym->final_value_is_known());
1972 const tls::Tls_optimization optimized_type
1973 = optimize_tls_reloc(is_final, r_type);
1981 // Relocate section data.
1983 template<int size, bool big_endian>
1985 Target_powerpc<size, big_endian>::relocate_section(
1986 const Relocate_info<size, big_endian>* relinfo,
1987 unsigned int sh_type,
1988 const unsigned char* prelocs,
1990 Output_section* output_section,
1991 bool needs_special_offset_handling,
1992 unsigned char* view,
1993 typename elfcpp::Elf_types<size>::Elf_Addr address,
1994 section_size_type view_size,
1995 const Reloc_symbol_changes* reloc_symbol_changes)
1997 typedef Target_powerpc<size, big_endian> Powerpc;
1998 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2000 gold_assert(sh_type == elfcpp::SHT_RELA);
2002 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2009 needs_special_offset_handling,
2013 reloc_symbol_changes);
2016 // Return the size of a relocation while scanning during a relocatable
2019 template<int size, bool big_endian>
2021 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2025 // We are always SHT_RELA, so we should never get here.
2030 // Scan the relocs during a relocatable link.
2032 template<int size, bool big_endian>
2034 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2035 Symbol_table* symtab,
2037 Sized_relobj_file<size, big_endian>* object,
2038 unsigned int data_shndx,
2039 unsigned int sh_type,
2040 const unsigned char* prelocs,
2042 Output_section* output_section,
2043 bool needs_special_offset_handling,
2044 size_t local_symbol_count,
2045 const unsigned char* plocal_symbols,
2046 Relocatable_relocs* rr)
2048 gold_assert(sh_type == elfcpp::SHT_RELA);
2050 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2051 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2053 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2054 Scan_relocatable_relocs>(
2062 needs_special_offset_handling,
2068 // Relocate a section during a relocatable link.
2070 template<int size, bool big_endian>
2072 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2073 const Relocate_info<size, big_endian>* relinfo,
2074 unsigned int sh_type,
2075 const unsigned char* prelocs,
2077 Output_section* output_section,
2078 off_t offset_in_output_section,
2079 const Relocatable_relocs* rr,
2080 unsigned char* view,
2081 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2082 section_size_type view_size,
2083 unsigned char* reloc_view,
2084 section_size_type reloc_view_size)
2086 gold_assert(sh_type == elfcpp::SHT_RELA);
2088 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
2093 offset_in_output_section,
2102 // Return the value to use for a dynamic which requires special
2103 // treatment. This is how we support equality comparisons of function
2104 // pointers across shared library boundaries, as described in the
2105 // processor specific ABI supplement.
2107 template<int size, bool big_endian>
2109 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2111 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2112 return this->plt_section()->address() + gsym->plt_offset();
2115 // The selector for powerpc object files.
2117 template<int size, bool big_endian>
2118 class Target_selector_powerpc : public Target_selector
2121 Target_selector_powerpc()
2122 : Target_selector(elfcpp::EM_NONE, size, big_endian,
2124 (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
2125 (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
2128 Target* do_recognize(int machine, int, int)
2133 if (machine != elfcpp::EM_PPC64)
2138 if (machine != elfcpp::EM_PPC)
2146 return this->instantiate_target();
2149 Target* do_instantiate_target()
2150 { return new Target_powerpc<size, big_endian>(); }
2153 Target_selector_powerpc<32, true> target_selector_ppc32;
2154 Target_selector_powerpc<32, false> target_selector_ppc32le;
2155 Target_selector_powerpc<64, true> target_selector_ppc64;
2156 Target_selector_powerpc<64, false> target_selector_ppc64le;
2158 } // End anonymous namespace.