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 set_got2_shndx(unsigned int shndx)
82 this->got2_section_ = shndx;
88 do_find_special_sections(Read_symbols_data* sd);
91 unsigned int got2_section_;
94 template<int size, bool big_endian>
95 class Target_powerpc : public Sized_target<size, big_endian>
98 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
101 : Sized_target<size, big_endian>(&powerpc_info),
102 got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
103 copy_relocs_(elfcpp::R_POWERPC_COPY),
104 dynbss_(NULL), got_mod_index_offset_(-1U)
108 // Process the relocations to determine unreferenced sections for
109 // garbage collection.
111 gc_process_relocs(Symbol_table* symtab,
113 Sized_relobj_file<size, big_endian>* object,
114 unsigned int data_shndx,
115 unsigned int sh_type,
116 const unsigned char* prelocs,
118 Output_section* output_section,
119 bool needs_special_offset_handling,
120 size_t local_symbol_count,
121 const unsigned char* plocal_symbols);
123 // Scan the relocations to look for symbol adjustments.
125 scan_relocs(Symbol_table* symtab,
127 Sized_relobj_file<size, big_endian>* object,
128 unsigned int data_shndx,
129 unsigned int sh_type,
130 const unsigned char* prelocs,
132 Output_section* output_section,
133 bool needs_special_offset_handling,
134 size_t local_symbol_count,
135 const unsigned char* plocal_symbols);
136 // Finalize the sections.
138 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
140 // Return the value to use for a dynamic which requires special
143 do_dynsym_value(const Symbol*) const;
145 // Relocate a section.
147 relocate_section(const Relocate_info<size, big_endian>*,
148 unsigned int sh_type,
149 const unsigned char* prelocs,
151 Output_section* output_section,
152 bool needs_special_offset_handling,
154 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
155 section_size_type view_size,
156 const Reloc_symbol_changes*);
158 // Scan the relocs during a relocatable link.
160 scan_relocatable_relocs(Symbol_table* symtab,
162 Sized_relobj_file<size, big_endian>* object,
163 unsigned int data_shndx,
164 unsigned int sh_type,
165 const unsigned char* prelocs,
167 Output_section* output_section,
168 bool needs_special_offset_handling,
169 size_t local_symbol_count,
170 const unsigned char* plocal_symbols,
171 Relocatable_relocs*);
173 // Relocate a section during a relocatable link.
175 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
176 unsigned int sh_type,
177 const unsigned char* prelocs,
179 Output_section* output_section,
180 off_t offset_in_output_section,
181 const Relocatable_relocs*,
183 typename elfcpp::Elf_types<size>::Elf_Addr,
185 unsigned char* reloc_view,
186 section_size_type reloc_view_size);
188 // Return whether SYM is defined by the ABI.
190 do_is_defined_by_abi(const Symbol* sym) const
192 return strcmp(sym->name(), "__tls_get_addr") == 0;
195 // Return the size of the GOT section.
199 gold_assert(this->got_ != NULL);
200 return this->got_->data_size();
203 // Get the PLT section.
204 const Output_data_plt_powerpc<size, big_endian>*
207 gold_assert(this->plt_ != NULL);
211 // Get the .glink section.
212 const Output_data_glink<size, big_endian>*
213 glink_section() const
215 gold_assert(this->glink_ != NULL);
219 // Get the GOT section.
220 const Output_data_got_powerpc<size, big_endian>*
223 gold_assert(this->got_ != NULL);
229 do_make_elf_object(const std::string&, Input_file*, off_t,
230 const elfcpp::Ehdr<size, big_endian>&);
232 // Return the number of entries in the GOT.
234 got_entry_count() const
236 if (this->got_ == NULL)
238 return this->got_size() / (size / 8);
241 // Return the number of entries in the PLT.
243 plt_entry_count() const;
245 // Return the offset of the first non-reserved PLT entry.
247 first_plt_entry_offset() const;
249 // Return the size of each PLT entry.
251 plt_entry_size() const;
255 // The class which scans relocations.
260 : issued_non_pic_error_(false)
264 get_reference_flags(unsigned int r_type);
267 local(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,
272 const elfcpp::Sym<size, big_endian>& lsym);
275 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
276 Sized_relobj_file<size, big_endian>* object,
277 unsigned int data_shndx,
278 Output_section* output_section,
279 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
283 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
285 Sized_relobj_file<size, big_endian>* ,
288 const elfcpp::Rela<size, big_endian>& ,
290 const elfcpp::Sym<size, big_endian>&)
294 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
296 Sized_relobj_file<size, big_endian>* ,
299 const elfcpp::Rela<size,
301 unsigned int , Symbol*)
306 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
307 unsigned int r_type);
310 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
311 unsigned int r_type, Symbol*);
314 generate_tls_call(Symbol_table* symtab, Layout* layout,
315 Target_powerpc* target);
318 check_non_pic(Relobj*, unsigned int r_type);
320 // Whether we have issued an error about a non-PIC compilation.
321 bool issued_non_pic_error_;
324 // The class which implements relocation.
328 // Do a relocation. Return false if the caller should not issue
329 // any warnings about this relocation.
331 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
332 Output_section*, size_t relnum,
333 const elfcpp::Rela<size, big_endian>&,
334 unsigned int r_type, const Sized_symbol<size>*,
335 const Symbol_value<size>*,
337 typename elfcpp::Elf_types<size>::Elf_Addr,
341 // Do a TLS relocation.
343 relocate_tls(const Relocate_info<size, big_endian>*,
344 Target_powerpc* target,
345 size_t relnum, const elfcpp::Rela<size, big_endian>&,
346 unsigned int r_type, const Sized_symbol<size>*,
347 const Symbol_value<size>*,
349 typename elfcpp::Elf_types<size>::Elf_Addr,
353 // A class which returns the size required for a relocation type,
354 // used while scanning relocs during a relocatable link.
355 class Relocatable_size_for_reloc
359 get_size_for_reloc(unsigned int, Relobj*)
366 // Adjust TLS relocation type based on the options and whether this
367 // is a local symbol.
368 static tls::Tls_optimization
369 optimize_tls_reloc(bool is_final, int r_type);
371 // Get the GOT section, creating it if necessary.
372 Output_data_got_powerpc<size, big_endian>*
373 got_section(Symbol_table*, Layout*);
377 make_glink_section(Layout*);
379 // Create the PLT section.
381 make_plt_section(Layout*);
383 // Create a PLT entry for a global symbol.
385 make_plt_entry(Layout*, Symbol*,
386 const elfcpp::Rela<size, big_endian>&,
387 const Sized_relobj<size, big_endian>* object);
389 // Create a GOT entry for the TLS module index.
391 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
392 Sized_relobj_file<size, big_endian>* object);
394 // Get the dynamic reloc section, creating it if necessary.
396 rela_dyn_section(Layout*);
398 // Copy a relocation against a global symbol.
400 copy_reloc(Symbol_table* symtab, Layout* layout,
401 Sized_relobj_file<size, big_endian>* object,
402 unsigned int shndx, Output_section* output_section,
403 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
405 this->copy_relocs_.copy_reloc(symtab, layout,
406 symtab->get_sized_symbol<size>(sym),
407 object, shndx, output_section,
408 reloc, this->rela_dyn_section(layout));
411 // Information about this specific target which we pass to the
412 // general Target structure.
413 static Target::Target_info powerpc_info;
415 // The types of GOT entries needed for this platform.
416 // These values are exposed to the ABI in an incremental link.
417 // Do not renumber existing values without changing the version
418 // number of the .gnu_incremental_inputs section.
421 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
422 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
423 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
426 // The GOT output section.
427 Output_data_got_powerpc<size, big_endian>* got_;
428 // The PLT output section.
429 Output_data_plt_powerpc<size, big_endian>* plt_;
430 // The .glink output section.
431 Output_data_glink<size, big_endian>* glink_;
432 // The dynamic reloc output section.
433 Reloc_section* rela_dyn_;
434 // Relocs saved to avoid a COPY reloc.
435 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
436 // Space for variables copied with a COPY reloc.
437 Output_data_space* dynbss_;
438 // Offset of the GOT entry for the TLS module index;
439 unsigned int got_mod_index_offset_;
443 Target::Target_info Target_powerpc<32, true>::powerpc_info =
446 true, // is_big_endian
447 elfcpp::EM_PPC, // machine_code
448 false, // has_make_symbol
449 false, // has_resolve
450 false, // has_code_fill
451 true, // is_default_stack_executable
452 false, // can_icf_inline_merge_sections
454 "/usr/lib/ld.so.1", // dynamic_linker
455 0x10000000, // default_text_segment_address
456 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
457 4 * 1024, // common_pagesize (overridable by -z common-page-size)
458 false, // isolate_execinstr
460 elfcpp::SHN_UNDEF, // small_common_shndx
461 elfcpp::SHN_UNDEF, // large_common_shndx
462 0, // small_common_section_flags
463 0, // large_common_section_flags
464 NULL, // attributes_section
465 NULL // attributes_vendor
469 Target::Target_info Target_powerpc<32, false>::powerpc_info =
472 false, // is_big_endian
473 elfcpp::EM_PPC, // machine_code
474 false, // has_make_symbol
475 false, // has_resolve
476 false, // has_code_fill
477 true, // is_default_stack_executable
478 false, // can_icf_inline_merge_sections
480 "/usr/lib/ld.so.1", // dynamic_linker
481 0x10000000, // default_text_segment_address
482 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
483 4 * 1024, // common_pagesize (overridable by -z common-page-size)
484 false, // isolate_execinstr
486 elfcpp::SHN_UNDEF, // small_common_shndx
487 elfcpp::SHN_UNDEF, // large_common_shndx
488 0, // small_common_section_flags
489 0, // large_common_section_flags
490 NULL, // attributes_section
491 NULL // attributes_vendor
495 Target::Target_info Target_powerpc<64, true>::powerpc_info =
498 true, // is_big_endian
499 elfcpp::EM_PPC64, // machine_code
500 false, // has_make_symbol
501 false, // has_resolve
502 false, // has_code_fill
503 true, // is_default_stack_executable
504 false, // can_icf_inline_merge_sections
506 "/usr/lib/ld.so.1", // dynamic_linker
507 0x10000000, // default_text_segment_address
508 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
509 8 * 1024, // common_pagesize (overridable by -z common-page-size)
510 false, // isolate_execinstr
512 elfcpp::SHN_UNDEF, // small_common_shndx
513 elfcpp::SHN_UNDEF, // large_common_shndx
514 0, // small_common_section_flags
515 0, // large_common_section_flags
516 NULL, // attributes_section
517 NULL // attributes_vendor
521 Target::Target_info Target_powerpc<64, false>::powerpc_info =
524 false, // is_big_endian
525 elfcpp::EM_PPC64, // machine_code
526 false, // has_make_symbol
527 false, // has_resolve
528 false, // has_code_fill
529 true, // is_default_stack_executable
530 false, // can_icf_inline_merge_sections
532 "/usr/lib/ld.so.1", // dynamic_linker
533 0x10000000, // default_text_segment_address
534 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
535 8 * 1024, // common_pagesize (overridable by -z common-page-size)
536 false, // isolate_execinstr
538 elfcpp::SHN_UNDEF, // small_common_shndx
539 elfcpp::SHN_UNDEF, // large_common_shndx
540 0, // small_common_section_flags
541 0, // large_common_section_flags
542 NULL, // attributes_section
543 NULL // attributes_vendor
546 template<int size, bool big_endian>
547 class Powerpc_relocate_functions
550 // Do a simple RELA relocation
551 template<int valsize>
553 rela(unsigned char* view,
554 unsigned int right_shift,
555 elfcpp::Elf_Xword dst_mask,
556 typename elfcpp::Swap<size, big_endian>::Valtype value,
557 typename elfcpp::Swap<size, big_endian>::Valtype addend)
559 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
560 Valtype* wv = reinterpret_cast<Valtype*>(view);
561 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
562 Valtype reloc = (value + addend) >> right_shift;
567 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
570 // Do a simple RELA relocation, unaligned.
571 template<int valsize>
573 rela_ua(unsigned char* view,
574 unsigned int right_shift,
575 elfcpp::Elf_Xword dst_mask,
576 typename elfcpp::Swap<size, big_endian>::Valtype value,
577 typename elfcpp::Swap<size, big_endian>::Valtype addend)
579 typedef typename elfcpp::Swap_unaligned<valsize,
580 big_endian>::Valtype Valtype;
581 Valtype* wv = reinterpret_cast<Valtype*>(view);
582 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
583 Valtype reloc = (value + addend) >> right_shift;
588 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
591 typedef Powerpc_relocate_functions<size, big_endian> This;
592 typedef Relocate_functions<size, big_endian> This_reloc;
594 // R_POWERPC_REL32: (Symbol + Addend - Address)
596 rel32(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)
600 { This_reloc::pcrela32(view, value, addend, address); }
602 // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
604 rel24(unsigned char* view,
605 typename elfcpp::Elf_types<size>::Elf_Addr value,
606 typename elfcpp::Elf_types<size>::Elf_Addr addend,
607 typename elfcpp::Elf_types<size>::Elf_Addr address)
609 This::template rela<32>(view, 0, 0x03fffffc, value - address, addend);
612 // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
614 rel14(unsigned char* view,
615 typename elfcpp::Elf_types<size>::Elf_Addr value,
616 typename elfcpp::Elf_types<size>::Elf_Addr addend,
617 typename elfcpp::Elf_types<size>::Elf_Addr address)
619 This::template rela<32>(view, 0, 0xfffc, value - address, addend);
622 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
624 addr16(unsigned char* view,
625 typename elfcpp::Elf_types<size>::Elf_Addr value,
626 typename elfcpp::Elf_types<size>::Elf_Addr addend)
627 { This_reloc::rela16(view, value, 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); }
645 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
647 addr16_hi(unsigned char* view,
648 typename elfcpp::Elf_types<size>::Elf_Addr value,
649 typename elfcpp::Elf_types<size>::Elf_Addr addend)
651 This::template rela<16>(view, 16, 0xffff, value, addend);
654 // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
655 // final value of the low 16 bits of the
656 // relocation is negative, add one.
658 addr16_ha(unsigned char* view,
659 typename elfcpp::Elf_types<size>::Elf_Addr value,
660 typename elfcpp::Elf_types<size>::Elf_Addr addend)
662 This::addr16_hi(view, value + 0x8000, addend);
665 // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
667 rel16(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_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
675 rel16_lo(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)
679 { This_reloc::pcrela16(view, value, addend, address); }
681 // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
683 rel16_hi(unsigned char* view,
684 typename elfcpp::Elf_types<size>::Elf_Addr value,
685 typename elfcpp::Elf_types<size>::Elf_Addr addend,
686 typename elfcpp::Elf_types<size>::Elf_Addr address)
688 This::template rela<16>(view, 16, 0xffff, value - address, addend);
691 // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
692 // final value of the low 16 bits of the
693 // relocation is negative, add one.
695 rel16_ha(unsigned char* view,
696 typename elfcpp::Elf_types<size>::Elf_Addr value,
697 typename elfcpp::Elf_types<size>::Elf_Addr addend,
698 typename elfcpp::Elf_types<size>::Elf_Addr address)
700 This::rel16_hi(view, value + 0x8000, addend, address);
704 // Stash away the index of .got2 in a relocatable object, if such
707 template<int size, bool big_endian>
709 Powerpc_relobj<size, big_endian>::do_find_special_sections(
710 Read_symbols_data* sd)
714 const unsigned char* const pshdrs = sd->section_headers->data();
715 const unsigned char* namesu = sd->section_names->data();
716 const char* names = reinterpret_cast<const char*>(namesu);
717 section_size_type names_size = sd->section_names_size;
718 const unsigned char* s;
720 s = this->find_shdr(pshdrs, ".got2", names, names_size, NULL);
723 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
724 this->set_got2_shndx(ndx);
727 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
730 // Set up PowerPC target specific relobj.
732 template<int size, bool big_endian>
734 Target_powerpc<size, big_endian>::do_make_elf_object(
735 const std::string& name,
736 Input_file* input_file,
737 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
739 int et = ehdr.get_e_type();
740 if (et == elfcpp::ET_REL)
742 Powerpc_relobj<size, big_endian>* obj =
743 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
747 else if (et == elfcpp::ET_DYN)
749 Sized_dynobj<size, big_endian>* obj =
750 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
756 gold_error(_("%s: unsupported ELF file type %d"),
762 template<int size, bool big_endian>
763 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
766 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
767 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
769 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
770 : Output_data_got<size, big_endian>(),
771 symtab_(symtab), layout_(layout),
772 header_ent_cnt_(size == 32 ? 3 : 1),
773 header_index_(size == 32 ? 0x2000 : 0)
778 // Create a new GOT entry and return its offset.
780 add_got_entry(Got_entry got_entry)
783 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
786 // Create a pair of new GOT entries and return the offset of the first.
788 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
790 this->reserve_ent(2);
791 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
795 // Value of _GLOBAL_OFFSET_TABLE_
799 return this->got_offset(this->header_index_);
802 // Ensure our GOT has a header.
804 set_final_data_size()
806 if (this->header_ent_cnt_ != 0)
808 Output_data_got<size, big_endian>::set_final_data_size();
811 // First word of GOT header needs some values that are not
812 // handled by Output_data_got so poke them in here.
813 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
815 do_write(Output_file* of)
817 replace_constant(this->header_index_,
819 ? this->layout_->dynamic_section()->address()
820 : this->address() + 0x8000));
822 Output_data_got<size, big_endian>::do_write(of);
827 reserve_ent(unsigned int cnt = 1)
829 if (this->header_ent_cnt_ == 0)
831 if (this->num_entries() + cnt > this->header_index_)
838 this->header_ent_cnt_ = 0;
839 this->header_index_ = this->num_entries();
842 Output_data_got<size, big_endian>::add_constant(0);
843 Output_data_got<size, big_endian>::add_constant(0);
844 Output_data_got<size, big_endian>::add_constant(0);
846 // Define _GLOBAL_OFFSET_TABLE_ at the header
847 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
848 Symbol_table::PREDEFINED,
849 this, this->g_o_t(), 0,
856 Output_data_got<size, big_endian>::add_constant(0);
860 Symbol_table* symtab_;
864 unsigned int header_ent_cnt_;
866 unsigned int header_index_;
869 // Get the GOT section, creating it if necessary.
871 template<int size, bool big_endian>
872 Output_data_got_powerpc<size, big_endian>*
873 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
876 if (this->got_ == NULL)
878 gold_assert(symtab != NULL && layout != NULL);
881 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
883 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
884 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
885 this->got_, ORDER_DATA, false);
891 // Get the dynamic reloc section, creating it if necessary.
893 template<int size, bool big_endian>
894 typename Target_powerpc<size, big_endian>::Reloc_section*
895 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
897 if (this->rela_dyn_ == NULL)
899 gold_assert(layout != NULL);
900 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
901 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
902 elfcpp::SHF_ALLOC, this->rela_dyn_,
903 ORDER_DYNAMIC_RELOCS, false);
905 return this->rela_dyn_;
908 // A class to handle the PLT data.
910 template<int size, bool big_endian>
911 class Output_data_plt_powerpc : public Output_section_data_build
914 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
915 size, big_endian> Reloc_section;
917 Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
919 // Add an entry to the PLT.
923 // Return the .rela.plt section data.
930 // Return the number of PLT entries.
933 { return (this->current_data_size() - initial_plt_entry_size) / plt_entry_size; }
935 // Return the offset of the first non-reserved PLT entry.
937 first_plt_entry_offset()
938 { return initial_plt_entry_size; }
940 // Return the size of a PLT entry.
943 { return plt_entry_size; }
947 do_adjust_output_section(Output_section* os)
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(Layout* layout,
972 Target_powerpc<size, big_endian>* targ)
973 : Output_section_data_build(size == 32 ? 4 : 8),
976 this->rel_ = new Reloc_section(false);
977 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
978 elfcpp::SHF_ALLOC, this->rel_,
979 ORDER_DYNAMIC_PLT_RELOCS, false);
982 // Add an entry to the PLT.
984 template<int size, bool big_endian>
986 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
988 if (!gsym->has_plt_offset())
990 off_t off = this->current_data_size();
993 off += initial_plt_entry_size;
994 gsym->set_plt_offset(off);
995 gsym->set_needs_dynsym_entry();
996 this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
997 off += plt_entry_size;
998 this->set_current_data_size(off);
1002 static const unsigned int addis_11_11 = 0x3d6b0000;
1003 static const unsigned int addis_11_30 = 0x3d7e0000;
1004 static const unsigned int addis_12_12 = 0x3d8c0000;
1005 static const unsigned int addi_11_11 = 0x396b0000;
1006 static const unsigned int add_0_11_11 = 0x7c0b5a14;
1007 static const unsigned int add_11_0_11 = 0x7d605a14;
1008 static const unsigned int b = 0x48000000;
1009 static const unsigned int bcl_20_31 = 0x429f0005;
1010 static const unsigned int bctr = 0x4e800420;
1011 static const unsigned int blrl = 0x4e800021;
1012 static const unsigned int lis_11 = 0x3d600000;
1013 static const unsigned int lis_12 = 0x3d800000;
1014 static const unsigned int lwzu_0_12 = 0x840c0000;
1015 static const unsigned int lwz_0_12 = 0x800c0000;
1016 static const unsigned int lwz_11_11 = 0x816b0000;
1017 static const unsigned int lwz_11_30 = 0x817e0000;
1018 static const unsigned int lwz_12_12 = 0x818c0000;
1019 static const unsigned int mflr_0 = 0x7c0802a6;
1020 static const unsigned int mflr_12 = 0x7d8802a6;
1021 static const unsigned int mtctr_0 = 0x7c0903a6;
1022 static const unsigned int mtctr_11 = 0x7d6903a6;
1023 static const unsigned int mtlr_0 = 0x7c0803a6;
1024 static const unsigned int nop = 0x60000000;
1025 static const unsigned int sub_11_11_12 = 0x7d6c5850;
1026 static const unsigned int addis_12_2 = 0x3d820000;
1027 static const unsigned int std_2_1 = 0xf8410000;
1028 static const unsigned int ld_11_12 = 0xe96c0000;
1029 static const unsigned int ld_2_12 = 0xe84c0000;
1030 static const unsigned int addi_12_12 = 0x398c0000;
1031 static const unsigned int ld_11_2 = 0xe9620000;
1032 static const unsigned int addi_2_2 = 0x38420000;
1033 static const unsigned int ld_2_2 = 0xe8420000;
1034 static const unsigned int mflr_11 = 0x7d6802a6;
1035 static const unsigned int ld_2_11 = 0xe84b0000;
1036 static const unsigned int mtlr_12 = 0x7d8803a6;
1037 static const unsigned int add_12_2_11 = 0x7d825a14;
1038 static const unsigned int li_0_0 = 0x38000000;
1039 static const unsigned int lis_0_0 = 0x3c000000;
1040 static const unsigned int ori_0_0_0 = 0x60000000;
1042 // Write out the PLT.
1044 template<int size, bool big_endian>
1046 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1050 const off_t offset = this->offset();
1051 const section_size_type oview_size
1052 = convert_to_section_size_type(this->data_size());
1053 unsigned char* const oview = of->get_output_view(offset, oview_size);
1054 unsigned char* pov = oview;
1055 unsigned char* endpov = oview + oview_size;
1057 // The address the .glink branch table
1058 const Output_data_glink<size, big_endian>* glink
1059 = this->targ_->glink_section();
1060 elfcpp::Elf_types<32>::Elf_Addr branch_tab
1061 = glink->address() + glink->pltresolve();
1063 while (pov < endpov)
1065 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1070 of->write_output_view(offset, oview_size, oview);
1074 // Create the PLT section.
1076 template<int size, bool big_endian>
1078 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1080 if (this->plt_ == NULL)
1082 if (this->glink_ == NULL)
1083 make_glink_section(layout);
1085 // Ensure that .rela.dyn always appears before .rela.plt This is
1086 // necessary due to how, on PowerPC and some other targets, .rela.dyn
1087 // needs to include .rela.plt in it's range.
1088 this->rela_dyn_section(layout);
1090 this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1091 layout->add_output_section_data(".plt",
1093 ? elfcpp::SHT_PROGBITS
1094 : elfcpp::SHT_NOBITS),
1095 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1104 // A class to handle .glink.
1106 template<int size, bool big_endian>
1107 class Output_data_glink : public Output_section_data
1110 Output_data_glink(Target_powerpc<size, big_endian>*);
1114 add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1115 const Sized_relobj<size, big_endian>*, unsigned int);
1118 find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1119 const Sized_relobj<size, big_endian>*, unsigned int) const;
1122 glink_entry_size() const
1127 // FIXME: We should be using multiple glink sections for
1128 // stubs to support > 33M applications.
1135 return this->pltresolve_;
1139 static const int pltresolve_size = 16*4;
1142 set_final_data_size();
1146 do_write(Output_file*);
1148 struct Glink_sym_ent
1150 Glink_sym_ent(const Symbol *sym,
1151 const elfcpp::Rela<size, big_endian>& reloc,
1152 const Sized_relobj<size, big_endian>* object,
1154 : sym_(sym), object_(0), shndx_(0), addend_(0)
1157 this->addend_ = reloc.get_r_addend();
1158 else if (parameters->options().output_is_position_independent())
1160 if (object != NULL && shndx != 0)
1161 this->addend_ = reloc.get_r_addend();
1162 if (this->addend_ != 0)
1164 this->object_ = object;
1165 this->shndx_ = shndx;
1171 const Sized_relobj<size, big_endian>* object_;
1172 unsigned int shndx_;
1173 unsigned int addend_;
1175 bool operator==(const Glink_sym_ent& that) const
1177 return (this->sym_ == that.sym_
1178 && this->object_ == that.object_
1179 && this->shndx_ == that.shndx_
1180 && this->addend_ == that.addend_);
1184 struct Glink_sym_ent_hash
1186 size_t operator()(const Glink_sym_ent& ent) const
1188 return (reinterpret_cast<uintptr_t>(ent.sym_)
1189 ^ reinterpret_cast<uintptr_t>(ent.object_)
1195 // Set of sym/shndx/addend entries.
1196 typedef Unordered_map<Glink_sym_ent, unsigned int,
1197 Glink_sym_ent_hash> Glink_entries;
1198 Glink_entries glink_entries_;
1200 // Offset of pltresolve stub (actually, branch table for 32-bit)
1203 // Allows access to .got and .plt for do_write.
1204 Target_powerpc<size, big_endian>* targ_;
1207 // Create the glink section.
1209 template<int size, bool big_endian>
1210 Output_data_glink<size, big_endian>::Output_data_glink(Target_powerpc<size, big_endian>* targ)
1211 : Output_section_data(16),
1212 pltresolve_(0), targ_(targ)
1216 // Add an entry to glink, if we do not already have one for this
1217 // sym/addend/shndx combo.
1219 template<int size, bool big_endian>
1221 Output_data_glink<size, big_endian>
1222 ::add_entry(const Symbol* gsym,
1223 const elfcpp::Rela<size, big_endian>& reloc,
1224 const Sized_relobj<size, big_endian>* object,
1227 Glink_sym_ent ent(gsym, reloc, object, shndx);
1228 unsigned int indx = this->glink_entries_.size();
1229 this->glink_entries_[ent] = indx;
1232 template<int size, bool big_endian>
1234 Output_data_glink<size, big_endian>
1235 ::find_entry(const Symbol* gsym,
1236 const elfcpp::Rela<size, big_endian>& reloc,
1237 const Sized_relobj<size, big_endian>* object,
1238 unsigned int shndx) const
1240 Glink_sym_ent ent(gsym, reloc, object, shndx);
1241 typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1242 gold_assert(p != this->glink_entries_.end());
1246 template<int size, bool big_endian>
1248 Output_data_glink<size, big_endian>::set_final_data_size()
1250 unsigned int count = this->glink_entries_.size();
1251 off_t total = count;
1258 this->pltresolve_ = total;
1260 // space for branch table
1261 total += 4 * (count - 1);
1263 total += -total & 15;
1264 total += this->pltresolve_size;
1269 this->pltresolve_ = total;
1270 total += this->pltresolve_size;
1272 // space for branch table
1275 total += 4 * (count - 0x8000);
1279 this->set_data_size(total);
1282 static inline uint32_t
1288 static inline uint32_t
1294 static inline uint32_t
1297 return hi(a + 0x8000);
1300 template<bool big_endian>
1302 write_insn(unsigned char *p, uint32_t v)
1304 elfcpp::Swap<32, big_endian>::writeval(p, v);
1307 // Write out .glink.
1309 template<int size, bool big_endian>
1311 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1313 const off_t off = this->offset();
1314 const section_size_type oview_size =
1315 convert_to_section_size_type(this->data_size());
1316 unsigned char* const oview = of->get_output_view(off, oview_size);
1319 // The base address of the .plt section.
1320 uint32_t plt_base = this->targ_->plt_section()->address();
1322 // The address of _GLOBAL_OFFSET_TABLE_.
1323 const Output_data_got_powerpc<size, big_endian> *got;
1324 typename elfcpp::Elf_types<size>::Elf_Addr g_o_t;
1325 got = this->targ_->got_section();
1326 g_o_t = got->address() + got->g_o_t();
1330 // Write out call stubs.
1331 typename Glink_entries::const_iterator g;
1332 for (g = this->glink_entries_.begin();
1333 g != this->glink_entries_.end();
1336 uint64_t plt_addr = plt_base + g->first.sym_->plt_offset();
1337 uint64_t got_addr = g_o_t;
1338 uint64_t pltoff = plt_addr - got_addr;
1340 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1341 gold_error(_("%s: linkage table error against `%s'"),
1342 g->first.object_->name().c_str(),
1343 g->first.sym_->demangled_name().c_str());
1345 p = oview + g->second * this->glink_entry_size();
1346 if (ha(pltoff) != 0)
1348 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
1349 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1350 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
1351 if (ha(pltoff + 16) != ha(pltoff))
1353 write_insn<big_endian>(p, addi_12_12 + l(pltoff)), p += 4;
1356 write_insn<big_endian>(p, mtctr_11), p += 4;
1357 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
1358 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
1359 write_insn<big_endian>(p, bctr), p += 4;
1363 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
1364 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
1365 if (ha(pltoff + 16) != ha(pltoff))
1367 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
1370 write_insn<big_endian>(p, mtctr_11), p += 4;
1371 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
1372 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
1373 write_insn<big_endian>(p, bctr), p += 4;
1377 // Write pltresolve stub.
1378 p = oview + this->pltresolve_;
1379 uint64_t after_bcl = this->address() + this->pltresolve_ + 16;
1380 uint64_t pltoff = plt_base - after_bcl;
1382 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
1384 write_insn<big_endian>(p, mflr_12), p += 4;
1385 write_insn<big_endian>(p, bcl_20_31), p += 4;
1386 write_insn<big_endian>(p, mflr_11), p += 4;
1387 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
1388 write_insn<big_endian>(p, mtlr_12), p += 4;
1389 write_insn<big_endian>(p, add_12_2_11), p += 4;
1390 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
1391 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
1392 write_insn<big_endian>(p, mtctr_11), p += 4;
1393 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
1394 write_insn<big_endian>(p, bctr), p += 4;
1395 while (p < oview + this->pltresolve_ + this->pltresolve_size)
1396 write_insn<big_endian>(p, nop), p += 4;
1398 // Write lazy link call stubs.
1400 while (p < oview + oview_size)
1404 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
1408 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
1409 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
1411 uint16_t branch_off = this->pltresolve_ + 8 - (p - oview);
1412 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
1418 // Write out call stubs.
1419 typename Glink_entries::const_iterator g;
1420 for (g = this->glink_entries_.begin();
1421 g != this->glink_entries_.end();
1424 uint32_t plt_addr = plt_base + g->first.sym_->plt_offset();
1427 p = oview + g->second * this->glink_entry_size();
1428 if (parameters->options().output_is_position_independent())
1430 if (g->first.shndx_)
1431 got_addr = (g->first.object_->output_section(g->first.shndx_)->address()
1432 + g->first.object_->output_section_offset(g->first.shndx_)
1433 + g->first.addend_);
1437 uint32_t pltoff = plt_addr - got_addr;
1438 if (ha(pltoff) == 0)
1440 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
1441 write_insn<big_endian>(p + 4, mtctr_11);
1442 write_insn<big_endian>(p + 8, bctr);
1446 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
1447 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
1448 write_insn<big_endian>(p + 8, mtctr_11);
1449 write_insn<big_endian>(p + 12, bctr);
1454 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
1455 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
1456 write_insn<big_endian>(p + 8, mtctr_11);
1457 write_insn<big_endian>(p + 12, bctr);
1461 // Write out pltresolve branch table.
1462 p = oview + this->pltresolve_;
1463 unsigned int the_end = oview_size - this->pltresolve_size;
1464 unsigned char *end_p = oview + the_end;
1465 while (p < end_p - 8 * 4)
1466 write_insn<big_endian>(p, b + end_p - p), p += 4;
1468 write_insn<big_endian>(p, nop), p += 4;
1470 // Write out pltresolve call stub.
1471 if (parameters->options().output_is_position_independent())
1473 uint32_t res0_off = this->pltresolve_;
1474 uint32_t after_bcl_off = the_end + 12;
1475 uint32_t bcl_res0 = after_bcl_off - res0_off;
1477 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
1478 write_insn<big_endian>(p + 4, mflr_0);
1479 write_insn<big_endian>(p + 8, bcl_20_31);
1480 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1481 write_insn<big_endian>(p + 16, mflr_12);
1482 write_insn<big_endian>(p + 20, mtlr_0);
1483 write_insn<big_endian>(p + 24, sub_11_11_12);
1485 uint32_t got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1487 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1488 if (ha(got_bcl) == ha(got_bcl + 4))
1490 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1491 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1495 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1496 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1498 write_insn<big_endian>(p + 40, mtctr_0);
1499 write_insn<big_endian>(p + 44, add_0_11_11);
1500 write_insn<big_endian>(p + 48, add_11_0_11);
1501 write_insn<big_endian>(p + 52, bctr);
1502 write_insn<big_endian>(p + 56, nop);
1503 write_insn<big_endian>(p + 60, nop);
1507 uint32_t res0 = this->pltresolve_ + this->address();
1509 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1510 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1511 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1512 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1514 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1515 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1516 write_insn<big_endian>(p + 16, mtctr_0);
1517 write_insn<big_endian>(p + 20, add_0_11_11);
1518 if (ha(g_o_t + 4) == ha(g_o_t + 8))
1519 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1521 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1522 write_insn<big_endian>(p + 28, add_11_0_11);
1523 write_insn<big_endian>(p + 32, bctr);
1524 write_insn<big_endian>(p + 36, nop);
1525 write_insn<big_endian>(p + 40, nop);
1526 write_insn<big_endian>(p + 44, nop);
1527 write_insn<big_endian>(p + 48, nop);
1528 write_insn<big_endian>(p + 52, nop);
1529 write_insn<big_endian>(p + 56, nop);
1530 write_insn<big_endian>(p + 60, nop);
1535 of->write_output_view(off, oview_size, oview);
1538 // Create the glink section.
1540 template<int size, bool big_endian>
1542 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
1544 if (this->glink_ == NULL)
1546 this->glink_ = new Output_data_glink<size, big_endian>(this);
1547 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
1548 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1549 this->glink_, ORDER_TEXT, false);
1553 // Create a PLT entry for a global symbol.
1555 template<int size, bool big_endian>
1557 Target_powerpc<size, big_endian>::make_plt_entry(Layout* layout,
1559 const elfcpp::Rela<size, big_endian>& reloc,
1560 const Sized_relobj<size, big_endian>* object)
1562 if (this->plt_ == NULL)
1563 this->make_plt_section(layout);
1565 this->plt_->add_entry(gsym);
1567 unsigned int got2_shndx = 0;
1568 if (size == 32 && object != NULL)
1570 const Powerpc_relobj<size, big_endian>* ppc_obj
1571 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1572 got2_shndx = ppc_obj->got2_shndx();
1574 this->glink_->add_entry(gsym, reloc, object, got2_shndx);
1577 // Return the number of entries in the PLT.
1579 template<int size, bool big_endian>
1581 Target_powerpc<size, big_endian>::plt_entry_count() const
1583 if (this->plt_ == NULL)
1585 return this->plt_->entry_count();
1588 // Return the offset of the first non-reserved PLT entry.
1590 template<int size, bool big_endian>
1592 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1594 return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1597 // Return the size of each PLT entry.
1599 template<int size, bool big_endian>
1601 Target_powerpc<size, big_endian>::plt_entry_size() const
1603 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1606 // Create a GOT entry for the TLS module index.
1608 template<int size, bool big_endian>
1610 Target_powerpc<size, big_endian>::got_mod_index_entry(
1611 Symbol_table* symtab,
1613 Sized_relobj_file<size, big_endian>* object)
1615 if (this->got_mod_index_offset_ == -1U)
1617 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1618 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1619 Output_data_got_powerpc<size, big_endian>* got;
1620 unsigned int got_offset;
1622 got = this->got_section(symtab, layout);
1623 got->reserve_ent(2);
1624 got_offset = got->add_constant(0);
1625 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1627 got->add_constant(0);
1628 this->got_mod_index_offset_ = got_offset;
1630 return this->got_mod_index_offset_;
1633 // Optimize the TLS relocation type based on what we know about the
1634 // symbol. IS_FINAL is true if the final address of this symbol is
1635 // known at link time.
1637 template<int size, bool big_endian>
1638 tls::Tls_optimization
1639 Target_powerpc<size, big_endian>::optimize_tls_reloc(bool, int)
1641 // If we are generating a shared library, then we can't do anything
1643 if (parameters->options().shared())
1644 return tls::TLSOPT_NONE;
1646 return tls::TLSOPT_NONE;
1649 // Get the Reference_flags for a particular relocation.
1651 template<int size, bool big_endian>
1653 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
1654 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_PPC_REL16:
1676 case elfcpp::R_PPC_REL16_LO:
1677 case elfcpp::R_PPC_REL16_HI:
1678 case elfcpp::R_PPC_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:
1839 case elfcpp::R_PPC64_ADDR64:
1840 case elfcpp::R_POWERPC_ADDR32:
1841 case elfcpp::R_POWERPC_ADDR16_HA:
1842 case elfcpp::R_POWERPC_ADDR16_LO:
1843 // If building a shared library (or a position-independent
1844 // executable), we need to create a dynamic relocation for
1846 if (parameters->options().output_is_position_independent())
1848 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1850 check_non_pic(object, r_type);
1851 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1853 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1854 rela_dyn->add_local(object, r_sym, r_type, output_section,
1855 data_shndx, reloc.get_r_offset(),
1856 reloc.get_r_addend());
1860 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1861 gold_assert(lsym.get_st_value() == 0);
1862 rela_dyn->add_local_relative(object, r_sym, r_type,
1863 output_section, data_shndx,
1864 reloc.get_r_offset(),
1865 reloc.get_r_addend(), false);
1870 case elfcpp::R_POWERPC_REL24:
1871 case elfcpp::R_PPC_LOCAL24PC:
1872 case elfcpp::R_POWERPC_REL32:
1873 case elfcpp::R_PPC_REL16_LO:
1874 case elfcpp::R_PPC_REL16_HA:
1877 case elfcpp::R_POWERPC_GOT16:
1878 case elfcpp::R_POWERPC_GOT16_LO:
1879 case elfcpp::R_POWERPC_GOT16_HI:
1880 case elfcpp::R_POWERPC_GOT16_HA:
1882 // The symbol requires a GOT entry.
1883 Output_data_got_powerpc<size, big_endian>* got;
1886 got = target->got_section(symtab, layout);
1887 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1889 // If we are generating a shared object, we need to add a
1890 // dynamic relocation for this symbol's GOT entry.
1891 if (parameters->options().output_is_position_independent())
1893 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1895 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1898 off = got->add_constant(0);
1899 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1900 rela_dyn->add_local_relative(object, r_sym,
1901 elfcpp::R_POWERPC_RELATIVE,
1902 got, off, 0, false);
1906 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1910 case elfcpp::R_PPC64_TOC16:
1911 case elfcpp::R_PPC64_TOC16_LO:
1912 case elfcpp::R_PPC64_TOC16_HI:
1913 case elfcpp::R_PPC64_TOC16_HA:
1914 case elfcpp::R_PPC64_TOC16_DS:
1915 case elfcpp::R_PPC64_TOC16_LO_DS:
1916 case elfcpp::R_PPC64_TOC:
1917 // We need a GOT section.
1918 target->got_section(symtab, layout);
1921 // These are relocations which should only be seen by the
1922 // dynamic linker, and should never be seen here.
1923 case elfcpp::R_POWERPC_COPY:
1924 case elfcpp::R_POWERPC_GLOB_DAT:
1925 case elfcpp::R_POWERPC_JMP_SLOT:
1926 case elfcpp::R_POWERPC_RELATIVE:
1927 case elfcpp::R_POWERPC_DTPMOD:
1928 gold_error(_("%s: unexpected reloc %u in object file"),
1929 object->name().c_str(), r_type);
1933 unsupported_reloc_local(object, r_type);
1938 // Report an unsupported relocation against a global symbol.
1940 template<int size, bool big_endian>
1942 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1943 Sized_relobj_file<size, big_endian>* object,
1944 unsigned int r_type,
1947 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1948 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1951 // Scan a relocation for a global symbol.
1953 template<int size, bool big_endian>
1955 Target_powerpc<size, big_endian>::Scan::global(
1956 Symbol_table* symtab,
1958 Target_powerpc<size, big_endian>* target,
1959 Sized_relobj_file<size, big_endian>* object,
1960 unsigned int data_shndx,
1961 Output_section* output_section,
1962 const elfcpp::Rela<size, big_endian>& reloc,
1963 unsigned int r_type,
1968 case elfcpp::R_POWERPC_NONE:
1969 case elfcpp::R_POWERPC_GNU_VTINHERIT:
1970 case elfcpp::R_POWERPC_GNU_VTENTRY:
1971 case elfcpp::R_PPC_LOCAL24PC:
1974 case elfcpp::R_POWERPC_ADDR16:
1975 case elfcpp::R_POWERPC_ADDR16_LO:
1976 case elfcpp::R_POWERPC_ADDR16_HI:
1977 case elfcpp::R_POWERPC_ADDR16_HA:
1978 case elfcpp::R_POWERPC_ADDR32:
1979 case elfcpp::R_PPC64_ADDR64:
1981 // Make a PLT entry if necessary.
1982 if (gsym->needs_plt_entry())
1984 target->make_plt_entry(layout, gsym, reloc, 0);
1985 // Since this is not a PC-relative relocation, we may be
1986 // taking the address of a function. In that case we need to
1987 // set the entry in the dynamic symbol table to the address of
1990 && gsym->is_from_dynobj() && !parameters->options().shared())
1991 gsym->set_needs_dynsym_value();
1993 // Make a dynamic relocation if necessary.
1994 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1996 if (gsym->may_need_copy_reloc())
1998 target->copy_reloc(symtab, layout, object,
1999 data_shndx, output_section, gsym, reloc);
2001 else if ((r_type == elfcpp::R_POWERPC_ADDR32
2002 || r_type == elfcpp::R_PPC64_ADDR64)
2003 && gsym->can_use_relative_reloc(false))
2005 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2006 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2007 output_section, object,
2008 data_shndx, reloc.get_r_offset(),
2009 reloc.get_r_addend(), false);
2013 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2015 check_non_pic(object, r_type);
2016 if (gsym->is_from_dynobj()
2017 || gsym->is_undefined()
2018 || gsym->is_preemptible())
2019 rela_dyn->add_global(gsym, r_type, output_section,
2021 reloc.get_r_offset(),
2022 reloc.get_r_addend());
2024 rela_dyn->add_global_relative(gsym, r_type,
2025 output_section, object,
2027 reloc.get_r_offset(),
2028 reloc.get_r_addend(), false);
2034 case elfcpp::R_PPC_PLTREL24:
2035 case elfcpp::R_POWERPC_REL24:
2037 if (gsym->needs_plt_entry()
2038 || (!gsym->final_value_is_known()
2039 && !(gsym->is_defined()
2040 && !gsym->is_from_dynobj()
2041 && !gsym->is_preemptible())))
2043 if (r_type == elfcpp::R_PPC_PLTREL24)
2044 target->make_plt_entry(layout, gsym, reloc, object);
2046 target->make_plt_entry(layout, gsym, reloc, 0);
2048 // Make a dynamic relocation if necessary.
2049 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2051 if (gsym->may_need_copy_reloc())
2053 target->copy_reloc(symtab, layout, object,
2054 data_shndx, output_section, gsym,
2059 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2060 check_non_pic(object, r_type);
2061 rela_dyn->add_global(gsym, r_type, output_section, object,
2062 data_shndx, reloc.get_r_offset(),
2063 reloc.get_r_addend());
2069 case elfcpp::R_PPC_REL16:
2070 case elfcpp::R_PPC_REL16_LO:
2071 case elfcpp::R_PPC_REL16_HI:
2072 case elfcpp::R_PPC_REL16_HA:
2075 case elfcpp::R_POWERPC_GOT16:
2076 case elfcpp::R_POWERPC_GOT16_LO:
2077 case elfcpp::R_POWERPC_GOT16_HI:
2078 case elfcpp::R_POWERPC_GOT16_HA:
2080 // The symbol requires a GOT entry.
2081 Output_data_got_powerpc<size, big_endian>* got;
2083 got = target->got_section(symtab, layout);
2084 if (gsym->final_value_is_known())
2085 got->add_global(gsym, GOT_TYPE_STANDARD);
2088 // If this symbol is not fully resolved, we need to add a
2089 // dynamic relocation for it.
2090 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2091 if (gsym->is_from_dynobj()
2092 || gsym->is_undefined()
2093 || gsym->is_preemptible())
2094 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2095 elfcpp::R_POWERPC_GLOB_DAT);
2096 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2098 unsigned int off = got->add_constant(0);
2100 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2101 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2102 got, off, 0, false);
2108 case elfcpp::R_PPC64_TOC:
2109 case elfcpp::R_PPC64_TOC16:
2110 case elfcpp::R_PPC64_TOC16_LO:
2111 case elfcpp::R_PPC64_TOC16_HI:
2112 case elfcpp::R_PPC64_TOC16_HA:
2113 case elfcpp::R_PPC64_TOC16_DS:
2114 case elfcpp::R_PPC64_TOC16_LO_DS:
2115 // We need a GOT section.
2116 target->got_section(symtab, layout);
2119 case elfcpp::R_POWERPC_GOT_TPREL16:
2120 case elfcpp::R_POWERPC_TLS:
2124 // These are relocations which should only be seen by the
2125 // dynamic linker, and should never be seen here.
2126 case elfcpp::R_POWERPC_COPY:
2127 case elfcpp::R_POWERPC_GLOB_DAT:
2128 case elfcpp::R_POWERPC_JMP_SLOT:
2129 case elfcpp::R_POWERPC_RELATIVE:
2130 case elfcpp::R_POWERPC_DTPMOD:
2131 gold_error(_("%s: unexpected reloc %u in object file"),
2132 object->name().c_str(), r_type);
2136 unsupported_reloc_global(object, r_type, gsym);
2141 // Process relocations for gc.
2143 template<int size, bool big_endian>
2145 Target_powerpc<size, big_endian>::gc_process_relocs(
2146 Symbol_table* symtab,
2148 Sized_relobj_file<size, big_endian>* object,
2149 unsigned int data_shndx,
2151 const unsigned char* prelocs,
2153 Output_section* output_section,
2154 bool needs_special_offset_handling,
2155 size_t local_symbol_count,
2156 const unsigned char* plocal_symbols)
2158 typedef Target_powerpc<size, big_endian> Powerpc;
2159 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2161 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2162 typename Target_powerpc::Relocatable_size_for_reloc>(
2171 needs_special_offset_handling,
2176 // Scan relocations for a section.
2178 template<int size, bool big_endian>
2180 Target_powerpc<size, big_endian>::scan_relocs(
2181 Symbol_table* symtab,
2183 Sized_relobj_file<size, big_endian>* object,
2184 unsigned int data_shndx,
2185 unsigned int sh_type,
2186 const unsigned char* prelocs,
2188 Output_section* output_section,
2189 bool needs_special_offset_handling,
2190 size_t local_symbol_count,
2191 const unsigned char* plocal_symbols)
2193 typedef Target_powerpc<size, big_endian> Powerpc;
2194 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2196 if (sh_type == elfcpp::SHT_REL)
2198 gold_error(_("%s: unsupported REL reloc section"),
2199 object->name().c_str());
2205 static Output_data_space* sdata;
2207 // Define _SDA_BASE_ at the start of the .sdata section.
2210 // layout->find_output_section(".sdata") == NULL
2211 sdata = new Output_data_space(4, "** sdata");
2213 = layout->add_output_section_data(".sdata", 0,
2215 | elfcpp::SHF_WRITE,
2216 sdata, ORDER_SMALL_DATA, false);
2217 symtab->define_in_output_data("_SDA_BASE_", NULL,
2218 Symbol_table::PREDEFINED,
2219 os, 32768, 0, elfcpp::STT_OBJECT,
2220 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2225 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2234 needs_special_offset_handling,
2239 // Finalize the sections.
2241 template<int size, bool big_endian>
2243 Target_powerpc<size, big_endian>::do_finalize_sections(
2245 const Input_objects*,
2248 // Fill in some more dynamic tags.
2249 const Reloc_section* rel_plt = (this->plt_ == NULL
2251 : this->plt_->rel_plt());
2252 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2253 this->rela_dyn_, true, size == 32);
2257 this->got_->finalize_data_size();
2258 Output_data_dynamic* odyn = layout->dynamic_data();
2259 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2260 this->got_, this->got_->g_o_t());
2263 // Emit any relocs we saved in an attempt to avoid generating COPY
2265 if (this->copy_relocs_.any_saved_relocs())
2266 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2269 // Perform a relocation.
2271 template<int size, bool big_endian>
2273 Target_powerpc<size, big_endian>::Relocate::relocate(
2274 const Relocate_info<size, big_endian>* relinfo,
2275 Target_powerpc* target,
2278 const elfcpp::Rela<size, big_endian>& rela,
2279 unsigned int r_type,
2280 const Sized_symbol<size>* gsym,
2281 const Symbol_value<size>* psymval,
2282 unsigned char* view,
2283 typename elfcpp::Elf_types<size>::Elf_Addr address,
2284 section_size_type /* view_size */)
2286 const unsigned int toc_base_offset = 0x8000;
2287 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
2288 const Powerpc_relobj<size, big_endian>* const object
2289 = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
2290 elfcpp::Elf_Xword value;
2292 if (r_type == elfcpp::R_POWERPC_GOT16
2293 || r_type == elfcpp::R_POWERPC_GOT16_LO
2294 || r_type == elfcpp::R_POWERPC_GOT16_HI
2295 || r_type == elfcpp::R_POWERPC_GOT16_HA
2296 || r_type == elfcpp::R_PPC64_GOT16_DS
2297 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
2301 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2302 value = gsym->got_offset(GOT_TYPE_STANDARD);
2306 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2307 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2308 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2310 value -= target->got_section()->g_o_t();
2312 else if (r_type == elfcpp::R_PPC64_TOC)
2314 value = target->got_section()->address() + toc_base_offset;
2316 else if (gsym != NULL
2317 && (r_type == elfcpp::R_POWERPC_REL24
2318 || r_type == elfcpp::R_PPC_PLTREL24)
2319 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2321 const Output_data_glink<size, big_endian>* glink;
2323 glink = target->glink_section();
2324 unsigned int shndx = 0;
2325 if (size == 32 && r_type == elfcpp::R_PPC_PLTREL24)
2326 shndx = object->got2_shndx();
2327 unsigned int glink_index = glink->find_entry(gsym, rela, object, shndx);
2328 value = glink->address() + glink_index * glink->glink_entry_size();
2332 elfcpp::Elf_Xword addend = 0;
2333 if (r_type != elfcpp::R_PPC_PLTREL24)
2334 addend = rela.get_r_addend();
2335 value = psymval->value(object, addend);
2340 case elfcpp::R_PPC64_TOC16:
2341 case elfcpp::R_PPC64_TOC16_LO:
2342 case elfcpp::R_PPC64_TOC16_HI:
2343 case elfcpp::R_PPC64_TOC16_HA:
2344 case elfcpp::R_PPC64_TOC16_DS:
2345 case elfcpp::R_PPC64_TOC16_LO_DS:
2346 // Subtract the TOC base address.
2347 value -= target->got_section()->address() + toc_base_offset;
2350 case elfcpp::R_POWERPC_SECTOFF:
2351 case elfcpp::R_POWERPC_SECTOFF_LO:
2352 case elfcpp::R_POWERPC_SECTOFF_HI:
2353 case elfcpp::R_POWERPC_SECTOFF_HA:
2354 case elfcpp::R_PPC64_SECTOFF_DS:
2355 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2357 value -= os->address();
2366 case elfcpp::R_POWERPC_NONE:
2367 case elfcpp::R_POWERPC_GNU_VTINHERIT:
2368 case elfcpp::R_POWERPC_GNU_VTENTRY:
2371 case elfcpp::R_POWERPC_REL32:
2372 Reloc::rel32(view, value, 0, address);
2375 case elfcpp::R_POWERPC_REL24:
2376 case elfcpp::R_PPC_PLTREL24:
2377 case elfcpp::R_PPC_LOCAL24PC:
2378 Reloc::rel24(view, value, 0, address);
2381 case elfcpp::R_POWERPC_REL14:
2382 Reloc::rel14(view, value, 0, address);
2385 case elfcpp::R_PPC64_ADDR64:
2386 case elfcpp::R_PPC64_TOC:
2387 Relocate_functions<size, big_endian>::rela64(view, value, 0);
2390 case elfcpp::R_POWERPC_ADDR32:
2391 Relocate_functions<size, big_endian>::rela32(view, value, 0);
2394 case elfcpp::R_POWERPC_ADDR16:
2395 case elfcpp::R_PPC64_TOC16:
2396 case elfcpp::R_POWERPC_GOT16:
2397 case elfcpp::R_POWERPC_SECTOFF:
2398 Reloc::addr16(view, value, 0);
2401 case elfcpp::R_POWERPC_ADDR16_LO:
2402 case elfcpp::R_PPC64_TOC16_LO:
2403 case elfcpp::R_POWERPC_GOT16_LO:
2404 case elfcpp::R_POWERPC_SECTOFF_LO:
2405 Reloc::addr16_lo(view, value, 0);
2408 case elfcpp::R_POWERPC_ADDR16_HI:
2409 case elfcpp::R_PPC64_TOC16_HI:
2410 case elfcpp::R_POWERPC_GOT16_HI:
2411 case elfcpp::R_POWERPC_SECTOFF_HI:
2412 Reloc::addr16_hi(view, value, 0);
2415 case elfcpp::R_POWERPC_ADDR16_HA:
2416 case elfcpp::R_PPC64_TOC16_HA:
2417 case elfcpp::R_POWERPC_GOT16_HA:
2418 case elfcpp::R_POWERPC_SECTOFF_HA:
2419 Reloc::addr16_ha(view, value, 0);
2422 case elfcpp::R_PPC_REL16_LO:
2423 Reloc::rel16_lo(view, value, 0, address);
2426 case elfcpp::R_PPC_REL16_HI:
2427 Reloc::rel16_hi(view, value, 0, address);
2430 case elfcpp::R_PPC_REL16_HA:
2431 Reloc::rel16_ha(view, value, 0, address);
2434 case elfcpp::R_PPC64_ADDR16_DS:
2435 case elfcpp::R_PPC64_ADDR16_LO_DS:
2436 case elfcpp::R_PPC64_TOC16_DS:
2437 case elfcpp::R_PPC64_TOC16_LO_DS:
2438 case elfcpp::R_PPC64_GOT16_DS:
2439 case elfcpp::R_PPC64_GOT16_LO_DS:
2440 case elfcpp::R_PPC64_SECTOFF_DS:
2441 case elfcpp::R_PPC64_SECTOFF_LO_DS:
2442 Reloc::addr16_ds(view, value, 0);
2445 case elfcpp::R_POWERPC_COPY:
2446 case elfcpp::R_POWERPC_GLOB_DAT:
2447 case elfcpp::R_POWERPC_JMP_SLOT:
2448 case elfcpp::R_POWERPC_RELATIVE:
2449 // This is an outstanding tls reloc, which is unexpected when
2451 case elfcpp::R_POWERPC_DTPMOD:
2452 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2453 _("unexpected reloc %u in object file"),
2458 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2459 _("unsupported reloc %u"),
2467 // Perform a TLS relocation.
2469 template<int size, bool big_endian>
2471 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
2472 const Relocate_info<size, big_endian>* relinfo,
2473 Target_powerpc<size, big_endian>* target,
2475 const elfcpp::Rela<size, big_endian>& rela,
2476 unsigned int r_type,
2477 const Sized_symbol<size>* gsym,
2478 const Symbol_value<size>* psymval,
2479 unsigned char* view,
2480 typename elfcpp::Elf_types<size>::Elf_Addr address,
2483 Output_segment* tls_segment = relinfo->layout->tls_segment();
2484 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2486 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2487 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2489 const bool is_final =
2491 ? !parameters->options().output_is_position_independent()
2492 : gsym->final_value_is_known());
2500 // Relocate section data.
2502 template<int size, bool big_endian>
2504 Target_powerpc<size, big_endian>::relocate_section(
2505 const Relocate_info<size, big_endian>* relinfo,
2506 unsigned int sh_type,
2507 const unsigned char* prelocs,
2509 Output_section* output_section,
2510 bool needs_special_offset_handling,
2511 unsigned char* view,
2512 typename elfcpp::Elf_types<size>::Elf_Addr address,
2513 section_size_type view_size,
2514 const Reloc_symbol_changes* reloc_symbol_changes)
2516 typedef Target_powerpc<size, big_endian> Powerpc;
2517 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2519 gold_assert(sh_type == elfcpp::SHT_RELA);
2521 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2528 needs_special_offset_handling,
2532 reloc_symbol_changes);
2535 class Powerpc_scan_relocatable_reloc
2538 // Return the strategy to use for a local symbol which is not a
2539 // section symbol, given the relocation type.
2540 inline Relocatable_relocs::Reloc_strategy
2541 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
2543 if (r_type == 0 && r_sym == 0)
2544 return Relocatable_relocs::RELOC_DISCARD;
2545 return Relocatable_relocs::RELOC_COPY;
2548 // Return the strategy to use for a local symbol which is a section
2549 // symbol, given the relocation type.
2550 inline Relocatable_relocs::Reloc_strategy
2551 local_section_strategy(unsigned int, Relobj*)
2553 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2556 // Return the strategy to use for a global symbol, given the
2557 // relocation type, the object, and the symbol index.
2558 inline Relocatable_relocs::Reloc_strategy
2559 global_strategy(unsigned int r_type, Relobj*, unsigned int)
2561 if (r_type == elfcpp::R_PPC_PLTREL24)
2562 return Relocatable_relocs::RELOC_SPECIAL;
2563 return Relocatable_relocs::RELOC_COPY;
2567 // Scan the relocs during a relocatable link.
2569 template<int size, bool big_endian>
2571 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2572 Symbol_table* symtab,
2574 Sized_relobj_file<size, big_endian>* object,
2575 unsigned int data_shndx,
2576 unsigned int sh_type,
2577 const unsigned char* prelocs,
2579 Output_section* output_section,
2580 bool needs_special_offset_handling,
2581 size_t local_symbol_count,
2582 const unsigned char* plocal_symbols,
2583 Relocatable_relocs* rr)
2585 gold_assert(sh_type == elfcpp::SHT_RELA);
2587 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2588 Powerpc_scan_relocatable_reloc>(
2596 needs_special_offset_handling,
2602 // Relocate a section during a relocatable link.
2604 template<int size, bool big_endian>
2606 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2607 const Relocate_info<size, big_endian>* relinfo,
2608 unsigned int sh_type,
2609 const unsigned char* prelocs,
2611 Output_section* output_section,
2612 off_t offset_in_output_section,
2613 const Relocatable_relocs* rr,
2615 typename elfcpp::Elf_types<size>::Elf_Addr,
2617 unsigned char* reloc_view,
2618 section_size_type reloc_view_size)
2620 gold_assert(sh_type == elfcpp::SHT_RELA);
2622 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2623 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
2625 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
2627 const int reloc_size
2628 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2629 const Address invalid_address = static_cast<Address>(0) - 1;
2631 Powerpc_relobj<size, big_endian>* const object
2632 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
2633 const unsigned int local_count = object->local_symbol_count();
2634 unsigned int got2_shndx = object->got2_shndx();
2635 typename elfcpp::Elf_types<size>::Elf_Swxword got2_addend = 0;
2636 if (got2_shndx != 0)
2637 got2_addend = object->get_output_section_offset(got2_shndx);
2639 unsigned char* pwrite = reloc_view;
2641 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2643 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
2644 if (strategy == Relocatable_relocs::RELOC_DISCARD)
2647 Reltype reloc(prelocs);
2648 Reltype_write reloc_write(pwrite);
2650 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2651 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2652 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2654 // Get the new symbol index.
2656 unsigned int new_symndx;
2657 if (r_sym < local_count)
2661 case Relocatable_relocs::RELOC_COPY:
2662 case Relocatable_relocs::RELOC_SPECIAL:
2663 new_symndx = object->symtab_index(r_sym);
2664 gold_assert(new_symndx != -1U);
2667 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
2669 // We are adjusting a section symbol. We need to find
2670 // the symbol table index of the section symbol for
2671 // the output section corresponding to input section
2672 // in which this symbol is defined.
2673 gold_assert(r_sym < local_count);
2675 unsigned int shndx =
2676 object->local_symbol_input_shndx(r_sym, &is_ordinary);
2677 gold_assert(is_ordinary);
2678 Output_section* os = object->output_section(shndx);
2679 gold_assert(os != NULL);
2680 gold_assert(os->needs_symtab_index());
2681 new_symndx = os->symtab_index();
2691 const Symbol* gsym = object->global_symbol(r_sym);
2692 gold_assert(gsym != NULL);
2693 if (gsym->is_forwarder())
2694 gsym = relinfo->symtab->resolve_forwards(gsym);
2696 gold_assert(gsym->has_symtab_index());
2697 new_symndx = gsym->symtab_index();
2700 // Get the new offset--the location in the output section where
2701 // this relocation should be applied.
2703 Address offset = reloc.get_r_offset();
2705 if (static_cast<Address>(offset_in_output_section) != invalid_address)
2706 new_offset = offset + offset_in_output_section;
2709 section_offset_type sot_offset =
2710 convert_types<section_offset_type, Address>(offset);
2711 section_offset_type new_sot_offset =
2712 output_section->output_offset(object, relinfo->data_shndx,
2714 gold_assert(new_sot_offset != -1);
2715 new_offset = new_sot_offset;
2718 reloc_write.put_r_offset(new_offset);
2719 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
2721 // Handle the reloc addend based on the strategy.
2722 typename elfcpp::Elf_types<size>::Elf_Swxword addend;
2723 addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2724 get_reloc_addend(&reloc);
2726 if (strategy == Relocatable_relocs::RELOC_COPY)
2728 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
2730 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
2732 addend = psymval->value(object, addend);
2734 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
2736 if (addend >= 32768)
2737 addend += got2_addend;
2742 Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2743 set_reloc_addend(&reloc_write, addend);
2745 pwrite += reloc_size;
2748 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
2749 == reloc_view_size);
2752 // Return the value to use for a dynamic which requires special
2753 // treatment. This is how we support equality comparisons of function
2754 // pointers across shared library boundaries, as described in the
2755 // processor specific ABI supplement.
2757 template<int size, bool big_endian>
2759 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2763 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2764 return this->plt_section()->address() + gsym->plt_offset();
2770 // The selector for powerpc object files.
2772 template<int size, bool big_endian>
2773 class Target_selector_powerpc : public Target_selector
2776 Target_selector_powerpc()
2777 : Target_selector(elfcpp::EM_NONE, size, big_endian,
2779 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2780 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2782 ? (big_endian ? "elf64ppc" : "elf64lppc")
2783 : (big_endian ? "elf32ppc" : "elf32lppc")))
2787 do_recognize(Input_file*, off_t, int machine, int, int)
2792 if (machine != elfcpp::EM_PPC64)
2797 if (machine != elfcpp::EM_PPC)
2805 return this->instantiate_target();
2809 do_instantiate_target()
2810 { return new Target_powerpc<size, big_endian>(); }
2813 Target_selector_powerpc<32, true> target_selector_ppc32;
2814 Target_selector_powerpc<32, false> target_selector_ppc32le;
2815 Target_selector_powerpc<64, true> target_selector_ppc64;
2816 Target_selector_powerpc<64, false> target_selector_ppc64le;
2818 } // End anonymous namespace.