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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 template<int size, bool big_endian>
49 class Output_data_plt_powerpc;
51 template<int size, bool big_endian>
52 class Output_data_brlt_powerpc;
54 template<int size, bool big_endian>
55 class Output_data_got_powerpc;
57 template<int size, bool big_endian>
58 class Output_data_glink;
60 template<int size, bool big_endian>
63 template<int size, bool big_endian>
64 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
67 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
68 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
69 typedef Unordered_map<Address, Section_refs> Access_from;
71 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
72 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
73 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
74 special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
80 // The .got2 section shndx.
85 return this->special_;
90 // The .opd section shndx.
97 return this->special_;
100 // Init OPD entry arrays.
102 init_opd(size_t opd_size)
104 size_t count = this->opd_ent_ndx(opd_size);
105 this->opd_ent_.resize(count);
108 // Return section and offset of function entry for .opd + R_OFF.
110 get_opd_ent(Address r_off, Address* value = NULL) const
112 size_t ndx = this->opd_ent_ndx(r_off);
113 gold_assert(ndx < this->opd_ent_.size());
114 gold_assert(this->opd_ent_[ndx].shndx != 0);
116 *value = this->opd_ent_[ndx].off;
117 return this->opd_ent_[ndx].shndx;
120 // Set section and offset of function entry for .opd + R_OFF.
122 set_opd_ent(Address r_off, unsigned int shndx, Address value)
124 size_t ndx = this->opd_ent_ndx(r_off);
125 gold_assert(ndx < this->opd_ent_.size());
126 this->opd_ent_[ndx].shndx = shndx;
127 this->opd_ent_[ndx].off = value;
130 // Return discard flag for .opd + R_OFF.
132 get_opd_discard(Address r_off) const
134 size_t ndx = this->opd_ent_ndx(r_off);
135 gold_assert(ndx < this->opd_ent_.size());
136 return this->opd_ent_[ndx].discard;
139 // Set discard flag for .opd + R_OFF.
141 set_opd_discard(Address r_off)
143 size_t ndx = this->opd_ent_ndx(r_off);
144 gold_assert(ndx < this->opd_ent_.size());
145 this->opd_ent_[ndx].discard = true;
150 { return &this->access_from_map_; }
152 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
153 // section at DST_OFF.
155 add_reference(Object* src_obj,
156 unsigned int src_indx,
157 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
159 Section_id src_id(src_obj, src_indx);
160 this->access_from_map_[dst_off].insert(src_id);
163 // Add a reference to the code section specified by the .opd entry
166 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
168 size_t ndx = this->opd_ent_ndx(dst_off);
169 if (ndx >= this->opd_ent_.size())
170 this->opd_ent_.resize(ndx + 1);
171 this->opd_ent_[ndx].gc_mark = true;
175 process_gc_mark(Symbol_table* symtab)
177 for (size_t i = 0; i < this->opd_ent_.size(); i++)
178 if (this->opd_ent_[i].gc_mark)
180 unsigned int shndx = this->opd_ent_[i].shndx;
181 symtab->gc()->worklist().push(Section_id(this, shndx));
187 { return this->opd_valid_; }
191 { this->opd_valid_ = true; }
193 // Examine .rela.opd to build info about function entry points.
195 scan_opd_relocs(size_t reloc_count,
196 const unsigned char* prelocs,
197 const unsigned char* plocal_syms);
199 // Perform the Sized_relobj_file method, then set up opd info from
202 do_read_relocs(Read_relocs_data*);
205 do_find_special_sections(Read_symbols_data* sd);
207 // Adjust this local symbol value. Return false if the symbol
208 // should be discarded from the output file.
210 do_adjust_local_symbol(Symbol_value<size>* lv) const
212 if (size == 64 && this->opd_shndx() != 0)
215 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
217 if (this->get_opd_discard(lv->input_value()))
223 // Return offset in output GOT section that this object will use
224 // as a TOC pointer. Won't be just a constant with multi-toc support.
226 toc_base_offset() const
230 set_has_14bit_branch(unsigned int shndx)
232 if (shndx >= this->has14_.size())
233 this->has14_.resize(shndx + 1);
234 this->has14_[shndx] = true;
238 has_14bit_branch(unsigned int shndx) const
239 { return shndx < this->has14_.size() && this->has14_[shndx]; }
242 set_stub_table(unsigned int shndx, Stub_table<size, big_endian>* stub_table)
244 if (shndx >= this->stub_table_.size())
245 this->stub_table_.resize(shndx + 1);
246 this->stub_table_[shndx] = stub_table;
249 Stub_table<size, big_endian>*
250 stub_table(unsigned int shndx)
252 if (shndx < this->stub_table_.size())
253 return this->stub_table_[shndx];
266 // Return index into opd_ent_ array for .opd entry at OFF.
267 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
268 // apart when the language doesn't use the last 8-byte word, the
269 // environment pointer. Thus dividing the entry section offset by
270 // 16 will give an index into opd_ent_ that works for either layout
271 // of .opd. (It leaves some elements of the vector unused when .opd
272 // entries are spaced 24 bytes apart, but we don't know the spacing
273 // until relocations are processed, and in any case it is possible
274 // for an object to have some entries spaced 16 bytes apart and
275 // others 24 bytes apart.)
277 opd_ent_ndx(size_t off) const
280 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
281 unsigned int special_;
283 // Set at the start of gc_process_relocs, when we know opd_ent_
284 // vector is valid. The flag could be made atomic and set in
285 // do_read_relocs with memory_order_release and then tested with
286 // memory_order_acquire, potentially resulting in fewer entries in
290 // The first 8-byte word of an OPD entry gives the address of the
291 // entry point of the function. Relocatable object files have a
292 // relocation on this word. The following vector records the
293 // section and offset specified by these relocations.
294 std::vector<Opd_ent> opd_ent_;
296 // References made to this object's .opd section when running
297 // gc_process_relocs for another object, before the opd_ent_ vector
298 // is valid for this object.
299 Access_from access_from_map_;
301 // Whether input section has a 14-bit branch reloc.
302 std::vector<bool> has14_;
304 // The stub table to use for a given input section.
305 std::vector<Stub_table<size, big_endian>*> stub_table_;
308 template<int size, bool big_endian>
309 class Target_powerpc : public Sized_target<size, big_endian>
313 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
314 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
315 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
316 static const Address invalid_address = static_cast<Address>(0) - 1;
317 // Offset of tp and dtp pointers from start of TLS block.
318 static const Address tp_offset = 0x7000;
319 static const Address dtp_offset = 0x8000;
322 : Sized_target<size, big_endian>(&powerpc_info),
323 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
324 glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
325 dynbss_(NULL), tlsld_got_offset_(-1U),
326 stub_tables_(), branch_lookup_table_(), branch_info_()
330 // Process the relocations to determine unreferenced sections for
331 // garbage collection.
333 gc_process_relocs(Symbol_table* symtab,
335 Sized_relobj_file<size, big_endian>* object,
336 unsigned int data_shndx,
337 unsigned int sh_type,
338 const unsigned char* prelocs,
340 Output_section* output_section,
341 bool needs_special_offset_handling,
342 size_t local_symbol_count,
343 const unsigned char* plocal_symbols);
345 // Scan the relocations to look for symbol adjustments.
347 scan_relocs(Symbol_table* symtab,
349 Sized_relobj_file<size, big_endian>* object,
350 unsigned int data_shndx,
351 unsigned int sh_type,
352 const unsigned char* prelocs,
354 Output_section* output_section,
355 bool needs_special_offset_handling,
356 size_t local_symbol_count,
357 const unsigned char* plocal_symbols);
359 // Map input .toc section to output .got section.
361 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
363 if (size == 64 && strcmp(name, ".toc") == 0)
371 // Provide linker defined save/restore functions.
373 define_save_restore_funcs(Layout*, Symbol_table*);
375 // No stubs unless a final link.
378 { return !parameters->options().relocatable(); }
381 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
383 // Stash info about branches, for stub generation.
385 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
386 unsigned int data_shndx, Address r_offset,
387 unsigned int r_type, unsigned int r_sym, Address addend)
389 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
390 this->branch_info_.push_back(info);
391 if (r_type == elfcpp::R_POWERPC_REL14
392 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
393 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
394 ppc_object->set_has_14bit_branch(data_shndx);
397 Stub_table<size, big_endian>*
401 do_define_standard_symbols(Symbol_table*, Layout*);
403 // Finalize the sections.
405 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
407 // Return the value to use for a dynamic which requires special
410 do_dynsym_value(const Symbol*) const;
412 // Return the PLT address to use for a local symbol.
414 do_plt_address_for_local(const Relobj*, unsigned int) const;
416 // Return the PLT address to use for a global symbol.
418 do_plt_address_for_global(const Symbol*) const;
420 // Return the offset to use for the GOT_INDX'th got entry which is
421 // for a local tls symbol specified by OBJECT, SYMNDX.
423 do_tls_offset_for_local(const Relobj* object,
425 unsigned int got_indx) const;
427 // Return the offset to use for the GOT_INDX'th got entry which is
428 // for global tls symbol GSYM.
430 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
432 // Relocate a section.
434 relocate_section(const Relocate_info<size, big_endian>*,
435 unsigned int sh_type,
436 const unsigned char* prelocs,
438 Output_section* output_section,
439 bool needs_special_offset_handling,
441 Address view_address,
442 section_size_type view_size,
443 const Reloc_symbol_changes*);
445 // Scan the relocs during a relocatable link.
447 scan_relocatable_relocs(Symbol_table* symtab,
449 Sized_relobj_file<size, big_endian>* object,
450 unsigned int data_shndx,
451 unsigned int sh_type,
452 const unsigned char* prelocs,
454 Output_section* output_section,
455 bool needs_special_offset_handling,
456 size_t local_symbol_count,
457 const unsigned char* plocal_symbols,
458 Relocatable_relocs*);
460 // Emit relocations for a section.
462 relocate_relocs(const Relocate_info<size, big_endian>*,
463 unsigned int sh_type,
464 const unsigned char* prelocs,
466 Output_section* output_section,
467 typename elfcpp::Elf_types<size>::Elf_Off
468 offset_in_output_section,
469 const Relocatable_relocs*,
471 Address view_address,
473 unsigned char* reloc_view,
474 section_size_type reloc_view_size);
476 // Return whether SYM is defined by the ABI.
478 do_is_defined_by_abi(const Symbol* sym) const
480 return strcmp(sym->name(), "__tls_get_addr") == 0;
483 // Return the size of the GOT section.
487 gold_assert(this->got_ != NULL);
488 return this->got_->data_size();
491 // Get the PLT section.
492 const Output_data_plt_powerpc<size, big_endian>*
495 gold_assert(this->plt_ != NULL);
499 // Get the IPLT section.
500 const Output_data_plt_powerpc<size, big_endian>*
503 gold_assert(this->iplt_ != NULL);
507 // Get the .glink section.
508 const Output_data_glink<size, big_endian>*
509 glink_section() const
511 gold_assert(this->glink_ != NULL);
515 // Get the GOT section.
516 const Output_data_got_powerpc<size, big_endian>*
519 gold_assert(this->got_ != NULL);
523 // Get the GOT section, creating it if necessary.
524 Output_data_got_powerpc<size, big_endian>*
525 got_section(Symbol_table*, Layout*);
528 do_make_elf_object(const std::string&, Input_file*, off_t,
529 const elfcpp::Ehdr<size, big_endian>&);
531 // Return the number of entries in the GOT.
533 got_entry_count() const
535 if (this->got_ == NULL)
537 return this->got_size() / (size / 8);
540 // Return the number of entries in the PLT.
542 plt_entry_count() const;
544 // Return the offset of the first non-reserved PLT entry.
546 first_plt_entry_offset() const;
548 // Return the size of each PLT entry.
550 plt_entry_size() const;
552 // Add any special sections for this symbol to the gc work list.
553 // For powerpc64, this adds the code section of a function
556 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
558 // Handle target specific gc actions when adding a gc reference from
559 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
560 // and DST_OFF. For powerpc64, this adds a referenc to the code
561 // section of a function descriptor.
563 do_gc_add_reference(Symbol_table* symtab,
565 unsigned int src_shndx,
567 unsigned int dst_shndx,
568 Address dst_off) const;
570 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
573 { return this->stub_tables_; }
575 const Output_data_brlt_powerpc<size, big_endian>*
577 { return this->brlt_section_; }
580 add_branch_lookup_table(Address to)
582 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
583 this->branch_lookup_table_.insert(std::make_pair(to, off));
587 find_branch_lookup_table(Address to)
589 typename Branch_lookup_table::const_iterator p
590 = this->branch_lookup_table_.find(to);
591 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
595 write_branch_lookup_table(unsigned char *oview)
597 for (typename Branch_lookup_table::const_iterator p
598 = this->branch_lookup_table_.begin();
599 p != this->branch_lookup_table_.end();
602 elfcpp::Swap<32, big_endian>::writeval(oview + p->second, p->first);
608 // The class which scans relocations.
612 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
615 : issued_non_pic_error_(false)
619 get_reference_flags(unsigned int r_type);
622 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
623 Sized_relobj_file<size, big_endian>* object,
624 unsigned int data_shndx,
625 Output_section* output_section,
626 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
627 const elfcpp::Sym<size, big_endian>& lsym,
631 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
632 Sized_relobj_file<size, big_endian>* object,
633 unsigned int data_shndx,
634 Output_section* output_section,
635 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
639 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
641 Sized_relobj_file<size, big_endian>* ,
644 const elfcpp::Rela<size, big_endian>& ,
646 const elfcpp::Sym<size, big_endian>&)
650 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
652 Sized_relobj_file<size, big_endian>* ,
655 const elfcpp::Rela<size,
657 unsigned int , Symbol*)
662 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
663 unsigned int r_type);
666 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
667 unsigned int r_type, Symbol*);
670 generate_tls_call(Symbol_table* symtab, Layout* layout,
671 Target_powerpc* target);
674 check_non_pic(Relobj*, unsigned int r_type);
677 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
678 unsigned int r_type);
680 // Whether we have issued an error about a non-PIC compilation.
681 bool issued_non_pic_error_;
685 symval_for_branch(Address value, const Sized_symbol<size>* gsym,
686 Powerpc_relobj<size, big_endian>* object,
687 unsigned int *dest_shndx);
689 // The class which implements relocation.
693 // Use 'at' branch hints when true, 'y' when false.
694 // FIXME maybe: set this with an option.
695 static const bool is_isa_v2 = true;
699 CALL_NOT_EXPECTED = 0,
705 : call_tls_get_addr_(CALL_NOT_EXPECTED)
710 if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
712 // FIXME: This needs to specify the location somehow.
713 gold_error(_("missing expected __tls_get_addr call"));
717 // Do a relocation. Return false if the caller should not issue
718 // any warnings about this relocation.
720 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
721 Output_section*, size_t relnum,
722 const elfcpp::Rela<size, big_endian>&,
723 unsigned int r_type, const Sized_symbol<size>*,
724 const Symbol_value<size>*,
726 typename elfcpp::Elf_types<size>::Elf_Addr,
729 // This is set if we should skip the next reloc, which should be a
730 // call to __tls_get_addr.
731 enum skip_tls call_tls_get_addr_;
734 class Relocate_comdat_behavior
737 // Decide what the linker should do for relocations that refer to
738 // discarded comdat sections.
739 inline Comdat_behavior
740 get(const char* name)
742 gold::Default_comdat_behavior default_behavior;
743 Comdat_behavior ret = default_behavior.get(name);
744 if (ret == CB_WARNING)
747 && (strcmp(name, ".fixup") == 0
748 || strcmp(name, ".got2") == 0))
751 && (strcmp(name, ".opd") == 0
752 || strcmp(name, ".toc") == 0
753 || strcmp(name, ".toc1") == 0))
760 // A class which returns the size required for a relocation type,
761 // used while scanning relocs during a relocatable link.
762 class Relocatable_size_for_reloc
766 get_size_for_reloc(unsigned int, Relobj*)
773 // Optimize the TLS relocation type based on what we know about the
774 // symbol. IS_FINAL is true if the final address of this symbol is
775 // known at link time.
777 tls::Tls_optimization
778 optimize_tls_gd(bool is_final)
780 // If we are generating a shared library, then we can't do anything
782 if (parameters->options().shared())
783 return tls::TLSOPT_NONE;
786 return tls::TLSOPT_TO_IE;
787 return tls::TLSOPT_TO_LE;
790 tls::Tls_optimization
793 if (parameters->options().shared())
794 return tls::TLSOPT_NONE;
796 return tls::TLSOPT_TO_LE;
799 tls::Tls_optimization
800 optimize_tls_ie(bool is_final)
802 if (!is_final || parameters->options().shared())
803 return tls::TLSOPT_NONE;
805 return tls::TLSOPT_TO_LE;
810 make_glink_section(Layout*);
812 // Create the PLT section.
814 make_plt_section(Symbol_table*, Layout*);
817 make_iplt_section(Symbol_table*, Layout*);
820 make_brlt_section(Layout*);
822 // Create a PLT entry for a global symbol.
824 make_plt_entry(Symbol_table*, Layout*, Symbol*);
826 // Create a PLT entry for a local IFUNC symbol.
828 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
829 Sized_relobj_file<size, big_endian>*,
833 // Create a GOT entry for local dynamic __tls_get_addr.
835 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
836 Sized_relobj_file<size, big_endian>* object);
839 tlsld_got_offset() const
841 return this->tlsld_got_offset_;
844 // Get the dynamic reloc section, creating it if necessary.
846 rela_dyn_section(Layout*);
848 // Copy a relocation against a global symbol.
850 copy_reloc(Symbol_table* symtab, Layout* layout,
851 Sized_relobj_file<size, big_endian>* object,
852 unsigned int shndx, Output_section* output_section,
853 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
855 this->copy_relocs_.copy_reloc(symtab, layout,
856 symtab->get_sized_symbol<size>(sym),
857 object, shndx, output_section,
858 reloc, this->rela_dyn_section(layout));
861 // Look over all the input sections, deciding where to place stub.
863 group_sections(Layout*, const Task*);
865 // Sort output sections by address.
869 operator()(const Output_section* sec1, const Output_section* sec2)
870 { return sec1->address() < sec2->address(); }
876 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
877 unsigned int data_shndx,
882 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
883 r_type_(r_type), r_sym_(r_sym), addend_(addend)
889 // If this branch needs a plt call stub, or a long branch stub, make one.
891 make_stub(Stub_table<size, big_endian>*,
892 Stub_table<size, big_endian>*,
893 Symbol_table*) const;
896 // The branch location..
897 Powerpc_relobj<size, big_endian>* object_;
900 // ..and the branch type and destination.
901 unsigned int r_type_;
906 // Information about this specific target which we pass to the
907 // general Target structure.
908 static Target::Target_info powerpc_info;
910 // The types of GOT entries needed for this platform.
911 // These values are exposed to the ABI in an incremental link.
912 // Do not renumber existing values without changing the version
913 // number of the .gnu_incremental_inputs section.
917 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
918 GOT_TYPE_DTPREL, // entry for @got@dtprel
919 GOT_TYPE_TPREL // entry for @got@tprel
923 Output_data_got_powerpc<size, big_endian>* got_;
925 Output_data_plt_powerpc<size, big_endian>* plt_;
927 Output_data_plt_powerpc<size, big_endian>* iplt_;
928 // Section holding long branch destinations.
929 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
930 // The .glink section.
931 Output_data_glink<size, big_endian>* glink_;
932 // The dynamic reloc section.
933 Reloc_section* rela_dyn_;
934 // Relocs saved to avoid a COPY reloc.
935 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
936 // Space for variables copied with a COPY reloc.
937 Output_data_space* dynbss_;
938 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
939 unsigned int tlsld_got_offset_;
941 Stub_tables stub_tables_;
942 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
943 Branch_lookup_table branch_lookup_table_;
945 typedef std::vector<Branch_info> Branches;
946 Branches branch_info_;
950 Target::Target_info Target_powerpc<32, true>::powerpc_info =
953 true, // is_big_endian
954 elfcpp::EM_PPC, // machine_code
955 false, // has_make_symbol
956 false, // has_resolve
957 false, // has_code_fill
958 true, // is_default_stack_executable
959 false, // can_icf_inline_merge_sections
961 "/usr/lib/ld.so.1", // dynamic_linker
962 0x10000000, // default_text_segment_address
963 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
964 4 * 1024, // common_pagesize (overridable by -z common-page-size)
965 false, // isolate_execinstr
967 elfcpp::SHN_UNDEF, // small_common_shndx
968 elfcpp::SHN_UNDEF, // large_common_shndx
969 0, // small_common_section_flags
970 0, // large_common_section_flags
971 NULL, // attributes_section
972 NULL // attributes_vendor
976 Target::Target_info Target_powerpc<32, false>::powerpc_info =
979 false, // is_big_endian
980 elfcpp::EM_PPC, // machine_code
981 false, // has_make_symbol
982 false, // has_resolve
983 false, // has_code_fill
984 true, // is_default_stack_executable
985 false, // can_icf_inline_merge_sections
987 "/usr/lib/ld.so.1", // dynamic_linker
988 0x10000000, // default_text_segment_address
989 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
990 4 * 1024, // common_pagesize (overridable by -z common-page-size)
991 false, // isolate_execinstr
993 elfcpp::SHN_UNDEF, // small_common_shndx
994 elfcpp::SHN_UNDEF, // large_common_shndx
995 0, // small_common_section_flags
996 0, // large_common_section_flags
997 NULL, // attributes_section
998 NULL // attributes_vendor
1002 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1005 true, // is_big_endian
1006 elfcpp::EM_PPC64, // machine_code
1007 false, // has_make_symbol
1008 false, // has_resolve
1009 false, // has_code_fill
1010 true, // is_default_stack_executable
1011 false, // can_icf_inline_merge_sections
1013 "/usr/lib/ld.so.1", // dynamic_linker
1014 0x10000000, // default_text_segment_address
1015 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1016 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1017 false, // isolate_execinstr
1019 elfcpp::SHN_UNDEF, // small_common_shndx
1020 elfcpp::SHN_UNDEF, // large_common_shndx
1021 0, // small_common_section_flags
1022 0, // large_common_section_flags
1023 NULL, // attributes_section
1024 NULL // attributes_vendor
1028 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1031 false, // is_big_endian
1032 elfcpp::EM_PPC64, // machine_code
1033 false, // has_make_symbol
1034 false, // has_resolve
1035 false, // has_code_fill
1036 true, // is_default_stack_executable
1037 false, // can_icf_inline_merge_sections
1039 "/usr/lib/ld.so.1", // dynamic_linker
1040 0x10000000, // default_text_segment_address
1041 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1042 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1043 false, // isolate_execinstr
1045 elfcpp::SHN_UNDEF, // small_common_shndx
1046 elfcpp::SHN_UNDEF, // large_common_shndx
1047 0, // small_common_section_flags
1048 0, // large_common_section_flags
1049 NULL, // attributes_section
1050 NULL // attributes_vendor
1054 is_branch_reloc(unsigned int r_type)
1056 return (r_type == elfcpp::R_POWERPC_REL24
1057 || r_type == elfcpp::R_PPC_PLTREL24
1058 || r_type == elfcpp::R_PPC_LOCAL24PC
1059 || r_type == elfcpp::R_POWERPC_REL14
1060 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1061 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1062 || r_type == elfcpp::R_POWERPC_ADDR24
1063 || r_type == elfcpp::R_POWERPC_ADDR14
1064 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1065 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1068 // If INSN is an opcode that may be used with an @tls operand, return
1069 // the transformed insn for TLS optimisation, otherwise return 0. If
1070 // REG is non-zero only match an insn with RB or RA equal to REG.
1072 at_tls_transform(uint32_t insn, unsigned int reg)
1074 if ((insn & (0x3f << 26)) != 31 << 26)
1078 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1079 rtra = insn & ((1 << 26) - (1 << 16));
1080 else if (((insn >> 16) & 0x1f) == reg)
1081 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1085 if ((insn & (0x3ff << 1)) == 266 << 1)
1088 else if ((insn & (0x1f << 1)) == 23 << 1
1089 && ((insn & (0x1f << 6)) < 14 << 6
1090 || ((insn & (0x1f << 6)) >= 16 << 6
1091 && (insn & (0x1f << 6)) < 24 << 6)))
1092 // load and store indexed -> dform
1093 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1094 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1095 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1096 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1097 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1099 insn = (58 << 26) | 2;
1106 // Modified version of symtab.h class Symbol member
1107 // Given a direct absolute or pc-relative static relocation against
1108 // the global symbol, this function returns whether a dynamic relocation
1113 needs_dynamic_reloc(const Symbol* gsym, int flags)
1115 // No dynamic relocations in a static link!
1116 if (parameters->doing_static_link())
1119 // A reference to an undefined symbol from an executable should be
1120 // statically resolved to 0, and does not need a dynamic relocation.
1121 // This matches gnu ld behavior.
1122 if (gsym->is_undefined() && !parameters->options().shared())
1125 // A reference to an absolute symbol does not need a dynamic relocation.
1126 if (gsym->is_absolute())
1129 // An absolute reference within a position-independent output file
1130 // will need a dynamic relocation.
1131 if ((flags & Symbol::ABSOLUTE_REF)
1132 && parameters->options().output_is_position_independent())
1135 // A function call that can branch to a local PLT entry does not need
1136 // a dynamic relocation.
1137 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
1140 // A reference to any PLT entry in a non-position-independent executable
1141 // does not need a dynamic relocation.
1142 // Except due to having function descriptors on powerpc64 we don't define
1143 // functions to their plt code in an executable, so this doesn't apply.
1145 && !parameters->options().output_is_position_independent()
1146 && gsym->has_plt_offset())
1149 // A reference to a symbol defined in a dynamic object or to a
1150 // symbol that is preemptible will need a dynamic relocation.
1151 if (gsym->is_from_dynobj()
1152 || gsym->is_undefined()
1153 || gsym->is_preemptible())
1156 // For all other cases, return FALSE.
1160 // Modified version of symtab.h class Symbol member
1161 // Whether we should use the PLT offset associated with a symbol for
1162 // a relocation. FLAGS is a set of Reference_flags.
1166 use_plt_offset(const Symbol* gsym, int flags)
1168 // If the symbol doesn't have a PLT offset, then naturally we
1169 // don't want to use it.
1170 if (!gsym->has_plt_offset())
1173 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1174 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1177 // If we are going to generate a dynamic relocation, then we will
1178 // wind up using that, so no need to use the PLT entry.
1179 if (needs_dynamic_reloc<size>(gsym, flags))
1182 // If the symbol is from a dynamic object, we need to use the PLT
1184 if (gsym->is_from_dynobj())
1187 // If we are generating a shared object, and gsym symbol is
1188 // undefined or preemptible, we need to use the PLT entry.
1189 if (parameters->options().shared()
1190 && (gsym->is_undefined() || gsym->is_preemptible()))
1193 // If gsym is a call to a weak undefined symbol, we need to use
1194 // the PLT entry; the symbol may be defined by a library loaded
1196 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1199 // Otherwise we can use the regular definition.
1203 template<int size, bool big_endian>
1204 class Powerpc_relocate_functions
1221 typedef Powerpc_relocate_functions<size, big_endian> This;
1222 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1224 template<int valsize>
1226 has_overflow_signed(Address value)
1228 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1229 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1230 limit <<= ((valsize - 1) >> 1);
1231 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1232 return value + limit > (limit << 1) - 1;
1235 template<int valsize>
1237 has_overflow_bitfield(Address value)
1239 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1240 limit <<= ((valsize - 1) >> 1);
1241 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1242 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1245 template<int valsize>
1246 static inline Status
1247 overflowed(Address value, Overflow_check overflow)
1249 if (overflow == CHECK_SIGNED)
1251 if (has_overflow_signed<valsize>(value))
1252 return STATUS_OVERFLOW;
1254 else if (overflow == CHECK_BITFIELD)
1256 if (has_overflow_bitfield<valsize>(value))
1257 return STATUS_OVERFLOW;
1262 // Do a simple RELA relocation
1263 template<int valsize>
1264 static inline Status
1265 rela(unsigned char* view, Address value, Overflow_check overflow)
1267 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1268 Valtype* wv = reinterpret_cast<Valtype*>(view);
1269 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1270 return overflowed<valsize>(value, overflow);
1273 template<int valsize>
1274 static inline Status
1275 rela(unsigned char* view,
1276 unsigned int right_shift,
1277 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1279 Overflow_check overflow)
1281 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1282 Valtype* wv = reinterpret_cast<Valtype*>(view);
1283 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1284 Valtype reloc = value >> right_shift;
1287 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1288 return overflowed<valsize>(value >> right_shift, overflow);
1291 // Do a simple RELA relocation, unaligned.
1292 template<int valsize>
1293 static inline Status
1294 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1296 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1297 return overflowed<valsize>(value, overflow);
1300 template<int valsize>
1301 static inline Status
1302 rela_ua(unsigned char* view,
1303 unsigned int right_shift,
1304 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1306 Overflow_check overflow)
1308 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1310 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1311 Valtype reloc = value >> right_shift;
1314 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1315 return overflowed<valsize>(value >> right_shift, overflow);
1319 // R_PPC64_ADDR64: (Symbol + Addend)
1321 addr64(unsigned char* view, Address value)
1322 { This::template rela<64>(view, value, CHECK_NONE); }
1324 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1326 addr64_u(unsigned char* view, Address value)
1327 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1329 // R_POWERPC_ADDR32: (Symbol + Addend)
1330 static inline Status
1331 addr32(unsigned char* view, Address value, Overflow_check overflow)
1332 { return This::template rela<32>(view, value, overflow); }
1334 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1335 static inline Status
1336 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1337 { return This::template rela_ua<32>(view, value, overflow); }
1339 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1340 static inline Status
1341 addr24(unsigned char* view, Address value, Overflow_check overflow)
1343 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1344 if (overflow != CHECK_NONE && (value & 3) != 0)
1345 stat = STATUS_OVERFLOW;
1349 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1350 static inline Status
1351 addr16(unsigned char* view, Address value, Overflow_check overflow)
1352 { return This::template rela<16>(view, value, overflow); }
1354 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1355 static inline Status
1356 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1357 { return This::template rela_ua<16>(view, value, overflow); }
1359 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1360 static inline Status
1361 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1363 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1364 if (overflow != CHECK_NONE && (value & 3) != 0)
1365 stat = STATUS_OVERFLOW;
1369 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1371 addr16_hi(unsigned char* view, Address value)
1372 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1374 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1376 addr16_ha(unsigned char* view, Address value)
1377 { This::addr16_hi(view, value + 0x8000); }
1379 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1381 addr16_hi2(unsigned char* view, Address value)
1382 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1384 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1386 addr16_ha2(unsigned char* view, Address value)
1387 { This::addr16_hi2(view, value + 0x8000); }
1389 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1391 addr16_hi3(unsigned char* view, Address value)
1392 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1394 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1396 addr16_ha3(unsigned char* view, Address value)
1397 { This::addr16_hi3(view, value + 0x8000); }
1399 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1400 static inline Status
1401 addr14(unsigned char* view, Address value, Overflow_check overflow)
1403 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1404 if (overflow != CHECK_NONE && (value & 3) != 0)
1405 stat = STATUS_OVERFLOW;
1410 // Stash away the index of .got2 or .opd in a relocatable object, if
1411 // such a section exists.
1413 template<int size, bool big_endian>
1415 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1416 Read_symbols_data* sd)
1418 const unsigned char* const pshdrs = sd->section_headers->data();
1419 const unsigned char* namesu = sd->section_names->data();
1420 const char* names = reinterpret_cast<const char*>(namesu);
1421 section_size_type names_size = sd->section_names_size;
1422 const unsigned char* s;
1424 s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1425 names, names_size, NULL);
1428 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1429 this->special_ = ndx;
1431 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1434 // Examine .rela.opd to build info about function entry points.
1436 template<int size, bool big_endian>
1438 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1440 const unsigned char* prelocs,
1441 const unsigned char* plocal_syms)
1445 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1447 const int reloc_size
1448 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1449 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1450 Address expected_off = 0;
1451 bool regular = true;
1452 unsigned int opd_ent_size = 0;
1454 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1456 Reltype reloc(prelocs);
1457 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1458 = reloc.get_r_info();
1459 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1460 if (r_type == elfcpp::R_PPC64_ADDR64)
1462 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1463 typename elfcpp::Elf_types<size>::Elf_Addr value;
1466 if (r_sym < this->local_symbol_count())
1468 typename elfcpp::Sym<size, big_endian>
1469 lsym(plocal_syms + r_sym * sym_size);
1470 shndx = lsym.get_st_shndx();
1471 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1472 value = lsym.get_st_value();
1475 shndx = this->symbol_section_and_value(r_sym, &value,
1477 this->set_opd_ent(reloc.get_r_offset(), shndx,
1478 value + reloc.get_r_addend());
1481 expected_off = reloc.get_r_offset();
1482 opd_ent_size = expected_off;
1484 else if (expected_off != reloc.get_r_offset())
1486 expected_off += opd_ent_size;
1488 else if (r_type == elfcpp::R_PPC64_TOC)
1490 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1495 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1496 this->name().c_str(), r_type);
1500 if (reloc_count <= 2)
1501 opd_ent_size = this->section_size(this->opd_shndx());
1502 if (opd_ent_size != 24 && opd_ent_size != 16)
1506 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1507 this->name().c_str());
1513 template<int size, bool big_endian>
1515 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1517 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1520 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1521 p != rd->relocs.end();
1524 if (p->data_shndx == this->opd_shndx())
1526 uint64_t opd_size = this->section_size(this->opd_shndx());
1527 gold_assert(opd_size == static_cast<size_t>(opd_size));
1530 this->init_opd(opd_size);
1531 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1532 rd->local_symbols->data());
1540 // Set up some symbols.
1542 template<int size, bool big_endian>
1544 Target_powerpc<size, big_endian>::do_define_standard_symbols(
1545 Symbol_table* symtab,
1550 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1551 // undefined when scanning relocs (and thus requires
1552 // non-relative dynamic relocs). The proper value will be
1554 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1555 if (gotsym != NULL && gotsym->is_undefined())
1557 Target_powerpc<size, big_endian>* target =
1558 static_cast<Target_powerpc<size, big_endian>*>(
1559 parameters->sized_target<size, big_endian>());
1560 Output_data_got_powerpc<size, big_endian>* got
1561 = target->got_section(symtab, layout);
1562 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1563 Symbol_table::PREDEFINED,
1567 elfcpp::STV_HIDDEN, 0,
1571 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1572 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
1573 if (sdasym != NULL && sdasym->is_undefined())
1575 Output_data_space* sdata = new Output_data_space(4, "** sdata");
1577 = layout->add_output_section_data(".sdata", 0,
1579 | elfcpp::SHF_WRITE,
1580 sdata, ORDER_SMALL_DATA, false);
1581 symtab->define_in_output_data("_SDA_BASE_", NULL,
1582 Symbol_table::PREDEFINED,
1583 os, 32768, 0, elfcpp::STT_OBJECT,
1584 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
1590 // Set up PowerPC target specific relobj.
1592 template<int size, bool big_endian>
1594 Target_powerpc<size, big_endian>::do_make_elf_object(
1595 const std::string& name,
1596 Input_file* input_file,
1597 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1599 int et = ehdr.get_e_type();
1600 // ET_EXEC files are valid input for --just-symbols/-R,
1601 // and we treat them as relocatable objects.
1602 if (et == elfcpp::ET_REL
1603 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1605 Powerpc_relobj<size, big_endian>* obj =
1606 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1610 else if (et == elfcpp::ET_DYN)
1612 Sized_dynobj<size, big_endian>* obj =
1613 new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1619 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1624 template<int size, bool big_endian>
1625 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1628 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1629 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1631 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1632 : Output_data_got<size, big_endian>(),
1633 symtab_(symtab), layout_(layout),
1634 header_ent_cnt_(size == 32 ? 3 : 1),
1635 header_index_(size == 32 ? 0x2000 : 0)
1640 // Create a new GOT entry and return its offset.
1642 add_got_entry(Got_entry got_entry)
1644 this->reserve_ent();
1645 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1648 // Create a pair of new GOT entries and return the offset of the first.
1650 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1652 this->reserve_ent(2);
1653 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1658 add_constant_pair(Valtype c1, Valtype c2)
1660 this->reserve_ent(2);
1661 unsigned int got_offset = this->add_constant(c1);
1662 this->add_constant(c2);
1666 // Offset of _GLOBAL_OFFSET_TABLE_.
1670 return this->got_offset(this->header_index_);
1673 // Offset of base used to access the GOT/TOC.
1674 // The got/toc pointer reg will be set to this value.
1676 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1679 return this->g_o_t();
1681 return (this->output_section()->address()
1682 + object->toc_base_offset()
1686 // Ensure our GOT has a header.
1688 set_final_data_size()
1690 if (this->header_ent_cnt_ != 0)
1691 this->make_header();
1692 Output_data_got<size, big_endian>::set_final_data_size();
1695 // First word of GOT header needs some values that are not
1696 // handled by Output_data_got so poke them in here.
1697 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1699 do_write(Output_file* of)
1702 if (size == 32 && this->layout_->dynamic_data() != NULL)
1703 val = this->layout_->dynamic_section()->address();
1705 val = this->output_section()->address() + 0x8000;
1706 this->replace_constant(this->header_index_, val);
1707 Output_data_got<size, big_endian>::do_write(of);
1712 reserve_ent(unsigned int cnt = 1)
1714 if (this->header_ent_cnt_ == 0)
1716 if (this->num_entries() + cnt > this->header_index_)
1717 this->make_header();
1723 this->header_ent_cnt_ = 0;
1724 this->header_index_ = this->num_entries();
1727 Output_data_got<size, big_endian>::add_constant(0);
1728 Output_data_got<size, big_endian>::add_constant(0);
1729 Output_data_got<size, big_endian>::add_constant(0);
1731 // Define _GLOBAL_OFFSET_TABLE_ at the header
1732 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1735 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
1736 sym->set_value(this->g_o_t());
1739 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1740 Symbol_table::PREDEFINED,
1741 this, this->g_o_t(), 0,
1744 elfcpp::STV_HIDDEN, 0,
1748 Output_data_got<size, big_endian>::add_constant(0);
1751 // Stashed pointers.
1752 Symbol_table* symtab_;
1756 unsigned int header_ent_cnt_;
1757 // GOT header index.
1758 unsigned int header_index_;
1761 // Get the GOT section, creating it if necessary.
1763 template<int size, bool big_endian>
1764 Output_data_got_powerpc<size, big_endian>*
1765 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1768 if (this->got_ == NULL)
1770 gold_assert(symtab != NULL && layout != NULL);
1773 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1775 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1776 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1777 this->got_, ORDER_DATA, false);
1783 // Get the dynamic reloc section, creating it if necessary.
1785 template<int size, bool big_endian>
1786 typename Target_powerpc<size, big_endian>::Reloc_section*
1787 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1789 if (this->rela_dyn_ == NULL)
1791 gold_assert(layout != NULL);
1792 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1793 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1794 elfcpp::SHF_ALLOC, this->rela_dyn_,
1795 ORDER_DYNAMIC_RELOCS, false);
1797 return this->rela_dyn_;
1803 // Determine the stub group size. The group size is the absolute
1804 // value of the parameter --stub-group-size. If --stub-group-size
1805 // is passed a negative value, we restrict stubs to be always before
1806 // the stubbed branches.
1807 Stub_control(int32_t size)
1808 : state_(NO_GROUP), stub_group_size_(abs(size)),
1809 stub14_group_size_(abs(size)),
1810 stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
1811 group_end_addr_(0), owner_(NULL), output_section_(NULL)
1813 if (stub_group_size_ == 1)
1816 if (stubs_always_before_branch_)
1818 stub_group_size_ = 0x1e00000;
1819 stub14_group_size_ = 0x7800;
1823 stub_group_size_ = 0x1c00000;
1824 stub14_group_size_ = 0x7000;
1826 suppress_size_errors_ = true;
1830 // Return true iff input section can be handled by current stub
1833 can_add_to_stub_group(Output_section* o,
1834 const Output_section::Input_section* i,
1837 const Output_section::Input_section*
1843 { return output_section_; }
1849 FINDING_STUB_SECTION,
1854 uint32_t stub_group_size_;
1855 uint32_t stub14_group_size_;
1856 bool stubs_always_before_branch_;
1857 bool suppress_size_errors_;
1858 uint64_t group_end_addr_;
1859 const Output_section::Input_section* owner_;
1860 Output_section* output_section_;
1863 // Return true iff input section can be handled by current stub/
1867 Stub_control::can_add_to_stub_group(Output_section* o,
1868 const Output_section::Input_section* i,
1872 = has14 ? this->stub14_group_size_ : this->stub_group_size_;
1873 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
1875 uint64_t start_addr = o->address();
1878 // .init and .fini sections are pasted together to form a single
1879 // function. We can't be adding stubs in the middle of the function.
1880 this_size = o->data_size();
1883 start_addr += i->relobj()->output_section_offset(i->shndx());
1884 this_size = i->data_size();
1886 uint64_t end_addr = start_addr + this_size;
1887 bool toobig = this_size > group_size;
1889 if (toobig && !this->suppress_size_errors_)
1890 gold_warning(_("%s:%s exceeds group size"),
1891 i->relobj()->name().c_str(),
1892 i->relobj()->section_name(i->shndx()).c_str());
1894 if (this->state_ != HAS_STUB_SECTION
1895 && (!whole_sec || this->output_section_ != o))
1898 this->output_section_ = o;
1901 if (this->state_ == NO_GROUP)
1903 this->state_ = FINDING_STUB_SECTION;
1904 this->group_end_addr_ = end_addr;
1906 else if (this->group_end_addr_ - start_addr < group_size)
1908 // Adding this section would make the group larger than GROUP_SIZE.
1909 else if (this->state_ == FINDING_STUB_SECTION
1910 && !this->stubs_always_before_branch_
1913 // But wait, there's more! Input sections up to GROUP_SIZE
1914 // bytes before the stub table can be handled by it too.
1915 this->state_ = HAS_STUB_SECTION;
1916 this->group_end_addr_ = end_addr;
1920 this->state_ = NO_GROUP;
1926 // Look over all the input sections, deciding where to place stubs.
1928 template<int size, bool big_endian>
1930 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
1933 Stub_control stub_control(parameters->options().stub_group_size());
1935 // Group input sections and insert stub table
1936 Stub_table<size, big_endian>* stub_table = NULL;
1937 Layout::Section_list section_list;
1938 layout->get_executable_sections(§ion_list);
1939 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
1940 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
1941 o != section_list.rend();
1944 typedef Output_section::Input_section_list Input_section_list;
1945 for (Input_section_list::const_reverse_iterator i
1946 = (*o)->input_sections().rbegin();
1947 i != (*o)->input_sections().rend();
1950 if (i->is_input_section())
1952 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1953 <Powerpc_relobj<size, big_endian>*>(i->relobj());
1954 bool has14 = ppcobj->has_14bit_branch(i->shndx());
1955 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
1957 stub_table->init(stub_control.owner(),
1958 stub_control.output_section());
1961 if (stub_table == NULL)
1962 stub_table = this->new_stub_table();
1963 ppcobj->set_stub_table(i->shndx(), stub_table);
1967 if (stub_table != NULL)
1968 stub_table->init(stub_control.owner(), stub_control.output_section());
1971 // If this branch needs a plt call stub, or a long branch stub, make one.
1973 template<int size, bool big_endian>
1975 Target_powerpc<size, big_endian>::Branch_info::make_stub(
1976 Stub_table<size, big_endian>* stub_table,
1977 Stub_table<size, big_endian>* ifunc_stub_table,
1978 Symbol_table* symtab) const
1980 Symbol* sym = this->object_->global_symbol(this->r_sym_);
1981 if (sym != NULL && sym->is_forwarder())
1982 sym = symtab->resolve_forwards(sym);
1983 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
1985 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(this->r_type_))
1986 : this->object_->local_has_plt_offset(this->r_sym_))
1988 if (stub_table == NULL)
1989 stub_table = this->object_->stub_table(this->shndx_);
1990 if (stub_table == NULL)
1992 // This is a ref from a data section to an ifunc symbol.
1993 stub_table = ifunc_stub_table;
1995 gold_assert(stub_table != NULL);
1997 stub_table->add_plt_call_entry(this->object_, gsym,
1998 this->r_type_, this->addend_);
2000 stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2001 this->r_type_, this->addend_);
2005 unsigned int max_branch_offset;
2006 if (this->r_type_ == elfcpp::R_POWERPC_REL14
2007 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2008 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2009 max_branch_offset = 1 << 15;
2010 else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2011 || this->r_type_ == elfcpp::R_PPC_PLTREL24
2012 || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2013 max_branch_offset = 1 << 25;
2016 Address from = this->object_->get_output_section_offset(this->shndx_);
2017 gold_assert(from != invalid_address);
2018 from += (this->object_->output_section(this->shndx_)->address()
2023 switch (gsym->source())
2025 case Symbol::FROM_OBJECT:
2027 Object* symobj = gsym->object();
2028 if (symobj->is_dynamic()
2029 || symobj->pluginobj() != NULL)
2032 unsigned int shndx = gsym->shndx(&is_ordinary);
2033 if (shndx == elfcpp::SHN_UNDEF)
2038 case Symbol::IS_UNDEFINED:
2044 Symbol_table::Compute_final_value_status status;
2045 to = symtab->compute_final_value<size>(gsym, &status);
2046 if (status != Symbol_table::CFVS_OK)
2051 const Symbol_value<size>* psymval
2052 = this->object_->local_symbol(this->r_sym_);
2053 Symbol_value<size> symval;
2054 typedef Sized_relobj_file<size, big_endian> ObjType;
2055 typename ObjType::Compute_final_local_value_status status
2056 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2058 if (status != ObjType::CFLV_OK
2059 || !symval.has_output_value())
2061 to = symval.value(this->object_, 0);
2063 if (stub_table == NULL)
2064 stub_table = this->object_->stub_table(this->shndx_);
2065 gold_assert(stub_table != NULL);
2066 if (size == 64 && is_branch_reloc(this->r_type_))
2068 unsigned int dest_shndx;
2069 to = stub_table->targ()->symval_for_branch(to, gsym, this->object_,
2072 Address delta = to - from;
2073 if (delta + max_branch_offset >= 2 * max_branch_offset)
2075 stub_table->add_long_branch_entry(this->object_, to);
2080 // Relaxation hook. This is where we do stub generation.
2082 template<int size, bool big_endian>
2084 Target_powerpc<size, big_endian>::do_relax(int pass,
2085 const Input_objects*,
2086 Symbol_table* symtab,
2090 unsigned int prev_brlt_size = 0;
2092 this->group_sections(layout, task);
2095 prev_brlt_size = this->branch_lookup_table_.size();
2096 this->branch_lookup_table_.clear();
2097 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2098 p != this->stub_tables_.end();
2101 (*p)->clear_long_branch_stubs();
2105 // We need address of stub tables valid for make_stub.
2106 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2107 p != this->stub_tables_.end();
2110 const Powerpc_relobj<size, big_endian>* object
2111 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2112 Address off = object->get_output_section_offset((*p)->shndx());
2113 gold_assert(off != invalid_address);
2114 Output_section* os = (*p)->output_section();
2115 (*p)->set_address_and_size(os, off);
2118 Stub_table<size, big_endian>* ifunc_stub_table
2119 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2120 Stub_table<size, big_endian>* one_stub_table
2121 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2122 for (typename Branches::const_iterator b = this->branch_info_.begin();
2123 b != this->branch_info_.end();
2126 b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2129 unsigned int num_huge_branches = this->branch_lookup_table_.size();
2130 bool again = num_huge_branches != prev_brlt_size;
2131 if (size == 64 && num_huge_branches != 0)
2132 this->make_brlt_section(layout);
2133 if (size == 64 && again)
2134 this->brlt_section_->set_current_size(num_huge_branches);
2136 typedef Unordered_set<Output_section*> Output_sections;
2137 Output_sections os_need_update;
2138 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2139 p != this->stub_tables_.end();
2142 if ((*p)->size_update())
2145 os_need_update.insert((*p)->output_section());
2149 for (typename Output_sections::iterator p = os_need_update.begin();
2150 p != os_need_update.end();
2153 Output_section* os = *p;
2155 typedef Output_section::Input_section_list Input_section_list;
2156 for (Input_section_list::const_iterator i = os->input_sections().begin();
2157 i != os->input_sections().end();
2160 off = align_address(off, i->addralign());
2161 if (i->is_input_section() || i->is_relaxed_input_section())
2162 i->relobj()->set_section_offset(i->shndx(), off);
2163 if (i->is_relaxed_input_section())
2165 Stub_table<size, big_endian>* stub_table
2166 = static_cast<Stub_table<size, big_endian>*>(
2167 i->relaxed_input_section());
2168 off += stub_table->set_address_and_size(os, off);
2171 off += i->data_size();
2173 // If .brlt is part of this output section, then we have just
2174 // done the offset adjustment.
2175 os->clear_section_offsets_need_adjustment();
2180 && num_huge_branches != 0
2181 && parameters->options().output_is_position_independent())
2183 // Fill in the BRLT relocs.
2184 this->brlt_section_->reset_data_size();
2185 for (typename Branch_lookup_table::const_iterator p
2186 = this->branch_lookup_table_.begin();
2187 p != this->branch_lookup_table_.end();
2190 this->brlt_section_->add_reloc(p->first, p->second);
2192 this->brlt_section_->finalize_data_size();
2197 // A class to handle the PLT data.
2199 template<int size, bool big_endian>
2200 class Output_data_plt_powerpc : public Output_section_data_build
2203 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2204 size, big_endian> Reloc_section;
2206 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2207 Reloc_section* plt_rel,
2208 unsigned int reserved_size,
2210 : Output_section_data_build(size == 32 ? 4 : 8),
2213 initial_plt_entry_size_(reserved_size),
2217 // Add an entry to the PLT.
2222 add_ifunc_entry(Symbol*);
2225 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2227 // Return the .rela.plt section data.
2234 // Return the number of PLT entries.
2238 return ((this->current_data_size() - this->initial_plt_entry_size_)
2242 // Return the offset of the first non-reserved PLT entry.
2244 first_plt_entry_offset()
2245 { return this->initial_plt_entry_size_; }
2247 // Return the size of a PLT entry.
2249 get_plt_entry_size()
2250 { return plt_entry_size; }
2254 do_adjust_output_section(Output_section* os)
2259 // Write to a map file.
2261 do_print_to_mapfile(Mapfile* mapfile) const
2262 { mapfile->print_output_data(this, this->name_); }
2265 // The size of an entry in the PLT.
2266 static const int plt_entry_size = size == 32 ? 4 : 24;
2268 // Write out the PLT data.
2270 do_write(Output_file*);
2272 // The reloc section.
2273 Reloc_section* rel_;
2274 // Allows access to .glink for do_write.
2275 Target_powerpc<size, big_endian>* targ_;
2276 // The size of the first reserved entry.
2277 int initial_plt_entry_size_;
2278 // What to report in map file.
2282 // Add an entry to the PLT.
2284 template<int size, bool big_endian>
2286 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
2288 if (!gsym->has_plt_offset())
2290 section_size_type off = this->current_data_size();
2292 off += this->first_plt_entry_offset();
2293 gsym->set_plt_offset(off);
2294 gsym->set_needs_dynsym_entry();
2295 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
2296 this->rel_->add_global(gsym, dynrel, this, off, 0);
2297 off += plt_entry_size;
2298 this->set_current_data_size(off);
2302 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2304 template<int size, bool big_endian>
2306 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
2308 if (!gsym->has_plt_offset())
2310 section_size_type off = this->current_data_size();
2311 gsym->set_plt_offset(off);
2312 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2314 dynrel = elfcpp::R_PPC64_JMP_IREL;
2315 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
2316 off += plt_entry_size;
2317 this->set_current_data_size(off);
2321 // Add an entry for a local ifunc symbol to the IPLT.
2323 template<int size, bool big_endian>
2325 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
2326 Sized_relobj_file<size, big_endian>* relobj,
2327 unsigned int local_sym_index)
2329 if (!relobj->local_has_plt_offset(local_sym_index))
2331 section_size_type off = this->current_data_size();
2332 relobj->set_local_plt_offset(local_sym_index, off);
2333 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2335 dynrel = elfcpp::R_PPC64_JMP_IREL;
2336 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
2338 off += plt_entry_size;
2339 this->set_current_data_size(off);
2343 static const uint32_t add_0_11_11 = 0x7c0b5a14;
2344 static const uint32_t add_3_3_2 = 0x7c631214;
2345 static const uint32_t add_3_3_13 = 0x7c636a14;
2346 static const uint32_t add_11_0_11 = 0x7d605a14;
2347 static const uint32_t add_12_2_11 = 0x7d825a14;
2348 static const uint32_t addi_11_11 = 0x396b0000;
2349 static const uint32_t addi_12_12 = 0x398c0000;
2350 static const uint32_t addi_2_2 = 0x38420000;
2351 static const uint32_t addi_3_2 = 0x38620000;
2352 static const uint32_t addi_3_3 = 0x38630000;
2353 static const uint32_t addis_0_2 = 0x3c020000;
2354 static const uint32_t addis_0_13 = 0x3c0d0000;
2355 static const uint32_t addis_11_11 = 0x3d6b0000;
2356 static const uint32_t addis_11_30 = 0x3d7e0000;
2357 static const uint32_t addis_12_12 = 0x3d8c0000;
2358 static const uint32_t addis_12_2 = 0x3d820000;
2359 static const uint32_t addis_3_2 = 0x3c620000;
2360 static const uint32_t addis_3_13 = 0x3c6d0000;
2361 static const uint32_t b = 0x48000000;
2362 static const uint32_t bcl_20_31 = 0x429f0005;
2363 static const uint32_t bctr = 0x4e800420;
2364 static const uint32_t blr = 0x4e800020;
2365 static const uint32_t blrl = 0x4e800021;
2366 static const uint32_t cror_15_15_15 = 0x4def7b82;
2367 static const uint32_t cror_31_31_31 = 0x4ffffb82;
2368 static const uint32_t ld_0_1 = 0xe8010000;
2369 static const uint32_t ld_0_12 = 0xe80c0000;
2370 static const uint32_t ld_11_12 = 0xe96c0000;
2371 static const uint32_t ld_11_2 = 0xe9620000;
2372 static const uint32_t ld_2_1 = 0xe8410000;
2373 static const uint32_t ld_2_11 = 0xe84b0000;
2374 static const uint32_t ld_2_12 = 0xe84c0000;
2375 static const uint32_t ld_2_2 = 0xe8420000;
2376 static const uint32_t lfd_0_1 = 0xc8010000;
2377 static const uint32_t li_0_0 = 0x38000000;
2378 static const uint32_t li_12_0 = 0x39800000;
2379 static const uint32_t lis_0_0 = 0x3c000000;
2380 static const uint32_t lis_11 = 0x3d600000;
2381 static const uint32_t lis_12 = 0x3d800000;
2382 static const uint32_t lwz_0_12 = 0x800c0000;
2383 static const uint32_t lwz_11_11 = 0x816b0000;
2384 static const uint32_t lwz_11_30 = 0x817e0000;
2385 static const uint32_t lwz_12_12 = 0x818c0000;
2386 static const uint32_t lwzu_0_12 = 0x840c0000;
2387 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
2388 static const uint32_t mflr_0 = 0x7c0802a6;
2389 static const uint32_t mflr_11 = 0x7d6802a6;
2390 static const uint32_t mflr_12 = 0x7d8802a6;
2391 static const uint32_t mtctr_0 = 0x7c0903a6;
2392 static const uint32_t mtctr_11 = 0x7d6903a6;
2393 static const uint32_t mtctr_12 = 0x7d8903a6;
2394 static const uint32_t mtlr_0 = 0x7c0803a6;
2395 static const uint32_t mtlr_12 = 0x7d8803a6;
2396 static const uint32_t nop = 0x60000000;
2397 static const uint32_t ori_0_0_0 = 0x60000000;
2398 static const uint32_t std_0_1 = 0xf8010000;
2399 static const uint32_t std_0_12 = 0xf80c0000;
2400 static const uint32_t std_2_1 = 0xf8410000;
2401 static const uint32_t stfd_0_1 = 0xd8010000;
2402 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
2403 static const uint32_t sub_11_11_12 = 0x7d6c5850;
2405 // Write out the PLT.
2407 template<int size, bool big_endian>
2409 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
2413 const section_size_type offset = this->offset();
2414 const section_size_type oview_size
2415 = convert_to_section_size_type(this->data_size());
2416 unsigned char* const oview = of->get_output_view(offset, oview_size);
2417 unsigned char* pov = oview;
2418 unsigned char* endpov = oview + oview_size;
2420 // The address of the .glink branch table
2421 const Output_data_glink<size, big_endian>* glink
2422 = this->targ_->glink_section();
2423 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
2425 while (pov < endpov)
2427 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
2432 of->write_output_view(offset, oview_size, oview);
2436 // Create the PLT section.
2438 template<int size, bool big_endian>
2440 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
2443 if (this->plt_ == NULL)
2445 if (this->got_ == NULL)
2446 this->got_section(symtab, layout);
2448 if (this->glink_ == NULL)
2449 make_glink_section(layout);
2451 // Ensure that .rela.dyn always appears before .rela.plt This is
2452 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2453 // needs to include .rela.plt in it's range.
2454 this->rela_dyn_section(layout);
2456 Reloc_section* plt_rel = new Reloc_section(false);
2457 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
2458 elfcpp::SHF_ALLOC, plt_rel,
2459 ORDER_DYNAMIC_PLT_RELOCS, false);
2461 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
2462 size == 32 ? 0 : 24,
2464 layout->add_output_section_data(".plt",
2466 ? elfcpp::SHT_PROGBITS
2467 : elfcpp::SHT_NOBITS),
2468 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2477 // Create the IPLT section.
2479 template<int size, bool big_endian>
2481 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
2484 if (this->iplt_ == NULL)
2486 this->make_plt_section(symtab, layout);
2488 Reloc_section* iplt_rel = new Reloc_section(false);
2489 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
2491 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
2493 this->plt_->output_section()->add_output_section_data(this->iplt_);
2497 // A section for huge long branch addresses, similar to plt section.
2499 template<int size, bool big_endian>
2500 class Output_data_brlt_powerpc : public Output_section_data_build
2503 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2504 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2505 size, big_endian> Reloc_section;
2507 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
2508 Reloc_section* brlt_rel)
2509 : Output_section_data_build(size == 32 ? 4 : 8),
2514 // Add a reloc for an entry in the BRLT.
2516 add_reloc(Address to, unsigned int off)
2517 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
2519 // Update section and reloc section size.
2521 set_current_size(unsigned int num_branches)
2523 this->reset_address_and_file_offset();
2524 this->set_current_data_size(num_branches * 16);
2525 this->finalize_data_size();
2526 Output_section* os = this->output_section();
2527 os->set_section_offsets_need_adjustment();
2528 if (this->rel_ != NULL)
2530 unsigned int reloc_size
2531 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2532 this->rel_->reset_address_and_file_offset();
2533 this->rel_->set_current_data_size(num_branches * reloc_size);
2534 this->rel_->finalize_data_size();
2535 Output_section* os = this->rel_->output_section();
2536 os->set_section_offsets_need_adjustment();
2542 do_adjust_output_section(Output_section* os)
2547 // Write to a map file.
2549 do_print_to_mapfile(Mapfile* mapfile) const
2550 { mapfile->print_output_data(this, "** BRLT"); }
2553 // Write out the BRLT data.
2555 do_write(Output_file*);
2557 // The reloc section.
2558 Reloc_section* rel_;
2559 Target_powerpc<size, big_endian>* targ_;
2562 // Make the branch lookup table section.
2564 template<int size, bool big_endian>
2566 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
2568 if (size == 64 && this->brlt_section_ == NULL)
2570 Reloc_section* brlt_rel = NULL;
2571 bool is_pic = parameters->options().output_is_position_independent();
2574 // When PIC we can't fill in .brlt (like .plt it can be a
2575 // bss style section) but must initialise at runtime via
2576 // dynamic relocats.
2577 this->rela_dyn_section(layout);
2578 brlt_rel = new Reloc_section(false);
2579 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
2582 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
2583 if (this->plt_ && is_pic)
2584 this->plt_->output_section()
2585 ->add_output_section_data(this->brlt_section_);
2587 layout->add_output_section_data(".brlt",
2588 (is_pic ? elfcpp::SHT_NOBITS
2589 : elfcpp::SHT_PROGBITS),
2590 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2591 this->brlt_section_,
2592 (is_pic ? ORDER_SMALL_BSS
2593 : ORDER_SMALL_DATA),
2598 // Write out .brlt when non-PIC.
2600 template<int size, bool big_endian>
2602 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
2604 if (size == 64 && !parameters->options().output_is_position_independent())
2606 const section_size_type offset = this->offset();
2607 const section_size_type oview_size
2608 = convert_to_section_size_type(this->data_size());
2609 unsigned char* const oview = of->get_output_view(offset, oview_size);
2611 this->targ_->write_branch_lookup_table(oview);
2612 of->write_output_view(offset, oview_size, oview);
2616 // Stub_table holds information about plt and long branch stubs.
2617 // Stubs are built in an area following some input section determined
2618 // by group_sections(). This input section is converted to a relaxed
2619 // input section allowing it to be resized to accommodate the stubs
2621 template<int size, bool big_endian>
2622 class Stub_table : public Output_relaxed_input_section
2625 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2626 static const Address invalid_address = static_cast<Address>(0) - 1;
2628 Stub_table(Target_powerpc<size, big_endian>* targ)
2629 : Output_relaxed_input_section(NULL, 0, 0),
2630 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
2631 orig_data_size_(0), plt_size_(0), branch_size_(0), prev_size_(0)
2634 // Delayed Output_relaxed_input_section init.
2636 init(const Output_section::Input_section*, Output_section*);
2638 // Add a plt call stub.
2640 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2646 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2651 // Find a given plt call stub.
2653 find_plt_call_entry(const Symbol*) const;
2656 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2657 unsigned int) const;
2660 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2666 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2671 // Add a long branch stub.
2673 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
2676 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
2679 clear_long_branch_stubs()
2681 this->long_branch_stubs_.clear();
2682 this->branch_size_ = 0;
2686 set_address_and_size(const Output_section* os, Address off)
2688 Address start_off = off;
2689 off += this->orig_data_size_;
2690 Address my_size = this->plt_size_ + this->branch_size_;
2692 off = align_address(off, this->stub_align());
2693 // Include original section size and alignment padding in size
2694 my_size += off - start_off;
2695 this->reset_address_and_file_offset();
2696 this->set_current_data_size(my_size);
2697 this->set_address_and_file_offset(os->address() + start_off,
2698 os->offset() + start_off);
2705 return align_address(this->address() + this->orig_data_size_,
2706 this->stub_align());
2712 return align_address(this->offset() + this->orig_data_size_,
2713 this->stub_align());
2718 { return this->plt_size_; }
2723 Output_section* os = this->output_section();
2724 if (os->addralign() < this->stub_align())
2726 os->set_addralign(this->stub_align());
2727 // FIXME: get rid of the insane checkpointing.
2728 // We can't increase alignment of the input section to which
2729 // stubs are attached; The input section may be .init which
2730 // is pasted together with other .init sections to form a
2731 // function. Aligning might insert zero padding resulting in
2732 // sigill. However we do need to increase alignment of the
2733 // output section so that the align_address() on offset in
2734 // set_address_and_size() adds the same padding as the
2735 // align_address() on address in stub_address().
2736 // What's more, we need this alignment for the layout done in
2737 // relaxation_loop_body() so that the output section starts at
2738 // a suitably aligned address.
2739 os->checkpoint_set_addralign(this->stub_align());
2741 if (this->prev_size_ != this->plt_size_ + this->branch_size_)
2743 this->prev_size_ = this->plt_size_ + this->branch_size_;
2751 { return this->prev_size_; }
2754 set_prev_size(section_size_type val)
2755 { this->prev_size_ = val; }
2757 Target_powerpc<size, big_endian>*
2764 { return size == 32 ? 16 : 32; }
2766 // We keep plt stubs aligned, so no fancy sizing.
2768 plt_call_size() const
2769 { return size == 32 ? 16 : 32; }
2771 // Return long branch stub size.
2773 branch_stub_size(Address to)
2775 Address loc = this->stub_address() + this->plt_size_ + this->branch_size_;
2776 if (loc - to + (1 << 25) < 2 << 25)
2778 if (size == 64 || !parameters->options().output_is_position_independent())
2785 do_write(Output_file*);
2787 // Plt call stub keys.
2791 Plt_stub_ent(const Symbol* sym)
2792 : sym_(sym), object_(0), addend_(0), locsym_(0)
2795 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
2796 unsigned int locsym_index)
2797 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
2800 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
2802 unsigned int r_type,
2804 : sym_(sym), object_(0), addend_(0), locsym_(0)
2807 this->addend_ = addend;
2808 else if (parameters->options().output_is_position_independent()
2809 && r_type == elfcpp::R_PPC_PLTREL24)
2811 this->addend_ = addend;
2812 if (this->addend_ >= 32768)
2813 this->object_ = object;
2817 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
2818 unsigned int locsym_index,
2819 unsigned int r_type,
2821 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
2824 this->addend_ = addend;
2825 else if (parameters->options().output_is_position_independent()
2826 && r_type == elfcpp::R_PPC_PLTREL24)
2827 this->addend_ = addend;
2830 bool operator==(const Plt_stub_ent& that) const
2832 return (this->sym_ == that.sym_
2833 && this->object_ == that.object_
2834 && this->addend_ == that.addend_
2835 && this->locsym_ == that.locsym_);
2839 const Sized_relobj_file<size, big_endian>* object_;
2840 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
2841 unsigned int locsym_;
2844 class Plt_stub_ent_hash
2847 size_t operator()(const Plt_stub_ent& ent) const
2849 return (reinterpret_cast<uintptr_t>(ent.sym_)
2850 ^ reinterpret_cast<uintptr_t>(ent.object_)
2856 // Long branch stub keys.
2857 class Branch_stub_ent
2860 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
2861 : dest_(to), toc_base_off_(0)
2864 toc_base_off_ = obj->toc_base_offset();
2867 bool operator==(const Branch_stub_ent& that) const
2869 return (this->dest_ == that.dest_
2871 || this->toc_base_off_ == that.toc_base_off_));
2875 unsigned int toc_base_off_;
2878 class Branch_stub_ent_hash
2881 size_t operator()(const Branch_stub_ent& ent) const
2882 { return ent.dest_ ^ ent.toc_base_off_; }
2885 // In a sane world this would be a global.
2886 Target_powerpc<size, big_endian>* targ_;
2887 // Map sym/object/addend to stub offset.
2888 typedef Unordered_map<Plt_stub_ent, unsigned int,
2889 Plt_stub_ent_hash> Plt_stub_entries;
2890 Plt_stub_entries plt_call_stubs_;
2891 // Map destination address to stub offset.
2892 typedef Unordered_map<Branch_stub_ent, unsigned int,
2893 Branch_stub_ent_hash> Branch_stub_entries;
2894 Branch_stub_entries long_branch_stubs_;
2895 // size of input section
2896 section_size_type orig_data_size_;
2898 section_size_type plt_size_, branch_size_, prev_size_;
2901 // Make a new stub table, and record.
2903 template<int size, bool big_endian>
2904 Stub_table<size, big_endian>*
2905 Target_powerpc<size, big_endian>::new_stub_table()
2907 Stub_table<size, big_endian>* stub_table
2908 = new Stub_table<size, big_endian>(this);
2909 this->stub_tables_.push_back(stub_table);
2913 // Delayed stub table initialisation, because we create the stub table
2914 // before we know to which section it will be attached.
2916 template<int size, bool big_endian>
2918 Stub_table<size, big_endian>::init(
2919 const Output_section::Input_section* owner,
2920 Output_section* output_section)
2922 this->set_relobj(owner->relobj());
2923 this->set_shndx(owner->shndx());
2924 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
2925 this->set_output_section(output_section);
2926 this->orig_data_size_ = owner->current_data_size();
2928 std::vector<Output_relaxed_input_section*> new_relaxed;
2929 new_relaxed.push_back(this);
2930 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
2933 // Add a plt call stub, if we do not already have one for this
2934 // sym/object/addend combo.
2936 template<int size, bool big_endian>
2938 Stub_table<size, big_endian>::add_plt_call_entry(
2939 const Sized_relobj_file<size, big_endian>* object,
2941 unsigned int r_type,
2944 Plt_stub_ent ent(object, gsym, r_type, addend);
2945 Address off = this->plt_size_;
2946 if (this->plt_call_stubs_.insert(std::make_pair(ent, off)).second)
2947 this->plt_size_ = off + this->plt_call_size();
2950 template<int size, bool big_endian>
2952 Stub_table<size, big_endian>::add_plt_call_entry(
2953 const Sized_relobj_file<size, big_endian>* object,
2954 unsigned int locsym_index,
2955 unsigned int r_type,
2958 Plt_stub_ent ent(object, locsym_index, r_type, addend);
2959 Address off = this->plt_size_;
2960 if (this->plt_call_stubs_.insert(std::make_pair(ent, off)).second)
2961 this->plt_size_ = off + this->plt_call_size();
2964 // Find a plt call stub.
2966 template<int size, bool big_endian>
2967 typename elfcpp::Elf_types<size>::Elf_Addr
2968 Stub_table<size, big_endian>::find_plt_call_entry(
2969 const Sized_relobj_file<size, big_endian>* object,
2971 unsigned int r_type,
2972 Address addend) const
2974 Plt_stub_ent ent(object, gsym, r_type, addend);
2975 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
2976 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
2979 template<int size, bool big_endian>
2980 typename elfcpp::Elf_types<size>::Elf_Addr
2981 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
2983 Plt_stub_ent ent(gsym);
2984 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
2985 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
2988 template<int size, bool big_endian>
2989 typename elfcpp::Elf_types<size>::Elf_Addr
2990 Stub_table<size, big_endian>::find_plt_call_entry(
2991 const Sized_relobj_file<size, big_endian>* object,
2992 unsigned int locsym_index,
2993 unsigned int r_type,
2994 Address addend) const
2996 Plt_stub_ent ent(object, locsym_index, r_type, addend);
2997 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
2998 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3001 template<int size, bool big_endian>
3002 typename elfcpp::Elf_types<size>::Elf_Addr
3003 Stub_table<size, big_endian>::find_plt_call_entry(
3004 const Sized_relobj_file<size, big_endian>* object,
3005 unsigned int locsym_index) const
3007 Plt_stub_ent ent(object, locsym_index);
3008 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3009 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3012 // Add a long branch stub if we don't already have one to given
3015 template<int size, bool big_endian>
3017 Stub_table<size, big_endian>::add_long_branch_entry(
3018 const Powerpc_relobj<size, big_endian>* object,
3021 Branch_stub_ent ent(object, to);
3022 Address off = this->branch_size_;
3023 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3025 unsigned int stub_size = this->branch_stub_size(to);
3026 this->branch_size_ = off + stub_size;
3027 if (size == 64 && stub_size != 4)
3028 this->targ_->add_branch_lookup_table(to);
3032 // Find long branch stub.
3034 template<int size, bool big_endian>
3035 typename elfcpp::Elf_types<size>::Elf_Addr
3036 Stub_table<size, big_endian>::find_long_branch_entry(
3037 const Powerpc_relobj<size, big_endian>* object,
3040 Branch_stub_ent ent(object, to);
3041 typename Branch_stub_entries::const_iterator p
3042 = this->long_branch_stubs_.find(ent);
3043 return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3046 // A class to handle .glink.
3048 template<int size, bool big_endian>
3049 class Output_data_glink : public Output_section_data
3052 static const int pltresolve_size = 16*4;
3054 Output_data_glink(Target_powerpc<size, big_endian>* targ)
3055 : Output_section_data(16), targ_(targ)
3059 // Write to a map file.
3061 do_print_to_mapfile(Mapfile* mapfile) const
3062 { mapfile->print_output_data(this, _("** glink")); }
3066 set_final_data_size();
3070 do_write(Output_file*);
3072 // Allows access to .got and .plt for do_write.
3073 Target_powerpc<size, big_endian>* targ_;
3076 template<int size, bool big_endian>
3078 Output_data_glink<size, big_endian>::set_final_data_size()
3080 unsigned int count = this->targ_->plt_entry_count();
3081 section_size_type total = 0;
3087 // space for branch table
3088 total += 4 * (count - 1);
3090 total += -total & 15;
3091 total += this->pltresolve_size;
3095 total += this->pltresolve_size;
3097 // space for branch table
3100 total += 4 * (count - 0x8000);
3104 this->set_data_size(total);
3107 static inline uint32_t
3113 static inline uint32_t
3119 static inline uint32_t
3122 return hi(a + 0x8000);
3125 template<bool big_endian>
3127 write_insn(unsigned char* p, uint32_t v)
3129 elfcpp::Swap<32, big_endian>::writeval(p, v);
3132 // Write out plt and long branch stub code.
3134 template<int size, bool big_endian>
3136 Stub_table<size, big_endian>::do_write(Output_file* of)
3138 if (this->plt_call_stubs_.empty()
3139 && this->long_branch_stubs_.empty())
3142 const section_size_type start_off = this->offset();
3143 const section_size_type off = this->stub_offset();
3144 const section_size_type oview_size =
3145 convert_to_section_size_type(this->data_size() - (off - start_off));
3146 unsigned char* const oview = of->get_output_view(off, oview_size);
3149 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3150 static const Address invalid_address = static_cast<Address>(0) - 1;
3154 const Output_data_got_powerpc<size, big_endian>* got
3155 = this->targ_->got_section();
3156 Address got_os_addr = got->output_section()->address();
3158 if (!this->plt_call_stubs_.empty())
3160 // The base address of the .plt section.
3161 Address plt_base = this->targ_->plt_section()->address();
3162 Address iplt_base = invalid_address;
3164 // Write out plt call stubs.
3165 typename Plt_stub_entries::const_iterator cs;
3166 for (cs = this->plt_call_stubs_.begin();
3167 cs != this->plt_call_stubs_.end();
3172 const Symbol* gsym = cs->first.sym_;
3175 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
3176 && gsym->can_use_relative_reloc(false));
3177 plt_addr = gsym->plt_offset();
3182 const Sized_relobj_file<size, big_endian>* relobj
3183 = cs->first.object_;
3184 unsigned int local_sym_index = cs->first.locsym_;
3185 plt_addr = relobj->local_plt_offset(local_sym_index);
3189 if (iplt_base == invalid_address)
3190 iplt_base = this->targ_->iplt_section()->address();
3191 plt_addr += iplt_base;
3194 plt_addr += plt_base;
3195 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3196 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
3197 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
3198 Address pltoff = plt_addr - got_addr;
3200 if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
3201 gold_error(_("%s: linkage table error against `%s'"),
3202 cs->first.object_->name().c_str(),
3203 cs->first.sym_->demangled_name().c_str());
3205 p = oview + cs->second;
3206 if (ha(pltoff) != 0)
3208 write_insn<big_endian>(p, addis_12_2 + ha(pltoff)), p += 4;
3209 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
3210 write_insn<big_endian>(p, ld_11_12 + l(pltoff)), p += 4;
3211 if (ha(pltoff + 16) != ha(pltoff))
3213 write_insn<big_endian>(p, addi_12_12 + l(pltoff)),p += 4;
3216 write_insn<big_endian>(p, mtctr_11), p += 4;
3217 write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)), p += 4;
3218 write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)), p += 4;
3219 write_insn<big_endian>(p, bctr);
3223 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
3224 write_insn<big_endian>(p, ld_11_2 + l(pltoff)), p += 4;
3225 if (ha(pltoff + 16) != ha(pltoff))
3227 write_insn<big_endian>(p, addi_2_2 + l(pltoff)), p += 4;
3230 write_insn<big_endian>(p, mtctr_11), p += 4;
3231 write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)), p += 4;
3232 write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)), p += 4;
3233 write_insn<big_endian>(p, bctr);
3238 // Write out long branch stubs.
3239 typename Branch_stub_entries::const_iterator bs;
3240 for (bs = this->long_branch_stubs_.begin();
3241 bs != this->long_branch_stubs_.end();
3244 p = oview + this->plt_size_ + bs->second;
3245 Address loc = this->stub_address() + this->plt_size_ + bs->second;
3246 Address delta = bs->first.dest_ - loc;
3247 if (delta + (1 << 25) < 2 << 25)
3248 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3252 = this->targ_->find_branch_lookup_table(bs->first.dest_);
3253 gold_assert(brlt_addr != invalid_address);
3254 brlt_addr += this->targ_->brlt_section()->address();
3255 Address got_addr = got_os_addr + bs->first.toc_base_off_;
3256 Address brltoff = brlt_addr - got_addr;
3257 if (ha(brltoff) == 0)
3259 write_insn<big_endian>(p, ld_11_2 + l(brltoff)), p += 4;
3263 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
3264 write_insn<big_endian>(p, ld_11_12 + l(brltoff)), p += 4;
3266 write_insn<big_endian>(p, mtctr_11), p += 4;
3267 write_insn<big_endian>(p, bctr);
3273 if (!this->plt_call_stubs_.empty())
3275 // The base address of the .plt section.
3276 Address plt_base = this->targ_->plt_section()->address();
3277 Address iplt_base = invalid_address;
3278 // The address of _GLOBAL_OFFSET_TABLE_.
3279 Address g_o_t = invalid_address;
3281 // Write out plt call stubs.
3282 typename Plt_stub_entries::const_iterator cs;
3283 for (cs = this->plt_call_stubs_.begin();
3284 cs != this->plt_call_stubs_.end();
3289 const Symbol* gsym = cs->first.sym_;
3292 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
3293 && gsym->can_use_relative_reloc(false));
3294 plt_addr = gsym->plt_offset();
3299 const Sized_relobj_file<size, big_endian>* relobj
3300 = cs->first.object_;
3301 unsigned int local_sym_index = cs->first.locsym_;
3302 plt_addr = relobj->local_plt_offset(local_sym_index);
3306 if (iplt_base == invalid_address)
3307 iplt_base = this->targ_->iplt_section()->address();
3308 plt_addr += iplt_base;
3311 plt_addr += plt_base;
3313 p = oview + cs->second;
3314 if (parameters->options().output_is_position_independent())
3317 const Powerpc_relobj<size, big_endian>* ppcobj
3318 = (static_cast<const Powerpc_relobj<size, big_endian>*>
3319 (cs->first.object_));
3320 if (ppcobj != NULL && cs->first.addend_ >= 32768)
3322 unsigned int got2 = ppcobj->got2_shndx();
3323 got_addr = ppcobj->get_output_section_offset(got2);
3324 gold_assert(got_addr != invalid_address);
3325 got_addr += (ppcobj->output_section(got2)->address()
3326 + cs->first.addend_);
3330 if (g_o_t == invalid_address)
3332 const Output_data_got_powerpc<size, big_endian>* got
3333 = this->targ_->got_section();
3334 g_o_t = got->address() + got->g_o_t();
3339 Address pltoff = plt_addr - got_addr;
3340 if (ha(pltoff) == 0)
3342 write_insn<big_endian>(p + 0, lwz_11_30 + l(pltoff));
3343 write_insn<big_endian>(p + 4, mtctr_11);
3344 write_insn<big_endian>(p + 8, bctr);
3348 write_insn<big_endian>(p + 0, addis_11_30 + ha(pltoff));
3349 write_insn<big_endian>(p + 4, lwz_11_11 + l(pltoff));
3350 write_insn<big_endian>(p + 8, mtctr_11);
3351 write_insn<big_endian>(p + 12, bctr);
3356 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
3357 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
3358 write_insn<big_endian>(p + 8, mtctr_11);
3359 write_insn<big_endian>(p + 12, bctr);
3364 // Write out long branch stubs.
3365 typename Branch_stub_entries::const_iterator bs;
3366 for (bs = this->long_branch_stubs_.begin();
3367 bs != this->long_branch_stubs_.end();
3370 p = oview + this->plt_size_ + bs->second;
3371 Address loc = this->stub_address() + this->plt_size_ + bs->second;
3372 Address delta = bs->first.dest_ - loc;
3373 if (delta + (1 << 25) < 2 << 25)
3374 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3375 else if (!parameters->options().output_is_position_independent())
3377 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
3378 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
3379 write_insn<big_endian>(p + 8, mtctr_12);
3380 write_insn<big_endian>(p + 12, bctr);
3385 write_insn<big_endian>(p + 0, mflr_0);
3386 write_insn<big_endian>(p + 4, bcl_20_31);
3387 write_insn<big_endian>(p + 8, mflr_12);
3388 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
3389 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
3390 write_insn<big_endian>(p + 20, mtlr_0);
3391 write_insn<big_endian>(p + 24, mtctr_12);
3392 write_insn<big_endian>(p + 28, bctr);
3398 // Write out .glink.
3400 template<int size, bool big_endian>
3402 Output_data_glink<size, big_endian>::do_write(Output_file* of)
3404 const section_size_type off = this->offset();
3405 const section_size_type oview_size =
3406 convert_to_section_size_type(this->data_size());
3407 unsigned char* const oview = of->get_output_view(off, oview_size);
3410 // The base address of the .plt section.
3411 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3412 Address plt_base = this->targ_->plt_section()->address();
3416 // Write pltresolve stub.
3418 Address after_bcl = this->address() + 16;
3419 Address pltoff = plt_base - after_bcl;
3421 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
3423 write_insn<big_endian>(p, mflr_12), p += 4;
3424 write_insn<big_endian>(p, bcl_20_31), p += 4;
3425 write_insn<big_endian>(p, mflr_11), p += 4;
3426 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
3427 write_insn<big_endian>(p, mtlr_12), p += 4;
3428 write_insn<big_endian>(p, add_12_2_11), p += 4;
3429 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
3430 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
3431 write_insn<big_endian>(p, mtctr_11), p += 4;
3432 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
3433 write_insn<big_endian>(p, bctr), p += 4;
3434 while (p < oview + this->pltresolve_size)
3435 write_insn<big_endian>(p, nop), p += 4;
3437 // Write lazy link call stubs.
3439 while (p < oview + oview_size)
3443 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
3447 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
3448 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
3450 uint32_t branch_off = 8 - (p - oview);
3451 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
3457 const Output_data_got_powerpc<size, big_endian>* got
3458 = this->targ_->got_section();
3459 // The address of _GLOBAL_OFFSET_TABLE_.
3460 Address g_o_t = got->address() + got->g_o_t();
3462 // Write out pltresolve branch table.
3464 unsigned int the_end = oview_size - this->pltresolve_size;
3465 unsigned char* end_p = oview + the_end;
3466 while (p < end_p - 8 * 4)
3467 write_insn<big_endian>(p, b + end_p - p), p += 4;
3469 write_insn<big_endian>(p, nop), p += 4;
3471 // Write out pltresolve call stub.
3472 if (parameters->options().output_is_position_independent())
3474 Address res0_off = 0;
3475 Address after_bcl_off = the_end + 12;
3476 Address bcl_res0 = after_bcl_off - res0_off;
3478 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
3479 write_insn<big_endian>(p + 4, mflr_0);
3480 write_insn<big_endian>(p + 8, bcl_20_31);
3481 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
3482 write_insn<big_endian>(p + 16, mflr_12);
3483 write_insn<big_endian>(p + 20, mtlr_0);
3484 write_insn<big_endian>(p + 24, sub_11_11_12);
3486 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
3488 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
3489 if (ha(got_bcl) == ha(got_bcl + 4))
3491 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
3492 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
3496 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
3497 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
3499 write_insn<big_endian>(p + 40, mtctr_0);
3500 write_insn<big_endian>(p + 44, add_0_11_11);
3501 write_insn<big_endian>(p + 48, add_11_0_11);
3502 write_insn<big_endian>(p + 52, bctr);
3503 write_insn<big_endian>(p + 56, nop);
3504 write_insn<big_endian>(p + 60, nop);
3508 Address res0 = this->address();
3510 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
3511 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
3512 if (ha(g_o_t + 4) == ha(g_o_t + 8))
3513 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
3515 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
3516 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
3517 write_insn<big_endian>(p + 16, mtctr_0);
3518 write_insn<big_endian>(p + 20, add_0_11_11);
3519 if (ha(g_o_t + 4) == ha(g_o_t + 8))
3520 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
3522 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
3523 write_insn<big_endian>(p + 28, add_11_0_11);
3524 write_insn<big_endian>(p + 32, bctr);
3525 write_insn<big_endian>(p + 36, nop);
3526 write_insn<big_endian>(p + 40, nop);
3527 write_insn<big_endian>(p + 44, nop);
3528 write_insn<big_endian>(p + 48, nop);
3529 write_insn<big_endian>(p + 52, nop);
3530 write_insn<big_endian>(p + 56, nop);
3531 write_insn<big_endian>(p + 60, nop);
3536 of->write_output_view(off, oview_size, oview);
3540 // A class to handle linker generated save/restore functions.
3542 template<int size, bool big_endian>
3543 class Output_data_save_res : public Output_section_data_build
3546 Output_data_save_res(Symbol_table* symtab);
3549 // Write to a map file.
3551 do_print_to_mapfile(Mapfile* mapfile) const
3552 { mapfile->print_output_data(this, _("** save/restore")); }
3555 do_write(Output_file*);
3558 // The maximum size of save/restore contents.
3559 static const unsigned int savres_max = 218*4;
3562 savres_define(Symbol_table* symtab,
3564 unsigned int lo, unsigned int hi,
3565 unsigned char* write_ent(unsigned char*, int),
3566 unsigned char* write_tail(unsigned char*, int));
3568 unsigned char *contents_;
3571 template<bool big_endian>
3572 static unsigned char*
3573 savegpr0(unsigned char* p, int r)
3575 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3576 write_insn<big_endian>(p, insn);
3580 template<bool big_endian>
3581 static unsigned char*
3582 savegpr0_tail(unsigned char* p, int r)
3584 p = savegpr0<big_endian>(p, r);
3585 uint32_t insn = std_0_1 + 16;
3586 write_insn<big_endian>(p, insn);
3588 write_insn<big_endian>(p, blr);
3592 template<bool big_endian>
3593 static unsigned char*
3594 restgpr0(unsigned char* p, int r)
3596 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3597 write_insn<big_endian>(p, insn);
3601 template<bool big_endian>
3602 static unsigned char*
3603 restgpr0_tail(unsigned char* p, int r)
3605 uint32_t insn = ld_0_1 + 16;
3606 write_insn<big_endian>(p, insn);
3608 p = restgpr0<big_endian>(p, r);
3609 write_insn<big_endian>(p, mtlr_0);
3613 p = restgpr0<big_endian>(p, 30);
3614 p = restgpr0<big_endian>(p, 31);
3616 write_insn<big_endian>(p, blr);
3620 template<bool big_endian>
3621 static unsigned char*
3622 savegpr1(unsigned char* p, int r)
3624 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
3625 write_insn<big_endian>(p, insn);
3629 template<bool big_endian>
3630 static unsigned char*
3631 savegpr1_tail(unsigned char* p, int r)
3633 p = savegpr1<big_endian>(p, r);
3634 write_insn<big_endian>(p, blr);
3638 template<bool big_endian>
3639 static unsigned char*
3640 restgpr1(unsigned char* p, int r)
3642 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
3643 write_insn<big_endian>(p, insn);
3647 template<bool big_endian>
3648 static unsigned char*
3649 restgpr1_tail(unsigned char* p, int r)
3651 p = restgpr1<big_endian>(p, r);
3652 write_insn<big_endian>(p, blr);
3656 template<bool big_endian>
3657 static unsigned char*
3658 savefpr(unsigned char* p, int r)
3660 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3661 write_insn<big_endian>(p, insn);
3665 template<bool big_endian>
3666 static unsigned char*
3667 savefpr0_tail(unsigned char* p, int r)
3669 p = savefpr<big_endian>(p, r);
3670 write_insn<big_endian>(p, std_0_1 + 16);
3672 write_insn<big_endian>(p, blr);
3676 template<bool big_endian>
3677 static unsigned char*
3678 restfpr(unsigned char* p, int r)
3680 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3681 write_insn<big_endian>(p, insn);
3685 template<bool big_endian>
3686 static unsigned char*
3687 restfpr0_tail(unsigned char* p, int r)
3689 write_insn<big_endian>(p, ld_0_1 + 16);
3691 p = restfpr<big_endian>(p, r);
3692 write_insn<big_endian>(p, mtlr_0);
3696 p = restfpr<big_endian>(p, 30);
3697 p = restfpr<big_endian>(p, 31);
3699 write_insn<big_endian>(p, blr);
3703 template<bool big_endian>
3704 static unsigned char*
3705 savefpr1_tail(unsigned char* p, int r)
3707 p = savefpr<big_endian>(p, r);
3708 write_insn<big_endian>(p, blr);
3712 template<bool big_endian>
3713 static unsigned char*
3714 restfpr1_tail(unsigned char* p, int r)
3716 p = restfpr<big_endian>(p, r);
3717 write_insn<big_endian>(p, blr);
3721 template<bool big_endian>
3722 static unsigned char*
3723 savevr(unsigned char* p, int r)
3725 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
3726 write_insn<big_endian>(p, insn);
3728 insn = stvx_0_12_0 + (r << 21);
3729 write_insn<big_endian>(p, insn);
3733 template<bool big_endian>
3734 static unsigned char*
3735 savevr_tail(unsigned char* p, int r)
3737 p = savevr<big_endian>(p, r);
3738 write_insn<big_endian>(p, blr);
3742 template<bool big_endian>
3743 static unsigned char*
3744 restvr(unsigned char* p, int r)
3746 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
3747 write_insn<big_endian>(p, insn);
3749 insn = lvx_0_12_0 + (r << 21);
3750 write_insn<big_endian>(p, insn);
3754 template<bool big_endian>
3755 static unsigned char*
3756 restvr_tail(unsigned char* p, int r)
3758 p = restvr<big_endian>(p, r);
3759 write_insn<big_endian>(p, blr);
3764 template<int size, bool big_endian>
3765 Output_data_save_res<size, big_endian>::Output_data_save_res(
3766 Symbol_table* symtab)
3767 : Output_section_data_build(4),
3770 this->savres_define(symtab,
3771 "_savegpr0_", 14, 31,
3772 savegpr0<big_endian>, savegpr0_tail<big_endian>);
3773 this->savres_define(symtab,
3774 "_restgpr0_", 14, 29,
3775 restgpr0<big_endian>, restgpr0_tail<big_endian>);
3776 this->savres_define(symtab,
3777 "_restgpr0_", 30, 31,
3778 restgpr0<big_endian>, restgpr0_tail<big_endian>);
3779 this->savres_define(symtab,
3780 "_savegpr1_", 14, 31,
3781 savegpr1<big_endian>, savegpr1_tail<big_endian>);
3782 this->savres_define(symtab,
3783 "_restgpr1_", 14, 31,
3784 restgpr1<big_endian>, restgpr1_tail<big_endian>);
3785 this->savres_define(symtab,
3786 "_savefpr_", 14, 31,
3787 savefpr<big_endian>, savefpr0_tail<big_endian>);
3788 this->savres_define(symtab,
3789 "_restfpr_", 14, 29,
3790 restfpr<big_endian>, restfpr0_tail<big_endian>);
3791 this->savres_define(symtab,
3792 "_restfpr_", 30, 31,
3793 restfpr<big_endian>, restfpr0_tail<big_endian>);
3794 this->savres_define(symtab,
3796 savefpr<big_endian>, savefpr1_tail<big_endian>);
3797 this->savres_define(symtab,
3799 restfpr<big_endian>, restfpr1_tail<big_endian>);
3800 this->savres_define(symtab,
3802 savevr<big_endian>, savevr_tail<big_endian>);
3803 this->savres_define(symtab,
3805 restvr<big_endian>, restvr_tail<big_endian>);
3808 template<int size, bool big_endian>
3810 Output_data_save_res<size, big_endian>::savres_define(
3811 Symbol_table* symtab,
3813 unsigned int lo, unsigned int hi,
3814 unsigned char* write_ent(unsigned char*, int),
3815 unsigned char* write_tail(unsigned char*, int))
3817 size_t len = strlen(name);
3818 bool writing = false;
3821 memcpy(sym, name, len);
3824 for (unsigned int i = lo; i <= hi; i++)
3826 sym[len + 0] = i / 10 + '0';
3827 sym[len + 1] = i % 10 + '0';
3828 Symbol* gsym = symtab->lookup(sym);
3829 bool refd = gsym != NULL && gsym->is_undefined();
3830 writing = writing || refd;
3833 if (this->contents_ == NULL)
3834 this->contents_ = new unsigned char[this->savres_max];
3836 section_size_type value = this->current_data_size();
3837 unsigned char* p = this->contents_ + value;
3839 p = write_ent(p, i);
3841 p = write_tail(p, i);
3842 section_size_type cur_size = p - this->contents_;
3843 this->set_current_data_size(cur_size);
3845 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
3846 this, value, cur_size - value,
3847 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
3848 elfcpp::STV_HIDDEN, 0, false, false);
3853 // Write out save/restore.
3855 template<int size, bool big_endian>
3857 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
3859 const section_size_type off = this->offset();
3860 const section_size_type oview_size =
3861 convert_to_section_size_type(this->data_size());
3862 unsigned char* const oview = of->get_output_view(off, oview_size);
3863 memcpy(oview, this->contents_, oview_size);
3864 of->write_output_view(off, oview_size, oview);
3868 // Create the glink section.
3870 template<int size, bool big_endian>
3872 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
3874 if (this->glink_ == NULL)
3876 this->glink_ = new Output_data_glink<size, big_endian>(this);
3877 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
3878 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
3879 this->glink_, ORDER_TEXT, false);
3883 // Create a PLT entry for a global symbol.
3885 template<int size, bool big_endian>
3887 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
3891 if (gsym->type() == elfcpp::STT_GNU_IFUNC
3892 && gsym->can_use_relative_reloc(false))
3894 if (this->iplt_ == NULL)
3895 this->make_iplt_section(symtab, layout);
3896 this->iplt_->add_ifunc_entry(gsym);
3900 if (this->plt_ == NULL)
3901 this->make_plt_section(symtab, layout);
3902 this->plt_->add_entry(gsym);
3906 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
3908 template<int size, bool big_endian>
3910 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
3911 Symbol_table* symtab,
3913 Sized_relobj_file<size, big_endian>* relobj,
3916 if (this->iplt_ == NULL)
3917 this->make_iplt_section(symtab, layout);
3918 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
3921 // Return the number of entries in the PLT.
3923 template<int size, bool big_endian>
3925 Target_powerpc<size, big_endian>::plt_entry_count() const
3927 if (this->plt_ == NULL)
3929 unsigned int count = this->plt_->entry_count();
3930 if (this->iplt_ != NULL)
3931 count += this->iplt_->entry_count();
3935 // Return the offset of the first non-reserved PLT entry.
3937 template<int size, bool big_endian>
3939 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
3941 return this->plt_->first_plt_entry_offset();
3944 // Return the size of each PLT entry.
3946 template<int size, bool big_endian>
3948 Target_powerpc<size, big_endian>::plt_entry_size() const
3950 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
3953 // Create a GOT entry for local dynamic __tls_get_addr calls.
3955 template<int size, bool big_endian>
3957 Target_powerpc<size, big_endian>::tlsld_got_offset(
3958 Symbol_table* symtab,
3960 Sized_relobj_file<size, big_endian>* object)
3962 if (this->tlsld_got_offset_ == -1U)
3964 gold_assert(symtab != NULL && layout != NULL && object != NULL);
3965 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
3966 Output_data_got_powerpc<size, big_endian>* got
3967 = this->got_section(symtab, layout);
3968 unsigned int got_offset = got->add_constant_pair(0, 0);
3969 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
3971 this->tlsld_got_offset_ = got_offset;
3973 return this->tlsld_got_offset_;
3976 // Get the Reference_flags for a particular relocation.
3978 template<int size, bool big_endian>
3980 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
3984 case elfcpp::R_POWERPC_NONE:
3985 case elfcpp::R_POWERPC_GNU_VTINHERIT:
3986 case elfcpp::R_POWERPC_GNU_VTENTRY:
3987 case elfcpp::R_PPC64_TOC:
3988 // No symbol reference.
3991 case elfcpp::R_PPC64_ADDR64:
3992 case elfcpp::R_PPC64_UADDR64:
3993 case elfcpp::R_POWERPC_ADDR32:
3994 case elfcpp::R_POWERPC_UADDR32:
3995 case elfcpp::R_POWERPC_ADDR16:
3996 case elfcpp::R_POWERPC_UADDR16:
3997 case elfcpp::R_POWERPC_ADDR16_LO:
3998 case elfcpp::R_POWERPC_ADDR16_HI:
3999 case elfcpp::R_POWERPC_ADDR16_HA:
4000 return Symbol::ABSOLUTE_REF;
4002 case elfcpp::R_POWERPC_ADDR24:
4003 case elfcpp::R_POWERPC_ADDR14:
4004 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4005 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4006 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
4008 case elfcpp::R_PPC64_REL64:
4009 case elfcpp::R_POWERPC_REL32:
4010 case elfcpp::R_PPC_LOCAL24PC:
4011 case elfcpp::R_POWERPC_REL16:
4012 case elfcpp::R_POWERPC_REL16_LO:
4013 case elfcpp::R_POWERPC_REL16_HI:
4014 case elfcpp::R_POWERPC_REL16_HA:
4015 return Symbol::RELATIVE_REF;
4017 case elfcpp::R_POWERPC_REL24:
4018 case elfcpp::R_PPC_PLTREL24:
4019 case elfcpp::R_POWERPC_REL14:
4020 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4021 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4022 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
4024 case elfcpp::R_POWERPC_GOT16:
4025 case elfcpp::R_POWERPC_GOT16_LO:
4026 case elfcpp::R_POWERPC_GOT16_HI:
4027 case elfcpp::R_POWERPC_GOT16_HA:
4028 case elfcpp::R_PPC64_GOT16_DS:
4029 case elfcpp::R_PPC64_GOT16_LO_DS:
4030 case elfcpp::R_PPC64_TOC16:
4031 case elfcpp::R_PPC64_TOC16_LO:
4032 case elfcpp::R_PPC64_TOC16_HI:
4033 case elfcpp::R_PPC64_TOC16_HA:
4034 case elfcpp::R_PPC64_TOC16_DS:
4035 case elfcpp::R_PPC64_TOC16_LO_DS:
4037 return Symbol::ABSOLUTE_REF;
4039 case elfcpp::R_POWERPC_GOT_TPREL16:
4040 case elfcpp::R_POWERPC_TLS:
4041 return Symbol::TLS_REF;
4043 case elfcpp::R_POWERPC_COPY:
4044 case elfcpp::R_POWERPC_GLOB_DAT:
4045 case elfcpp::R_POWERPC_JMP_SLOT:
4046 case elfcpp::R_POWERPC_RELATIVE:
4047 case elfcpp::R_POWERPC_DTPMOD:
4049 // Not expected. We will give an error later.
4054 // Report an unsupported relocation against a local symbol.
4056 template<int size, bool big_endian>
4058 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
4059 Sized_relobj_file<size, big_endian>* object,
4060 unsigned int r_type)
4062 gold_error(_("%s: unsupported reloc %u against local symbol"),
4063 object->name().c_str(), r_type);
4066 // We are about to emit a dynamic relocation of type R_TYPE. If the
4067 // dynamic linker does not support it, issue an error.
4069 template<int size, bool big_endian>
4071 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
4072 unsigned int r_type)
4074 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
4076 // These are the relocation types supported by glibc for both 32-bit
4077 // and 64-bit powerpc.
4080 case elfcpp::R_POWERPC_NONE:
4081 case elfcpp::R_POWERPC_RELATIVE:
4082 case elfcpp::R_POWERPC_GLOB_DAT:
4083 case elfcpp::R_POWERPC_DTPMOD:
4084 case elfcpp::R_POWERPC_DTPREL:
4085 case elfcpp::R_POWERPC_TPREL:
4086 case elfcpp::R_POWERPC_JMP_SLOT:
4087 case elfcpp::R_POWERPC_COPY:
4088 case elfcpp::R_POWERPC_IRELATIVE:
4089 case elfcpp::R_POWERPC_ADDR32:
4090 case elfcpp::R_POWERPC_UADDR32:
4091 case elfcpp::R_POWERPC_ADDR24:
4092 case elfcpp::R_POWERPC_ADDR16:
4093 case elfcpp::R_POWERPC_UADDR16:
4094 case elfcpp::R_POWERPC_ADDR16_LO:
4095 case elfcpp::R_POWERPC_ADDR16_HI:
4096 case elfcpp::R_POWERPC_ADDR16_HA:
4097 case elfcpp::R_POWERPC_ADDR14:
4098 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4099 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4100 case elfcpp::R_POWERPC_REL32:
4101 case elfcpp::R_POWERPC_REL24:
4102 case elfcpp::R_POWERPC_TPREL16:
4103 case elfcpp::R_POWERPC_TPREL16_LO:
4104 case elfcpp::R_POWERPC_TPREL16_HI:
4105 case elfcpp::R_POWERPC_TPREL16_HA:
4116 // These are the relocation types supported only on 64-bit.
4117 case elfcpp::R_PPC64_ADDR64:
4118 case elfcpp::R_PPC64_UADDR64:
4119 case elfcpp::R_PPC64_JMP_IREL:
4120 case elfcpp::R_PPC64_ADDR16_DS:
4121 case elfcpp::R_PPC64_ADDR16_LO_DS:
4122 case elfcpp::R_PPC64_ADDR16_HIGHER:
4123 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4124 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4125 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4126 case elfcpp::R_PPC64_REL64:
4127 case elfcpp::R_POWERPC_ADDR30:
4128 case elfcpp::R_PPC64_TPREL16_DS:
4129 case elfcpp::R_PPC64_TPREL16_LO_DS:
4130 case elfcpp::R_PPC64_TPREL16_HIGHER:
4131 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4132 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4133 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4144 // These are the relocation types supported only on 32-bit.
4145 // ??? glibc ld.so doesn't need to support these.
4146 case elfcpp::R_POWERPC_DTPREL16:
4147 case elfcpp::R_POWERPC_DTPREL16_LO:
4148 case elfcpp::R_POWERPC_DTPREL16_HI:
4149 case elfcpp::R_POWERPC_DTPREL16_HA:
4157 // This prevents us from issuing more than one error per reloc
4158 // section. But we can still wind up issuing more than one
4159 // error per object file.
4160 if (this->issued_non_pic_error_)
4162 gold_assert(parameters->options().output_is_position_independent());
4163 object->error(_("requires unsupported dynamic reloc; "
4164 "recompile with -fPIC"));
4165 this->issued_non_pic_error_ = true;
4169 // Return whether we need to make a PLT entry for a relocation of the
4170 // given type against a STT_GNU_IFUNC symbol.
4172 template<int size, bool big_endian>
4174 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4175 Sized_relobj_file<size, big_endian>* object,
4176 unsigned int r_type)
4178 // In non-pic code any reference will resolve to the plt call stub
4179 // for the ifunc symbol.
4180 if (size == 32 && !parameters->options().output_is_position_independent())
4185 // Word size refs from data sections are OK.
4186 case elfcpp::R_POWERPC_ADDR32:
4187 case elfcpp::R_POWERPC_UADDR32:
4192 case elfcpp::R_PPC64_ADDR64:
4193 case elfcpp::R_PPC64_UADDR64:
4198 // GOT refs are good.
4199 case elfcpp::R_POWERPC_GOT16:
4200 case elfcpp::R_POWERPC_GOT16_LO:
4201 case elfcpp::R_POWERPC_GOT16_HI:
4202 case elfcpp::R_POWERPC_GOT16_HA:
4203 case elfcpp::R_PPC64_GOT16_DS:
4204 case elfcpp::R_PPC64_GOT16_LO_DS:
4207 // So are function calls.
4208 case elfcpp::R_POWERPC_ADDR24:
4209 case elfcpp::R_POWERPC_ADDR14:
4210 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4211 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4212 case elfcpp::R_POWERPC_REL24:
4213 case elfcpp::R_PPC_PLTREL24:
4214 case elfcpp::R_POWERPC_REL14:
4215 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4216 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4223 // Anything else is a problem.
4224 // If we are building a static executable, the libc startup function
4225 // responsible for applying indirect function relocations is going
4226 // to complain about the reloc type.
4227 // If we are building a dynamic executable, we will have a text
4228 // relocation. The dynamic loader will set the text segment
4229 // writable and non-executable to apply text relocations. So we'll
4230 // segfault when trying to run the indirection function to resolve
4232 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4233 object->name().c_str(), r_type);
4237 // Scan a relocation for a local symbol.
4239 template<int size, bool big_endian>
4241 Target_powerpc<size, big_endian>::Scan::local(
4242 Symbol_table* symtab,
4244 Target_powerpc<size, big_endian>* target,
4245 Sized_relobj_file<size, big_endian>* object,
4246 unsigned int data_shndx,
4247 Output_section* output_section,
4248 const elfcpp::Rela<size, big_endian>& reloc,
4249 unsigned int r_type,
4250 const elfcpp::Sym<size, big_endian>& lsym,
4253 Powerpc_relobj<size, big_endian>* ppc_object
4254 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4259 && data_shndx == ppc_object->opd_shndx()
4260 && r_type == elfcpp::R_PPC64_ADDR64)
4261 ppc_object->set_opd_discard(reloc.get_r_offset());
4265 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4266 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4267 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
4269 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4270 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4271 r_type, r_sym, reloc.get_r_addend());
4272 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4277 case elfcpp::R_POWERPC_NONE:
4278 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4279 case elfcpp::R_POWERPC_GNU_VTENTRY:
4280 case elfcpp::R_PPC64_TOCSAVE:
4281 case elfcpp::R_PPC_EMB_MRKREF:
4282 case elfcpp::R_POWERPC_TLS:
4285 case elfcpp::R_PPC64_TOC:
4287 Output_data_got_powerpc<size, big_endian>* got
4288 = target->got_section(symtab, layout);
4289 if (parameters->options().output_is_position_independent())
4291 Address off = reloc.get_r_offset();
4293 && data_shndx == ppc_object->opd_shndx()
4294 && ppc_object->get_opd_discard(off - 8))
4297 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4298 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
4299 rela_dyn->add_output_section_relative(got->output_section(),
4300 elfcpp::R_POWERPC_RELATIVE,
4302 object, data_shndx, off,
4303 symobj->toc_base_offset());
4308 case elfcpp::R_PPC64_ADDR64:
4309 case elfcpp::R_PPC64_UADDR64:
4310 case elfcpp::R_POWERPC_ADDR32:
4311 case elfcpp::R_POWERPC_UADDR32:
4312 case elfcpp::R_POWERPC_ADDR24:
4313 case elfcpp::R_POWERPC_ADDR16:
4314 case elfcpp::R_POWERPC_ADDR16_LO:
4315 case elfcpp::R_POWERPC_ADDR16_HI:
4316 case elfcpp::R_POWERPC_ADDR16_HA:
4317 case elfcpp::R_POWERPC_UADDR16:
4318 case elfcpp::R_PPC64_ADDR16_HIGHER:
4319 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4320 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4321 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4322 case elfcpp::R_PPC64_ADDR16_DS:
4323 case elfcpp::R_PPC64_ADDR16_LO_DS:
4324 case elfcpp::R_POWERPC_ADDR14:
4325 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4326 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4327 // If building a shared library (or a position-independent
4328 // executable), we need to create a dynamic relocation for
4330 if (parameters->options().output_is_position_independent()
4331 || (size == 64 && is_ifunc))
4333 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4335 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
4336 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
4338 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4339 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4342 rela_dyn = target->iplt_section()->rel_plt();
4343 dynrel = elfcpp::R_POWERPC_IRELATIVE;
4345 rela_dyn->add_local_relative(object, r_sym, dynrel,
4346 output_section, data_shndx,
4347 reloc.get_r_offset(),
4348 reloc.get_r_addend(), false);
4352 check_non_pic(object, r_type);
4353 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4354 rela_dyn->add_local(object, r_sym, r_type, output_section,
4355 data_shndx, reloc.get_r_offset(),
4356 reloc.get_r_addend());
4361 case elfcpp::R_POWERPC_REL24:
4362 case elfcpp::R_PPC_PLTREL24:
4363 case elfcpp::R_PPC_LOCAL24PC:
4364 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4365 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4366 reloc.get_r_addend());
4369 case elfcpp::R_POWERPC_REL14:
4370 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4371 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4372 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4373 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4374 reloc.get_r_addend());
4377 case elfcpp::R_PPC64_REL64:
4378 case elfcpp::R_POWERPC_REL32:
4379 case elfcpp::R_POWERPC_REL16:
4380 case elfcpp::R_POWERPC_REL16_LO:
4381 case elfcpp::R_POWERPC_REL16_HI:
4382 case elfcpp::R_POWERPC_REL16_HA:
4383 case elfcpp::R_POWERPC_SECTOFF:
4384 case elfcpp::R_POWERPC_TPREL16:
4385 case elfcpp::R_POWERPC_DTPREL16:
4386 case elfcpp::R_POWERPC_SECTOFF_LO:
4387 case elfcpp::R_POWERPC_TPREL16_LO:
4388 case elfcpp::R_POWERPC_DTPREL16_LO:
4389 case elfcpp::R_POWERPC_SECTOFF_HI:
4390 case elfcpp::R_POWERPC_TPREL16_HI:
4391 case elfcpp::R_POWERPC_DTPREL16_HI:
4392 case elfcpp::R_POWERPC_SECTOFF_HA:
4393 case elfcpp::R_POWERPC_TPREL16_HA:
4394 case elfcpp::R_POWERPC_DTPREL16_HA:
4395 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4396 case elfcpp::R_PPC64_TPREL16_HIGHER:
4397 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4398 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4399 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4400 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4401 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4402 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4403 case elfcpp::R_PPC64_TPREL16_DS:
4404 case elfcpp::R_PPC64_TPREL16_LO_DS:
4405 case elfcpp::R_PPC64_DTPREL16_DS:
4406 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4407 case elfcpp::R_PPC64_SECTOFF_DS:
4408 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4409 case elfcpp::R_PPC64_TLSGD:
4410 case elfcpp::R_PPC64_TLSLD:
4413 case elfcpp::R_POWERPC_GOT16:
4414 case elfcpp::R_POWERPC_GOT16_LO:
4415 case elfcpp::R_POWERPC_GOT16_HI:
4416 case elfcpp::R_POWERPC_GOT16_HA:
4417 case elfcpp::R_PPC64_GOT16_DS:
4418 case elfcpp::R_PPC64_GOT16_LO_DS:
4420 // The symbol requires a GOT entry.
4421 Output_data_got_powerpc<size, big_endian>* got
4422 = target->got_section(symtab, layout);
4423 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4425 if (!parameters->options().output_is_position_independent())
4427 if (size == 32 && is_ifunc)
4428 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
4430 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
4432 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
4434 // If we are generating a shared object or a pie, this
4435 // symbol's GOT entry will be set by a dynamic relocation.
4437 off = got->add_constant(0);
4438 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
4440 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4441 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4444 rela_dyn = target->iplt_section()->rel_plt();
4445 dynrel = elfcpp::R_POWERPC_IRELATIVE;
4447 rela_dyn->add_local_relative(object, r_sym, dynrel,
4448 got, off, 0, false);
4453 case elfcpp::R_PPC64_TOC16:
4454 case elfcpp::R_PPC64_TOC16_LO:
4455 case elfcpp::R_PPC64_TOC16_HI:
4456 case elfcpp::R_PPC64_TOC16_HA:
4457 case elfcpp::R_PPC64_TOC16_DS:
4458 case elfcpp::R_PPC64_TOC16_LO_DS:
4459 // We need a GOT section.
4460 target->got_section(symtab, layout);
4463 case elfcpp::R_POWERPC_GOT_TLSGD16:
4464 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4465 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4466 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4468 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
4469 if (tls_type == tls::TLSOPT_NONE)
4471 Output_data_got_powerpc<size, big_endian>* got
4472 = target->got_section(symtab, layout);
4473 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4474 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4475 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
4476 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
4478 else if (tls_type == tls::TLSOPT_TO_LE)
4480 // no GOT relocs needed for Local Exec.
4487 case elfcpp::R_POWERPC_GOT_TLSLD16:
4488 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4489 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4490 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4492 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4493 if (tls_type == tls::TLSOPT_NONE)
4494 target->tlsld_got_offset(symtab, layout, object);
4495 else if (tls_type == tls::TLSOPT_TO_LE)
4497 // no GOT relocs needed for Local Exec.
4498 if (parameters->options().emit_relocs())
4500 Output_section* os = layout->tls_segment()->first_section();
4501 gold_assert(os != NULL);
4502 os->set_needs_symtab_index();
4510 case elfcpp::R_POWERPC_GOT_DTPREL16:
4511 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4512 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4513 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4515 Output_data_got_powerpc<size, big_endian>* got
4516 = target->got_section(symtab, layout);
4517 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4518 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
4522 case elfcpp::R_POWERPC_GOT_TPREL16:
4523 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4524 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4525 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4527 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
4528 if (tls_type == tls::TLSOPT_NONE)
4530 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4531 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
4533 Output_data_got_powerpc<size, big_endian>* got
4534 = target->got_section(symtab, layout);
4535 unsigned int off = got->add_constant(0);
4536 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
4538 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4539 rela_dyn->add_symbolless_local_addend(object, r_sym,
4540 elfcpp::R_POWERPC_TPREL,
4544 else if (tls_type == tls::TLSOPT_TO_LE)
4546 // no GOT relocs needed for Local Exec.
4554 unsupported_reloc_local(object, r_type);
4559 // Report an unsupported relocation against a global symbol.
4561 template<int size, bool big_endian>
4563 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
4564 Sized_relobj_file<size, big_endian>* object,
4565 unsigned int r_type,
4568 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4569 object->name().c_str(), r_type, gsym->demangled_name().c_str());
4572 // Scan a relocation for a global symbol.
4574 template<int size, bool big_endian>
4576 Target_powerpc<size, big_endian>::Scan::global(
4577 Symbol_table* symtab,
4579 Target_powerpc<size, big_endian>* target,
4580 Sized_relobj_file<size, big_endian>* object,
4581 unsigned int data_shndx,
4582 Output_section* output_section,
4583 const elfcpp::Rela<size, big_endian>& reloc,
4584 unsigned int r_type,
4587 Powerpc_relobj<size, big_endian>* ppc_object
4588 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4590 // A STT_GNU_IFUNC symbol may require a PLT entry.
4591 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4592 && this->reloc_needs_plt_for_ifunc(object, r_type))
4594 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4595 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4596 reloc.get_r_addend());
4597 target->make_plt_entry(symtab, layout, gsym);
4602 case elfcpp::R_POWERPC_NONE:
4603 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4604 case elfcpp::R_POWERPC_GNU_VTENTRY:
4605 case elfcpp::R_PPC_LOCAL24PC:
4606 case elfcpp::R_PPC_EMB_MRKREF:
4607 case elfcpp::R_POWERPC_TLS:
4610 case elfcpp::R_PPC64_TOC:
4612 Output_data_got_powerpc<size, big_endian>* got
4613 = target->got_section(symtab, layout);
4614 if (parameters->options().output_is_position_independent())
4616 Address off = reloc.get_r_offset();
4618 && data_shndx == ppc_object->opd_shndx()
4619 && ppc_object->get_opd_discard(off - 8))
4622 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4623 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
4624 if (data_shndx != ppc_object->opd_shndx())
4625 symobj = static_cast
4626 <Powerpc_relobj<size, big_endian>*>(gsym->object());
4627 rela_dyn->add_output_section_relative(got->output_section(),
4628 elfcpp::R_POWERPC_RELATIVE,
4630 object, data_shndx, off,
4631 symobj->toc_base_offset());
4636 case elfcpp::R_PPC64_ADDR64:
4638 && data_shndx == ppc_object->opd_shndx()
4639 && (gsym->is_defined_in_discarded_section()
4640 || gsym->object() != object))
4642 ppc_object->set_opd_discard(reloc.get_r_offset());
4646 case elfcpp::R_PPC64_UADDR64:
4647 case elfcpp::R_POWERPC_ADDR32:
4648 case elfcpp::R_POWERPC_UADDR32:
4649 case elfcpp::R_POWERPC_ADDR24:
4650 case elfcpp::R_POWERPC_ADDR16:
4651 case elfcpp::R_POWERPC_ADDR16_LO:
4652 case elfcpp::R_POWERPC_ADDR16_HI:
4653 case elfcpp::R_POWERPC_ADDR16_HA:
4654 case elfcpp::R_POWERPC_UADDR16:
4655 case elfcpp::R_PPC64_ADDR16_HIGHER:
4656 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4657 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4658 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4659 case elfcpp::R_PPC64_ADDR16_DS:
4660 case elfcpp::R_PPC64_ADDR16_LO_DS:
4661 case elfcpp::R_POWERPC_ADDR14:
4662 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4663 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4665 // Make a PLT entry if necessary.
4666 if (gsym->needs_plt_entry())
4668 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4670 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4671 reloc.get_r_addend());
4672 target->make_plt_entry(symtab, layout, gsym);
4673 // Since this is not a PC-relative relocation, we may be
4674 // taking the address of a function. In that case we need to
4675 // set the entry in the dynamic symbol table to the address of
4676 // the PLT call stub.
4678 && gsym->is_from_dynobj()
4679 && !parameters->options().output_is_position_independent())
4680 gsym->set_needs_dynsym_value();
4682 // Make a dynamic relocation if necessary.
4683 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
4684 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
4686 if (gsym->may_need_copy_reloc())
4688 target->copy_reloc(symtab, layout, object,
4689 data_shndx, output_section, gsym, reloc);
4691 else if ((size == 32
4692 && r_type == elfcpp::R_POWERPC_ADDR32
4693 && gsym->can_use_relative_reloc(false)
4694 && !(gsym->visibility() == elfcpp::STV_PROTECTED
4695 && parameters->options().shared()))
4697 && r_type == elfcpp::R_PPC64_ADDR64
4698 && (gsym->can_use_relative_reloc(false)
4699 || data_shndx == ppc_object->opd_shndx())))
4701 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4702 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4703 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4705 rela_dyn = target->iplt_section()->rel_plt();
4706 dynrel = elfcpp::R_POWERPC_IRELATIVE;
4708 rela_dyn->add_symbolless_global_addend(
4709 gsym, dynrel, output_section, object, data_shndx,
4710 reloc.get_r_offset(), reloc.get_r_addend());
4714 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4715 check_non_pic(object, r_type);
4716 rela_dyn->add_global(gsym, r_type, output_section,
4718 reloc.get_r_offset(),
4719 reloc.get_r_addend());
4725 case elfcpp::R_PPC_PLTREL24:
4726 case elfcpp::R_POWERPC_REL24:
4727 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4728 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4729 reloc.get_r_addend());
4730 if (gsym->needs_plt_entry()
4731 || (!gsym->final_value_is_known()
4732 && (gsym->is_undefined()
4733 || gsym->is_from_dynobj()
4734 || gsym->is_preemptible())))
4735 target->make_plt_entry(symtab, layout, gsym);
4738 case elfcpp::R_PPC64_REL64:
4739 case elfcpp::R_POWERPC_REL32:
4740 // Make a dynamic relocation if necessary.
4741 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
4743 if (gsym->may_need_copy_reloc())
4745 target->copy_reloc(symtab, layout, object,
4746 data_shndx, output_section, gsym,
4751 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4752 check_non_pic(object, r_type);
4753 rela_dyn->add_global(gsym, r_type, output_section, object,
4754 data_shndx, reloc.get_r_offset(),
4755 reloc.get_r_addend());
4760 case elfcpp::R_POWERPC_REL14:
4761 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4762 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4763 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4764 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4765 reloc.get_r_addend());
4768 case elfcpp::R_POWERPC_REL16:
4769 case elfcpp::R_POWERPC_REL16_LO:
4770 case elfcpp::R_POWERPC_REL16_HI:
4771 case elfcpp::R_POWERPC_REL16_HA:
4772 case elfcpp::R_POWERPC_SECTOFF:
4773 case elfcpp::R_POWERPC_TPREL16:
4774 case elfcpp::R_POWERPC_DTPREL16:
4775 case elfcpp::R_POWERPC_SECTOFF_LO:
4776 case elfcpp::R_POWERPC_TPREL16_LO:
4777 case elfcpp::R_POWERPC_DTPREL16_LO:
4778 case elfcpp::R_POWERPC_SECTOFF_HI:
4779 case elfcpp::R_POWERPC_TPREL16_HI:
4780 case elfcpp::R_POWERPC_DTPREL16_HI:
4781 case elfcpp::R_POWERPC_SECTOFF_HA:
4782 case elfcpp::R_POWERPC_TPREL16_HA:
4783 case elfcpp::R_POWERPC_DTPREL16_HA:
4784 case elfcpp::R_PPC64_DTPREL16_HIGHER:
4785 case elfcpp::R_PPC64_TPREL16_HIGHER:
4786 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4787 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4788 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4789 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4790 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4791 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4792 case elfcpp::R_PPC64_TPREL16_DS:
4793 case elfcpp::R_PPC64_TPREL16_LO_DS:
4794 case elfcpp::R_PPC64_DTPREL16_DS:
4795 case elfcpp::R_PPC64_DTPREL16_LO_DS:
4796 case elfcpp::R_PPC64_SECTOFF_DS:
4797 case elfcpp::R_PPC64_SECTOFF_LO_DS:
4798 case elfcpp::R_PPC64_TLSGD:
4799 case elfcpp::R_PPC64_TLSLD:
4802 case elfcpp::R_POWERPC_GOT16:
4803 case elfcpp::R_POWERPC_GOT16_LO:
4804 case elfcpp::R_POWERPC_GOT16_HI:
4805 case elfcpp::R_POWERPC_GOT16_HA:
4806 case elfcpp::R_PPC64_GOT16_DS:
4807 case elfcpp::R_PPC64_GOT16_LO_DS:
4809 // The symbol requires a GOT entry.
4810 Output_data_got_powerpc<size, big_endian>* got;
4812 got = target->got_section(symtab, layout);
4813 if (gsym->final_value_is_known())
4815 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
4816 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
4818 got->add_global(gsym, GOT_TYPE_STANDARD);
4820 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
4822 // If we are generating a shared object or a pie, this
4823 // symbol's GOT entry will be set by a dynamic relocation.
4824 unsigned int off = got->add_constant(0);
4825 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
4827 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4828 if (gsym->can_use_relative_reloc(false)
4830 && gsym->visibility() == elfcpp::STV_PROTECTED
4831 && parameters->options().shared()))
4833 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4834 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4836 rela_dyn = target->iplt_section()->rel_plt();
4837 dynrel = elfcpp::R_POWERPC_IRELATIVE;
4839 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
4843 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
4844 rela_dyn->add_global(gsym, dynrel, got, off, 0);
4850 case elfcpp::R_PPC64_TOC16:
4851 case elfcpp::R_PPC64_TOC16_LO:
4852 case elfcpp::R_PPC64_TOC16_HI:
4853 case elfcpp::R_PPC64_TOC16_HA:
4854 case elfcpp::R_PPC64_TOC16_DS:
4855 case elfcpp::R_PPC64_TOC16_LO_DS:
4856 // We need a GOT section.
4857 target->got_section(symtab, layout);
4860 case elfcpp::R_POWERPC_GOT_TLSGD16:
4861 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4862 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4863 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4865 const bool final = gsym->final_value_is_known();
4866 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
4867 if (tls_type == tls::TLSOPT_NONE)
4869 Output_data_got_powerpc<size, big_endian>* got
4870 = target->got_section(symtab, layout);
4871 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
4872 target->rela_dyn_section(layout),
4873 elfcpp::R_POWERPC_DTPMOD,
4874 elfcpp::R_POWERPC_DTPREL);
4876 else if (tls_type == tls::TLSOPT_TO_IE)
4878 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
4880 Output_data_got_powerpc<size, big_endian>* got
4881 = target->got_section(symtab, layout);
4882 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4883 if (gsym->is_undefined()
4884 || gsym->is_from_dynobj())
4886 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
4887 elfcpp::R_POWERPC_TPREL);
4891 unsigned int off = got->add_constant(0);
4892 gsym->set_got_offset(GOT_TYPE_TPREL, off);
4893 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
4894 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
4899 else if (tls_type == tls::TLSOPT_TO_LE)
4901 // no GOT relocs needed for Local Exec.
4908 case elfcpp::R_POWERPC_GOT_TLSLD16:
4909 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4910 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4911 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4913 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4914 if (tls_type == tls::TLSOPT_NONE)
4915 target->tlsld_got_offset(symtab, layout, object);
4916 else if (tls_type == tls::TLSOPT_TO_LE)
4918 // no GOT relocs needed for Local Exec.
4919 if (parameters->options().emit_relocs())
4921 Output_section* os = layout->tls_segment()->first_section();
4922 gold_assert(os != NULL);
4923 os->set_needs_symtab_index();
4931 case elfcpp::R_POWERPC_GOT_DTPREL16:
4932 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4933 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4934 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4936 Output_data_got_powerpc<size, big_endian>* got
4937 = target->got_section(symtab, layout);
4938 if (!gsym->final_value_is_known()
4939 && (gsym->is_from_dynobj()
4940 || gsym->is_undefined()
4941 || gsym->is_preemptible()))
4942 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
4943 target->rela_dyn_section(layout),
4944 elfcpp::R_POWERPC_DTPREL);
4946 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
4950 case elfcpp::R_POWERPC_GOT_TPREL16:
4951 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4952 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4953 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4955 const bool final = gsym->final_value_is_known();
4956 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
4957 if (tls_type == tls::TLSOPT_NONE)
4959 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
4961 Output_data_got_powerpc<size, big_endian>* got
4962 = target->got_section(symtab, layout);
4963 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4964 if (gsym->is_undefined()
4965 || gsym->is_from_dynobj())
4967 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
4968 elfcpp::R_POWERPC_TPREL);
4972 unsigned int off = got->add_constant(0);
4973 gsym->set_got_offset(GOT_TYPE_TPREL, off);
4974 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
4975 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
4980 else if (tls_type == tls::TLSOPT_TO_LE)
4982 // no GOT relocs needed for Local Exec.
4990 unsupported_reloc_global(object, r_type, gsym);
4995 // Process relocations for gc.
4997 template<int size, bool big_endian>
4999 Target_powerpc<size, big_endian>::gc_process_relocs(
5000 Symbol_table* symtab,
5002 Sized_relobj_file<size, big_endian>* object,
5003 unsigned int data_shndx,
5005 const unsigned char* prelocs,
5007 Output_section* output_section,
5008 bool needs_special_offset_handling,
5009 size_t local_symbol_count,
5010 const unsigned char* plocal_symbols)
5012 typedef Target_powerpc<size, big_endian> Powerpc;
5013 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5014 Powerpc_relobj<size, big_endian>* ppc_object
5015 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5017 ppc_object->set_opd_valid();
5018 if (size == 64 && data_shndx == ppc_object->opd_shndx())
5020 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
5021 for (p = ppc_object->access_from_map()->begin();
5022 p != ppc_object->access_from_map()->end();
5025 Address dst_off = p->first;
5026 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5027 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
5028 for (s = p->second.begin(); s != p->second.end(); ++s)
5030 Object* src_obj = s->first;
5031 unsigned int src_indx = s->second;
5032 symtab->gc()->add_reference(src_obj, src_indx,
5033 ppc_object, dst_indx);
5037 ppc_object->access_from_map()->clear();
5038 ppc_object->process_gc_mark(symtab);
5039 // Don't look at .opd relocs as .opd will reference everything.
5043 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
5044 typename Target_powerpc::Relocatable_size_for_reloc>(
5053 needs_special_offset_handling,
5058 // Handle target specific gc actions when adding a gc reference from
5059 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5060 // and DST_OFF. For powerpc64, this adds a referenc to the code
5061 // section of a function descriptor.
5063 template<int size, bool big_endian>
5065 Target_powerpc<size, big_endian>::do_gc_add_reference(
5066 Symbol_table* symtab,
5068 unsigned int src_shndx,
5070 unsigned int dst_shndx,
5071 Address dst_off) const
5073 Powerpc_relobj<size, big_endian>* ppc_object
5074 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
5076 && !ppc_object->is_dynamic()
5077 && dst_shndx == ppc_object->opd_shndx())
5079 if (ppc_object->opd_valid())
5081 dst_shndx = ppc_object->get_opd_ent(dst_off);
5082 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
5086 // If we haven't run scan_opd_relocs, we must delay
5087 // processing this function descriptor reference.
5088 ppc_object->add_reference(src_obj, src_shndx, dst_off);
5093 // Add any special sections for this symbol to the gc work list.
5094 // For powerpc64, this adds the code section of a function
5097 template<int size, bool big_endian>
5099 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
5100 Symbol_table* symtab,
5105 Powerpc_relobj<size, big_endian>* ppc_object
5106 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
5108 unsigned int shndx = sym->shndx(&is_ordinary);
5109 if (is_ordinary && shndx == ppc_object->opd_shndx())
5111 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
5112 Address dst_off = gsym->value();
5113 if (ppc_object->opd_valid())
5115 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5116 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
5119 ppc_object->add_gc_mark(dst_off);
5124 // Scan relocations for a section.
5126 template<int size, bool big_endian>
5128 Target_powerpc<size, big_endian>::scan_relocs(
5129 Symbol_table* symtab,
5131 Sized_relobj_file<size, big_endian>* object,
5132 unsigned int data_shndx,
5133 unsigned int sh_type,
5134 const unsigned char* prelocs,
5136 Output_section* output_section,
5137 bool needs_special_offset_handling,
5138 size_t local_symbol_count,
5139 const unsigned char* plocal_symbols)
5141 typedef Target_powerpc<size, big_endian> Powerpc;
5142 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5144 if (sh_type == elfcpp::SHT_REL)
5146 gold_error(_("%s: unsupported REL reloc section"),
5147 object->name().c_str());
5151 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
5160 needs_special_offset_handling,
5165 // Functor class for processing the global symbol table.
5166 // Removes symbols defined on discarded opd entries.
5168 template<bool big_endian>
5169 class Global_symbol_visitor_opd
5172 Global_symbol_visitor_opd()
5176 operator()(Sized_symbol<64>* sym)
5178 if (sym->has_symtab_index()
5179 || sym->source() != Symbol::FROM_OBJECT
5180 || !sym->in_real_elf())
5183 Powerpc_relobj<64, big_endian>* symobj
5184 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
5185 if (symobj->is_dynamic()
5186 || symobj->opd_shndx() == 0)
5190 unsigned int shndx = sym->shndx(&is_ordinary);
5191 if (shndx == symobj->opd_shndx()
5192 && symobj->get_opd_discard(sym->value()))
5193 sym->set_symtab_index(-1U);
5197 template<int size, bool big_endian>
5199 Target_powerpc<size, big_endian>::define_save_restore_funcs(
5201 Symbol_table* symtab)
5205 Output_data_save_res<64, big_endian>* savres
5206 = new Output_data_save_res<64, big_endian>(symtab);
5207 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5208 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5209 savres, ORDER_TEXT, false);
5213 // Finalize the sections.
5215 template<int size, bool big_endian>
5217 Target_powerpc<size, big_endian>::do_finalize_sections(
5219 const Input_objects*,
5220 Symbol_table* symtab)
5222 if (parameters->doing_static_link())
5224 // At least some versions of glibc elf-init.o have a strong
5225 // reference to __rela_iplt marker syms. A weak ref would be
5227 if (this->iplt_ != NULL)
5229 Reloc_section* rel = this->iplt_->rel_plt();
5230 symtab->define_in_output_data("__rela_iplt_start", NULL,
5231 Symbol_table::PREDEFINED, rel, 0, 0,
5232 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5233 elfcpp::STV_HIDDEN, 0, false, true);
5234 symtab->define_in_output_data("__rela_iplt_end", NULL,
5235 Symbol_table::PREDEFINED, rel, 0, 0,
5236 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5237 elfcpp::STV_HIDDEN, 0, true, true);
5241 symtab->define_as_constant("__rela_iplt_start", NULL,
5242 Symbol_table::PREDEFINED, 0, 0,
5243 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5244 elfcpp::STV_HIDDEN, 0, true, false);
5245 symtab->define_as_constant("__rela_iplt_end", NULL,
5246 Symbol_table::PREDEFINED, 0, 0,
5247 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5248 elfcpp::STV_HIDDEN, 0, true, false);
5254 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
5255 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
5257 if (!parameters->options().relocatable())
5259 this->define_save_restore_funcs(layout, symtab);
5261 // Annoyingly, we need to make these sections now whether or
5262 // not we need them. If we delay until do_relax then we
5263 // need to mess with the relaxation machinery checkpointing.
5264 this->got_section(symtab, layout);
5265 this->make_brlt_section(layout);
5269 // Fill in some more dynamic tags.
5270 Output_data_dynamic* odyn = layout->dynamic_data();
5273 const Reloc_section* rel_plt = (this->plt_ == NULL
5275 : this->plt_->rel_plt());
5276 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
5277 this->rela_dyn_, true, size == 32);
5281 if (this->got_ != NULL)
5283 this->got_->finalize_data_size();
5284 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
5285 this->got_, this->got_->g_o_t());
5290 if (this->glink_ != NULL)
5292 this->glink_->finalize_data_size();
5293 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
5295 (this->glink_->pltresolve_size
5301 // Emit any relocs we saved in an attempt to avoid generating COPY
5303 if (this->copy_relocs_.any_saved_relocs())
5304 this->copy_relocs_.emit(this->rela_dyn_section(layout));
5307 // Return the value to use for a branch relocation.
5309 template<int size, bool big_endian>
5310 typename elfcpp::Elf_types<size>::Elf_Addr
5311 Target_powerpc<size, big_endian>::symval_for_branch(
5313 const Sized_symbol<size>* gsym,
5314 Powerpc_relobj<size, big_endian>* object,
5315 unsigned int *dest_shndx)
5321 // If the symbol is defined in an opd section, ie. is a function
5322 // descriptor, use the function descriptor code entry address
5323 Powerpc_relobj<size, big_endian>* symobj = object;
5325 && gsym->source() != Symbol::FROM_OBJECT)
5328 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
5329 unsigned int shndx = symobj->opd_shndx();
5332 Address opd_addr = symobj->get_output_section_offset(shndx);
5333 gold_assert(opd_addr != invalid_address);
5334 opd_addr += symobj->output_section(shndx)->address();
5335 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
5338 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
5339 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
5340 gold_assert(sec_addr != invalid_address);
5341 sec_addr += symobj->output_section(*dest_shndx)->address();
5342 value = sec_addr + sec_off;
5347 // Perform a relocation.
5349 template<int size, bool big_endian>
5351 Target_powerpc<size, big_endian>::Relocate::relocate(
5352 const Relocate_info<size, big_endian>* relinfo,
5353 Target_powerpc* target,
5356 const elfcpp::Rela<size, big_endian>& rela,
5357 unsigned int r_type,
5358 const Sized_symbol<size>* gsym,
5359 const Symbol_value<size>* psymval,
5360 unsigned char* view,
5362 section_size_type view_size)
5364 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
5365 || r_type == elfcpp::R_PPC_PLTREL24)
5367 && strcmp(gsym->name(), "__tls_get_addr") == 0);
5368 enum skip_tls last_tls = this->call_tls_get_addr_;
5369 this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
5372 if (last_tls == CALL_NOT_EXPECTED)
5373 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5374 _("__tls_get_addr call lacks marker reloc"));
5375 else if (last_tls == CALL_SKIP)
5378 else if (last_tls != CALL_NOT_EXPECTED)
5379 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5380 _("missing expected __tls_get_addr call"));
5382 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
5383 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
5384 Powerpc_relobj<size, big_endian>* const object
5385 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5387 bool has_plt_value = false;
5388 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5390 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
5391 : object->local_has_plt_offset(r_sym))
5393 Stub_table<size, big_endian>* stub_table
5394 = object->stub_table(relinfo->data_shndx);
5395 if (stub_table == NULL)
5397 // This is a ref from a data section to an ifunc symbol.
5398 if (target->stub_tables().size() != 0)
5399 stub_table = target->stub_tables()[0];
5401 gold_assert(stub_table != NULL);
5404 off = stub_table->find_plt_call_entry(object, gsym, r_type,
5405 rela.get_r_addend());
5407 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
5408 rela.get_r_addend());
5409 gold_assert(off != invalid_address);
5410 value = stub_table->stub_address() + off;
5411 has_plt_value = true;
5414 if (r_type == elfcpp::R_POWERPC_GOT16
5415 || r_type == elfcpp::R_POWERPC_GOT16_LO
5416 || r_type == elfcpp::R_POWERPC_GOT16_HI
5417 || r_type == elfcpp::R_POWERPC_GOT16_HA
5418 || r_type == elfcpp::R_PPC64_GOT16_DS
5419 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
5423 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5424 value = gsym->got_offset(GOT_TYPE_STANDARD);
5428 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5429 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5430 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
5432 value -= target->got_section()->got_base_offset(object);
5434 else if (r_type == elfcpp::R_PPC64_TOC)
5436 value = (target->got_section()->output_section()->address()
5437 + object->toc_base_offset());
5439 else if (gsym != NULL
5440 && (r_type == elfcpp::R_POWERPC_REL24
5441 || r_type == elfcpp::R_PPC_PLTREL24)
5446 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5447 Valtype* wv = reinterpret_cast<Valtype*>(view);
5448 bool can_plt_call = false;
5449 if (rela.get_r_offset() + 8 <= view_size)
5451 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
5452 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
5455 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
5457 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
5458 can_plt_call = true;
5463 // If we don't have a branch and link followed by a nop,
5464 // we can't go via the plt because there is no place to
5465 // put a toc restoring instruction.
5466 // Unless we know we won't be returning.
5467 if (strcmp(gsym->name(), "__libc_start_main") == 0)
5468 can_plt_call = true;
5472 // This is not an error in one special case: A self
5473 // call. It isn't possible to cheaply verify we have
5474 // such a call so just check for a call to the same
5477 Address code = value;
5478 if (gsym->source() == Symbol::FROM_OBJECT
5479 && gsym->object() == object)
5481 Address addend = rela.get_r_addend();
5482 unsigned int dest_shndx;
5483 Address opdent = psymval->value(object, addend);
5484 code = target->symval_for_branch(opdent, gsym, object,
5487 if (dest_shndx == 0)
5488 dest_shndx = gsym->shndx(&is_ordinary);
5489 ok = dest_shndx == relinfo->data_shndx;
5493 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5494 _("call lacks nop, can't restore toc; "
5495 "recompile with -fPIC"));
5501 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5502 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5503 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5504 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5506 // First instruction of a global dynamic sequence, arg setup insn.
5507 const bool final = gsym == NULL || gsym->final_value_is_known();
5508 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5509 enum Got_type got_type = GOT_TYPE_STANDARD;
5510 if (tls_type == tls::TLSOPT_NONE)
5511 got_type = GOT_TYPE_TLSGD;
5512 else if (tls_type == tls::TLSOPT_TO_IE)
5513 got_type = GOT_TYPE_TPREL;
5514 if (got_type != GOT_TYPE_STANDARD)
5518 gold_assert(gsym->has_got_offset(got_type));
5519 value = gsym->got_offset(got_type);
5523 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5524 gold_assert(object->local_has_got_offset(r_sym, got_type));
5525 value = object->local_got_offset(r_sym, got_type);
5527 value -= target->got_section()->got_base_offset(object);
5529 if (tls_type == tls::TLSOPT_TO_IE)
5531 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5532 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5534 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5535 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5536 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
5538 insn |= 32 << 26; // lwz
5540 insn |= 58 << 26; // ld
5541 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5543 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5544 - elfcpp::R_POWERPC_GOT_TLSGD16);
5546 else if (tls_type == tls::TLSOPT_TO_LE)
5548 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5549 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5551 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5552 Insn insn = addis_3_13;
5555 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5556 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5557 value = psymval->value(object, rela.get_r_addend());
5561 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5563 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5564 r_type = elfcpp::R_POWERPC_NONE;
5568 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5569 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5570 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5571 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5573 // First instruction of a local dynamic sequence, arg setup insn.
5574 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5575 if (tls_type == tls::TLSOPT_NONE)
5577 value = target->tlsld_got_offset();
5578 value -= target->got_section()->got_base_offset(object);
5582 gold_assert(tls_type == tls::TLSOPT_TO_LE);
5583 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5584 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5586 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5587 Insn insn = addis_3_13;
5590 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5591 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5596 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5598 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5599 r_type = elfcpp::R_POWERPC_NONE;
5603 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
5604 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
5605 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
5606 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
5608 // Accesses relative to a local dynamic sequence address,
5609 // no optimisation here.
5612 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
5613 value = gsym->got_offset(GOT_TYPE_DTPREL);
5617 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5618 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
5619 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
5621 value -= target->got_section()->got_base_offset(object);
5623 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5624 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5625 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5626 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5628 // First instruction of initial exec sequence.
5629 const bool final = gsym == NULL || gsym->final_value_is_known();
5630 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5631 if (tls_type == tls::TLSOPT_NONE)
5635 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
5636 value = gsym->got_offset(GOT_TYPE_TPREL);
5640 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5641 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
5642 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
5644 value -= target->got_section()->got_base_offset(object);
5648 gold_assert(tls_type == tls::TLSOPT_TO_LE);
5649 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5650 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5652 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5653 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5654 insn &= (1 << 26) - (1 << 21); // extract rt from ld
5659 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5660 r_type = elfcpp::R_POWERPC_TPREL16_HA;
5661 value = psymval->value(object, rela.get_r_addend());
5665 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5667 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5668 r_type = elfcpp::R_POWERPC_NONE;
5672 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5673 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5675 // Second instruction of a global dynamic sequence,
5676 // the __tls_get_addr call
5677 this->call_tls_get_addr_ = CALL_EXPECTED;
5678 const bool final = gsym == NULL || gsym->final_value_is_known();
5679 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5680 if (tls_type != tls::TLSOPT_NONE)
5682 if (tls_type == tls::TLSOPT_TO_IE)
5684 Insn* iview = reinterpret_cast<Insn*>(view);
5685 Insn insn = add_3_3_13;
5688 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5689 r_type = elfcpp::R_POWERPC_NONE;
5693 Insn* iview = reinterpret_cast<Insn*>(view);
5694 Insn insn = addi_3_3;
5695 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5696 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5697 view += 2 * big_endian;
5698 value = psymval->value(object, rela.get_r_addend());
5700 this->call_tls_get_addr_ = CALL_SKIP;
5703 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5704 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5706 // Second instruction of a local dynamic sequence,
5707 // the __tls_get_addr call
5708 this->call_tls_get_addr_ = CALL_EXPECTED;
5709 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5710 if (tls_type == tls::TLSOPT_TO_LE)
5712 Insn* iview = reinterpret_cast<Insn*>(view);
5713 Insn insn = addi_3_3;
5714 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5715 this->call_tls_get_addr_ = CALL_SKIP;
5716 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5717 view += 2 * big_endian;
5721 else if (r_type == elfcpp::R_POWERPC_TLS)
5723 // Second instruction of an initial exec sequence
5724 const bool final = gsym == NULL || gsym->final_value_is_known();
5725 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5726 if (tls_type == tls::TLSOPT_TO_LE)
5728 Insn* iview = reinterpret_cast<Insn*>(view);
5729 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5730 unsigned int reg = size == 32 ? 2 : 13;
5731 insn = at_tls_transform(insn, reg);
5732 gold_assert(insn != 0);
5733 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5734 r_type = elfcpp::R_POWERPC_TPREL16_LO;
5735 view += 2 * big_endian;
5736 value = psymval->value(object, rela.get_r_addend());
5739 else if (!has_plt_value)
5742 unsigned int dest_shndx;
5743 if (r_type != elfcpp::R_PPC_PLTREL24)
5744 addend = rela.get_r_addend();
5745 value = psymval->value(object, addend);
5746 if (size == 64 && is_branch_reloc(r_type))
5747 value = target->symval_for_branch(value, gsym, object, &dest_shndx);
5748 unsigned int max_branch_offset = 0;
5749 if (r_type == elfcpp::R_POWERPC_REL24
5750 || r_type == elfcpp::R_PPC_PLTREL24
5751 || r_type == elfcpp::R_PPC_LOCAL24PC)
5752 max_branch_offset = 1 << 25;
5753 else if (r_type == elfcpp::R_POWERPC_REL14
5754 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
5755 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
5756 max_branch_offset = 1 << 15;
5757 if (max_branch_offset != 0
5758 && value - address + max_branch_offset >= 2 * max_branch_offset)
5760 Stub_table<size, big_endian>* stub_table
5761 = object->stub_table(relinfo->data_shndx);
5762 gold_assert(stub_table != NULL);
5763 Address off = stub_table->find_long_branch_entry(object, value);
5764 if (off != invalid_address)
5765 value = stub_table->stub_address() + stub_table->plt_size() + off;
5771 case elfcpp::R_PPC64_REL64:
5772 case elfcpp::R_POWERPC_REL32:
5773 case elfcpp::R_POWERPC_REL24:
5774 case elfcpp::R_PPC_PLTREL24:
5775 case elfcpp::R_PPC_LOCAL24PC:
5776 case elfcpp::R_POWERPC_REL16:
5777 case elfcpp::R_POWERPC_REL16_LO:
5778 case elfcpp::R_POWERPC_REL16_HI:
5779 case elfcpp::R_POWERPC_REL16_HA:
5780 case elfcpp::R_POWERPC_REL14:
5781 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5782 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5786 case elfcpp::R_PPC64_TOC16:
5787 case elfcpp::R_PPC64_TOC16_LO:
5788 case elfcpp::R_PPC64_TOC16_HI:
5789 case elfcpp::R_PPC64_TOC16_HA:
5790 case elfcpp::R_PPC64_TOC16_DS:
5791 case elfcpp::R_PPC64_TOC16_LO_DS:
5792 // Subtract the TOC base address.
5793 value -= (target->got_section()->output_section()->address()
5794 + object->toc_base_offset());
5797 case elfcpp::R_POWERPC_SECTOFF:
5798 case elfcpp::R_POWERPC_SECTOFF_LO:
5799 case elfcpp::R_POWERPC_SECTOFF_HI:
5800 case elfcpp::R_POWERPC_SECTOFF_HA:
5801 case elfcpp::R_PPC64_SECTOFF_DS:
5802 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5804 value -= os->address();
5807 case elfcpp::R_PPC64_TPREL16_DS:
5808 case elfcpp::R_PPC64_TPREL16_LO_DS:
5810 // R_PPC_TLSGD and R_PPC_TLSLD
5812 case elfcpp::R_POWERPC_TPREL16:
5813 case elfcpp::R_POWERPC_TPREL16_LO:
5814 case elfcpp::R_POWERPC_TPREL16_HI:
5815 case elfcpp::R_POWERPC_TPREL16_HA:
5816 case elfcpp::R_POWERPC_TPREL:
5817 case elfcpp::R_PPC64_TPREL16_HIGHER:
5818 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5819 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5820 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5821 // tls symbol values are relative to tls_segment()->vaddr()
5825 case elfcpp::R_PPC64_DTPREL16_DS:
5826 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5827 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5828 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5829 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5830 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5832 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
5833 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
5835 case elfcpp::R_POWERPC_DTPREL16:
5836 case elfcpp::R_POWERPC_DTPREL16_LO:
5837 case elfcpp::R_POWERPC_DTPREL16_HI:
5838 case elfcpp::R_POWERPC_DTPREL16_HA:
5839 case elfcpp::R_POWERPC_DTPREL:
5840 // tls symbol values are relative to tls_segment()->vaddr()
5841 value -= dtp_offset;
5848 Insn branch_bit = 0;
5851 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5852 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5853 branch_bit = 1 << 21;
5854 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5855 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5857 Insn* iview = reinterpret_cast<Insn*>(view);
5858 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5861 if (this->is_isa_v2)
5863 // Set 'a' bit. This is 0b00010 in BO field for branch
5864 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
5865 // for branch on CTR insns (BO == 1a00t or 1a01t).
5866 if ((insn & (0x14 << 21)) == (0x04 << 21))
5868 else if ((insn & (0x14 << 21)) == (0x10 << 21))
5875 // Invert 'y' bit if not the default.
5876 if (static_cast<Signed_address>(value) < 0)
5879 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5887 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
5890 case elfcpp::R_POWERPC_ADDR32:
5891 case elfcpp::R_POWERPC_UADDR32:
5893 overflow = Reloc::CHECK_BITFIELD;
5896 case elfcpp::R_POWERPC_REL32:
5898 overflow = Reloc::CHECK_SIGNED;
5901 case elfcpp::R_POWERPC_ADDR24:
5902 case elfcpp::R_POWERPC_ADDR16:
5903 case elfcpp::R_POWERPC_UADDR16:
5904 case elfcpp::R_PPC64_ADDR16_DS:
5905 case elfcpp::R_POWERPC_ADDR14:
5906 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5907 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5908 overflow = Reloc::CHECK_BITFIELD;
5911 case elfcpp::R_POWERPC_REL24:
5912 case elfcpp::R_PPC_PLTREL24:
5913 case elfcpp::R_PPC_LOCAL24PC:
5914 case elfcpp::R_POWERPC_REL16:
5915 case elfcpp::R_PPC64_TOC16:
5916 case elfcpp::R_POWERPC_GOT16:
5917 case elfcpp::R_POWERPC_SECTOFF:
5918 case elfcpp::R_POWERPC_TPREL16:
5919 case elfcpp::R_POWERPC_DTPREL16:
5920 case elfcpp::R_PPC64_TPREL16_DS:
5921 case elfcpp::R_PPC64_DTPREL16_DS:
5922 case elfcpp::R_PPC64_TOC16_DS:
5923 case elfcpp::R_PPC64_GOT16_DS:
5924 case elfcpp::R_PPC64_SECTOFF_DS:
5925 case elfcpp::R_POWERPC_REL14:
5926 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5927 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5928 case elfcpp::R_POWERPC_GOT_TLSGD16:
5929 case elfcpp::R_POWERPC_GOT_TLSLD16:
5930 case elfcpp::R_POWERPC_GOT_TPREL16:
5931 case elfcpp::R_POWERPC_GOT_DTPREL16:
5932 overflow = Reloc::CHECK_SIGNED;
5936 typename Powerpc_relocate_functions<size, big_endian>::Status status
5937 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
5940 case elfcpp::R_POWERPC_NONE:
5941 case elfcpp::R_POWERPC_TLS:
5942 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5943 case elfcpp::R_POWERPC_GNU_VTENTRY:
5944 case elfcpp::R_PPC_EMB_MRKREF:
5947 case elfcpp::R_PPC64_ADDR64:
5948 case elfcpp::R_PPC64_REL64:
5949 case elfcpp::R_PPC64_TOC:
5950 Reloc::addr64(view, value);
5953 case elfcpp::R_POWERPC_TPREL:
5954 case elfcpp::R_POWERPC_DTPREL:
5956 Reloc::addr64(view, value);
5958 status = Reloc::addr32(view, value, overflow);
5961 case elfcpp::R_PPC64_UADDR64:
5962 Reloc::addr64_u(view, value);
5965 case elfcpp::R_POWERPC_ADDR32:
5966 status = Reloc::addr32(view, value, overflow);
5969 case elfcpp::R_POWERPC_REL32:
5970 case elfcpp::R_POWERPC_UADDR32:
5971 status = Reloc::addr32_u(view, value, overflow);
5974 case elfcpp::R_POWERPC_ADDR24:
5975 case elfcpp::R_POWERPC_REL24:
5976 case elfcpp::R_PPC_PLTREL24:
5977 case elfcpp::R_PPC_LOCAL24PC:
5978 status = Reloc::addr24(view, value, overflow);
5981 case elfcpp::R_POWERPC_GOT_DTPREL16:
5982 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5985 status = Reloc::addr16_ds(view, value, overflow);
5988 case elfcpp::R_POWERPC_ADDR16:
5989 case elfcpp::R_POWERPC_REL16:
5990 case elfcpp::R_PPC64_TOC16:
5991 case elfcpp::R_POWERPC_GOT16:
5992 case elfcpp::R_POWERPC_SECTOFF:
5993 case elfcpp::R_POWERPC_TPREL16:
5994 case elfcpp::R_POWERPC_DTPREL16:
5995 case elfcpp::R_POWERPC_GOT_TLSGD16:
5996 case elfcpp::R_POWERPC_GOT_TLSLD16:
5997 case elfcpp::R_POWERPC_GOT_TPREL16:
5998 case elfcpp::R_POWERPC_ADDR16_LO:
5999 case elfcpp::R_POWERPC_REL16_LO:
6000 case elfcpp::R_PPC64_TOC16_LO:
6001 case elfcpp::R_POWERPC_GOT16_LO:
6002 case elfcpp::R_POWERPC_SECTOFF_LO:
6003 case elfcpp::R_POWERPC_TPREL16_LO:
6004 case elfcpp::R_POWERPC_DTPREL16_LO:
6005 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6006 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6007 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6008 status = Reloc::addr16(view, value, overflow);
6011 case elfcpp::R_POWERPC_UADDR16:
6012 status = Reloc::addr16_u(view, value, overflow);
6015 case elfcpp::R_POWERPC_ADDR16_HI:
6016 case elfcpp::R_POWERPC_REL16_HI:
6017 case elfcpp::R_PPC64_TOC16_HI:
6018 case elfcpp::R_POWERPC_GOT16_HI:
6019 case elfcpp::R_POWERPC_SECTOFF_HI:
6020 case elfcpp::R_POWERPC_TPREL16_HI:
6021 case elfcpp::R_POWERPC_DTPREL16_HI:
6022 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6023 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6024 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6025 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6026 Reloc::addr16_hi(view, value);
6029 case elfcpp::R_POWERPC_ADDR16_HA:
6030 case elfcpp::R_POWERPC_REL16_HA:
6031 case elfcpp::R_PPC64_TOC16_HA:
6032 case elfcpp::R_POWERPC_GOT16_HA:
6033 case elfcpp::R_POWERPC_SECTOFF_HA:
6034 case elfcpp::R_POWERPC_TPREL16_HA:
6035 case elfcpp::R_POWERPC_DTPREL16_HA:
6036 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6037 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6038 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6039 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6040 Reloc::addr16_ha(view, value);
6043 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6045 // R_PPC_EMB_NADDR16_LO
6047 case elfcpp::R_PPC64_ADDR16_HIGHER:
6048 case elfcpp::R_PPC64_TPREL16_HIGHER:
6049 Reloc::addr16_hi2(view, value);
6052 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6054 // R_PPC_EMB_NADDR16_HI
6056 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6057 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6058 Reloc::addr16_ha2(view, value);
6061 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6063 // R_PPC_EMB_NADDR16_HA
6065 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6066 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6067 Reloc::addr16_hi3(view, value);
6070 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6074 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6075 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6076 Reloc::addr16_ha3(view, value);
6079 case elfcpp::R_PPC64_DTPREL16_DS:
6080 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6082 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6084 case elfcpp::R_PPC64_TPREL16_DS:
6085 case elfcpp::R_PPC64_TPREL16_LO_DS:
6087 // R_PPC_TLSGD, R_PPC_TLSLD
6089 case elfcpp::R_PPC64_ADDR16_DS:
6090 case elfcpp::R_PPC64_ADDR16_LO_DS:
6091 case elfcpp::R_PPC64_TOC16_DS:
6092 case elfcpp::R_PPC64_TOC16_LO_DS:
6093 case elfcpp::R_PPC64_GOT16_DS:
6094 case elfcpp::R_PPC64_GOT16_LO_DS:
6095 case elfcpp::R_PPC64_SECTOFF_DS:
6096 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6097 status = Reloc::addr16_ds(view, value, overflow);
6100 case elfcpp::R_POWERPC_ADDR14:
6101 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6102 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6103 case elfcpp::R_POWERPC_REL14:
6104 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6105 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6106 status = Reloc::addr14(view, value, overflow);
6109 case elfcpp::R_POWERPC_COPY:
6110 case elfcpp::R_POWERPC_GLOB_DAT:
6111 case elfcpp::R_POWERPC_JMP_SLOT:
6112 case elfcpp::R_POWERPC_RELATIVE:
6113 case elfcpp::R_POWERPC_DTPMOD:
6114 case elfcpp::R_PPC64_JMP_IREL:
6115 case elfcpp::R_POWERPC_IRELATIVE:
6116 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6117 _("unexpected reloc %u in object file"),
6121 case elfcpp::R_PPC_EMB_SDA21:
6126 // R_PPC64_TOCSAVE. For the time being this can be ignored.
6130 case elfcpp::R_PPC_EMB_SDA2I16:
6131 case elfcpp::R_PPC_EMB_SDA2REL:
6134 // R_PPC64_TLSGD, R_PPC64_TLSLD
6137 case elfcpp::R_POWERPC_PLT32:
6138 case elfcpp::R_POWERPC_PLTREL32:
6139 case elfcpp::R_POWERPC_PLT16_LO:
6140 case elfcpp::R_POWERPC_PLT16_HI:
6141 case elfcpp::R_POWERPC_PLT16_HA:
6142 case elfcpp::R_PPC_SDAREL16:
6143 case elfcpp::R_POWERPC_ADDR30:
6144 case elfcpp::R_PPC64_PLT64:
6145 case elfcpp::R_PPC64_PLTREL64:
6146 case elfcpp::R_PPC64_PLTGOT16:
6147 case elfcpp::R_PPC64_PLTGOT16_LO:
6148 case elfcpp::R_PPC64_PLTGOT16_HI:
6149 case elfcpp::R_PPC64_PLTGOT16_HA:
6150 case elfcpp::R_PPC64_PLT16_LO_DS:
6151 case elfcpp::R_PPC64_PLTGOT16_DS:
6152 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
6153 case elfcpp::R_PPC_EMB_RELSEC16:
6154 case elfcpp::R_PPC_EMB_RELST_LO:
6155 case elfcpp::R_PPC_EMB_RELST_HI:
6156 case elfcpp::R_PPC_EMB_RELST_HA:
6157 case elfcpp::R_PPC_EMB_BIT_FLD:
6158 case elfcpp::R_PPC_EMB_RELSDA:
6159 case elfcpp::R_PPC_TOC16:
6162 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6163 _("unsupported reloc %u"),
6167 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
6168 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6169 _("relocation overflow"));
6174 // Relocate section data.
6176 template<int size, bool big_endian>
6178 Target_powerpc<size, big_endian>::relocate_section(
6179 const Relocate_info<size, big_endian>* relinfo,
6180 unsigned int sh_type,
6181 const unsigned char* prelocs,
6183 Output_section* output_section,
6184 bool needs_special_offset_handling,
6185 unsigned char* view,
6187 section_size_type view_size,
6188 const Reloc_symbol_changes* reloc_symbol_changes)
6190 typedef Target_powerpc<size, big_endian> Powerpc;
6191 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
6192 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
6193 Powerpc_comdat_behavior;
6195 gold_assert(sh_type == elfcpp::SHT_RELA);
6197 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
6198 Powerpc_relocate, Powerpc_comdat_behavior>(
6204 needs_special_offset_handling,
6208 reloc_symbol_changes);
6211 class Powerpc_scan_relocatable_reloc
6214 // Return the strategy to use for a local symbol which is not a
6215 // section symbol, given the relocation type.
6216 inline Relocatable_relocs::Reloc_strategy
6217 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
6219 if (r_type == 0 && r_sym == 0)
6220 return Relocatable_relocs::RELOC_DISCARD;
6221 return Relocatable_relocs::RELOC_COPY;
6224 // Return the strategy to use for a local symbol which is a section
6225 // symbol, given the relocation type.
6226 inline Relocatable_relocs::Reloc_strategy
6227 local_section_strategy(unsigned int, Relobj*)
6229 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
6232 // Return the strategy to use for a global symbol, given the
6233 // relocation type, the object, and the symbol index.
6234 inline Relocatable_relocs::Reloc_strategy
6235 global_strategy(unsigned int r_type, Relobj*, unsigned int)
6237 if (r_type == elfcpp::R_PPC_PLTREL24)
6238 return Relocatable_relocs::RELOC_SPECIAL;
6239 return Relocatable_relocs::RELOC_COPY;
6243 // Scan the relocs during a relocatable link.
6245 template<int size, bool big_endian>
6247 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
6248 Symbol_table* symtab,
6250 Sized_relobj_file<size, big_endian>* object,
6251 unsigned int data_shndx,
6252 unsigned int sh_type,
6253 const unsigned char* prelocs,
6255 Output_section* output_section,
6256 bool needs_special_offset_handling,
6257 size_t local_symbol_count,
6258 const unsigned char* plocal_symbols,
6259 Relocatable_relocs* rr)
6261 gold_assert(sh_type == elfcpp::SHT_RELA);
6263 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
6264 Powerpc_scan_relocatable_reloc>(
6272 needs_special_offset_handling,
6278 // Emit relocations for a section.
6279 // This is a modified version of the function by the same name in
6280 // target-reloc.h. Using relocate_special_relocatable for
6281 // R_PPC_PLTREL24 would require duplication of the entire body of the
6282 // loop, so we may as well duplicate the whole thing.
6284 template<int size, bool big_endian>
6286 Target_powerpc<size, big_endian>::relocate_relocs(
6287 const Relocate_info<size, big_endian>* relinfo,
6288 unsigned int sh_type,
6289 const unsigned char* prelocs,
6291 Output_section* output_section,
6292 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
6293 const Relocatable_relocs* rr,
6295 Address view_address,
6297 unsigned char* reloc_view,
6298 section_size_type reloc_view_size)
6300 gold_assert(sh_type == elfcpp::SHT_RELA);
6302 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
6304 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
6306 const int reloc_size
6307 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
6309 Powerpc_relobj<size, big_endian>* const object
6310 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6311 const unsigned int local_count = object->local_symbol_count();
6312 unsigned int got2_shndx = object->got2_shndx();
6313 Address got2_addend = 0;
6314 if (got2_shndx != 0)
6316 got2_addend = object->get_output_section_offset(got2_shndx);
6317 gold_assert(got2_addend != invalid_address);
6320 unsigned char* pwrite = reloc_view;
6321 bool zap_next = false;
6322 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6324 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
6325 if (strategy == Relocatable_relocs::RELOC_DISCARD)
6328 Reltype reloc(prelocs);
6329 Reltype_write reloc_write(pwrite);
6331 Address offset = reloc.get_r_offset();
6332 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
6333 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
6334 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
6335 const unsigned int orig_r_sym = r_sym;
6336 typename elfcpp::Elf_types<size>::Elf_Swxword addend
6337 = reloc.get_r_addend();
6338 const Symbol* gsym = NULL;
6342 // We could arrange to discard these and other relocs for
6343 // tls optimised sequences in the strategy methods, but for
6344 // now do as BFD ld does.
6345 r_type = elfcpp::R_POWERPC_NONE;
6349 // Get the new symbol index.
6350 if (r_sym < local_count)
6354 case Relocatable_relocs::RELOC_COPY:
6355 case Relocatable_relocs::RELOC_SPECIAL:
6358 r_sym = object->symtab_index(r_sym);
6359 gold_assert(r_sym != -1U);
6363 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
6365 // We are adjusting a section symbol. We need to find
6366 // the symbol table index of the section symbol for
6367 // the output section corresponding to input section
6368 // in which this symbol is defined.
6369 gold_assert(r_sym < local_count);
6371 unsigned int shndx =
6372 object->local_symbol_input_shndx(r_sym, &is_ordinary);
6373 gold_assert(is_ordinary);
6374 Output_section* os = object->output_section(shndx);
6375 gold_assert(os != NULL);
6376 gold_assert(os->needs_symtab_index());
6377 r_sym = os->symtab_index();
6387 gsym = object->global_symbol(r_sym);
6388 gold_assert(gsym != NULL);
6389 if (gsym->is_forwarder())
6390 gsym = relinfo->symtab->resolve_forwards(gsym);
6392 gold_assert(gsym->has_symtab_index());
6393 r_sym = gsym->symtab_index();
6396 // Get the new offset--the location in the output section where
6397 // this relocation should be applied.
6398 if (static_cast<Address>(offset_in_output_section) != invalid_address)
6399 offset += offset_in_output_section;
6402 section_offset_type sot_offset =
6403 convert_types<section_offset_type, Address>(offset);
6404 section_offset_type new_sot_offset =
6405 output_section->output_offset(object, relinfo->data_shndx,
6407 gold_assert(new_sot_offset != -1);
6408 offset = new_sot_offset;
6411 // In an object file, r_offset is an offset within the section.
6412 // In an executable or dynamic object, generated by
6413 // --emit-relocs, r_offset is an absolute address.
6414 if (!parameters->options().relocatable())
6416 offset += view_address;
6417 if (static_cast<Address>(offset_in_output_section) != invalid_address)
6418 offset -= offset_in_output_section;
6421 // Handle the reloc addend based on the strategy.
6422 if (strategy == Relocatable_relocs::RELOC_COPY)
6424 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
6426 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
6427 addend = psymval->value(object, addend);
6429 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
6431 if (addend >= 32768)
6432 addend += got2_addend;
6437 if (!parameters->options().relocatable())
6439 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6440 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6441 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6442 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6444 // First instruction of a global dynamic sequence,
6446 const bool final = gsym == NULL || gsym->final_value_is_known();
6447 switch (this->optimize_tls_gd(final))
6449 case tls::TLSOPT_TO_IE:
6450 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6451 - elfcpp::R_POWERPC_GOT_TLSGD16);
6453 case tls::TLSOPT_TO_LE:
6454 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6455 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6456 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6459 r_type = elfcpp::R_POWERPC_NONE;
6460 offset -= 2 * big_endian;
6467 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6468 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6469 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6470 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6472 // First instruction of a local dynamic sequence,
6474 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
6476 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6477 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6479 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6480 const Output_section* os = relinfo->layout->tls_segment()
6482 gold_assert(os != NULL);
6483 gold_assert(os->needs_symtab_index());
6484 r_sym = os->symtab_index();
6485 addend = dtp_offset;
6489 r_type = elfcpp::R_POWERPC_NONE;
6490 offset -= 2 * big_endian;
6494 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6495 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6496 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6497 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6499 // First instruction of initial exec sequence.
6500 const bool final = gsym == NULL || gsym->final_value_is_known();
6501 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
6503 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6504 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6505 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6508 r_type = elfcpp::R_POWERPC_NONE;
6509 offset -= 2 * big_endian;
6513 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6514 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6516 // Second instruction of a global dynamic sequence,
6517 // the __tls_get_addr call
6518 const bool final = gsym == NULL || gsym->final_value_is_known();
6519 switch (this->optimize_tls_gd(final))
6521 case tls::TLSOPT_TO_IE:
6522 r_type = elfcpp::R_POWERPC_NONE;
6525 case tls::TLSOPT_TO_LE:
6526 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6527 offset += 2 * big_endian;
6534 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6535 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6537 // Second instruction of a local dynamic sequence,
6538 // the __tls_get_addr call
6539 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
6541 const Output_section* os = relinfo->layout->tls_segment()
6543 gold_assert(os != NULL);
6544 gold_assert(os->needs_symtab_index());
6545 r_sym = os->symtab_index();
6546 addend = dtp_offset;
6547 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6548 offset += 2 * big_endian;
6552 else if (r_type == elfcpp::R_POWERPC_TLS)
6554 // Second instruction of an initial exec sequence
6555 const bool final = gsym == NULL || gsym->final_value_is_known();
6556 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
6558 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6559 offset += 2 * big_endian;
6564 reloc_write.put_r_offset(offset);
6565 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
6566 reloc_write.put_r_addend(addend);
6568 pwrite += reloc_size;
6571 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
6572 == reloc_view_size);
6575 // Return the value to use for a dynamic symbol which requires special
6576 // treatment. This is how we support equality comparisons of function
6577 // pointers across shared library boundaries, as described in the
6578 // processor specific ABI supplement.
6580 template<int size, bool big_endian>
6582 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6586 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6587 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
6588 p != this->stub_tables_.end();
6591 Address off = (*p)->find_plt_call_entry(gsym);
6592 if (off != invalid_address)
6593 return (*p)->stub_address() + off;
6599 // Return the PLT address to use for a local symbol.
6600 template<int size, bool big_endian>
6602 Target_powerpc<size, big_endian>::do_plt_address_for_local(
6603 const Relobj* object,
6604 unsigned int symndx) const
6608 const Sized_relobj<size, big_endian>* relobj
6609 = static_cast<const Sized_relobj<size, big_endian>*>(object);
6610 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
6611 p != this->stub_tables_.end();
6614 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
6616 if (off != invalid_address)
6617 return (*p)->stub_address() + off;
6623 // Return the PLT address to use for a global symbol.
6624 template<int size, bool big_endian>
6626 Target_powerpc<size, big_endian>::do_plt_address_for_global(
6627 const Symbol* gsym) const
6631 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
6632 p != this->stub_tables_.end();
6635 Address off = (*p)->find_plt_call_entry(gsym);
6636 if (off != invalid_address)
6637 return (*p)->stub_address() + off;
6643 // Return the offset to use for the GOT_INDX'th got entry which is
6644 // for a local tls symbol specified by OBJECT, SYMNDX.
6645 template<int size, bool big_endian>
6647 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
6648 const Relobj* object,
6649 unsigned int symndx,
6650 unsigned int got_indx) const
6652 const Powerpc_relobj<size, big_endian>* ppc_object
6653 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
6654 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
6656 for (Got_type got_type = GOT_TYPE_TLSGD;
6657 got_type <= GOT_TYPE_TPREL;
6658 got_type = Got_type(got_type + 1))
6659 if (ppc_object->local_has_got_offset(symndx, got_type))
6661 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
6662 if (got_type == GOT_TYPE_TLSGD)
6664 if (off == got_indx * (size / 8))
6666 if (got_type == GOT_TYPE_TPREL)
6676 // Return the offset to use for the GOT_INDX'th got entry which is
6677 // for global tls symbol GSYM.
6678 template<int size, bool big_endian>
6680 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
6682 unsigned int got_indx) const
6684 if (gsym->type() == elfcpp::STT_TLS)
6686 for (Got_type got_type = GOT_TYPE_TLSGD;
6687 got_type <= GOT_TYPE_TPREL;
6688 got_type = Got_type(got_type + 1))
6689 if (gsym->has_got_offset(got_type))
6691 unsigned int off = gsym->got_offset(got_type);
6692 if (got_type == GOT_TYPE_TLSGD)
6694 if (off == got_indx * (size / 8))
6696 if (got_type == GOT_TYPE_TPREL)
6706 // The selector for powerpc object files.
6708 template<int size, bool big_endian>
6709 class Target_selector_powerpc : public Target_selector
6712 Target_selector_powerpc()
6713 : Target_selector(elfcpp::EM_NONE, size, big_endian,
6715 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
6716 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
6718 ? (big_endian ? "elf64ppc" : "elf64lppc")
6719 : (big_endian ? "elf32ppc" : "elf32lppc")))
6723 do_recognize(Input_file*, off_t, int machine, int, int)
6728 if (machine != elfcpp::EM_PPC64)
6733 if (machine != elfcpp::EM_PPC)
6741 return this->instantiate_target();
6745 do_instantiate_target()
6746 { return new Target_powerpc<size, big_endian>(); }
6749 Target_selector_powerpc<32, true> target_selector_ppc32;
6750 Target_selector_powerpc<32, false> target_selector_ppc32le;
6751 Target_selector_powerpc<64, true> target_selector_ppc64;
6752 Target_selector_powerpc<64, false> target_selector_ppc64le;
6754 } // End anonymous namespace.