1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
27 #include "parameters.h"
34 #include "copy-relocs.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
47 template<int size, bool big_endian>
48 class Output_data_plt_powerpc;
50 template<int size, bool big_endian>
51 class Target_powerpc : public Sized_target<size, big_endian>
54 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
57 : Sized_target<size, big_endian>(&powerpc_info),
58 got_(NULL), got2_(NULL), toc_(NULL),
59 plt_(NULL), rela_dyn_(NULL),
60 copy_relocs_(elfcpp::R_POWERPC_COPY),
61 dynbss_(NULL), got_mod_index_offset_(-1U)
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
68 gc_process_relocs(Symbol_table* symtab,
70 Sized_relobj_file<size, big_endian>* object,
71 unsigned int data_shndx,
73 const unsigned char* prelocs,
75 Output_section* output_section,
76 bool needs_special_offset_handling,
77 size_t local_symbol_count,
78 const unsigned char* plocal_symbols);
80 // Scan the relocations to look for symbol adjustments.
82 scan_relocs(Symbol_table* symtab,
84 Sized_relobj_file<size, big_endian>* object,
85 unsigned int data_shndx,
87 const unsigned char* prelocs,
89 Output_section* output_section,
90 bool needs_special_offset_handling,
91 size_t local_symbol_count,
92 const unsigned char* plocal_symbols);
93 // Finalize the sections.
95 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
97 // Return the value to use for a dynamic which requires special
100 do_dynsym_value(const Symbol*) const;
102 // Relocate a section.
104 relocate_section(const Relocate_info<size, big_endian>*,
105 unsigned int sh_type,
106 const unsigned char* prelocs,
108 Output_section* output_section,
109 bool needs_special_offset_handling,
111 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
112 section_size_type view_size,
113 const Reloc_symbol_changes*);
115 // Scan the relocs during a relocatable link.
117 scan_relocatable_relocs(Symbol_table* symtab,
119 Sized_relobj_file<size, big_endian>* object,
120 unsigned int data_shndx,
121 unsigned int sh_type,
122 const unsigned char* prelocs,
124 Output_section* output_section,
125 bool needs_special_offset_handling,
126 size_t local_symbol_count,
127 const unsigned char* plocal_symbols,
128 Relocatable_relocs*);
130 // Relocate a section during a relocatable link.
132 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
133 unsigned int sh_type,
134 const unsigned char* prelocs,
136 Output_section* output_section,
137 off_t offset_in_output_section,
138 const Relocatable_relocs*,
140 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
141 section_size_type view_size,
142 unsigned char* reloc_view,
143 section_size_type reloc_view_size);
145 // Return whether SYM is defined by the ABI.
147 do_is_defined_by_abi(const Symbol* sym) const
149 return strcmp(sym->name(), "___tls_get_addr") == 0;
152 // Return the size of the GOT section.
156 gold_assert(this->got_ != NULL);
157 return this->got_->data_size();
160 // Return the number of entries in the GOT.
162 got_entry_count() const
164 if (this->got_ == NULL)
166 return this->got_size() / (size / 8);
169 // Return the number of entries in the PLT.
171 plt_entry_count() const;
173 // Return the offset of the first non-reserved PLT entry.
175 first_plt_entry_offset() const;
177 // Return the size of each PLT entry.
179 plt_entry_size() const;
183 // The class which scans relocations.
188 : issued_non_pic_error_(false)
192 get_reference_flags(unsigned int r_type);
195 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
196 Sized_relobj_file<size, big_endian>* object,
197 unsigned int data_shndx,
198 Output_section* output_section,
199 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
200 const elfcpp::Sym<size, big_endian>& lsym);
203 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
204 Sized_relobj_file<size, big_endian>* object,
205 unsigned int data_shndx,
206 Output_section* output_section,
207 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
211 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
213 Sized_relobj_file<size, big_endian>* ,
216 const elfcpp::Rela<size, big_endian>& ,
218 const elfcpp::Sym<size, big_endian>&)
222 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
224 Sized_relobj_file<size, big_endian>* ,
227 const elfcpp::Rela<size,
229 unsigned int , Symbol*)
234 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
235 unsigned int r_type);
238 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
239 unsigned int r_type, Symbol*);
242 generate_tls_call(Symbol_table* symtab, Layout* layout,
243 Target_powerpc* target);
246 check_non_pic(Relobj*, unsigned int r_type);
248 // Whether we have issued an error about a non-PIC compilation.
249 bool issued_non_pic_error_;
252 // The class which implements relocation.
256 // Do a relocation. Return false if the caller should not issue
257 // any warnings about this relocation.
259 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
260 Output_section*, size_t relnum,
261 const elfcpp::Rela<size, big_endian>&,
262 unsigned int r_type, const Sized_symbol<size>*,
263 const Symbol_value<size>*,
265 typename elfcpp::Elf_types<size>::Elf_Addr,
269 // Do a TLS relocation.
271 relocate_tls(const Relocate_info<size, big_endian>*,
272 Target_powerpc* target,
273 size_t relnum, const elfcpp::Rela<size, big_endian>&,
274 unsigned int r_type, const Sized_symbol<size>*,
275 const Symbol_value<size>*,
277 typename elfcpp::Elf_types<size>::Elf_Addr,
281 // A class which returns the size required for a relocation type,
282 // used while scanning relocs during a relocatable link.
283 class Relocatable_size_for_reloc
287 get_size_for_reloc(unsigned int, Relobj*);
290 // Get the GOT section, creating it if necessary.
291 Output_data_got<size, big_endian>*
292 got_section(Symbol_table*, Layout*);
297 gold_assert(this->got2_ != NULL);
301 // Get the TOC section.
305 gold_assert(this->toc_ != NULL);
309 // Create a PLT entry for a global symbol.
311 make_plt_entry(Symbol_table*, Layout*, Symbol*);
313 // Create a GOT entry for the TLS module index.
315 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
316 Sized_relobj_file<size, big_endian>* object);
318 // Get the PLT section.
319 const Output_data_plt_powerpc<size, big_endian>*
322 gold_assert(this->plt_ != NULL);
326 // Get the dynamic reloc section, creating it if necessary.
328 rela_dyn_section(Layout*);
330 // Copy a relocation against a global symbol.
332 copy_reloc(Symbol_table* symtab, Layout* layout,
333 Sized_relobj_file<size, big_endian>* object,
334 unsigned int shndx, Output_section* output_section,
335 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
337 this->copy_relocs_.copy_reloc(symtab, layout,
338 symtab->get_sized_symbol<size>(sym),
339 object, shndx, output_section,
340 reloc, this->rela_dyn_section(layout));
343 // Information about this specific target which we pass to the
344 // general Target structure.
345 static Target::Target_info powerpc_info;
347 // The types of GOT entries needed for this platform.
348 // These values are exposed to the ABI in an incremental link.
349 // Do not renumber existing values without changing the version
350 // number of the .gnu_incremental_inputs section.
353 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
354 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
355 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
359 Output_data_got<size, big_endian>* got_;
361 Output_data_space* got2_;
363 Output_data_space* toc_;
365 Output_data_plt_powerpc<size, big_endian>* plt_;
366 // The dynamic reloc section.
367 Reloc_section* rela_dyn_;
368 // Relocs saved to avoid a COPY reloc.
369 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
370 // Space for variables copied with a COPY reloc.
371 Output_data_space* dynbss_;
372 // Offset of the GOT entry for the TLS module index;
373 unsigned int got_mod_index_offset_;
377 Target::Target_info Target_powerpc<32, true>::powerpc_info =
380 true, // is_big_endian
381 elfcpp::EM_PPC, // machine_code
382 false, // has_make_symbol
383 false, // has_resolve
384 false, // has_code_fill
385 true, // is_default_stack_executable
386 false, // can_icf_inline_merge_sections
388 "/usr/lib/ld.so.1", // dynamic_linker
389 0x10000000, // default_text_segment_address
390 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
391 4 * 1024, // common_pagesize (overridable by -z common-page-size)
392 elfcpp::SHN_UNDEF, // small_common_shndx
393 elfcpp::SHN_UNDEF, // large_common_shndx
394 0, // small_common_section_flags
395 0, // large_common_section_flags
396 NULL, // attributes_section
397 NULL // attributes_vendor
401 Target::Target_info Target_powerpc<32, false>::powerpc_info =
404 false, // is_big_endian
405 elfcpp::EM_PPC, // machine_code
406 false, // has_make_symbol
407 false, // has_resolve
408 false, // has_code_fill
409 true, // is_default_stack_executable
410 false, // can_icf_inline_merge_sections
412 "/usr/lib/ld.so.1", // dynamic_linker
413 0x10000000, // default_text_segment_address
414 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
415 4 * 1024, // common_pagesize (overridable by -z common-page-size)
416 elfcpp::SHN_UNDEF, // small_common_shndx
417 elfcpp::SHN_UNDEF, // large_common_shndx
418 0, // small_common_section_flags
419 0, // large_common_section_flags
420 NULL, // attributes_section
421 NULL // attributes_vendor
425 Target::Target_info Target_powerpc<64, true>::powerpc_info =
428 true, // is_big_endian
429 elfcpp::EM_PPC64, // machine_code
430 false, // has_make_symbol
431 false, // has_resolve
432 false, // has_code_fill
433 true, // is_default_stack_executable
434 false, // can_icf_inline_merge_sections
436 "/usr/lib/ld.so.1", // dynamic_linker
437 0x10000000, // default_text_segment_address
438 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
439 8 * 1024, // common_pagesize (overridable by -z common-page-size)
440 elfcpp::SHN_UNDEF, // small_common_shndx
441 elfcpp::SHN_UNDEF, // large_common_shndx
442 0, // small_common_section_flags
443 0, // large_common_section_flags
444 NULL, // attributes_section
445 NULL // attributes_vendor
449 Target::Target_info Target_powerpc<64, false>::powerpc_info =
452 false, // is_big_endian
453 elfcpp::EM_PPC64, // machine_code
454 false, // has_make_symbol
455 false, // has_resolve
456 false, // has_code_fill
457 true, // is_default_stack_executable
458 false, // can_icf_inline_merge_sections
460 "/usr/lib/ld.so.1", // dynamic_linker
461 0x10000000, // default_text_segment_address
462 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
463 8 * 1024, // common_pagesize (overridable by -z common-page-size)
464 elfcpp::SHN_UNDEF, // small_common_shndx
465 elfcpp::SHN_UNDEF, // large_common_shndx
466 0, // small_common_section_flags
467 0, // large_common_section_flags
468 NULL, // attributes_section
469 NULL // attributes_vendor
472 template<int size, bool big_endian>
473 class Powerpc_relocate_functions
476 // Do a simple relocation with the addend in the relocation.
477 template<int valsize>
479 rela(unsigned char* view,
480 unsigned int right_shift,
481 elfcpp::Elf_Xword dst_mask,
482 typename elfcpp::Swap<size, big_endian>::Valtype value,
483 typename elfcpp::Swap<size, big_endian>::Valtype addend)
485 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
486 Valtype* wv = reinterpret_cast<Valtype*>(view);
487 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
488 Valtype reloc = ((value + addend) >> right_shift);
493 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
496 // Do a simple relocation using a symbol value with the addend in
498 template<int valsize>
500 rela(unsigned char* view,
501 unsigned int right_shift,
502 elfcpp::Elf_Xword dst_mask,
503 const Sized_relobj_file<size, big_endian>* object,
504 const Symbol_value<size>* psymval,
505 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
507 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
508 Valtype* wv = reinterpret_cast<Valtype*>(view);
509 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
510 Valtype reloc = (psymval->value(object, addend) >> right_shift);
515 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
518 // Do a simple relocation using a symbol value with the addend in
519 // the relocation, unaligned.
520 template<int valsize>
522 rela_ua(unsigned char* view, unsigned int right_shift,
523 elfcpp::Elf_Xword dst_mask,
524 const Sized_relobj_file<size, big_endian>* object,
525 const Symbol_value<size>* psymval,
526 typename elfcpp::Swap<size, big_endian>::Valtype addend)
528 typedef typename elfcpp::Swap_unaligned<valsize,
529 big_endian>::Valtype Valtype;
530 unsigned char* wv = view;
531 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
532 Valtype reloc = (psymval->value(object, addend) >> right_shift);
537 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
540 // Do a simple PC relative relocation with a Symbol_value with the
541 // addend in the relocation.
542 template<int valsize>
544 pcrela(unsigned char* view, unsigned int right_shift,
545 elfcpp::Elf_Xword dst_mask,
546 const Sized_relobj_file<size, big_endian>* object,
547 const Symbol_value<size>* psymval,
548 typename elfcpp::Swap<size, big_endian>::Valtype addend,
549 typename elfcpp::Elf_types<size>::Elf_Addr address)
551 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
552 Valtype* wv = reinterpret_cast<Valtype*>(view);
553 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
554 Valtype reloc = ((psymval->value(object, addend) - address)
560 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
563 template<int valsize>
565 pcrela_unaligned(unsigned char* view,
566 const Sized_relobj_file<size, big_endian>* object,
567 const Symbol_value<size>* psymval,
568 typename elfcpp::Swap<size, big_endian>::Valtype addend,
569 typename elfcpp::Elf_types<size>::Elf_Addr address)
571 typedef typename elfcpp::Swap_unaligned<valsize,
572 big_endian>::Valtype Valtype;
573 unsigned char* wv = view;
574 Valtype reloc = (psymval->value(object, addend) - address);
576 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
579 typedef Powerpc_relocate_functions<size, big_endian> This;
580 typedef Relocate_functions<size, big_endian> This_reloc;
582 // R_POWERPC_REL32: (Symbol + Addend - Address)
584 rel32(unsigned char* view,
585 const Sized_relobj_file<size, big_endian>* object,
586 const Symbol_value<size>* psymval,
587 typename elfcpp::Elf_types<size>::Elf_Addr addend,
588 typename elfcpp::Elf_types<size>::Elf_Addr address)
589 { This_reloc::pcrela32(view, object, psymval, addend, address); }
591 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
593 rel24(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)
599 This::template pcrela<32>(view, 0, 0x03fffffc, object,
600 psymval, addend, address);
603 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
605 rel14(unsigned char* view,
606 const Sized_relobj_file<size, big_endian>* object,
607 const Symbol_value<size>* psymval,
608 typename elfcpp::Elf_types<size>::Elf_Addr addend,
609 typename elfcpp::Elf_types<size>::Elf_Addr address)
611 This::template pcrela<32>(view, 0, 0x0000fffc, object,
612 psymval, addend, address);
615 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
617 addr16(unsigned char* view,
618 typename elfcpp::Elf_types<size>::Elf_Addr value,
619 typename elfcpp::Elf_types<size>::Elf_Addr addend)
620 { This_reloc::rela16(view, value, addend); }
623 addr16(unsigned char* view,
624 const Sized_relobj_file<size, big_endian>* object,
625 const Symbol_value<size>* psymval,
626 typename elfcpp::Elf_types<size>::Elf_Addr addend)
627 { This_reloc::rela16(view, object, psymval, addend); }
629 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
631 addr16_ds(unsigned char* view,
632 typename elfcpp::Elf_types<size>::Elf_Addr value,
633 typename elfcpp::Elf_types<size>::Elf_Addr addend)
635 This::template rela<16>(view, 0, 0xfffc, value, addend);
638 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
640 addr16_lo(unsigned char* view,
641 typename elfcpp::Elf_types<size>::Elf_Addr value,
642 typename elfcpp::Elf_types<size>::Elf_Addr addend)
643 { This_reloc::rela16(view, value, addend); }
646 addr16_lo(unsigned char* view,
647 const Sized_relobj_file<size, big_endian>* object,
648 const Symbol_value<size>* psymval,
649 typename elfcpp::Elf_types<size>::Elf_Addr addend)
650 { This_reloc::rela16(view, object, psymval, addend); }
652 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
654 addr16_hi(unsigned char* view,
655 typename elfcpp::Elf_types<size>::Elf_Addr value,
656 typename elfcpp::Elf_types<size>::Elf_Addr addend)
658 This::template rela<16>(view, 16, 0xffff, value, addend);
662 addr16_hi(unsigned char* view,
663 const Sized_relobj_file<size, big_endian>* object,
664 const Symbol_value<size>* psymval,
665 typename elfcpp::Elf_types<size>::Elf_Addr addend)
667 This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
670 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
671 // final value of the low 16 bits of the
672 // relocation is negative, add one.
674 addr16_ha(unsigned char* view,
675 typename elfcpp::Elf_types<size>::Elf_Addr value,
676 typename elfcpp::Elf_types<size>::Elf_Addr addend)
678 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
680 reloc = value + addend;
686 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
690 addr16_ha(unsigned char* view,
691 const Sized_relobj_file<size, big_endian>* object,
692 const Symbol_value<size>* psymval,
693 typename elfcpp::Elf_types<size>::Elf_Addr addend)
695 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
697 reloc = psymval->value(object, addend);
703 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
706 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
708 rel16(unsigned char* view,
709 const Sized_relobj_file<size, big_endian>* object,
710 const Symbol_value<size>* psymval,
711 typename elfcpp::Elf_types<size>::Elf_Addr addend,
712 typename elfcpp::Elf_types<size>::Elf_Addr address)
713 { This_reloc::pcrela16(view, object, psymval, addend, address); }
715 // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
717 rel16_lo(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_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
726 rel16_hi(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)
732 This::template pcrela<16>(view, 16, 0xffff, object,
733 psymval, addend, address);
736 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
737 // final value of the low 16 bits of the
738 // relocation is negative, add one.
740 rel16_ha(unsigned char* view,
741 const Sized_relobj_file<size, big_endian>* object,
742 const Symbol_value<size>* psymval,
743 typename elfcpp::Elf_types<size>::Elf_Addr addend,
744 typename elfcpp::Elf_types<size>::Elf_Addr address)
746 typename elfcpp::Elf_types<size>::Elf_Addr reloc;
748 reloc = (psymval->value(object, addend) - address);
753 elfcpp::Swap<16, big_endian>::writeval(view, reloc);
757 // Get the GOT section, creating it if necessary.
759 template<int size, bool big_endian>
760 Output_data_got<size, big_endian>*
761 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
764 if (this->got_ == NULL)
766 gold_assert(symtab != NULL && layout != NULL);
768 this->got_ = new Output_data_got<size, big_endian>();
770 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
771 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
772 this->got_, ORDER_DATA, false);
774 // Create the GOT2 or TOC in the .got section.
777 this->got2_ = new Output_data_space(4, "** GOT2");
778 layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
781 this->got2_, ORDER_DATA, false);
785 this->toc_ = new Output_data_space(8, "** TOC");
786 layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
789 this->toc_, ORDER_DATA, false);
792 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
793 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
794 Symbol_table::PREDEFINED,
796 0, 0, elfcpp::STT_OBJECT,
798 elfcpp::STV_HIDDEN, 0,
805 // Get the dynamic reloc section, creating it if necessary.
807 template<int size, bool big_endian>
808 typename Target_powerpc<size, big_endian>::Reloc_section*
809 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
811 if (this->rela_dyn_ == NULL)
813 gold_assert(layout != NULL);
814 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
815 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
816 elfcpp::SHF_ALLOC, this->rela_dyn_,
817 ORDER_DYNAMIC_RELOCS, false);
819 return this->rela_dyn_;
822 // A class to handle the PLT data.
824 template<int size, bool big_endian>
825 class Output_data_plt_powerpc : public Output_section_data
828 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
829 size, big_endian> Reloc_section;
831 Output_data_plt_powerpc(Layout*);
833 // Add an entry to the PLT.
834 void add_entry(Symbol* gsym);
836 // Return the .rela.plt section data.
837 const Reloc_section* rel_plt() const
842 // Return the number of PLT entries.
845 { return this->count_; }
847 // Return the offset of the first non-reserved PLT entry.
849 first_plt_entry_offset()
850 { return 4 * base_plt_entry_size; }
852 // Return the size of a PLT entry.
855 { return base_plt_entry_size; }
858 void do_adjust_output_section(Output_section* os);
861 // The size of an entry in the PLT.
862 static const int base_plt_entry_size = (size == 32 ? 16 : 24);
864 // Set the final size.
866 set_final_data_size()
868 unsigned int full_count = this->count_ + 4;
870 this->set_data_size(full_count * base_plt_entry_size);
873 // Write out the PLT data.
875 do_write(Output_file*);
877 // The reloc section.
879 // The number of PLT entries.
883 // Create the PLT section. The ordinary .got section is an argument,
884 // since we need to refer to the start.
886 template<int size, bool big_endian>
887 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
888 : Output_section_data(size == 32 ? 4 : 8), count_(0)
890 this->rel_ = new Reloc_section(false);
891 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
892 elfcpp::SHF_ALLOC, this->rel_,
893 ORDER_DYNAMIC_PLT_RELOCS, false);
896 template<int size, bool big_endian>
898 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
903 // Add an entry to the PLT.
905 template<int size, bool big_endian>
907 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
909 gold_assert(!gsym->has_plt_offset());
910 unsigned int index = this->count_+ + 4;
911 section_offset_type plt_offset;
914 plt_offset = index * base_plt_entry_size;
918 gsym->set_plt_offset(plt_offset);
922 gsym->set_needs_dynsym_entry();
923 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
927 static const unsigned int addis_11_11 = 0x3d6b0000;
928 static const unsigned int addis_11_30 = 0x3d7e0000;
929 static const unsigned int addis_12_12 = 0x3d8c0000;
930 static const unsigned int addi_11_11 = 0x396b0000;
931 static const unsigned int add_0_11_11 = 0x7c0b5a14;
932 static const unsigned int add_11_0_11 = 0x7d605a14;
933 static const unsigned int b = 0x48000000;
934 static const unsigned int bcl_20_31 = 0x429f0005;
935 static const unsigned int bctr = 0x4e800420;
936 static const unsigned int lis_11 = 0x3d600000;
937 static const unsigned int lis_12 = 0x3d800000;
938 static const unsigned int lwzu_0_12 = 0x840c0000;
939 static const unsigned int lwz_0_12 = 0x800c0000;
940 static const unsigned int lwz_11_11 = 0x816b0000;
941 static const unsigned int lwz_11_30 = 0x817e0000;
942 static const unsigned int lwz_12_12 = 0x818c0000;
943 static const unsigned int mflr_0 = 0x7c0802a6;
944 static const unsigned int mflr_12 = 0x7d8802a6;
945 static const unsigned int mtctr_0 = 0x7c0903a6;
946 static const unsigned int mtctr_11 = 0x7d6903a6;
947 static const unsigned int mtlr_0 = 0x7c0803a6;
948 static const unsigned int nop = 0x60000000;
949 static const unsigned int sub_11_11_12 = 0x7d6c5850;
951 static const unsigned int addis_r12_r2 = 0x3d820000; /* addis %r12,%r2,xxx@ha */
952 static const unsigned int std_r2_40r1 = 0xf8410028; /* std %r2,40(%r1) */
953 static const unsigned int ld_r11_0r12 = 0xe96c0000; /* ld %r11,xxx+0@l(%r12) */
954 static const unsigned int ld_r2_0r12 = 0xe84c0000; /* ld %r2,xxx+8@l(%r12) */
955 /* ld %r11,xxx+16@l(%r12) */
958 // Write out the PLT.
960 template<int size, bool big_endian>
962 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
964 const off_t offset = this->offset();
965 const section_size_type oview_size =
966 convert_to_section_size_type(this->data_size());
967 unsigned char* const oview = of->get_output_view(offset, oview_size);
968 unsigned char* pov = oview;
970 memset(pov, 0, base_plt_entry_size * 4);
971 pov += base_plt_entry_size * 4;
973 unsigned int plt_offset = base_plt_entry_size * 4;
974 const unsigned int count = this->count_;
978 for (unsigned int i = 0; i < count; i++)
984 for (unsigned int i = 0; i < count; i++)
986 elfcpp::Swap<32, true>::writeval(pov + 0x00,
987 lwz_11_30 + plt_offset);
988 elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
989 elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
990 elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
991 pov += base_plt_entry_size;
992 plt_offset += base_plt_entry_size;
996 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
998 of->write_output_view(offset, oview_size, oview);
1001 // Create a PLT entry for a global symbol.
1003 template<int size, bool big_endian>
1005 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1009 if (gsym->has_plt_offset())
1012 if (this->plt_ == NULL)
1014 // Create the GOT section first.
1015 this->got_section(symtab, layout);
1017 // Ensure that .rela.dyn always appears before .rela.plt This is
1018 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1019 // needs to include .rela.plt in it's range.
1020 this->rela_dyn_section(layout);
1022 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
1023 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1025 | elfcpp::SHF_EXECINSTR
1026 | elfcpp::SHF_WRITE),
1027 this->plt_, ORDER_PLT, false);
1029 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1030 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1031 Symbol_table::PREDEFINED,
1033 0, 0, elfcpp::STT_OBJECT,
1035 elfcpp::STV_HIDDEN, 0,
1039 this->plt_->add_entry(gsym);
1042 // Return the number of entries in the PLT.
1044 template<int size, bool big_endian>
1046 Target_powerpc<size, big_endian>::plt_entry_count() const
1048 if (this->plt_ == NULL)
1050 return this->plt_->entry_count();
1053 // Return the offset of the first non-reserved PLT entry.
1055 template<int size, bool big_endian>
1057 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1059 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1062 // Return the size of each PLT entry.
1064 template<int size, bool big_endian>
1066 Target_powerpc<size, big_endian>::plt_entry_size() const
1068 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1071 // Create a GOT entry for the TLS module index.
1073 template<int size, bool big_endian>
1075 Target_powerpc<size, big_endian>::got_mod_index_entry(
1076 Symbol_table* symtab,
1078 Sized_relobj_file<size, big_endian>* object)
1080 if (this->got_mod_index_offset_ == -1U)
1082 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1083 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1084 Output_data_got<size, big_endian>* got;
1085 unsigned int got_offset;
1087 got = this->got_section(symtab, layout);
1088 got_offset = got->add_constant(0);
1089 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1091 got->add_constant(0);
1092 this->got_mod_index_offset_ = got_offset;
1094 return this->got_mod_index_offset_;
1097 // Optimize the TLS relocation type based on what we know about the
1098 // symbol. IS_FINAL is true if the final address of this symbol is
1099 // known at link time.
1101 static tls::Tls_optimization
1102 optimize_tls_reloc(bool /* is_final */, int r_type)
1104 // If we are generating a shared library, then we can't do anything
1106 if (parameters->options().shared())
1107 return tls::TLSOPT_NONE;
1116 // Get the Reference_flags for a particular relocation.
1118 template<int size, bool big_endian>
1120 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
1121 unsigned int r_type)
1125 case elfcpp::R_POWERPC_NONE:
1126 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1127 case elfcpp::R_POWERPC_GNU_VTENTRY:
1128 case elfcpp::R_PPC64_TOC:
1129 // No symbol reference.
1132 case elfcpp::R_POWERPC_ADDR16:
1133 case elfcpp::R_POWERPC_ADDR16_LO:
1134 case elfcpp::R_POWERPC_ADDR16_HI:
1135 case elfcpp::R_POWERPC_ADDR16_HA:
1136 case elfcpp::R_POWERPC_ADDR32:
1137 case elfcpp::R_PPC64_ADDR64:
1138 return Symbol::ABSOLUTE_REF;
1140 case elfcpp::R_POWERPC_REL24:
1141 case elfcpp::R_PPC_LOCAL24PC:
1142 case elfcpp::R_PPC_REL16:
1143 case elfcpp::R_PPC_REL16_LO:
1144 case elfcpp::R_PPC_REL16_HI:
1145 case elfcpp::R_PPC_REL16_HA:
1146 return Symbol::RELATIVE_REF;
1148 case elfcpp::R_PPC_PLTREL24:
1149 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1151 case elfcpp::R_POWERPC_GOT16:
1152 case elfcpp::R_POWERPC_GOT16_LO:
1153 case elfcpp::R_POWERPC_GOT16_HI:
1154 case elfcpp::R_POWERPC_GOT16_HA:
1155 case elfcpp::R_PPC64_TOC16:
1156 case elfcpp::R_PPC64_TOC16_LO:
1157 case elfcpp::R_PPC64_TOC16_HI:
1158 case elfcpp::R_PPC64_TOC16_HA:
1159 case elfcpp::R_PPC64_TOC16_DS:
1160 case elfcpp::R_PPC64_TOC16_LO_DS:
1162 return Symbol::ABSOLUTE_REF;
1164 case elfcpp::R_POWERPC_GOT_TPREL16:
1165 case elfcpp::R_POWERPC_TLS:
1166 return Symbol::TLS_REF;
1168 case elfcpp::R_POWERPC_COPY:
1169 case elfcpp::R_POWERPC_GLOB_DAT:
1170 case elfcpp::R_POWERPC_JMP_SLOT:
1171 case elfcpp::R_POWERPC_RELATIVE:
1172 case elfcpp::R_POWERPC_DTPMOD:
1174 // Not expected. We will give an error later.
1179 // Report an unsupported relocation against a local symbol.
1181 template<int size, bool big_endian>
1183 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1184 Sized_relobj_file<size, big_endian>* object,
1185 unsigned int r_type)
1187 gold_error(_("%s: unsupported reloc %u against local symbol"),
1188 object->name().c_str(), r_type);
1191 // We are about to emit a dynamic relocation of type R_TYPE. If the
1192 // dynamic linker does not support it, issue an error.
1194 template<int size, bool big_endian>
1196 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1197 unsigned int r_type)
1199 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1201 // These are the relocation types supported by glibc for both 32-bit
1202 // and 64-bit powerpc.
1205 case elfcpp::R_POWERPC_RELATIVE:
1206 case elfcpp::R_POWERPC_GLOB_DAT:
1207 case elfcpp::R_POWERPC_DTPMOD:
1208 case elfcpp::R_POWERPC_DTPREL:
1209 case elfcpp::R_POWERPC_TPREL:
1210 case elfcpp::R_POWERPC_JMP_SLOT:
1211 case elfcpp::R_POWERPC_COPY:
1212 case elfcpp::R_POWERPC_ADDR32:
1213 case elfcpp::R_POWERPC_ADDR24:
1214 case elfcpp::R_POWERPC_REL24:
1225 // These are the relocation types supported only on 64-bit.
1226 case elfcpp::R_PPC64_ADDR64:
1227 case elfcpp::R_PPC64_TPREL16_LO_DS:
1228 case elfcpp::R_PPC64_TPREL16_DS:
1229 case elfcpp::R_POWERPC_TPREL16:
1230 case elfcpp::R_POWERPC_TPREL16_LO:
1231 case elfcpp::R_POWERPC_TPREL16_HI:
1232 case elfcpp::R_POWERPC_TPREL16_HA:
1233 case elfcpp::R_PPC64_TPREL16_HIGHER:
1234 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1235 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1236 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1237 case elfcpp::R_PPC64_ADDR16_LO_DS:
1238 case elfcpp::R_POWERPC_ADDR16_LO:
1239 case elfcpp::R_POWERPC_ADDR16_HI:
1240 case elfcpp::R_POWERPC_ADDR16_HA:
1241 case elfcpp::R_POWERPC_ADDR30:
1242 case elfcpp::R_PPC64_UADDR64:
1243 case elfcpp::R_POWERPC_UADDR32:
1244 case elfcpp::R_POWERPC_ADDR16:
1245 case elfcpp::R_POWERPC_UADDR16:
1246 case elfcpp::R_PPC64_ADDR16_DS:
1247 case elfcpp::R_PPC64_ADDR16_HIGHER:
1248 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1249 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1250 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1251 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1252 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1253 case elfcpp::R_POWERPC_REL32:
1254 case elfcpp::R_PPC64_REL64:
1265 // These are the relocation types supported only on 32-bit.
1272 // This prevents us from issuing more than one error per reloc
1273 // section. But we can still wind up issuing more than one
1274 // error per object file.
1275 if (this->issued_non_pic_error_)
1277 gold_assert(parameters->options().output_is_position_independent());
1278 object->error(_("requires unsupported dynamic reloc; "
1279 "recompile with -fPIC"));
1280 this->issued_non_pic_error_ = true;
1284 // Scan a relocation for a local symbol.
1286 template<int size, bool big_endian>
1288 Target_powerpc<size, big_endian>::Scan::local(
1289 Symbol_table* symtab,
1291 Target_powerpc<size, big_endian>* target,
1292 Sized_relobj_file<size, big_endian>* object,
1293 unsigned int data_shndx,
1294 Output_section* output_section,
1295 const elfcpp::Rela<size, big_endian>& reloc,
1296 unsigned int r_type,
1297 const elfcpp::Sym<size, big_endian>& lsym)
1301 case elfcpp::R_POWERPC_NONE:
1302 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1303 case elfcpp::R_POWERPC_GNU_VTENTRY:
1306 case elfcpp::R_PPC64_ADDR64:
1307 case elfcpp::R_POWERPC_ADDR32:
1308 case elfcpp::R_POWERPC_ADDR16_HA:
1309 case elfcpp::R_POWERPC_ADDR16_LO:
1310 // If building a shared library (or a position-independent
1311 // executable), we need to create a dynamic relocation for
1313 if (parameters->options().output_is_position_independent())
1315 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1317 check_non_pic(object, r_type);
1318 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1320 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1321 rela_dyn->add_local(object, r_sym, r_type, output_section,
1322 data_shndx, reloc.get_r_offset(),
1323 reloc.get_r_addend());
1327 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1328 gold_assert(lsym.get_st_value() == 0);
1329 rela_dyn->add_local_relative(object, r_sym, r_type,
1330 output_section, data_shndx,
1331 reloc.get_r_offset(),
1332 reloc.get_r_addend());
1337 case elfcpp::R_POWERPC_REL24:
1338 case elfcpp::R_PPC_LOCAL24PC:
1339 case elfcpp::R_POWERPC_REL32:
1340 case elfcpp::R_PPC_REL16_LO:
1341 case elfcpp::R_PPC_REL16_HA:
1344 case elfcpp::R_POWERPC_GOT16:
1345 case elfcpp::R_POWERPC_GOT16_LO:
1346 case elfcpp::R_POWERPC_GOT16_HI:
1347 case elfcpp::R_POWERPC_GOT16_HA:
1348 case elfcpp::R_PPC64_TOC16:
1349 case elfcpp::R_PPC64_TOC16_LO:
1350 case elfcpp::R_PPC64_TOC16_HI:
1351 case elfcpp::R_PPC64_TOC16_HA:
1352 case elfcpp::R_PPC64_TOC16_DS:
1353 case elfcpp::R_PPC64_TOC16_LO_DS:
1355 // The symbol requires a GOT entry.
1356 Output_data_got<size, big_endian>* got;
1359 got = target->got_section(symtab, layout);
1360 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1362 // If we are generating a shared object, we need to add a
1363 // dynamic relocation for this symbol's GOT entry.
1364 if (parameters->options().output_is_position_independent())
1366 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1368 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1371 off = got->add_constant(0);
1372 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1373 rela_dyn->add_local_relative(object, r_sym,
1374 elfcpp::R_POWERPC_RELATIVE,
1379 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1383 case elfcpp::R_PPC64_TOC:
1384 // We need a GOT section.
1385 target->got_section(symtab, layout);
1388 // These are relocations which should only be seen by the
1389 // dynamic linker, and should never be seen here.
1390 case elfcpp::R_POWERPC_COPY:
1391 case elfcpp::R_POWERPC_GLOB_DAT:
1392 case elfcpp::R_POWERPC_JMP_SLOT:
1393 case elfcpp::R_POWERPC_RELATIVE:
1394 case elfcpp::R_POWERPC_DTPMOD:
1395 gold_error(_("%s: unexpected reloc %u in object file"),
1396 object->name().c_str(), r_type);
1400 unsupported_reloc_local(object, r_type);
1405 // Report an unsupported relocation against a global symbol.
1407 template<int size, bool big_endian>
1409 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1410 Sized_relobj_file<size, big_endian>* object,
1411 unsigned int r_type,
1414 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1415 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1418 // Scan a relocation for a global symbol.
1420 template<int size, bool big_endian>
1422 Target_powerpc<size, big_endian>::Scan::global(
1423 Symbol_table* symtab,
1425 Target_powerpc<size, big_endian>* target,
1426 Sized_relobj_file<size, big_endian>* object,
1427 unsigned int data_shndx,
1428 Output_section* output_section,
1429 const elfcpp::Rela<size, big_endian>& reloc,
1430 unsigned int r_type,
1435 case elfcpp::R_POWERPC_NONE:
1436 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1437 case elfcpp::R_POWERPC_GNU_VTENTRY:
1440 case elfcpp::R_PPC_PLTREL24:
1441 // If the symbol is fully resolved, this is just a PC32 reloc.
1442 // Otherwise we need a PLT entry.
1443 if (gsym->final_value_is_known())
1445 // If building a shared library, we can also skip the PLT entry
1446 // if the symbol is defined in the output file and is protected
1448 if (gsym->is_defined()
1449 && !gsym->is_from_dynobj()
1450 && !gsym->is_preemptible())
1452 target->make_plt_entry(symtab, layout, gsym);
1455 case elfcpp::R_POWERPC_ADDR16:
1456 case elfcpp::R_POWERPC_ADDR16_LO:
1457 case elfcpp::R_POWERPC_ADDR16_HI:
1458 case elfcpp::R_POWERPC_ADDR16_HA:
1459 case elfcpp::R_POWERPC_ADDR32:
1460 case elfcpp::R_PPC64_ADDR64:
1462 // Make a PLT entry if necessary.
1463 if (gsym->needs_plt_entry())
1465 target->make_plt_entry(symtab, layout, gsym);
1466 // Since this is not a PC-relative relocation, we may be
1467 // taking the address of a function. In that case we need to
1468 // set the entry in the dynamic symbol table to the address of
1470 if (gsym->is_from_dynobj() && !parameters->options().shared())
1471 gsym->set_needs_dynsym_value();
1473 // Make a dynamic relocation if necessary.
1474 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1476 if (gsym->may_need_copy_reloc())
1478 target->copy_reloc(symtab, layout, object,
1479 data_shndx, output_section, gsym, reloc);
1481 else if ((r_type == elfcpp::R_POWERPC_ADDR32
1482 || r_type == elfcpp::R_PPC64_ADDR64)
1483 && gsym->can_use_relative_reloc(false))
1485 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1486 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1487 output_section, object,
1488 data_shndx, reloc.get_r_offset(),
1489 reloc.get_r_addend());
1493 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1495 check_non_pic(object, r_type);
1496 if (gsym->is_from_dynobj()
1497 || gsym->is_undefined()
1498 || gsym->is_preemptible())
1499 rela_dyn->add_global(gsym, r_type, output_section,
1501 reloc.get_r_offset(),
1502 reloc.get_r_addend());
1504 rela_dyn->add_global_relative(gsym, r_type,
1505 output_section, object,
1507 reloc.get_r_offset(),
1508 reloc.get_r_addend());
1514 case elfcpp::R_POWERPC_REL24:
1515 case elfcpp::R_PPC_LOCAL24PC:
1516 case elfcpp::R_PPC_REL16:
1517 case elfcpp::R_PPC_REL16_LO:
1518 case elfcpp::R_PPC_REL16_HI:
1519 case elfcpp::R_PPC_REL16_HA:
1521 if (gsym->needs_plt_entry())
1522 target->make_plt_entry(symtab, layout, gsym);
1523 // Make a dynamic relocation if necessary.
1524 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1526 if (gsym->may_need_copy_reloc())
1528 target->copy_reloc(symtab, layout, object,
1529 data_shndx, output_section, gsym,
1534 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1535 check_non_pic(object, r_type);
1536 rela_dyn->add_global(gsym, r_type, output_section, object,
1537 data_shndx, reloc.get_r_offset(),
1538 reloc.get_r_addend());
1544 case elfcpp::R_POWERPC_GOT16:
1545 case elfcpp::R_POWERPC_GOT16_LO:
1546 case elfcpp::R_POWERPC_GOT16_HI:
1547 case elfcpp::R_POWERPC_GOT16_HA:
1548 case elfcpp::R_PPC64_TOC16:
1549 case elfcpp::R_PPC64_TOC16_LO:
1550 case elfcpp::R_PPC64_TOC16_HI:
1551 case elfcpp::R_PPC64_TOC16_HA:
1552 case elfcpp::R_PPC64_TOC16_DS:
1553 case elfcpp::R_PPC64_TOC16_LO_DS:
1555 // The symbol requires a GOT entry.
1556 Output_data_got<size, big_endian>* got;
1558 got = target->got_section(symtab, layout);
1559 if (gsym->final_value_is_known())
1560 got->add_global(gsym, GOT_TYPE_STANDARD);
1563 // If this symbol is not fully resolved, we need to add a
1564 // dynamic relocation for it.
1565 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1566 if (gsym->is_from_dynobj()
1567 || gsym->is_undefined()
1568 || gsym->is_preemptible())
1569 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1570 elfcpp::R_POWERPC_GLOB_DAT);
1571 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1573 unsigned int off = got->add_constant(0);
1575 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1576 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1583 case elfcpp::R_PPC64_TOC:
1584 // We need a GOT section.
1585 target->got_section(symtab, layout);
1588 case elfcpp::R_POWERPC_GOT_TPREL16:
1589 case elfcpp::R_POWERPC_TLS:
1593 // These are relocations which should only be seen by the
1594 // dynamic linker, and should never be seen here.
1595 case elfcpp::R_POWERPC_COPY:
1596 case elfcpp::R_POWERPC_GLOB_DAT:
1597 case elfcpp::R_POWERPC_JMP_SLOT:
1598 case elfcpp::R_POWERPC_RELATIVE:
1599 case elfcpp::R_POWERPC_DTPMOD:
1600 gold_error(_("%s: unexpected reloc %u in object file"),
1601 object->name().c_str(), r_type);
1605 unsupported_reloc_global(object, r_type, gsym);
1610 // Process relocations for gc.
1612 template<int size, bool big_endian>
1614 Target_powerpc<size, big_endian>::gc_process_relocs(
1615 Symbol_table* symtab,
1617 Sized_relobj_file<size, big_endian>* object,
1618 unsigned int data_shndx,
1620 const unsigned char* prelocs,
1622 Output_section* output_section,
1623 bool needs_special_offset_handling,
1624 size_t local_symbol_count,
1625 const unsigned char* plocal_symbols)
1627 typedef Target_powerpc<size, big_endian> Powerpc;
1628 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1630 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
1631 typename Target_powerpc::Relocatable_size_for_reloc>(
1640 needs_special_offset_handling,
1645 // Scan relocations for a section.
1647 template<int size, bool big_endian>
1649 Target_powerpc<size, big_endian>::scan_relocs(
1650 Symbol_table* symtab,
1652 Sized_relobj_file<size, big_endian>* object,
1653 unsigned int data_shndx,
1654 unsigned int sh_type,
1655 const unsigned char* prelocs,
1657 Output_section* output_section,
1658 bool needs_special_offset_handling,
1659 size_t local_symbol_count,
1660 const unsigned char* plocal_symbols)
1662 typedef Target_powerpc<size, big_endian> Powerpc;
1663 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1664 static Output_data_space* sdata;
1666 if (sh_type == elfcpp::SHT_REL)
1668 gold_error(_("%s: unsupported REL reloc section"),
1669 object->name().c_str());
1673 // Define _SDA_BASE_ at the start of the .sdata section.
1676 // layout->find_output_section(".sdata") == NULL
1677 sdata = new Output_data_space(4, "** sdata");
1678 Output_section* os = layout->add_output_section_data(".sdata", 0,
1680 | elfcpp::SHF_WRITE,
1684 symtab->define_in_output_data("_SDA_BASE_", NULL,
1685 Symbol_table::PREDEFINED,
1690 elfcpp::STV_HIDDEN, 0,
1694 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1703 needs_special_offset_handling,
1708 // Finalize the sections.
1710 template<int size, bool big_endian>
1712 Target_powerpc<size, big_endian>::do_finalize_sections(
1714 const Input_objects*,
1717 // Fill in some more dynamic tags.
1718 const Reloc_section* rel_plt = (this->plt_ == NULL
1720 : this->plt_->rel_plt());
1721 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
1722 this->rela_dyn_, true, size == 32);
1724 // Emit any relocs we saved in an attempt to avoid generating COPY
1726 if (this->copy_relocs_.any_saved_relocs())
1727 this->copy_relocs_.emit(this->rela_dyn_section(layout));
1730 // Perform a relocation.
1732 template<int size, bool big_endian>
1734 Target_powerpc<size, big_endian>::Relocate::relocate(
1735 const Relocate_info<size, big_endian>* relinfo,
1736 Target_powerpc* target,
1739 const elfcpp::Rela<size, big_endian>& rela,
1740 unsigned int r_type,
1741 const Sized_symbol<size>* gsym,
1742 const Symbol_value<size>* psymval,
1743 unsigned char* view,
1744 typename elfcpp::Elf_types<size>::Elf_Addr address,
1745 section_size_type /* view_size */)
1747 const unsigned int toc_base_offset = 0x8000;
1748 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1750 // Pick the value to use for symbols defined in shared objects.
1751 Symbol_value<size> symval;
1753 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
1755 elfcpp::Elf_Xword value;
1757 value = target->plt_section()->address() + gsym->plt_offset();
1759 symval.set_output_value(value);
1764 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1765 elfcpp::Elf_Xword addend = rela.get_r_addend();
1767 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
1768 // pointer points to the beginning, not the end, of the table.
1769 // So we just use the plain offset.
1770 unsigned int got_offset = 0;
1771 unsigned int got2_offset = 0;
1774 case elfcpp::R_PPC64_TOC16:
1775 case elfcpp::R_PPC64_TOC16_LO:
1776 case elfcpp::R_PPC64_TOC16_HI:
1777 case elfcpp::R_PPC64_TOC16_HA:
1778 case elfcpp::R_PPC64_TOC16_DS:
1779 case elfcpp::R_PPC64_TOC16_LO_DS:
1780 // Subtract the TOC base address.
1781 addend -= target->toc_section()->address() + toc_base_offset;
1784 case elfcpp::R_POWERPC_GOT16:
1785 case elfcpp::R_POWERPC_GOT16_LO:
1786 case elfcpp::R_POWERPC_GOT16_HI:
1787 case elfcpp::R_POWERPC_GOT16_HA:
1788 case elfcpp::R_PPC64_GOT16_DS:
1789 case elfcpp::R_PPC64_GOT16_LO_DS:
1792 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1793 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1797 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1798 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1799 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1803 // R_PPC_PLTREL24 is rather special. If non-zero,
1804 // the addend specifies the GOT pointer offset within .got2.
1805 case elfcpp::R_PPC_PLTREL24:
1806 if (addend >= 32768)
1808 Output_data_space* got2;
1809 got2 = target->got2_section();
1810 got2_offset = got2->offset();
1811 addend += got2_offset;
1821 case elfcpp::R_POWERPC_NONE:
1822 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1823 case elfcpp::R_POWERPC_GNU_VTENTRY:
1826 case elfcpp::R_POWERPC_REL32:
1827 Reloc::rel32(view, object, psymval, addend, address);
1830 case elfcpp::R_POWERPC_REL24:
1831 Reloc::rel24(view, object, psymval, addend, address);
1834 case elfcpp::R_POWERPC_REL14:
1835 Reloc::rel14(view, object, psymval, addend, address);
1838 case elfcpp::R_PPC_PLTREL24:
1839 Reloc::rel24(view, object, psymval, addend, address);
1842 case elfcpp::R_PPC_LOCAL24PC:
1843 Reloc::rel24(view, object, psymval, addend, address);
1846 case elfcpp::R_PPC64_ADDR64:
1847 if (!parameters->options().output_is_position_independent())
1848 Relocate_functions<size, big_endian>::rela64(view, object,
1852 case elfcpp::R_POWERPC_ADDR32:
1853 if (!parameters->options().output_is_position_independent())
1854 Relocate_functions<size, big_endian>::rela32(view, object,
1858 case elfcpp::R_POWERPC_ADDR16_LO:
1859 Reloc::addr16_lo(view, object, psymval, addend);
1862 case elfcpp::R_POWERPC_ADDR16_HI:
1863 Reloc::addr16_hi(view, object, psymval, addend);
1866 case elfcpp::R_POWERPC_ADDR16_HA:
1867 Reloc::addr16_ha(view, object, psymval, addend);
1870 case elfcpp::R_PPC_REL16_LO:
1871 Reloc::rel16_lo(view, object, psymval, addend, address);
1874 case elfcpp::R_PPC_REL16_HI:
1875 Reloc::rel16_lo(view, object, psymval, addend, address);
1878 case elfcpp::R_PPC_REL16_HA:
1879 Reloc::rel16_ha(view, object, psymval, addend, address);
1882 case elfcpp::R_POWERPC_GOT16:
1883 Reloc::addr16(view, got_offset, addend);
1886 case elfcpp::R_POWERPC_GOT16_LO:
1887 Reloc::addr16_lo(view, got_offset, addend);
1890 case elfcpp::R_POWERPC_GOT16_HI:
1891 Reloc::addr16_hi(view, got_offset, addend);
1894 case elfcpp::R_POWERPC_GOT16_HA:
1895 Reloc::addr16_ha(view, got_offset, addend);
1898 case elfcpp::R_PPC64_TOC16:
1899 Reloc::addr16(view, got_offset, addend);
1902 case elfcpp::R_PPC64_TOC16_LO:
1903 Reloc::addr16_lo(view, got_offset, addend);
1906 case elfcpp::R_PPC64_TOC16_HI:
1907 Reloc::addr16_hi(view, got_offset, addend);
1910 case elfcpp::R_PPC64_TOC16_HA:
1911 Reloc::addr16_ha(view, got_offset, addend);
1914 case elfcpp::R_PPC64_TOC16_DS:
1915 case elfcpp::R_PPC64_TOC16_LO_DS:
1916 Reloc::addr16_ds(view, got_offset, addend);
1919 case elfcpp::R_PPC64_TOC:
1921 elfcpp::Elf_types<64>::Elf_Addr value;
1922 value = target->toc_section()->address() + toc_base_offset;
1923 Relocate_functions<64, false>::rela64(view, value, addend);
1927 case elfcpp::R_POWERPC_COPY:
1928 case elfcpp::R_POWERPC_GLOB_DAT:
1929 case elfcpp::R_POWERPC_JMP_SLOT:
1930 case elfcpp::R_POWERPC_RELATIVE:
1931 // This is an outstanding tls reloc, which is unexpected when
1933 case elfcpp::R_POWERPC_DTPMOD:
1934 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1935 _("unexpected reloc %u in object file"),
1940 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1941 _("unsupported reloc %u"),
1949 // Perform a TLS relocation.
1951 template<int size, bool big_endian>
1953 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1954 const Relocate_info<size, big_endian>* relinfo,
1955 Target_powerpc<size, big_endian>* target,
1957 const elfcpp::Rela<size, big_endian>& rela,
1958 unsigned int r_type,
1959 const Sized_symbol<size>* gsym,
1960 const Symbol_value<size>* psymval,
1961 unsigned char* view,
1962 typename elfcpp::Elf_types<size>::Elf_Addr address,
1965 Output_segment* tls_segment = relinfo->layout->tls_segment();
1966 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1967 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1969 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1970 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1972 const bool is_final =
1974 ? !parameters->options().output_is_position_independent()
1975 : gsym->final_value_is_known());
1976 const tls::Tls_optimization optimized_type
1977 = optimize_tls_reloc(is_final, r_type);
1985 // Relocate section data.
1987 template<int size, bool big_endian>
1989 Target_powerpc<size, big_endian>::relocate_section(
1990 const Relocate_info<size, big_endian>* relinfo,
1991 unsigned int sh_type,
1992 const unsigned char* prelocs,
1994 Output_section* output_section,
1995 bool needs_special_offset_handling,
1996 unsigned char* view,
1997 typename elfcpp::Elf_types<size>::Elf_Addr address,
1998 section_size_type view_size,
1999 const Reloc_symbol_changes* reloc_symbol_changes)
2001 typedef Target_powerpc<size, big_endian> Powerpc;
2002 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2004 gold_assert(sh_type == elfcpp::SHT_RELA);
2006 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2013 needs_special_offset_handling,
2017 reloc_symbol_changes);
2020 // Return the size of a relocation while scanning during a relocatable
2023 template<int size, bool big_endian>
2025 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2029 // We are always SHT_RELA, so we should never get here.
2034 // Scan the relocs during a relocatable link.
2036 template<int size, bool big_endian>
2038 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2039 Symbol_table* symtab,
2041 Sized_relobj_file<size, big_endian>* object,
2042 unsigned int data_shndx,
2043 unsigned int sh_type,
2044 const unsigned char* prelocs,
2046 Output_section* output_section,
2047 bool needs_special_offset_handling,
2048 size_t local_symbol_count,
2049 const unsigned char* plocal_symbols,
2050 Relocatable_relocs* rr)
2052 gold_assert(sh_type == elfcpp::SHT_RELA);
2054 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2055 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2057 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2058 Scan_relocatable_relocs>(
2066 needs_special_offset_handling,
2072 // Relocate a section during a relocatable link.
2074 template<int size, bool big_endian>
2076 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2077 const Relocate_info<size, big_endian>* relinfo,
2078 unsigned int sh_type,
2079 const unsigned char* prelocs,
2081 Output_section* output_section,
2082 off_t offset_in_output_section,
2083 const Relocatable_relocs* rr,
2084 unsigned char* view,
2085 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2086 section_size_type view_size,
2087 unsigned char* reloc_view,
2088 section_size_type reloc_view_size)
2090 gold_assert(sh_type == elfcpp::SHT_RELA);
2092 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
2097 offset_in_output_section,
2106 // Return the value to use for a dynamic which requires special
2107 // treatment. This is how we support equality comparisons of function
2108 // pointers across shared library boundaries, as described in the
2109 // processor specific ABI supplement.
2111 template<int size, bool big_endian>
2113 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2115 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2116 return this->plt_section()->address() + gsym->plt_offset();
2119 // The selector for powerpc object files.
2121 template<int size, bool big_endian>
2122 class Target_selector_powerpc : public Target_selector
2125 Target_selector_powerpc()
2126 : Target_selector(elfcpp::EM_NONE, size, big_endian,
2128 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2129 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2131 ? (big_endian ? "elf64ppc" : "elf64lppc")
2132 : (big_endian ? "elf32ppc" : "elf32lppc")))
2135 Target* do_recognize(int machine, int, int)
2140 if (machine != elfcpp::EM_PPC64)
2145 if (machine != elfcpp::EM_PPC)
2153 return this->instantiate_target();
2156 Target* do_instantiate_target()
2157 { return new Target_powerpc<size, big_endian>(); }
2160 Target_selector_powerpc<32, true> target_selector_ppc32;
2161 Target_selector_powerpc<32, false> target_selector_ppc32le;
2162 Target_selector_powerpc<64, true> target_selector_ppc64;
2163 Target_selector_powerpc<64, false> target_selector_ppc64le;
2165 } // End anonymous namespace.