1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
27 #include "parameters.h"
34 #include "copy-relocs.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
47 template<int size, bool big_endian>
48 class Output_data_plt_powerpc;
50 template<int size, bool big_endian>
51 class Output_data_got_powerpc;
53 template<int size, bool big_endian>
54 class Output_data_glink;
56 template<int size, bool big_endian>
57 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
60 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
61 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
62 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
73 return this->got2_section_;
79 do_find_special_sections(Read_symbols_data* sd);
82 unsigned int got2_section_;
85 template<int size, bool big_endian>
86 class Target_powerpc : public Sized_target<size, big_endian>
90 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
93 : Sized_target<size, big_endian>(&powerpc_info),
94 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
95 copy_relocs_(elfcpp::R_POWERPC_COPY),
96 dynbss_(NULL), got_mod_index_offset_(-1U)
100 // Process the relocations to determine unreferenced sections for
101 // garbage collection.
103 gc_process_relocs(Symbol_table* symtab,
105 Sized_relobj_file<size, big_endian>* object,
106 unsigned int data_shndx,
107 unsigned int sh_type,
108 const unsigned char* prelocs,
110 Output_section* output_section,
111 bool needs_special_offset_handling,
112 size_t local_symbol_count,
113 const unsigned char* plocal_symbols);
115 // Scan the relocations to look for symbol adjustments.
117 scan_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 // Finalize the sections.
130 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
132 // Return the value to use for a dynamic which requires special
135 do_dynsym_value(const Symbol*) const;
137 // Relocate a section.
139 relocate_section(const Relocate_info<size, big_endian>*,
140 unsigned int sh_type,
141 const unsigned char* prelocs,
143 Output_section* output_section,
144 bool needs_special_offset_handling,
146 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
147 section_size_type view_size,
148 const Reloc_symbol_changes*);
150 // Scan the relocs during a relocatable link.
152 scan_relocatable_relocs(Symbol_table* symtab,
154 Sized_relobj_file<size, big_endian>* object,
155 unsigned int data_shndx,
156 unsigned int sh_type,
157 const unsigned char* prelocs,
159 Output_section* output_section,
160 bool needs_special_offset_handling,
161 size_t local_symbol_count,
162 const unsigned char* plocal_symbols,
163 Relocatable_relocs*);
165 // Relocate a section during a relocatable link.
167 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
168 unsigned int sh_type,
169 const unsigned char* prelocs,
171 Output_section* output_section,
172 off_t offset_in_output_section,
173 const Relocatable_relocs*,
175 typename elfcpp::Elf_types<size>::Elf_Addr,
177 unsigned char* reloc_view,
178 section_size_type reloc_view_size);
180 // Return whether SYM is defined by the ABI.
182 do_is_defined_by_abi(const Symbol* sym) const
184 return strcmp(sym->name(), "__tls_get_addr") == 0;
187 // Return the size of the GOT section.
191 gold_assert(this->got_ != NULL);
192 return this->got_->data_size();
195 // Get the PLT section.
196 const Output_data_plt_powerpc<size, big_endian>*
199 gold_assert(this->plt_ != NULL);
203 // Get the .glink section.
204 const Output_data_glink<size, big_endian>*
205 glink_section() const
207 gold_assert(this->glink_ != NULL);
211 // Get the GOT section.
212 const Output_data_got_powerpc<size, big_endian>*
215 gold_assert(this->got_ != NULL);
221 do_make_elf_object(const std::string&, Input_file*, off_t,
222 const elfcpp::Ehdr<size, big_endian>&);
224 // Return the number of entries in the GOT.
226 got_entry_count() const
228 if (this->got_ == NULL)
230 return this->got_size() / (size / 8);
233 // Return the number of entries in the PLT.
235 plt_entry_count() const;
237 // Return the offset of the first non-reserved PLT entry.
239 first_plt_entry_offset() const;
241 // Return the size of each PLT entry.
243 plt_entry_size() const;
247 // The class which scans relocations.
252 : issued_non_pic_error_(false)
256 get_reference_flags(unsigned int r_type);
259 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
260 Sized_relobj_file<size, big_endian>* object,
261 unsigned int data_shndx,
262 Output_section* output_section,
263 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
264 const elfcpp::Sym<size, big_endian>& lsym);
267 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
268 Sized_relobj_file<size, big_endian>* object,
269 unsigned int data_shndx,
270 Output_section* output_section,
271 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
275 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
277 Sized_relobj_file<size, big_endian>* ,
280 const elfcpp::Rela<size, big_endian>& ,
282 const elfcpp::Sym<size, big_endian>&)
286 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
288 Sized_relobj_file<size, big_endian>* ,
291 const elfcpp::Rela<size,
293 unsigned int , Symbol*)
298 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
299 unsigned int r_type);
302 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
303 unsigned int r_type, Symbol*);
306 generate_tls_call(Symbol_table* symtab, Layout* layout,
307 Target_powerpc* target);
310 check_non_pic(Relobj*, unsigned int r_type);
312 // Whether we have issued an error about a non-PIC compilation.
313 bool issued_non_pic_error_;
316 // The class which implements relocation.
320 // Do a relocation. Return false if the caller should not issue
321 // any warnings about this relocation.
323 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
324 Output_section*, size_t relnum,
325 const elfcpp::Rela<size, big_endian>&,
326 unsigned int r_type, const Sized_symbol<size>*,
327 const Symbol_value<size>*,
329 typename elfcpp::Elf_types<size>::Elf_Addr,
333 // Do a TLS relocation.
335 relocate_tls(const Relocate_info<size, big_endian>*,
336 Target_powerpc* target,
337 size_t relnum, const elfcpp::Rela<size, big_endian>&,
338 unsigned int r_type, const Sized_symbol<size>*,
339 const Symbol_value<size>*,
341 typename elfcpp::Elf_types<size>::Elf_Addr,
345 // A class which returns the size required for a relocation type,
346 // used while scanning relocs during a relocatable link.
347 class Relocatable_size_for_reloc
351 get_size_for_reloc(unsigned int, Relobj*)
358 // Adjust TLS relocation type based on the options and whether this
359 // is a local symbol.
360 static tls::Tls_optimization
361 optimize_tls_reloc(bool is_final, int r_type);
363 // Get the GOT section, creating it if necessary.
364 Output_data_got_powerpc<size, big_endian>*
365 got_section(Symbol_table*, Layout*);
369 make_glink_section(Layout*);
371 // Create the PLT section.
373 make_plt_section(Layout*);
375 // Create a PLT entry for a global symbol.
377 make_plt_entry(Layout*, Symbol*,
378 const elfcpp::Rela<size, big_endian>&,
379 const Sized_relobj<size, big_endian>* object);
381 // Create a GOT entry for the TLS module index.
383 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
384 Sized_relobj_file<size, big_endian>* object);
386 // Get the dynamic reloc section, creating it if necessary.
388 rela_dyn_section(Layout*);
390 // Copy a relocation against a global symbol.
392 copy_reloc(Symbol_table* symtab, Layout* layout,
393 Sized_relobj_file<size, big_endian>* object,
394 unsigned int shndx, Output_section* output_section,
395 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
397 this->copy_relocs_.copy_reloc(symtab, layout,
398 symtab->get_sized_symbol<size>(sym),
399 object, shndx, output_section,
400 reloc, this->rela_dyn_section(layout));
403 // Information about this specific target which we pass to the
404 // general Target structure.
405 static Target::Target_info powerpc_info;
407 // The types of GOT entries needed for this platform.
408 // These values are exposed to the ABI in an incremental link.
409 // Do not renumber existing values without changing the version
410 // number of the .gnu_incremental_inputs section.
413 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
414 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
415 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
418 // The GOT output section.
419 Output_data_got_powerpc<size, big_endian>* got_;
420 // The PLT output section.
421 Output_data_plt_powerpc<size, big_endian>* plt_;
422 // The .glink output section.
423 Output_data_glink<size, big_endian>* glink_;
424 // The dynamic reloc output section.
425 Reloc_section* rela_dyn_;
426 // Relocs saved to avoid a COPY reloc.
427 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
428 // Space for variables copied with a COPY reloc.
429 Output_data_space* dynbss_;
430 // Offset of the GOT entry for the TLS module index;
431 unsigned int got_mod_index_offset_;
435 Target::Target_info Target_powerpc<32, true>::powerpc_info =
438 true, // is_big_endian
439 elfcpp::EM_PPC, // machine_code
440 false, // has_make_symbol
441 false, // has_resolve
442 false, // has_code_fill
443 true, // is_default_stack_executable
444 false, // can_icf_inline_merge_sections
446 "/usr/lib/ld.so.1", // dynamic_linker
447 0x10000000, // default_text_segment_address
448 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
449 4 * 1024, // common_pagesize (overridable by -z common-page-size)
450 false, // isolate_execinstr
452 elfcpp::SHN_UNDEF, // small_common_shndx
453 elfcpp::SHN_UNDEF, // large_common_shndx
454 0, // small_common_section_flags
455 0, // large_common_section_flags
456 NULL, // attributes_section
457 NULL // attributes_vendor
461 Target::Target_info Target_powerpc<32, false>::powerpc_info =
464 false, // is_big_endian
465 elfcpp::EM_PPC, // machine_code
466 false, // has_make_symbol
467 false, // has_resolve
468 false, // has_code_fill
469 true, // is_default_stack_executable
470 false, // can_icf_inline_merge_sections
472 "/usr/lib/ld.so.1", // dynamic_linker
473 0x10000000, // default_text_segment_address
474 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
475 4 * 1024, // common_pagesize (overridable by -z common-page-size)
476 false, // isolate_execinstr
478 elfcpp::SHN_UNDEF, // small_common_shndx
479 elfcpp::SHN_UNDEF, // large_common_shndx
480 0, // small_common_section_flags
481 0, // large_common_section_flags
482 NULL, // attributes_section
483 NULL // attributes_vendor
487 Target::Target_info Target_powerpc<64, true>::powerpc_info =
490 true, // is_big_endian
491 elfcpp::EM_PPC64, // machine_code
492 false, // has_make_symbol
493 false, // has_resolve
494 false, // has_code_fill
495 true, // is_default_stack_executable
496 false, // can_icf_inline_merge_sections
498 "/usr/lib/ld.so.1", // dynamic_linker
499 0x10000000, // default_text_segment_address
500 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
501 8 * 1024, // common_pagesize (overridable by -z common-page-size)
502 false, // isolate_execinstr
504 elfcpp::SHN_UNDEF, // small_common_shndx
505 elfcpp::SHN_UNDEF, // large_common_shndx
506 0, // small_common_section_flags
507 0, // large_common_section_flags
508 NULL, // attributes_section
509 NULL // attributes_vendor
513 Target::Target_info Target_powerpc<64, false>::powerpc_info =
516 false, // is_big_endian
517 elfcpp::EM_PPC64, // machine_code
518 false, // has_make_symbol
519 false, // has_resolve
520 false, // has_code_fill
521 true, // is_default_stack_executable
522 false, // can_icf_inline_merge_sections
524 "/usr/lib/ld.so.1", // dynamic_linker
525 0x10000000, // default_text_segment_address
526 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
527 8 * 1024, // common_pagesize (overridable by -z common-page-size)
528 false, // isolate_execinstr
530 elfcpp::SHN_UNDEF, // small_common_shndx
531 elfcpp::SHN_UNDEF, // large_common_shndx
532 0, // small_common_section_flags
533 0, // large_common_section_flags
534 NULL, // attributes_section
535 NULL // attributes_vendor
538 template<int size, bool big_endian>
539 class Powerpc_relocate_functions
542 // Do a simple RELA relocation
543 template<int valsize>
545 rela(unsigned char* view,
546 unsigned int right_shift,
547 elfcpp::Elf_Xword dst_mask,
548 typename elfcpp::Swap<size, big_endian>::Valtype value,
549 typename elfcpp::Swap<size, big_endian>::Valtype addend)
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 = (value + addend) >> right_shift;
559 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
562 // Do a simple RELA relocation, unaligned.
563 template<int valsize>
565 rela_ua(unsigned char* view,
566 unsigned int right_shift,
567 elfcpp::Elf_Xword dst_mask,
568 typename elfcpp::Swap<size, big_endian>::Valtype value,
569 typename elfcpp::Swap<size, big_endian>::Valtype addend)
571 typedef typename elfcpp::Swap_unaligned<valsize,
572 big_endian>::Valtype Valtype;
573 Valtype* wv = reinterpret_cast<Valtype*>(view);
574 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
575 Valtype reloc = (value + addend) >> right_shift;
580 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
583 typedef Powerpc_relocate_functions<size, big_endian> This;
584 typedef Relocate_functions<size, big_endian> This_reloc;
586 // R_POWERPC_REL32: (Symbol + Addend - Address)
588 rel32(unsigned char* view,
589 typename elfcpp::Elf_types<size>::Elf_Addr value,
590 typename elfcpp::Elf_types<size>::Elf_Addr addend,
591 typename elfcpp::Elf_types<size>::Elf_Addr address)
592 { This_reloc::pcrela32(view, value, addend, address); }
594 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
596 rel24(unsigned char* view,
597 typename elfcpp::Elf_types<size>::Elf_Addr value,
598 typename elfcpp::Elf_types<size>::Elf_Addr addend,
599 typename elfcpp::Elf_types<size>::Elf_Addr address)
601 This::template rela<32>(view, 0, 0x03fffffc, value - address, addend);
604 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
606 rel14(unsigned char* view,
607 typename elfcpp::Elf_types<size>::Elf_Addr value,
608 typename elfcpp::Elf_types<size>::Elf_Addr addend,
609 typename elfcpp::Elf_types<size>::Elf_Addr address)
611 This::template rela<32>(view, 0, 0xfffc, value - address, addend);
614 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
616 addr16(unsigned char* view,
617 typename elfcpp::Elf_types<size>::Elf_Addr value,
618 typename elfcpp::Elf_types<size>::Elf_Addr addend)
619 { This_reloc::rela16(view, value, addend); }
621 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
623 addr16_ds(unsigned char* view,
624 typename elfcpp::Elf_types<size>::Elf_Addr value,
625 typename elfcpp::Elf_types<size>::Elf_Addr addend)
627 This::template rela<16>(view, 0, 0xfffc, value, addend);
630 // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
632 addr16_lo(unsigned char* view,
633 typename elfcpp::Elf_types<size>::Elf_Addr value,
634 typename elfcpp::Elf_types<size>::Elf_Addr addend)
635 { This_reloc::rela16(view, value, addend); }
637 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
639 addr16_hi(unsigned char* view,
640 typename elfcpp::Elf_types<size>::Elf_Addr value,
641 typename elfcpp::Elf_types<size>::Elf_Addr addend)
643 This::template rela<16>(view, 16, 0xffff, value, addend);
646 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
647 // final value of the low 16 bits of the
648 // relocation is negative, add one.
650 addr16_ha(unsigned char* view,
651 typename elfcpp::Elf_types<size>::Elf_Addr value,
652 typename elfcpp::Elf_types<size>::Elf_Addr addend)
654 This::addr16_hi(view, value + 0x8000, addend);
657 // R_POWERPC_REL16: (Symbol + Addend - Address) & 0xffff
659 rel16(unsigned char* view,
660 typename elfcpp::Elf_types<size>::Elf_Addr value,
661 typename elfcpp::Elf_types<size>::Elf_Addr addend,
662 typename elfcpp::Elf_types<size>::Elf_Addr address)
663 { This_reloc::pcrela16(view, value, addend, address); }
665 // R_POWERPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
667 rel16_lo(unsigned char* view,
668 typename elfcpp::Elf_types<size>::Elf_Addr value,
669 typename elfcpp::Elf_types<size>::Elf_Addr addend,
670 typename elfcpp::Elf_types<size>::Elf_Addr address)
671 { This_reloc::pcrela16(view, value, addend, address); }
673 // R_POWERPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
675 rel16_hi(unsigned char* view,
676 typename elfcpp::Elf_types<size>::Elf_Addr value,
677 typename elfcpp::Elf_types<size>::Elf_Addr addend,
678 typename elfcpp::Elf_types<size>::Elf_Addr address)
680 This::template rela<16>(view, 16, 0xffff, value - address, addend);
683 // R_POWERPC_REL16_HA: Same as R_POWERPC_REL16_HI except that if the
684 // final value of the low 16 bits of the
685 // relocation is negative, add one.
687 rel16_ha(unsigned char* view,
688 typename elfcpp::Elf_types<size>::Elf_Addr value,
689 typename elfcpp::Elf_types<size>::Elf_Addr addend,
690 typename elfcpp::Elf_types<size>::Elf_Addr address)
692 This::rel16_hi(view, value + 0x8000, addend, address);
696 // Stash away the index of .got2 in a relocatable object, if such
699 template<int size, bool big_endian>
701 Powerpc_relobj<size, big_endian>::do_find_special_sections(
702 Read_symbols_data* sd)
706 const unsigned char* const pshdrs = sd->section_headers->data();
707 const unsigned char* namesu = sd->section_names->data();
708 const char* names = reinterpret_cast<const char*>(namesu);
709 section_size_type names_size = sd->section_names_size;
710 const unsigned char* s;
712 s = this->find_shdr(pshdrs, ".got2", names, names_size, NULL);
715 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
716 this->got2_section_ = ndx;
719 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
722 // Set up PowerPC target specific relobj.
724 template<int size, bool big_endian>
726 Target_powerpc<size, big_endian>::do_make_elf_object(
727 const std::string& name,
728 Input_file* input_file,
729 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
731 int et = ehdr.get_e_type();
732 if (et == elfcpp::ET_REL)
734 Powerpc_relobj<size, big_endian>* obj =
735 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
739 else if (et == elfcpp::ET_DYN)
741 Sized_dynobj<size, big_endian>* obj =
742 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
748 gold_error(_("%s: unsupported ELF file type %d"),
754 template<int size, bool big_endian>
755 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
758 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
759 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
761 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
762 : Output_data_got<size, big_endian>(),
763 symtab_(symtab), layout_(layout),
764 header_ent_cnt_(size == 32 ? 3 : 1),
765 header_index_(size == 32 ? 0x2000 : 0)
770 // Create a new GOT entry and return its offset.
772 add_got_entry(Got_entry got_entry)
775 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
778 // Create a pair of new GOT entries and return the offset of the first.
780 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
782 this->reserve_ent(2);
783 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
787 // Value of _GLOBAL_OFFSET_TABLE_
791 return this->got_offset(this->header_index_);
794 // Ensure our GOT has a header.
796 set_final_data_size()
798 if (this->header_ent_cnt_ != 0)
800 Output_data_got<size, big_endian>::set_final_data_size();
803 // First word of GOT header needs some values that are not
804 // handled by Output_data_got so poke them in here.
805 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
807 do_write(Output_file* of)
809 this->replace_constant(this->header_index_,
811 ? this->layout_->dynamic_section()->address()
812 : this->address() + 0x8000));
814 Output_data_got<size, big_endian>::do_write(of);
819 reserve_ent(unsigned int cnt = 1)
821 if (this->header_ent_cnt_ == 0)
823 if (this->num_entries() + cnt > this->header_index_)
830 this->header_ent_cnt_ = 0;
831 this->header_index_ = this->num_entries();
834 Output_data_got<size, big_endian>::add_constant(0);
835 Output_data_got<size, big_endian>::add_constant(0);
836 Output_data_got<size, big_endian>::add_constant(0);
838 // Define _GLOBAL_OFFSET_TABLE_ at the header
839 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
840 Symbol_table::PREDEFINED,
841 this, this->g_o_t(), 0,
848 Output_data_got<size, big_endian>::add_constant(0);
852 Symbol_table* symtab_;
856 unsigned int header_ent_cnt_;
858 unsigned int header_index_;
861 // Get the GOT section, creating it if necessary.
863 template<int size, bool big_endian>
864 Output_data_got_powerpc<size, big_endian>*
865 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
868 if (this->got_ == NULL)
870 gold_assert(symtab != NULL && layout != NULL);
873 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
875 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
876 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
877 this->got_, ORDER_DATA, false);
883 // Get the dynamic reloc section, creating it if necessary.
885 template<int size, bool big_endian>
886 typename Target_powerpc<size, big_endian>::Reloc_section*
887 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
889 if (this->rela_dyn_ == NULL)
891 gold_assert(layout != NULL);
892 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
893 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
894 elfcpp::SHF_ALLOC, this->rela_dyn_,
895 ORDER_DYNAMIC_RELOCS, false);
897 return this->rela_dyn_;
900 // A class to handle the PLT data.
902 template<int size, bool big_endian>
903 class Output_data_plt_powerpc : public Output_section_data_build
906 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
907 size, big_endian> Reloc_section;
909 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
911 // Add an entry to the PLT.
915 // Return the .rela.plt section data.
922 // Return the number of PLT entries.
926 return ((this->current_data_size() - initial_plt_entry_size)
930 // Return the offset of the first non-reserved PLT entry.
932 first_plt_entry_offset()
933 { return initial_plt_entry_size; }
935 // Return the size of a PLT entry.
938 { return plt_entry_size; }
942 do_adjust_output_section(Output_section* os)
947 // Write to a map file.
949 do_print_to_mapfile(Mapfile* mapfile) const
950 { mapfile->print_output_data(this, _("** PLT")); }
953 // The size of an entry in the PLT.
954 static const int plt_entry_size = size == 32 ? 4 : 24;
955 // The size of the first reserved entry.
956 static const int initial_plt_entry_size = size == 32 ? 0 : 24;
958 // Write out the PLT data.
960 do_write(Output_file*);
962 // The reloc section.
964 // Allows access to .glink for do_write.
965 Target_powerpc<size, big_endian>* targ_;
968 // Create the PLT section.
970 template<int size, bool big_endian>
971 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
973 Target_powerpc<size, big_endian>* targ)
974 : Output_section_data_build(size == 32 ? 4 : 8),
977 this->rel_ = new Reloc_section(false);
978 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
979 elfcpp::SHF_ALLOC, this->rel_,
980 ORDER_DYNAMIC_PLT_RELOCS, false);
983 // Add an entry to the PLT.
985 template<int size, bool big_endian>
987 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
989 if (!gsym->has_plt_offset())
991 off_t off = this->current_data_size();
994 off += initial_plt_entry_size;
995 gsym->set_plt_offset(off);
996 gsym->set_needs_dynsym_entry();
997 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
998 off += plt_entry_size;
999 this->set_current_data_size(off);
1003 static const uint32_t addis_11_11 = 0x3d6b0000;
1004 static const uint32_t addis_11_30 = 0x3d7e0000;
1005 static const uint32_t addis_12_12 = 0x3d8c0000;
1006 static const uint32_t addi_11_11 = 0x396b0000;
1007 static const uint32_t add_0_11_11 = 0x7c0b5a14;
1008 static const uint32_t add_11_0_11 = 0x7d605a14;
1009 static const uint32_t b = 0x48000000;
1010 static const uint32_t bcl_20_31 = 0x429f0005;
1011 static const uint32_t bctr = 0x4e800420;
1012 static const uint32_t blrl = 0x4e800021;
1013 static const uint32_t lis_11 = 0x3d600000;
1014 static const uint32_t lis_12 = 0x3d800000;
1015 static const uint32_t lwzu_0_12 = 0x840c0000;
1016 static const uint32_t lwz_0_12 = 0x800c0000;
1017 static const uint32_t lwz_11_11 = 0x816b0000;
1018 static const uint32_t lwz_11_30 = 0x817e0000;
1019 static const uint32_t lwz_12_12 = 0x818c0000;
1020 static const uint32_t mflr_0 = 0x7c0802a6;
1021 static const uint32_t mflr_12 = 0x7d8802a6;
1022 static const uint32_t mtctr_0 = 0x7c0903a6;
1023 static const uint32_t mtctr_11 = 0x7d6903a6;
1024 static const uint32_t mtlr_0 = 0x7c0803a6;
1025 static const uint32_t nop = 0x60000000;
1026 static const uint32_t sub_11_11_12 = 0x7d6c5850;
1027 static const uint32_t addis_12_2 = 0x3d820000;
1028 static const uint32_t std_2_1 = 0xf8410000;
1029 static const uint32_t ld_11_12 = 0xe96c0000;
1030 static const uint32_t ld_2_12 = 0xe84c0000;
1031 static const uint32_t addi_12_12 = 0x398c0000;
1032 static const uint32_t ld_11_2 = 0xe9620000;
1033 static const uint32_t addi_2_2 = 0x38420000;
1034 static const uint32_t ld_2_2 = 0xe8420000;
1035 static const uint32_t mflr_11 = 0x7d6802a6;
1036 static const uint32_t ld_2_11 = 0xe84b0000;
1037 static const uint32_t mtlr_12 = 0x7d8803a6;
1038 static const uint32_t add_12_2_11 = 0x7d825a14;
1039 static const uint32_t li_0_0 = 0x38000000;
1040 static const uint32_t lis_0_0 = 0x3c000000;
1041 static const uint32_t ori_0_0_0 = 0x60000000;
1043 // Write out the PLT.
1045 template<int size, bool big_endian>
1047 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1051 const off_t offset = this->offset();
1052 const section_size_type oview_size
1053 = convert_to_section_size_type(this->data_size());
1054 unsigned char* const oview = of->get_output_view(offset, oview_size);
1055 unsigned char* pov = oview;
1056 unsigned char* endpov = oview + oview_size;
1058 // The address the .glink branch table
1059 const Output_data_glink<size, big_endian>* glink
1060 = this->targ_->glink_section();
1061 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1062 = glink->address() + glink->pltresolve();
1064 while (pov < endpov)
1066 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1071 of->write_output_view(offset, oview_size, oview);
1075 // Create the PLT section.
1077 template<int size, bool big_endian>
1079 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1081 if (this->plt_ == NULL)
1083 if (this->glink_ == NULL)
1084 make_glink_section(layout);
1086 // Ensure that .rela.dyn always appears before .rela.plt This is
1087 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1088 // needs to include .rela.plt in it's range.
1089 this->rela_dyn_section(layout);
1091 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1092 layout->add_output_section_data(".plt",
1094 ? elfcpp::SHT_PROGBITS
1095 : elfcpp::SHT_NOBITS),
1096 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1105 // A class to handle .glink.
1107 template<int size, bool big_endian>
1108 class Output_data_glink : public Output_section_data
1111 Output_data_glink(Target_powerpc<size, big_endian>*);
1115 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1116 const Sized_relobj<size, big_endian>*);
1119 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1120 const Sized_relobj<size, big_endian>*) const;
1123 glink_entry_size() const
1128 // FIXME: We should be using multiple glink sections for
1129 // stubs to support > 33M applications.
1136 return this->pltresolve_;
1140 // Write to a map file.
1142 do_print_to_mapfile(Mapfile* mapfile) const
1143 { mapfile->print_output_data(this, _("** glink")); }
1146 static const int pltresolve_size = 16*4;
1149 set_final_data_size();
1153 do_write(Output_file*);
1158 Glink_sym_ent(const Symbol* sym,
1159 const elfcpp::Rela<size, big_endian>& reloc,
1160 const Sized_relobj<size, big_endian>* object)
1161 : sym_(sym), object_(0), addend_(0)
1164 this->addend_ = reloc.get_r_addend();
1165 else if (parameters->options().output_is_position_independent()
1166 && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1167 == elfcpp::R_PPC_PLTREL24))
1169 this->addend_ = reloc.get_r_addend();
1170 if (this->addend_ != 0)
1171 this->object_ = object;
1176 const Sized_relobj<size, big_endian>* object_;
1177 unsigned int addend_;
1179 bool operator==(const Glink_sym_ent& that) const
1181 return (this->sym_ == that.sym_
1182 && this->object_ == that.object_
1183 && this->addend_ == that.addend_);
1187 class Glink_sym_ent_hash
1190 size_t operator()(const Glink_sym_ent& ent) const
1192 return (reinterpret_cast<uintptr_t>(ent.sym_)
1193 ^ reinterpret_cast<uintptr_t>(ent.object_)
1198 // Map sym/object/addend to index.
1199 typedef Unordered_map<Glink_sym_ent, unsigned int,
1200 Glink_sym_ent_hash> Glink_entries;
1201 Glink_entries glink_entries_;
1203 // Offset of pltresolve stub (actually, branch table for 32-bit)
1206 // Allows access to .got and .plt for do_write.
1207 Target_powerpc<size, big_endian>* targ_;
1210 // Create the glink section.
1212 template<int size, bool big_endian>
1213 Output_data_glink<size, big_endian>::Output_data_glink(
1214 Target_powerpc<size, big_endian>* targ)
1215 : Output_section_data(16),
1216 pltresolve_(0), targ_(targ)
1220 // Add an entry to glink, if we do not already have one for this
1221 // sym/object/addend combo.
1223 template<int size, bool big_endian>
1225 Output_data_glink<size, big_endian>::add_entry(
1227 const elfcpp::Rela<size, big_endian>& reloc,
1228 const Sized_relobj<size, big_endian>* object)
1230 Glink_sym_ent ent(gsym, reloc, object);
1231 unsigned int indx = this->glink_entries_.size();
1232 this->glink_entries_.insert(std::make_pair(ent, indx));
1235 template<int size, bool big_endian>
1237 Output_data_glink<size, big_endian>::find_entry(
1239 const elfcpp::Rela<size, big_endian>& reloc,
1240 const Sized_relobj<size, big_endian>* object) const
1242 Glink_sym_ent ent(gsym, reloc, object);
1243 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1244 gold_assert(p != this->glink_entries_.end());
1248 template<int size, bool big_endian>
1250 Output_data_glink<size, big_endian>::set_final_data_size()
1252 unsigned int count = this->glink_entries_.size();
1253 off_t total = count;
1260 this->pltresolve_ = total;
1262 // space for branch table
1263 total += 4 * (count - 1);
1265 total += -total & 15;
1266 total += this->pltresolve_size;
1271 this->pltresolve_ = total;
1272 total += this->pltresolve_size;
1274 // space for branch table
1277 total += 4 * (count - 0x8000);
1281 this->set_data_size(total);
1284 static inline uint32_t
1290 static inline uint32_t
1296 static inline uint32_t
1299 return hi(a + 0x8000);
1302 template<bool big_endian>
1304 write_insn(unsigned char *p, uint32_t v)
1306 elfcpp::Swap<32, big_endian>::writeval(p, v);
1309 // Write out .glink.
1311 template<int size, bool big_endian>
1313 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1315 const off_t off = this->offset();
1316 const section_size_type oview_size =
1317 convert_to_section_size_type(this->data_size());
1318 unsigned char* const oview = of->get_output_view(off, oview_size);
1321 // The base address of the .plt section.
1322 uint32_t plt_base = this->targ_->plt_section()->address();
1324 // The address of _GLOBAL_OFFSET_TABLE_.
1325 const Output_data_got_powerpc<size, big_endian> *got;
1326 typename elfcpp::Elf_types<size>::Elf_Addr g_o_t;
1327 got = this->targ_->got_section();
1328 g_o_t = got->address() + got->g_o_t();
1332 // Write out call stubs.
1333 typename Glink_entries::const_iterator g;
1334 for (g = this->glink_entries_.begin();
1335 g != this->glink_entries_.end();
1338 uint64_t plt_addr = plt_base + g->first.sym_->plt_offset();
1339 uint64_t got_addr = g_o_t;
1340 uint64_t pltoff = plt_addr - got_addr;
1342 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1343 gold_error(_("%s: linkage table error against `%s'"),
1344 g->first.object_->name().c_str(),
1345 g->first.sym_->demangled_name().c_str());
1347 p = oview + g->second * this->glink_entry_size();
1348 if (ha(pltoff) != 0)
1350 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1351 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1352 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1353 if (ha(pltoff + 16) != ha(pltoff))
1355 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1358 write_insn<big_endian>(p, mtctr_11), p += 4;
1359 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1360 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1361 write_insn<big_endian>(p, bctr), p += 4;
1365 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1366 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1367 if (ha(pltoff + 16) != ha(pltoff))
1369 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1372 write_insn<big_endian>(p, mtctr_11), p += 4;
1373 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1374 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1375 write_insn<big_endian>(p, bctr), p += 4;
1379 // Write pltresolve stub.
1380 p = oview + this->pltresolve_;
1381 uint64_t after_bcl = this->address() + this->pltresolve_ + 16;
1382 uint64_t pltoff = plt_base - after_bcl;
1384 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1386 write_insn<big_endian>(p, mflr_12), p += 4;
1387 write_insn<big_endian>(p, bcl_20_31), p += 4;
1388 write_insn<big_endian>(p, mflr_11), p += 4;
1389 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1390 write_insn<big_endian>(p, mtlr_12), p += 4;
1391 write_insn<big_endian>(p, add_12_2_11), p += 4;
1392 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1393 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1394 write_insn<big_endian>(p, mtctr_11), p += 4;
1395 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1396 write_insn<big_endian>(p, bctr), p += 4;
1397 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1398 write_insn<big_endian>(p, nop), p += 4;
1400 // Write lazy link call stubs.
1402 while (p < oview + oview_size)
1406 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1410 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1411 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1413 uint16_t branch_off = this->pltresolve_ + 8 - (p - oview);
1414 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1420 // Write out call stubs.
1421 typename Glink_entries::const_iterator g;
1422 for (g = this->glink_entries_.begin();
1423 g != this->glink_entries_.end();
1426 uint32_t plt_addr = plt_base + g->first.sym_->plt_offset();
1429 p = oview + g->second * this->glink_entry_size();
1430 if (parameters->options().output_is_position_independent())
1432 const Powerpc_relobj<size, big_endian>* object = static_cast
1433 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1436 unsigned int got2 = object->got2_shndx();
1437 got_addr = (g->first.object_->output_section(got2)->address()
1438 + g->first.object_->output_section_offset(got2)
1439 + g->first.addend_);
1444 uint32_t pltoff = plt_addr - got_addr;
1445 if (ha(pltoff) == 0)
1447 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1448 write_insn<big_endian>(p + 4, mtctr_11);
1449 write_insn<big_endian>(p + 8, bctr);
1453 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1454 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1455 write_insn<big_endian>(p + 8, mtctr_11);
1456 write_insn<big_endian>(p + 12, bctr);
1461 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1462 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1463 write_insn<big_endian>(p + 8, mtctr_11);
1464 write_insn<big_endian>(p + 12, bctr);
1468 // Write out pltresolve branch table.
1469 p = oview + this->pltresolve_;
1470 unsigned int the_end = oview_size - this->pltresolve_size;
1471 unsigned char *end_p = oview + the_end;
1472 while (p < end_p - 8 * 4)
1473 write_insn<big_endian>(p, b + end_p - p), p += 4;
1475 write_insn<big_endian>(p, nop), p += 4;
1477 // Write out pltresolve call stub.
1478 if (parameters->options().output_is_position_independent())
1480 uint32_t res0_off = this->pltresolve_;
1481 uint32_t after_bcl_off = the_end + 12;
1482 uint32_t bcl_res0 = after_bcl_off - res0_off;
1484 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1485 write_insn<big_endian>(p + 4, mflr_0);
1486 write_insn<big_endian>(p + 8, bcl_20_31);
1487 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1488 write_insn<big_endian>(p + 16, mflr_12);
1489 write_insn<big_endian>(p + 20, mtlr_0);
1490 write_insn<big_endian>(p + 24, sub_11_11_12);
1492 uint32_t got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1494 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1495 if (ha(got_bcl) == ha(got_bcl + 4))
1497 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1498 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1502 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1503 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1505 write_insn<big_endian>(p + 40, mtctr_0);
1506 write_insn<big_endian>(p + 44, add_0_11_11);
1507 write_insn<big_endian>(p + 48, add_11_0_11);
1508 write_insn<big_endian>(p + 52, bctr);
1509 write_insn<big_endian>(p + 56, nop);
1510 write_insn<big_endian>(p + 60, nop);
1514 uint32_t res0 = this->pltresolve_ + this->address();
1516 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1517 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1518 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1519 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1521 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1522 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1523 write_insn<big_endian>(p + 16, mtctr_0);
1524 write_insn<big_endian>(p + 20, add_0_11_11);
1525 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1526 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1528 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1529 write_insn<big_endian>(p + 28, add_11_0_11);
1530 write_insn<big_endian>(p + 32, bctr);
1531 write_insn<big_endian>(p + 36, nop);
1532 write_insn<big_endian>(p + 40, nop);
1533 write_insn<big_endian>(p + 44, nop);
1534 write_insn<big_endian>(p + 48, nop);
1535 write_insn<big_endian>(p + 52, nop);
1536 write_insn<big_endian>(p + 56, nop);
1537 write_insn<big_endian>(p + 60, nop);
1542 of->write_output_view(off, oview_size, oview);
1545 // Create the glink section.
1547 template<int size, bool big_endian>
1549 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
1551 if (this->glink_ == NULL)
1553 this->glink_ = new Output_data_glink<size, big_endian>(this);
1554 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
1555 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1556 this->glink_, ORDER_TEXT, false);
1560 // Create a PLT entry for a global symbol.
1562 template<int size, bool big_endian>
1564 Target_powerpc<size, big_endian>::make_plt_entry(
1567 const elfcpp::Rela<size, big_endian>& reloc,
1568 const Sized_relobj<size, big_endian>* object)
1570 if (this->plt_ == NULL)
1571 this->make_plt_section(layout);
1573 this->plt_->add_entry(gsym);
1575 this->glink_->add_entry(gsym, reloc, object);
1578 // Return the number of entries in the PLT.
1580 template<int size, bool big_endian>
1582 Target_powerpc<size, big_endian>::plt_entry_count() const
1584 if (this->plt_ == NULL)
1586 return this->plt_->entry_count();
1589 // Return the offset of the first non-reserved PLT entry.
1591 template<int size, bool big_endian>
1593 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1595 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1598 // Return the size of each PLT entry.
1600 template<int size, bool big_endian>
1602 Target_powerpc<size, big_endian>::plt_entry_size() const
1604 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1607 // Create a GOT entry for the TLS module index.
1609 template<int size, bool big_endian>
1611 Target_powerpc<size, big_endian>::got_mod_index_entry(
1612 Symbol_table* symtab,
1614 Sized_relobj_file<size, big_endian>* object)
1616 if (this->got_mod_index_offset_ == -1U)
1618 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1619 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1620 Output_data_got_powerpc<size, big_endian>* got;
1621 unsigned int got_offset;
1623 got = this->got_section(symtab, layout);
1624 got->reserve_ent(2);
1625 got_offset = got->add_constant(0);
1626 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1628 got->add_constant(0);
1629 this->got_mod_index_offset_ = got_offset;
1631 return this->got_mod_index_offset_;
1634 // Optimize the TLS relocation type based on what we know about the
1635 // symbol. IS_FINAL is true if the final address of this symbol is
1636 // known at link time.
1638 template<int size, bool big_endian>
1639 tls::Tls_optimization
1640 Target_powerpc<size, big_endian>::optimize_tls_reloc(bool, int)
1642 // If we are generating a shared library, then we can't do anything
1644 if (parameters->options().shared())
1645 return tls::TLSOPT_NONE;
1647 return tls::TLSOPT_NONE;
1650 // Get the Reference_flags for a particular relocation.
1652 template<int size, bool big_endian>
1654 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
1658 case elfcpp::R_POWERPC_NONE:
1659 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1660 case elfcpp::R_POWERPC_GNU_VTENTRY:
1661 case elfcpp::R_PPC64_TOC:
1662 // No symbol reference.
1665 case elfcpp::R_POWERPC_ADDR16:
1666 case elfcpp::R_POWERPC_ADDR16_LO:
1667 case elfcpp::R_POWERPC_ADDR16_HI:
1668 case elfcpp::R_POWERPC_ADDR16_HA:
1669 case elfcpp::R_POWERPC_ADDR32:
1670 case elfcpp::R_PPC64_ADDR64:
1671 return Symbol::ABSOLUTE_REF;
1673 case elfcpp::R_POWERPC_REL24:
1674 case elfcpp::R_PPC_LOCAL24PC:
1675 case elfcpp::R_POWERPC_REL16:
1676 case elfcpp::R_POWERPC_REL16_LO:
1677 case elfcpp::R_POWERPC_REL16_HI:
1678 case elfcpp::R_POWERPC_REL16_HA:
1679 return Symbol::RELATIVE_REF;
1681 case elfcpp::R_PPC_PLTREL24:
1682 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1684 case elfcpp::R_POWERPC_GOT16:
1685 case elfcpp::R_POWERPC_GOT16_LO:
1686 case elfcpp::R_POWERPC_GOT16_HI:
1687 case elfcpp::R_POWERPC_GOT16_HA:
1688 case elfcpp::R_PPC64_TOC16:
1689 case elfcpp::R_PPC64_TOC16_LO:
1690 case elfcpp::R_PPC64_TOC16_HI:
1691 case elfcpp::R_PPC64_TOC16_HA:
1692 case elfcpp::R_PPC64_TOC16_DS:
1693 case elfcpp::R_PPC64_TOC16_LO_DS:
1695 return Symbol::ABSOLUTE_REF;
1697 case elfcpp::R_POWERPC_GOT_TPREL16:
1698 case elfcpp::R_POWERPC_TLS:
1699 return Symbol::TLS_REF;
1701 case elfcpp::R_POWERPC_COPY:
1702 case elfcpp::R_POWERPC_GLOB_DAT:
1703 case elfcpp::R_POWERPC_JMP_SLOT:
1704 case elfcpp::R_POWERPC_RELATIVE:
1705 case elfcpp::R_POWERPC_DTPMOD:
1707 // Not expected. We will give an error later.
1712 // Report an unsupported relocation against a local symbol.
1714 template<int size, bool big_endian>
1716 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1717 Sized_relobj_file<size, big_endian>* object,
1718 unsigned int r_type)
1720 gold_error(_("%s: unsupported reloc %u against local symbol"),
1721 object->name().c_str(), r_type);
1724 // We are about to emit a dynamic relocation of type R_TYPE. If the
1725 // dynamic linker does not support it, issue an error.
1727 template<int size, bool big_endian>
1729 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1730 unsigned int r_type)
1732 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1734 // These are the relocation types supported by glibc for both 32-bit
1735 // and 64-bit powerpc.
1738 case elfcpp::R_POWERPC_RELATIVE:
1739 case elfcpp::R_POWERPC_GLOB_DAT:
1740 case elfcpp::R_POWERPC_DTPMOD:
1741 case elfcpp::R_POWERPC_DTPREL:
1742 case elfcpp::R_POWERPC_TPREL:
1743 case elfcpp::R_POWERPC_JMP_SLOT:
1744 case elfcpp::R_POWERPC_COPY:
1745 case elfcpp::R_POWERPC_ADDR32:
1746 case elfcpp::R_POWERPC_ADDR24:
1747 case elfcpp::R_POWERPC_REL24:
1758 // These are the relocation types supported only on 64-bit.
1759 case elfcpp::R_PPC64_ADDR64:
1760 case elfcpp::R_PPC64_TPREL16_LO_DS:
1761 case elfcpp::R_PPC64_TPREL16_DS:
1762 case elfcpp::R_POWERPC_TPREL16:
1763 case elfcpp::R_POWERPC_TPREL16_LO:
1764 case elfcpp::R_POWERPC_TPREL16_HI:
1765 case elfcpp::R_POWERPC_TPREL16_HA:
1766 case elfcpp::R_PPC64_TPREL16_HIGHER:
1767 case elfcpp::R_PPC64_TPREL16_HIGHEST:
1768 case elfcpp::R_PPC64_TPREL16_HIGHERA:
1769 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1770 case elfcpp::R_PPC64_ADDR16_LO_DS:
1771 case elfcpp::R_POWERPC_ADDR16_LO:
1772 case elfcpp::R_POWERPC_ADDR16_HI:
1773 case elfcpp::R_POWERPC_ADDR16_HA:
1774 case elfcpp::R_POWERPC_ADDR30:
1775 case elfcpp::R_PPC64_UADDR64:
1776 case elfcpp::R_POWERPC_UADDR32:
1777 case elfcpp::R_POWERPC_ADDR16:
1778 case elfcpp::R_POWERPC_UADDR16:
1779 case elfcpp::R_PPC64_ADDR16_DS:
1780 case elfcpp::R_PPC64_ADDR16_HIGHER:
1781 case elfcpp::R_PPC64_ADDR16_HIGHEST:
1782 case elfcpp::R_PPC64_ADDR16_HIGHERA:
1783 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1784 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1785 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1786 case elfcpp::R_POWERPC_REL32:
1787 case elfcpp::R_PPC64_REL64:
1798 // These are the relocation types supported only on 32-bit.
1805 // This prevents us from issuing more than one error per reloc
1806 // section. But we can still wind up issuing more than one
1807 // error per object file.
1808 if (this->issued_non_pic_error_)
1810 gold_assert(parameters->options().output_is_position_independent());
1811 object->error(_("requires unsupported dynamic reloc; "
1812 "recompile with -fPIC"));
1813 this->issued_non_pic_error_ = true;
1817 // Scan a relocation for a local symbol.
1819 template<int size, bool big_endian>
1821 Target_powerpc<size, big_endian>::Scan::local(
1822 Symbol_table* symtab,
1824 Target_powerpc<size, big_endian>* target,
1825 Sized_relobj_file<size, big_endian>* object,
1826 unsigned int data_shndx,
1827 Output_section* output_section,
1828 const elfcpp::Rela<size, big_endian>& reloc,
1829 unsigned int r_type,
1830 const elfcpp::Sym<size, big_endian>& lsym)
1834 case elfcpp::R_POWERPC_NONE:
1835 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1836 case elfcpp::R_POWERPC_GNU_VTENTRY:
1837 case elfcpp::R_PPC64_TOCSAVE:
1840 case elfcpp::R_PPC64_ADDR64:
1841 case elfcpp::R_POWERPC_ADDR32:
1842 case elfcpp::R_POWERPC_ADDR16_HA:
1843 case elfcpp::R_POWERPC_ADDR16_LO:
1844 // If building a shared library (or a position-independent
1845 // executable), we need to create a dynamic relocation for
1847 if (parameters->options().output_is_position_independent())
1849 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1851 check_non_pic(object, r_type);
1852 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1854 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1855 rela_dyn->add_local(object, r_sym, r_type, output_section,
1856 data_shndx, reloc.get_r_offset(),
1857 reloc.get_r_addend());
1861 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1862 gold_assert(lsym.get_st_value() == 0);
1863 rela_dyn->add_local_relative(object, r_sym, r_type,
1864 output_section, data_shndx,
1865 reloc.get_r_offset(),
1866 reloc.get_r_addend(), false);
1871 case elfcpp::R_POWERPC_REL24:
1872 case elfcpp::R_PPC_LOCAL24PC:
1873 case elfcpp::R_POWERPC_REL32:
1874 case elfcpp::R_POWERPC_REL16_LO:
1875 case elfcpp::R_POWERPC_REL16_HA:
1878 case elfcpp::R_POWERPC_GOT16:
1879 case elfcpp::R_POWERPC_GOT16_LO:
1880 case elfcpp::R_POWERPC_GOT16_HI:
1881 case elfcpp::R_POWERPC_GOT16_HA:
1883 // The symbol requires a GOT entry.
1884 Output_data_got_powerpc<size, big_endian>* got;
1887 got = target->got_section(symtab, layout);
1888 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1890 // If we are generating a shared object, we need to add a
1891 // dynamic relocation for this symbol's GOT entry.
1892 if (parameters->options().output_is_position_independent())
1894 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1896 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1899 off = got->add_constant(0);
1900 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1901 rela_dyn->add_local_relative(object, r_sym,
1902 elfcpp::R_POWERPC_RELATIVE,
1903 got, off, 0, false);
1907 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1911 case elfcpp::R_PPC64_TOC16:
1912 case elfcpp::R_PPC64_TOC16_LO:
1913 case elfcpp::R_PPC64_TOC16_HI:
1914 case elfcpp::R_PPC64_TOC16_HA:
1915 case elfcpp::R_PPC64_TOC16_DS:
1916 case elfcpp::R_PPC64_TOC16_LO_DS:
1917 case elfcpp::R_PPC64_TOC:
1918 // We need a GOT section.
1919 target->got_section(symtab, layout);
1922 // These are relocations which should only be seen by the
1923 // dynamic linker, and should never be seen here.
1924 case elfcpp::R_POWERPC_COPY:
1925 case elfcpp::R_POWERPC_GLOB_DAT:
1926 case elfcpp::R_POWERPC_JMP_SLOT:
1927 case elfcpp::R_POWERPC_RELATIVE:
1928 case elfcpp::R_POWERPC_DTPMOD:
1929 gold_error(_("%s: unexpected reloc %u in object file"),
1930 object->name().c_str(), r_type);
1934 unsupported_reloc_local(object, r_type);
1939 // Report an unsupported relocation against a global symbol.
1941 template<int size, bool big_endian>
1943 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1944 Sized_relobj_file<size, big_endian>* object,
1945 unsigned int r_type,
1948 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1949 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1952 // Scan a relocation for a global symbol.
1954 template<int size, bool big_endian>
1956 Target_powerpc<size, big_endian>::Scan::global(
1957 Symbol_table* symtab,
1959 Target_powerpc<size, big_endian>* target,
1960 Sized_relobj_file<size, big_endian>* object,
1961 unsigned int data_shndx,
1962 Output_section* output_section,
1963 const elfcpp::Rela<size, big_endian>& reloc,
1964 unsigned int r_type,
1969 case elfcpp::R_POWERPC_NONE:
1970 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1971 case elfcpp::R_POWERPC_GNU_VTENTRY:
1972 case elfcpp::R_PPC_LOCAL24PC:
1975 case elfcpp::R_POWERPC_ADDR16:
1976 case elfcpp::R_POWERPC_ADDR16_LO:
1977 case elfcpp::R_POWERPC_ADDR16_HI:
1978 case elfcpp::R_POWERPC_ADDR16_HA:
1979 case elfcpp::R_POWERPC_ADDR32:
1980 case elfcpp::R_PPC64_ADDR64:
1982 // Make a PLT entry if necessary.
1983 if (gsym->needs_plt_entry())
1985 target->make_plt_entry(layout, gsym, reloc, 0);
1986 // Since this is not a PC-relative relocation, we may be
1987 // taking the address of a function. In that case we need to
1988 // set the entry in the dynamic symbol table to the address of
1991 && gsym->is_from_dynobj() && !parameters->options().shared())
1992 gsym->set_needs_dynsym_value();
1994 // Make a dynamic relocation if necessary.
1995 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1997 if (gsym->may_need_copy_reloc())
1999 target->copy_reloc(symtab, layout, object,
2000 data_shndx, output_section, gsym, reloc);
2002 else if ((r_type == elfcpp::R_POWERPC_ADDR32
2003 || r_type == elfcpp::R_PPC64_ADDR64)
2004 && gsym->can_use_relative_reloc(false))
2006 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2007 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2008 output_section, object,
2009 data_shndx, reloc.get_r_offset(),
2010 reloc.get_r_addend(), false);
2014 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2016 check_non_pic(object, r_type);
2017 if (gsym->is_from_dynobj()
2018 || gsym->is_undefined()
2019 || gsym->is_preemptible())
2020 rela_dyn->add_global(gsym, r_type, output_section,
2022 reloc.get_r_offset(),
2023 reloc.get_r_addend());
2025 rela_dyn->add_global_relative(gsym, r_type,
2026 output_section, object,
2028 reloc.get_r_offset(),
2029 reloc.get_r_addend(), false);
2035 case elfcpp::R_PPC_PLTREL24:
2036 case elfcpp::R_POWERPC_REL24:
2038 if (gsym->needs_plt_entry()
2039 || (!gsym->final_value_is_known()
2040 && !(gsym->is_defined()
2041 && !gsym->is_from_dynobj()
2042 && !gsym->is_preemptible())))
2043 target->make_plt_entry(layout, gsym, reloc, object);
2044 // Make a dynamic relocation if necessary.
2045 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2047 if (gsym->may_need_copy_reloc())
2049 target->copy_reloc(symtab, layout, object,
2050 data_shndx, output_section, gsym,
2055 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2056 check_non_pic(object, r_type);
2057 rela_dyn->add_global(gsym, r_type, output_section, object,
2058 data_shndx, reloc.get_r_offset(),
2059 reloc.get_r_addend());
2065 case elfcpp::R_POWERPC_REL16:
2066 case elfcpp::R_POWERPC_REL16_LO:
2067 case elfcpp::R_POWERPC_REL16_HI:
2068 case elfcpp::R_POWERPC_REL16_HA:
2071 case elfcpp::R_POWERPC_GOT16:
2072 case elfcpp::R_POWERPC_GOT16_LO:
2073 case elfcpp::R_POWERPC_GOT16_HI:
2074 case elfcpp::R_POWERPC_GOT16_HA:
2076 // The symbol requires a GOT entry.
2077 Output_data_got_powerpc<size, big_endian>* got;
2079 got = target->got_section(symtab, layout);
2080 if (gsym->final_value_is_known())
2081 got->add_global(gsym, GOT_TYPE_STANDARD);
2084 // If this symbol is not fully resolved, we need to add a
2085 // dynamic relocation for it.
2086 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2087 if (gsym->is_from_dynobj()
2088 || gsym->is_undefined()
2089 || gsym->is_preemptible())
2090 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2091 elfcpp::R_POWERPC_GLOB_DAT);
2092 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2094 unsigned int off = got->add_constant(0);
2096 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2097 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2098 got, off, 0, false);
2104 case elfcpp::R_PPC64_TOC:
2105 case elfcpp::R_PPC64_TOC16:
2106 case elfcpp::R_PPC64_TOC16_LO:
2107 case elfcpp::R_PPC64_TOC16_HI:
2108 case elfcpp::R_PPC64_TOC16_HA:
2109 case elfcpp::R_PPC64_TOC16_DS:
2110 case elfcpp::R_PPC64_TOC16_LO_DS:
2111 // We need a GOT section.
2112 target->got_section(symtab, layout);
2115 case elfcpp::R_POWERPC_GOT_TPREL16:
2116 case elfcpp::R_POWERPC_TLS:
2120 // These are relocations which should only be seen by the
2121 // dynamic linker, and should never be seen here.
2122 case elfcpp::R_POWERPC_COPY:
2123 case elfcpp::R_POWERPC_GLOB_DAT:
2124 case elfcpp::R_POWERPC_JMP_SLOT:
2125 case elfcpp::R_POWERPC_RELATIVE:
2126 case elfcpp::R_POWERPC_DTPMOD:
2127 gold_error(_("%s: unexpected reloc %u in object file"),
2128 object->name().c_str(), r_type);
2132 unsupported_reloc_global(object, r_type, gsym);
2137 // Process relocations for gc.
2139 template<int size, bool big_endian>
2141 Target_powerpc<size, big_endian>::gc_process_relocs(
2142 Symbol_table* symtab,
2144 Sized_relobj_file<size, big_endian>* object,
2145 unsigned int data_shndx,
2147 const unsigned char* prelocs,
2149 Output_section* output_section,
2150 bool needs_special_offset_handling,
2151 size_t local_symbol_count,
2152 const unsigned char* plocal_symbols)
2154 typedef Target_powerpc<size, big_endian> Powerpc;
2155 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2157 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2158 typename Target_powerpc::Relocatable_size_for_reloc>(
2167 needs_special_offset_handling,
2172 // Scan relocations for a section.
2174 template<int size, bool big_endian>
2176 Target_powerpc<size, big_endian>::scan_relocs(
2177 Symbol_table* symtab,
2179 Sized_relobj_file<size, big_endian>* object,
2180 unsigned int data_shndx,
2181 unsigned int sh_type,
2182 const unsigned char* prelocs,
2184 Output_section* output_section,
2185 bool needs_special_offset_handling,
2186 size_t local_symbol_count,
2187 const unsigned char* plocal_symbols)
2189 typedef Target_powerpc<size, big_endian> Powerpc;
2190 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2192 if (sh_type == elfcpp::SHT_REL)
2194 gold_error(_("%s: unsupported REL reloc section"),
2195 object->name().c_str());
2201 static Output_data_space* sdata;
2203 // Define _SDA_BASE_ at the start of the .sdata section.
2206 // layout->find_output_section(".sdata") == NULL
2207 sdata = new Output_data_space(4, "** sdata");
2209 = layout->add_output_section_data(".sdata", 0,
2211 | elfcpp::SHF_WRITE,
2212 sdata, ORDER_SMALL_DATA, false);
2213 symtab->define_in_output_data("_SDA_BASE_", NULL,
2214 Symbol_table::PREDEFINED,
2215 os, 32768, 0, elfcpp::STT_OBJECT,
2216 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2221 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2230 needs_special_offset_handling,
2235 // Finalize the sections.
2237 template<int size, bool big_endian>
2239 Target_powerpc<size, big_endian>::do_finalize_sections(
2241 const Input_objects*,
2244 // Fill in some more dynamic tags.
2245 const Reloc_section* rel_plt = (this->plt_ == NULL
2247 : this->plt_->rel_plt());
2248 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2249 this->rela_dyn_, true, size == 32);
2253 this->got_->finalize_data_size();
2254 Output_data_dynamic* odyn = layout->dynamic_data();
2255 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2256 this->got_, this->got_->g_o_t());
2259 // Emit any relocs we saved in an attempt to avoid generating COPY
2261 if (this->copy_relocs_.any_saved_relocs())
2262 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2265 // Perform a relocation.
2267 template<int size, bool big_endian>
2269 Target_powerpc<size, big_endian>::Relocate::relocate(
2270 const Relocate_info<size, big_endian>* relinfo,
2271 Target_powerpc* target,
2274 const elfcpp::Rela<size, big_endian>& rela,
2275 unsigned int r_type,
2276 const Sized_symbol<size>* gsym,
2277 const Symbol_value<size>* psymval,
2278 unsigned char* view,
2279 typename elfcpp::Elf_types<size>::Elf_Addr address,
2280 section_size_type /* view_size */)
2282 const unsigned int toc_base_offset = 0x8000;
2283 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
2284 const Powerpc_relobj<size, big_endian>* const object
2285 = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
2286 elfcpp::Elf_Xword value;
2288 if (r_type == elfcpp::R_POWERPC_GOT16
2289 || r_type == elfcpp::R_POWERPC_GOT16_LO
2290 || r_type == elfcpp::R_POWERPC_GOT16_HI
2291 || r_type == elfcpp::R_POWERPC_GOT16_HA
2292 || r_type == elfcpp::R_PPC64_GOT16_DS
2293 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
2297 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2298 value = gsym->got_offset(GOT_TYPE_STANDARD);
2302 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2303 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2304 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2306 value -= target->got_section()->g_o_t();
2308 else if (r_type == elfcpp::R_PPC64_TOC)
2310 value = target->got_section()->address() + toc_base_offset;
2312 else if (gsym != NULL
2313 && (r_type == elfcpp::R_POWERPC_REL24
2314 || r_type == elfcpp::R_PPC_PLTREL24)
2315 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2317 const Output_data_glink<size, big_endian>* glink;
2319 glink = target->glink_section();
2320 unsigned int glink_index = glink->find_entry(gsym, rela, object);
2321 value = glink->address() + glink_index * glink->glink_entry_size();
2325 elfcpp::Elf_Xword addend = 0;
2326 if (r_type != elfcpp::R_PPC_PLTREL24)
2327 addend = rela.get_r_addend();
2328 value = psymval->value(object, addend);
2333 case elfcpp::R_PPC64_TOC16:
2334 case elfcpp::R_PPC64_TOC16_LO:
2335 case elfcpp::R_PPC64_TOC16_HI:
2336 case elfcpp::R_PPC64_TOC16_HA:
2337 case elfcpp::R_PPC64_TOC16_DS:
2338 case elfcpp::R_PPC64_TOC16_LO_DS:
2339 // Subtract the TOC base address.
2340 value -= target->got_section()->address() + toc_base_offset;
2343 case elfcpp::R_POWERPC_SECTOFF:
2344 case elfcpp::R_POWERPC_SECTOFF_LO:
2345 case elfcpp::R_POWERPC_SECTOFF_HI:
2346 case elfcpp::R_POWERPC_SECTOFF_HA:
2347 case elfcpp::R_PPC64_SECTOFF_DS:
2348 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2350 value -= os->address();
2359 case elfcpp::R_POWERPC_NONE:
2360 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2361 case elfcpp::R_POWERPC_GNU_VTENTRY:
2364 case elfcpp::R_POWERPC_REL32:
2365 Reloc::rel32(view, value, 0, address);
2368 case elfcpp::R_POWERPC_REL24:
2369 case elfcpp::R_PPC_PLTREL24:
2370 case elfcpp::R_PPC_LOCAL24PC:
2371 Reloc::rel24(view, value, 0, address);
2374 case elfcpp::R_POWERPC_REL14:
2375 Reloc::rel14(view, value, 0, address);
2378 case elfcpp::R_PPC64_ADDR64:
2379 case elfcpp::R_PPC64_TOC:
2380 Relocate_functions<size, big_endian>::rela64(view, value, 0);
2383 case elfcpp::R_POWERPC_ADDR32:
2384 Relocate_functions<size, big_endian>::rela32(view, value, 0);
2387 case elfcpp::R_POWERPC_ADDR16:
2388 case elfcpp::R_PPC64_TOC16:
2389 case elfcpp::R_POWERPC_GOT16:
2390 case elfcpp::R_POWERPC_SECTOFF:
2391 Reloc::addr16(view, value, 0);
2394 case elfcpp::R_POWERPC_ADDR16_LO:
2395 case elfcpp::R_PPC64_TOC16_LO:
2396 case elfcpp::R_POWERPC_GOT16_LO:
2397 case elfcpp::R_POWERPC_SECTOFF_LO:
2398 Reloc::addr16_lo(view, value, 0);
2401 case elfcpp::R_POWERPC_ADDR16_HI:
2402 case elfcpp::R_PPC64_TOC16_HI:
2403 case elfcpp::R_POWERPC_GOT16_HI:
2404 case elfcpp::R_POWERPC_SECTOFF_HI:
2405 Reloc::addr16_hi(view, value, 0);
2408 case elfcpp::R_POWERPC_ADDR16_HA:
2409 case elfcpp::R_PPC64_TOC16_HA:
2410 case elfcpp::R_POWERPC_GOT16_HA:
2411 case elfcpp::R_POWERPC_SECTOFF_HA:
2412 Reloc::addr16_ha(view, value, 0);
2415 case elfcpp::R_POWERPC_REL16_LO:
2416 Reloc::rel16_lo(view, value, 0, address);
2419 case elfcpp::R_POWERPC_REL16_HI:
2420 Reloc::rel16_hi(view, value, 0, address);
2423 case elfcpp::R_POWERPC_REL16_HA:
2424 Reloc::rel16_ha(view, value, 0, address);
2427 case elfcpp::R_PPC64_ADDR16_DS:
2428 case elfcpp::R_PPC64_ADDR16_LO_DS:
2429 case elfcpp::R_PPC64_TOC16_DS:
2430 case elfcpp::R_PPC64_TOC16_LO_DS:
2431 case elfcpp::R_PPC64_GOT16_DS:
2432 case elfcpp::R_PPC64_GOT16_LO_DS:
2433 case elfcpp::R_PPC64_SECTOFF_DS:
2434 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2435 Reloc::addr16_ds(view, value, 0);
2438 case elfcpp::R_POWERPC_COPY:
2439 case elfcpp::R_POWERPC_GLOB_DAT:
2440 case elfcpp::R_POWERPC_JMP_SLOT:
2441 case elfcpp::R_POWERPC_RELATIVE:
2442 // This is an outstanding tls reloc, which is unexpected when
2444 case elfcpp::R_POWERPC_DTPMOD:
2445 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2446 _("unexpected reloc %u in object file"),
2450 case elfcpp::R_PPC64_TOCSAVE:
2451 // For the time being this can be ignored.
2455 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2456 _("unsupported reloc %u"),
2464 // Perform a TLS relocation.
2466 template<int size, bool big_endian>
2468 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
2469 const Relocate_info<size, big_endian>* relinfo,
2470 Target_powerpc<size, big_endian>* target,
2472 const elfcpp::Rela<size, big_endian>& rela,
2473 unsigned int r_type,
2474 const Sized_symbol<size>* gsym,
2475 const Symbol_value<size>* psymval,
2476 unsigned char* view,
2477 typename elfcpp::Elf_types<size>::Elf_Addr address,
2480 Output_segment* tls_segment = relinfo->layout->tls_segment();
2481 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2483 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2484 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2486 const bool is_final =
2488 ? !parameters->options().output_is_position_independent()
2489 : gsym->final_value_is_known());
2497 // Relocate section data.
2499 template<int size, bool big_endian>
2501 Target_powerpc<size, big_endian>::relocate_section(
2502 const Relocate_info<size, big_endian>* relinfo,
2503 unsigned int sh_type,
2504 const unsigned char* prelocs,
2506 Output_section* output_section,
2507 bool needs_special_offset_handling,
2508 unsigned char* view,
2509 typename elfcpp::Elf_types<size>::Elf_Addr address,
2510 section_size_type view_size,
2511 const Reloc_symbol_changes* reloc_symbol_changes)
2513 typedef Target_powerpc<size, big_endian> Powerpc;
2514 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2516 gold_assert(sh_type == elfcpp::SHT_RELA);
2518 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2525 needs_special_offset_handling,
2529 reloc_symbol_changes);
2532 class Powerpc_scan_relocatable_reloc
2535 // Return the strategy to use for a local symbol which is not a
2536 // section symbol, given the relocation type.
2537 inline Relocatable_relocs::Reloc_strategy
2538 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
2540 if (r_type == 0 && r_sym == 0)
2541 return Relocatable_relocs::RELOC_DISCARD;
2542 return Relocatable_relocs::RELOC_COPY;
2545 // Return the strategy to use for a local symbol which is a section
2546 // symbol, given the relocation type.
2547 inline Relocatable_relocs::Reloc_strategy
2548 local_section_strategy(unsigned int, Relobj*)
2550 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2553 // Return the strategy to use for a global symbol, given the
2554 // relocation type, the object, and the symbol index.
2555 inline Relocatable_relocs::Reloc_strategy
2556 global_strategy(unsigned int r_type, Relobj*, unsigned int)
2558 if (r_type == elfcpp::R_PPC_PLTREL24)
2559 return Relocatable_relocs::RELOC_SPECIAL;
2560 return Relocatable_relocs::RELOC_COPY;
2564 // Scan the relocs during a relocatable link.
2566 template<int size, bool big_endian>
2568 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2569 Symbol_table* symtab,
2571 Sized_relobj_file<size, big_endian>* object,
2572 unsigned int data_shndx,
2573 unsigned int sh_type,
2574 const unsigned char* prelocs,
2576 Output_section* output_section,
2577 bool needs_special_offset_handling,
2578 size_t local_symbol_count,
2579 const unsigned char* plocal_symbols,
2580 Relocatable_relocs* rr)
2582 gold_assert(sh_type == elfcpp::SHT_RELA);
2584 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2585 Powerpc_scan_relocatable_reloc>(
2593 needs_special_offset_handling,
2599 // Relocate a section during a relocatable link.
2601 template<int size, bool big_endian>
2603 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2604 const Relocate_info<size, big_endian>* relinfo,
2605 unsigned int sh_type,
2606 const unsigned char* prelocs,
2608 Output_section* output_section,
2609 off_t offset_in_output_section,
2610 const Relocatable_relocs* rr,
2612 typename elfcpp::Elf_types<size>::Elf_Addr,
2614 unsigned char* reloc_view,
2615 section_size_type reloc_view_size)
2617 gold_assert(sh_type == elfcpp::SHT_RELA);
2619 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2620 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
2622 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
2624 const int reloc_size
2625 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2626 const Address invalid_address = static_cast<Address>(0) - 1;
2628 Powerpc_relobj<size, big_endian>* const object
2629 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
2630 const unsigned int local_count = object->local_symbol_count();
2631 unsigned int got2_shndx = object->got2_shndx();
2632 typename elfcpp::Elf_types<size>::Elf_Swxword got2_addend = 0;
2633 if (got2_shndx != 0)
2634 got2_addend = object->get_output_section_offset(got2_shndx);
2636 unsigned char* pwrite = reloc_view;
2638 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2640 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
2641 if (strategy == Relocatable_relocs::RELOC_DISCARD)
2644 Reltype reloc(prelocs);
2645 Reltype_write reloc_write(pwrite);
2647 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2648 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2649 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2651 // Get the new symbol index.
2653 unsigned int new_symndx;
2654 if (r_sym < local_count)
2658 case Relocatable_relocs::RELOC_COPY:
2659 case Relocatable_relocs::RELOC_SPECIAL:
2660 new_symndx = object->symtab_index(r_sym);
2661 gold_assert(new_symndx != -1U);
2664 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
2666 // We are adjusting a section symbol. We need to find
2667 // the symbol table index of the section symbol for
2668 // the output section corresponding to input section
2669 // in which this symbol is defined.
2670 gold_assert(r_sym < local_count);
2672 unsigned int shndx =
2673 object->local_symbol_input_shndx(r_sym, &is_ordinary);
2674 gold_assert(is_ordinary);
2675 Output_section* os = object->output_section(shndx);
2676 gold_assert(os != NULL);
2677 gold_assert(os->needs_symtab_index());
2678 new_symndx = os->symtab_index();
2688 const Symbol* gsym = object->global_symbol(r_sym);
2689 gold_assert(gsym != NULL);
2690 if (gsym->is_forwarder())
2691 gsym = relinfo->symtab->resolve_forwards(gsym);
2693 gold_assert(gsym->has_symtab_index());
2694 new_symndx = gsym->symtab_index();
2697 // Get the new offset--the location in the output section where
2698 // this relocation should be applied.
2700 Address offset = reloc.get_r_offset();
2702 if (static_cast<Address>(offset_in_output_section) != invalid_address)
2703 new_offset = offset + offset_in_output_section;
2706 section_offset_type sot_offset =
2707 convert_types<section_offset_type, Address>(offset);
2708 section_offset_type new_sot_offset =
2709 output_section->output_offset(object, relinfo->data_shndx,
2711 gold_assert(new_sot_offset != -1);
2712 new_offset = new_sot_offset;
2715 reloc_write.put_r_offset(new_offset);
2716 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
2718 // Handle the reloc addend based on the strategy.
2719 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
2720 addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2721 get_reloc_addend(&reloc);
2723 if (strategy == Relocatable_relocs::RELOC_COPY)
2725 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
2727 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
2729 addend = psymval->value(object, addend);
2731 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
2733 if (addend >= 32768)
2734 addend += got2_addend;
2739 Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2740 set_reloc_addend(&reloc_write, addend);
2742 pwrite += reloc_size;
2745 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
2746 == reloc_view_size);
2749 // Return the value to use for a dynamic which requires special
2750 // treatment. This is how we support equality comparisons of function
2751 // pointers across shared library boundaries, as described in the
2752 // processor specific ABI supplement.
2754 template<int size, bool big_endian>
2756 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2760 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2761 return this->plt_section()->address() + gsym->plt_offset();
2767 // The selector for powerpc object files.
2769 template<int size, bool big_endian>
2770 class Target_selector_powerpc : public Target_selector
2773 Target_selector_powerpc()
2774 : Target_selector(elfcpp::EM_NONE, size, big_endian,
2776 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2777 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2779 ? (big_endian ? "elf64ppc" : "elf64lppc")
2780 : (big_endian ? "elf32ppc" : "elf32lppc")))
2784 do_recognize(Input_file*, off_t, int machine, int, int)
2789 if (machine != elfcpp::EM_PPC64)
2794 if (machine != elfcpp::EM_PPC)
2802 return this->instantiate_target();
2806 do_instantiate_target()
2807 { return new Target_powerpc<size, big_endian>(); }
2810 Target_selector_powerpc<32, true> target_selector_ppc32;
2811 Target_selector_powerpc<32, false> target_selector_ppc32le;
2812 Target_selector_powerpc<64, true> target_selector_ppc64;
2813 Target_selector_powerpc<64, false> target_selector_ppc64le;
2815 } // End anonymous namespace.