1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2014 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.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
50 template<int size, bool big_endian>
51 class Output_data_plt_powerpc;
53 template<int size, bool big_endian>
54 class Output_data_brlt_powerpc;
56 template<int size, bool big_endian>
57 class Output_data_got_powerpc;
59 template<int size, bool big_endian>
60 class Output_data_glink;
62 template<int size, bool big_endian>
66 is_branch_reloc(unsigned int r_type);
68 template<int size, bool big_endian>
69 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
72 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
73 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
74 typedef Unordered_map<Address, Section_refs> Access_from;
76 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
77 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
78 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
79 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
80 opd_ent_(), access_from_map_(), has14_(), stub_table_(),
81 e_flags_(ehdr.get_e_flags()), st_other_()
83 this->set_abiversion(0);
89 // Read the symbols then set up st_other vector.
91 do_read_symbols(Read_symbols_data*);
93 // The .got2 section shndx.
98 return this->special_;
103 // The .opd section shndx.
110 return this->special_;
113 // Init OPD entry arrays.
115 init_opd(size_t opd_size)
117 size_t count = this->opd_ent_ndx(opd_size);
118 this->opd_ent_.resize(count);
121 // Return section and offset of function entry for .opd + R_OFF.
123 get_opd_ent(Address r_off, Address* value = NULL) const
125 size_t ndx = this->opd_ent_ndx(r_off);
126 gold_assert(ndx < this->opd_ent_.size());
127 gold_assert(this->opd_ent_[ndx].shndx != 0);
129 *value = this->opd_ent_[ndx].off;
130 return this->opd_ent_[ndx].shndx;
133 // Set section and offset of function entry for .opd + R_OFF.
135 set_opd_ent(Address r_off, unsigned int shndx, Address value)
137 size_t ndx = this->opd_ent_ndx(r_off);
138 gold_assert(ndx < this->opd_ent_.size());
139 this->opd_ent_[ndx].shndx = shndx;
140 this->opd_ent_[ndx].off = value;
143 // Return discard flag for .opd + R_OFF.
145 get_opd_discard(Address r_off) const
147 size_t ndx = this->opd_ent_ndx(r_off);
148 gold_assert(ndx < this->opd_ent_.size());
149 return this->opd_ent_[ndx].discard;
152 // Set discard flag for .opd + R_OFF.
154 set_opd_discard(Address r_off)
156 size_t ndx = this->opd_ent_ndx(r_off);
157 gold_assert(ndx < this->opd_ent_.size());
158 this->opd_ent_[ndx].discard = true;
163 { return this->opd_valid_; }
167 { this->opd_valid_ = true; }
169 // Examine .rela.opd to build info about function entry points.
171 scan_opd_relocs(size_t reloc_count,
172 const unsigned char* prelocs,
173 const unsigned char* plocal_syms);
175 // Perform the Sized_relobj_file method, then set up opd info from
178 do_read_relocs(Read_relocs_data*);
181 do_find_special_sections(Read_symbols_data* sd);
183 // Adjust this local symbol value. Return false if the symbol
184 // should be discarded from the output file.
186 do_adjust_local_symbol(Symbol_value<size>* lv) const
188 if (size == 64 && this->opd_shndx() != 0)
191 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
193 if (this->get_opd_discard(lv->input_value()))
201 { return &this->access_from_map_; }
203 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
204 // section at DST_OFF.
206 add_reference(Object* src_obj,
207 unsigned int src_indx,
208 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
210 Section_id src_id(src_obj, src_indx);
211 this->access_from_map_[dst_off].insert(src_id);
214 // Add a reference to the code section specified by the .opd entry
217 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
219 size_t ndx = this->opd_ent_ndx(dst_off);
220 if (ndx >= this->opd_ent_.size())
221 this->opd_ent_.resize(ndx + 1);
222 this->opd_ent_[ndx].gc_mark = true;
226 process_gc_mark(Symbol_table* symtab)
228 for (size_t i = 0; i < this->opd_ent_.size(); i++)
229 if (this->opd_ent_[i].gc_mark)
231 unsigned int shndx = this->opd_ent_[i].shndx;
232 symtab->gc()->worklist().push(Section_id(this, shndx));
236 // Return offset in output GOT section that this object will use
237 // as a TOC pointer. Won't be just a constant with multi-toc support.
239 toc_base_offset() const
243 set_has_small_toc_reloc()
244 { has_small_toc_reloc_ = true; }
247 has_small_toc_reloc() const
248 { return has_small_toc_reloc_; }
251 set_has_14bit_branch(unsigned int shndx)
253 if (shndx >= this->has14_.size())
254 this->has14_.resize(shndx + 1);
255 this->has14_[shndx] = true;
259 has_14bit_branch(unsigned int shndx) const
260 { return shndx < this->has14_.size() && this->has14_[shndx]; }
263 set_stub_table(unsigned int shndx, Stub_table<size, big_endian>* stub_table)
265 if (shndx >= this->stub_table_.size())
266 this->stub_table_.resize(shndx + 1);
267 this->stub_table_[shndx] = stub_table;
270 Stub_table<size, big_endian>*
271 stub_table(unsigned int shndx)
273 if (shndx < this->stub_table_.size())
274 return this->stub_table_[shndx];
280 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
282 // Set ABI version for input and output
284 set_abiversion(int ver);
287 ppc64_local_entry_offset(const Symbol* sym) const
288 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
291 ppc64_local_entry_offset(unsigned int symndx) const
292 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
303 // Return index into opd_ent_ array for .opd entry at OFF.
304 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
305 // apart when the language doesn't use the last 8-byte word, the
306 // environment pointer. Thus dividing the entry section offset by
307 // 16 will give an index into opd_ent_ that works for either layout
308 // of .opd. (It leaves some elements of the vector unused when .opd
309 // entries are spaced 24 bytes apart, but we don't know the spacing
310 // until relocations are processed, and in any case it is possible
311 // for an object to have some entries spaced 16 bytes apart and
312 // others 24 bytes apart.)
314 opd_ent_ndx(size_t off) const
317 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
318 unsigned int special_;
320 // For 64-bit, whether this object uses small model relocs to access
322 bool has_small_toc_reloc_;
324 // Set at the start of gc_process_relocs, when we know opd_ent_
325 // vector is valid. The flag could be made atomic and set in
326 // do_read_relocs with memory_order_release and then tested with
327 // memory_order_acquire, potentially resulting in fewer entries in
331 // The first 8-byte word of an OPD entry gives the address of the
332 // entry point of the function. Relocatable object files have a
333 // relocation on this word. The following vector records the
334 // section and offset specified by these relocations.
335 std::vector<Opd_ent> opd_ent_;
337 // References made to this object's .opd section when running
338 // gc_process_relocs for another object, before the opd_ent_ vector
339 // is valid for this object.
340 Access_from access_from_map_;
342 // Whether input section has a 14-bit branch reloc.
343 std::vector<bool> has14_;
345 // The stub table to use for a given input section.
346 std::vector<Stub_table<size, big_endian>*> stub_table_;
349 elfcpp::Elf_Word e_flags_;
351 // ELF st_other field for local symbols.
352 std::vector<unsigned char> st_other_;
355 template<int size, bool big_endian>
356 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
359 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
361 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
362 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
363 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
364 opd_shndx_(0), opd_ent_(), e_flags_(ehdr.get_e_flags())
366 this->set_abiversion(0);
372 // Call Sized_dynobj::do_read_symbols to read the symbols then
373 // read .opd from a dynamic object, filling in opd_ent_ vector,
375 do_read_symbols(Read_symbols_data*);
377 // The .opd section shndx.
381 return this->opd_shndx_;
384 // The .opd section address.
388 return this->opd_address_;
391 // Init OPD entry arrays.
393 init_opd(size_t opd_size)
395 size_t count = this->opd_ent_ndx(opd_size);
396 this->opd_ent_.resize(count);
399 // Return section and offset of function entry for .opd + R_OFF.
401 get_opd_ent(Address r_off, Address* value = NULL) const
403 size_t ndx = this->opd_ent_ndx(r_off);
404 gold_assert(ndx < this->opd_ent_.size());
405 gold_assert(this->opd_ent_[ndx].shndx != 0);
407 *value = this->opd_ent_[ndx].off;
408 return this->opd_ent_[ndx].shndx;
411 // Set section and offset of function entry for .opd + R_OFF.
413 set_opd_ent(Address r_off, unsigned int shndx, Address value)
415 size_t ndx = this->opd_ent_ndx(r_off);
416 gold_assert(ndx < this->opd_ent_.size());
417 this->opd_ent_[ndx].shndx = shndx;
418 this->opd_ent_[ndx].off = value;
423 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
425 // Set ABI version for input and output.
427 set_abiversion(int ver);
430 // Used to specify extent of executable sections.
433 Sec_info(Address start_, Address len_, unsigned int shndx_)
434 : start(start_), len(len_), shndx(shndx_)
438 operator<(const Sec_info& that) const
439 { return this->start < that.start; }
452 // Return index into opd_ent_ array for .opd entry at OFF.
454 opd_ent_ndx(size_t off) const
457 // For 64-bit the .opd section shndx and address.
458 unsigned int opd_shndx_;
459 Address opd_address_;
461 // The first 8-byte word of an OPD entry gives the address of the
462 // entry point of the function. Records the section and offset
463 // corresponding to the address. Note that in dynamic objects,
464 // offset is *not* relative to the section.
465 std::vector<Opd_ent> opd_ent_;
468 elfcpp::Elf_Word e_flags_;
471 template<int size, bool big_endian>
472 class Target_powerpc : public Sized_target<size, big_endian>
476 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
477 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
478 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
479 static const Address invalid_address = static_cast<Address>(0) - 1;
480 // Offset of tp and dtp pointers from start of TLS block.
481 static const Address tp_offset = 0x7000;
482 static const Address dtp_offset = 0x8000;
485 : Sized_target<size, big_endian>(&powerpc_info),
486 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
487 glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
488 tlsld_got_offset_(-1U),
489 stub_tables_(), branch_lookup_table_(), branch_info_(),
490 plt_thread_safe_(false)
494 // Process the relocations to determine unreferenced sections for
495 // garbage collection.
497 gc_process_relocs(Symbol_table* symtab,
499 Sized_relobj_file<size, big_endian>* object,
500 unsigned int data_shndx,
501 unsigned int sh_type,
502 const unsigned char* prelocs,
504 Output_section* output_section,
505 bool needs_special_offset_handling,
506 size_t local_symbol_count,
507 const unsigned char* plocal_symbols);
509 // Scan the relocations to look for symbol adjustments.
511 scan_relocs(Symbol_table* symtab,
513 Sized_relobj_file<size, big_endian>* object,
514 unsigned int data_shndx,
515 unsigned int sh_type,
516 const unsigned char* prelocs,
518 Output_section* output_section,
519 bool needs_special_offset_handling,
520 size_t local_symbol_count,
521 const unsigned char* plocal_symbols);
523 // Map input .toc section to output .got section.
525 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
527 if (size == 64 && strcmp(name, ".toc") == 0)
535 // Provide linker defined save/restore functions.
537 define_save_restore_funcs(Layout*, Symbol_table*);
539 // No stubs unless a final link.
542 { return !parameters->options().relocatable(); }
545 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
548 do_plt_fde_location(const Output_data*, unsigned char*,
549 uint64_t*, off_t*) const;
551 // Stash info about branches, for stub generation.
553 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
554 unsigned int data_shndx, Address r_offset,
555 unsigned int r_type, unsigned int r_sym, Address addend)
557 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
558 this->branch_info_.push_back(info);
559 if (r_type == elfcpp::R_POWERPC_REL14
560 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
561 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
562 ppc_object->set_has_14bit_branch(data_shndx);
565 Stub_table<size, big_endian>*
569 do_define_standard_symbols(Symbol_table*, Layout*);
571 // Finalize the sections.
573 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
575 // Return the value to use for a dynamic which requires special
578 do_dynsym_value(const Symbol*) const;
580 // Return the PLT address to use for a local symbol.
582 do_plt_address_for_local(const Relobj*, unsigned int) const;
584 // Return the PLT address to use for a global symbol.
586 do_plt_address_for_global(const Symbol*) const;
588 // Return the offset to use for the GOT_INDX'th got entry which is
589 // for a local tls symbol specified by OBJECT, SYMNDX.
591 do_tls_offset_for_local(const Relobj* object,
593 unsigned int got_indx) const;
595 // Return the offset to use for the GOT_INDX'th got entry which is
596 // for global tls symbol GSYM.
598 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
601 do_function_location(Symbol_location*) const;
604 do_can_check_for_function_pointers() const
607 // Adjust -fsplit-stack code which calls non-split-stack code.
609 do_calls_non_split(Relobj* object, unsigned int shndx,
610 section_offset_type fnoffset, section_size_type fnsize,
611 unsigned char* view, section_size_type view_size,
612 std::string* from, std::string* to) const;
614 // Relocate a section.
616 relocate_section(const Relocate_info<size, big_endian>*,
617 unsigned int sh_type,
618 const unsigned char* prelocs,
620 Output_section* output_section,
621 bool needs_special_offset_handling,
623 Address view_address,
624 section_size_type view_size,
625 const Reloc_symbol_changes*);
627 // Scan the relocs during a relocatable link.
629 scan_relocatable_relocs(Symbol_table* symtab,
631 Sized_relobj_file<size, big_endian>* object,
632 unsigned int data_shndx,
633 unsigned int sh_type,
634 const unsigned char* prelocs,
636 Output_section* output_section,
637 bool needs_special_offset_handling,
638 size_t local_symbol_count,
639 const unsigned char* plocal_symbols,
640 Relocatable_relocs*);
642 // Emit relocations for a section.
644 relocate_relocs(const Relocate_info<size, big_endian>*,
645 unsigned int sh_type,
646 const unsigned char* prelocs,
648 Output_section* output_section,
649 typename elfcpp::Elf_types<size>::Elf_Off
650 offset_in_output_section,
651 const Relocatable_relocs*,
653 Address view_address,
655 unsigned char* reloc_view,
656 section_size_type reloc_view_size);
658 // Return whether SYM is defined by the ABI.
660 do_is_defined_by_abi(const Symbol* sym) const
662 return strcmp(sym->name(), "__tls_get_addr") == 0;
665 // Return the size of the GOT section.
669 gold_assert(this->got_ != NULL);
670 return this->got_->data_size();
673 // Get the PLT section.
674 const Output_data_plt_powerpc<size, big_endian>*
677 gold_assert(this->plt_ != NULL);
681 // Get the IPLT section.
682 const Output_data_plt_powerpc<size, big_endian>*
685 gold_assert(this->iplt_ != NULL);
689 // Get the .glink section.
690 const Output_data_glink<size, big_endian>*
691 glink_section() const
693 gold_assert(this->glink_ != NULL);
697 Output_data_glink<size, big_endian>*
700 gold_assert(this->glink_ != NULL);
704 bool has_glink() const
705 { return this->glink_ != NULL; }
707 // Get the GOT section.
708 const Output_data_got_powerpc<size, big_endian>*
711 gold_assert(this->got_ != NULL);
715 // Get the GOT section, creating it if necessary.
716 Output_data_got_powerpc<size, big_endian>*
717 got_section(Symbol_table*, Layout*);
720 do_make_elf_object(const std::string&, Input_file*, off_t,
721 const elfcpp::Ehdr<size, big_endian>&);
723 // Return the number of entries in the GOT.
725 got_entry_count() const
727 if (this->got_ == NULL)
729 return this->got_size() / (size / 8);
732 // Return the number of entries in the PLT.
734 plt_entry_count() const;
736 // Return the offset of the first non-reserved PLT entry.
738 first_plt_entry_offset() const
742 if (this->abiversion() >= 2)
747 // Return the size of each PLT entry.
749 plt_entry_size() const
753 if (this->abiversion() >= 2)
758 // Add any special sections for this symbol to the gc work list.
759 // For powerpc64, this adds the code section of a function
762 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
764 // Handle target specific gc actions when adding a gc reference from
765 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
766 // and DST_OFF. For powerpc64, this adds a referenc to the code
767 // section of a function descriptor.
769 do_gc_add_reference(Symbol_table* symtab,
771 unsigned int src_shndx,
773 unsigned int dst_shndx,
774 Address dst_off) const;
776 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
779 { return this->stub_tables_; }
781 const Output_data_brlt_powerpc<size, big_endian>*
783 { return this->brlt_section_; }
786 add_branch_lookup_table(Address to)
788 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
789 this->branch_lookup_table_.insert(std::make_pair(to, off));
793 find_branch_lookup_table(Address to)
795 typename Branch_lookup_table::const_iterator p
796 = this->branch_lookup_table_.find(to);
797 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
801 write_branch_lookup_table(unsigned char *oview)
803 for (typename Branch_lookup_table::const_iterator p
804 = this->branch_lookup_table_.begin();
805 p != this->branch_lookup_table_.end();
808 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
813 plt_thread_safe() const
814 { return this->plt_thread_safe_; }
818 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
821 set_abiversion (int ver)
823 elfcpp::Elf_Word flags = this->processor_specific_flags();
824 flags &= ~elfcpp::EF_PPC64_ABI;
825 flags |= ver & elfcpp::EF_PPC64_ABI;
826 this->set_processor_specific_flags(flags);
829 // Offset to to save stack slot
832 { return this->abiversion() < 2 ? 40 : 24; }
848 : tls_get_addr_(NOT_EXPECTED),
849 relinfo_(NULL), relnum_(0), r_offset_(0)
854 if (this->tls_get_addr_ != NOT_EXPECTED)
861 if (this->relinfo_ != NULL)
862 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
863 _("missing expected __tls_get_addr call"));
867 expect_tls_get_addr_call(
868 const Relocate_info<size, big_endian>* relinfo,
872 this->tls_get_addr_ = EXPECTED;
873 this->relinfo_ = relinfo;
874 this->relnum_ = relnum;
875 this->r_offset_ = r_offset;
879 expect_tls_get_addr_call()
880 { this->tls_get_addr_ = EXPECTED; }
883 skip_next_tls_get_addr_call()
884 {this->tls_get_addr_ = SKIP; }
887 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
889 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
890 || r_type == elfcpp::R_PPC_PLTREL24)
892 && strcmp(gsym->name(), "__tls_get_addr") == 0);
893 Tls_get_addr last_tls = this->tls_get_addr_;
894 this->tls_get_addr_ = NOT_EXPECTED;
895 if (is_tls_call && last_tls != EXPECTED)
897 else if (!is_tls_call && last_tls != NOT_EXPECTED)
906 // What we're up to regarding calls to __tls_get_addr.
907 // On powerpc, the branch and link insn making a call to
908 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
909 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
910 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
911 // The marker relocation always comes first, and has the same
912 // symbol as the reloc on the insn setting up the __tls_get_addr
913 // argument. This ties the arg setup insn with the call insn,
914 // allowing ld to safely optimize away the call. We check that
915 // every call to __tls_get_addr has a marker relocation, and that
916 // every marker relocation is on a call to __tls_get_addr.
917 Tls_get_addr tls_get_addr_;
918 // Info about the last reloc for error message.
919 const Relocate_info<size, big_endian>* relinfo_;
924 // The class which scans relocations.
925 class Scan : protected Track_tls
928 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
931 : Track_tls(), issued_non_pic_error_(false)
935 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
938 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
939 Sized_relobj_file<size, big_endian>* object,
940 unsigned int data_shndx,
941 Output_section* output_section,
942 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
943 const elfcpp::Sym<size, big_endian>& lsym,
947 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
948 Sized_relobj_file<size, big_endian>* object,
949 unsigned int data_shndx,
950 Output_section* output_section,
951 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
955 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
957 Sized_relobj_file<size, big_endian>* relobj,
960 const elfcpp::Rela<size, big_endian>& ,
962 const elfcpp::Sym<size, big_endian>&)
964 // PowerPC64 .opd is not folded, so any identical function text
965 // may be folded and we'll still keep function addresses distinct.
966 // That means no reloc is of concern here.
969 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
970 <Powerpc_relobj<size, big_endian>*>(relobj);
971 if (ppcobj->abiversion() == 1)
974 // For 32-bit and ELFv2, conservatively assume anything but calls to
975 // function code might be taking the address of the function.
976 return !is_branch_reloc(r_type);
980 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
982 Sized_relobj_file<size, big_endian>* relobj,
985 const elfcpp::Rela<size, big_endian>& ,
992 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
993 <Powerpc_relobj<size, big_endian>*>(relobj);
994 if (ppcobj->abiversion() == 1)
997 return !is_branch_reloc(r_type);
1001 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1002 Sized_relobj_file<size, big_endian>* object,
1003 unsigned int r_type, bool report_err);
1007 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1008 unsigned int r_type);
1011 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1012 unsigned int r_type, Symbol*);
1015 generate_tls_call(Symbol_table* symtab, Layout* layout,
1016 Target_powerpc* target);
1019 check_non_pic(Relobj*, unsigned int r_type);
1021 // Whether we have issued an error about a non-PIC compilation.
1022 bool issued_non_pic_error_;
1026 symval_for_branch(const Symbol_table* symtab,
1027 const Sized_symbol<size>* gsym,
1028 Powerpc_relobj<size, big_endian>* object,
1029 Address *value, unsigned int *dest_shndx);
1031 // The class which implements relocation.
1032 class Relocate : protected Track_tls
1035 // Use 'at' branch hints when true, 'y' when false.
1036 // FIXME maybe: set this with an option.
1037 static const bool is_isa_v2 = true;
1043 // Do a relocation. Return false if the caller should not issue
1044 // any warnings about this relocation.
1046 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
1047 Output_section*, size_t relnum,
1048 const elfcpp::Rela<size, big_endian>&,
1049 unsigned int r_type, const Sized_symbol<size>*,
1050 const Symbol_value<size>*,
1052 typename elfcpp::Elf_types<size>::Elf_Addr,
1056 class Relocate_comdat_behavior
1059 // Decide what the linker should do for relocations that refer to
1060 // discarded comdat sections.
1061 inline Comdat_behavior
1062 get(const char* name)
1064 gold::Default_comdat_behavior default_behavior;
1065 Comdat_behavior ret = default_behavior.get(name);
1066 if (ret == CB_WARNING)
1069 && (strcmp(name, ".fixup") == 0
1070 || strcmp(name, ".got2") == 0))
1073 && (strcmp(name, ".opd") == 0
1074 || strcmp(name, ".toc") == 0
1075 || strcmp(name, ".toc1") == 0))
1082 // A class which returns the size required for a relocation type,
1083 // used while scanning relocs during a relocatable link.
1084 class Relocatable_size_for_reloc
1088 get_size_for_reloc(unsigned int, Relobj*)
1095 // Optimize the TLS relocation type based on what we know about the
1096 // symbol. IS_FINAL is true if the final address of this symbol is
1097 // known at link time.
1099 tls::Tls_optimization
1100 optimize_tls_gd(bool is_final)
1102 // If we are generating a shared library, then we can't do anything
1104 if (parameters->options().shared())
1105 return tls::TLSOPT_NONE;
1108 return tls::TLSOPT_TO_IE;
1109 return tls::TLSOPT_TO_LE;
1112 tls::Tls_optimization
1115 if (parameters->options().shared())
1116 return tls::TLSOPT_NONE;
1118 return tls::TLSOPT_TO_LE;
1121 tls::Tls_optimization
1122 optimize_tls_ie(bool is_final)
1124 if (!is_final || parameters->options().shared())
1125 return tls::TLSOPT_NONE;
1127 return tls::TLSOPT_TO_LE;
1132 make_glink_section(Layout*);
1134 // Create the PLT section.
1136 make_plt_section(Symbol_table*, Layout*);
1139 make_iplt_section(Symbol_table*, Layout*);
1142 make_brlt_section(Layout*);
1144 // Create a PLT entry for a global symbol.
1146 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1148 // Create a PLT entry for a local IFUNC symbol.
1150 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1151 Sized_relobj_file<size, big_endian>*,
1155 // Create a GOT entry for local dynamic __tls_get_addr.
1157 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1158 Sized_relobj_file<size, big_endian>* object);
1161 tlsld_got_offset() const
1163 return this->tlsld_got_offset_;
1166 // Get the dynamic reloc section, creating it if necessary.
1168 rela_dyn_section(Layout*);
1170 // Similarly, but for ifunc symbols get the one for ifunc.
1172 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1174 // Copy a relocation against a global symbol.
1176 copy_reloc(Symbol_table* symtab, Layout* layout,
1177 Sized_relobj_file<size, big_endian>* object,
1178 unsigned int shndx, Output_section* output_section,
1179 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1181 this->copy_relocs_.copy_reloc(symtab, layout,
1182 symtab->get_sized_symbol<size>(sym),
1183 object, shndx, output_section,
1184 reloc, this->rela_dyn_section(layout));
1187 // Look over all the input sections, deciding where to place stubs.
1189 group_sections(Layout*, const Task*);
1191 // Sort output sections by address.
1192 struct Sort_sections
1195 operator()(const Output_section* sec1, const Output_section* sec2)
1196 { return sec1->address() < sec2->address(); }
1202 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1203 unsigned int data_shndx,
1205 unsigned int r_type,
1208 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1209 r_type_(r_type), r_sym_(r_sym), addend_(addend)
1215 // If this branch needs a plt call stub, or a long branch stub, make one.
1217 make_stub(Stub_table<size, big_endian>*,
1218 Stub_table<size, big_endian>*,
1219 Symbol_table*) const;
1222 // The branch location..
1223 Powerpc_relobj<size, big_endian>* object_;
1224 unsigned int shndx_;
1226 // ..and the branch type and destination.
1227 unsigned int r_type_;
1228 unsigned int r_sym_;
1232 // Information about this specific target which we pass to the
1233 // general Target structure.
1234 static Target::Target_info powerpc_info;
1236 // The types of GOT entries needed for this platform.
1237 // These values are exposed to the ABI in an incremental link.
1238 // Do not renumber existing values without changing the version
1239 // number of the .gnu_incremental_inputs section.
1243 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1244 GOT_TYPE_DTPREL, // entry for @got@dtprel
1245 GOT_TYPE_TPREL // entry for @got@tprel
1249 Output_data_got_powerpc<size, big_endian>* got_;
1250 // The PLT section. This is a container for a table of addresses,
1251 // and their relocations. Each address in the PLT has a dynamic
1252 // relocation (R_*_JMP_SLOT) and each address will have a
1253 // corresponding entry in .glink for lazy resolution of the PLT.
1254 // ppc32 initialises the PLT to point at the .glink entry, while
1255 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1256 // linker adds a stub that loads the PLT entry into ctr then
1257 // branches to ctr. There may be more than one stub for each PLT
1258 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1259 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1260 Output_data_plt_powerpc<size, big_endian>* plt_;
1261 // The IPLT section. Like plt_, this is a container for a table of
1262 // addresses and their relocations, specifically for STT_GNU_IFUNC
1263 // functions that resolve locally (STT_GNU_IFUNC functions that
1264 // don't resolve locally go in PLT). Unlike plt_, these have no
1265 // entry in .glink for lazy resolution, and the relocation section
1266 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1267 // the relocation section may contain relocations against
1268 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1269 // relocation section will appear at the end of other dynamic
1270 // relocations, so that ld.so applies these relocations after other
1271 // dynamic relocations. In a static executable, the relocation
1272 // section is emitted and marked with __rela_iplt_start and
1273 // __rela_iplt_end symbols.
1274 Output_data_plt_powerpc<size, big_endian>* iplt_;
1275 // Section holding long branch destinations.
1276 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1277 // The .glink section.
1278 Output_data_glink<size, big_endian>* glink_;
1279 // The dynamic reloc section.
1280 Reloc_section* rela_dyn_;
1281 // Relocs saved to avoid a COPY reloc.
1282 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1283 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1284 unsigned int tlsld_got_offset_;
1286 Stub_tables stub_tables_;
1287 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1288 Branch_lookup_table branch_lookup_table_;
1290 typedef std::vector<Branch_info> Branches;
1291 Branches branch_info_;
1293 bool plt_thread_safe_;
1297 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1300 true, // is_big_endian
1301 elfcpp::EM_PPC, // machine_code
1302 false, // has_make_symbol
1303 false, // has_resolve
1304 false, // has_code_fill
1305 true, // is_default_stack_executable
1306 false, // can_icf_inline_merge_sections
1308 "/usr/lib/ld.so.1", // dynamic_linker
1309 0x10000000, // default_text_segment_address
1310 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1311 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1312 false, // isolate_execinstr
1314 elfcpp::SHN_UNDEF, // small_common_shndx
1315 elfcpp::SHN_UNDEF, // large_common_shndx
1316 0, // small_common_section_flags
1317 0, // large_common_section_flags
1318 NULL, // attributes_section
1319 NULL, // attributes_vendor
1320 "_start" // entry_symbol_name
1324 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1327 false, // is_big_endian
1328 elfcpp::EM_PPC, // machine_code
1329 false, // has_make_symbol
1330 false, // has_resolve
1331 false, // has_code_fill
1332 true, // is_default_stack_executable
1333 false, // can_icf_inline_merge_sections
1335 "/usr/lib/ld.so.1", // dynamic_linker
1336 0x10000000, // default_text_segment_address
1337 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1338 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1339 false, // isolate_execinstr
1341 elfcpp::SHN_UNDEF, // small_common_shndx
1342 elfcpp::SHN_UNDEF, // large_common_shndx
1343 0, // small_common_section_flags
1344 0, // large_common_section_flags
1345 NULL, // attributes_section
1346 NULL, // attributes_vendor
1347 "_start" // entry_symbol_name
1351 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1354 true, // is_big_endian
1355 elfcpp::EM_PPC64, // machine_code
1356 false, // has_make_symbol
1357 false, // has_resolve
1358 false, // has_code_fill
1359 true, // is_default_stack_executable
1360 false, // can_icf_inline_merge_sections
1362 "/usr/lib/ld.so.1", // dynamic_linker
1363 0x10000000, // default_text_segment_address
1364 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1365 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1366 false, // isolate_execinstr
1368 elfcpp::SHN_UNDEF, // small_common_shndx
1369 elfcpp::SHN_UNDEF, // large_common_shndx
1370 0, // small_common_section_flags
1371 0, // large_common_section_flags
1372 NULL, // attributes_section
1373 NULL, // attributes_vendor
1374 "_start" // entry_symbol_name
1378 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1381 false, // is_big_endian
1382 elfcpp::EM_PPC64, // machine_code
1383 false, // has_make_symbol
1384 false, // has_resolve
1385 false, // has_code_fill
1386 true, // is_default_stack_executable
1387 false, // can_icf_inline_merge_sections
1389 "/usr/lib/ld.so.1", // dynamic_linker
1390 0x10000000, // default_text_segment_address
1391 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1392 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1393 false, // isolate_execinstr
1395 elfcpp::SHN_UNDEF, // small_common_shndx
1396 elfcpp::SHN_UNDEF, // large_common_shndx
1397 0, // small_common_section_flags
1398 0, // large_common_section_flags
1399 NULL, // attributes_section
1400 NULL, // attributes_vendor
1401 "_start" // entry_symbol_name
1405 is_branch_reloc(unsigned int r_type)
1407 return (r_type == elfcpp::R_POWERPC_REL24
1408 || r_type == elfcpp::R_PPC_PLTREL24
1409 || r_type == elfcpp::R_PPC_LOCAL24PC
1410 || r_type == elfcpp::R_POWERPC_REL14
1411 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1412 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1413 || r_type == elfcpp::R_POWERPC_ADDR24
1414 || r_type == elfcpp::R_POWERPC_ADDR14
1415 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1416 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1419 // If INSN is an opcode that may be used with an @tls operand, return
1420 // the transformed insn for TLS optimisation, otherwise return 0. If
1421 // REG is non-zero only match an insn with RB or RA equal to REG.
1423 at_tls_transform(uint32_t insn, unsigned int reg)
1425 if ((insn & (0x3f << 26)) != 31 << 26)
1429 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1430 rtra = insn & ((1 << 26) - (1 << 16));
1431 else if (((insn >> 16) & 0x1f) == reg)
1432 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1436 if ((insn & (0x3ff << 1)) == 266 << 1)
1439 else if ((insn & (0x1f << 1)) == 23 << 1
1440 && ((insn & (0x1f << 6)) < 14 << 6
1441 || ((insn & (0x1f << 6)) >= 16 << 6
1442 && (insn & (0x1f << 6)) < 24 << 6)))
1443 // load and store indexed -> dform
1444 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1445 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1446 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1447 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1448 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1450 insn = (58 << 26) | 2;
1458 template<int size, bool big_endian>
1459 class Powerpc_relocate_functions
1479 typedef Powerpc_relocate_functions<size, big_endian> This;
1480 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1482 template<int valsize>
1484 has_overflow_signed(Address value)
1486 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1487 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1488 limit <<= ((valsize - 1) >> 1);
1489 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1490 return value + limit > (limit << 1) - 1;
1493 template<int valsize>
1495 has_overflow_unsigned(Address value)
1497 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1498 limit <<= ((valsize - 1) >> 1);
1499 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1500 return value > (limit << 1) - 1;
1503 template<int valsize>
1505 has_overflow_bitfield(Address value)
1507 return (has_overflow_unsigned<valsize>(value)
1508 && has_overflow_signed<valsize>(value));
1511 template<int valsize>
1512 static inline Status
1513 overflowed(Address value, Overflow_check overflow)
1515 if (overflow == CHECK_SIGNED)
1517 if (has_overflow_signed<valsize>(value))
1518 return STATUS_OVERFLOW;
1520 else if (overflow == CHECK_UNSIGNED)
1522 if (has_overflow_unsigned<valsize>(value))
1523 return STATUS_OVERFLOW;
1525 else if (overflow == CHECK_BITFIELD)
1527 if (has_overflow_bitfield<valsize>(value))
1528 return STATUS_OVERFLOW;
1533 // Do a simple RELA relocation
1534 template<int fieldsize, int valsize>
1535 static inline Status
1536 rela(unsigned char* view, Address value, Overflow_check overflow)
1538 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1539 Valtype* wv = reinterpret_cast<Valtype*>(view);
1540 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1541 return overflowed<valsize>(value, overflow);
1544 template<int fieldsize, int valsize>
1545 static inline Status
1546 rela(unsigned char* view,
1547 unsigned int right_shift,
1548 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1550 Overflow_check overflow)
1552 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1553 Valtype* wv = reinterpret_cast<Valtype*>(view);
1554 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1555 Valtype reloc = value >> right_shift;
1558 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1559 return overflowed<valsize>(value >> right_shift, overflow);
1562 // Do a simple RELA relocation, unaligned.
1563 template<int fieldsize, int valsize>
1564 static inline Status
1565 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1567 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1568 return overflowed<valsize>(value, overflow);
1571 template<int fieldsize, int valsize>
1572 static inline Status
1573 rela_ua(unsigned char* view,
1574 unsigned int right_shift,
1575 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1577 Overflow_check overflow)
1579 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
1581 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
1582 Valtype reloc = value >> right_shift;
1585 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
1586 return overflowed<valsize>(value >> right_shift, overflow);
1590 // R_PPC64_ADDR64: (Symbol + Addend)
1592 addr64(unsigned char* view, Address value)
1593 { This::template rela<64,64>(view, value, CHECK_NONE); }
1595 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1597 addr64_u(unsigned char* view, Address value)
1598 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
1600 // R_POWERPC_ADDR32: (Symbol + Addend)
1601 static inline Status
1602 addr32(unsigned char* view, Address value, Overflow_check overflow)
1603 { return This::template rela<32,32>(view, value, overflow); }
1605 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1606 static inline Status
1607 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1608 { return This::template rela_ua<32,32>(view, value, overflow); }
1610 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1611 static inline Status
1612 addr24(unsigned char* view, Address value, Overflow_check overflow)
1614 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1616 if (overflow != CHECK_NONE && (value & 3) != 0)
1617 stat = STATUS_OVERFLOW;
1621 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1622 static inline Status
1623 addr16(unsigned char* view, Address value, Overflow_check overflow)
1624 { return This::template rela<16,16>(view, value, overflow); }
1626 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1627 static inline Status
1628 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1629 { return This::template rela_ua<16,16>(view, value, overflow); }
1631 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1632 static inline Status
1633 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1635 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
1636 if ((value & 3) != 0)
1637 stat = STATUS_OVERFLOW;
1641 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1643 addr16_hi(unsigned char* view, Address value)
1644 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
1646 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1648 addr16_ha(unsigned char* view, Address value)
1649 { This::addr16_hi(view, value + 0x8000); }
1651 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1653 addr16_hi2(unsigned char* view, Address value)
1654 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
1656 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1658 addr16_ha2(unsigned char* view, Address value)
1659 { This::addr16_hi2(view, value + 0x8000); }
1661 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1663 addr16_hi3(unsigned char* view, Address value)
1664 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
1666 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1668 addr16_ha3(unsigned char* view, Address value)
1669 { This::addr16_hi3(view, value + 0x8000); }
1671 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1672 static inline Status
1673 addr14(unsigned char* view, Address value, Overflow_check overflow)
1675 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
1676 if (overflow != CHECK_NONE && (value & 3) != 0)
1677 stat = STATUS_OVERFLOW;
1682 // Set ABI version for input and output.
1684 template<int size, bool big_endian>
1686 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1688 this->e_flags_ |= ver;
1689 if (this->abiversion() != 0)
1691 Target_powerpc<size, big_endian>* target =
1692 static_cast<Target_powerpc<size, big_endian>*>(
1693 parameters->sized_target<size, big_endian>());
1694 if (target->abiversion() == 0)
1695 target->set_abiversion(this->abiversion());
1696 else if (target->abiversion() != this->abiversion())
1697 gold_error(_("%s: ABI version %d is not compatible "
1698 "with ABI version %d output"),
1699 this->name().c_str(),
1700 this->abiversion(), target->abiversion());
1705 // Stash away the index of .got2 or .opd in a relocatable object, if
1706 // such a section exists.
1708 template<int size, bool big_endian>
1710 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1711 Read_symbols_data* sd)
1713 const unsigned char* const pshdrs = sd->section_headers->data();
1714 const unsigned char* namesu = sd->section_names->data();
1715 const char* names = reinterpret_cast<const char*>(namesu);
1716 section_size_type names_size = sd->section_names_size;
1717 const unsigned char* s;
1719 s = this->template find_shdr<size, big_endian>(pshdrs,
1720 size == 32 ? ".got2" : ".opd",
1721 names, names_size, NULL);
1724 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1725 this->special_ = ndx;
1728 if (this->abiversion() == 0)
1729 this->set_abiversion(1);
1730 else if (this->abiversion() > 1)
1731 gold_error(_("%s: .opd invalid in abiv%d"),
1732 this->name().c_str(), this->abiversion());
1735 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1738 // Examine .rela.opd to build info about function entry points.
1740 template<int size, bool big_endian>
1742 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1744 const unsigned char* prelocs,
1745 const unsigned char* plocal_syms)
1749 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1751 const int reloc_size
1752 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1753 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1754 Address expected_off = 0;
1755 bool regular = true;
1756 unsigned int opd_ent_size = 0;
1758 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1760 Reltype reloc(prelocs);
1761 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1762 = reloc.get_r_info();
1763 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1764 if (r_type == elfcpp::R_PPC64_ADDR64)
1766 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1767 typename elfcpp::Elf_types<size>::Elf_Addr value;
1770 if (r_sym < this->local_symbol_count())
1772 typename elfcpp::Sym<size, big_endian>
1773 lsym(plocal_syms + r_sym * sym_size);
1774 shndx = lsym.get_st_shndx();
1775 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1776 value = lsym.get_st_value();
1779 shndx = this->symbol_section_and_value(r_sym, &value,
1781 this->set_opd_ent(reloc.get_r_offset(), shndx,
1782 value + reloc.get_r_addend());
1785 expected_off = reloc.get_r_offset();
1786 opd_ent_size = expected_off;
1788 else if (expected_off != reloc.get_r_offset())
1790 expected_off += opd_ent_size;
1792 else if (r_type == elfcpp::R_PPC64_TOC)
1794 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1799 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1800 this->name().c_str(), r_type);
1804 if (reloc_count <= 2)
1805 opd_ent_size = this->section_size(this->opd_shndx());
1806 if (opd_ent_size != 24 && opd_ent_size != 16)
1810 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1811 this->name().c_str());
1817 template<int size, bool big_endian>
1819 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1821 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1824 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1825 p != rd->relocs.end();
1828 if (p->data_shndx == this->opd_shndx())
1830 uint64_t opd_size = this->section_size(this->opd_shndx());
1831 gold_assert(opd_size == static_cast<size_t>(opd_size));
1834 this->init_opd(opd_size);
1835 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1836 rd->local_symbols->data());
1844 // Read the symbols then set up st_other vector.
1846 template<int size, bool big_endian>
1848 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1850 this->base_read_symbols(sd);
1853 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1854 const unsigned char* const pshdrs = sd->section_headers->data();
1855 const unsigned int loccount = this->do_local_symbol_count();
1858 this->st_other_.resize(loccount);
1859 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1860 off_t locsize = loccount * sym_size;
1861 const unsigned int symtab_shndx = this->symtab_shndx();
1862 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
1863 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
1864 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
1865 locsize, true, false);
1867 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1869 elfcpp::Sym<size, big_endian> sym(psyms);
1870 unsigned char st_other = sym.get_st_other();
1871 this->st_other_[i] = st_other;
1872 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1874 if (this->abiversion() == 0)
1875 this->set_abiversion(2);
1876 else if (this->abiversion() < 2)
1877 gold_error(_("%s: local symbol %d has invalid st_other"
1878 " for ABI version 1"),
1879 this->name().c_str(), i);
1886 template<int size, bool big_endian>
1888 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
1890 this->e_flags_ |= ver;
1891 if (this->abiversion() != 0)
1893 Target_powerpc<size, big_endian>* target =
1894 static_cast<Target_powerpc<size, big_endian>*>(
1895 parameters->sized_target<size, big_endian>());
1896 if (target->abiversion() == 0)
1897 target->set_abiversion(this->abiversion());
1898 else if (target->abiversion() != this->abiversion())
1899 gold_error(_("%s: ABI version %d is not compatible "
1900 "with ABI version %d output"),
1901 this->name().c_str(),
1902 this->abiversion(), target->abiversion());
1907 // Call Sized_dynobj::base_read_symbols to read the symbols then
1908 // read .opd from a dynamic object, filling in opd_ent_ vector,
1910 template<int size, bool big_endian>
1912 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1914 this->base_read_symbols(sd);
1917 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1918 const unsigned char* const pshdrs = sd->section_headers->data();
1919 const unsigned char* namesu = sd->section_names->data();
1920 const char* names = reinterpret_cast<const char*>(namesu);
1921 const unsigned char* s = NULL;
1922 const unsigned char* opd;
1923 section_size_type opd_size;
1925 // Find and read .opd section.
1928 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
1929 sd->section_names_size,
1934 typename elfcpp::Shdr<size, big_endian> shdr(s);
1935 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1936 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1938 if (this->abiversion() == 0)
1939 this->set_abiversion(1);
1940 else if (this->abiversion() > 1)
1941 gold_error(_("%s: .opd invalid in abiv%d"),
1942 this->name().c_str(), this->abiversion());
1944 this->opd_shndx_ = (s - pshdrs) / shdr_size;
1945 this->opd_address_ = shdr.get_sh_addr();
1946 opd_size = convert_to_section_size_type(shdr.get_sh_size());
1947 opd = this->get_view(shdr.get_sh_offset(), opd_size,
1953 // Build set of executable sections.
1954 // Using a set is probably overkill. There is likely to be only
1955 // a few executable sections, typically .init, .text and .fini,
1956 // and they are generally grouped together.
1957 typedef std::set<Sec_info> Exec_sections;
1958 Exec_sections exec_sections;
1960 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
1962 typename elfcpp::Shdr<size, big_endian> shdr(s);
1963 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1964 && ((shdr.get_sh_flags()
1965 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1966 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1967 && shdr.get_sh_size() != 0)
1969 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
1970 shdr.get_sh_size(), i));
1973 if (exec_sections.empty())
1976 // Look over the OPD entries. This is complicated by the fact
1977 // that some binaries will use two-word entries while others
1978 // will use the standard three-word entries. In most cases
1979 // the third word (the environment pointer for languages like
1980 // Pascal) is unused and will be zero. If the third word is
1981 // used it should not be pointing into executable sections,
1983 this->init_opd(opd_size);
1984 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
1986 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1987 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
1988 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
1990 // Chances are that this is the third word of an OPD entry.
1992 typename Exec_sections::const_iterator e
1993 = exec_sections.upper_bound(Sec_info(val, 0, 0));
1994 if (e != exec_sections.begin())
1997 if (e->start <= val && val < e->start + e->len)
1999 // We have an address in an executable section.
2000 // VAL ought to be the function entry, set it up.
2001 this->set_opd_ent(p - opd, e->shndx, val);
2002 // Skip second word of OPD entry, the TOC pointer.
2006 // If we didn't match any executable sections, we likely
2007 // have a non-zero third word in the OPD entry.
2012 // Set up some symbols.
2014 template<int size, bool big_endian>
2016 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2017 Symbol_table* symtab,
2022 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2023 // undefined when scanning relocs (and thus requires
2024 // non-relative dynamic relocs). The proper value will be
2026 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2027 if (gotsym != NULL && gotsym->is_undefined())
2029 Target_powerpc<size, big_endian>* target =
2030 static_cast<Target_powerpc<size, big_endian>*>(
2031 parameters->sized_target<size, big_endian>());
2032 Output_data_got_powerpc<size, big_endian>* got
2033 = target->got_section(symtab, layout);
2034 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2035 Symbol_table::PREDEFINED,
2039 elfcpp::STV_HIDDEN, 0,
2043 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2044 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2045 if (sdasym != NULL && sdasym->is_undefined())
2047 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2049 = layout->add_output_section_data(".sdata", 0,
2051 | elfcpp::SHF_WRITE,
2052 sdata, ORDER_SMALL_DATA, false);
2053 symtab->define_in_output_data("_SDA_BASE_", NULL,
2054 Symbol_table::PREDEFINED,
2055 os, 32768, 0, elfcpp::STT_OBJECT,
2056 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2062 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2063 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2064 if (gotsym != NULL && gotsym->is_undefined())
2066 Target_powerpc<size, big_endian>* target =
2067 static_cast<Target_powerpc<size, big_endian>*>(
2068 parameters->sized_target<size, big_endian>());
2069 Output_data_got_powerpc<size, big_endian>* got
2070 = target->got_section(symtab, layout);
2071 symtab->define_in_output_data(".TOC.", NULL,
2072 Symbol_table::PREDEFINED,
2076 elfcpp::STV_HIDDEN, 0,
2082 // Set up PowerPC target specific relobj.
2084 template<int size, bool big_endian>
2086 Target_powerpc<size, big_endian>::do_make_elf_object(
2087 const std::string& name,
2088 Input_file* input_file,
2089 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2091 int et = ehdr.get_e_type();
2092 // ET_EXEC files are valid input for --just-symbols/-R,
2093 // and we treat them as relocatable objects.
2094 if (et == elfcpp::ET_REL
2095 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2097 Powerpc_relobj<size, big_endian>* obj =
2098 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2102 else if (et == elfcpp::ET_DYN)
2104 Powerpc_dynobj<size, big_endian>* obj =
2105 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2111 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2116 template<int size, bool big_endian>
2117 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2120 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2121 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2123 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2124 : Output_data_got<size, big_endian>(),
2125 symtab_(symtab), layout_(layout),
2126 header_ent_cnt_(size == 32 ? 3 : 1),
2127 header_index_(size == 32 ? 0x2000 : 0)
2130 // Override all the Output_data_got methods we use so as to first call
2133 add_global(Symbol* gsym, unsigned int got_type)
2135 this->reserve_ent();
2136 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2140 add_global_plt(Symbol* gsym, unsigned int got_type)
2142 this->reserve_ent();
2143 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2147 add_global_tls(Symbol* gsym, unsigned int got_type)
2148 { return this->add_global_plt(gsym, got_type); }
2151 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2152 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2154 this->reserve_ent();
2155 Output_data_got<size, big_endian>::
2156 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2160 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2161 Output_data_reloc_generic* rel_dyn,
2162 unsigned int r_type_1, unsigned int r_type_2)
2164 this->reserve_ent(2);
2165 Output_data_got<size, big_endian>::
2166 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2170 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2172 this->reserve_ent();
2173 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2178 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2180 this->reserve_ent();
2181 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2186 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2187 { return this->add_local_plt(object, sym_index, got_type); }
2190 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2191 unsigned int got_type,
2192 Output_data_reloc_generic* rel_dyn,
2193 unsigned int r_type)
2195 this->reserve_ent(2);
2196 Output_data_got<size, big_endian>::
2197 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2201 add_constant(Valtype constant)
2203 this->reserve_ent();
2204 return Output_data_got<size, big_endian>::add_constant(constant);
2208 add_constant_pair(Valtype c1, Valtype c2)
2210 this->reserve_ent(2);
2211 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2214 // Offset of _GLOBAL_OFFSET_TABLE_.
2218 return this->got_offset(this->header_index_);
2221 // Offset of base used to access the GOT/TOC.
2222 // The got/toc pointer reg will be set to this value.
2224 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2227 return this->g_o_t();
2229 return (this->output_section()->address()
2230 + object->toc_base_offset()
2234 // Ensure our GOT has a header.
2236 set_final_data_size()
2238 if (this->header_ent_cnt_ != 0)
2239 this->make_header();
2240 Output_data_got<size, big_endian>::set_final_data_size();
2243 // First word of GOT header needs some values that are not
2244 // handled by Output_data_got so poke them in here.
2245 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2247 do_write(Output_file* of)
2250 if (size == 32 && this->layout_->dynamic_data() != NULL)
2251 val = this->layout_->dynamic_section()->address();
2253 val = this->output_section()->address() + 0x8000;
2254 this->replace_constant(this->header_index_, val);
2255 Output_data_got<size, big_endian>::do_write(of);
2260 reserve_ent(unsigned int cnt = 1)
2262 if (this->header_ent_cnt_ == 0)
2264 if (this->num_entries() + cnt > this->header_index_)
2265 this->make_header();
2271 this->header_ent_cnt_ = 0;
2272 this->header_index_ = this->num_entries();
2275 Output_data_got<size, big_endian>::add_constant(0);
2276 Output_data_got<size, big_endian>::add_constant(0);
2277 Output_data_got<size, big_endian>::add_constant(0);
2279 // Define _GLOBAL_OFFSET_TABLE_ at the header
2280 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2283 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2284 sym->set_value(this->g_o_t());
2287 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2288 Symbol_table::PREDEFINED,
2289 this, this->g_o_t(), 0,
2292 elfcpp::STV_HIDDEN, 0,
2296 Output_data_got<size, big_endian>::add_constant(0);
2299 // Stashed pointers.
2300 Symbol_table* symtab_;
2304 unsigned int header_ent_cnt_;
2305 // GOT header index.
2306 unsigned int header_index_;
2309 // Get the GOT section, creating it if necessary.
2311 template<int size, bool big_endian>
2312 Output_data_got_powerpc<size, big_endian>*
2313 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2316 if (this->got_ == NULL)
2318 gold_assert(symtab != NULL && layout != NULL);
2321 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2323 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2324 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2325 this->got_, ORDER_DATA, false);
2331 // Get the dynamic reloc section, creating it if necessary.
2333 template<int size, bool big_endian>
2334 typename Target_powerpc<size, big_endian>::Reloc_section*
2335 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2337 if (this->rela_dyn_ == NULL)
2339 gold_assert(layout != NULL);
2340 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2341 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2342 elfcpp::SHF_ALLOC, this->rela_dyn_,
2343 ORDER_DYNAMIC_RELOCS, false);
2345 return this->rela_dyn_;
2348 // Similarly, but for ifunc symbols get the one for ifunc.
2350 template<int size, bool big_endian>
2351 typename Target_powerpc<size, big_endian>::Reloc_section*
2352 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2357 return this->rela_dyn_section(layout);
2359 if (this->iplt_ == NULL)
2360 this->make_iplt_section(symtab, layout);
2361 return this->iplt_->rel_plt();
2367 // Determine the stub group size. The group size is the absolute
2368 // value of the parameter --stub-group-size. If --stub-group-size
2369 // is passed a negative value, we restrict stubs to be always before
2370 // the stubbed branches.
2371 Stub_control(int32_t size)
2372 : state_(NO_GROUP), stub_group_size_(abs(size)),
2373 stub14_group_size_(abs(size) >> 10),
2374 stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
2375 group_end_addr_(0), owner_(NULL), output_section_(NULL)
2377 if (stub_group_size_ == 1)
2380 if (stubs_always_before_branch_)
2382 stub_group_size_ = 0x1e00000;
2383 stub14_group_size_ = 0x7800;
2387 stub_group_size_ = 0x1c00000;
2388 stub14_group_size_ = 0x7000;
2390 suppress_size_errors_ = true;
2394 // Return true iff input section can be handled by current stub
2397 can_add_to_stub_group(Output_section* o,
2398 const Output_section::Input_section* i,
2401 const Output_section::Input_section*
2407 { return output_section_; }
2410 set_output_and_owner(Output_section* o,
2411 const Output_section::Input_section* i)
2413 this->output_section_ = o;
2421 FINDING_STUB_SECTION,
2426 uint32_t stub_group_size_;
2427 uint32_t stub14_group_size_;
2428 bool stubs_always_before_branch_;
2429 bool suppress_size_errors_;
2430 uint64_t group_end_addr_;
2431 const Output_section::Input_section* owner_;
2432 Output_section* output_section_;
2435 // Return true iff input section can be handled by current stub
2439 Stub_control::can_add_to_stub_group(Output_section* o,
2440 const Output_section::Input_section* i,
2444 = has14 ? this->stub14_group_size_ : this->stub_group_size_;
2445 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2447 uint64_t start_addr = o->address();
2450 // .init and .fini sections are pasted together to form a single
2451 // function. We can't be adding stubs in the middle of the function.
2452 this_size = o->data_size();
2455 start_addr += i->relobj()->output_section_offset(i->shndx());
2456 this_size = i->data_size();
2458 uint64_t end_addr = start_addr + this_size;
2459 bool toobig = this_size > group_size;
2461 if (toobig && !this->suppress_size_errors_)
2462 gold_warning(_("%s:%s exceeds group size"),
2463 i->relobj()->name().c_str(),
2464 i->relobj()->section_name(i->shndx()).c_str());
2466 if (this->state_ != HAS_STUB_SECTION
2467 && (!whole_sec || this->output_section_ != o)
2468 && (this->state_ == NO_GROUP
2469 || this->group_end_addr_ - end_addr < group_size))
2472 this->output_section_ = o;
2475 if (this->state_ == NO_GROUP)
2477 this->state_ = FINDING_STUB_SECTION;
2478 this->group_end_addr_ = end_addr;
2480 else if (this->group_end_addr_ - start_addr < group_size)
2482 // Adding this section would make the group larger than GROUP_SIZE.
2483 else if (this->state_ == FINDING_STUB_SECTION
2484 && !this->stubs_always_before_branch_
2487 // But wait, there's more! Input sections up to GROUP_SIZE
2488 // bytes before the stub table can be handled by it too.
2489 this->state_ = HAS_STUB_SECTION;
2490 this->group_end_addr_ = end_addr;
2494 this->state_ = NO_GROUP;
2500 // Look over all the input sections, deciding where to place stubs.
2502 template<int size, bool big_endian>
2504 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2507 Stub_control stub_control(parameters->options().stub_group_size());
2509 // Group input sections and insert stub table
2510 Stub_table<size, big_endian>* stub_table = NULL;
2511 Layout::Section_list section_list;
2512 layout->get_executable_sections(§ion_list);
2513 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2514 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2515 o != section_list.rend();
2518 typedef Output_section::Input_section_list Input_section_list;
2519 for (Input_section_list::const_reverse_iterator i
2520 = (*o)->input_sections().rbegin();
2521 i != (*o)->input_sections().rend();
2524 if (i->is_input_section())
2526 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2527 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2528 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2529 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2531 stub_table->init(stub_control.owner(),
2532 stub_control.output_section());
2533 stub_control.set_output_and_owner(*o, &*i);
2536 if (stub_table == NULL)
2537 stub_table = this->new_stub_table();
2538 ppcobj->set_stub_table(i->shndx(), stub_table);
2542 if (stub_table != NULL)
2544 const Output_section::Input_section* i = stub_control.owner();
2545 if (!i->is_input_section())
2547 // Corner case. A new stub group was made for the first
2548 // section (last one looked at here) for some reason, but
2549 // the first section is already being used as the owner for
2550 // a stub table for following sections. Force it into that
2552 gold_assert(this->stub_tables_.size() >= 2);
2553 this->stub_tables_.pop_back();
2555 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2556 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2557 ppcobj->set_stub_table(i->shndx(), this->stub_tables_.back());
2560 stub_table->init(i, stub_control.output_section());
2564 // If this branch needs a plt call stub, or a long branch stub, make one.
2566 template<int size, bool big_endian>
2568 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2569 Stub_table<size, big_endian>* stub_table,
2570 Stub_table<size, big_endian>* ifunc_stub_table,
2571 Symbol_table* symtab) const
2573 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2574 if (sym != NULL && sym->is_forwarder())
2575 sym = symtab->resolve_forwards(sym);
2576 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2577 Target_powerpc<size, big_endian>* target =
2578 static_cast<Target_powerpc<size, big_endian>*>(
2579 parameters->sized_target<size, big_endian>());
2581 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
2582 : this->object_->local_has_plt_offset(this->r_sym_))
2586 && target->abiversion() >= 2
2587 && !parameters->options().output_is_position_independent()
2588 && !is_branch_reloc(this->r_type_))
2589 target->glink_section()->add_global_entry(gsym);
2592 if (stub_table == NULL)
2593 stub_table = this->object_->stub_table(this->shndx_);
2594 if (stub_table == NULL)
2596 // This is a ref from a data section to an ifunc symbol.
2597 stub_table = ifunc_stub_table;
2599 gold_assert(stub_table != NULL);
2601 stub_table->add_plt_call_entry(this->object_, gsym,
2602 this->r_type_, this->addend_);
2604 stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2605 this->r_type_, this->addend_);
2610 Address max_branch_offset;
2611 if (this->r_type_ == elfcpp::R_POWERPC_REL14
2612 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2613 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2614 max_branch_offset = 1 << 15;
2615 else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2616 || this->r_type_ == elfcpp::R_PPC_PLTREL24
2617 || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2618 max_branch_offset = 1 << 25;
2621 Address from = this->object_->get_output_section_offset(this->shndx_);
2622 gold_assert(from != invalid_address);
2623 from += (this->object_->output_section(this->shndx_)->address()
2628 switch (gsym->source())
2630 case Symbol::FROM_OBJECT:
2632 Object* symobj = gsym->object();
2633 if (symobj->is_dynamic()
2634 || symobj->pluginobj() != NULL)
2637 unsigned int shndx = gsym->shndx(&is_ordinary);
2638 if (shndx == elfcpp::SHN_UNDEF)
2643 case Symbol::IS_UNDEFINED:
2649 Symbol_table::Compute_final_value_status status;
2650 to = symtab->compute_final_value<size>(gsym, &status);
2651 if (status != Symbol_table::CFVS_OK)
2654 to += this->object_->ppc64_local_entry_offset(gsym);
2658 const Symbol_value<size>* psymval
2659 = this->object_->local_symbol(this->r_sym_);
2660 Symbol_value<size> symval;
2661 typedef Sized_relobj_file<size, big_endian> ObjType;
2662 typename ObjType::Compute_final_local_value_status status
2663 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2665 if (status != ObjType::CFLV_OK
2666 || !symval.has_output_value())
2668 to = symval.value(this->object_, 0);
2670 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
2672 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
2673 to += this->addend_;
2674 if (stub_table == NULL)
2675 stub_table = this->object_->stub_table(this->shndx_);
2676 if (size == 64 && target->abiversion() < 2)
2678 unsigned int dest_shndx;
2679 if (!target->symval_for_branch(symtab, gsym, this->object_,
2683 Address delta = to - from;
2684 if (delta + max_branch_offset >= 2 * max_branch_offset)
2686 if (stub_table == NULL)
2688 gold_warning(_("%s:%s: branch in non-executable section,"
2689 " no long branch stub for you"),
2690 this->object_->name().c_str(),
2691 this->object_->section_name(this->shndx_).c_str());
2694 stub_table->add_long_branch_entry(this->object_, to);
2699 // Relaxation hook. This is where we do stub generation.
2701 template<int size, bool big_endian>
2703 Target_powerpc<size, big_endian>::do_relax(int pass,
2704 const Input_objects*,
2705 Symbol_table* symtab,
2709 unsigned int prev_brlt_size = 0;
2713 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
2715 && this->abiversion() < 2
2717 && !parameters->options().user_set_plt_thread_safe())
2719 static const char* const thread_starter[] =
2723 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2725 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2726 "mq_notify", "create_timer",
2731 "GOMP_parallel_start",
2732 "GOMP_parallel_loop_static",
2733 "GOMP_parallel_loop_static_start",
2734 "GOMP_parallel_loop_dynamic",
2735 "GOMP_parallel_loop_dynamic_start",
2736 "GOMP_parallel_loop_guided",
2737 "GOMP_parallel_loop_guided_start",
2738 "GOMP_parallel_loop_runtime",
2739 "GOMP_parallel_loop_runtime_start",
2740 "GOMP_parallel_sections",
2741 "GOMP_parallel_sections_start",
2746 if (parameters->options().shared())
2750 for (unsigned int i = 0;
2751 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2754 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2755 thread_safe = (sym != NULL
2757 && sym->in_real_elf());
2763 this->plt_thread_safe_ = thread_safe;
2764 this->group_sections(layout, task);
2767 // We need address of stub tables valid for make_stub.
2768 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2769 p != this->stub_tables_.end();
2772 const Powerpc_relobj<size, big_endian>* object
2773 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2774 Address off = object->get_output_section_offset((*p)->shndx());
2775 gold_assert(off != invalid_address);
2776 Output_section* os = (*p)->output_section();
2777 (*p)->set_address_and_size(os, off);
2782 // Clear plt call stubs, long branch stubs and branch lookup table.
2783 prev_brlt_size = this->branch_lookup_table_.size();
2784 this->branch_lookup_table_.clear();
2785 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2786 p != this->stub_tables_.end();
2789 (*p)->clear_stubs();
2793 // Build all the stubs.
2794 Stub_table<size, big_endian>* ifunc_stub_table
2795 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2796 Stub_table<size, big_endian>* one_stub_table
2797 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2798 for (typename Branches::const_iterator b = this->branch_info_.begin();
2799 b != this->branch_info_.end();
2802 b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2805 // Did anything change size?
2806 unsigned int num_huge_branches = this->branch_lookup_table_.size();
2807 bool again = num_huge_branches != prev_brlt_size;
2808 if (size == 64 && num_huge_branches != 0)
2809 this->make_brlt_section(layout);
2810 if (size == 64 && again)
2811 this->brlt_section_->set_current_size(num_huge_branches);
2813 typedef Unordered_set<Output_section*> Output_sections;
2814 Output_sections os_need_update;
2815 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2816 p != this->stub_tables_.end();
2819 if ((*p)->size_update())
2822 (*p)->add_eh_frame(layout);
2823 os_need_update.insert((*p)->output_section());
2827 // Set output section offsets for all input sections in an output
2828 // section that just changed size. Anything past the stubs will
2830 for (typename Output_sections::iterator p = os_need_update.begin();
2831 p != os_need_update.end();
2834 Output_section* os = *p;
2836 typedef Output_section::Input_section_list Input_section_list;
2837 for (Input_section_list::const_iterator i = os->input_sections().begin();
2838 i != os->input_sections().end();
2841 off = align_address(off, i->addralign());
2842 if (i->is_input_section() || i->is_relaxed_input_section())
2843 i->relobj()->set_section_offset(i->shndx(), off);
2844 if (i->is_relaxed_input_section())
2846 Stub_table<size, big_endian>* stub_table
2847 = static_cast<Stub_table<size, big_endian>*>(
2848 i->relaxed_input_section());
2849 off += stub_table->set_address_and_size(os, off);
2852 off += i->data_size();
2854 // If .branch_lt is part of this output section, then we have
2855 // just done the offset adjustment.
2856 os->clear_section_offsets_need_adjustment();
2861 && num_huge_branches != 0
2862 && parameters->options().output_is_position_independent())
2864 // Fill in the BRLT relocs.
2865 this->brlt_section_->reset_brlt_sizes();
2866 for (typename Branch_lookup_table::const_iterator p
2867 = this->branch_lookup_table_.begin();
2868 p != this->branch_lookup_table_.end();
2871 this->brlt_section_->add_reloc(p->first, p->second);
2873 this->brlt_section_->finalize_brlt_sizes();
2878 template<int size, bool big_endian>
2880 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2881 unsigned char* oview,
2885 uint64_t address = plt->address();
2886 off_t len = plt->data_size();
2888 if (plt == this->glink_)
2890 // See Output_data_glink::do_write() for glink contents.
2893 gold_assert(parameters->doing_static_link());
2894 // Static linking may need stubs, to support ifunc and long
2895 // branches. We need to create an output section for
2896 // .eh_frame early in the link process, to have a place to
2897 // attach stub .eh_frame info. We also need to have
2898 // registered a CIE that matches the stub CIE. Both of
2899 // these requirements are satisfied by creating an FDE and
2900 // CIE for .glink, even though static linking will leave
2901 // .glink zero length.
2902 // ??? Hopefully generating an FDE with a zero address range
2903 // won't confuse anything that consumes .eh_frame info.
2905 else if (size == 64)
2907 // There is one word before __glink_PLTresolve
2911 else if (parameters->options().output_is_position_independent())
2913 // There are two FDEs for a position independent glink.
2914 // The first covers the branch table, the second
2915 // __glink_PLTresolve at the end of glink.
2916 off_t resolve_size = this->glink_->pltresolve_size;
2917 if (oview[9] == elfcpp::DW_CFA_nop)
2918 len -= resolve_size;
2921 address += len - resolve_size;
2928 // Must be a stub table.
2929 const Stub_table<size, big_endian>* stub_table
2930 = static_cast<const Stub_table<size, big_endian>*>(plt);
2931 uint64_t stub_address = stub_table->stub_address();
2932 len -= stub_address - address;
2933 address = stub_address;
2936 *paddress = address;
2940 // A class to handle the PLT data.
2942 template<int size, bool big_endian>
2943 class Output_data_plt_powerpc : public Output_section_data_build
2946 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2947 size, big_endian> Reloc_section;
2949 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2950 Reloc_section* plt_rel,
2952 : Output_section_data_build(size == 32 ? 4 : 8),
2958 // Add an entry to the PLT.
2963 add_ifunc_entry(Symbol*);
2966 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2968 // Return the .rela.plt section data.
2975 // Return the number of PLT entries.
2979 if (this->current_data_size() == 0)
2981 return ((this->current_data_size() - this->first_plt_entry_offset())
2982 / this->plt_entry_size());
2987 do_adjust_output_section(Output_section* os)
2992 // Write to a map file.
2994 do_print_to_mapfile(Mapfile* mapfile) const
2995 { mapfile->print_output_data(this, this->name_); }
2998 // Return the offset of the first non-reserved PLT entry.
3000 first_plt_entry_offset() const
3002 // IPLT has no reserved entry.
3003 if (this->name_[3] == 'I')
3005 return this->targ_->first_plt_entry_offset();
3008 // Return the size of each PLT entry.
3010 plt_entry_size() const
3012 return this->targ_->plt_entry_size();
3015 // Write out the PLT data.
3017 do_write(Output_file*);
3019 // The reloc section.
3020 Reloc_section* rel_;
3021 // Allows access to .glink for do_write.
3022 Target_powerpc<size, big_endian>* targ_;
3023 // What to report in map file.
3027 // Add an entry to the PLT.
3029 template<int size, bool big_endian>
3031 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3033 if (!gsym->has_plt_offset())
3035 section_size_type off = this->current_data_size();
3037 off += this->first_plt_entry_offset();
3038 gsym->set_plt_offset(off);
3039 gsym->set_needs_dynsym_entry();
3040 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3041 this->rel_->add_global(gsym, dynrel, this, off, 0);
3042 off += this->plt_entry_size();
3043 this->set_current_data_size(off);
3047 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3049 template<int size, bool big_endian>
3051 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3053 if (!gsym->has_plt_offset())
3055 section_size_type off = this->current_data_size();
3056 gsym->set_plt_offset(off);
3057 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3058 if (size == 64 && this->targ_->abiversion() < 2)
3059 dynrel = elfcpp::R_PPC64_JMP_IREL;
3060 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3061 off += this->plt_entry_size();
3062 this->set_current_data_size(off);
3066 // Add an entry for a local ifunc symbol to the IPLT.
3068 template<int size, bool big_endian>
3070 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3071 Sized_relobj_file<size, big_endian>* relobj,
3072 unsigned int local_sym_index)
3074 if (!relobj->local_has_plt_offset(local_sym_index))
3076 section_size_type off = this->current_data_size();
3077 relobj->set_local_plt_offset(local_sym_index, off);
3078 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3079 if (size == 64 && this->targ_->abiversion() < 2)
3080 dynrel = elfcpp::R_PPC64_JMP_IREL;
3081 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3083 off += this->plt_entry_size();
3084 this->set_current_data_size(off);
3088 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3089 static const uint32_t add_2_2_11 = 0x7c425a14;
3090 static const uint32_t add_3_3_2 = 0x7c631214;
3091 static const uint32_t add_3_3_13 = 0x7c636a14;
3092 static const uint32_t add_11_0_11 = 0x7d605a14;
3093 static const uint32_t add_11_2_11 = 0x7d625a14;
3094 static const uint32_t add_11_11_2 = 0x7d6b1214;
3095 static const uint32_t addi_0_12 = 0x380c0000;
3096 static const uint32_t addi_2_2 = 0x38420000;
3097 static const uint32_t addi_3_3 = 0x38630000;
3098 static const uint32_t addi_11_11 = 0x396b0000;
3099 static const uint32_t addi_12_1 = 0x39810000;
3100 static const uint32_t addi_12_12 = 0x398c0000;
3101 static const uint32_t addis_0_2 = 0x3c020000;
3102 static const uint32_t addis_0_13 = 0x3c0d0000;
3103 static const uint32_t addis_2_12 = 0x3c4c0000;
3104 static const uint32_t addis_11_2 = 0x3d620000;
3105 static const uint32_t addis_11_11 = 0x3d6b0000;
3106 static const uint32_t addis_11_30 = 0x3d7e0000;
3107 static const uint32_t addis_12_1 = 0x3d810000;
3108 static const uint32_t addis_12_2 = 0x3d820000;
3109 static const uint32_t addis_12_12 = 0x3d8c0000;
3110 static const uint32_t b = 0x48000000;
3111 static const uint32_t bcl_20_31 = 0x429f0005;
3112 static const uint32_t bctr = 0x4e800420;
3113 static const uint32_t blr = 0x4e800020;
3114 static const uint32_t bnectr_p4 = 0x4ce20420;
3115 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
3116 static const uint32_t cmpldi_2_0 = 0x28220000;
3117 static const uint32_t cror_15_15_15 = 0x4def7b82;
3118 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3119 static const uint32_t ld_0_1 = 0xe8010000;
3120 static const uint32_t ld_0_12 = 0xe80c0000;
3121 static const uint32_t ld_2_1 = 0xe8410000;
3122 static const uint32_t ld_2_2 = 0xe8420000;
3123 static const uint32_t ld_2_11 = 0xe84b0000;
3124 static const uint32_t ld_11_2 = 0xe9620000;
3125 static const uint32_t ld_11_11 = 0xe96b0000;
3126 static const uint32_t ld_12_2 = 0xe9820000;
3127 static const uint32_t ld_12_11 = 0xe98b0000;
3128 static const uint32_t ld_12_12 = 0xe98c0000;
3129 static const uint32_t lfd_0_1 = 0xc8010000;
3130 static const uint32_t li_0_0 = 0x38000000;
3131 static const uint32_t li_12_0 = 0x39800000;
3132 static const uint32_t lis_0 = 0x3c000000;
3133 static const uint32_t lis_11 = 0x3d600000;
3134 static const uint32_t lis_12 = 0x3d800000;
3135 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3136 static const uint32_t lwz_0_12 = 0x800c0000;
3137 static const uint32_t lwz_11_11 = 0x816b0000;
3138 static const uint32_t lwz_11_30 = 0x817e0000;
3139 static const uint32_t lwz_12_12 = 0x818c0000;
3140 static const uint32_t lwzu_0_12 = 0x840c0000;
3141 static const uint32_t mflr_0 = 0x7c0802a6;
3142 static const uint32_t mflr_11 = 0x7d6802a6;
3143 static const uint32_t mflr_12 = 0x7d8802a6;
3144 static const uint32_t mtctr_0 = 0x7c0903a6;
3145 static const uint32_t mtctr_11 = 0x7d6903a6;
3146 static const uint32_t mtctr_12 = 0x7d8903a6;
3147 static const uint32_t mtlr_0 = 0x7c0803a6;
3148 static const uint32_t mtlr_12 = 0x7d8803a6;
3149 static const uint32_t nop = 0x60000000;
3150 static const uint32_t ori_0_0_0 = 0x60000000;
3151 static const uint32_t srdi_0_0_2 = 0x7800f082;
3152 static const uint32_t std_0_1 = 0xf8010000;
3153 static const uint32_t std_0_12 = 0xf80c0000;
3154 static const uint32_t std_2_1 = 0xf8410000;
3155 static const uint32_t stfd_0_1 = 0xd8010000;
3156 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3157 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3158 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3159 static const uint32_t xor_2_12_12 = 0x7d826278;
3160 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3162 // Write out the PLT.
3164 template<int size, bool big_endian>
3166 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3168 if (size == 32 && this->name_[3] != 'I')
3170 const section_size_type offset = this->offset();
3171 const section_size_type oview_size
3172 = convert_to_section_size_type(this->data_size());
3173 unsigned char* const oview = of->get_output_view(offset, oview_size);
3174 unsigned char* pov = oview;
3175 unsigned char* endpov = oview + oview_size;
3177 // The address of the .glink branch table
3178 const Output_data_glink<size, big_endian>* glink
3179 = this->targ_->glink_section();
3180 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3182 while (pov < endpov)
3184 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3189 of->write_output_view(offset, oview_size, oview);
3193 // Create the PLT section.
3195 template<int size, bool big_endian>
3197 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3200 if (this->plt_ == NULL)
3202 if (this->got_ == NULL)
3203 this->got_section(symtab, layout);
3205 if (this->glink_ == NULL)
3206 make_glink_section(layout);
3208 // Ensure that .rela.dyn always appears before .rela.plt This is
3209 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3210 // needs to include .rela.plt in its range.
3211 this->rela_dyn_section(layout);
3213 Reloc_section* plt_rel = new Reloc_section(false);
3214 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3215 elfcpp::SHF_ALLOC, plt_rel,
3216 ORDER_DYNAMIC_PLT_RELOCS, false);
3218 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3220 layout->add_output_section_data(".plt",
3222 ? elfcpp::SHT_PROGBITS
3223 : elfcpp::SHT_NOBITS),
3224 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3233 // Create the IPLT section.
3235 template<int size, bool big_endian>
3237 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3240 if (this->iplt_ == NULL)
3242 this->make_plt_section(symtab, layout);
3244 Reloc_section* iplt_rel = new Reloc_section(false);
3245 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3247 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3249 this->plt_->output_section()->add_output_section_data(this->iplt_);
3253 // A section for huge long branch addresses, similar to plt section.
3255 template<int size, bool big_endian>
3256 class Output_data_brlt_powerpc : public Output_section_data_build
3259 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3260 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3261 size, big_endian> Reloc_section;
3263 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3264 Reloc_section* brlt_rel)
3265 : Output_section_data_build(size == 32 ? 4 : 8),
3273 this->reset_data_size();
3274 this->rel_->reset_data_size();
3278 finalize_brlt_sizes()
3280 this->finalize_data_size();
3281 this->rel_->finalize_data_size();
3284 // Add a reloc for an entry in the BRLT.
3286 add_reloc(Address to, unsigned int off)
3287 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3289 // Update section and reloc section size.
3291 set_current_size(unsigned int num_branches)
3293 this->reset_address_and_file_offset();
3294 this->set_current_data_size(num_branches * 16);
3295 this->finalize_data_size();
3296 Output_section* os = this->output_section();
3297 os->set_section_offsets_need_adjustment();
3298 if (this->rel_ != NULL)
3300 unsigned int reloc_size
3301 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3302 this->rel_->reset_address_and_file_offset();
3303 this->rel_->set_current_data_size(num_branches * reloc_size);
3304 this->rel_->finalize_data_size();
3305 Output_section* os = this->rel_->output_section();
3306 os->set_section_offsets_need_adjustment();
3312 do_adjust_output_section(Output_section* os)
3317 // Write to a map file.
3319 do_print_to_mapfile(Mapfile* mapfile) const
3320 { mapfile->print_output_data(this, "** BRLT"); }
3323 // Write out the BRLT data.
3325 do_write(Output_file*);
3327 // The reloc section.
3328 Reloc_section* rel_;
3329 Target_powerpc<size, big_endian>* targ_;
3332 // Make the branch lookup table section.
3334 template<int size, bool big_endian>
3336 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3338 if (size == 64 && this->brlt_section_ == NULL)
3340 Reloc_section* brlt_rel = NULL;
3341 bool is_pic = parameters->options().output_is_position_independent();
3344 // When PIC we can't fill in .branch_lt (like .plt it can be
3345 // a bss style section) but must initialise at runtime via
3346 // dynamic relocats.
3347 this->rela_dyn_section(layout);
3348 brlt_rel = new Reloc_section(false);
3349 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3352 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3353 if (this->plt_ && is_pic)
3354 this->plt_->output_section()
3355 ->add_output_section_data(this->brlt_section_);
3357 layout->add_output_section_data(".branch_lt",
3358 (is_pic ? elfcpp::SHT_NOBITS
3359 : elfcpp::SHT_PROGBITS),
3360 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3361 this->brlt_section_,
3362 (is_pic ? ORDER_SMALL_BSS
3363 : ORDER_SMALL_DATA),
3368 // Write out .branch_lt when non-PIC.
3370 template<int size, bool big_endian>
3372 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3374 if (size == 64 && !parameters->options().output_is_position_independent())
3376 const section_size_type offset = this->offset();
3377 const section_size_type oview_size
3378 = convert_to_section_size_type(this->data_size());
3379 unsigned char* const oview = of->get_output_view(offset, oview_size);
3381 this->targ_->write_branch_lookup_table(oview);
3382 of->write_output_view(offset, oview_size, oview);
3386 static inline uint32_t
3392 static inline uint32_t
3398 static inline uint32_t
3401 return hi(a + 0x8000);
3407 static const unsigned char eh_frame_cie[12];
3411 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3414 'z', 'R', 0, // Augmentation string.
3415 4, // Code alignment.
3416 0x80 - size / 8 , // Data alignment.
3418 1, // Augmentation size.
3419 (elfcpp::DW_EH_PE_pcrel
3420 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3421 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3424 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3425 static const unsigned char glink_eh_frame_fde_64v1[] =
3427 0, 0, 0, 0, // Replaced with offset to .glink.
3428 0, 0, 0, 0, // Replaced with size of .glink.
3429 0, // Augmentation size.
3430 elfcpp::DW_CFA_advance_loc + 1,
3431 elfcpp::DW_CFA_register, 65, 12,
3432 elfcpp::DW_CFA_advance_loc + 4,
3433 elfcpp::DW_CFA_restore_extended, 65
3436 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3437 static const unsigned char glink_eh_frame_fde_64v2[] =
3439 0, 0, 0, 0, // Replaced with offset to .glink.
3440 0, 0, 0, 0, // Replaced with size of .glink.
3441 0, // Augmentation size.
3442 elfcpp::DW_CFA_advance_loc + 1,
3443 elfcpp::DW_CFA_register, 65, 0,
3444 elfcpp::DW_CFA_advance_loc + 4,
3445 elfcpp::DW_CFA_restore_extended, 65
3448 // Describe __glink_PLTresolve use of LR, 32-bit version.
3449 static const unsigned char glink_eh_frame_fde_32[] =
3451 0, 0, 0, 0, // Replaced with offset to .glink.
3452 0, 0, 0, 0, // Replaced with size of .glink.
3453 0, // Augmentation size.
3454 elfcpp::DW_CFA_advance_loc + 2,
3455 elfcpp::DW_CFA_register, 65, 0,
3456 elfcpp::DW_CFA_advance_loc + 4,
3457 elfcpp::DW_CFA_restore_extended, 65
3460 static const unsigned char default_fde[] =
3462 0, 0, 0, 0, // Replaced with offset to stubs.
3463 0, 0, 0, 0, // Replaced with size of stubs.
3464 0, // Augmentation size.
3465 elfcpp::DW_CFA_nop, // Pad.
3470 template<bool big_endian>
3472 write_insn(unsigned char* p, uint32_t v)
3474 elfcpp::Swap<32, big_endian>::writeval(p, v);
3477 // Stub_table holds information about plt and long branch stubs.
3478 // Stubs are built in an area following some input section determined
3479 // by group_sections(). This input section is converted to a relaxed
3480 // input section allowing it to be resized to accommodate the stubs
3482 template<int size, bool big_endian>
3483 class Stub_table : public Output_relaxed_input_section
3486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3487 static const Address invalid_address = static_cast<Address>(0) - 1;
3489 Stub_table(Target_powerpc<size, big_endian>* targ)
3490 : Output_relaxed_input_section(NULL, 0, 0),
3491 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3492 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3493 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3496 // Delayed Output_relaxed_input_section init.
3498 init(const Output_section::Input_section*, Output_section*);
3500 // Add a plt call stub.
3502 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3508 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3513 // Find a given plt call stub.
3515 find_plt_call_entry(const Symbol*) const;
3518 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3519 unsigned int) const;
3522 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3528 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3533 // Add a long branch stub.
3535 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3538 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3544 this->plt_call_stubs_.clear();
3545 this->plt_size_ = 0;
3546 this->long_branch_stubs_.clear();
3547 this->branch_size_ = 0;
3551 set_address_and_size(const Output_section* os, Address off)
3553 Address start_off = off;
3554 off += this->orig_data_size_;
3555 Address my_size = this->plt_size_ + this->branch_size_;
3557 off = align_address(off, this->stub_align());
3558 // Include original section size and alignment padding in size
3559 my_size += off - start_off;
3560 this->reset_address_and_file_offset();
3561 this->set_current_data_size(my_size);
3562 this->set_address_and_file_offset(os->address() + start_off,
3563 os->offset() + start_off);
3568 stub_address() const
3570 return align_address(this->address() + this->orig_data_size_,
3571 this->stub_align());
3577 return align_address(this->offset() + this->orig_data_size_,
3578 this->stub_align());
3583 { return this->plt_size_; }
3588 Output_section* os = this->output_section();
3589 if (os->addralign() < this->stub_align())
3591 os->set_addralign(this->stub_align());
3592 // FIXME: get rid of the insane checkpointing.
3593 // We can't increase alignment of the input section to which
3594 // stubs are attached; The input section may be .init which
3595 // is pasted together with other .init sections to form a
3596 // function. Aligning might insert zero padding resulting in
3597 // sigill. However we do need to increase alignment of the
3598 // output section so that the align_address() on offset in
3599 // set_address_and_size() adds the same padding as the
3600 // align_address() on address in stub_address().
3601 // What's more, we need this alignment for the layout done in
3602 // relaxation_loop_body() so that the output section starts at
3603 // a suitably aligned address.
3604 os->checkpoint_set_addralign(this->stub_align());
3606 if (this->last_plt_size_ != this->plt_size_
3607 || this->last_branch_size_ != this->branch_size_)
3609 this->last_plt_size_ = this->plt_size_;
3610 this->last_branch_size_ = this->branch_size_;
3616 // Add .eh_frame info for this stub section. Unlike other linker
3617 // generated .eh_frame this is added late in the link, because we
3618 // only want the .eh_frame info if this particular stub section is
3621 add_eh_frame(Layout* layout)
3623 if (!this->eh_frame_added_)
3625 if (!parameters->options().ld_generated_unwind_info())
3628 // Since we add stub .eh_frame info late, it must be placed
3629 // after all other linker generated .eh_frame info so that
3630 // merge mapping need not be updated for input sections.
3631 // There is no provision to use a different CIE to that used
3633 if (!this->targ_->has_glink())
3636 layout->add_eh_frame_for_plt(this,
3637 Eh_cie<size>::eh_frame_cie,
3638 sizeof (Eh_cie<size>::eh_frame_cie),
3640 sizeof (default_fde));
3641 this->eh_frame_added_ = true;
3645 Target_powerpc<size, big_endian>*
3651 class Plt_stub_ent_hash;
3652 typedef Unordered_map<Plt_stub_ent, unsigned int,
3653 Plt_stub_ent_hash> Plt_stub_entries;
3655 // Alignment of stub section.
3661 unsigned int min_align = 32;
3662 unsigned int user_align = 1 << parameters->options().plt_align();
3663 return std::max(user_align, min_align);
3666 // Return the plt offset for the given call stub.
3668 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3670 const Symbol* gsym = p->first.sym_;
3673 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3674 && gsym->can_use_relative_reloc(false));
3675 return gsym->plt_offset();
3680 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3681 unsigned int local_sym_index = p->first.locsym_;
3682 return relobj->local_plt_offset(local_sym_index);
3686 // Size of a given plt call stub.
3688 plt_call_size(typename Plt_stub_entries::const_iterator p) const
3694 Address plt_addr = this->plt_off(p, &is_iplt);
3696 plt_addr += this->targ_->iplt_section()->address();
3698 plt_addr += this->targ_->plt_section()->address();
3699 Address got_addr = this->targ_->got_section()->output_section()->address();
3700 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3701 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3702 got_addr += ppcobj->toc_base_offset();
3703 Address off = plt_addr - got_addr;
3704 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
3705 if (this->targ_->abiversion() < 2)
3707 bool static_chain = parameters->options().plt_static_chain();
3708 bool thread_safe = this->targ_->plt_thread_safe();
3712 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3714 unsigned int align = 1 << parameters->options().plt_align();
3716 bytes = (bytes + align - 1) & -align;
3720 // Return long branch stub size.
3722 branch_stub_size(Address to)
3725 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3726 if (to - loc + (1 << 25) < 2 << 25)
3728 if (size == 64 || !parameters->options().output_is_position_independent())
3735 do_write(Output_file*);
3737 // Plt call stub keys.
3741 Plt_stub_ent(const Symbol* sym)
3742 : sym_(sym), object_(0), addend_(0), locsym_(0)
3745 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3746 unsigned int locsym_index)
3747 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3750 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3752 unsigned int r_type,
3754 : sym_(sym), object_(0), addend_(0), locsym_(0)
3757 this->addend_ = addend;
3758 else if (parameters->options().output_is_position_independent()
3759 && r_type == elfcpp::R_PPC_PLTREL24)
3761 this->addend_ = addend;
3762 if (this->addend_ >= 32768)
3763 this->object_ = object;
3767 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3768 unsigned int locsym_index,
3769 unsigned int r_type,
3771 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3774 this->addend_ = addend;
3775 else if (parameters->options().output_is_position_independent()
3776 && r_type == elfcpp::R_PPC_PLTREL24)
3777 this->addend_ = addend;
3780 bool operator==(const Plt_stub_ent& that) const
3782 return (this->sym_ == that.sym_
3783 && this->object_ == that.object_
3784 && this->addend_ == that.addend_
3785 && this->locsym_ == that.locsym_);
3789 const Sized_relobj_file<size, big_endian>* object_;
3790 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3791 unsigned int locsym_;
3794 class Plt_stub_ent_hash
3797 size_t operator()(const Plt_stub_ent& ent) const
3799 return (reinterpret_cast<uintptr_t>(ent.sym_)
3800 ^ reinterpret_cast<uintptr_t>(ent.object_)
3806 // Long branch stub keys.
3807 class Branch_stub_ent
3810 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3811 : dest_(to), toc_base_off_(0)
3814 toc_base_off_ = obj->toc_base_offset();
3817 bool operator==(const Branch_stub_ent& that) const
3819 return (this->dest_ == that.dest_
3821 || this->toc_base_off_ == that.toc_base_off_));
3825 unsigned int toc_base_off_;
3828 class Branch_stub_ent_hash
3831 size_t operator()(const Branch_stub_ent& ent) const
3832 { return ent.dest_ ^ ent.toc_base_off_; }
3835 // In a sane world this would be a global.
3836 Target_powerpc<size, big_endian>* targ_;
3837 // Map sym/object/addend to stub offset.
3838 Plt_stub_entries plt_call_stubs_;
3839 // Map destination address to stub offset.
3840 typedef Unordered_map<Branch_stub_ent, unsigned int,
3841 Branch_stub_ent_hash> Branch_stub_entries;
3842 Branch_stub_entries long_branch_stubs_;
3843 // size of input section
3844 section_size_type orig_data_size_;
3846 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3847 // Whether .eh_frame info has been created for this stub section.
3848 bool eh_frame_added_;
3851 // Make a new stub table, and record.
3853 template<int size, bool big_endian>
3854 Stub_table<size, big_endian>*
3855 Target_powerpc<size, big_endian>::new_stub_table()
3857 Stub_table<size, big_endian>* stub_table
3858 = new Stub_table<size, big_endian>(this);
3859 this->stub_tables_.push_back(stub_table);
3863 // Delayed stub table initialisation, because we create the stub table
3864 // before we know to which section it will be attached.
3866 template<int size, bool big_endian>
3868 Stub_table<size, big_endian>::init(
3869 const Output_section::Input_section* owner,
3870 Output_section* output_section)
3872 this->set_relobj(owner->relobj());
3873 this->set_shndx(owner->shndx());
3874 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3875 this->set_output_section(output_section);
3876 this->orig_data_size_ = owner->current_data_size();
3878 std::vector<Output_relaxed_input_section*> new_relaxed;
3879 new_relaxed.push_back(this);
3880 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3883 // Add a plt call stub, if we do not already have one for this
3884 // sym/object/addend combo.
3886 template<int size, bool big_endian>
3888 Stub_table<size, big_endian>::add_plt_call_entry(
3889 const Sized_relobj_file<size, big_endian>* object,
3891 unsigned int r_type,
3894 Plt_stub_ent ent(object, gsym, r_type, addend);
3895 unsigned int off = this->plt_size_;
3896 std::pair<typename Plt_stub_entries::iterator, bool> p
3897 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3899 this->plt_size_ = off + this->plt_call_size(p.first);
3902 template<int size, bool big_endian>
3904 Stub_table<size, big_endian>::add_plt_call_entry(
3905 const Sized_relobj_file<size, big_endian>* object,
3906 unsigned int locsym_index,
3907 unsigned int r_type,
3910 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3911 unsigned int off = this->plt_size_;
3912 std::pair<typename Plt_stub_entries::iterator, bool> p
3913 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3915 this->plt_size_ = off + this->plt_call_size(p.first);
3918 // Find a plt call stub.
3920 template<int size, bool big_endian>
3921 typename Stub_table<size, big_endian>::Address
3922 Stub_table<size, big_endian>::find_plt_call_entry(
3923 const Sized_relobj_file<size, big_endian>* object,
3925 unsigned int r_type,
3926 Address addend) const
3928 Plt_stub_ent ent(object, gsym, r_type, addend);
3929 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3930 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3933 template<int size, bool big_endian>
3934 typename Stub_table<size, big_endian>::Address
3935 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3937 Plt_stub_ent ent(gsym);
3938 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3939 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3942 template<int size, bool big_endian>
3943 typename Stub_table<size, big_endian>::Address
3944 Stub_table<size, big_endian>::find_plt_call_entry(
3945 const Sized_relobj_file<size, big_endian>* object,
3946 unsigned int locsym_index,
3947 unsigned int r_type,
3948 Address addend) const
3950 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3951 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3952 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3955 template<int size, bool big_endian>
3956 typename Stub_table<size, big_endian>::Address
3957 Stub_table<size, big_endian>::find_plt_call_entry(
3958 const Sized_relobj_file<size, big_endian>* object,
3959 unsigned int locsym_index) const
3961 Plt_stub_ent ent(object, locsym_index);
3962 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3963 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3966 // Add a long branch stub if we don't already have one to given
3969 template<int size, bool big_endian>
3971 Stub_table<size, big_endian>::add_long_branch_entry(
3972 const Powerpc_relobj<size, big_endian>* object,
3975 Branch_stub_ent ent(object, to);
3976 Address off = this->branch_size_;
3977 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3979 unsigned int stub_size = this->branch_stub_size(to);
3980 this->branch_size_ = off + stub_size;
3981 if (size == 64 && stub_size != 4)
3982 this->targ_->add_branch_lookup_table(to);
3986 // Find long branch stub.
3988 template<int size, bool big_endian>
3989 typename Stub_table<size, big_endian>::Address
3990 Stub_table<size, big_endian>::find_long_branch_entry(
3991 const Powerpc_relobj<size, big_endian>* object,
3994 Branch_stub_ent ent(object, to);
3995 typename Branch_stub_entries::const_iterator p
3996 = this->long_branch_stubs_.find(ent);
3997 return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
4000 // A class to handle .glink.
4002 template<int size, bool big_endian>
4003 class Output_data_glink : public Output_section_data
4006 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4007 static const Address invalid_address = static_cast<Address>(0) - 1;
4008 static const int pltresolve_size = 16*4;
4010 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4011 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4012 end_branch_table_(), ge_size_(0)
4016 add_eh_frame(Layout* layout);
4019 add_global_entry(const Symbol*);
4022 find_global_entry(const Symbol*) const;
4025 global_entry_address() const
4027 gold_assert(this->is_data_size_valid());
4028 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4029 return this->address() + global_entry_off;
4033 // Write to a map file.
4035 do_print_to_mapfile(Mapfile* mapfile) const
4036 { mapfile->print_output_data(this, _("** glink")); }
4040 set_final_data_size();
4044 do_write(Output_file*);
4046 // Allows access to .got and .plt for do_write.
4047 Target_powerpc<size, big_endian>* targ_;
4049 // Map sym to stub offset.
4050 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4051 Global_entry_stub_entries global_entry_stubs_;
4053 unsigned int end_branch_table_, ge_size_;
4056 template<int size, bool big_endian>
4058 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4060 if (!parameters->options().ld_generated_unwind_info())
4065 if (this->targ_->abiversion() < 2)
4066 layout->add_eh_frame_for_plt(this,
4067 Eh_cie<64>::eh_frame_cie,
4068 sizeof (Eh_cie<64>::eh_frame_cie),
4069 glink_eh_frame_fde_64v1,
4070 sizeof (glink_eh_frame_fde_64v1));
4072 layout->add_eh_frame_for_plt(this,
4073 Eh_cie<64>::eh_frame_cie,
4074 sizeof (Eh_cie<64>::eh_frame_cie),
4075 glink_eh_frame_fde_64v2,
4076 sizeof (glink_eh_frame_fde_64v2));
4080 // 32-bit .glink can use the default since the CIE return
4081 // address reg, LR, is valid.
4082 layout->add_eh_frame_for_plt(this,
4083 Eh_cie<32>::eh_frame_cie,
4084 sizeof (Eh_cie<32>::eh_frame_cie),
4086 sizeof (default_fde));
4087 // Except where LR is used in a PIC __glink_PLTresolve.
4088 if (parameters->options().output_is_position_independent())
4089 layout->add_eh_frame_for_plt(this,
4090 Eh_cie<32>::eh_frame_cie,
4091 sizeof (Eh_cie<32>::eh_frame_cie),
4092 glink_eh_frame_fde_32,
4093 sizeof (glink_eh_frame_fde_32));
4097 template<int size, bool big_endian>
4099 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4101 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4102 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4104 this->ge_size_ += 16;
4107 template<int size, bool big_endian>
4108 typename Output_data_glink<size, big_endian>::Address
4109 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4111 typename Global_entry_stub_entries::const_iterator p
4112 = this->global_entry_stubs_.find(gsym);
4113 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4116 template<int size, bool big_endian>
4118 Output_data_glink<size, big_endian>::set_final_data_size()
4120 unsigned int count = this->targ_->plt_entry_count();
4121 section_size_type total = 0;
4127 // space for branch table
4128 total += 4 * (count - 1);
4130 total += -total & 15;
4131 total += this->pltresolve_size;
4135 total += this->pltresolve_size;
4137 // space for branch table
4139 if (this->targ_->abiversion() < 2)
4143 total += 4 * (count - 0x8000);
4147 this->end_branch_table_ = total;
4148 total = (total + 15) & -16;
4149 total += this->ge_size_;
4151 this->set_data_size(total);
4154 // Write out plt and long branch stub code.
4156 template<int size, bool big_endian>
4158 Stub_table<size, big_endian>::do_write(Output_file* of)
4160 if (this->plt_call_stubs_.empty()
4161 && this->long_branch_stubs_.empty())
4164 const section_size_type start_off = this->offset();
4165 const section_size_type off = this->stub_offset();
4166 const section_size_type oview_size =
4167 convert_to_section_size_type(this->data_size() - (off - start_off));
4168 unsigned char* const oview = of->get_output_view(off, oview_size);
4173 const Output_data_got_powerpc<size, big_endian>* got
4174 = this->targ_->got_section();
4175 Address got_os_addr = got->output_section()->address();
4177 if (!this->plt_call_stubs_.empty())
4179 // The base address of the .plt section.
4180 Address plt_base = this->targ_->plt_section()->address();
4181 Address iplt_base = invalid_address;
4183 // Write out plt call stubs.
4184 typename Plt_stub_entries::const_iterator cs;
4185 for (cs = this->plt_call_stubs_.begin();
4186 cs != this->plt_call_stubs_.end();
4190 Address pltoff = this->plt_off(cs, &is_iplt);
4191 Address plt_addr = pltoff;
4194 if (iplt_base == invalid_address)
4195 iplt_base = this->targ_->iplt_section()->address();
4196 plt_addr += iplt_base;
4199 plt_addr += plt_base;
4200 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4201 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4202 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4203 Address off = plt_addr - got_addr;
4205 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4206 gold_error(_("%s: linkage table error against `%s'"),
4207 cs->first.object_->name().c_str(),
4208 cs->first.sym_->demangled_name().c_str());
4210 bool plt_load_toc = this->targ_->abiversion() < 2;
4212 = plt_load_toc && parameters->options().plt_static_chain();
4214 = plt_load_toc && this->targ_->plt_thread_safe();
4215 bool use_fake_dep = false;
4216 Address cmp_branch_off = 0;
4219 unsigned int pltindex
4220 = ((pltoff - this->targ_->first_plt_entry_offset())
4221 / this->targ_->plt_entry_size());
4223 = (this->targ_->glink_section()->pltresolve_size
4225 if (pltindex > 32768)
4226 glinkoff += (pltindex - 32768) * 4;
4228 = this->targ_->glink_section()->address() + glinkoff;
4230 = (this->stub_address() + cs->second + 24
4231 + 4 * (ha(off) != 0)
4232 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4233 + 4 * static_chain);
4234 cmp_branch_off = to - from;
4235 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4238 p = oview + cs->second;
4241 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4245 write_insn<big_endian>(p, addis_11_2 + ha(off));
4247 write_insn<big_endian>(p, ld_12_11 + l(off));
4252 write_insn<big_endian>(p, addis_12_2 + ha(off));
4254 write_insn<big_endian>(p, ld_12_12 + l(off));
4258 && ha(off + 8 + 8 * static_chain) != ha(off))
4260 write_insn<big_endian>(p, addi_11_11 + l(off));
4264 write_insn<big_endian>(p, mtctr_12);
4270 write_insn<big_endian>(p, xor_2_12_12);
4272 write_insn<big_endian>(p, add_11_11_2);
4275 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4279 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4286 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4288 write_insn<big_endian>(p, ld_12_2 + l(off));
4291 && ha(off + 8 + 8 * static_chain) != ha(off))
4293 write_insn<big_endian>(p, addi_2_2 + l(off));
4297 write_insn<big_endian>(p, mtctr_12);
4303 write_insn<big_endian>(p, xor_11_12_12);
4305 write_insn<big_endian>(p, add_2_2_11);
4310 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4313 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4317 if (thread_safe && !use_fake_dep)
4319 write_insn<big_endian>(p, cmpldi_2_0);
4321 write_insn<big_endian>(p, bnectr_p4);
4323 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4326 write_insn<big_endian>(p, bctr);
4330 // Write out long branch stubs.
4331 typename Branch_stub_entries::const_iterator bs;
4332 for (bs = this->long_branch_stubs_.begin();
4333 bs != this->long_branch_stubs_.end();
4336 p = oview + this->plt_size_ + bs->second;
4337 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4338 Address delta = bs->first.dest_ - loc;
4339 if (delta + (1 << 25) < 2 << 25)
4340 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4344 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4345 gold_assert(brlt_addr != invalid_address);
4346 brlt_addr += this->targ_->brlt_section()->address();
4347 Address got_addr = got_os_addr + bs->first.toc_base_off_;
4348 Address brltoff = brlt_addr - got_addr;
4349 if (ha(brltoff) == 0)
4351 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
4355 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
4356 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
4358 write_insn<big_endian>(p, mtctr_12), p += 4;
4359 write_insn<big_endian>(p, bctr);
4365 if (!this->plt_call_stubs_.empty())
4367 // The base address of the .plt section.
4368 Address plt_base = this->targ_->plt_section()->address();
4369 Address iplt_base = invalid_address;
4370 // The address of _GLOBAL_OFFSET_TABLE_.
4371 Address g_o_t = invalid_address;
4373 // Write out plt call stubs.
4374 typename Plt_stub_entries::const_iterator cs;
4375 for (cs = this->plt_call_stubs_.begin();
4376 cs != this->plt_call_stubs_.end();
4380 Address plt_addr = this->plt_off(cs, &is_iplt);
4383 if (iplt_base == invalid_address)
4384 iplt_base = this->targ_->iplt_section()->address();
4385 plt_addr += iplt_base;
4388 plt_addr += plt_base;
4390 p = oview + cs->second;
4391 if (parameters->options().output_is_position_independent())
4394 const Powerpc_relobj<size, big_endian>* ppcobj
4395 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4396 (cs->first.object_));
4397 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4399 unsigned int got2 = ppcobj->got2_shndx();
4400 got_addr = ppcobj->get_output_section_offset(got2);
4401 gold_assert(got_addr != invalid_address);
4402 got_addr += (ppcobj->output_section(got2)->address()
4403 + cs->first.addend_);
4407 if (g_o_t == invalid_address)
4409 const Output_data_got_powerpc<size, big_endian>* got
4410 = this->targ_->got_section();
4411 g_o_t = got->address() + got->g_o_t();
4416 Address off = plt_addr - got_addr;
4419 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
4420 write_insn<big_endian>(p + 4, mtctr_11);
4421 write_insn<big_endian>(p + 8, bctr);
4425 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4426 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
4427 write_insn<big_endian>(p + 8, mtctr_11);
4428 write_insn<big_endian>(p + 12, bctr);
4433 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4434 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4435 write_insn<big_endian>(p + 8, mtctr_11);
4436 write_insn<big_endian>(p + 12, bctr);
4441 // Write out long branch stubs.
4442 typename Branch_stub_entries::const_iterator bs;
4443 for (bs = this->long_branch_stubs_.begin();
4444 bs != this->long_branch_stubs_.end();
4447 p = oview + this->plt_size_ + bs->second;
4448 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4449 Address delta = bs->first.dest_ - loc;
4450 if (delta + (1 << 25) < 2 << 25)
4451 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4452 else if (!parameters->options().output_is_position_independent())
4454 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
4455 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
4456 write_insn<big_endian>(p + 8, mtctr_12);
4457 write_insn<big_endian>(p + 12, bctr);
4462 write_insn<big_endian>(p + 0, mflr_0);
4463 write_insn<big_endian>(p + 4, bcl_20_31);
4464 write_insn<big_endian>(p + 8, mflr_12);
4465 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4466 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4467 write_insn<big_endian>(p + 20, mtlr_0);
4468 write_insn<big_endian>(p + 24, mtctr_12);
4469 write_insn<big_endian>(p + 28, bctr);
4475 // Write out .glink.
4477 template<int size, bool big_endian>
4479 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4481 const section_size_type off = this->offset();
4482 const section_size_type oview_size =
4483 convert_to_section_size_type(this->data_size());
4484 unsigned char* const oview = of->get_output_view(off, oview_size);
4487 // The base address of the .plt section.
4488 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4489 Address plt_base = this->targ_->plt_section()->address();
4493 if (this->end_branch_table_ != 0)
4495 // Write pltresolve stub.
4497 Address after_bcl = this->address() + 16;
4498 Address pltoff = plt_base - after_bcl;
4500 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
4502 if (this->targ_->abiversion() < 2)
4504 write_insn<big_endian>(p, mflr_12), p += 4;
4505 write_insn<big_endian>(p, bcl_20_31), p += 4;
4506 write_insn<big_endian>(p, mflr_11), p += 4;
4507 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4508 write_insn<big_endian>(p, mtlr_12), p += 4;
4509 write_insn<big_endian>(p, add_11_2_11), p += 4;
4510 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4511 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
4512 write_insn<big_endian>(p, mtctr_12), p += 4;
4513 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
4517 write_insn<big_endian>(p, mflr_0), p += 4;
4518 write_insn<big_endian>(p, bcl_20_31), p += 4;
4519 write_insn<big_endian>(p, mflr_11), p += 4;
4520 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4521 write_insn<big_endian>(p, mtlr_0), p += 4;
4522 write_insn<big_endian>(p, sub_12_12_11), p += 4;
4523 write_insn<big_endian>(p, add_11_2_11), p += 4;
4524 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
4525 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4526 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
4527 write_insn<big_endian>(p, mtctr_12), p += 4;
4528 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
4530 write_insn<big_endian>(p, bctr), p += 4;
4531 while (p < oview + this->pltresolve_size)
4532 write_insn<big_endian>(p, nop), p += 4;
4534 // Write lazy link call stubs.
4536 while (p < oview + this->end_branch_table_)
4538 if (this->targ_->abiversion() < 2)
4542 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4546 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
4547 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4550 uint32_t branch_off = 8 - (p - oview);
4551 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4556 Address plt_base = this->targ_->plt_section()->address();
4557 Address iplt_base = invalid_address;
4558 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4559 Address global_entry_base = this->address() + global_entry_off;
4560 typename Global_entry_stub_entries::const_iterator ge;
4561 for (ge = this->global_entry_stubs_.begin();
4562 ge != this->global_entry_stubs_.end();
4565 p = oview + global_entry_off + ge->second;
4566 Address plt_addr = ge->first->plt_offset();
4567 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
4568 && ge->first->can_use_relative_reloc(false))
4570 if (iplt_base == invalid_address)
4571 iplt_base = this->targ_->iplt_section()->address();
4572 plt_addr += iplt_base;
4575 plt_addr += plt_base;
4576 Address my_addr = global_entry_base + ge->second;
4577 Address off = plt_addr - my_addr;
4579 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
4580 gold_error(_("%s: linkage table error against `%s'"),
4581 ge->first->object()->name().c_str(),
4582 ge->first->demangled_name().c_str());
4584 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
4585 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
4586 write_insn<big_endian>(p, mtctr_12), p += 4;
4587 write_insn<big_endian>(p, bctr);
4592 const Output_data_got_powerpc<size, big_endian>* got
4593 = this->targ_->got_section();
4594 // The address of _GLOBAL_OFFSET_TABLE_.
4595 Address g_o_t = got->address() + got->g_o_t();
4597 // Write out pltresolve branch table.
4599 unsigned int the_end = oview_size - this->pltresolve_size;
4600 unsigned char* end_p = oview + the_end;
4601 while (p < end_p - 8 * 4)
4602 write_insn<big_endian>(p, b + end_p - p), p += 4;
4604 write_insn<big_endian>(p, nop), p += 4;
4606 // Write out pltresolve call stub.
4607 if (parameters->options().output_is_position_independent())
4609 Address res0_off = 0;
4610 Address after_bcl_off = the_end + 12;
4611 Address bcl_res0 = after_bcl_off - res0_off;
4613 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
4614 write_insn<big_endian>(p + 4, mflr_0);
4615 write_insn<big_endian>(p + 8, bcl_20_31);
4616 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4617 write_insn<big_endian>(p + 16, mflr_12);
4618 write_insn<big_endian>(p + 20, mtlr_0);
4619 write_insn<big_endian>(p + 24, sub_11_11_12);
4621 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4623 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4624 if (ha(got_bcl) == ha(got_bcl + 4))
4626 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4627 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4631 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4632 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4634 write_insn<big_endian>(p + 40, mtctr_0);
4635 write_insn<big_endian>(p + 44, add_0_11_11);
4636 write_insn<big_endian>(p + 48, add_11_0_11);
4637 write_insn<big_endian>(p + 52, bctr);
4638 write_insn<big_endian>(p + 56, nop);
4639 write_insn<big_endian>(p + 60, nop);
4643 Address res0 = this->address();
4645 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4646 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4647 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4648 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4650 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4651 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4652 write_insn<big_endian>(p + 16, mtctr_0);
4653 write_insn<big_endian>(p + 20, add_0_11_11);
4654 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4655 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4657 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4658 write_insn<big_endian>(p + 28, add_11_0_11);
4659 write_insn<big_endian>(p + 32, bctr);
4660 write_insn<big_endian>(p + 36, nop);
4661 write_insn<big_endian>(p + 40, nop);
4662 write_insn<big_endian>(p + 44, nop);
4663 write_insn<big_endian>(p + 48, nop);
4664 write_insn<big_endian>(p + 52, nop);
4665 write_insn<big_endian>(p + 56, nop);
4666 write_insn<big_endian>(p + 60, nop);
4671 of->write_output_view(off, oview_size, oview);
4675 // A class to handle linker generated save/restore functions.
4677 template<int size, bool big_endian>
4678 class Output_data_save_res : public Output_section_data_build
4681 Output_data_save_res(Symbol_table* symtab);
4684 // Write to a map file.
4686 do_print_to_mapfile(Mapfile* mapfile) const
4687 { mapfile->print_output_data(this, _("** save/restore")); }
4690 do_write(Output_file*);
4693 // The maximum size of save/restore contents.
4694 static const unsigned int savres_max = 218*4;
4697 savres_define(Symbol_table* symtab,
4699 unsigned int lo, unsigned int hi,
4700 unsigned char* write_ent(unsigned char*, int),
4701 unsigned char* write_tail(unsigned char*, int));
4703 unsigned char *contents_;
4706 template<bool big_endian>
4707 static unsigned char*
4708 savegpr0(unsigned char* p, int r)
4710 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4711 write_insn<big_endian>(p, insn);
4715 template<bool big_endian>
4716 static unsigned char*
4717 savegpr0_tail(unsigned char* p, int r)
4719 p = savegpr0<big_endian>(p, r);
4720 uint32_t insn = std_0_1 + 16;
4721 write_insn<big_endian>(p, insn);
4723 write_insn<big_endian>(p, blr);
4727 template<bool big_endian>
4728 static unsigned char*
4729 restgpr0(unsigned char* p, int r)
4731 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4732 write_insn<big_endian>(p, insn);
4736 template<bool big_endian>
4737 static unsigned char*
4738 restgpr0_tail(unsigned char* p, int r)
4740 uint32_t insn = ld_0_1 + 16;
4741 write_insn<big_endian>(p, insn);
4743 p = restgpr0<big_endian>(p, r);
4744 write_insn<big_endian>(p, mtlr_0);
4748 p = restgpr0<big_endian>(p, 30);
4749 p = restgpr0<big_endian>(p, 31);
4751 write_insn<big_endian>(p, blr);
4755 template<bool big_endian>
4756 static unsigned char*
4757 savegpr1(unsigned char* p, int r)
4759 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4760 write_insn<big_endian>(p, insn);
4764 template<bool big_endian>
4765 static unsigned char*
4766 savegpr1_tail(unsigned char* p, int r)
4768 p = savegpr1<big_endian>(p, r);
4769 write_insn<big_endian>(p, blr);
4773 template<bool big_endian>
4774 static unsigned char*
4775 restgpr1(unsigned char* p, int r)
4777 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4778 write_insn<big_endian>(p, insn);
4782 template<bool big_endian>
4783 static unsigned char*
4784 restgpr1_tail(unsigned char* p, int r)
4786 p = restgpr1<big_endian>(p, r);
4787 write_insn<big_endian>(p, blr);
4791 template<bool big_endian>
4792 static unsigned char*
4793 savefpr(unsigned char* p, int r)
4795 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4796 write_insn<big_endian>(p, insn);
4800 template<bool big_endian>
4801 static unsigned char*
4802 savefpr0_tail(unsigned char* p, int r)
4804 p = savefpr<big_endian>(p, r);
4805 write_insn<big_endian>(p, std_0_1 + 16);
4807 write_insn<big_endian>(p, blr);
4811 template<bool big_endian>
4812 static unsigned char*
4813 restfpr(unsigned char* p, int r)
4815 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4816 write_insn<big_endian>(p, insn);
4820 template<bool big_endian>
4821 static unsigned char*
4822 restfpr0_tail(unsigned char* p, int r)
4824 write_insn<big_endian>(p, ld_0_1 + 16);
4826 p = restfpr<big_endian>(p, r);
4827 write_insn<big_endian>(p, mtlr_0);
4831 p = restfpr<big_endian>(p, 30);
4832 p = restfpr<big_endian>(p, 31);
4834 write_insn<big_endian>(p, blr);
4838 template<bool big_endian>
4839 static unsigned char*
4840 savefpr1_tail(unsigned char* p, int r)
4842 p = savefpr<big_endian>(p, r);
4843 write_insn<big_endian>(p, blr);
4847 template<bool big_endian>
4848 static unsigned char*
4849 restfpr1_tail(unsigned char* p, int r)
4851 p = restfpr<big_endian>(p, r);
4852 write_insn<big_endian>(p, blr);
4856 template<bool big_endian>
4857 static unsigned char*
4858 savevr(unsigned char* p, int r)
4860 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4861 write_insn<big_endian>(p, insn);
4863 insn = stvx_0_12_0 + (r << 21);
4864 write_insn<big_endian>(p, insn);
4868 template<bool big_endian>
4869 static unsigned char*
4870 savevr_tail(unsigned char* p, int r)
4872 p = savevr<big_endian>(p, r);
4873 write_insn<big_endian>(p, blr);
4877 template<bool big_endian>
4878 static unsigned char*
4879 restvr(unsigned char* p, int r)
4881 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4882 write_insn<big_endian>(p, insn);
4884 insn = lvx_0_12_0 + (r << 21);
4885 write_insn<big_endian>(p, insn);
4889 template<bool big_endian>
4890 static unsigned char*
4891 restvr_tail(unsigned char* p, int r)
4893 p = restvr<big_endian>(p, r);
4894 write_insn<big_endian>(p, blr);
4899 template<int size, bool big_endian>
4900 Output_data_save_res<size, big_endian>::Output_data_save_res(
4901 Symbol_table* symtab)
4902 : Output_section_data_build(4),
4905 this->savres_define(symtab,
4906 "_savegpr0_", 14, 31,
4907 savegpr0<big_endian>, savegpr0_tail<big_endian>);
4908 this->savres_define(symtab,
4909 "_restgpr0_", 14, 29,
4910 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4911 this->savres_define(symtab,
4912 "_restgpr0_", 30, 31,
4913 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4914 this->savres_define(symtab,
4915 "_savegpr1_", 14, 31,
4916 savegpr1<big_endian>, savegpr1_tail<big_endian>);
4917 this->savres_define(symtab,
4918 "_restgpr1_", 14, 31,
4919 restgpr1<big_endian>, restgpr1_tail<big_endian>);
4920 this->savres_define(symtab,
4921 "_savefpr_", 14, 31,
4922 savefpr<big_endian>, savefpr0_tail<big_endian>);
4923 this->savres_define(symtab,
4924 "_restfpr_", 14, 29,
4925 restfpr<big_endian>, restfpr0_tail<big_endian>);
4926 this->savres_define(symtab,
4927 "_restfpr_", 30, 31,
4928 restfpr<big_endian>, restfpr0_tail<big_endian>);
4929 this->savres_define(symtab,
4931 savefpr<big_endian>, savefpr1_tail<big_endian>);
4932 this->savres_define(symtab,
4934 restfpr<big_endian>, restfpr1_tail<big_endian>);
4935 this->savres_define(symtab,
4937 savevr<big_endian>, savevr_tail<big_endian>);
4938 this->savres_define(symtab,
4940 restvr<big_endian>, restvr_tail<big_endian>);
4943 template<int size, bool big_endian>
4945 Output_data_save_res<size, big_endian>::savres_define(
4946 Symbol_table* symtab,
4948 unsigned int lo, unsigned int hi,
4949 unsigned char* write_ent(unsigned char*, int),
4950 unsigned char* write_tail(unsigned char*, int))
4952 size_t len = strlen(name);
4953 bool writing = false;
4956 memcpy(sym, name, len);
4959 for (unsigned int i = lo; i <= hi; i++)
4961 sym[len + 0] = i / 10 + '0';
4962 sym[len + 1] = i % 10 + '0';
4963 Symbol* gsym = symtab->lookup(sym);
4964 bool refd = gsym != NULL && gsym->is_undefined();
4965 writing = writing || refd;
4968 if (this->contents_ == NULL)
4969 this->contents_ = new unsigned char[this->savres_max];
4971 section_size_type value = this->current_data_size();
4972 unsigned char* p = this->contents_ + value;
4974 p = write_ent(p, i);
4976 p = write_tail(p, i);
4977 section_size_type cur_size = p - this->contents_;
4978 this->set_current_data_size(cur_size);
4980 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4981 this, value, cur_size - value,
4982 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4983 elfcpp::STV_HIDDEN, 0, false, false);
4988 // Write out save/restore.
4990 template<int size, bool big_endian>
4992 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4994 const section_size_type off = this->offset();
4995 const section_size_type oview_size =
4996 convert_to_section_size_type(this->data_size());
4997 unsigned char* const oview = of->get_output_view(off, oview_size);
4998 memcpy(oview, this->contents_, oview_size);
4999 of->write_output_view(off, oview_size, oview);
5003 // Create the glink section.
5005 template<int size, bool big_endian>
5007 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
5009 if (this->glink_ == NULL)
5011 this->glink_ = new Output_data_glink<size, big_endian>(this);
5012 this->glink_->add_eh_frame(layout);
5013 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5014 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5015 this->glink_, ORDER_TEXT, false);
5019 // Create a PLT entry for a global symbol.
5021 template<int size, bool big_endian>
5023 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5027 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5028 && gsym->can_use_relative_reloc(false))
5030 if (this->iplt_ == NULL)
5031 this->make_iplt_section(symtab, layout);
5032 this->iplt_->add_ifunc_entry(gsym);
5036 if (this->plt_ == NULL)
5037 this->make_plt_section(symtab, layout);
5038 this->plt_->add_entry(gsym);
5042 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5044 template<int size, bool big_endian>
5046 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5047 Symbol_table* symtab,
5049 Sized_relobj_file<size, big_endian>* relobj,
5052 if (this->iplt_ == NULL)
5053 this->make_iplt_section(symtab, layout);
5054 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5057 // Return the number of entries in the PLT.
5059 template<int size, bool big_endian>
5061 Target_powerpc<size, big_endian>::plt_entry_count() const
5063 if (this->plt_ == NULL)
5065 return this->plt_->entry_count();
5068 // Create a GOT entry for local dynamic __tls_get_addr calls.
5070 template<int size, bool big_endian>
5072 Target_powerpc<size, big_endian>::tlsld_got_offset(
5073 Symbol_table* symtab,
5075 Sized_relobj_file<size, big_endian>* object)
5077 if (this->tlsld_got_offset_ == -1U)
5079 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5080 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5081 Output_data_got_powerpc<size, big_endian>* got
5082 = this->got_section(symtab, layout);
5083 unsigned int got_offset = got->add_constant_pair(0, 0);
5084 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5086 this->tlsld_got_offset_ = got_offset;
5088 return this->tlsld_got_offset_;
5091 // Get the Reference_flags for a particular relocation.
5093 template<int size, bool big_endian>
5095 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5096 unsigned int r_type,
5097 const Target_powerpc* target)
5103 case elfcpp::R_POWERPC_NONE:
5104 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5105 case elfcpp::R_POWERPC_GNU_VTENTRY:
5106 case elfcpp::R_PPC64_TOC:
5107 // No symbol reference.
5110 case elfcpp::R_PPC64_ADDR64:
5111 case elfcpp::R_PPC64_UADDR64:
5112 case elfcpp::R_POWERPC_ADDR32:
5113 case elfcpp::R_POWERPC_UADDR32:
5114 case elfcpp::R_POWERPC_ADDR16:
5115 case elfcpp::R_POWERPC_UADDR16:
5116 case elfcpp::R_POWERPC_ADDR16_LO:
5117 case elfcpp::R_POWERPC_ADDR16_HI:
5118 case elfcpp::R_POWERPC_ADDR16_HA:
5119 ref = Symbol::ABSOLUTE_REF;
5122 case elfcpp::R_POWERPC_ADDR24:
5123 case elfcpp::R_POWERPC_ADDR14:
5124 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5125 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5126 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5129 case elfcpp::R_PPC64_REL64:
5130 case elfcpp::R_POWERPC_REL32:
5131 case elfcpp::R_PPC_LOCAL24PC:
5132 case elfcpp::R_POWERPC_REL16:
5133 case elfcpp::R_POWERPC_REL16_LO:
5134 case elfcpp::R_POWERPC_REL16_HI:
5135 case elfcpp::R_POWERPC_REL16_HA:
5136 ref = Symbol::RELATIVE_REF;
5139 case elfcpp::R_POWERPC_REL24:
5140 case elfcpp::R_PPC_PLTREL24:
5141 case elfcpp::R_POWERPC_REL14:
5142 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5143 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5144 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5147 case elfcpp::R_POWERPC_GOT16:
5148 case elfcpp::R_POWERPC_GOT16_LO:
5149 case elfcpp::R_POWERPC_GOT16_HI:
5150 case elfcpp::R_POWERPC_GOT16_HA:
5151 case elfcpp::R_PPC64_GOT16_DS:
5152 case elfcpp::R_PPC64_GOT16_LO_DS:
5153 case elfcpp::R_PPC64_TOC16:
5154 case elfcpp::R_PPC64_TOC16_LO:
5155 case elfcpp::R_PPC64_TOC16_HI:
5156 case elfcpp::R_PPC64_TOC16_HA:
5157 case elfcpp::R_PPC64_TOC16_DS:
5158 case elfcpp::R_PPC64_TOC16_LO_DS:
5160 ref = Symbol::ABSOLUTE_REF;
5163 case elfcpp::R_POWERPC_GOT_TPREL16:
5164 case elfcpp::R_POWERPC_TLS:
5165 ref = Symbol::TLS_REF;
5168 case elfcpp::R_POWERPC_COPY:
5169 case elfcpp::R_POWERPC_GLOB_DAT:
5170 case elfcpp::R_POWERPC_JMP_SLOT:
5171 case elfcpp::R_POWERPC_RELATIVE:
5172 case elfcpp::R_POWERPC_DTPMOD:
5174 // Not expected. We will give an error later.
5178 if (size == 64 && target->abiversion() < 2)
5179 ref |= Symbol::FUNC_DESC_ABI;
5183 // Report an unsupported relocation against a local symbol.
5185 template<int size, bool big_endian>
5187 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5188 Sized_relobj_file<size, big_endian>* object,
5189 unsigned int r_type)
5191 gold_error(_("%s: unsupported reloc %u against local symbol"),
5192 object->name().c_str(), r_type);
5195 // We are about to emit a dynamic relocation of type R_TYPE. If the
5196 // dynamic linker does not support it, issue an error.
5198 template<int size, bool big_endian>
5200 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5201 unsigned int r_type)
5203 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5205 // These are the relocation types supported by glibc for both 32-bit
5206 // and 64-bit powerpc.
5209 case elfcpp::R_POWERPC_NONE:
5210 case elfcpp::R_POWERPC_RELATIVE:
5211 case elfcpp::R_POWERPC_GLOB_DAT:
5212 case elfcpp::R_POWERPC_DTPMOD:
5213 case elfcpp::R_POWERPC_DTPREL:
5214 case elfcpp::R_POWERPC_TPREL:
5215 case elfcpp::R_POWERPC_JMP_SLOT:
5216 case elfcpp::R_POWERPC_COPY:
5217 case elfcpp::R_POWERPC_IRELATIVE:
5218 case elfcpp::R_POWERPC_ADDR32:
5219 case elfcpp::R_POWERPC_UADDR32:
5220 case elfcpp::R_POWERPC_ADDR24:
5221 case elfcpp::R_POWERPC_ADDR16:
5222 case elfcpp::R_POWERPC_UADDR16:
5223 case elfcpp::R_POWERPC_ADDR16_LO:
5224 case elfcpp::R_POWERPC_ADDR16_HI:
5225 case elfcpp::R_POWERPC_ADDR16_HA:
5226 case elfcpp::R_POWERPC_ADDR14:
5227 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5228 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5229 case elfcpp::R_POWERPC_REL32:
5230 case elfcpp::R_POWERPC_REL24:
5231 case elfcpp::R_POWERPC_TPREL16:
5232 case elfcpp::R_POWERPC_TPREL16_LO:
5233 case elfcpp::R_POWERPC_TPREL16_HI:
5234 case elfcpp::R_POWERPC_TPREL16_HA:
5245 // These are the relocation types supported only on 64-bit.
5246 case elfcpp::R_PPC64_ADDR64:
5247 case elfcpp::R_PPC64_UADDR64:
5248 case elfcpp::R_PPC64_JMP_IREL:
5249 case elfcpp::R_PPC64_ADDR16_DS:
5250 case elfcpp::R_PPC64_ADDR16_LO_DS:
5251 case elfcpp::R_PPC64_ADDR16_HIGH:
5252 case elfcpp::R_PPC64_ADDR16_HIGHA:
5253 case elfcpp::R_PPC64_ADDR16_HIGHER:
5254 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5255 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5256 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5257 case elfcpp::R_PPC64_REL64:
5258 case elfcpp::R_POWERPC_ADDR30:
5259 case elfcpp::R_PPC64_TPREL16_DS:
5260 case elfcpp::R_PPC64_TPREL16_LO_DS:
5261 case elfcpp::R_PPC64_TPREL16_HIGH:
5262 case elfcpp::R_PPC64_TPREL16_HIGHA:
5263 case elfcpp::R_PPC64_TPREL16_HIGHER:
5264 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5265 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5266 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5277 // These are the relocation types supported only on 32-bit.
5278 // ??? glibc ld.so doesn't need to support these.
5279 case elfcpp::R_POWERPC_DTPREL16:
5280 case elfcpp::R_POWERPC_DTPREL16_LO:
5281 case elfcpp::R_POWERPC_DTPREL16_HI:
5282 case elfcpp::R_POWERPC_DTPREL16_HA:
5290 // This prevents us from issuing more than one error per reloc
5291 // section. But we can still wind up issuing more than one
5292 // error per object file.
5293 if (this->issued_non_pic_error_)
5295 gold_assert(parameters->options().output_is_position_independent());
5296 object->error(_("requires unsupported dynamic reloc; "
5297 "recompile with -fPIC"));
5298 this->issued_non_pic_error_ = true;
5302 // Return whether we need to make a PLT entry for a relocation of the
5303 // given type against a STT_GNU_IFUNC symbol.
5305 template<int size, bool big_endian>
5307 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5308 Target_powerpc<size, big_endian>* target,
5309 Sized_relobj_file<size, big_endian>* object,
5310 unsigned int r_type,
5313 // In non-pic code any reference will resolve to the plt call stub
5314 // for the ifunc symbol.
5315 if ((size == 32 || target->abiversion() >= 2)
5316 && !parameters->options().output_is_position_independent())
5321 // Word size refs from data sections are OK, but don't need a PLT entry.
5322 case elfcpp::R_POWERPC_ADDR32:
5323 case elfcpp::R_POWERPC_UADDR32:
5328 case elfcpp::R_PPC64_ADDR64:
5329 case elfcpp::R_PPC64_UADDR64:
5334 // GOT refs are good, but also don't need a PLT entry.
5335 case elfcpp::R_POWERPC_GOT16:
5336 case elfcpp::R_POWERPC_GOT16_LO:
5337 case elfcpp::R_POWERPC_GOT16_HI:
5338 case elfcpp::R_POWERPC_GOT16_HA:
5339 case elfcpp::R_PPC64_GOT16_DS:
5340 case elfcpp::R_PPC64_GOT16_LO_DS:
5343 // Function calls are good, and these do need a PLT entry.
5344 case elfcpp::R_POWERPC_ADDR24:
5345 case elfcpp::R_POWERPC_ADDR14:
5346 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5347 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5348 case elfcpp::R_POWERPC_REL24:
5349 case elfcpp::R_PPC_PLTREL24:
5350 case elfcpp::R_POWERPC_REL14:
5351 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5352 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5359 // Anything else is a problem.
5360 // If we are building a static executable, the libc startup function
5361 // responsible for applying indirect function relocations is going
5362 // to complain about the reloc type.
5363 // If we are building a dynamic executable, we will have a text
5364 // relocation. The dynamic loader will set the text segment
5365 // writable and non-executable to apply text relocations. So we'll
5366 // segfault when trying to run the indirection function to resolve
5369 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5370 object->name().c_str(), r_type);
5374 // Scan a relocation for a local symbol.
5376 template<int size, bool big_endian>
5378 Target_powerpc<size, big_endian>::Scan::local(
5379 Symbol_table* symtab,
5381 Target_powerpc<size, big_endian>* target,
5382 Sized_relobj_file<size, big_endian>* object,
5383 unsigned int data_shndx,
5384 Output_section* output_section,
5385 const elfcpp::Rela<size, big_endian>& reloc,
5386 unsigned int r_type,
5387 const elfcpp::Sym<size, big_endian>& lsym,
5390 this->maybe_skip_tls_get_addr_call(r_type, NULL);
5392 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5393 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5395 this->expect_tls_get_addr_call();
5396 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5397 if (tls_type != tls::TLSOPT_NONE)
5398 this->skip_next_tls_get_addr_call();
5400 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5401 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5403 this->expect_tls_get_addr_call();
5404 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5405 if (tls_type != tls::TLSOPT_NONE)
5406 this->skip_next_tls_get_addr_call();
5409 Powerpc_relobj<size, big_endian>* ppc_object
5410 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5415 && data_shndx == ppc_object->opd_shndx()
5416 && r_type == elfcpp::R_PPC64_ADDR64)
5417 ppc_object->set_opd_discard(reloc.get_r_offset());
5421 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5422 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5423 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5425 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5426 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5427 r_type, r_sym, reloc.get_r_addend());
5428 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5433 case elfcpp::R_POWERPC_NONE:
5434 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5435 case elfcpp::R_POWERPC_GNU_VTENTRY:
5436 case elfcpp::R_PPC64_TOCSAVE:
5437 case elfcpp::R_POWERPC_TLS:
5440 case elfcpp::R_PPC64_TOC:
5442 Output_data_got_powerpc<size, big_endian>* got
5443 = target->got_section(symtab, layout);
5444 if (parameters->options().output_is_position_independent())
5446 Address off = reloc.get_r_offset();
5448 && target->abiversion() < 2
5449 && data_shndx == ppc_object->opd_shndx()
5450 && ppc_object->get_opd_discard(off - 8))
5453 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5454 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5455 rela_dyn->add_output_section_relative(got->output_section(),
5456 elfcpp::R_POWERPC_RELATIVE,
5458 object, data_shndx, off,
5459 symobj->toc_base_offset());
5464 case elfcpp::R_PPC64_ADDR64:
5465 case elfcpp::R_PPC64_UADDR64:
5466 case elfcpp::R_POWERPC_ADDR32:
5467 case elfcpp::R_POWERPC_UADDR32:
5468 case elfcpp::R_POWERPC_ADDR24:
5469 case elfcpp::R_POWERPC_ADDR16:
5470 case elfcpp::R_POWERPC_ADDR16_LO:
5471 case elfcpp::R_POWERPC_ADDR16_HI:
5472 case elfcpp::R_POWERPC_ADDR16_HA:
5473 case elfcpp::R_POWERPC_UADDR16:
5474 case elfcpp::R_PPC64_ADDR16_HIGH:
5475 case elfcpp::R_PPC64_ADDR16_HIGHA:
5476 case elfcpp::R_PPC64_ADDR16_HIGHER:
5477 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5478 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5479 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5480 case elfcpp::R_PPC64_ADDR16_DS:
5481 case elfcpp::R_PPC64_ADDR16_LO_DS:
5482 case elfcpp::R_POWERPC_ADDR14:
5483 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5484 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5485 // If building a shared library (or a position-independent
5486 // executable), we need to create a dynamic relocation for
5488 if (parameters->options().output_is_position_independent()
5489 || (size == 64 && is_ifunc && target->abiversion() < 2))
5491 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5493 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5494 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5495 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5497 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5498 : elfcpp::R_POWERPC_RELATIVE);
5499 rela_dyn->add_local_relative(object, r_sym, dynrel,
5500 output_section, data_shndx,
5501 reloc.get_r_offset(),
5502 reloc.get_r_addend(), false);
5504 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
5506 check_non_pic(object, r_type);
5507 rela_dyn->add_local(object, r_sym, r_type, output_section,
5508 data_shndx, reloc.get_r_offset(),
5509 reloc.get_r_addend());
5513 gold_assert(lsym.get_st_value() == 0);
5514 unsigned int shndx = lsym.get_st_shndx();
5516 shndx = object->adjust_sym_shndx(r_sym, shndx,
5519 object->error(_("section symbol %u has bad shndx %u"),
5522 rela_dyn->add_local_section(object, shndx, r_type,
5523 output_section, data_shndx,
5524 reloc.get_r_offset());
5529 case elfcpp::R_POWERPC_REL24:
5530 case elfcpp::R_PPC_PLTREL24:
5531 case elfcpp::R_PPC_LOCAL24PC:
5532 case elfcpp::R_POWERPC_REL14:
5533 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5534 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5536 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5537 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5538 reloc.get_r_addend());
5541 case elfcpp::R_PPC64_REL64:
5542 case elfcpp::R_POWERPC_REL32:
5543 case elfcpp::R_POWERPC_REL16:
5544 case elfcpp::R_POWERPC_REL16_LO:
5545 case elfcpp::R_POWERPC_REL16_HI:
5546 case elfcpp::R_POWERPC_REL16_HA:
5547 case elfcpp::R_POWERPC_SECTOFF:
5548 case elfcpp::R_POWERPC_SECTOFF_LO:
5549 case elfcpp::R_POWERPC_SECTOFF_HI:
5550 case elfcpp::R_POWERPC_SECTOFF_HA:
5551 case elfcpp::R_PPC64_SECTOFF_DS:
5552 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5553 case elfcpp::R_POWERPC_TPREL16:
5554 case elfcpp::R_POWERPC_TPREL16_LO:
5555 case elfcpp::R_POWERPC_TPREL16_HI:
5556 case elfcpp::R_POWERPC_TPREL16_HA:
5557 case elfcpp::R_PPC64_TPREL16_DS:
5558 case elfcpp::R_PPC64_TPREL16_LO_DS:
5559 case elfcpp::R_PPC64_TPREL16_HIGH:
5560 case elfcpp::R_PPC64_TPREL16_HIGHA:
5561 case elfcpp::R_PPC64_TPREL16_HIGHER:
5562 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5563 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5564 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5565 case elfcpp::R_POWERPC_DTPREL16:
5566 case elfcpp::R_POWERPC_DTPREL16_LO:
5567 case elfcpp::R_POWERPC_DTPREL16_HI:
5568 case elfcpp::R_POWERPC_DTPREL16_HA:
5569 case elfcpp::R_PPC64_DTPREL16_DS:
5570 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5571 case elfcpp::R_PPC64_DTPREL16_HIGH:
5572 case elfcpp::R_PPC64_DTPREL16_HIGHA:
5573 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5574 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5575 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5576 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5577 case elfcpp::R_PPC64_TLSGD:
5578 case elfcpp::R_PPC64_TLSLD:
5579 case elfcpp::R_PPC64_ADDR64_LOCAL:
5582 case elfcpp::R_POWERPC_GOT16:
5583 case elfcpp::R_POWERPC_GOT16_LO:
5584 case elfcpp::R_POWERPC_GOT16_HI:
5585 case elfcpp::R_POWERPC_GOT16_HA:
5586 case elfcpp::R_PPC64_GOT16_DS:
5587 case elfcpp::R_PPC64_GOT16_LO_DS:
5589 // The symbol requires a GOT entry.
5590 Output_data_got_powerpc<size, big_endian>* got
5591 = target->got_section(symtab, layout);
5592 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5594 if (!parameters->options().output_is_position_independent())
5597 && (size == 32 || target->abiversion() >= 2))
5598 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5600 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5602 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5604 // If we are generating a shared object or a pie, this
5605 // symbol's GOT entry will be set by a dynamic relocation.
5607 off = got->add_constant(0);
5608 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5610 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5612 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5613 : elfcpp::R_POWERPC_RELATIVE);
5614 rela_dyn->add_local_relative(object, r_sym, dynrel,
5615 got, off, 0, false);
5620 case elfcpp::R_PPC64_TOC16:
5621 case elfcpp::R_PPC64_TOC16_LO:
5622 case elfcpp::R_PPC64_TOC16_HI:
5623 case elfcpp::R_PPC64_TOC16_HA:
5624 case elfcpp::R_PPC64_TOC16_DS:
5625 case elfcpp::R_PPC64_TOC16_LO_DS:
5626 // We need a GOT section.
5627 target->got_section(symtab, layout);
5630 case elfcpp::R_POWERPC_GOT_TLSGD16:
5631 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5632 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5633 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5635 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5636 if (tls_type == tls::TLSOPT_NONE)
5638 Output_data_got_powerpc<size, big_endian>* got
5639 = target->got_section(symtab, layout);
5640 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5641 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5642 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5643 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5645 else if (tls_type == tls::TLSOPT_TO_LE)
5647 // no GOT relocs needed for Local Exec.
5654 case elfcpp::R_POWERPC_GOT_TLSLD16:
5655 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5656 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5657 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5659 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5660 if (tls_type == tls::TLSOPT_NONE)
5661 target->tlsld_got_offset(symtab, layout, object);
5662 else if (tls_type == tls::TLSOPT_TO_LE)
5664 // no GOT relocs needed for Local Exec.
5665 if (parameters->options().emit_relocs())
5667 Output_section* os = layout->tls_segment()->first_section();
5668 gold_assert(os != NULL);
5669 os->set_needs_symtab_index();
5677 case elfcpp::R_POWERPC_GOT_DTPREL16:
5678 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5679 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5680 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5682 Output_data_got_powerpc<size, big_endian>* got
5683 = target->got_section(symtab, layout);
5684 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5685 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5689 case elfcpp::R_POWERPC_GOT_TPREL16:
5690 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5691 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5692 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5694 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5695 if (tls_type == tls::TLSOPT_NONE)
5697 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5698 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5700 Output_data_got_powerpc<size, big_endian>* got
5701 = target->got_section(symtab, layout);
5702 unsigned int off = got->add_constant(0);
5703 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5705 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5706 rela_dyn->add_symbolless_local_addend(object, r_sym,
5707 elfcpp::R_POWERPC_TPREL,
5711 else if (tls_type == tls::TLSOPT_TO_LE)
5713 // no GOT relocs needed for Local Exec.
5721 unsupported_reloc_local(object, r_type);
5727 case elfcpp::R_POWERPC_GOT_TLSLD16:
5728 case elfcpp::R_POWERPC_GOT_TLSGD16:
5729 case elfcpp::R_POWERPC_GOT_TPREL16:
5730 case elfcpp::R_POWERPC_GOT_DTPREL16:
5731 case elfcpp::R_POWERPC_GOT16:
5732 case elfcpp::R_PPC64_GOT16_DS:
5733 case elfcpp::R_PPC64_TOC16:
5734 case elfcpp::R_PPC64_TOC16_DS:
5735 ppc_object->set_has_small_toc_reloc();
5741 // Report an unsupported relocation against a global symbol.
5743 template<int size, bool big_endian>
5745 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5746 Sized_relobj_file<size, big_endian>* object,
5747 unsigned int r_type,
5750 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5751 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5754 // Scan a relocation for a global symbol.
5756 template<int size, bool big_endian>
5758 Target_powerpc<size, big_endian>::Scan::global(
5759 Symbol_table* symtab,
5761 Target_powerpc<size, big_endian>* target,
5762 Sized_relobj_file<size, big_endian>* object,
5763 unsigned int data_shndx,
5764 Output_section* output_section,
5765 const elfcpp::Rela<size, big_endian>& reloc,
5766 unsigned int r_type,
5769 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5772 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5773 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5775 this->expect_tls_get_addr_call();
5776 const bool final = gsym->final_value_is_known();
5777 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5778 if (tls_type != tls::TLSOPT_NONE)
5779 this->skip_next_tls_get_addr_call();
5781 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5782 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5784 this->expect_tls_get_addr_call();
5785 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5786 if (tls_type != tls::TLSOPT_NONE)
5787 this->skip_next_tls_get_addr_call();
5790 Powerpc_relobj<size, big_endian>* ppc_object
5791 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5793 // A STT_GNU_IFUNC symbol may require a PLT entry.
5794 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5795 bool pushed_ifunc = false;
5796 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5798 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5799 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5800 reloc.get_r_addend());
5801 target->make_plt_entry(symtab, layout, gsym);
5802 pushed_ifunc = true;
5807 case elfcpp::R_POWERPC_NONE:
5808 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5809 case elfcpp::R_POWERPC_GNU_VTENTRY:
5810 case elfcpp::R_PPC_LOCAL24PC:
5811 case elfcpp::R_POWERPC_TLS:
5814 case elfcpp::R_PPC64_TOC:
5816 Output_data_got_powerpc<size, big_endian>* got
5817 = target->got_section(symtab, layout);
5818 if (parameters->options().output_is_position_independent())
5820 Address off = reloc.get_r_offset();
5822 && data_shndx == ppc_object->opd_shndx()
5823 && ppc_object->get_opd_discard(off - 8))
5826 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5827 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5828 if (data_shndx != ppc_object->opd_shndx())
5829 symobj = static_cast
5830 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5831 rela_dyn->add_output_section_relative(got->output_section(),
5832 elfcpp::R_POWERPC_RELATIVE,
5834 object, data_shndx, off,
5835 symobj->toc_base_offset());
5840 case elfcpp::R_PPC64_ADDR64:
5842 && target->abiversion() < 2
5843 && data_shndx == ppc_object->opd_shndx()
5844 && (gsym->is_defined_in_discarded_section()
5845 || gsym->object() != object))
5847 ppc_object->set_opd_discard(reloc.get_r_offset());
5851 case elfcpp::R_PPC64_UADDR64:
5852 case elfcpp::R_POWERPC_ADDR32:
5853 case elfcpp::R_POWERPC_UADDR32:
5854 case elfcpp::R_POWERPC_ADDR24:
5855 case elfcpp::R_POWERPC_ADDR16:
5856 case elfcpp::R_POWERPC_ADDR16_LO:
5857 case elfcpp::R_POWERPC_ADDR16_HI:
5858 case elfcpp::R_POWERPC_ADDR16_HA:
5859 case elfcpp::R_POWERPC_UADDR16:
5860 case elfcpp::R_PPC64_ADDR16_HIGH:
5861 case elfcpp::R_PPC64_ADDR16_HIGHA:
5862 case elfcpp::R_PPC64_ADDR16_HIGHER:
5863 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5864 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5865 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5866 case elfcpp::R_PPC64_ADDR16_DS:
5867 case elfcpp::R_PPC64_ADDR16_LO_DS:
5868 case elfcpp::R_POWERPC_ADDR14:
5869 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5870 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5872 // Make a PLT entry if necessary.
5873 if (gsym->needs_plt_entry())
5875 // Since this is not a PC-relative relocation, we may be
5876 // taking the address of a function. In that case we need to
5877 // set the entry in the dynamic symbol table to the address of
5878 // the PLT call stub.
5879 bool need_ifunc_plt = false;
5880 if ((size == 32 || target->abiversion() >= 2)
5881 && gsym->is_from_dynobj()
5882 && !parameters->options().output_is_position_independent())
5884 gsym->set_needs_dynsym_value();
5885 need_ifunc_plt = true;
5887 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
5889 target->push_branch(ppc_object, data_shndx,
5890 reloc.get_r_offset(), r_type,
5891 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5892 reloc.get_r_addend());
5893 target->make_plt_entry(symtab, layout, gsym);
5896 // Make a dynamic relocation if necessary.
5897 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
5898 || (size == 64 && is_ifunc && target->abiversion() < 2))
5900 if (!parameters->options().output_is_position_independent()
5901 && gsym->may_need_copy_reloc())
5903 target->copy_reloc(symtab, layout, object,
5904 data_shndx, output_section, gsym, reloc);
5906 else if ((((size == 32
5907 && r_type == elfcpp::R_POWERPC_ADDR32)
5909 && r_type == elfcpp::R_PPC64_ADDR64
5910 && target->abiversion() >= 2))
5911 && gsym->can_use_relative_reloc(false)
5912 && !(gsym->visibility() == elfcpp::STV_PROTECTED
5913 && parameters->options().shared()))
5915 && r_type == elfcpp::R_PPC64_ADDR64
5916 && target->abiversion() < 2
5917 && (gsym->can_use_relative_reloc(false)
5918 || data_shndx == ppc_object->opd_shndx())))
5920 Reloc_section* rela_dyn
5921 = target->rela_dyn_section(symtab, layout, is_ifunc);
5922 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5923 : elfcpp::R_POWERPC_RELATIVE);
5924 rela_dyn->add_symbolless_global_addend(
5925 gsym, dynrel, output_section, object, data_shndx,
5926 reloc.get_r_offset(), reloc.get_r_addend());
5930 Reloc_section* rela_dyn
5931 = target->rela_dyn_section(symtab, layout, is_ifunc);
5932 check_non_pic(object, r_type);
5933 rela_dyn->add_global(gsym, r_type, output_section,
5935 reloc.get_r_offset(),
5936 reloc.get_r_addend());
5942 case elfcpp::R_PPC_PLTREL24:
5943 case elfcpp::R_POWERPC_REL24:
5946 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5948 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5949 reloc.get_r_addend());
5950 if (gsym->needs_plt_entry()
5951 || (!gsym->final_value_is_known()
5952 && (gsym->is_undefined()
5953 || gsym->is_from_dynobj()
5954 || gsym->is_preemptible())))
5955 target->make_plt_entry(symtab, layout, gsym);
5959 case elfcpp::R_PPC64_REL64:
5960 case elfcpp::R_POWERPC_REL32:
5961 // Make a dynamic relocation if necessary.
5962 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
5964 if (!parameters->options().output_is_position_independent()
5965 && gsym->may_need_copy_reloc())
5967 target->copy_reloc(symtab, layout, object,
5968 data_shndx, output_section, gsym,
5973 Reloc_section* rela_dyn
5974 = target->rela_dyn_section(symtab, layout, is_ifunc);
5975 check_non_pic(object, r_type);
5976 rela_dyn->add_global(gsym, r_type, output_section, object,
5977 data_shndx, reloc.get_r_offset(),
5978 reloc.get_r_addend());
5983 case elfcpp::R_POWERPC_REL14:
5984 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5985 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5987 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5988 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5989 reloc.get_r_addend());
5992 case elfcpp::R_POWERPC_REL16:
5993 case elfcpp::R_POWERPC_REL16_LO:
5994 case elfcpp::R_POWERPC_REL16_HI:
5995 case elfcpp::R_POWERPC_REL16_HA:
5996 case elfcpp::R_POWERPC_SECTOFF:
5997 case elfcpp::R_POWERPC_SECTOFF_LO:
5998 case elfcpp::R_POWERPC_SECTOFF_HI:
5999 case elfcpp::R_POWERPC_SECTOFF_HA:
6000 case elfcpp::R_PPC64_SECTOFF_DS:
6001 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6002 case elfcpp::R_POWERPC_TPREL16:
6003 case elfcpp::R_POWERPC_TPREL16_LO:
6004 case elfcpp::R_POWERPC_TPREL16_HI:
6005 case elfcpp::R_POWERPC_TPREL16_HA:
6006 case elfcpp::R_PPC64_TPREL16_DS:
6007 case elfcpp::R_PPC64_TPREL16_LO_DS:
6008 case elfcpp::R_PPC64_TPREL16_HIGH:
6009 case elfcpp::R_PPC64_TPREL16_HIGHA:
6010 case elfcpp::R_PPC64_TPREL16_HIGHER:
6011 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6012 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6013 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6014 case elfcpp::R_POWERPC_DTPREL16:
6015 case elfcpp::R_POWERPC_DTPREL16_LO:
6016 case elfcpp::R_POWERPC_DTPREL16_HI:
6017 case elfcpp::R_POWERPC_DTPREL16_HA:
6018 case elfcpp::R_PPC64_DTPREL16_DS:
6019 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6020 case elfcpp::R_PPC64_DTPREL16_HIGH:
6021 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6022 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6023 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6024 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6025 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6026 case elfcpp::R_PPC64_TLSGD:
6027 case elfcpp::R_PPC64_TLSLD:
6028 case elfcpp::R_PPC64_ADDR64_LOCAL:
6031 case elfcpp::R_POWERPC_GOT16:
6032 case elfcpp::R_POWERPC_GOT16_LO:
6033 case elfcpp::R_POWERPC_GOT16_HI:
6034 case elfcpp::R_POWERPC_GOT16_HA:
6035 case elfcpp::R_PPC64_GOT16_DS:
6036 case elfcpp::R_PPC64_GOT16_LO_DS:
6038 // The symbol requires a GOT entry.
6039 Output_data_got_powerpc<size, big_endian>* got;
6041 got = target->got_section(symtab, layout);
6042 if (gsym->final_value_is_known())
6045 && (size == 32 || target->abiversion() >= 2))
6046 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6048 got->add_global(gsym, GOT_TYPE_STANDARD);
6050 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
6052 // If we are generating a shared object or a pie, this
6053 // symbol's GOT entry will be set by a dynamic relocation.
6054 unsigned int off = got->add_constant(0);
6055 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
6057 Reloc_section* rela_dyn
6058 = target->rela_dyn_section(symtab, layout, is_ifunc);
6060 if (gsym->can_use_relative_reloc(false)
6062 || target->abiversion() >= 2)
6063 && gsym->visibility() == elfcpp::STV_PROTECTED
6064 && parameters->options().shared()))
6066 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6067 : elfcpp::R_POWERPC_RELATIVE);
6068 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
6072 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
6073 rela_dyn->add_global(gsym, dynrel, got, off, 0);
6079 case elfcpp::R_PPC64_TOC16:
6080 case elfcpp::R_PPC64_TOC16_LO:
6081 case elfcpp::R_PPC64_TOC16_HI:
6082 case elfcpp::R_PPC64_TOC16_HA:
6083 case elfcpp::R_PPC64_TOC16_DS:
6084 case elfcpp::R_PPC64_TOC16_LO_DS:
6085 // We need a GOT section.
6086 target->got_section(symtab, layout);
6089 case elfcpp::R_POWERPC_GOT_TLSGD16:
6090 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6091 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6092 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6094 const bool final = gsym->final_value_is_known();
6095 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6096 if (tls_type == tls::TLSOPT_NONE)
6098 Output_data_got_powerpc<size, big_endian>* got
6099 = target->got_section(symtab, layout);
6100 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6101 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
6102 elfcpp::R_POWERPC_DTPMOD,
6103 elfcpp::R_POWERPC_DTPREL);
6105 else if (tls_type == tls::TLSOPT_TO_IE)
6107 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6109 Output_data_got_powerpc<size, big_endian>* got
6110 = target->got_section(symtab, layout);
6111 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6112 if (gsym->is_undefined()
6113 || gsym->is_from_dynobj())
6115 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6116 elfcpp::R_POWERPC_TPREL);
6120 unsigned int off = got->add_constant(0);
6121 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6122 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6123 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6128 else if (tls_type == tls::TLSOPT_TO_LE)
6130 // no GOT relocs needed for Local Exec.
6137 case elfcpp::R_POWERPC_GOT_TLSLD16:
6138 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6139 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6140 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6142 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6143 if (tls_type == tls::TLSOPT_NONE)
6144 target->tlsld_got_offset(symtab, layout, object);
6145 else if (tls_type == tls::TLSOPT_TO_LE)
6147 // no GOT relocs needed for Local Exec.
6148 if (parameters->options().emit_relocs())
6150 Output_section* os = layout->tls_segment()->first_section();
6151 gold_assert(os != NULL);
6152 os->set_needs_symtab_index();
6160 case elfcpp::R_POWERPC_GOT_DTPREL16:
6161 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6162 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6163 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6165 Output_data_got_powerpc<size, big_endian>* got
6166 = target->got_section(symtab, layout);
6167 if (!gsym->final_value_is_known()
6168 && (gsym->is_from_dynobj()
6169 || gsym->is_undefined()
6170 || gsym->is_preemptible()))
6171 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6172 target->rela_dyn_section(layout),
6173 elfcpp::R_POWERPC_DTPREL);
6175 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
6179 case elfcpp::R_POWERPC_GOT_TPREL16:
6180 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6181 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6182 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6184 const bool final = gsym->final_value_is_known();
6185 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6186 if (tls_type == tls::TLSOPT_NONE)
6188 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6190 Output_data_got_powerpc<size, big_endian>* got
6191 = target->got_section(symtab, layout);
6192 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6193 if (gsym->is_undefined()
6194 || gsym->is_from_dynobj())
6196 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6197 elfcpp::R_POWERPC_TPREL);
6201 unsigned int off = got->add_constant(0);
6202 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6203 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6204 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6209 else if (tls_type == tls::TLSOPT_TO_LE)
6211 // no GOT relocs needed for Local Exec.
6219 unsupported_reloc_global(object, r_type, gsym);
6225 case elfcpp::R_POWERPC_GOT_TLSLD16:
6226 case elfcpp::R_POWERPC_GOT_TLSGD16:
6227 case elfcpp::R_POWERPC_GOT_TPREL16:
6228 case elfcpp::R_POWERPC_GOT_DTPREL16:
6229 case elfcpp::R_POWERPC_GOT16:
6230 case elfcpp::R_PPC64_GOT16_DS:
6231 case elfcpp::R_PPC64_TOC16:
6232 case elfcpp::R_PPC64_TOC16_DS:
6233 ppc_object->set_has_small_toc_reloc();
6239 // Process relocations for gc.
6241 template<int size, bool big_endian>
6243 Target_powerpc<size, big_endian>::gc_process_relocs(
6244 Symbol_table* symtab,
6246 Sized_relobj_file<size, big_endian>* object,
6247 unsigned int data_shndx,
6249 const unsigned char* prelocs,
6251 Output_section* output_section,
6252 bool needs_special_offset_handling,
6253 size_t local_symbol_count,
6254 const unsigned char* plocal_symbols)
6256 typedef Target_powerpc<size, big_endian> Powerpc;
6257 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6258 Powerpc_relobj<size, big_endian>* ppc_object
6259 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6261 ppc_object->set_opd_valid();
6262 if (size == 64 && data_shndx == ppc_object->opd_shndx())
6264 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
6265 for (p = ppc_object->access_from_map()->begin();
6266 p != ppc_object->access_from_map()->end();
6269 Address dst_off = p->first;
6270 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6271 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
6272 for (s = p->second.begin(); s != p->second.end(); ++s)
6274 Object* src_obj = s->first;
6275 unsigned int src_indx = s->second;
6276 symtab->gc()->add_reference(src_obj, src_indx,
6277 ppc_object, dst_indx);
6281 ppc_object->access_from_map()->clear();
6282 ppc_object->process_gc_mark(symtab);
6283 // Don't look at .opd relocs as .opd will reference everything.
6287 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
6288 typename Target_powerpc::Relocatable_size_for_reloc>(
6297 needs_special_offset_handling,
6302 // Handle target specific gc actions when adding a gc reference from
6303 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6304 // and DST_OFF. For powerpc64, this adds a referenc to the code
6305 // section of a function descriptor.
6307 template<int size, bool big_endian>
6309 Target_powerpc<size, big_endian>::do_gc_add_reference(
6310 Symbol_table* symtab,
6312 unsigned int src_shndx,
6314 unsigned int dst_shndx,
6315 Address dst_off) const
6317 if (size != 64 || dst_obj->is_dynamic())
6320 Powerpc_relobj<size, big_endian>* ppc_object
6321 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
6322 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
6324 if (ppc_object->opd_valid())
6326 dst_shndx = ppc_object->get_opd_ent(dst_off);
6327 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
6331 // If we haven't run scan_opd_relocs, we must delay
6332 // processing this function descriptor reference.
6333 ppc_object->add_reference(src_obj, src_shndx, dst_off);
6338 // Add any special sections for this symbol to the gc work list.
6339 // For powerpc64, this adds the code section of a function
6342 template<int size, bool big_endian>
6344 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
6345 Symbol_table* symtab,
6350 Powerpc_relobj<size, big_endian>* ppc_object
6351 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
6353 unsigned int shndx = sym->shndx(&is_ordinary);
6354 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
6356 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
6357 Address dst_off = gsym->value();
6358 if (ppc_object->opd_valid())
6360 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6361 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
6364 ppc_object->add_gc_mark(dst_off);
6369 // For a symbol location in .opd, set LOC to the location of the
6372 template<int size, bool big_endian>
6374 Target_powerpc<size, big_endian>::do_function_location(
6375 Symbol_location* loc) const
6377 if (size == 64 && loc->shndx != 0)
6379 if (loc->object->is_dynamic())
6381 Powerpc_dynobj<size, big_endian>* ppc_object
6382 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
6383 if (loc->shndx == ppc_object->opd_shndx())
6386 Address off = loc->offset - ppc_object->opd_address();
6387 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
6388 loc->offset = dest_off;
6393 const Powerpc_relobj<size, big_endian>* ppc_object
6394 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
6395 if (loc->shndx == ppc_object->opd_shndx())
6398 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
6399 loc->offset = dest_off;
6405 // FNOFFSET in section SHNDX in OBJECT is the start of a function
6406 // compiled with -fsplit-stack. The function calls non-split-stack
6407 // code. Change the function to ensure it has enough stack space to
6408 // call some random function.
6410 template<int size, bool big_endian>
6412 Target_powerpc<size, big_endian>::do_calls_non_split(
6415 section_offset_type fnoffset,
6416 section_size_type fnsize,
6417 unsigned char* view,
6418 section_size_type view_size,
6420 std::string* to) const
6422 // 32-bit not supported.
6426 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
6427 view, view_size, from, to);
6431 // The function always starts with
6432 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
6433 // addis %r12,%r1,-allocate@ha
6434 // addi %r12,%r12,-allocate@l
6436 // but note that the addis or addi may be replaced with a nop
6438 unsigned char *entry = view + fnoffset;
6439 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
6441 if ((insn & 0xffff0000) == addis_2_12)
6443 /* Skip ELFv2 global entry code. */
6445 insn = elfcpp::Swap<32, big_endian>::readval(entry);
6448 unsigned char *pinsn = entry;
6450 const uint32_t ld_private_ss = 0xe80d8fc0;
6451 if (insn == ld_private_ss)
6453 int32_t allocate = 0;
6457 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
6458 if ((insn & 0xffff0000) == addis_12_1)
6459 allocate += (insn & 0xffff) << 16;
6460 else if ((insn & 0xffff0000) == addi_12_1
6461 || (insn & 0xffff0000) == addi_12_12)
6462 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
6463 else if (insn != nop)
6466 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
6468 int extra = parameters->options().split_stack_adjust_size();
6470 if (allocate >= 0 || extra < 0)
6472 object->error(_("split-stack stack size overflow at "
6473 "section %u offset %0zx"),
6474 shndx, static_cast<size_t>(fnoffset));
6478 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
6479 if (insn != addis_12_1)
6481 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
6483 insn = addi_12_12 | (allocate & 0xffff);
6484 if (insn != addi_12_12)
6486 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
6492 insn = addi_12_1 | (allocate & 0xffff);
6493 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
6496 if (pinsn != entry + 12)
6497 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
6505 if (!object->has_no_split_stack())
6506 object->error(_("failed to match split-stack sequence at "
6507 "section %u offset %0zx"),
6508 shndx, static_cast<size_t>(fnoffset));
6512 // Scan relocations for a section.
6514 template<int size, bool big_endian>
6516 Target_powerpc<size, big_endian>::scan_relocs(
6517 Symbol_table* symtab,
6519 Sized_relobj_file<size, big_endian>* object,
6520 unsigned int data_shndx,
6521 unsigned int sh_type,
6522 const unsigned char* prelocs,
6524 Output_section* output_section,
6525 bool needs_special_offset_handling,
6526 size_t local_symbol_count,
6527 const unsigned char* plocal_symbols)
6529 typedef Target_powerpc<size, big_endian> Powerpc;
6530 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6532 if (sh_type == elfcpp::SHT_REL)
6534 gold_error(_("%s: unsupported REL reloc section"),
6535 object->name().c_str());
6539 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
6548 needs_special_offset_handling,
6553 // Functor class for processing the global symbol table.
6554 // Removes symbols defined on discarded opd entries.
6556 template<bool big_endian>
6557 class Global_symbol_visitor_opd
6560 Global_symbol_visitor_opd()
6564 operator()(Sized_symbol<64>* sym)
6566 if (sym->has_symtab_index()
6567 || sym->source() != Symbol::FROM_OBJECT
6568 || !sym->in_real_elf())
6571 if (sym->object()->is_dynamic())
6574 Powerpc_relobj<64, big_endian>* symobj
6575 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6576 if (symobj->opd_shndx() == 0)
6580 unsigned int shndx = sym->shndx(&is_ordinary);
6581 if (shndx == symobj->opd_shndx()
6582 && symobj->get_opd_discard(sym->value()))
6584 sym->set_undefined();
6585 sym->set_visibility(elfcpp::STV_DEFAULT);
6586 sym->set_is_defined_in_discarded_section();
6587 sym->set_symtab_index(-1U);
6592 template<int size, bool big_endian>
6594 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6596 Symbol_table* symtab)
6600 Output_data_save_res<64, big_endian>* savres
6601 = new Output_data_save_res<64, big_endian>(symtab);
6602 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6603 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6604 savres, ORDER_TEXT, false);
6608 // Sort linker created .got section first (for the header), then input
6609 // sections belonging to files using small model code.
6611 template<bool big_endian>
6612 class Sort_toc_sections
6616 operator()(const Output_section::Input_section& is1,
6617 const Output_section::Input_section& is2) const
6619 if (!is1.is_input_section() && is2.is_input_section())
6622 = (is1.is_input_section()
6623 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6624 ->has_small_toc_reloc()));
6626 = (is2.is_input_section()
6627 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6628 ->has_small_toc_reloc()));
6629 return small1 && !small2;
6633 // Finalize the sections.
6635 template<int size, bool big_endian>
6637 Target_powerpc<size, big_endian>::do_finalize_sections(
6639 const Input_objects*,
6640 Symbol_table* symtab)
6642 if (parameters->doing_static_link())
6644 // At least some versions of glibc elf-init.o have a strong
6645 // reference to __rela_iplt marker syms. A weak ref would be
6647 if (this->iplt_ != NULL)
6649 Reloc_section* rel = this->iplt_->rel_plt();
6650 symtab->define_in_output_data("__rela_iplt_start", NULL,
6651 Symbol_table::PREDEFINED, rel, 0, 0,
6652 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6653 elfcpp::STV_HIDDEN, 0, false, true);
6654 symtab->define_in_output_data("__rela_iplt_end", NULL,
6655 Symbol_table::PREDEFINED, rel, 0, 0,
6656 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6657 elfcpp::STV_HIDDEN, 0, true, true);
6661 symtab->define_as_constant("__rela_iplt_start", NULL,
6662 Symbol_table::PREDEFINED, 0, 0,
6663 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6664 elfcpp::STV_HIDDEN, 0, true, false);
6665 symtab->define_as_constant("__rela_iplt_end", NULL,
6666 Symbol_table::PREDEFINED, 0, 0,
6667 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6668 elfcpp::STV_HIDDEN, 0, true, false);
6674 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6675 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6677 if (!parameters->options().relocatable())
6679 this->define_save_restore_funcs(layout, symtab);
6681 // Annoyingly, we need to make these sections now whether or
6682 // not we need them. If we delay until do_relax then we
6683 // need to mess with the relaxation machinery checkpointing.
6684 this->got_section(symtab, layout);
6685 this->make_brlt_section(layout);
6687 if (parameters->options().toc_sort())
6689 Output_section* os = this->got_->output_section();
6690 if (os != NULL && os->input_sections().size() > 1)
6691 std::stable_sort(os->input_sections().begin(),
6692 os->input_sections().end(),
6693 Sort_toc_sections<big_endian>());
6698 // Fill in some more dynamic tags.
6699 Output_data_dynamic* odyn = layout->dynamic_data();
6702 const Reloc_section* rel_plt = (this->plt_ == NULL
6704 : this->plt_->rel_plt());
6705 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6706 this->rela_dyn_, true, size == 32);
6710 if (this->got_ != NULL)
6712 this->got_->finalize_data_size();
6713 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6714 this->got_, this->got_->g_o_t());
6719 if (this->glink_ != NULL)
6721 this->glink_->finalize_data_size();
6722 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6724 (this->glink_->pltresolve_size
6730 // Emit any relocs we saved in an attempt to avoid generating COPY
6732 if (this->copy_relocs_.any_saved_relocs())
6733 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6736 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6740 ok_lo_toc_insn(uint32_t insn)
6742 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6743 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6744 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6745 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6746 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6747 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6748 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6749 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6750 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6751 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6752 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6753 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6754 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6755 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6756 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6758 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6759 && ((insn & 3) == 0 || (insn & 3) == 3))
6760 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6763 // Return the value to use for a branch relocation.
6765 template<int size, bool big_endian>
6767 Target_powerpc<size, big_endian>::symval_for_branch(
6768 const Symbol_table* symtab,
6769 const Sized_symbol<size>* gsym,
6770 Powerpc_relobj<size, big_endian>* object,
6772 unsigned int *dest_shndx)
6774 if (size == 32 || this->abiversion() >= 2)
6778 // If the symbol is defined in an opd section, ie. is a function
6779 // descriptor, use the function descriptor code entry address
6780 Powerpc_relobj<size, big_endian>* symobj = object;
6782 && gsym->source() != Symbol::FROM_OBJECT)
6785 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6786 unsigned int shndx = symobj->opd_shndx();
6789 Address opd_addr = symobj->get_output_section_offset(shndx);
6790 if (opd_addr == invalid_address)
6792 opd_addr += symobj->output_section_address(shndx);
6793 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
6796 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6797 if (symtab->is_section_folded(symobj, *dest_shndx))
6800 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6801 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6802 *dest_shndx = folded.second;
6804 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6805 if (sec_addr == invalid_address)
6808 sec_addr += symobj->output_section(*dest_shndx)->address();
6809 *value = sec_addr + sec_off;
6814 // Perform a relocation.
6816 template<int size, bool big_endian>
6818 Target_powerpc<size, big_endian>::Relocate::relocate(
6819 const Relocate_info<size, big_endian>* relinfo,
6820 Target_powerpc* target,
6823 const elfcpp::Rela<size, big_endian>& rela,
6824 unsigned int r_type,
6825 const Sized_symbol<size>* gsym,
6826 const Symbol_value<size>* psymval,
6827 unsigned char* view,
6829 section_size_type view_size)
6834 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6836 case Track_tls::NOT_EXPECTED:
6837 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6838 _("__tls_get_addr call lacks marker reloc"));
6840 case Track_tls::EXPECTED:
6841 // We have already complained.
6843 case Track_tls::SKIP:
6845 case Track_tls::NORMAL:
6849 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6850 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6851 Powerpc_relobj<size, big_endian>* const object
6852 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6854 bool has_stub_value = false;
6855 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6857 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
6858 : object->local_has_plt_offset(r_sym))
6859 && (!psymval->is_ifunc_symbol()
6860 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
6864 && target->abiversion() >= 2
6865 && !parameters->options().output_is_position_independent()
6866 && !is_branch_reloc(r_type))
6868 Address off = target->glink_section()->find_global_entry(gsym);
6869 if (off != invalid_address)
6871 value = target->glink_section()->global_entry_address() + off;
6872 has_stub_value = true;
6877 Stub_table<size, big_endian>* stub_table
6878 = object->stub_table(relinfo->data_shndx);
6879 if (stub_table == NULL)
6881 // This is a ref from a data section to an ifunc symbol.
6882 if (target->stub_tables().size() != 0)
6883 stub_table = target->stub_tables()[0];
6885 if (stub_table != NULL)
6889 off = stub_table->find_plt_call_entry(object, gsym, r_type,
6890 rela.get_r_addend());
6892 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6893 rela.get_r_addend());
6894 if (off != invalid_address)
6896 value = stub_table->stub_address() + off;
6897 has_stub_value = true;
6901 // We don't care too much about bogus debug references to
6902 // non-local functions, but otherwise there had better be a plt
6903 // call stub or global entry stub as appropriate.
6904 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
6907 if (r_type == elfcpp::R_POWERPC_GOT16
6908 || r_type == elfcpp::R_POWERPC_GOT16_LO
6909 || r_type == elfcpp::R_POWERPC_GOT16_HI
6910 || r_type == elfcpp::R_POWERPC_GOT16_HA
6911 || r_type == elfcpp::R_PPC64_GOT16_DS
6912 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6916 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6917 value = gsym->got_offset(GOT_TYPE_STANDARD);
6921 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6922 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6923 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6925 value -= target->got_section()->got_base_offset(object);
6927 else if (r_type == elfcpp::R_PPC64_TOC)
6929 value = (target->got_section()->output_section()->address()
6930 + object->toc_base_offset());
6932 else if (gsym != NULL
6933 && (r_type == elfcpp::R_POWERPC_REL24
6934 || r_type == elfcpp::R_PPC_PLTREL24)
6939 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6940 Valtype* wv = reinterpret_cast<Valtype*>(view);
6941 bool can_plt_call = false;
6942 if (rela.get_r_offset() + 8 <= view_size)
6944 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6945 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6948 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6950 elfcpp::Swap<32, big_endian>::
6951 writeval(wv + 1, ld_2_1 + target->stk_toc());
6952 can_plt_call = true;
6957 // If we don't have a branch and link followed by a nop,
6958 // we can't go via the plt because there is no place to
6959 // put a toc restoring instruction.
6960 // Unless we know we won't be returning.
6961 if (strcmp(gsym->name(), "__libc_start_main") == 0)
6962 can_plt_call = true;
6966 // g++ as of 20130507 emits self-calls without a
6967 // following nop. This is arguably wrong since we have
6968 // conflicting information. On the one hand a global
6969 // symbol and on the other a local call sequence, but
6970 // don't error for this special case.
6971 // It isn't possible to cheaply verify we have exactly
6972 // such a call. Allow all calls to the same section.
6974 Address code = value;
6975 if (gsym->source() == Symbol::FROM_OBJECT
6976 && gsym->object() == object)
6978 unsigned int dest_shndx = 0;
6979 if (target->abiversion() < 2)
6981 Address addend = rela.get_r_addend();
6982 code = psymval->value(object, addend);
6983 target->symval_for_branch(relinfo->symtab, gsym, object,
6984 &code, &dest_shndx);
6987 if (dest_shndx == 0)
6988 dest_shndx = gsym->shndx(&is_ordinary);
6989 ok = dest_shndx == relinfo->data_shndx;
6993 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6994 _("call lacks nop, can't restore toc; "
6995 "recompile with -fPIC"));
7001 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7002 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7003 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7004 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7006 // First instruction of a global dynamic sequence, arg setup insn.
7007 const bool final = gsym == NULL || gsym->final_value_is_known();
7008 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7009 enum Got_type got_type = GOT_TYPE_STANDARD;
7010 if (tls_type == tls::TLSOPT_NONE)
7011 got_type = GOT_TYPE_TLSGD;
7012 else if (tls_type == tls::TLSOPT_TO_IE)
7013 got_type = GOT_TYPE_TPREL;
7014 if (got_type != GOT_TYPE_STANDARD)
7018 gold_assert(gsym->has_got_offset(got_type));
7019 value = gsym->got_offset(got_type);
7023 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7024 gold_assert(object->local_has_got_offset(r_sym, got_type));
7025 value = object->local_got_offset(r_sym, got_type);
7027 value -= target->got_section()->got_base_offset(object);
7029 if (tls_type == tls::TLSOPT_TO_IE)
7031 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7032 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7034 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7035 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7036 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
7038 insn |= 32 << 26; // lwz
7040 insn |= 58 << 26; // ld
7041 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7043 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7044 - elfcpp::R_POWERPC_GOT_TLSGD16);
7046 else if (tls_type == tls::TLSOPT_TO_LE)
7048 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7049 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7051 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7052 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7053 insn &= (1 << 26) - (1 << 21); // extract rt
7058 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7059 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7060 value = psymval->value(object, rela.get_r_addend());
7064 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7066 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7067 r_type = elfcpp::R_POWERPC_NONE;
7071 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7072 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7073 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7074 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7076 // First instruction of a local dynamic sequence, arg setup insn.
7077 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7078 if (tls_type == tls::TLSOPT_NONE)
7080 value = target->tlsld_got_offset();
7081 value -= target->got_section()->got_base_offset(object);
7085 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7086 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7087 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7089 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7090 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7091 insn &= (1 << 26) - (1 << 21); // extract rt
7096 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7097 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7102 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7104 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7105 r_type = elfcpp::R_POWERPC_NONE;
7109 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
7110 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
7111 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
7112 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
7114 // Accesses relative to a local dynamic sequence address,
7115 // no optimisation here.
7118 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
7119 value = gsym->got_offset(GOT_TYPE_DTPREL);
7123 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7124 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
7125 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
7127 value -= target->got_section()->got_base_offset(object);
7129 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7130 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7131 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7132 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7134 // First instruction of initial exec sequence.
7135 const bool final = gsym == NULL || gsym->final_value_is_known();
7136 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7137 if (tls_type == tls::TLSOPT_NONE)
7141 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
7142 value = gsym->got_offset(GOT_TYPE_TPREL);
7146 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7147 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
7148 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
7150 value -= target->got_section()->got_base_offset(object);
7154 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7155 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7156 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7158 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7159 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7160 insn &= (1 << 26) - (1 << 21); // extract rt from ld
7165 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7166 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7167 value = psymval->value(object, rela.get_r_addend());
7171 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7173 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7174 r_type = elfcpp::R_POWERPC_NONE;
7178 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7179 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7181 // Second instruction of a global dynamic sequence,
7182 // the __tls_get_addr call
7183 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7184 const bool final = gsym == NULL || gsym->final_value_is_known();
7185 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7186 if (tls_type != tls::TLSOPT_NONE)
7188 if (tls_type == tls::TLSOPT_TO_IE)
7190 Insn* iview = reinterpret_cast<Insn*>(view);
7191 Insn insn = add_3_3_13;
7194 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7195 r_type = elfcpp::R_POWERPC_NONE;
7199 Insn* iview = reinterpret_cast<Insn*>(view);
7200 Insn insn = addi_3_3;
7201 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7202 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7203 view += 2 * big_endian;
7204 value = psymval->value(object, rela.get_r_addend());
7206 this->skip_next_tls_get_addr_call();
7209 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7210 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7212 // Second instruction of a local dynamic sequence,
7213 // the __tls_get_addr call
7214 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7215 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7216 if (tls_type == tls::TLSOPT_TO_LE)
7218 Insn* iview = reinterpret_cast<Insn*>(view);
7219 Insn insn = addi_3_3;
7220 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7221 this->skip_next_tls_get_addr_call();
7222 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7223 view += 2 * big_endian;
7227 else if (r_type == elfcpp::R_POWERPC_TLS)
7229 // Second instruction of an initial exec sequence
7230 const bool final = gsym == NULL || gsym->final_value_is_known();
7231 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7232 if (tls_type == tls::TLSOPT_TO_LE)
7234 Insn* iview = reinterpret_cast<Insn*>(view);
7235 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7236 unsigned int reg = size == 32 ? 2 : 13;
7237 insn = at_tls_transform(insn, reg);
7238 gold_assert(insn != 0);
7239 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7240 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7241 view += 2 * big_endian;
7242 value = psymval->value(object, rela.get_r_addend());
7245 else if (!has_stub_value)
7248 if (!(size == 32 && r_type == elfcpp::R_PPC_PLTREL24))
7249 addend = rela.get_r_addend();
7250 value = psymval->value(object, addend);
7251 if (size == 64 && is_branch_reloc(r_type))
7253 if (target->abiversion() >= 2)
7256 value += object->ppc64_local_entry_offset(gsym);
7258 value += object->ppc64_local_entry_offset(r_sym);
7262 unsigned int dest_shndx;
7263 target->symval_for_branch(relinfo->symtab, gsym, object,
7264 &value, &dest_shndx);
7267 Address max_branch_offset = 0;
7268 if (r_type == elfcpp::R_POWERPC_REL24
7269 || r_type == elfcpp::R_PPC_PLTREL24
7270 || r_type == elfcpp::R_PPC_LOCAL24PC)
7271 max_branch_offset = 1 << 25;
7272 else if (r_type == elfcpp::R_POWERPC_REL14
7273 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
7274 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
7275 max_branch_offset = 1 << 15;
7276 if (max_branch_offset != 0
7277 && value - address + max_branch_offset >= 2 * max_branch_offset)
7279 Stub_table<size, big_endian>* stub_table
7280 = object->stub_table(relinfo->data_shndx);
7281 if (stub_table != NULL)
7283 Address off = stub_table->find_long_branch_entry(object, value);
7284 if (off != invalid_address)
7286 value = (stub_table->stub_address() + stub_table->plt_size()
7288 has_stub_value = true;
7296 case elfcpp::R_PPC64_REL64:
7297 case elfcpp::R_POWERPC_REL32:
7298 case elfcpp::R_POWERPC_REL24:
7299 case elfcpp::R_PPC_PLTREL24:
7300 case elfcpp::R_PPC_LOCAL24PC:
7301 case elfcpp::R_POWERPC_REL16:
7302 case elfcpp::R_POWERPC_REL16_LO:
7303 case elfcpp::R_POWERPC_REL16_HI:
7304 case elfcpp::R_POWERPC_REL16_HA:
7305 case elfcpp::R_POWERPC_REL14:
7306 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7307 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7311 case elfcpp::R_PPC64_TOC16:
7312 case elfcpp::R_PPC64_TOC16_LO:
7313 case elfcpp::R_PPC64_TOC16_HI:
7314 case elfcpp::R_PPC64_TOC16_HA:
7315 case elfcpp::R_PPC64_TOC16_DS:
7316 case elfcpp::R_PPC64_TOC16_LO_DS:
7317 // Subtract the TOC base address.
7318 value -= (target->got_section()->output_section()->address()
7319 + object->toc_base_offset());
7322 case elfcpp::R_POWERPC_SECTOFF:
7323 case elfcpp::R_POWERPC_SECTOFF_LO:
7324 case elfcpp::R_POWERPC_SECTOFF_HI:
7325 case elfcpp::R_POWERPC_SECTOFF_HA:
7326 case elfcpp::R_PPC64_SECTOFF_DS:
7327 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7329 value -= os->address();
7332 case elfcpp::R_PPC64_TPREL16_DS:
7333 case elfcpp::R_PPC64_TPREL16_LO_DS:
7334 case elfcpp::R_PPC64_TPREL16_HIGH:
7335 case elfcpp::R_PPC64_TPREL16_HIGHA:
7337 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7339 case elfcpp::R_POWERPC_TPREL16:
7340 case elfcpp::R_POWERPC_TPREL16_LO:
7341 case elfcpp::R_POWERPC_TPREL16_HI:
7342 case elfcpp::R_POWERPC_TPREL16_HA:
7343 case elfcpp::R_POWERPC_TPREL:
7344 case elfcpp::R_PPC64_TPREL16_HIGHER:
7345 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7346 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7347 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7348 // tls symbol values are relative to tls_segment()->vaddr()
7352 case elfcpp::R_PPC64_DTPREL16_DS:
7353 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7354 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7355 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7356 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7357 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7359 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7360 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7362 case elfcpp::R_POWERPC_DTPREL16:
7363 case elfcpp::R_POWERPC_DTPREL16_LO:
7364 case elfcpp::R_POWERPC_DTPREL16_HI:
7365 case elfcpp::R_POWERPC_DTPREL16_HA:
7366 case elfcpp::R_POWERPC_DTPREL:
7367 case elfcpp::R_PPC64_DTPREL16_HIGH:
7368 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7369 // tls symbol values are relative to tls_segment()->vaddr()
7370 value -= dtp_offset;
7373 case elfcpp::R_PPC64_ADDR64_LOCAL:
7375 value += object->ppc64_local_entry_offset(gsym);
7377 value += object->ppc64_local_entry_offset(r_sym);
7384 Insn branch_bit = 0;
7387 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7388 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7389 branch_bit = 1 << 21;
7390 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7391 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7393 Insn* iview = reinterpret_cast<Insn*>(view);
7394 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7397 if (this->is_isa_v2)
7399 // Set 'a' bit. This is 0b00010 in BO field for branch
7400 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7401 // for branch on CTR insns (BO == 1a00t or 1a01t).
7402 if ((insn & (0x14 << 21)) == (0x04 << 21))
7404 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7411 // Invert 'y' bit if not the default.
7412 if (static_cast<Signed_address>(value) < 0)
7415 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7425 // Multi-instruction sequences that access the TOC can be
7426 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7427 // to nop; addi rb,r2,x;
7433 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7434 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7435 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7436 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7437 case elfcpp::R_POWERPC_GOT16_HA:
7438 case elfcpp::R_PPC64_TOC16_HA:
7439 if (parameters->options().toc_optimize())
7441 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7442 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7443 if ((insn & ((0x3f << 26) | 0x1f << 16))
7444 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7445 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7446 _("toc optimization is not supported "
7447 "for %#08x instruction"), insn);
7448 else if (value + 0x8000 < 0x10000)
7450 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
7456 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7457 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7458 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7459 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7460 case elfcpp::R_POWERPC_GOT16_LO:
7461 case elfcpp::R_PPC64_GOT16_LO_DS:
7462 case elfcpp::R_PPC64_TOC16_LO:
7463 case elfcpp::R_PPC64_TOC16_LO_DS:
7464 if (parameters->options().toc_optimize())
7466 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7467 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7468 if (!ok_lo_toc_insn(insn))
7469 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7470 _("toc optimization is not supported "
7471 "for %#08x instruction"), insn);
7472 else if (value + 0x8000 < 0x10000)
7474 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
7476 // Transform addic to addi when we change reg.
7477 insn &= ~((0x3f << 26) | (0x1f << 16));
7478 insn |= (14u << 26) | (2 << 16);
7482 insn &= ~(0x1f << 16);
7485 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7492 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
7493 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
7496 case elfcpp::R_POWERPC_ADDR32:
7497 case elfcpp::R_POWERPC_UADDR32:
7499 overflow = Reloc::CHECK_BITFIELD;
7502 case elfcpp::R_POWERPC_REL32:
7504 overflow = Reloc::CHECK_SIGNED;
7507 case elfcpp::R_POWERPC_UADDR16:
7508 overflow = Reloc::CHECK_BITFIELD;
7511 case elfcpp::R_POWERPC_ADDR16:
7512 // We really should have three separate relocations,
7513 // one for 16-bit data, one for insns with 16-bit signed fields,
7514 // and one for insns with 16-bit unsigned fields.
7515 overflow = Reloc::CHECK_BITFIELD;
7516 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
7517 overflow = Reloc::CHECK_LOW_INSN;
7520 case elfcpp::R_POWERPC_ADDR16_HI:
7521 case elfcpp::R_POWERPC_ADDR16_HA:
7522 case elfcpp::R_POWERPC_GOT16_HI:
7523 case elfcpp::R_POWERPC_GOT16_HA:
7524 case elfcpp::R_POWERPC_PLT16_HI:
7525 case elfcpp::R_POWERPC_PLT16_HA:
7526 case elfcpp::R_POWERPC_SECTOFF_HI:
7527 case elfcpp::R_POWERPC_SECTOFF_HA:
7528 case elfcpp::R_PPC64_TOC16_HI:
7529 case elfcpp::R_PPC64_TOC16_HA:
7530 case elfcpp::R_PPC64_PLTGOT16_HI:
7531 case elfcpp::R_PPC64_PLTGOT16_HA:
7532 case elfcpp::R_POWERPC_TPREL16_HI:
7533 case elfcpp::R_POWERPC_TPREL16_HA:
7534 case elfcpp::R_POWERPC_DTPREL16_HI:
7535 case elfcpp::R_POWERPC_DTPREL16_HA:
7536 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7537 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7538 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7539 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7540 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7541 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7542 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7543 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7544 case elfcpp::R_POWERPC_REL16_HI:
7545 case elfcpp::R_POWERPC_REL16_HA:
7547 overflow = Reloc::CHECK_HIGH_INSN;
7550 case elfcpp::R_POWERPC_REL16:
7551 case elfcpp::R_PPC64_TOC16:
7552 case elfcpp::R_POWERPC_GOT16:
7553 case elfcpp::R_POWERPC_SECTOFF:
7554 case elfcpp::R_POWERPC_TPREL16:
7555 case elfcpp::R_POWERPC_DTPREL16:
7556 case elfcpp::R_POWERPC_GOT_TLSGD16:
7557 case elfcpp::R_POWERPC_GOT_TLSLD16:
7558 case elfcpp::R_POWERPC_GOT_TPREL16:
7559 case elfcpp::R_POWERPC_GOT_DTPREL16:
7560 overflow = Reloc::CHECK_LOW_INSN;
7563 case elfcpp::R_POWERPC_ADDR24:
7564 case elfcpp::R_POWERPC_ADDR14:
7565 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7566 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7567 case elfcpp::R_PPC64_ADDR16_DS:
7568 case elfcpp::R_POWERPC_REL24:
7569 case elfcpp::R_PPC_PLTREL24:
7570 case elfcpp::R_PPC_LOCAL24PC:
7571 case elfcpp::R_PPC64_TPREL16_DS:
7572 case elfcpp::R_PPC64_DTPREL16_DS:
7573 case elfcpp::R_PPC64_TOC16_DS:
7574 case elfcpp::R_PPC64_GOT16_DS:
7575 case elfcpp::R_PPC64_SECTOFF_DS:
7576 case elfcpp::R_POWERPC_REL14:
7577 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7578 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7579 overflow = Reloc::CHECK_SIGNED;
7583 if (overflow == Reloc::CHECK_LOW_INSN
7584 || overflow == Reloc::CHECK_HIGH_INSN)
7586 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7587 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7589 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
7590 overflow = Reloc::CHECK_BITFIELD;
7591 else if (overflow == Reloc::CHECK_LOW_INSN
7592 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
7593 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
7594 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
7595 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
7596 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
7597 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
7598 overflow = Reloc::CHECK_UNSIGNED;
7600 overflow = Reloc::CHECK_SIGNED;
7603 typename Powerpc_relocate_functions<size, big_endian>::Status status
7604 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
7607 case elfcpp::R_POWERPC_NONE:
7608 case elfcpp::R_POWERPC_TLS:
7609 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7610 case elfcpp::R_POWERPC_GNU_VTENTRY:
7613 case elfcpp::R_PPC64_ADDR64:
7614 case elfcpp::R_PPC64_REL64:
7615 case elfcpp::R_PPC64_TOC:
7616 case elfcpp::R_PPC64_ADDR64_LOCAL:
7617 Reloc::addr64(view, value);
7620 case elfcpp::R_POWERPC_TPREL:
7621 case elfcpp::R_POWERPC_DTPREL:
7623 Reloc::addr64(view, value);
7625 status = Reloc::addr32(view, value, overflow);
7628 case elfcpp::R_PPC64_UADDR64:
7629 Reloc::addr64_u(view, value);
7632 case elfcpp::R_POWERPC_ADDR32:
7633 status = Reloc::addr32(view, value, overflow);
7636 case elfcpp::R_POWERPC_REL32:
7637 case elfcpp::R_POWERPC_UADDR32:
7638 status = Reloc::addr32_u(view, value, overflow);
7641 case elfcpp::R_POWERPC_ADDR24:
7642 case elfcpp::R_POWERPC_REL24:
7643 case elfcpp::R_PPC_PLTREL24:
7644 case elfcpp::R_PPC_LOCAL24PC:
7645 status = Reloc::addr24(view, value, overflow);
7648 case elfcpp::R_POWERPC_GOT_DTPREL16:
7649 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7650 case elfcpp::R_POWERPC_GOT_TPREL16:
7651 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7654 // On ppc64 these are all ds form
7655 status = Reloc::addr16_ds(view, value, overflow);
7658 case elfcpp::R_POWERPC_ADDR16:
7659 case elfcpp::R_POWERPC_REL16:
7660 case elfcpp::R_PPC64_TOC16:
7661 case elfcpp::R_POWERPC_GOT16:
7662 case elfcpp::R_POWERPC_SECTOFF:
7663 case elfcpp::R_POWERPC_TPREL16:
7664 case elfcpp::R_POWERPC_DTPREL16:
7665 case elfcpp::R_POWERPC_GOT_TLSGD16:
7666 case elfcpp::R_POWERPC_GOT_TLSLD16:
7667 case elfcpp::R_POWERPC_ADDR16_LO:
7668 case elfcpp::R_POWERPC_REL16_LO:
7669 case elfcpp::R_PPC64_TOC16_LO:
7670 case elfcpp::R_POWERPC_GOT16_LO:
7671 case elfcpp::R_POWERPC_SECTOFF_LO:
7672 case elfcpp::R_POWERPC_TPREL16_LO:
7673 case elfcpp::R_POWERPC_DTPREL16_LO:
7674 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7675 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7676 status = Reloc::addr16(view, value, overflow);
7679 case elfcpp::R_POWERPC_UADDR16:
7680 status = Reloc::addr16_u(view, value, overflow);
7683 case elfcpp::R_PPC64_ADDR16_HIGH:
7684 case elfcpp::R_PPC64_TPREL16_HIGH:
7685 case elfcpp::R_PPC64_DTPREL16_HIGH:
7687 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7689 case elfcpp::R_POWERPC_ADDR16_HI:
7690 case elfcpp::R_POWERPC_REL16_HI:
7691 case elfcpp::R_PPC64_TOC16_HI:
7692 case elfcpp::R_POWERPC_GOT16_HI:
7693 case elfcpp::R_POWERPC_SECTOFF_HI:
7694 case elfcpp::R_POWERPC_TPREL16_HI:
7695 case elfcpp::R_POWERPC_DTPREL16_HI:
7696 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7697 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7698 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7699 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7700 Reloc::addr16_hi(view, value);
7703 case elfcpp::R_PPC64_ADDR16_HIGHA:
7704 case elfcpp::R_PPC64_TPREL16_HIGHA:
7705 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7707 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7709 case elfcpp::R_POWERPC_ADDR16_HA:
7710 case elfcpp::R_POWERPC_REL16_HA:
7711 case elfcpp::R_PPC64_TOC16_HA:
7712 case elfcpp::R_POWERPC_GOT16_HA:
7713 case elfcpp::R_POWERPC_SECTOFF_HA:
7714 case elfcpp::R_POWERPC_TPREL16_HA:
7715 case elfcpp::R_POWERPC_DTPREL16_HA:
7716 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7717 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7718 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7719 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7720 Reloc::addr16_ha(view, value);
7723 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7725 // R_PPC_EMB_NADDR16_LO
7727 case elfcpp::R_PPC64_ADDR16_HIGHER:
7728 case elfcpp::R_PPC64_TPREL16_HIGHER:
7729 Reloc::addr16_hi2(view, value);
7732 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7734 // R_PPC_EMB_NADDR16_HI
7736 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7737 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7738 Reloc::addr16_ha2(view, value);
7741 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7743 // R_PPC_EMB_NADDR16_HA
7745 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7746 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7747 Reloc::addr16_hi3(view, value);
7750 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7754 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7755 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7756 Reloc::addr16_ha3(view, value);
7759 case elfcpp::R_PPC64_DTPREL16_DS:
7760 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7762 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7764 case elfcpp::R_PPC64_TPREL16_DS:
7765 case elfcpp::R_PPC64_TPREL16_LO_DS:
7767 // R_PPC_TLSGD, R_PPC_TLSLD
7769 case elfcpp::R_PPC64_ADDR16_DS:
7770 case elfcpp::R_PPC64_ADDR16_LO_DS:
7771 case elfcpp::R_PPC64_TOC16_DS:
7772 case elfcpp::R_PPC64_TOC16_LO_DS:
7773 case elfcpp::R_PPC64_GOT16_DS:
7774 case elfcpp::R_PPC64_GOT16_LO_DS:
7775 case elfcpp::R_PPC64_SECTOFF_DS:
7776 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7777 status = Reloc::addr16_ds(view, value, overflow);
7780 case elfcpp::R_POWERPC_ADDR14:
7781 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7782 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7783 case elfcpp::R_POWERPC_REL14:
7784 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7785 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7786 status = Reloc::addr14(view, value, overflow);
7789 case elfcpp::R_POWERPC_COPY:
7790 case elfcpp::R_POWERPC_GLOB_DAT:
7791 case elfcpp::R_POWERPC_JMP_SLOT:
7792 case elfcpp::R_POWERPC_RELATIVE:
7793 case elfcpp::R_POWERPC_DTPMOD:
7794 case elfcpp::R_PPC64_JMP_IREL:
7795 case elfcpp::R_POWERPC_IRELATIVE:
7796 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7797 _("unexpected reloc %u in object file"),
7801 case elfcpp::R_PPC_EMB_SDA21:
7806 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7810 case elfcpp::R_PPC_EMB_SDA2I16:
7811 case elfcpp::R_PPC_EMB_SDA2REL:
7814 // R_PPC64_TLSGD, R_PPC64_TLSLD
7817 case elfcpp::R_POWERPC_PLT32:
7818 case elfcpp::R_POWERPC_PLTREL32:
7819 case elfcpp::R_POWERPC_PLT16_LO:
7820 case elfcpp::R_POWERPC_PLT16_HI:
7821 case elfcpp::R_POWERPC_PLT16_HA:
7822 case elfcpp::R_PPC_SDAREL16:
7823 case elfcpp::R_POWERPC_ADDR30:
7824 case elfcpp::R_PPC64_PLT64:
7825 case elfcpp::R_PPC64_PLTREL64:
7826 case elfcpp::R_PPC64_PLTGOT16:
7827 case elfcpp::R_PPC64_PLTGOT16_LO:
7828 case elfcpp::R_PPC64_PLTGOT16_HI:
7829 case elfcpp::R_PPC64_PLTGOT16_HA:
7830 case elfcpp::R_PPC64_PLT16_LO_DS:
7831 case elfcpp::R_PPC64_PLTGOT16_DS:
7832 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7833 case elfcpp::R_PPC_EMB_RELSDA:
7834 case elfcpp::R_PPC_TOC16:
7837 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7838 _("unsupported reloc %u"),
7842 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
7845 && gsym->is_undefined()
7846 && is_branch_reloc(r_type))))
7848 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7849 _("relocation overflow"));
7851 gold_info(_("try relinking with a smaller --stub-group-size"));
7857 // Relocate section data.
7859 template<int size, bool big_endian>
7861 Target_powerpc<size, big_endian>::relocate_section(
7862 const Relocate_info<size, big_endian>* relinfo,
7863 unsigned int sh_type,
7864 const unsigned char* prelocs,
7866 Output_section* output_section,
7867 bool needs_special_offset_handling,
7868 unsigned char* view,
7870 section_size_type view_size,
7871 const Reloc_symbol_changes* reloc_symbol_changes)
7873 typedef Target_powerpc<size, big_endian> Powerpc;
7874 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7875 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7876 Powerpc_comdat_behavior;
7878 gold_assert(sh_type == elfcpp::SHT_RELA);
7880 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7881 Powerpc_relocate, Powerpc_comdat_behavior>(
7887 needs_special_offset_handling,
7891 reloc_symbol_changes);
7894 class Powerpc_scan_relocatable_reloc
7897 // Return the strategy to use for a local symbol which is not a
7898 // section symbol, given the relocation type.
7899 inline Relocatable_relocs::Reloc_strategy
7900 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7902 if (r_type == 0 && r_sym == 0)
7903 return Relocatable_relocs::RELOC_DISCARD;
7904 return Relocatable_relocs::RELOC_COPY;
7907 // Return the strategy to use for a local symbol which is a section
7908 // symbol, given the relocation type.
7909 inline Relocatable_relocs::Reloc_strategy
7910 local_section_strategy(unsigned int, Relobj*)
7912 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7915 // Return the strategy to use for a global symbol, given the
7916 // relocation type, the object, and the symbol index.
7917 inline Relocatable_relocs::Reloc_strategy
7918 global_strategy(unsigned int r_type, Relobj*, unsigned int)
7920 if (r_type == elfcpp::R_PPC_PLTREL24)
7921 return Relocatable_relocs::RELOC_SPECIAL;
7922 return Relocatable_relocs::RELOC_COPY;
7926 // Scan the relocs during a relocatable link.
7928 template<int size, bool big_endian>
7930 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7931 Symbol_table* symtab,
7933 Sized_relobj_file<size, big_endian>* object,
7934 unsigned int data_shndx,
7935 unsigned int sh_type,
7936 const unsigned char* prelocs,
7938 Output_section* output_section,
7939 bool needs_special_offset_handling,
7940 size_t local_symbol_count,
7941 const unsigned char* plocal_symbols,
7942 Relocatable_relocs* rr)
7944 gold_assert(sh_type == elfcpp::SHT_RELA);
7946 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7947 Powerpc_scan_relocatable_reloc>(
7955 needs_special_offset_handling,
7961 // Emit relocations for a section.
7962 // This is a modified version of the function by the same name in
7963 // target-reloc.h. Using relocate_special_relocatable for
7964 // R_PPC_PLTREL24 would require duplication of the entire body of the
7965 // loop, so we may as well duplicate the whole thing.
7967 template<int size, bool big_endian>
7969 Target_powerpc<size, big_endian>::relocate_relocs(
7970 const Relocate_info<size, big_endian>* relinfo,
7971 unsigned int sh_type,
7972 const unsigned char* prelocs,
7974 Output_section* output_section,
7975 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7976 const Relocatable_relocs* rr,
7978 Address view_address,
7980 unsigned char* reloc_view,
7981 section_size_type reloc_view_size)
7983 gold_assert(sh_type == elfcpp::SHT_RELA);
7985 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7987 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7989 const int reloc_size
7990 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7992 Powerpc_relobj<size, big_endian>* const object
7993 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7994 const unsigned int local_count = object->local_symbol_count();
7995 unsigned int got2_shndx = object->got2_shndx();
7996 Address got2_addend = 0;
7997 if (got2_shndx != 0)
7999 got2_addend = object->get_output_section_offset(got2_shndx);
8000 gold_assert(got2_addend != invalid_address);
8003 unsigned char* pwrite = reloc_view;
8004 bool zap_next = false;
8005 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
8007 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
8008 if (strategy == Relocatable_relocs::RELOC_DISCARD)
8011 Reltype reloc(prelocs);
8012 Reltype_write reloc_write(pwrite);
8014 Address offset = reloc.get_r_offset();
8015 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
8016 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
8017 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
8018 const unsigned int orig_r_sym = r_sym;
8019 typename elfcpp::Elf_types<size>::Elf_Swxword addend
8020 = reloc.get_r_addend();
8021 const Symbol* gsym = NULL;
8025 // We could arrange to discard these and other relocs for
8026 // tls optimised sequences in the strategy methods, but for
8027 // now do as BFD ld does.
8028 r_type = elfcpp::R_POWERPC_NONE;
8032 // Get the new symbol index.
8033 if (r_sym < local_count)
8037 case Relocatable_relocs::RELOC_COPY:
8038 case Relocatable_relocs::RELOC_SPECIAL:
8041 r_sym = object->symtab_index(r_sym);
8042 gold_assert(r_sym != -1U);
8046 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
8048 // We are adjusting a section symbol. We need to find
8049 // the symbol table index of the section symbol for
8050 // the output section corresponding to input section
8051 // in which this symbol is defined.
8052 gold_assert(r_sym < local_count);
8054 unsigned int shndx =
8055 object->local_symbol_input_shndx(r_sym, &is_ordinary);
8056 gold_assert(is_ordinary);
8057 Output_section* os = object->output_section(shndx);
8058 gold_assert(os != NULL);
8059 gold_assert(os->needs_symtab_index());
8060 r_sym = os->symtab_index();
8070 gsym = object->global_symbol(r_sym);
8071 gold_assert(gsym != NULL);
8072 if (gsym->is_forwarder())
8073 gsym = relinfo->symtab->resolve_forwards(gsym);
8075 gold_assert(gsym->has_symtab_index());
8076 r_sym = gsym->symtab_index();
8079 // Get the new offset--the location in the output section where
8080 // this relocation should be applied.
8081 if (static_cast<Address>(offset_in_output_section) != invalid_address)
8082 offset += offset_in_output_section;
8085 section_offset_type sot_offset =
8086 convert_types<section_offset_type, Address>(offset);
8087 section_offset_type new_sot_offset =
8088 output_section->output_offset(object, relinfo->data_shndx,
8090 gold_assert(new_sot_offset != -1);
8091 offset = new_sot_offset;
8094 // In an object file, r_offset is an offset within the section.
8095 // In an executable or dynamic object, generated by
8096 // --emit-relocs, r_offset is an absolute address.
8097 if (!parameters->options().relocatable())
8099 offset += view_address;
8100 if (static_cast<Address>(offset_in_output_section) != invalid_address)
8101 offset -= offset_in_output_section;
8104 // Handle the reloc addend based on the strategy.
8105 if (strategy == Relocatable_relocs::RELOC_COPY)
8107 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
8109 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
8110 addend = psymval->value(object, addend);
8112 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
8114 if (addend >= 32768)
8115 addend += got2_addend;
8120 if (!parameters->options().relocatable())
8122 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8123 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
8124 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
8125 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
8127 // First instruction of a global dynamic sequence,
8129 const bool final = gsym == NULL || gsym->final_value_is_known();
8130 switch (this->optimize_tls_gd(final))
8132 case tls::TLSOPT_TO_IE:
8133 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
8134 - elfcpp::R_POWERPC_GOT_TLSGD16);
8136 case tls::TLSOPT_TO_LE:
8137 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8138 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8139 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8142 r_type = elfcpp::R_POWERPC_NONE;
8143 offset -= 2 * big_endian;
8150 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8151 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8152 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8153 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8155 // First instruction of a local dynamic sequence,
8157 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8159 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8160 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8162 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8163 const Output_section* os = relinfo->layout->tls_segment()
8165 gold_assert(os != NULL);
8166 gold_assert(os->needs_symtab_index());
8167 r_sym = os->symtab_index();
8168 addend = dtp_offset;
8172 r_type = elfcpp::R_POWERPC_NONE;
8173 offset -= 2 * big_endian;
8177 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8178 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8179 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8180 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8182 // First instruction of initial exec sequence.
8183 const bool final = gsym == NULL || gsym->final_value_is_known();
8184 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8186 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8187 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8188 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8191 r_type = elfcpp::R_POWERPC_NONE;
8192 offset -= 2 * big_endian;
8196 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8197 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8199 // Second instruction of a global dynamic sequence,
8200 // the __tls_get_addr call
8201 const bool final = gsym == NULL || gsym->final_value_is_known();
8202 switch (this->optimize_tls_gd(final))
8204 case tls::TLSOPT_TO_IE:
8205 r_type = elfcpp::R_POWERPC_NONE;
8208 case tls::TLSOPT_TO_LE:
8209 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8210 offset += 2 * big_endian;
8217 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8218 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8220 // Second instruction of a local dynamic sequence,
8221 // the __tls_get_addr call
8222 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8224 const Output_section* os = relinfo->layout->tls_segment()
8226 gold_assert(os != NULL);
8227 gold_assert(os->needs_symtab_index());
8228 r_sym = os->symtab_index();
8229 addend = dtp_offset;
8230 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8231 offset += 2 * big_endian;
8235 else if (r_type == elfcpp::R_POWERPC_TLS)
8237 // Second instruction of an initial exec sequence
8238 const bool final = gsym == NULL || gsym->final_value_is_known();
8239 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8241 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8242 offset += 2 * big_endian;
8247 reloc_write.put_r_offset(offset);
8248 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
8249 reloc_write.put_r_addend(addend);
8251 pwrite += reloc_size;
8254 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
8255 == reloc_view_size);
8258 // Return the value to use for a dynamic symbol which requires special
8259 // treatment. This is how we support equality comparisons of function
8260 // pointers across shared library boundaries, as described in the
8261 // processor specific ABI supplement.
8263 template<int size, bool big_endian>
8265 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8269 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8270 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8271 p != this->stub_tables_.end();
8274 Address off = (*p)->find_plt_call_entry(gsym);
8275 if (off != invalid_address)
8276 return (*p)->stub_address() + off;
8279 else if (this->abiversion() >= 2)
8281 Address off = this->glink_section()->find_global_entry(gsym);
8282 if (off != invalid_address)
8283 return this->glink_section()->global_entry_address() + off;
8288 // Return the PLT address to use for a local symbol.
8289 template<int size, bool big_endian>
8291 Target_powerpc<size, big_endian>::do_plt_address_for_local(
8292 const Relobj* object,
8293 unsigned int symndx) const
8297 const Sized_relobj<size, big_endian>* relobj
8298 = static_cast<const Sized_relobj<size, big_endian>*>(object);
8299 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8300 p != this->stub_tables_.end();
8303 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
8305 if (off != invalid_address)
8306 return (*p)->stub_address() + off;
8312 // Return the PLT address to use for a global symbol.
8313 template<int size, bool big_endian>
8315 Target_powerpc<size, big_endian>::do_plt_address_for_global(
8316 const Symbol* gsym) const
8320 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8321 p != this->stub_tables_.end();
8324 Address off = (*p)->find_plt_call_entry(gsym);
8325 if (off != invalid_address)
8326 return (*p)->stub_address() + off;
8329 else if (this->abiversion() >= 2)
8331 Address off = this->glink_section()->find_global_entry(gsym);
8332 if (off != invalid_address)
8333 return this->glink_section()->global_entry_address() + off;
8338 // Return the offset to use for the GOT_INDX'th got entry which is
8339 // for a local tls symbol specified by OBJECT, SYMNDX.
8340 template<int size, bool big_endian>
8342 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
8343 const Relobj* object,
8344 unsigned int symndx,
8345 unsigned int got_indx) const
8347 const Powerpc_relobj<size, big_endian>* ppc_object
8348 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
8349 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
8351 for (Got_type got_type = GOT_TYPE_TLSGD;
8352 got_type <= GOT_TYPE_TPREL;
8353 got_type = Got_type(got_type + 1))
8354 if (ppc_object->local_has_got_offset(symndx, got_type))
8356 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
8357 if (got_type == GOT_TYPE_TLSGD)
8359 if (off == got_indx * (size / 8))
8361 if (got_type == GOT_TYPE_TPREL)
8371 // Return the offset to use for the GOT_INDX'th got entry which is
8372 // for global tls symbol GSYM.
8373 template<int size, bool big_endian>
8375 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
8377 unsigned int got_indx) const
8379 if (gsym->type() == elfcpp::STT_TLS)
8381 for (Got_type got_type = GOT_TYPE_TLSGD;
8382 got_type <= GOT_TYPE_TPREL;
8383 got_type = Got_type(got_type + 1))
8384 if (gsym->has_got_offset(got_type))
8386 unsigned int off = gsym->got_offset(got_type);
8387 if (got_type == GOT_TYPE_TLSGD)
8389 if (off == got_indx * (size / 8))
8391 if (got_type == GOT_TYPE_TPREL)
8401 // The selector for powerpc object files.
8403 template<int size, bool big_endian>
8404 class Target_selector_powerpc : public Target_selector
8407 Target_selector_powerpc()
8408 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
8411 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
8412 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
8414 ? (big_endian ? "elf64ppc" : "elf64lppc")
8415 : (big_endian ? "elf32ppc" : "elf32lppc")))
8419 do_instantiate_target()
8420 { return new Target_powerpc<size, big_endian>(); }
8423 Target_selector_powerpc<32, true> target_selector_ppc32;
8424 Target_selector_powerpc<32, false> target_selector_ppc32le;
8425 Target_selector_powerpc<64, true> target_selector_ppc64;
8426 Target_selector_powerpc<64, false> target_selector_ppc64le;
8428 // Instantiate these constants for -O0
8429 template<int size, bool big_endian>
8430 const int Output_data_glink<size, big_endian>::pltresolve_size;
8431 template<int size, bool big_endian>
8432 const typename Output_data_glink<size, big_endian>::Address
8433 Output_data_glink<size, big_endian>::invalid_address;
8434 template<int size, bool big_endian>
8435 const typename Stub_table<size, big_endian>::Address
8436 Stub_table<size, big_endian>::invalid_address;
8437 template<int size, bool big_endian>
8438 const typename Target_powerpc<size, big_endian>::Address
8439 Target_powerpc<size, big_endian>::invalid_address;
8441 } // End anonymous namespace.