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 // Relocate a section.
609 relocate_section(const Relocate_info<size, big_endian>*,
610 unsigned int sh_type,
611 const unsigned char* prelocs,
613 Output_section* output_section,
614 bool needs_special_offset_handling,
616 Address view_address,
617 section_size_type view_size,
618 const Reloc_symbol_changes*);
620 // Scan the relocs during a relocatable link.
622 scan_relocatable_relocs(Symbol_table* symtab,
624 Sized_relobj_file<size, big_endian>* object,
625 unsigned int data_shndx,
626 unsigned int sh_type,
627 const unsigned char* prelocs,
629 Output_section* output_section,
630 bool needs_special_offset_handling,
631 size_t local_symbol_count,
632 const unsigned char* plocal_symbols,
633 Relocatable_relocs*);
635 // Emit relocations for a section.
637 relocate_relocs(const Relocate_info<size, big_endian>*,
638 unsigned int sh_type,
639 const unsigned char* prelocs,
641 Output_section* output_section,
642 typename elfcpp::Elf_types<size>::Elf_Off
643 offset_in_output_section,
644 const Relocatable_relocs*,
646 Address view_address,
648 unsigned char* reloc_view,
649 section_size_type reloc_view_size);
651 // Return whether SYM is defined by the ABI.
653 do_is_defined_by_abi(const Symbol* sym) const
655 return strcmp(sym->name(), "__tls_get_addr") == 0;
658 // Return the size of the GOT section.
662 gold_assert(this->got_ != NULL);
663 return this->got_->data_size();
666 // Get the PLT section.
667 const Output_data_plt_powerpc<size, big_endian>*
670 gold_assert(this->plt_ != NULL);
674 // Get the IPLT section.
675 const Output_data_plt_powerpc<size, big_endian>*
678 gold_assert(this->iplt_ != NULL);
682 // Get the .glink section.
683 const Output_data_glink<size, big_endian>*
684 glink_section() const
686 gold_assert(this->glink_ != NULL);
690 Output_data_glink<size, big_endian>*
693 gold_assert(this->glink_ != NULL);
697 bool has_glink() const
698 { return this->glink_ != NULL; }
700 // Get the GOT section.
701 const Output_data_got_powerpc<size, big_endian>*
704 gold_assert(this->got_ != NULL);
708 // Get the GOT section, creating it if necessary.
709 Output_data_got_powerpc<size, big_endian>*
710 got_section(Symbol_table*, Layout*);
713 do_make_elf_object(const std::string&, Input_file*, off_t,
714 const elfcpp::Ehdr<size, big_endian>&);
716 // Return the number of entries in the GOT.
718 got_entry_count() const
720 if (this->got_ == NULL)
722 return this->got_size() / (size / 8);
725 // Return the number of entries in the PLT.
727 plt_entry_count() const;
729 // Return the offset of the first non-reserved PLT entry.
731 first_plt_entry_offset() const
735 if (this->abiversion() >= 2)
740 // Return the size of each PLT entry.
742 plt_entry_size() const
746 if (this->abiversion() >= 2)
751 // Add any special sections for this symbol to the gc work list.
752 // For powerpc64, this adds the code section of a function
755 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
757 // Handle target specific gc actions when adding a gc reference from
758 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
759 // and DST_OFF. For powerpc64, this adds a referenc to the code
760 // section of a function descriptor.
762 do_gc_add_reference(Symbol_table* symtab,
764 unsigned int src_shndx,
766 unsigned int dst_shndx,
767 Address dst_off) const;
769 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
772 { return this->stub_tables_; }
774 const Output_data_brlt_powerpc<size, big_endian>*
776 { return this->brlt_section_; }
779 add_branch_lookup_table(Address to)
781 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
782 this->branch_lookup_table_.insert(std::make_pair(to, off));
786 find_branch_lookup_table(Address to)
788 typename Branch_lookup_table::const_iterator p
789 = this->branch_lookup_table_.find(to);
790 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
794 write_branch_lookup_table(unsigned char *oview)
796 for (typename Branch_lookup_table::const_iterator p
797 = this->branch_lookup_table_.begin();
798 p != this->branch_lookup_table_.end();
801 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
806 plt_thread_safe() const
807 { return this->plt_thread_safe_; }
811 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
814 set_abiversion (int ver)
816 elfcpp::Elf_Word flags = this->processor_specific_flags();
817 flags &= ~elfcpp::EF_PPC64_ABI;
818 flags |= ver & elfcpp::EF_PPC64_ABI;
819 this->set_processor_specific_flags(flags);
822 // Offset to to save stack slot
825 { return this->abiversion() < 2 ? 40 : 24; }
841 : tls_get_addr_(NOT_EXPECTED),
842 relinfo_(NULL), relnum_(0), r_offset_(0)
847 if (this->tls_get_addr_ != NOT_EXPECTED)
854 if (this->relinfo_ != NULL)
855 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
856 _("missing expected __tls_get_addr call"));
860 expect_tls_get_addr_call(
861 const Relocate_info<size, big_endian>* relinfo,
865 this->tls_get_addr_ = EXPECTED;
866 this->relinfo_ = relinfo;
867 this->relnum_ = relnum;
868 this->r_offset_ = r_offset;
872 expect_tls_get_addr_call()
873 { this->tls_get_addr_ = EXPECTED; }
876 skip_next_tls_get_addr_call()
877 {this->tls_get_addr_ = SKIP; }
880 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
882 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
883 || r_type == elfcpp::R_PPC_PLTREL24)
885 && strcmp(gsym->name(), "__tls_get_addr") == 0);
886 Tls_get_addr last_tls = this->tls_get_addr_;
887 this->tls_get_addr_ = NOT_EXPECTED;
888 if (is_tls_call && last_tls != EXPECTED)
890 else if (!is_tls_call && last_tls != NOT_EXPECTED)
899 // What we're up to regarding calls to __tls_get_addr.
900 // On powerpc, the branch and link insn making a call to
901 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
902 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
903 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
904 // The marker relocation always comes first, and has the same
905 // symbol as the reloc on the insn setting up the __tls_get_addr
906 // argument. This ties the arg setup insn with the call insn,
907 // allowing ld to safely optimize away the call. We check that
908 // every call to __tls_get_addr has a marker relocation, and that
909 // every marker relocation is on a call to __tls_get_addr.
910 Tls_get_addr tls_get_addr_;
911 // Info about the last reloc for error message.
912 const Relocate_info<size, big_endian>* relinfo_;
917 // The class which scans relocations.
918 class Scan : protected Track_tls
921 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
924 : Track_tls(), issued_non_pic_error_(false)
928 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
931 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
932 Sized_relobj_file<size, big_endian>* object,
933 unsigned int data_shndx,
934 Output_section* output_section,
935 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
936 const elfcpp::Sym<size, big_endian>& lsym,
940 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
941 Sized_relobj_file<size, big_endian>* object,
942 unsigned int data_shndx,
943 Output_section* output_section,
944 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
948 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
950 Sized_relobj_file<size, big_endian>* relobj,
953 const elfcpp::Rela<size, big_endian>& ,
955 const elfcpp::Sym<size, big_endian>&)
957 // PowerPC64 .opd is not folded, so any identical function text
958 // may be folded and we'll still keep function addresses distinct.
959 // That means no reloc is of concern here.
962 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
963 <Powerpc_relobj<size, big_endian>*>(relobj);
964 if (ppcobj->abiversion() == 1)
967 // For 32-bit and ELFv2, conservatively assume anything but calls to
968 // function code might be taking the address of the function.
969 return !is_branch_reloc(r_type);
973 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
975 Sized_relobj_file<size, big_endian>* relobj,
978 const elfcpp::Rela<size, big_endian>& ,
985 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
986 <Powerpc_relobj<size, big_endian>*>(relobj);
987 if (ppcobj->abiversion() == 1)
990 return !is_branch_reloc(r_type);
994 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
995 Sized_relobj_file<size, big_endian>* object,
996 unsigned int r_type, bool report_err);
1000 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1001 unsigned int r_type);
1004 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1005 unsigned int r_type, Symbol*);
1008 generate_tls_call(Symbol_table* symtab, Layout* layout,
1009 Target_powerpc* target);
1012 check_non_pic(Relobj*, unsigned int r_type);
1014 // Whether we have issued an error about a non-PIC compilation.
1015 bool issued_non_pic_error_;
1019 symval_for_branch(const Symbol_table* symtab,
1020 const Sized_symbol<size>* gsym,
1021 Powerpc_relobj<size, big_endian>* object,
1022 Address *value, unsigned int *dest_shndx);
1024 // The class which implements relocation.
1025 class Relocate : protected Track_tls
1028 // Use 'at' branch hints when true, 'y' when false.
1029 // FIXME maybe: set this with an option.
1030 static const bool is_isa_v2 = true;
1036 // Do a relocation. Return false if the caller should not issue
1037 // any warnings about this relocation.
1039 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
1040 Output_section*, size_t relnum,
1041 const elfcpp::Rela<size, big_endian>&,
1042 unsigned int r_type, const Sized_symbol<size>*,
1043 const Symbol_value<size>*,
1045 typename elfcpp::Elf_types<size>::Elf_Addr,
1049 class Relocate_comdat_behavior
1052 // Decide what the linker should do for relocations that refer to
1053 // discarded comdat sections.
1054 inline Comdat_behavior
1055 get(const char* name)
1057 gold::Default_comdat_behavior default_behavior;
1058 Comdat_behavior ret = default_behavior.get(name);
1059 if (ret == CB_WARNING)
1062 && (strcmp(name, ".fixup") == 0
1063 || strcmp(name, ".got2") == 0))
1066 && (strcmp(name, ".opd") == 0
1067 || strcmp(name, ".toc") == 0
1068 || strcmp(name, ".toc1") == 0))
1075 // A class which returns the size required for a relocation type,
1076 // used while scanning relocs during a relocatable link.
1077 class Relocatable_size_for_reloc
1081 get_size_for_reloc(unsigned int, Relobj*)
1088 // Optimize the TLS relocation type based on what we know about the
1089 // symbol. IS_FINAL is true if the final address of this symbol is
1090 // known at link time.
1092 tls::Tls_optimization
1093 optimize_tls_gd(bool is_final)
1095 // If we are generating a shared library, then we can't do anything
1097 if (parameters->options().shared())
1098 return tls::TLSOPT_NONE;
1101 return tls::TLSOPT_TO_IE;
1102 return tls::TLSOPT_TO_LE;
1105 tls::Tls_optimization
1108 if (parameters->options().shared())
1109 return tls::TLSOPT_NONE;
1111 return tls::TLSOPT_TO_LE;
1114 tls::Tls_optimization
1115 optimize_tls_ie(bool is_final)
1117 if (!is_final || parameters->options().shared())
1118 return tls::TLSOPT_NONE;
1120 return tls::TLSOPT_TO_LE;
1125 make_glink_section(Layout*);
1127 // Create the PLT section.
1129 make_plt_section(Symbol_table*, Layout*);
1132 make_iplt_section(Symbol_table*, Layout*);
1135 make_brlt_section(Layout*);
1137 // Create a PLT entry for a global symbol.
1139 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1141 // Create a PLT entry for a local IFUNC symbol.
1143 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1144 Sized_relobj_file<size, big_endian>*,
1148 // Create a GOT entry for local dynamic __tls_get_addr.
1150 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1151 Sized_relobj_file<size, big_endian>* object);
1154 tlsld_got_offset() const
1156 return this->tlsld_got_offset_;
1159 // Get the dynamic reloc section, creating it if necessary.
1161 rela_dyn_section(Layout*);
1163 // Similarly, but for ifunc symbols get the one for ifunc.
1165 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1167 // Copy a relocation against a global symbol.
1169 copy_reloc(Symbol_table* symtab, Layout* layout,
1170 Sized_relobj_file<size, big_endian>* object,
1171 unsigned int shndx, Output_section* output_section,
1172 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1174 this->copy_relocs_.copy_reloc(symtab, layout,
1175 symtab->get_sized_symbol<size>(sym),
1176 object, shndx, output_section,
1177 reloc, this->rela_dyn_section(layout));
1180 // Look over all the input sections, deciding where to place stubs.
1182 group_sections(Layout*, const Task*);
1184 // Sort output sections by address.
1185 struct Sort_sections
1188 operator()(const Output_section* sec1, const Output_section* sec2)
1189 { return sec1->address() < sec2->address(); }
1195 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1196 unsigned int data_shndx,
1198 unsigned int r_type,
1201 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1202 r_type_(r_type), r_sym_(r_sym), addend_(addend)
1208 // If this branch needs a plt call stub, or a long branch stub, make one.
1210 make_stub(Stub_table<size, big_endian>*,
1211 Stub_table<size, big_endian>*,
1212 Symbol_table*) const;
1215 // The branch location..
1216 Powerpc_relobj<size, big_endian>* object_;
1217 unsigned int shndx_;
1219 // ..and the branch type and destination.
1220 unsigned int r_type_;
1221 unsigned int r_sym_;
1225 // Information about this specific target which we pass to the
1226 // general Target structure.
1227 static Target::Target_info powerpc_info;
1229 // The types of GOT entries needed for this platform.
1230 // These values are exposed to the ABI in an incremental link.
1231 // Do not renumber existing values without changing the version
1232 // number of the .gnu_incremental_inputs section.
1236 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1237 GOT_TYPE_DTPREL, // entry for @got@dtprel
1238 GOT_TYPE_TPREL // entry for @got@tprel
1242 Output_data_got_powerpc<size, big_endian>* got_;
1243 // The PLT section. This is a container for a table of addresses,
1244 // and their relocations. Each address in the PLT has a dynamic
1245 // relocation (R_*_JMP_SLOT) and each address will have a
1246 // corresponding entry in .glink for lazy resolution of the PLT.
1247 // ppc32 initialises the PLT to point at the .glink entry, while
1248 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1249 // linker adds a stub that loads the PLT entry into ctr then
1250 // branches to ctr. There may be more than one stub for each PLT
1251 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1252 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1253 Output_data_plt_powerpc<size, big_endian>* plt_;
1254 // The IPLT section. Like plt_, this is a container for a table of
1255 // addresses and their relocations, specifically for STT_GNU_IFUNC
1256 // functions that resolve locally (STT_GNU_IFUNC functions that
1257 // don't resolve locally go in PLT). Unlike plt_, these have no
1258 // entry in .glink for lazy resolution, and the relocation section
1259 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1260 // the relocation section may contain relocations against
1261 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1262 // relocation section will appear at the end of other dynamic
1263 // relocations, so that ld.so applies these relocations after other
1264 // dynamic relocations. In a static executable, the relocation
1265 // section is emitted and marked with __rela_iplt_start and
1266 // __rela_iplt_end symbols.
1267 Output_data_plt_powerpc<size, big_endian>* iplt_;
1268 // Section holding long branch destinations.
1269 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1270 // The .glink section.
1271 Output_data_glink<size, big_endian>* glink_;
1272 // The dynamic reloc section.
1273 Reloc_section* rela_dyn_;
1274 // Relocs saved to avoid a COPY reloc.
1275 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1276 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1277 unsigned int tlsld_got_offset_;
1279 Stub_tables stub_tables_;
1280 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1281 Branch_lookup_table branch_lookup_table_;
1283 typedef std::vector<Branch_info> Branches;
1284 Branches branch_info_;
1286 bool plt_thread_safe_;
1290 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1293 true, // is_big_endian
1294 elfcpp::EM_PPC, // machine_code
1295 false, // has_make_symbol
1296 false, // has_resolve
1297 false, // has_code_fill
1298 true, // is_default_stack_executable
1299 false, // can_icf_inline_merge_sections
1301 "/usr/lib/ld.so.1", // dynamic_linker
1302 0x10000000, // default_text_segment_address
1303 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1304 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1305 false, // isolate_execinstr
1307 elfcpp::SHN_UNDEF, // small_common_shndx
1308 elfcpp::SHN_UNDEF, // large_common_shndx
1309 0, // small_common_section_flags
1310 0, // large_common_section_flags
1311 NULL, // attributes_section
1312 NULL, // attributes_vendor
1313 "_start" // entry_symbol_name
1317 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1320 false, // is_big_endian
1321 elfcpp::EM_PPC, // machine_code
1322 false, // has_make_symbol
1323 false, // has_resolve
1324 false, // has_code_fill
1325 true, // is_default_stack_executable
1326 false, // can_icf_inline_merge_sections
1328 "/usr/lib/ld.so.1", // dynamic_linker
1329 0x10000000, // default_text_segment_address
1330 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1331 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1332 false, // isolate_execinstr
1334 elfcpp::SHN_UNDEF, // small_common_shndx
1335 elfcpp::SHN_UNDEF, // large_common_shndx
1336 0, // small_common_section_flags
1337 0, // large_common_section_flags
1338 NULL, // attributes_section
1339 NULL, // attributes_vendor
1340 "_start" // entry_symbol_name
1344 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1347 true, // is_big_endian
1348 elfcpp::EM_PPC64, // machine_code
1349 false, // has_make_symbol
1350 false, // has_resolve
1351 false, // has_code_fill
1352 true, // is_default_stack_executable
1353 false, // can_icf_inline_merge_sections
1355 "/usr/lib/ld.so.1", // dynamic_linker
1356 0x10000000, // default_text_segment_address
1357 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1358 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1359 false, // isolate_execinstr
1361 elfcpp::SHN_UNDEF, // small_common_shndx
1362 elfcpp::SHN_UNDEF, // large_common_shndx
1363 0, // small_common_section_flags
1364 0, // large_common_section_flags
1365 NULL, // attributes_section
1366 NULL, // attributes_vendor
1367 "_start" // entry_symbol_name
1371 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1374 false, // is_big_endian
1375 elfcpp::EM_PPC64, // machine_code
1376 false, // has_make_symbol
1377 false, // has_resolve
1378 false, // has_code_fill
1379 true, // is_default_stack_executable
1380 false, // can_icf_inline_merge_sections
1382 "/usr/lib/ld.so.1", // dynamic_linker
1383 0x10000000, // default_text_segment_address
1384 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1385 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1386 false, // isolate_execinstr
1388 elfcpp::SHN_UNDEF, // small_common_shndx
1389 elfcpp::SHN_UNDEF, // large_common_shndx
1390 0, // small_common_section_flags
1391 0, // large_common_section_flags
1392 NULL, // attributes_section
1393 NULL, // attributes_vendor
1394 "_start" // entry_symbol_name
1398 is_branch_reloc(unsigned int r_type)
1400 return (r_type == elfcpp::R_POWERPC_REL24
1401 || r_type == elfcpp::R_PPC_PLTREL24
1402 || r_type == elfcpp::R_PPC_LOCAL24PC
1403 || r_type == elfcpp::R_POWERPC_REL14
1404 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1405 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1406 || r_type == elfcpp::R_POWERPC_ADDR24
1407 || r_type == elfcpp::R_POWERPC_ADDR14
1408 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1409 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1412 // If INSN is an opcode that may be used with an @tls operand, return
1413 // the transformed insn for TLS optimisation, otherwise return 0. If
1414 // REG is non-zero only match an insn with RB or RA equal to REG.
1416 at_tls_transform(uint32_t insn, unsigned int reg)
1418 if ((insn & (0x3f << 26)) != 31 << 26)
1422 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1423 rtra = insn & ((1 << 26) - (1 << 16));
1424 else if (((insn >> 16) & 0x1f) == reg)
1425 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1429 if ((insn & (0x3ff << 1)) == 266 << 1)
1432 else if ((insn & (0x1f << 1)) == 23 << 1
1433 && ((insn & (0x1f << 6)) < 14 << 6
1434 || ((insn & (0x1f << 6)) >= 16 << 6
1435 && (insn & (0x1f << 6)) < 24 << 6)))
1436 // load and store indexed -> dform
1437 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1438 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1439 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1440 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1441 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1443 insn = (58 << 26) | 2;
1451 template<int size, bool big_endian>
1452 class Powerpc_relocate_functions
1472 typedef Powerpc_relocate_functions<size, big_endian> This;
1473 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1475 template<int valsize>
1477 has_overflow_signed(Address value)
1479 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1480 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1481 limit <<= ((valsize - 1) >> 1);
1482 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1483 return value + limit > (limit << 1) - 1;
1486 template<int valsize>
1488 has_overflow_unsigned(Address value)
1490 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1491 limit <<= ((valsize - 1) >> 1);
1492 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1493 return value > (limit << 1) - 1;
1496 template<int valsize>
1498 has_overflow_bitfield(Address value)
1500 return (has_overflow_unsigned<valsize>(value)
1501 && has_overflow_signed<valsize>(value));
1504 template<int valsize>
1505 static inline Status
1506 overflowed(Address value, Overflow_check overflow)
1508 if (overflow == CHECK_SIGNED)
1510 if (has_overflow_signed<valsize>(value))
1511 return STATUS_OVERFLOW;
1513 else if (overflow == CHECK_UNSIGNED)
1515 if (has_overflow_unsigned<valsize>(value))
1516 return STATUS_OVERFLOW;
1518 else if (overflow == CHECK_BITFIELD)
1520 if (has_overflow_bitfield<valsize>(value))
1521 return STATUS_OVERFLOW;
1526 // Do a simple RELA relocation
1527 template<int fieldsize, int valsize>
1528 static inline Status
1529 rela(unsigned char* view, Address value, Overflow_check overflow)
1531 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1532 Valtype* wv = reinterpret_cast<Valtype*>(view);
1533 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1534 return overflowed<valsize>(value, overflow);
1537 template<int fieldsize, int valsize>
1538 static inline Status
1539 rela(unsigned char* view,
1540 unsigned int right_shift,
1541 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1543 Overflow_check overflow)
1545 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1546 Valtype* wv = reinterpret_cast<Valtype*>(view);
1547 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1548 Valtype reloc = value >> right_shift;
1551 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
1552 return overflowed<valsize>(value >> right_shift, overflow);
1555 // Do a simple RELA relocation, unaligned.
1556 template<int fieldsize, int valsize>
1557 static inline Status
1558 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1560 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
1561 return overflowed<valsize>(value, overflow);
1564 template<int fieldsize, int valsize>
1565 static inline Status
1566 rela_ua(unsigned char* view,
1567 unsigned int right_shift,
1568 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1570 Overflow_check overflow)
1572 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
1574 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
1575 Valtype reloc = value >> right_shift;
1578 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
1579 return overflowed<valsize>(value >> right_shift, overflow);
1583 // R_PPC64_ADDR64: (Symbol + Addend)
1585 addr64(unsigned char* view, Address value)
1586 { This::template rela<64,64>(view, value, CHECK_NONE); }
1588 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1590 addr64_u(unsigned char* view, Address value)
1591 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
1593 // R_POWERPC_ADDR32: (Symbol + Addend)
1594 static inline Status
1595 addr32(unsigned char* view, Address value, Overflow_check overflow)
1596 { return This::template rela<32,32>(view, value, overflow); }
1598 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1599 static inline Status
1600 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1601 { return This::template rela_ua<32,32>(view, value, overflow); }
1603 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1604 static inline Status
1605 addr24(unsigned char* view, Address value, Overflow_check overflow)
1607 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
1609 if (overflow != CHECK_NONE && (value & 3) != 0)
1610 stat = STATUS_OVERFLOW;
1614 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1615 static inline Status
1616 addr16(unsigned char* view, Address value, Overflow_check overflow)
1617 { return This::template rela<16,16>(view, value, overflow); }
1619 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1620 static inline Status
1621 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1622 { return This::template rela_ua<16,16>(view, value, overflow); }
1624 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1625 static inline Status
1626 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1628 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
1629 if (overflow != CHECK_NONE && (value & 3) != 0)
1630 stat = STATUS_OVERFLOW;
1634 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1636 addr16_hi(unsigned char* view, Address value)
1637 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
1639 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1641 addr16_ha(unsigned char* view, Address value)
1642 { This::addr16_hi(view, value + 0x8000); }
1644 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1646 addr16_hi2(unsigned char* view, Address value)
1647 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
1649 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1651 addr16_ha2(unsigned char* view, Address value)
1652 { This::addr16_hi2(view, value + 0x8000); }
1654 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1656 addr16_hi3(unsigned char* view, Address value)
1657 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
1659 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1661 addr16_ha3(unsigned char* view, Address value)
1662 { This::addr16_hi3(view, value + 0x8000); }
1664 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1665 static inline Status
1666 addr14(unsigned char* view, Address value, Overflow_check overflow)
1668 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
1669 if (overflow != CHECK_NONE && (value & 3) != 0)
1670 stat = STATUS_OVERFLOW;
1675 // Set ABI version for input and output.
1677 template<int size, bool big_endian>
1679 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
1681 this->e_flags_ |= ver;
1682 if (this->abiversion() != 0)
1684 Target_powerpc<size, big_endian>* target =
1685 static_cast<Target_powerpc<size, big_endian>*>(
1686 parameters->sized_target<size, big_endian>());
1687 if (target->abiversion() == 0)
1688 target->set_abiversion(this->abiversion());
1689 else if (target->abiversion() != this->abiversion())
1690 gold_error(_("%s: ABI version %d is not compatible "
1691 "with ABI version %d output"),
1692 this->name().c_str(),
1693 this->abiversion(), target->abiversion());
1698 // Stash away the index of .got2 or .opd in a relocatable object, if
1699 // such a section exists.
1701 template<int size, bool big_endian>
1703 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1704 Read_symbols_data* sd)
1706 const unsigned char* const pshdrs = sd->section_headers->data();
1707 const unsigned char* namesu = sd->section_names->data();
1708 const char* names = reinterpret_cast<const char*>(namesu);
1709 section_size_type names_size = sd->section_names_size;
1710 const unsigned char* s;
1712 s = this->template find_shdr<size, big_endian>(pshdrs,
1713 size == 32 ? ".got2" : ".opd",
1714 names, names_size, NULL);
1717 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1718 this->special_ = ndx;
1721 if (this->abiversion() == 0)
1722 this->set_abiversion(1);
1723 else if (this->abiversion() > 1)
1724 gold_error(_("%s: .opd invalid in abiv%d"),
1725 this->name().c_str(), this->abiversion());
1728 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1731 // Examine .rela.opd to build info about function entry points.
1733 template<int size, bool big_endian>
1735 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1737 const unsigned char* prelocs,
1738 const unsigned char* plocal_syms)
1742 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1744 const int reloc_size
1745 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1746 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1747 Address expected_off = 0;
1748 bool regular = true;
1749 unsigned int opd_ent_size = 0;
1751 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1753 Reltype reloc(prelocs);
1754 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1755 = reloc.get_r_info();
1756 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1757 if (r_type == elfcpp::R_PPC64_ADDR64)
1759 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1760 typename elfcpp::Elf_types<size>::Elf_Addr value;
1763 if (r_sym < this->local_symbol_count())
1765 typename elfcpp::Sym<size, big_endian>
1766 lsym(plocal_syms + r_sym * sym_size);
1767 shndx = lsym.get_st_shndx();
1768 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1769 value = lsym.get_st_value();
1772 shndx = this->symbol_section_and_value(r_sym, &value,
1774 this->set_opd_ent(reloc.get_r_offset(), shndx,
1775 value + reloc.get_r_addend());
1778 expected_off = reloc.get_r_offset();
1779 opd_ent_size = expected_off;
1781 else if (expected_off != reloc.get_r_offset())
1783 expected_off += opd_ent_size;
1785 else if (r_type == elfcpp::R_PPC64_TOC)
1787 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1792 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1793 this->name().c_str(), r_type);
1797 if (reloc_count <= 2)
1798 opd_ent_size = this->section_size(this->opd_shndx());
1799 if (opd_ent_size != 24 && opd_ent_size != 16)
1803 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1804 this->name().c_str());
1810 template<int size, bool big_endian>
1812 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1814 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1817 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1818 p != rd->relocs.end();
1821 if (p->data_shndx == this->opd_shndx())
1823 uint64_t opd_size = this->section_size(this->opd_shndx());
1824 gold_assert(opd_size == static_cast<size_t>(opd_size));
1827 this->init_opd(opd_size);
1828 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1829 rd->local_symbols->data());
1837 // Read the symbols then set up st_other vector.
1839 template<int size, bool big_endian>
1841 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1843 this->base_read_symbols(sd);
1846 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1847 const unsigned char* const pshdrs = sd->section_headers->data();
1848 const unsigned int loccount = this->do_local_symbol_count();
1851 this->st_other_.resize(loccount);
1852 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1853 off_t locsize = loccount * sym_size;
1854 const unsigned int symtab_shndx = this->symtab_shndx();
1855 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
1856 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
1857 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
1858 locsize, true, false);
1860 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1862 elfcpp::Sym<size, big_endian> sym(psyms);
1863 unsigned char st_other = sym.get_st_other();
1864 this->st_other_[i] = st_other;
1865 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1867 if (this->abiversion() == 0)
1868 this->set_abiversion(2);
1869 else if (this->abiversion() < 2)
1870 gold_error(_("%s: local symbol %d has invalid st_other"
1871 " for ABI version 1"),
1872 this->name().c_str(), i);
1879 template<int size, bool big_endian>
1881 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
1883 this->e_flags_ |= ver;
1884 if (this->abiversion() != 0)
1886 Target_powerpc<size, big_endian>* target =
1887 static_cast<Target_powerpc<size, big_endian>*>(
1888 parameters->sized_target<size, big_endian>());
1889 if (target->abiversion() == 0)
1890 target->set_abiversion(this->abiversion());
1891 else if (target->abiversion() != this->abiversion())
1892 gold_error(_("%s: ABI version %d is not compatible "
1893 "with ABI version %d output"),
1894 this->name().c_str(),
1895 this->abiversion(), target->abiversion());
1900 // Call Sized_dynobj::base_read_symbols to read the symbols then
1901 // read .opd from a dynamic object, filling in opd_ent_ vector,
1903 template<int size, bool big_endian>
1905 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1907 this->base_read_symbols(sd);
1910 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1911 const unsigned char* const pshdrs = sd->section_headers->data();
1912 const unsigned char* namesu = sd->section_names->data();
1913 const char* names = reinterpret_cast<const char*>(namesu);
1914 const unsigned char* s = NULL;
1915 const unsigned char* opd;
1916 section_size_type opd_size;
1918 // Find and read .opd section.
1921 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
1922 sd->section_names_size,
1927 typename elfcpp::Shdr<size, big_endian> shdr(s);
1928 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1929 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1931 if (this->abiversion() == 0)
1932 this->set_abiversion(1);
1933 else if (this->abiversion() > 1)
1934 gold_error(_("%s: .opd invalid in abiv%d"),
1935 this->name().c_str(), this->abiversion());
1937 this->opd_shndx_ = (s - pshdrs) / shdr_size;
1938 this->opd_address_ = shdr.get_sh_addr();
1939 opd_size = convert_to_section_size_type(shdr.get_sh_size());
1940 opd = this->get_view(shdr.get_sh_offset(), opd_size,
1946 // Build set of executable sections.
1947 // Using a set is probably overkill. There is likely to be only
1948 // a few executable sections, typically .init, .text and .fini,
1949 // and they are generally grouped together.
1950 typedef std::set<Sec_info> Exec_sections;
1951 Exec_sections exec_sections;
1953 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
1955 typename elfcpp::Shdr<size, big_endian> shdr(s);
1956 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1957 && ((shdr.get_sh_flags()
1958 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1959 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1960 && shdr.get_sh_size() != 0)
1962 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
1963 shdr.get_sh_size(), i));
1966 if (exec_sections.empty())
1969 // Look over the OPD entries. This is complicated by the fact
1970 // that some binaries will use two-word entries while others
1971 // will use the standard three-word entries. In most cases
1972 // the third word (the environment pointer for languages like
1973 // Pascal) is unused and will be zero. If the third word is
1974 // used it should not be pointing into executable sections,
1976 this->init_opd(opd_size);
1977 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
1979 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1980 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
1981 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
1983 // Chances are that this is the third word of an OPD entry.
1985 typename Exec_sections::const_iterator e
1986 = exec_sections.upper_bound(Sec_info(val, 0, 0));
1987 if (e != exec_sections.begin())
1990 if (e->start <= val && val < e->start + e->len)
1992 // We have an address in an executable section.
1993 // VAL ought to be the function entry, set it up.
1994 this->set_opd_ent(p - opd, e->shndx, val);
1995 // Skip second word of OPD entry, the TOC pointer.
1999 // If we didn't match any executable sections, we likely
2000 // have a non-zero third word in the OPD entry.
2005 // Set up some symbols.
2007 template<int size, bool big_endian>
2009 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2010 Symbol_table* symtab,
2015 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2016 // undefined when scanning relocs (and thus requires
2017 // non-relative dynamic relocs). The proper value will be
2019 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2020 if (gotsym != NULL && gotsym->is_undefined())
2022 Target_powerpc<size, big_endian>* target =
2023 static_cast<Target_powerpc<size, big_endian>*>(
2024 parameters->sized_target<size, big_endian>());
2025 Output_data_got_powerpc<size, big_endian>* got
2026 = target->got_section(symtab, layout);
2027 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2028 Symbol_table::PREDEFINED,
2032 elfcpp::STV_HIDDEN, 0,
2036 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2037 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2038 if (sdasym != NULL && sdasym->is_undefined())
2040 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2042 = layout->add_output_section_data(".sdata", 0,
2044 | elfcpp::SHF_WRITE,
2045 sdata, ORDER_SMALL_DATA, false);
2046 symtab->define_in_output_data("_SDA_BASE_", NULL,
2047 Symbol_table::PREDEFINED,
2048 os, 32768, 0, elfcpp::STT_OBJECT,
2049 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2055 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2056 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2057 if (gotsym != NULL && gotsym->is_undefined())
2059 Target_powerpc<size, big_endian>* target =
2060 static_cast<Target_powerpc<size, big_endian>*>(
2061 parameters->sized_target<size, big_endian>());
2062 Output_data_got_powerpc<size, big_endian>* got
2063 = target->got_section(symtab, layout);
2064 symtab->define_in_output_data(".TOC.", NULL,
2065 Symbol_table::PREDEFINED,
2069 elfcpp::STV_HIDDEN, 0,
2075 // Set up PowerPC target specific relobj.
2077 template<int size, bool big_endian>
2079 Target_powerpc<size, big_endian>::do_make_elf_object(
2080 const std::string& name,
2081 Input_file* input_file,
2082 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2084 int et = ehdr.get_e_type();
2085 // ET_EXEC files are valid input for --just-symbols/-R,
2086 // and we treat them as relocatable objects.
2087 if (et == elfcpp::ET_REL
2088 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2090 Powerpc_relobj<size, big_endian>* obj =
2091 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2095 else if (et == elfcpp::ET_DYN)
2097 Powerpc_dynobj<size, big_endian>* obj =
2098 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2104 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2109 template<int size, bool big_endian>
2110 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2113 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2114 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2116 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2117 : Output_data_got<size, big_endian>(),
2118 symtab_(symtab), layout_(layout),
2119 header_ent_cnt_(size == 32 ? 3 : 1),
2120 header_index_(size == 32 ? 0x2000 : 0)
2123 // Override all the Output_data_got methods we use so as to first call
2126 add_global(Symbol* gsym, unsigned int got_type)
2128 this->reserve_ent();
2129 return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2133 add_global_plt(Symbol* gsym, unsigned int got_type)
2135 this->reserve_ent();
2136 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2140 add_global_tls(Symbol* gsym, unsigned int got_type)
2141 { return this->add_global_plt(gsym, got_type); }
2144 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2145 Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2147 this->reserve_ent();
2148 Output_data_got<size, big_endian>::
2149 add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2153 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2154 Output_data_reloc_generic* rel_dyn,
2155 unsigned int r_type_1, unsigned int r_type_2)
2157 this->reserve_ent(2);
2158 Output_data_got<size, big_endian>::
2159 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2163 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2165 this->reserve_ent();
2166 return Output_data_got<size, big_endian>::add_local(object, sym_index,
2171 add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2173 this->reserve_ent();
2174 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2179 add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2180 { return this->add_local_plt(object, sym_index, got_type); }
2183 add_local_tls_pair(Relobj* object, unsigned int sym_index,
2184 unsigned int got_type,
2185 Output_data_reloc_generic* rel_dyn,
2186 unsigned int r_type)
2188 this->reserve_ent(2);
2189 Output_data_got<size, big_endian>::
2190 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2194 add_constant(Valtype constant)
2196 this->reserve_ent();
2197 return Output_data_got<size, big_endian>::add_constant(constant);
2201 add_constant_pair(Valtype c1, Valtype c2)
2203 this->reserve_ent(2);
2204 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2207 // Offset of _GLOBAL_OFFSET_TABLE_.
2211 return this->got_offset(this->header_index_);
2214 // Offset of base used to access the GOT/TOC.
2215 // The got/toc pointer reg will be set to this value.
2217 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2220 return this->g_o_t();
2222 return (this->output_section()->address()
2223 + object->toc_base_offset()
2227 // Ensure our GOT has a header.
2229 set_final_data_size()
2231 if (this->header_ent_cnt_ != 0)
2232 this->make_header();
2233 Output_data_got<size, big_endian>::set_final_data_size();
2236 // First word of GOT header needs some values that are not
2237 // handled by Output_data_got so poke them in here.
2238 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2240 do_write(Output_file* of)
2243 if (size == 32 && this->layout_->dynamic_data() != NULL)
2244 val = this->layout_->dynamic_section()->address();
2246 val = this->output_section()->address() + 0x8000;
2247 this->replace_constant(this->header_index_, val);
2248 Output_data_got<size, big_endian>::do_write(of);
2253 reserve_ent(unsigned int cnt = 1)
2255 if (this->header_ent_cnt_ == 0)
2257 if (this->num_entries() + cnt > this->header_index_)
2258 this->make_header();
2264 this->header_ent_cnt_ = 0;
2265 this->header_index_ = this->num_entries();
2268 Output_data_got<size, big_endian>::add_constant(0);
2269 Output_data_got<size, big_endian>::add_constant(0);
2270 Output_data_got<size, big_endian>::add_constant(0);
2272 // Define _GLOBAL_OFFSET_TABLE_ at the header
2273 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2276 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2277 sym->set_value(this->g_o_t());
2280 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2281 Symbol_table::PREDEFINED,
2282 this, this->g_o_t(), 0,
2285 elfcpp::STV_HIDDEN, 0,
2289 Output_data_got<size, big_endian>::add_constant(0);
2292 // Stashed pointers.
2293 Symbol_table* symtab_;
2297 unsigned int header_ent_cnt_;
2298 // GOT header index.
2299 unsigned int header_index_;
2302 // Get the GOT section, creating it if necessary.
2304 template<int size, bool big_endian>
2305 Output_data_got_powerpc<size, big_endian>*
2306 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2309 if (this->got_ == NULL)
2311 gold_assert(symtab != NULL && layout != NULL);
2314 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2316 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2317 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2318 this->got_, ORDER_DATA, false);
2324 // Get the dynamic reloc section, creating it if necessary.
2326 template<int size, bool big_endian>
2327 typename Target_powerpc<size, big_endian>::Reloc_section*
2328 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2330 if (this->rela_dyn_ == NULL)
2332 gold_assert(layout != NULL);
2333 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2334 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2335 elfcpp::SHF_ALLOC, this->rela_dyn_,
2336 ORDER_DYNAMIC_RELOCS, false);
2338 return this->rela_dyn_;
2341 // Similarly, but for ifunc symbols get the one for ifunc.
2343 template<int size, bool big_endian>
2344 typename Target_powerpc<size, big_endian>::Reloc_section*
2345 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2350 return this->rela_dyn_section(layout);
2352 if (this->iplt_ == NULL)
2353 this->make_iplt_section(symtab, layout);
2354 return this->iplt_->rel_plt();
2360 // Determine the stub group size. The group size is the absolute
2361 // value of the parameter --stub-group-size. If --stub-group-size
2362 // is passed a negative value, we restrict stubs to be always before
2363 // the stubbed branches.
2364 Stub_control(int32_t size)
2365 : state_(NO_GROUP), stub_group_size_(abs(size)),
2366 stub14_group_size_(abs(size) >> 10),
2367 stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
2368 group_end_addr_(0), owner_(NULL), output_section_(NULL)
2370 if (stub_group_size_ == 1)
2373 if (stubs_always_before_branch_)
2375 stub_group_size_ = 0x1e00000;
2376 stub14_group_size_ = 0x7800;
2380 stub_group_size_ = 0x1c00000;
2381 stub14_group_size_ = 0x7000;
2383 suppress_size_errors_ = true;
2387 // Return true iff input section can be handled by current stub
2390 can_add_to_stub_group(Output_section* o,
2391 const Output_section::Input_section* i,
2394 const Output_section::Input_section*
2400 { return output_section_; }
2403 set_output_and_owner(Output_section* o,
2404 const Output_section::Input_section* i)
2406 this->output_section_ = o;
2414 FINDING_STUB_SECTION,
2419 uint32_t stub_group_size_;
2420 uint32_t stub14_group_size_;
2421 bool stubs_always_before_branch_;
2422 bool suppress_size_errors_;
2423 uint64_t group_end_addr_;
2424 const Output_section::Input_section* owner_;
2425 Output_section* output_section_;
2428 // Return true iff input section can be handled by current stub
2432 Stub_control::can_add_to_stub_group(Output_section* o,
2433 const Output_section::Input_section* i,
2437 = has14 ? this->stub14_group_size_ : this->stub_group_size_;
2438 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2440 uint64_t start_addr = o->address();
2443 // .init and .fini sections are pasted together to form a single
2444 // function. We can't be adding stubs in the middle of the function.
2445 this_size = o->data_size();
2448 start_addr += i->relobj()->output_section_offset(i->shndx());
2449 this_size = i->data_size();
2451 uint64_t end_addr = start_addr + this_size;
2452 bool toobig = this_size > group_size;
2454 if (toobig && !this->suppress_size_errors_)
2455 gold_warning(_("%s:%s exceeds group size"),
2456 i->relobj()->name().c_str(),
2457 i->relobj()->section_name(i->shndx()).c_str());
2459 if (this->state_ != HAS_STUB_SECTION
2460 && (!whole_sec || this->output_section_ != o)
2461 && (this->state_ == NO_GROUP
2462 || this->group_end_addr_ - end_addr < group_size))
2465 this->output_section_ = o;
2468 if (this->state_ == NO_GROUP)
2470 this->state_ = FINDING_STUB_SECTION;
2471 this->group_end_addr_ = end_addr;
2473 else if (this->group_end_addr_ - start_addr < group_size)
2475 // Adding this section would make the group larger than GROUP_SIZE.
2476 else if (this->state_ == FINDING_STUB_SECTION
2477 && !this->stubs_always_before_branch_
2480 // But wait, there's more! Input sections up to GROUP_SIZE
2481 // bytes before the stub table can be handled by it too.
2482 this->state_ = HAS_STUB_SECTION;
2483 this->group_end_addr_ = end_addr;
2487 this->state_ = NO_GROUP;
2493 // Look over all the input sections, deciding where to place stubs.
2495 template<int size, bool big_endian>
2497 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2500 Stub_control stub_control(parameters->options().stub_group_size());
2502 // Group input sections and insert stub table
2503 Stub_table<size, big_endian>* stub_table = NULL;
2504 Layout::Section_list section_list;
2505 layout->get_executable_sections(§ion_list);
2506 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2507 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2508 o != section_list.rend();
2511 typedef Output_section::Input_section_list Input_section_list;
2512 for (Input_section_list::const_reverse_iterator i
2513 = (*o)->input_sections().rbegin();
2514 i != (*o)->input_sections().rend();
2517 if (i->is_input_section())
2519 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2520 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2521 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2522 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2524 stub_table->init(stub_control.owner(),
2525 stub_control.output_section());
2526 stub_control.set_output_and_owner(*o, &*i);
2529 if (stub_table == NULL)
2530 stub_table = this->new_stub_table();
2531 ppcobj->set_stub_table(i->shndx(), stub_table);
2535 if (stub_table != NULL)
2537 const Output_section::Input_section* i = stub_control.owner();
2538 if (!i->is_input_section())
2540 // Corner case. A new stub group was made for the first
2541 // section (last one looked at here) for some reason, but
2542 // the first section is already being used as the owner for
2543 // a stub table for following sections. Force it into that
2545 gold_assert(this->stub_tables_.size() >= 2);
2546 this->stub_tables_.pop_back();
2548 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2549 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2550 ppcobj->set_stub_table(i->shndx(), this->stub_tables_.back());
2553 stub_table->init(i, stub_control.output_section());
2557 // If this branch needs a plt call stub, or a long branch stub, make one.
2559 template<int size, bool big_endian>
2561 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2562 Stub_table<size, big_endian>* stub_table,
2563 Stub_table<size, big_endian>* ifunc_stub_table,
2564 Symbol_table* symtab) const
2566 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2567 if (sym != NULL && sym->is_forwarder())
2568 sym = symtab->resolve_forwards(sym);
2569 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2570 Target_powerpc<size, big_endian>* target =
2571 static_cast<Target_powerpc<size, big_endian>*>(
2572 parameters->sized_target<size, big_endian>());
2574 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
2575 : this->object_->local_has_plt_offset(this->r_sym_))
2579 && target->abiversion() >= 2
2580 && !parameters->options().output_is_position_independent()
2581 && !is_branch_reloc(this->r_type_))
2582 target->glink_section()->add_global_entry(gsym);
2585 if (stub_table == NULL)
2586 stub_table = this->object_->stub_table(this->shndx_);
2587 if (stub_table == NULL)
2589 // This is a ref from a data section to an ifunc symbol.
2590 stub_table = ifunc_stub_table;
2592 gold_assert(stub_table != NULL);
2594 stub_table->add_plt_call_entry(this->object_, gsym,
2595 this->r_type_, this->addend_);
2597 stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2598 this->r_type_, this->addend_);
2603 unsigned long max_branch_offset;
2604 if (this->r_type_ == elfcpp::R_POWERPC_REL14
2605 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2606 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2607 max_branch_offset = 1 << 15;
2608 else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2609 || this->r_type_ == elfcpp::R_PPC_PLTREL24
2610 || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2611 max_branch_offset = 1 << 25;
2614 Address from = this->object_->get_output_section_offset(this->shndx_);
2615 gold_assert(from != invalid_address);
2616 from += (this->object_->output_section(this->shndx_)->address()
2621 switch (gsym->source())
2623 case Symbol::FROM_OBJECT:
2625 Object* symobj = gsym->object();
2626 if (symobj->is_dynamic()
2627 || symobj->pluginobj() != NULL)
2630 unsigned int shndx = gsym->shndx(&is_ordinary);
2631 if (shndx == elfcpp::SHN_UNDEF)
2636 case Symbol::IS_UNDEFINED:
2642 Symbol_table::Compute_final_value_status status;
2643 to = symtab->compute_final_value<size>(gsym, &status);
2644 if (status != Symbol_table::CFVS_OK)
2647 to += this->object_->ppc64_local_entry_offset(gsym);
2651 const Symbol_value<size>* psymval
2652 = this->object_->local_symbol(this->r_sym_);
2653 Symbol_value<size> symval;
2654 typedef Sized_relobj_file<size, big_endian> ObjType;
2655 typename ObjType::Compute_final_local_value_status status
2656 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2658 if (status != ObjType::CFLV_OK
2659 || !symval.has_output_value())
2661 to = symval.value(this->object_, 0);
2663 to += this->object_->ppc64_local_entry_offset(this->r_sym_);
2665 to += this->addend_;
2666 if (stub_table == NULL)
2667 stub_table = this->object_->stub_table(this->shndx_);
2668 if (size == 64 && target->abiversion() < 2)
2670 unsigned int dest_shndx;
2671 if (!target->symval_for_branch(symtab, gsym, this->object_,
2675 Address delta = to - from;
2676 if (delta + max_branch_offset >= 2 * max_branch_offset)
2678 if (stub_table == NULL)
2680 gold_warning(_("%s:%s: branch in non-executable section,"
2681 " no long branch stub for you"),
2682 this->object_->name().c_str(),
2683 this->object_->section_name(this->shndx_).c_str());
2686 stub_table->add_long_branch_entry(this->object_, to);
2691 // Relaxation hook. This is where we do stub generation.
2693 template<int size, bool big_endian>
2695 Target_powerpc<size, big_endian>::do_relax(int pass,
2696 const Input_objects*,
2697 Symbol_table* symtab,
2701 unsigned int prev_brlt_size = 0;
2705 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
2707 && this->abiversion() < 2
2709 && !parameters->options().user_set_plt_thread_safe())
2711 static const char* const thread_starter[] =
2715 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2717 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2718 "mq_notify", "create_timer",
2723 "GOMP_parallel_start",
2724 "GOMP_parallel_loop_static",
2725 "GOMP_parallel_loop_static_start",
2726 "GOMP_parallel_loop_dynamic",
2727 "GOMP_parallel_loop_dynamic_start",
2728 "GOMP_parallel_loop_guided",
2729 "GOMP_parallel_loop_guided_start",
2730 "GOMP_parallel_loop_runtime",
2731 "GOMP_parallel_loop_runtime_start",
2732 "GOMP_parallel_sections",
2733 "GOMP_parallel_sections_start",
2738 if (parameters->options().shared())
2742 for (unsigned int i = 0;
2743 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2746 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2747 thread_safe = (sym != NULL
2749 && sym->in_real_elf());
2755 this->plt_thread_safe_ = thread_safe;
2756 this->group_sections(layout, task);
2759 // We need address of stub tables valid for make_stub.
2760 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2761 p != this->stub_tables_.end();
2764 const Powerpc_relobj<size, big_endian>* object
2765 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2766 Address off = object->get_output_section_offset((*p)->shndx());
2767 gold_assert(off != invalid_address);
2768 Output_section* os = (*p)->output_section();
2769 (*p)->set_address_and_size(os, off);
2774 // Clear plt call stubs, long branch stubs and branch lookup table.
2775 prev_brlt_size = this->branch_lookup_table_.size();
2776 this->branch_lookup_table_.clear();
2777 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2778 p != this->stub_tables_.end();
2781 (*p)->clear_stubs();
2785 // Build all the stubs.
2786 Stub_table<size, big_endian>* ifunc_stub_table
2787 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2788 Stub_table<size, big_endian>* one_stub_table
2789 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2790 for (typename Branches::const_iterator b = this->branch_info_.begin();
2791 b != this->branch_info_.end();
2794 b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2797 // Did anything change size?
2798 unsigned int num_huge_branches = this->branch_lookup_table_.size();
2799 bool again = num_huge_branches != prev_brlt_size;
2800 if (size == 64 && num_huge_branches != 0)
2801 this->make_brlt_section(layout);
2802 if (size == 64 && again)
2803 this->brlt_section_->set_current_size(num_huge_branches);
2805 typedef Unordered_set<Output_section*> Output_sections;
2806 Output_sections os_need_update;
2807 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2808 p != this->stub_tables_.end();
2811 if ((*p)->size_update())
2814 (*p)->add_eh_frame(layout);
2815 os_need_update.insert((*p)->output_section());
2819 // Set output section offsets for all input sections in an output
2820 // section that just changed size. Anything past the stubs will
2822 for (typename Output_sections::iterator p = os_need_update.begin();
2823 p != os_need_update.end();
2826 Output_section* os = *p;
2828 typedef Output_section::Input_section_list Input_section_list;
2829 for (Input_section_list::const_iterator i = os->input_sections().begin();
2830 i != os->input_sections().end();
2833 off = align_address(off, i->addralign());
2834 if (i->is_input_section() || i->is_relaxed_input_section())
2835 i->relobj()->set_section_offset(i->shndx(), off);
2836 if (i->is_relaxed_input_section())
2838 Stub_table<size, big_endian>* stub_table
2839 = static_cast<Stub_table<size, big_endian>*>(
2840 i->relaxed_input_section());
2841 off += stub_table->set_address_and_size(os, off);
2844 off += i->data_size();
2846 // If .branch_lt is part of this output section, then we have
2847 // just done the offset adjustment.
2848 os->clear_section_offsets_need_adjustment();
2853 && num_huge_branches != 0
2854 && parameters->options().output_is_position_independent())
2856 // Fill in the BRLT relocs.
2857 this->brlt_section_->reset_brlt_sizes();
2858 for (typename Branch_lookup_table::const_iterator p
2859 = this->branch_lookup_table_.begin();
2860 p != this->branch_lookup_table_.end();
2863 this->brlt_section_->add_reloc(p->first, p->second);
2865 this->brlt_section_->finalize_brlt_sizes();
2870 template<int size, bool big_endian>
2872 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2873 unsigned char* oview,
2877 uint64_t address = plt->address();
2878 off_t len = plt->data_size();
2880 if (plt == this->glink_)
2882 // See Output_data_glink::do_write() for glink contents.
2885 gold_assert(parameters->doing_static_link());
2886 // Static linking may need stubs, to support ifunc and long
2887 // branches. We need to create an output section for
2888 // .eh_frame early in the link process, to have a place to
2889 // attach stub .eh_frame info. We also need to have
2890 // registered a CIE that matches the stub CIE. Both of
2891 // these requirements are satisfied by creating an FDE and
2892 // CIE for .glink, even though static linking will leave
2893 // .glink zero length.
2894 // ??? Hopefully generating an FDE with a zero address range
2895 // won't confuse anything that consumes .eh_frame info.
2897 else if (size == 64)
2899 // There is one word before __glink_PLTresolve
2903 else if (parameters->options().output_is_position_independent())
2905 // There are two FDEs for a position independent glink.
2906 // The first covers the branch table, the second
2907 // __glink_PLTresolve at the end of glink.
2908 off_t resolve_size = this->glink_->pltresolve_size;
2909 if (oview[9] == elfcpp::DW_CFA_nop)
2910 len -= resolve_size;
2913 address += len - resolve_size;
2920 // Must be a stub table.
2921 const Stub_table<size, big_endian>* stub_table
2922 = static_cast<const Stub_table<size, big_endian>*>(plt);
2923 uint64_t stub_address = stub_table->stub_address();
2924 len -= stub_address - address;
2925 address = stub_address;
2928 *paddress = address;
2932 // A class to handle the PLT data.
2934 template<int size, bool big_endian>
2935 class Output_data_plt_powerpc : public Output_section_data_build
2938 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2939 size, big_endian> Reloc_section;
2941 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2942 Reloc_section* plt_rel,
2944 : Output_section_data_build(size == 32 ? 4 : 8),
2950 // Add an entry to the PLT.
2955 add_ifunc_entry(Symbol*);
2958 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2960 // Return the .rela.plt section data.
2967 // Return the number of PLT entries.
2971 if (this->current_data_size() == 0)
2973 return ((this->current_data_size() - this->first_plt_entry_offset())
2974 / this->plt_entry_size());
2979 do_adjust_output_section(Output_section* os)
2984 // Write to a map file.
2986 do_print_to_mapfile(Mapfile* mapfile) const
2987 { mapfile->print_output_data(this, this->name_); }
2990 // Return the offset of the first non-reserved PLT entry.
2992 first_plt_entry_offset() const
2994 // IPLT has no reserved entry.
2995 if (this->name_[3] == 'I')
2997 return this->targ_->first_plt_entry_offset();
3000 // Return the size of each PLT entry.
3002 plt_entry_size() const
3004 return this->targ_->plt_entry_size();
3007 // Write out the PLT data.
3009 do_write(Output_file*);
3011 // The reloc section.
3012 Reloc_section* rel_;
3013 // Allows access to .glink for do_write.
3014 Target_powerpc<size, big_endian>* targ_;
3015 // What to report in map file.
3019 // Add an entry to the PLT.
3021 template<int size, bool big_endian>
3023 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3025 if (!gsym->has_plt_offset())
3027 section_size_type off = this->current_data_size();
3029 off += this->first_plt_entry_offset();
3030 gsym->set_plt_offset(off);
3031 gsym->set_needs_dynsym_entry();
3032 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3033 this->rel_->add_global(gsym, dynrel, this, off, 0);
3034 off += this->plt_entry_size();
3035 this->set_current_data_size(off);
3039 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3041 template<int size, bool big_endian>
3043 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3045 if (!gsym->has_plt_offset())
3047 section_size_type off = this->current_data_size();
3048 gsym->set_plt_offset(off);
3049 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3050 if (size == 64 && this->targ_->abiversion() < 2)
3051 dynrel = elfcpp::R_PPC64_JMP_IREL;
3052 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3053 off += this->plt_entry_size();
3054 this->set_current_data_size(off);
3058 // Add an entry for a local ifunc symbol to the IPLT.
3060 template<int size, bool big_endian>
3062 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3063 Sized_relobj_file<size, big_endian>* relobj,
3064 unsigned int local_sym_index)
3066 if (!relobj->local_has_plt_offset(local_sym_index))
3068 section_size_type off = this->current_data_size();
3069 relobj->set_local_plt_offset(local_sym_index, off);
3070 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3071 if (size == 64 && this->targ_->abiversion() < 2)
3072 dynrel = elfcpp::R_PPC64_JMP_IREL;
3073 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3075 off += this->plt_entry_size();
3076 this->set_current_data_size(off);
3080 static const uint32_t add_0_11_11 = 0x7c0b5a14;
3081 static const uint32_t add_2_2_11 = 0x7c425a14;
3082 static const uint32_t add_3_3_2 = 0x7c631214;
3083 static const uint32_t add_3_3_13 = 0x7c636a14;
3084 static const uint32_t add_11_0_11 = 0x7d605a14;
3085 static const uint32_t add_11_2_11 = 0x7d625a14;
3086 static const uint32_t add_11_11_2 = 0x7d6b1214;
3087 static const uint32_t addi_0_12 = 0x380c0000;
3088 static const uint32_t addi_2_2 = 0x38420000;
3089 static const uint32_t addi_3_3 = 0x38630000;
3090 static const uint32_t addi_11_11 = 0x396b0000;
3091 static const uint32_t addi_12_12 = 0x398c0000;
3092 static const uint32_t addis_0_2 = 0x3c020000;
3093 static const uint32_t addis_0_13 = 0x3c0d0000;
3094 static const uint32_t addis_3_2 = 0x3c620000;
3095 static const uint32_t addis_3_13 = 0x3c6d0000;
3096 static const uint32_t addis_11_2 = 0x3d620000;
3097 static const uint32_t addis_11_11 = 0x3d6b0000;
3098 static const uint32_t addis_11_30 = 0x3d7e0000;
3099 static const uint32_t addis_12_2 = 0x3d820000;
3100 static const uint32_t addis_12_12 = 0x3d8c0000;
3101 static const uint32_t b = 0x48000000;
3102 static const uint32_t bcl_20_31 = 0x429f0005;
3103 static const uint32_t bctr = 0x4e800420;
3104 static const uint32_t blr = 0x4e800020;
3105 static const uint32_t bnectr_p4 = 0x4ce20420;
3106 static const uint32_t cmpldi_2_0 = 0x28220000;
3107 static const uint32_t cror_15_15_15 = 0x4def7b82;
3108 static const uint32_t cror_31_31_31 = 0x4ffffb82;
3109 static const uint32_t ld_0_1 = 0xe8010000;
3110 static const uint32_t ld_0_12 = 0xe80c0000;
3111 static const uint32_t ld_2_1 = 0xe8410000;
3112 static const uint32_t ld_2_2 = 0xe8420000;
3113 static const uint32_t ld_2_11 = 0xe84b0000;
3114 static const uint32_t ld_11_2 = 0xe9620000;
3115 static const uint32_t ld_11_11 = 0xe96b0000;
3116 static const uint32_t ld_12_2 = 0xe9820000;
3117 static const uint32_t ld_12_11 = 0xe98b0000;
3118 static const uint32_t ld_12_12 = 0xe98c0000;
3119 static const uint32_t lfd_0_1 = 0xc8010000;
3120 static const uint32_t li_0_0 = 0x38000000;
3121 static const uint32_t li_12_0 = 0x39800000;
3122 static const uint32_t lis_0_0 = 0x3c000000;
3123 static const uint32_t lis_11 = 0x3d600000;
3124 static const uint32_t lis_12 = 0x3d800000;
3125 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
3126 static const uint32_t lwz_0_12 = 0x800c0000;
3127 static const uint32_t lwz_11_11 = 0x816b0000;
3128 static const uint32_t lwz_11_30 = 0x817e0000;
3129 static const uint32_t lwz_12_12 = 0x818c0000;
3130 static const uint32_t lwzu_0_12 = 0x840c0000;
3131 static const uint32_t mflr_0 = 0x7c0802a6;
3132 static const uint32_t mflr_11 = 0x7d6802a6;
3133 static const uint32_t mflr_12 = 0x7d8802a6;
3134 static const uint32_t mtctr_0 = 0x7c0903a6;
3135 static const uint32_t mtctr_11 = 0x7d6903a6;
3136 static const uint32_t mtctr_12 = 0x7d8903a6;
3137 static const uint32_t mtlr_0 = 0x7c0803a6;
3138 static const uint32_t mtlr_12 = 0x7d8803a6;
3139 static const uint32_t nop = 0x60000000;
3140 static const uint32_t ori_0_0_0 = 0x60000000;
3141 static const uint32_t srdi_0_0_2 = 0x7800f082;
3142 static const uint32_t std_0_1 = 0xf8010000;
3143 static const uint32_t std_0_12 = 0xf80c0000;
3144 static const uint32_t std_2_1 = 0xf8410000;
3145 static const uint32_t stfd_0_1 = 0xd8010000;
3146 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
3147 static const uint32_t sub_11_11_12 = 0x7d6c5850;
3148 static const uint32_t sub_12_12_11 = 0x7d8b6050;
3149 static const uint32_t xor_2_12_12 = 0x7d826278;
3150 static const uint32_t xor_11_12_12 = 0x7d8b6278;
3152 // Write out the PLT.
3154 template<int size, bool big_endian>
3156 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3158 if (size == 32 && this->name_[3] != 'I')
3160 const section_size_type offset = this->offset();
3161 const section_size_type oview_size
3162 = convert_to_section_size_type(this->data_size());
3163 unsigned char* const oview = of->get_output_view(offset, oview_size);
3164 unsigned char* pov = oview;
3165 unsigned char* endpov = oview + oview_size;
3167 // The address of the .glink branch table
3168 const Output_data_glink<size, big_endian>* glink
3169 = this->targ_->glink_section();
3170 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3172 while (pov < endpov)
3174 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3179 of->write_output_view(offset, oview_size, oview);
3183 // Create the PLT section.
3185 template<int size, bool big_endian>
3187 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3190 if (this->plt_ == NULL)
3192 if (this->got_ == NULL)
3193 this->got_section(symtab, layout);
3195 if (this->glink_ == NULL)
3196 make_glink_section(layout);
3198 // Ensure that .rela.dyn always appears before .rela.plt This is
3199 // necessary due to how, on PowerPC and some other targets, .rela.dyn
3200 // needs to include .rela.plt in its range.
3201 this->rela_dyn_section(layout);
3203 Reloc_section* plt_rel = new Reloc_section(false);
3204 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3205 elfcpp::SHF_ALLOC, plt_rel,
3206 ORDER_DYNAMIC_PLT_RELOCS, false);
3208 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3210 layout->add_output_section_data(".plt",
3212 ? elfcpp::SHT_PROGBITS
3213 : elfcpp::SHT_NOBITS),
3214 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3223 // Create the IPLT section.
3225 template<int size, bool big_endian>
3227 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3230 if (this->iplt_ == NULL)
3232 this->make_plt_section(symtab, layout);
3234 Reloc_section* iplt_rel = new Reloc_section(false);
3235 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3237 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3239 this->plt_->output_section()->add_output_section_data(this->iplt_);
3243 // A section for huge long branch addresses, similar to plt section.
3245 template<int size, bool big_endian>
3246 class Output_data_brlt_powerpc : public Output_section_data_build
3249 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3250 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3251 size, big_endian> Reloc_section;
3253 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3254 Reloc_section* brlt_rel)
3255 : Output_section_data_build(size == 32 ? 4 : 8),
3263 this->reset_data_size();
3264 this->rel_->reset_data_size();
3268 finalize_brlt_sizes()
3270 this->finalize_data_size();
3271 this->rel_->finalize_data_size();
3274 // Add a reloc for an entry in the BRLT.
3276 add_reloc(Address to, unsigned int off)
3277 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3279 // Update section and reloc section size.
3281 set_current_size(unsigned int num_branches)
3283 this->reset_address_and_file_offset();
3284 this->set_current_data_size(num_branches * 16);
3285 this->finalize_data_size();
3286 Output_section* os = this->output_section();
3287 os->set_section_offsets_need_adjustment();
3288 if (this->rel_ != NULL)
3290 unsigned int reloc_size
3291 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3292 this->rel_->reset_address_and_file_offset();
3293 this->rel_->set_current_data_size(num_branches * reloc_size);
3294 this->rel_->finalize_data_size();
3295 Output_section* os = this->rel_->output_section();
3296 os->set_section_offsets_need_adjustment();
3302 do_adjust_output_section(Output_section* os)
3307 // Write to a map file.
3309 do_print_to_mapfile(Mapfile* mapfile) const
3310 { mapfile->print_output_data(this, "** BRLT"); }
3313 // Write out the BRLT data.
3315 do_write(Output_file*);
3317 // The reloc section.
3318 Reloc_section* rel_;
3319 Target_powerpc<size, big_endian>* targ_;
3322 // Make the branch lookup table section.
3324 template<int size, bool big_endian>
3326 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3328 if (size == 64 && this->brlt_section_ == NULL)
3330 Reloc_section* brlt_rel = NULL;
3331 bool is_pic = parameters->options().output_is_position_independent();
3334 // When PIC we can't fill in .branch_lt (like .plt it can be
3335 // a bss style section) but must initialise at runtime via
3336 // dynamic relocats.
3337 this->rela_dyn_section(layout);
3338 brlt_rel = new Reloc_section(false);
3339 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3342 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3343 if (this->plt_ && is_pic)
3344 this->plt_->output_section()
3345 ->add_output_section_data(this->brlt_section_);
3347 layout->add_output_section_data(".branch_lt",
3348 (is_pic ? elfcpp::SHT_NOBITS
3349 : elfcpp::SHT_PROGBITS),
3350 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3351 this->brlt_section_,
3352 (is_pic ? ORDER_SMALL_BSS
3353 : ORDER_SMALL_DATA),
3358 // Write out .branch_lt when non-PIC.
3360 template<int size, bool big_endian>
3362 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3364 if (size == 64 && !parameters->options().output_is_position_independent())
3366 const section_size_type offset = this->offset();
3367 const section_size_type oview_size
3368 = convert_to_section_size_type(this->data_size());
3369 unsigned char* const oview = of->get_output_view(offset, oview_size);
3371 this->targ_->write_branch_lookup_table(oview);
3372 of->write_output_view(offset, oview_size, oview);
3376 static inline uint32_t
3382 static inline uint32_t
3388 static inline uint32_t
3391 return hi(a + 0x8000);
3397 static const unsigned char eh_frame_cie[12];
3401 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3404 'z', 'R', 0, // Augmentation string.
3405 4, // Code alignment.
3406 0x80 - size / 8 , // Data alignment.
3408 1, // Augmentation size.
3409 (elfcpp::DW_EH_PE_pcrel
3410 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3411 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3414 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3415 static const unsigned char glink_eh_frame_fde_64v1[] =
3417 0, 0, 0, 0, // Replaced with offset to .glink.
3418 0, 0, 0, 0, // Replaced with size of .glink.
3419 0, // Augmentation size.
3420 elfcpp::DW_CFA_advance_loc + 1,
3421 elfcpp::DW_CFA_register, 65, 12,
3422 elfcpp::DW_CFA_advance_loc + 4,
3423 elfcpp::DW_CFA_restore_extended, 65
3426 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3427 static const unsigned char glink_eh_frame_fde_64v2[] =
3429 0, 0, 0, 0, // Replaced with offset to .glink.
3430 0, 0, 0, 0, // Replaced with size of .glink.
3431 0, // Augmentation size.
3432 elfcpp::DW_CFA_advance_loc + 1,
3433 elfcpp::DW_CFA_register, 65, 0,
3434 elfcpp::DW_CFA_advance_loc + 4,
3435 elfcpp::DW_CFA_restore_extended, 65
3438 // Describe __glink_PLTresolve use of LR, 32-bit version.
3439 static const unsigned char glink_eh_frame_fde_32[] =
3441 0, 0, 0, 0, // Replaced with offset to .glink.
3442 0, 0, 0, 0, // Replaced with size of .glink.
3443 0, // Augmentation size.
3444 elfcpp::DW_CFA_advance_loc + 2,
3445 elfcpp::DW_CFA_register, 65, 0,
3446 elfcpp::DW_CFA_advance_loc + 4,
3447 elfcpp::DW_CFA_restore_extended, 65
3450 static const unsigned char default_fde[] =
3452 0, 0, 0, 0, // Replaced with offset to stubs.
3453 0, 0, 0, 0, // Replaced with size of stubs.
3454 0, // Augmentation size.
3455 elfcpp::DW_CFA_nop, // Pad.
3460 template<bool big_endian>
3462 write_insn(unsigned char* p, uint32_t v)
3464 elfcpp::Swap<32, big_endian>::writeval(p, v);
3467 // Stub_table holds information about plt and long branch stubs.
3468 // Stubs are built in an area following some input section determined
3469 // by group_sections(). This input section is converted to a relaxed
3470 // input section allowing it to be resized to accommodate the stubs
3472 template<int size, bool big_endian>
3473 class Stub_table : public Output_relaxed_input_section
3476 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3477 static const Address invalid_address = static_cast<Address>(0) - 1;
3479 Stub_table(Target_powerpc<size, big_endian>* targ)
3480 : Output_relaxed_input_section(NULL, 0, 0),
3481 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3482 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3483 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3486 // Delayed Output_relaxed_input_section init.
3488 init(const Output_section::Input_section*, Output_section*);
3490 // Add a plt call stub.
3492 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3498 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3503 // Find a given plt call stub.
3505 find_plt_call_entry(const Symbol*) const;
3508 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3509 unsigned int) const;
3512 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3518 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3523 // Add a long branch stub.
3525 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3528 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3534 this->plt_call_stubs_.clear();
3535 this->plt_size_ = 0;
3536 this->long_branch_stubs_.clear();
3537 this->branch_size_ = 0;
3541 set_address_and_size(const Output_section* os, Address off)
3543 Address start_off = off;
3544 off += this->orig_data_size_;
3545 Address my_size = this->plt_size_ + this->branch_size_;
3547 off = align_address(off, this->stub_align());
3548 // Include original section size and alignment padding in size
3549 my_size += off - start_off;
3550 this->reset_address_and_file_offset();
3551 this->set_current_data_size(my_size);
3552 this->set_address_and_file_offset(os->address() + start_off,
3553 os->offset() + start_off);
3558 stub_address() const
3560 return align_address(this->address() + this->orig_data_size_,
3561 this->stub_align());
3567 return align_address(this->offset() + this->orig_data_size_,
3568 this->stub_align());
3573 { return this->plt_size_; }
3578 Output_section* os = this->output_section();
3579 if (os->addralign() < this->stub_align())
3581 os->set_addralign(this->stub_align());
3582 // FIXME: get rid of the insane checkpointing.
3583 // We can't increase alignment of the input section to which
3584 // stubs are attached; The input section may be .init which
3585 // is pasted together with other .init sections to form a
3586 // function. Aligning might insert zero padding resulting in
3587 // sigill. However we do need to increase alignment of the
3588 // output section so that the align_address() on offset in
3589 // set_address_and_size() adds the same padding as the
3590 // align_address() on address in stub_address().
3591 // What's more, we need this alignment for the layout done in
3592 // relaxation_loop_body() so that the output section starts at
3593 // a suitably aligned address.
3594 os->checkpoint_set_addralign(this->stub_align());
3596 if (this->last_plt_size_ != this->plt_size_
3597 || this->last_branch_size_ != this->branch_size_)
3599 this->last_plt_size_ = this->plt_size_;
3600 this->last_branch_size_ = this->branch_size_;
3606 // Add .eh_frame info for this stub section. Unlike other linker
3607 // generated .eh_frame this is added late in the link, because we
3608 // only want the .eh_frame info if this particular stub section is
3611 add_eh_frame(Layout* layout)
3613 if (!this->eh_frame_added_)
3615 if (!parameters->options().ld_generated_unwind_info())
3618 // Since we add stub .eh_frame info late, it must be placed
3619 // after all other linker generated .eh_frame info so that
3620 // merge mapping need not be updated for input sections.
3621 // There is no provision to use a different CIE to that used
3623 if (!this->targ_->has_glink())
3626 layout->add_eh_frame_for_plt(this,
3627 Eh_cie<size>::eh_frame_cie,
3628 sizeof (Eh_cie<size>::eh_frame_cie),
3630 sizeof (default_fde));
3631 this->eh_frame_added_ = true;
3635 Target_powerpc<size, big_endian>*
3641 class Plt_stub_ent_hash;
3642 typedef Unordered_map<Plt_stub_ent, unsigned int,
3643 Plt_stub_ent_hash> Plt_stub_entries;
3645 // Alignment of stub section.
3651 unsigned int min_align = 32;
3652 unsigned int user_align = 1 << parameters->options().plt_align();
3653 return std::max(user_align, min_align);
3656 // Return the plt offset for the given call stub.
3658 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3660 const Symbol* gsym = p->first.sym_;
3663 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3664 && gsym->can_use_relative_reloc(false));
3665 return gsym->plt_offset();
3670 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3671 unsigned int local_sym_index = p->first.locsym_;
3672 return relobj->local_plt_offset(local_sym_index);
3676 // Size of a given plt call stub.
3678 plt_call_size(typename Plt_stub_entries::const_iterator p) const
3684 Address plt_addr = this->plt_off(p, &is_iplt);
3686 plt_addr += this->targ_->iplt_section()->address();
3688 plt_addr += this->targ_->plt_section()->address();
3689 Address got_addr = this->targ_->got_section()->output_section()->address();
3690 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3691 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3692 got_addr += ppcobj->toc_base_offset();
3693 Address off = plt_addr - got_addr;
3694 unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
3695 if (this->targ_->abiversion() < 2)
3697 bool static_chain = parameters->options().plt_static_chain();
3698 bool thread_safe = this->targ_->plt_thread_safe();
3702 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3704 unsigned int align = 1 << parameters->options().plt_align();
3706 bytes = (bytes + align - 1) & -align;
3710 // Return long branch stub size.
3712 branch_stub_size(Address to)
3715 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3716 if (to - loc + (1 << 25) < 2 << 25)
3718 if (size == 64 || !parameters->options().output_is_position_independent())
3725 do_write(Output_file*);
3727 // Plt call stub keys.
3731 Plt_stub_ent(const Symbol* sym)
3732 : sym_(sym), object_(0), addend_(0), locsym_(0)
3735 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3736 unsigned int locsym_index)
3737 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3740 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3742 unsigned int r_type,
3744 : sym_(sym), object_(0), addend_(0), locsym_(0)
3747 this->addend_ = addend;
3748 else if (parameters->options().output_is_position_independent()
3749 && r_type == elfcpp::R_PPC_PLTREL24)
3751 this->addend_ = addend;
3752 if (this->addend_ >= 32768)
3753 this->object_ = object;
3757 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3758 unsigned int locsym_index,
3759 unsigned int r_type,
3761 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3764 this->addend_ = addend;
3765 else if (parameters->options().output_is_position_independent()
3766 && r_type == elfcpp::R_PPC_PLTREL24)
3767 this->addend_ = addend;
3770 bool operator==(const Plt_stub_ent& that) const
3772 return (this->sym_ == that.sym_
3773 && this->object_ == that.object_
3774 && this->addend_ == that.addend_
3775 && this->locsym_ == that.locsym_);
3779 const Sized_relobj_file<size, big_endian>* object_;
3780 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3781 unsigned int locsym_;
3784 class Plt_stub_ent_hash
3787 size_t operator()(const Plt_stub_ent& ent) const
3789 return (reinterpret_cast<uintptr_t>(ent.sym_)
3790 ^ reinterpret_cast<uintptr_t>(ent.object_)
3796 // Long branch stub keys.
3797 class Branch_stub_ent
3800 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3801 : dest_(to), toc_base_off_(0)
3804 toc_base_off_ = obj->toc_base_offset();
3807 bool operator==(const Branch_stub_ent& that) const
3809 return (this->dest_ == that.dest_
3811 || this->toc_base_off_ == that.toc_base_off_));
3815 unsigned int toc_base_off_;
3818 class Branch_stub_ent_hash
3821 size_t operator()(const Branch_stub_ent& ent) const
3822 { return ent.dest_ ^ ent.toc_base_off_; }
3825 // In a sane world this would be a global.
3826 Target_powerpc<size, big_endian>* targ_;
3827 // Map sym/object/addend to stub offset.
3828 Plt_stub_entries plt_call_stubs_;
3829 // Map destination address to stub offset.
3830 typedef Unordered_map<Branch_stub_ent, unsigned int,
3831 Branch_stub_ent_hash> Branch_stub_entries;
3832 Branch_stub_entries long_branch_stubs_;
3833 // size of input section
3834 section_size_type orig_data_size_;
3836 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3837 // Whether .eh_frame info has been created for this stub section.
3838 bool eh_frame_added_;
3841 // Make a new stub table, and record.
3843 template<int size, bool big_endian>
3844 Stub_table<size, big_endian>*
3845 Target_powerpc<size, big_endian>::new_stub_table()
3847 Stub_table<size, big_endian>* stub_table
3848 = new Stub_table<size, big_endian>(this);
3849 this->stub_tables_.push_back(stub_table);
3853 // Delayed stub table initialisation, because we create the stub table
3854 // before we know to which section it will be attached.
3856 template<int size, bool big_endian>
3858 Stub_table<size, big_endian>::init(
3859 const Output_section::Input_section* owner,
3860 Output_section* output_section)
3862 this->set_relobj(owner->relobj());
3863 this->set_shndx(owner->shndx());
3864 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3865 this->set_output_section(output_section);
3866 this->orig_data_size_ = owner->current_data_size();
3868 std::vector<Output_relaxed_input_section*> new_relaxed;
3869 new_relaxed.push_back(this);
3870 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3873 // Add a plt call stub, if we do not already have one for this
3874 // sym/object/addend combo.
3876 template<int size, bool big_endian>
3878 Stub_table<size, big_endian>::add_plt_call_entry(
3879 const Sized_relobj_file<size, big_endian>* object,
3881 unsigned int r_type,
3884 Plt_stub_ent ent(object, gsym, r_type, addend);
3885 unsigned int off = this->plt_size_;
3886 std::pair<typename Plt_stub_entries::iterator, bool> p
3887 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3889 this->plt_size_ = off + this->plt_call_size(p.first);
3892 template<int size, bool big_endian>
3894 Stub_table<size, big_endian>::add_plt_call_entry(
3895 const Sized_relobj_file<size, big_endian>* object,
3896 unsigned int locsym_index,
3897 unsigned int r_type,
3900 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3901 unsigned int off = this->plt_size_;
3902 std::pair<typename Plt_stub_entries::iterator, bool> p
3903 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3905 this->plt_size_ = off + this->plt_call_size(p.first);
3908 // Find a plt call stub.
3910 template<int size, bool big_endian>
3911 typename Stub_table<size, big_endian>::Address
3912 Stub_table<size, big_endian>::find_plt_call_entry(
3913 const Sized_relobj_file<size, big_endian>* object,
3915 unsigned int r_type,
3916 Address addend) const
3918 Plt_stub_ent ent(object, gsym, r_type, addend);
3919 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3920 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3923 template<int size, bool big_endian>
3924 typename Stub_table<size, big_endian>::Address
3925 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3927 Plt_stub_ent ent(gsym);
3928 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3929 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3932 template<int size, bool big_endian>
3933 typename Stub_table<size, big_endian>::Address
3934 Stub_table<size, big_endian>::find_plt_call_entry(
3935 const Sized_relobj_file<size, big_endian>* object,
3936 unsigned int locsym_index,
3937 unsigned int r_type,
3938 Address addend) const
3940 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3941 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3942 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3945 template<int size, bool big_endian>
3946 typename Stub_table<size, big_endian>::Address
3947 Stub_table<size, big_endian>::find_plt_call_entry(
3948 const Sized_relobj_file<size, big_endian>* object,
3949 unsigned int locsym_index) const
3951 Plt_stub_ent ent(object, locsym_index);
3952 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3953 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3956 // Add a long branch stub if we don't already have one to given
3959 template<int size, bool big_endian>
3961 Stub_table<size, big_endian>::add_long_branch_entry(
3962 const Powerpc_relobj<size, big_endian>* object,
3965 Branch_stub_ent ent(object, to);
3966 Address off = this->branch_size_;
3967 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3969 unsigned int stub_size = this->branch_stub_size(to);
3970 this->branch_size_ = off + stub_size;
3971 if (size == 64 && stub_size != 4)
3972 this->targ_->add_branch_lookup_table(to);
3976 // Find long branch stub.
3978 template<int size, bool big_endian>
3979 typename Stub_table<size, big_endian>::Address
3980 Stub_table<size, big_endian>::find_long_branch_entry(
3981 const Powerpc_relobj<size, big_endian>* object,
3984 Branch_stub_ent ent(object, to);
3985 typename Branch_stub_entries::const_iterator p
3986 = this->long_branch_stubs_.find(ent);
3987 return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3990 // A class to handle .glink.
3992 template<int size, bool big_endian>
3993 class Output_data_glink : public Output_section_data
3996 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3997 static const Address invalid_address = static_cast<Address>(0) - 1;
3998 static const int pltresolve_size = 16*4;
4000 Output_data_glink(Target_powerpc<size, big_endian>* targ)
4001 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4002 end_branch_table_(), ge_size_(0)
4006 add_eh_frame(Layout* layout);
4009 add_global_entry(const Symbol*);
4012 find_global_entry(const Symbol*) const;
4015 global_entry_address() const
4017 gold_assert(this->is_data_size_valid());
4018 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4019 return this->address() + global_entry_off;
4023 // Write to a map file.
4025 do_print_to_mapfile(Mapfile* mapfile) const
4026 { mapfile->print_output_data(this, _("** glink")); }
4030 set_final_data_size();
4034 do_write(Output_file*);
4036 // Allows access to .got and .plt for do_write.
4037 Target_powerpc<size, big_endian>* targ_;
4039 // Map sym to stub offset.
4040 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4041 Global_entry_stub_entries global_entry_stubs_;
4043 unsigned int end_branch_table_, ge_size_;
4046 template<int size, bool big_endian>
4048 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4050 if (!parameters->options().ld_generated_unwind_info())
4055 if (this->targ_->abiversion() < 2)
4056 layout->add_eh_frame_for_plt(this,
4057 Eh_cie<64>::eh_frame_cie,
4058 sizeof (Eh_cie<64>::eh_frame_cie),
4059 glink_eh_frame_fde_64v1,
4060 sizeof (glink_eh_frame_fde_64v1));
4062 layout->add_eh_frame_for_plt(this,
4063 Eh_cie<64>::eh_frame_cie,
4064 sizeof (Eh_cie<64>::eh_frame_cie),
4065 glink_eh_frame_fde_64v2,
4066 sizeof (glink_eh_frame_fde_64v2));
4070 // 32-bit .glink can use the default since the CIE return
4071 // address reg, LR, is valid.
4072 layout->add_eh_frame_for_plt(this,
4073 Eh_cie<32>::eh_frame_cie,
4074 sizeof (Eh_cie<32>::eh_frame_cie),
4076 sizeof (default_fde));
4077 // Except where LR is used in a PIC __glink_PLTresolve.
4078 if (parameters->options().output_is_position_independent())
4079 layout->add_eh_frame_for_plt(this,
4080 Eh_cie<32>::eh_frame_cie,
4081 sizeof (Eh_cie<32>::eh_frame_cie),
4082 glink_eh_frame_fde_32,
4083 sizeof (glink_eh_frame_fde_32));
4087 template<int size, bool big_endian>
4089 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4091 std::pair<typename Global_entry_stub_entries::iterator, bool> p
4092 = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4094 this->ge_size_ += 16;
4097 template<int size, bool big_endian>
4098 typename Output_data_glink<size, big_endian>::Address
4099 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4101 typename Global_entry_stub_entries::const_iterator p
4102 = this->global_entry_stubs_.find(gsym);
4103 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4106 template<int size, bool big_endian>
4108 Output_data_glink<size, big_endian>::set_final_data_size()
4110 unsigned int count = this->targ_->plt_entry_count();
4111 section_size_type total = 0;
4117 // space for branch table
4118 total += 4 * (count - 1);
4120 total += -total & 15;
4121 total += this->pltresolve_size;
4125 total += this->pltresolve_size;
4127 // space for branch table
4129 if (this->targ_->abiversion() < 2)
4133 total += 4 * (count - 0x8000);
4137 this->end_branch_table_ = total;
4138 total = (total + 15) & -16;
4139 total += this->ge_size_;
4141 this->set_data_size(total);
4144 // Write out plt and long branch stub code.
4146 template<int size, bool big_endian>
4148 Stub_table<size, big_endian>::do_write(Output_file* of)
4150 if (this->plt_call_stubs_.empty()
4151 && this->long_branch_stubs_.empty())
4154 const section_size_type start_off = this->offset();
4155 const section_size_type off = this->stub_offset();
4156 const section_size_type oview_size =
4157 convert_to_section_size_type(this->data_size() - (off - start_off));
4158 unsigned char* const oview = of->get_output_view(off, oview_size);
4163 const Output_data_got_powerpc<size, big_endian>* got
4164 = this->targ_->got_section();
4165 Address got_os_addr = got->output_section()->address();
4167 if (!this->plt_call_stubs_.empty())
4169 // The base address of the .plt section.
4170 Address plt_base = this->targ_->plt_section()->address();
4171 Address iplt_base = invalid_address;
4173 // Write out plt call stubs.
4174 typename Plt_stub_entries::const_iterator cs;
4175 for (cs = this->plt_call_stubs_.begin();
4176 cs != this->plt_call_stubs_.end();
4180 Address pltoff = this->plt_off(cs, &is_iplt);
4181 Address plt_addr = pltoff;
4184 if (iplt_base == invalid_address)
4185 iplt_base = this->targ_->iplt_section()->address();
4186 plt_addr += iplt_base;
4189 plt_addr += plt_base;
4190 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4191 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4192 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4193 Address off = plt_addr - got_addr;
4195 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4196 gold_error(_("%s: linkage table error against `%s'"),
4197 cs->first.object_->name().c_str(),
4198 cs->first.sym_->demangled_name().c_str());
4200 bool plt_load_toc = this->targ_->abiversion() < 2;
4202 = plt_load_toc && parameters->options().plt_static_chain();
4204 = plt_load_toc && this->targ_->plt_thread_safe();
4205 bool use_fake_dep = false;
4206 Address cmp_branch_off = 0;
4209 unsigned int pltindex
4210 = ((pltoff - this->targ_->first_plt_entry_offset())
4211 / this->targ_->plt_entry_size());
4213 = (this->targ_->glink_section()->pltresolve_size
4215 if (pltindex > 32768)
4216 glinkoff += (pltindex - 32768) * 4;
4218 = this->targ_->glink_section()->address() + glinkoff;
4220 = (this->stub_address() + cs->second + 24
4221 + 4 * (ha(off) != 0)
4222 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4223 + 4 * static_chain);
4224 cmp_branch_off = to - from;
4225 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4228 p = oview + cs->second;
4231 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4235 write_insn<big_endian>(p, addis_11_2 + ha(off));
4237 write_insn<big_endian>(p, ld_12_11 + l(off));
4242 write_insn<big_endian>(p, addis_12_2 + ha(off));
4244 write_insn<big_endian>(p, ld_12_12 + l(off));
4248 && ha(off + 8 + 8 * static_chain) != ha(off))
4250 write_insn<big_endian>(p, addi_11_11 + l(off));
4254 write_insn<big_endian>(p, mtctr_12);
4260 write_insn<big_endian>(p, xor_2_12_12);
4262 write_insn<big_endian>(p, add_11_11_2);
4265 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4269 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4276 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4278 write_insn<big_endian>(p, ld_12_2 + l(off));
4281 && ha(off + 8 + 8 * static_chain) != ha(off))
4283 write_insn<big_endian>(p, addi_2_2 + l(off));
4287 write_insn<big_endian>(p, mtctr_12);
4293 write_insn<big_endian>(p, xor_11_12_12);
4295 write_insn<big_endian>(p, add_2_2_11);
4300 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4303 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4307 if (thread_safe && !use_fake_dep)
4309 write_insn<big_endian>(p, cmpldi_2_0);
4311 write_insn<big_endian>(p, bnectr_p4);
4313 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4316 write_insn<big_endian>(p, bctr);
4320 // Write out long branch stubs.
4321 typename Branch_stub_entries::const_iterator bs;
4322 for (bs = this->long_branch_stubs_.begin();
4323 bs != this->long_branch_stubs_.end();
4326 p = oview + this->plt_size_ + bs->second;
4327 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4328 Address delta = bs->first.dest_ - loc;
4329 if (delta + (1 << 25) < 2 << 25)
4330 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4334 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4335 gold_assert(brlt_addr != invalid_address);
4336 brlt_addr += this->targ_->brlt_section()->address();
4337 Address got_addr = got_os_addr + bs->first.toc_base_off_;
4338 Address brltoff = brlt_addr - got_addr;
4339 if (ha(brltoff) == 0)
4341 write_insn<big_endian>(p, ld_12_2 + l(brltoff)), p += 4;
4345 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
4346 write_insn<big_endian>(p, ld_12_12 + l(brltoff)), p += 4;
4348 write_insn<big_endian>(p, mtctr_12), p += 4;
4349 write_insn<big_endian>(p, bctr);
4355 if (!this->plt_call_stubs_.empty())
4357 // The base address of the .plt section.
4358 Address plt_base = this->targ_->plt_section()->address();
4359 Address iplt_base = invalid_address;
4360 // The address of _GLOBAL_OFFSET_TABLE_.
4361 Address g_o_t = invalid_address;
4363 // Write out plt call stubs.
4364 typename Plt_stub_entries::const_iterator cs;
4365 for (cs = this->plt_call_stubs_.begin();
4366 cs != this->plt_call_stubs_.end();
4370 Address plt_addr = this->plt_off(cs, &is_iplt);
4373 if (iplt_base == invalid_address)
4374 iplt_base = this->targ_->iplt_section()->address();
4375 plt_addr += iplt_base;
4378 plt_addr += plt_base;
4380 p = oview + cs->second;
4381 if (parameters->options().output_is_position_independent())
4384 const Powerpc_relobj<size, big_endian>* ppcobj
4385 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4386 (cs->first.object_));
4387 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4389 unsigned int got2 = ppcobj->got2_shndx();
4390 got_addr = ppcobj->get_output_section_offset(got2);
4391 gold_assert(got_addr != invalid_address);
4392 got_addr += (ppcobj->output_section(got2)->address()
4393 + cs->first.addend_);
4397 if (g_o_t == invalid_address)
4399 const Output_data_got_powerpc<size, big_endian>* got
4400 = this->targ_->got_section();
4401 g_o_t = got->address() + got->g_o_t();
4406 Address off = plt_addr - got_addr;
4409 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
4410 write_insn<big_endian>(p + 4, mtctr_11);
4411 write_insn<big_endian>(p + 8, bctr);
4415 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4416 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
4417 write_insn<big_endian>(p + 8, mtctr_11);
4418 write_insn<big_endian>(p + 12, bctr);
4423 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4424 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4425 write_insn<big_endian>(p + 8, mtctr_11);
4426 write_insn<big_endian>(p + 12, bctr);
4431 // Write out long branch stubs.
4432 typename Branch_stub_entries::const_iterator bs;
4433 for (bs = this->long_branch_stubs_.begin();
4434 bs != this->long_branch_stubs_.end();
4437 p = oview + this->plt_size_ + bs->second;
4438 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4439 Address delta = bs->first.dest_ - loc;
4440 if (delta + (1 << 25) < 2 << 25)
4441 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4442 else if (!parameters->options().output_is_position_independent())
4444 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
4445 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
4446 write_insn<big_endian>(p + 8, mtctr_12);
4447 write_insn<big_endian>(p + 12, bctr);
4452 write_insn<big_endian>(p + 0, mflr_0);
4453 write_insn<big_endian>(p + 4, bcl_20_31);
4454 write_insn<big_endian>(p + 8, mflr_12);
4455 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4456 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4457 write_insn<big_endian>(p + 20, mtlr_0);
4458 write_insn<big_endian>(p + 24, mtctr_12);
4459 write_insn<big_endian>(p + 28, bctr);
4465 // Write out .glink.
4467 template<int size, bool big_endian>
4469 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4471 const section_size_type off = this->offset();
4472 const section_size_type oview_size =
4473 convert_to_section_size_type(this->data_size());
4474 unsigned char* const oview = of->get_output_view(off, oview_size);
4477 // The base address of the .plt section.
4478 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4479 Address plt_base = this->targ_->plt_section()->address();
4483 if (this->end_branch_table_ != 0)
4485 // Write pltresolve stub.
4487 Address after_bcl = this->address() + 16;
4488 Address pltoff = plt_base - after_bcl;
4490 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
4492 if (this->targ_->abiversion() < 2)
4494 write_insn<big_endian>(p, mflr_12), p += 4;
4495 write_insn<big_endian>(p, bcl_20_31), p += 4;
4496 write_insn<big_endian>(p, mflr_11), p += 4;
4497 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4498 write_insn<big_endian>(p, mtlr_12), p += 4;
4499 write_insn<big_endian>(p, add_11_2_11), p += 4;
4500 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4501 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
4502 write_insn<big_endian>(p, mtctr_12), p += 4;
4503 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
4507 write_insn<big_endian>(p, mflr_0), p += 4;
4508 write_insn<big_endian>(p, bcl_20_31), p += 4;
4509 write_insn<big_endian>(p, mflr_11), p += 4;
4510 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4511 write_insn<big_endian>(p, mtlr_0), p += 4;
4512 write_insn<big_endian>(p, sub_12_12_11), p += 4;
4513 write_insn<big_endian>(p, add_11_2_11), p += 4;
4514 write_insn<big_endian>(p, addi_0_12 + l(-48)), p += 4;
4515 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
4516 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
4517 write_insn<big_endian>(p, mtctr_12), p += 4;
4518 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
4520 write_insn<big_endian>(p, bctr), p += 4;
4521 while (p < oview + this->pltresolve_size)
4522 write_insn<big_endian>(p, nop), p += 4;
4524 // Write lazy link call stubs.
4526 while (p < oview + this->end_branch_table_)
4528 if (this->targ_->abiversion() < 2)
4532 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4536 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
4537 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4540 uint32_t branch_off = 8 - (p - oview);
4541 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4546 Address plt_base = this->targ_->plt_section()->address();
4547 Address iplt_base = invalid_address;
4548 unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4549 Address global_entry_base = this->address() + global_entry_off;
4550 typename Global_entry_stub_entries::const_iterator ge;
4551 for (ge = this->global_entry_stubs_.begin();
4552 ge != this->global_entry_stubs_.end();
4555 p = oview + global_entry_off + ge->second;
4556 Address plt_addr = ge->first->plt_offset();
4557 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
4558 && ge->first->can_use_relative_reloc(false))
4560 if (iplt_base == invalid_address)
4561 iplt_base = this->targ_->iplt_section()->address();
4562 plt_addr += iplt_base;
4565 plt_addr += plt_base;
4566 Address my_addr = global_entry_base + ge->second;
4567 Address off = plt_addr - my_addr;
4569 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
4570 gold_error(_("%s: linkage table error against `%s'"),
4571 ge->first->object()->name().c_str(),
4572 ge->first->demangled_name().c_str());
4574 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
4575 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
4576 write_insn<big_endian>(p, mtctr_12), p += 4;
4577 write_insn<big_endian>(p, bctr);
4582 const Output_data_got_powerpc<size, big_endian>* got
4583 = this->targ_->got_section();
4584 // The address of _GLOBAL_OFFSET_TABLE_.
4585 Address g_o_t = got->address() + got->g_o_t();
4587 // Write out pltresolve branch table.
4589 unsigned int the_end = oview_size - this->pltresolve_size;
4590 unsigned char* end_p = oview + the_end;
4591 while (p < end_p - 8 * 4)
4592 write_insn<big_endian>(p, b + end_p - p), p += 4;
4594 write_insn<big_endian>(p, nop), p += 4;
4596 // Write out pltresolve call stub.
4597 if (parameters->options().output_is_position_independent())
4599 Address res0_off = 0;
4600 Address after_bcl_off = the_end + 12;
4601 Address bcl_res0 = after_bcl_off - res0_off;
4603 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
4604 write_insn<big_endian>(p + 4, mflr_0);
4605 write_insn<big_endian>(p + 8, bcl_20_31);
4606 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4607 write_insn<big_endian>(p + 16, mflr_12);
4608 write_insn<big_endian>(p + 20, mtlr_0);
4609 write_insn<big_endian>(p + 24, sub_11_11_12);
4611 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4613 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4614 if (ha(got_bcl) == ha(got_bcl + 4))
4616 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4617 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4621 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4622 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4624 write_insn<big_endian>(p + 40, mtctr_0);
4625 write_insn<big_endian>(p + 44, add_0_11_11);
4626 write_insn<big_endian>(p + 48, add_11_0_11);
4627 write_insn<big_endian>(p + 52, bctr);
4628 write_insn<big_endian>(p + 56, nop);
4629 write_insn<big_endian>(p + 60, nop);
4633 Address res0 = this->address();
4635 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4636 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4637 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4638 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4640 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4641 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4642 write_insn<big_endian>(p + 16, mtctr_0);
4643 write_insn<big_endian>(p + 20, add_0_11_11);
4644 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4645 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4647 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4648 write_insn<big_endian>(p + 28, add_11_0_11);
4649 write_insn<big_endian>(p + 32, bctr);
4650 write_insn<big_endian>(p + 36, nop);
4651 write_insn<big_endian>(p + 40, nop);
4652 write_insn<big_endian>(p + 44, nop);
4653 write_insn<big_endian>(p + 48, nop);
4654 write_insn<big_endian>(p + 52, nop);
4655 write_insn<big_endian>(p + 56, nop);
4656 write_insn<big_endian>(p + 60, nop);
4661 of->write_output_view(off, oview_size, oview);
4665 // A class to handle linker generated save/restore functions.
4667 template<int size, bool big_endian>
4668 class Output_data_save_res : public Output_section_data_build
4671 Output_data_save_res(Symbol_table* symtab);
4674 // Write to a map file.
4676 do_print_to_mapfile(Mapfile* mapfile) const
4677 { mapfile->print_output_data(this, _("** save/restore")); }
4680 do_write(Output_file*);
4683 // The maximum size of save/restore contents.
4684 static const unsigned int savres_max = 218*4;
4687 savres_define(Symbol_table* symtab,
4689 unsigned int lo, unsigned int hi,
4690 unsigned char* write_ent(unsigned char*, int),
4691 unsigned char* write_tail(unsigned char*, int));
4693 unsigned char *contents_;
4696 template<bool big_endian>
4697 static unsigned char*
4698 savegpr0(unsigned char* p, int r)
4700 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4701 write_insn<big_endian>(p, insn);
4705 template<bool big_endian>
4706 static unsigned char*
4707 savegpr0_tail(unsigned char* p, int r)
4709 p = savegpr0<big_endian>(p, r);
4710 uint32_t insn = std_0_1 + 16;
4711 write_insn<big_endian>(p, insn);
4713 write_insn<big_endian>(p, blr);
4717 template<bool big_endian>
4718 static unsigned char*
4719 restgpr0(unsigned char* p, int r)
4721 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4722 write_insn<big_endian>(p, insn);
4726 template<bool big_endian>
4727 static unsigned char*
4728 restgpr0_tail(unsigned char* p, int r)
4730 uint32_t insn = ld_0_1 + 16;
4731 write_insn<big_endian>(p, insn);
4733 p = restgpr0<big_endian>(p, r);
4734 write_insn<big_endian>(p, mtlr_0);
4738 p = restgpr0<big_endian>(p, 30);
4739 p = restgpr0<big_endian>(p, 31);
4741 write_insn<big_endian>(p, blr);
4745 template<bool big_endian>
4746 static unsigned char*
4747 savegpr1(unsigned char* p, int r)
4749 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4750 write_insn<big_endian>(p, insn);
4754 template<bool big_endian>
4755 static unsigned char*
4756 savegpr1_tail(unsigned char* p, int r)
4758 p = savegpr1<big_endian>(p, r);
4759 write_insn<big_endian>(p, blr);
4763 template<bool big_endian>
4764 static unsigned char*
4765 restgpr1(unsigned char* p, int r)
4767 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4768 write_insn<big_endian>(p, insn);
4772 template<bool big_endian>
4773 static unsigned char*
4774 restgpr1_tail(unsigned char* p, int r)
4776 p = restgpr1<big_endian>(p, r);
4777 write_insn<big_endian>(p, blr);
4781 template<bool big_endian>
4782 static unsigned char*
4783 savefpr(unsigned char* p, int r)
4785 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4786 write_insn<big_endian>(p, insn);
4790 template<bool big_endian>
4791 static unsigned char*
4792 savefpr0_tail(unsigned char* p, int r)
4794 p = savefpr<big_endian>(p, r);
4795 write_insn<big_endian>(p, std_0_1 + 16);
4797 write_insn<big_endian>(p, blr);
4801 template<bool big_endian>
4802 static unsigned char*
4803 restfpr(unsigned char* p, int r)
4805 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4806 write_insn<big_endian>(p, insn);
4810 template<bool big_endian>
4811 static unsigned char*
4812 restfpr0_tail(unsigned char* p, int r)
4814 write_insn<big_endian>(p, ld_0_1 + 16);
4816 p = restfpr<big_endian>(p, r);
4817 write_insn<big_endian>(p, mtlr_0);
4821 p = restfpr<big_endian>(p, 30);
4822 p = restfpr<big_endian>(p, 31);
4824 write_insn<big_endian>(p, blr);
4828 template<bool big_endian>
4829 static unsigned char*
4830 savefpr1_tail(unsigned char* p, int r)
4832 p = savefpr<big_endian>(p, r);
4833 write_insn<big_endian>(p, blr);
4837 template<bool big_endian>
4838 static unsigned char*
4839 restfpr1_tail(unsigned char* p, int r)
4841 p = restfpr<big_endian>(p, r);
4842 write_insn<big_endian>(p, blr);
4846 template<bool big_endian>
4847 static unsigned char*
4848 savevr(unsigned char* p, int r)
4850 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4851 write_insn<big_endian>(p, insn);
4853 insn = stvx_0_12_0 + (r << 21);
4854 write_insn<big_endian>(p, insn);
4858 template<bool big_endian>
4859 static unsigned char*
4860 savevr_tail(unsigned char* p, int r)
4862 p = savevr<big_endian>(p, r);
4863 write_insn<big_endian>(p, blr);
4867 template<bool big_endian>
4868 static unsigned char*
4869 restvr(unsigned char* p, int r)
4871 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4872 write_insn<big_endian>(p, insn);
4874 insn = lvx_0_12_0 + (r << 21);
4875 write_insn<big_endian>(p, insn);
4879 template<bool big_endian>
4880 static unsigned char*
4881 restvr_tail(unsigned char* p, int r)
4883 p = restvr<big_endian>(p, r);
4884 write_insn<big_endian>(p, blr);
4889 template<int size, bool big_endian>
4890 Output_data_save_res<size, big_endian>::Output_data_save_res(
4891 Symbol_table* symtab)
4892 : Output_section_data_build(4),
4895 this->savres_define(symtab,
4896 "_savegpr0_", 14, 31,
4897 savegpr0<big_endian>, savegpr0_tail<big_endian>);
4898 this->savres_define(symtab,
4899 "_restgpr0_", 14, 29,
4900 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4901 this->savres_define(symtab,
4902 "_restgpr0_", 30, 31,
4903 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4904 this->savres_define(symtab,
4905 "_savegpr1_", 14, 31,
4906 savegpr1<big_endian>, savegpr1_tail<big_endian>);
4907 this->savres_define(symtab,
4908 "_restgpr1_", 14, 31,
4909 restgpr1<big_endian>, restgpr1_tail<big_endian>);
4910 this->savres_define(symtab,
4911 "_savefpr_", 14, 31,
4912 savefpr<big_endian>, savefpr0_tail<big_endian>);
4913 this->savres_define(symtab,
4914 "_restfpr_", 14, 29,
4915 restfpr<big_endian>, restfpr0_tail<big_endian>);
4916 this->savres_define(symtab,
4917 "_restfpr_", 30, 31,
4918 restfpr<big_endian>, restfpr0_tail<big_endian>);
4919 this->savres_define(symtab,
4921 savefpr<big_endian>, savefpr1_tail<big_endian>);
4922 this->savres_define(symtab,
4924 restfpr<big_endian>, restfpr1_tail<big_endian>);
4925 this->savres_define(symtab,
4927 savevr<big_endian>, savevr_tail<big_endian>);
4928 this->savres_define(symtab,
4930 restvr<big_endian>, restvr_tail<big_endian>);
4933 template<int size, bool big_endian>
4935 Output_data_save_res<size, big_endian>::savres_define(
4936 Symbol_table* symtab,
4938 unsigned int lo, unsigned int hi,
4939 unsigned char* write_ent(unsigned char*, int),
4940 unsigned char* write_tail(unsigned char*, int))
4942 size_t len = strlen(name);
4943 bool writing = false;
4946 memcpy(sym, name, len);
4949 for (unsigned int i = lo; i <= hi; i++)
4951 sym[len + 0] = i / 10 + '0';
4952 sym[len + 1] = i % 10 + '0';
4953 Symbol* gsym = symtab->lookup(sym);
4954 bool refd = gsym != NULL && gsym->is_undefined();
4955 writing = writing || refd;
4958 if (this->contents_ == NULL)
4959 this->contents_ = new unsigned char[this->savres_max];
4961 section_size_type value = this->current_data_size();
4962 unsigned char* p = this->contents_ + value;
4964 p = write_ent(p, i);
4966 p = write_tail(p, i);
4967 section_size_type cur_size = p - this->contents_;
4968 this->set_current_data_size(cur_size);
4970 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4971 this, value, cur_size - value,
4972 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4973 elfcpp::STV_HIDDEN, 0, false, false);
4978 // Write out save/restore.
4980 template<int size, bool big_endian>
4982 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4984 const section_size_type off = this->offset();
4985 const section_size_type oview_size =
4986 convert_to_section_size_type(this->data_size());
4987 unsigned char* const oview = of->get_output_view(off, oview_size);
4988 memcpy(oview, this->contents_, oview_size);
4989 of->write_output_view(off, oview_size, oview);
4993 // Create the glink section.
4995 template<int size, bool big_endian>
4997 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4999 if (this->glink_ == NULL)
5001 this->glink_ = new Output_data_glink<size, big_endian>(this);
5002 this->glink_->add_eh_frame(layout);
5003 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5004 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5005 this->glink_, ORDER_TEXT, false);
5009 // Create a PLT entry for a global symbol.
5011 template<int size, bool big_endian>
5013 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5017 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5018 && gsym->can_use_relative_reloc(false))
5020 if (this->iplt_ == NULL)
5021 this->make_iplt_section(symtab, layout);
5022 this->iplt_->add_ifunc_entry(gsym);
5026 if (this->plt_ == NULL)
5027 this->make_plt_section(symtab, layout);
5028 this->plt_->add_entry(gsym);
5032 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5034 template<int size, bool big_endian>
5036 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5037 Symbol_table* symtab,
5039 Sized_relobj_file<size, big_endian>* relobj,
5042 if (this->iplt_ == NULL)
5043 this->make_iplt_section(symtab, layout);
5044 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5047 // Return the number of entries in the PLT.
5049 template<int size, bool big_endian>
5051 Target_powerpc<size, big_endian>::plt_entry_count() const
5053 if (this->plt_ == NULL)
5055 return this->plt_->entry_count();
5058 // Create a GOT entry for local dynamic __tls_get_addr calls.
5060 template<int size, bool big_endian>
5062 Target_powerpc<size, big_endian>::tlsld_got_offset(
5063 Symbol_table* symtab,
5065 Sized_relobj_file<size, big_endian>* object)
5067 if (this->tlsld_got_offset_ == -1U)
5069 gold_assert(symtab != NULL && layout != NULL && object != NULL);
5070 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5071 Output_data_got_powerpc<size, big_endian>* got
5072 = this->got_section(symtab, layout);
5073 unsigned int got_offset = got->add_constant_pair(0, 0);
5074 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5076 this->tlsld_got_offset_ = got_offset;
5078 return this->tlsld_got_offset_;
5081 // Get the Reference_flags for a particular relocation.
5083 template<int size, bool big_endian>
5085 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5086 unsigned int r_type,
5087 const Target_powerpc* target)
5093 case elfcpp::R_POWERPC_NONE:
5094 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5095 case elfcpp::R_POWERPC_GNU_VTENTRY:
5096 case elfcpp::R_PPC64_TOC:
5097 // No symbol reference.
5100 case elfcpp::R_PPC64_ADDR64:
5101 case elfcpp::R_PPC64_UADDR64:
5102 case elfcpp::R_POWERPC_ADDR32:
5103 case elfcpp::R_POWERPC_UADDR32:
5104 case elfcpp::R_POWERPC_ADDR16:
5105 case elfcpp::R_POWERPC_UADDR16:
5106 case elfcpp::R_POWERPC_ADDR16_LO:
5107 case elfcpp::R_POWERPC_ADDR16_HI:
5108 case elfcpp::R_POWERPC_ADDR16_HA:
5109 ref = Symbol::ABSOLUTE_REF;
5112 case elfcpp::R_POWERPC_ADDR24:
5113 case elfcpp::R_POWERPC_ADDR14:
5114 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5115 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5116 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5119 case elfcpp::R_PPC64_REL64:
5120 case elfcpp::R_POWERPC_REL32:
5121 case elfcpp::R_PPC_LOCAL24PC:
5122 case elfcpp::R_POWERPC_REL16:
5123 case elfcpp::R_POWERPC_REL16_LO:
5124 case elfcpp::R_POWERPC_REL16_HI:
5125 case elfcpp::R_POWERPC_REL16_HA:
5126 ref = Symbol::RELATIVE_REF;
5129 case elfcpp::R_POWERPC_REL24:
5130 case elfcpp::R_PPC_PLTREL24:
5131 case elfcpp::R_POWERPC_REL14:
5132 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5133 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5134 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5137 case elfcpp::R_POWERPC_GOT16:
5138 case elfcpp::R_POWERPC_GOT16_LO:
5139 case elfcpp::R_POWERPC_GOT16_HI:
5140 case elfcpp::R_POWERPC_GOT16_HA:
5141 case elfcpp::R_PPC64_GOT16_DS:
5142 case elfcpp::R_PPC64_GOT16_LO_DS:
5143 case elfcpp::R_PPC64_TOC16:
5144 case elfcpp::R_PPC64_TOC16_LO:
5145 case elfcpp::R_PPC64_TOC16_HI:
5146 case elfcpp::R_PPC64_TOC16_HA:
5147 case elfcpp::R_PPC64_TOC16_DS:
5148 case elfcpp::R_PPC64_TOC16_LO_DS:
5150 ref = Symbol::ABSOLUTE_REF;
5153 case elfcpp::R_POWERPC_GOT_TPREL16:
5154 case elfcpp::R_POWERPC_TLS:
5155 ref = Symbol::TLS_REF;
5158 case elfcpp::R_POWERPC_COPY:
5159 case elfcpp::R_POWERPC_GLOB_DAT:
5160 case elfcpp::R_POWERPC_JMP_SLOT:
5161 case elfcpp::R_POWERPC_RELATIVE:
5162 case elfcpp::R_POWERPC_DTPMOD:
5164 // Not expected. We will give an error later.
5168 if (size == 64 && target->abiversion() < 2)
5169 ref |= Symbol::FUNC_DESC_ABI;
5173 // Report an unsupported relocation against a local symbol.
5175 template<int size, bool big_endian>
5177 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5178 Sized_relobj_file<size, big_endian>* object,
5179 unsigned int r_type)
5181 gold_error(_("%s: unsupported reloc %u against local symbol"),
5182 object->name().c_str(), r_type);
5185 // We are about to emit a dynamic relocation of type R_TYPE. If the
5186 // dynamic linker does not support it, issue an error.
5188 template<int size, bool big_endian>
5190 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5191 unsigned int r_type)
5193 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5195 // These are the relocation types supported by glibc for both 32-bit
5196 // and 64-bit powerpc.
5199 case elfcpp::R_POWERPC_NONE:
5200 case elfcpp::R_POWERPC_RELATIVE:
5201 case elfcpp::R_POWERPC_GLOB_DAT:
5202 case elfcpp::R_POWERPC_DTPMOD:
5203 case elfcpp::R_POWERPC_DTPREL:
5204 case elfcpp::R_POWERPC_TPREL:
5205 case elfcpp::R_POWERPC_JMP_SLOT:
5206 case elfcpp::R_POWERPC_COPY:
5207 case elfcpp::R_POWERPC_IRELATIVE:
5208 case elfcpp::R_POWERPC_ADDR32:
5209 case elfcpp::R_POWERPC_UADDR32:
5210 case elfcpp::R_POWERPC_ADDR24:
5211 case elfcpp::R_POWERPC_ADDR16:
5212 case elfcpp::R_POWERPC_UADDR16:
5213 case elfcpp::R_POWERPC_ADDR16_LO:
5214 case elfcpp::R_POWERPC_ADDR16_HI:
5215 case elfcpp::R_POWERPC_ADDR16_HA:
5216 case elfcpp::R_POWERPC_ADDR14:
5217 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5218 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5219 case elfcpp::R_POWERPC_REL32:
5220 case elfcpp::R_POWERPC_REL24:
5221 case elfcpp::R_POWERPC_TPREL16:
5222 case elfcpp::R_POWERPC_TPREL16_LO:
5223 case elfcpp::R_POWERPC_TPREL16_HI:
5224 case elfcpp::R_POWERPC_TPREL16_HA:
5235 // These are the relocation types supported only on 64-bit.
5236 case elfcpp::R_PPC64_ADDR64:
5237 case elfcpp::R_PPC64_UADDR64:
5238 case elfcpp::R_PPC64_JMP_IREL:
5239 case elfcpp::R_PPC64_ADDR16_DS:
5240 case elfcpp::R_PPC64_ADDR16_LO_DS:
5241 case elfcpp::R_PPC64_ADDR16_HIGH:
5242 case elfcpp::R_PPC64_ADDR16_HIGHA:
5243 case elfcpp::R_PPC64_ADDR16_HIGHER:
5244 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5245 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5246 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5247 case elfcpp::R_PPC64_REL64:
5248 case elfcpp::R_POWERPC_ADDR30:
5249 case elfcpp::R_PPC64_TPREL16_DS:
5250 case elfcpp::R_PPC64_TPREL16_LO_DS:
5251 case elfcpp::R_PPC64_TPREL16_HIGH:
5252 case elfcpp::R_PPC64_TPREL16_HIGHA:
5253 case elfcpp::R_PPC64_TPREL16_HIGHER:
5254 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5255 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5256 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5267 // These are the relocation types supported only on 32-bit.
5268 // ??? glibc ld.so doesn't need to support these.
5269 case elfcpp::R_POWERPC_DTPREL16:
5270 case elfcpp::R_POWERPC_DTPREL16_LO:
5271 case elfcpp::R_POWERPC_DTPREL16_HI:
5272 case elfcpp::R_POWERPC_DTPREL16_HA:
5280 // This prevents us from issuing more than one error per reloc
5281 // section. But we can still wind up issuing more than one
5282 // error per object file.
5283 if (this->issued_non_pic_error_)
5285 gold_assert(parameters->options().output_is_position_independent());
5286 object->error(_("requires unsupported dynamic reloc; "
5287 "recompile with -fPIC"));
5288 this->issued_non_pic_error_ = true;
5292 // Return whether we need to make a PLT entry for a relocation of the
5293 // given type against a STT_GNU_IFUNC symbol.
5295 template<int size, bool big_endian>
5297 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5298 Target_powerpc<size, big_endian>* target,
5299 Sized_relobj_file<size, big_endian>* object,
5300 unsigned int r_type,
5303 // In non-pic code any reference will resolve to the plt call stub
5304 // for the ifunc symbol.
5305 if ((size == 32 || target->abiversion() >= 2)
5306 && !parameters->options().output_is_position_independent())
5311 // Word size refs from data sections are OK, but don't need a PLT entry.
5312 case elfcpp::R_POWERPC_ADDR32:
5313 case elfcpp::R_POWERPC_UADDR32:
5318 case elfcpp::R_PPC64_ADDR64:
5319 case elfcpp::R_PPC64_UADDR64:
5324 // GOT refs are good, but also don't need a PLT entry.
5325 case elfcpp::R_POWERPC_GOT16:
5326 case elfcpp::R_POWERPC_GOT16_LO:
5327 case elfcpp::R_POWERPC_GOT16_HI:
5328 case elfcpp::R_POWERPC_GOT16_HA:
5329 case elfcpp::R_PPC64_GOT16_DS:
5330 case elfcpp::R_PPC64_GOT16_LO_DS:
5333 // Function calls are good, and these do need a PLT entry.
5334 case elfcpp::R_POWERPC_ADDR24:
5335 case elfcpp::R_POWERPC_ADDR14:
5336 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5337 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5338 case elfcpp::R_POWERPC_REL24:
5339 case elfcpp::R_PPC_PLTREL24:
5340 case elfcpp::R_POWERPC_REL14:
5341 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5342 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5349 // Anything else is a problem.
5350 // If we are building a static executable, the libc startup function
5351 // responsible for applying indirect function relocations is going
5352 // to complain about the reloc type.
5353 // If we are building a dynamic executable, we will have a text
5354 // relocation. The dynamic loader will set the text segment
5355 // writable and non-executable to apply text relocations. So we'll
5356 // segfault when trying to run the indirection function to resolve
5359 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5360 object->name().c_str(), r_type);
5364 // Scan a relocation for a local symbol.
5366 template<int size, bool big_endian>
5368 Target_powerpc<size, big_endian>::Scan::local(
5369 Symbol_table* symtab,
5371 Target_powerpc<size, big_endian>* target,
5372 Sized_relobj_file<size, big_endian>* object,
5373 unsigned int data_shndx,
5374 Output_section* output_section,
5375 const elfcpp::Rela<size, big_endian>& reloc,
5376 unsigned int r_type,
5377 const elfcpp::Sym<size, big_endian>& lsym,
5380 this->maybe_skip_tls_get_addr_call(r_type, NULL);
5382 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5383 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5385 this->expect_tls_get_addr_call();
5386 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5387 if (tls_type != tls::TLSOPT_NONE)
5388 this->skip_next_tls_get_addr_call();
5390 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5391 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5393 this->expect_tls_get_addr_call();
5394 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5395 if (tls_type != tls::TLSOPT_NONE)
5396 this->skip_next_tls_get_addr_call();
5399 Powerpc_relobj<size, big_endian>* ppc_object
5400 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5405 && data_shndx == ppc_object->opd_shndx()
5406 && r_type == elfcpp::R_PPC64_ADDR64)
5407 ppc_object->set_opd_discard(reloc.get_r_offset());
5411 // A local STT_GNU_IFUNC symbol may require a PLT entry.
5412 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5413 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5415 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5416 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5417 r_type, r_sym, reloc.get_r_addend());
5418 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5423 case elfcpp::R_POWERPC_NONE:
5424 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5425 case elfcpp::R_POWERPC_GNU_VTENTRY:
5426 case elfcpp::R_PPC64_TOCSAVE:
5427 case elfcpp::R_POWERPC_TLS:
5430 case elfcpp::R_PPC64_TOC:
5432 Output_data_got_powerpc<size, big_endian>* got
5433 = target->got_section(symtab, layout);
5434 if (parameters->options().output_is_position_independent())
5436 Address off = reloc.get_r_offset();
5438 && target->abiversion() < 2
5439 && data_shndx == ppc_object->opd_shndx()
5440 && ppc_object->get_opd_discard(off - 8))
5443 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5444 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5445 rela_dyn->add_output_section_relative(got->output_section(),
5446 elfcpp::R_POWERPC_RELATIVE,
5448 object, data_shndx, off,
5449 symobj->toc_base_offset());
5454 case elfcpp::R_PPC64_ADDR64:
5455 case elfcpp::R_PPC64_UADDR64:
5456 case elfcpp::R_POWERPC_ADDR32:
5457 case elfcpp::R_POWERPC_UADDR32:
5458 case elfcpp::R_POWERPC_ADDR24:
5459 case elfcpp::R_POWERPC_ADDR16:
5460 case elfcpp::R_POWERPC_ADDR16_LO:
5461 case elfcpp::R_POWERPC_ADDR16_HI:
5462 case elfcpp::R_POWERPC_ADDR16_HA:
5463 case elfcpp::R_POWERPC_UADDR16:
5464 case elfcpp::R_PPC64_ADDR16_HIGH:
5465 case elfcpp::R_PPC64_ADDR16_HIGHA:
5466 case elfcpp::R_PPC64_ADDR16_HIGHER:
5467 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5468 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5469 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5470 case elfcpp::R_PPC64_ADDR16_DS:
5471 case elfcpp::R_PPC64_ADDR16_LO_DS:
5472 case elfcpp::R_POWERPC_ADDR14:
5473 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5474 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5475 // If building a shared library (or a position-independent
5476 // executable), we need to create a dynamic relocation for
5478 if (parameters->options().output_is_position_independent()
5479 || (size == 64 && is_ifunc && target->abiversion() < 2))
5481 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5483 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5484 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5485 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5487 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5488 : elfcpp::R_POWERPC_RELATIVE);
5489 rela_dyn->add_local_relative(object, r_sym, dynrel,
5490 output_section, data_shndx,
5491 reloc.get_r_offset(),
5492 reloc.get_r_addend(), false);
5494 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
5496 check_non_pic(object, r_type);
5497 rela_dyn->add_local(object, r_sym, r_type, output_section,
5498 data_shndx, reloc.get_r_offset(),
5499 reloc.get_r_addend());
5503 gold_assert(lsym.get_st_value() == 0);
5504 unsigned int shndx = lsym.get_st_shndx();
5506 shndx = object->adjust_sym_shndx(r_sym, shndx,
5509 object->error(_("section symbol %u has bad shndx %u"),
5512 rela_dyn->add_local_section(object, shndx, r_type,
5513 output_section, data_shndx,
5514 reloc.get_r_offset());
5519 case elfcpp::R_POWERPC_REL24:
5520 case elfcpp::R_PPC_PLTREL24:
5521 case elfcpp::R_PPC_LOCAL24PC:
5522 case elfcpp::R_POWERPC_REL14:
5523 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5524 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5526 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5527 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5528 reloc.get_r_addend());
5531 case elfcpp::R_PPC64_REL64:
5532 case elfcpp::R_POWERPC_REL32:
5533 case elfcpp::R_POWERPC_REL16:
5534 case elfcpp::R_POWERPC_REL16_LO:
5535 case elfcpp::R_POWERPC_REL16_HI:
5536 case elfcpp::R_POWERPC_REL16_HA:
5537 case elfcpp::R_POWERPC_SECTOFF:
5538 case elfcpp::R_POWERPC_SECTOFF_LO:
5539 case elfcpp::R_POWERPC_SECTOFF_HI:
5540 case elfcpp::R_POWERPC_SECTOFF_HA:
5541 case elfcpp::R_PPC64_SECTOFF_DS:
5542 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5543 case elfcpp::R_POWERPC_TPREL16:
5544 case elfcpp::R_POWERPC_TPREL16_LO:
5545 case elfcpp::R_POWERPC_TPREL16_HI:
5546 case elfcpp::R_POWERPC_TPREL16_HA:
5547 case elfcpp::R_PPC64_TPREL16_DS:
5548 case elfcpp::R_PPC64_TPREL16_LO_DS:
5549 case elfcpp::R_PPC64_TPREL16_HIGH:
5550 case elfcpp::R_PPC64_TPREL16_HIGHA:
5551 case elfcpp::R_PPC64_TPREL16_HIGHER:
5552 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5553 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5554 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5555 case elfcpp::R_POWERPC_DTPREL16:
5556 case elfcpp::R_POWERPC_DTPREL16_LO:
5557 case elfcpp::R_POWERPC_DTPREL16_HI:
5558 case elfcpp::R_POWERPC_DTPREL16_HA:
5559 case elfcpp::R_PPC64_DTPREL16_DS:
5560 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5561 case elfcpp::R_PPC64_DTPREL16_HIGH:
5562 case elfcpp::R_PPC64_DTPREL16_HIGHA:
5563 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5564 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5565 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5566 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5567 case elfcpp::R_PPC64_TLSGD:
5568 case elfcpp::R_PPC64_TLSLD:
5569 case elfcpp::R_PPC64_ADDR64_LOCAL:
5572 case elfcpp::R_POWERPC_GOT16:
5573 case elfcpp::R_POWERPC_GOT16_LO:
5574 case elfcpp::R_POWERPC_GOT16_HI:
5575 case elfcpp::R_POWERPC_GOT16_HA:
5576 case elfcpp::R_PPC64_GOT16_DS:
5577 case elfcpp::R_PPC64_GOT16_LO_DS:
5579 // The symbol requires a GOT entry.
5580 Output_data_got_powerpc<size, big_endian>* got
5581 = target->got_section(symtab, layout);
5582 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5584 if (!parameters->options().output_is_position_independent())
5586 if ((size == 32 && is_ifunc)
5587 || (size == 64 && target->abiversion() >= 2))
5588 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5590 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5592 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5594 // If we are generating a shared object or a pie, this
5595 // symbol's GOT entry will be set by a dynamic relocation.
5597 off = got->add_constant(0);
5598 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5600 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5602 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5603 : elfcpp::R_POWERPC_RELATIVE);
5604 rela_dyn->add_local_relative(object, r_sym, dynrel,
5605 got, off, 0, false);
5610 case elfcpp::R_PPC64_TOC16:
5611 case elfcpp::R_PPC64_TOC16_LO:
5612 case elfcpp::R_PPC64_TOC16_HI:
5613 case elfcpp::R_PPC64_TOC16_HA:
5614 case elfcpp::R_PPC64_TOC16_DS:
5615 case elfcpp::R_PPC64_TOC16_LO_DS:
5616 // We need a GOT section.
5617 target->got_section(symtab, layout);
5620 case elfcpp::R_POWERPC_GOT_TLSGD16:
5621 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5622 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5623 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5625 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5626 if (tls_type == tls::TLSOPT_NONE)
5628 Output_data_got_powerpc<size, big_endian>* got
5629 = target->got_section(symtab, layout);
5630 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5631 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5632 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5633 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5635 else if (tls_type == tls::TLSOPT_TO_LE)
5637 // no GOT relocs needed for Local Exec.
5644 case elfcpp::R_POWERPC_GOT_TLSLD16:
5645 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5646 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5647 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5649 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5650 if (tls_type == tls::TLSOPT_NONE)
5651 target->tlsld_got_offset(symtab, layout, object);
5652 else if (tls_type == tls::TLSOPT_TO_LE)
5654 // no GOT relocs needed for Local Exec.
5655 if (parameters->options().emit_relocs())
5657 Output_section* os = layout->tls_segment()->first_section();
5658 gold_assert(os != NULL);
5659 os->set_needs_symtab_index();
5667 case elfcpp::R_POWERPC_GOT_DTPREL16:
5668 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5669 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5670 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5672 Output_data_got_powerpc<size, big_endian>* got
5673 = target->got_section(symtab, layout);
5674 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5675 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5679 case elfcpp::R_POWERPC_GOT_TPREL16:
5680 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5681 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5682 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5684 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5685 if (tls_type == tls::TLSOPT_NONE)
5687 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5688 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5690 Output_data_got_powerpc<size, big_endian>* got
5691 = target->got_section(symtab, layout);
5692 unsigned int off = got->add_constant(0);
5693 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5695 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5696 rela_dyn->add_symbolless_local_addend(object, r_sym,
5697 elfcpp::R_POWERPC_TPREL,
5701 else if (tls_type == tls::TLSOPT_TO_LE)
5703 // no GOT relocs needed for Local Exec.
5711 unsupported_reloc_local(object, r_type);
5717 case elfcpp::R_POWERPC_GOT_TLSLD16:
5718 case elfcpp::R_POWERPC_GOT_TLSGD16:
5719 case elfcpp::R_POWERPC_GOT_TPREL16:
5720 case elfcpp::R_POWERPC_GOT_DTPREL16:
5721 case elfcpp::R_POWERPC_GOT16:
5722 case elfcpp::R_PPC64_GOT16_DS:
5723 case elfcpp::R_PPC64_TOC16:
5724 case elfcpp::R_PPC64_TOC16_DS:
5725 ppc_object->set_has_small_toc_reloc();
5731 // Report an unsupported relocation against a global symbol.
5733 template<int size, bool big_endian>
5735 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5736 Sized_relobj_file<size, big_endian>* object,
5737 unsigned int r_type,
5740 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5741 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5744 // Scan a relocation for a global symbol.
5746 template<int size, bool big_endian>
5748 Target_powerpc<size, big_endian>::Scan::global(
5749 Symbol_table* symtab,
5751 Target_powerpc<size, big_endian>* target,
5752 Sized_relobj_file<size, big_endian>* object,
5753 unsigned int data_shndx,
5754 Output_section* output_section,
5755 const elfcpp::Rela<size, big_endian>& reloc,
5756 unsigned int r_type,
5759 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5762 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5763 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5765 this->expect_tls_get_addr_call();
5766 const bool final = gsym->final_value_is_known();
5767 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5768 if (tls_type != tls::TLSOPT_NONE)
5769 this->skip_next_tls_get_addr_call();
5771 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5772 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5774 this->expect_tls_get_addr_call();
5775 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5776 if (tls_type != tls::TLSOPT_NONE)
5777 this->skip_next_tls_get_addr_call();
5780 Powerpc_relobj<size, big_endian>* ppc_object
5781 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5783 // A STT_GNU_IFUNC symbol may require a PLT entry.
5784 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5785 bool pushed_ifunc = false;
5786 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5788 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5789 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5790 reloc.get_r_addend());
5791 target->make_plt_entry(symtab, layout, gsym);
5792 pushed_ifunc = true;
5797 case elfcpp::R_POWERPC_NONE:
5798 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5799 case elfcpp::R_POWERPC_GNU_VTENTRY:
5800 case elfcpp::R_PPC_LOCAL24PC:
5801 case elfcpp::R_POWERPC_TLS:
5804 case elfcpp::R_PPC64_TOC:
5806 Output_data_got_powerpc<size, big_endian>* got
5807 = target->got_section(symtab, layout);
5808 if (parameters->options().output_is_position_independent())
5810 Address off = reloc.get_r_offset();
5812 && data_shndx == ppc_object->opd_shndx()
5813 && ppc_object->get_opd_discard(off - 8))
5816 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5817 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5818 if (data_shndx != ppc_object->opd_shndx())
5819 symobj = static_cast
5820 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5821 rela_dyn->add_output_section_relative(got->output_section(),
5822 elfcpp::R_POWERPC_RELATIVE,
5824 object, data_shndx, off,
5825 symobj->toc_base_offset());
5830 case elfcpp::R_PPC64_ADDR64:
5832 && target->abiversion() < 2
5833 && data_shndx == ppc_object->opd_shndx()
5834 && (gsym->is_defined_in_discarded_section()
5835 || gsym->object() != object))
5837 ppc_object->set_opd_discard(reloc.get_r_offset());
5841 case elfcpp::R_PPC64_UADDR64:
5842 case elfcpp::R_POWERPC_ADDR32:
5843 case elfcpp::R_POWERPC_UADDR32:
5844 case elfcpp::R_POWERPC_ADDR24:
5845 case elfcpp::R_POWERPC_ADDR16:
5846 case elfcpp::R_POWERPC_ADDR16_LO:
5847 case elfcpp::R_POWERPC_ADDR16_HI:
5848 case elfcpp::R_POWERPC_ADDR16_HA:
5849 case elfcpp::R_POWERPC_UADDR16:
5850 case elfcpp::R_PPC64_ADDR16_HIGH:
5851 case elfcpp::R_PPC64_ADDR16_HIGHA:
5852 case elfcpp::R_PPC64_ADDR16_HIGHER:
5853 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5854 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5855 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5856 case elfcpp::R_PPC64_ADDR16_DS:
5857 case elfcpp::R_PPC64_ADDR16_LO_DS:
5858 case elfcpp::R_POWERPC_ADDR14:
5859 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5860 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5862 // Make a PLT entry if necessary.
5863 if (gsym->needs_plt_entry())
5865 // Since this is not a PC-relative relocation, we may be
5866 // taking the address of a function. In that case we need to
5867 // set the entry in the dynamic symbol table to the address of
5868 // the PLT call stub.
5869 bool need_ifunc_plt = false;
5870 if ((size == 32 || target->abiversion() >= 2)
5871 && gsym->is_from_dynobj()
5872 && !parameters->options().output_is_position_independent())
5874 gsym->set_needs_dynsym_value();
5875 need_ifunc_plt = true;
5877 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
5879 target->push_branch(ppc_object, data_shndx,
5880 reloc.get_r_offset(), r_type,
5881 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5882 reloc.get_r_addend());
5883 target->make_plt_entry(symtab, layout, gsym);
5886 // Make a dynamic relocation if necessary.
5887 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
5888 || (size == 64 && is_ifunc && target->abiversion() < 2))
5890 if (!parameters->options().output_is_position_independent()
5891 && gsym->may_need_copy_reloc())
5893 target->copy_reloc(symtab, layout, object,
5894 data_shndx, output_section, gsym, reloc);
5896 else if ((((size == 32
5897 && r_type == elfcpp::R_POWERPC_ADDR32)
5899 && r_type == elfcpp::R_PPC64_ADDR64
5900 && target->abiversion() >= 2))
5901 && gsym->can_use_relative_reloc(false)
5902 && !(gsym->visibility() == elfcpp::STV_PROTECTED
5903 && parameters->options().shared()))
5905 && r_type == elfcpp::R_PPC64_ADDR64
5906 && target->abiversion() < 2
5907 && (gsym->can_use_relative_reloc(false)
5908 || data_shndx == ppc_object->opd_shndx())))
5910 Reloc_section* rela_dyn
5911 = target->rela_dyn_section(symtab, layout, is_ifunc);
5912 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5913 : elfcpp::R_POWERPC_RELATIVE);
5914 rela_dyn->add_symbolless_global_addend(
5915 gsym, dynrel, output_section, object, data_shndx,
5916 reloc.get_r_offset(), reloc.get_r_addend());
5920 Reloc_section* rela_dyn
5921 = target->rela_dyn_section(symtab, layout, is_ifunc);
5922 check_non_pic(object, r_type);
5923 rela_dyn->add_global(gsym, r_type, output_section,
5925 reloc.get_r_offset(),
5926 reloc.get_r_addend());
5932 case elfcpp::R_PPC_PLTREL24:
5933 case elfcpp::R_POWERPC_REL24:
5936 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5938 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5939 reloc.get_r_addend());
5940 if (gsym->needs_plt_entry()
5941 || (!gsym->final_value_is_known()
5942 && (gsym->is_undefined()
5943 || gsym->is_from_dynobj()
5944 || gsym->is_preemptible())))
5945 target->make_plt_entry(symtab, layout, gsym);
5949 case elfcpp::R_PPC64_REL64:
5950 case elfcpp::R_POWERPC_REL32:
5951 // Make a dynamic relocation if necessary.
5952 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
5954 if (!parameters->options().output_is_position_independent()
5955 && gsym->may_need_copy_reloc())
5957 target->copy_reloc(symtab, layout, object,
5958 data_shndx, output_section, gsym,
5963 Reloc_section* rela_dyn
5964 = target->rela_dyn_section(symtab, layout, is_ifunc);
5965 check_non_pic(object, r_type);
5966 rela_dyn->add_global(gsym, r_type, output_section, object,
5967 data_shndx, reloc.get_r_offset(),
5968 reloc.get_r_addend());
5973 case elfcpp::R_POWERPC_REL14:
5974 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5975 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5977 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5978 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5979 reloc.get_r_addend());
5982 case elfcpp::R_POWERPC_REL16:
5983 case elfcpp::R_POWERPC_REL16_LO:
5984 case elfcpp::R_POWERPC_REL16_HI:
5985 case elfcpp::R_POWERPC_REL16_HA:
5986 case elfcpp::R_POWERPC_SECTOFF:
5987 case elfcpp::R_POWERPC_SECTOFF_LO:
5988 case elfcpp::R_POWERPC_SECTOFF_HI:
5989 case elfcpp::R_POWERPC_SECTOFF_HA:
5990 case elfcpp::R_PPC64_SECTOFF_DS:
5991 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5992 case elfcpp::R_POWERPC_TPREL16:
5993 case elfcpp::R_POWERPC_TPREL16_LO:
5994 case elfcpp::R_POWERPC_TPREL16_HI:
5995 case elfcpp::R_POWERPC_TPREL16_HA:
5996 case elfcpp::R_PPC64_TPREL16_DS:
5997 case elfcpp::R_PPC64_TPREL16_LO_DS:
5998 case elfcpp::R_PPC64_TPREL16_HIGH:
5999 case elfcpp::R_PPC64_TPREL16_HIGHA:
6000 case elfcpp::R_PPC64_TPREL16_HIGHER:
6001 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6002 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6003 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6004 case elfcpp::R_POWERPC_DTPREL16:
6005 case elfcpp::R_POWERPC_DTPREL16_LO:
6006 case elfcpp::R_POWERPC_DTPREL16_HI:
6007 case elfcpp::R_POWERPC_DTPREL16_HA:
6008 case elfcpp::R_PPC64_DTPREL16_DS:
6009 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6010 case elfcpp::R_PPC64_DTPREL16_HIGH:
6011 case elfcpp::R_PPC64_DTPREL16_HIGHA:
6012 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6013 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6014 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6015 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6016 case elfcpp::R_PPC64_TLSGD:
6017 case elfcpp::R_PPC64_TLSLD:
6018 case elfcpp::R_PPC64_ADDR64_LOCAL:
6021 case elfcpp::R_POWERPC_GOT16:
6022 case elfcpp::R_POWERPC_GOT16_LO:
6023 case elfcpp::R_POWERPC_GOT16_HI:
6024 case elfcpp::R_POWERPC_GOT16_HA:
6025 case elfcpp::R_PPC64_GOT16_DS:
6026 case elfcpp::R_PPC64_GOT16_LO_DS:
6028 // The symbol requires a GOT entry.
6029 Output_data_got_powerpc<size, big_endian>* got;
6031 got = target->got_section(symtab, layout);
6032 if (gsym->final_value_is_known())
6034 if ((size == 32 && is_ifunc)
6035 || (size == 64 && target->abiversion() >= 2))
6036 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6038 got->add_global(gsym, GOT_TYPE_STANDARD);
6040 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
6042 // If we are generating a shared object or a pie, this
6043 // symbol's GOT entry will be set by a dynamic relocation.
6044 unsigned int off = got->add_constant(0);
6045 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
6047 Reloc_section* rela_dyn
6048 = target->rela_dyn_section(symtab, layout, is_ifunc);
6050 if (gsym->can_use_relative_reloc(false)
6052 || target->abiversion() >= 2)
6053 && gsym->visibility() == elfcpp::STV_PROTECTED
6054 && parameters->options().shared()))
6056 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6057 : elfcpp::R_POWERPC_RELATIVE);
6058 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
6062 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
6063 rela_dyn->add_global(gsym, dynrel, got, off, 0);
6069 case elfcpp::R_PPC64_TOC16:
6070 case elfcpp::R_PPC64_TOC16_LO:
6071 case elfcpp::R_PPC64_TOC16_HI:
6072 case elfcpp::R_PPC64_TOC16_HA:
6073 case elfcpp::R_PPC64_TOC16_DS:
6074 case elfcpp::R_PPC64_TOC16_LO_DS:
6075 // We need a GOT section.
6076 target->got_section(symtab, layout);
6079 case elfcpp::R_POWERPC_GOT_TLSGD16:
6080 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6081 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6082 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6084 const bool final = gsym->final_value_is_known();
6085 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6086 if (tls_type == tls::TLSOPT_NONE)
6088 Output_data_got_powerpc<size, big_endian>* got
6089 = target->got_section(symtab, layout);
6090 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6091 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
6092 elfcpp::R_POWERPC_DTPMOD,
6093 elfcpp::R_POWERPC_DTPREL);
6095 else if (tls_type == tls::TLSOPT_TO_IE)
6097 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6099 Output_data_got_powerpc<size, big_endian>* got
6100 = target->got_section(symtab, layout);
6101 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6102 if (gsym->is_undefined()
6103 || gsym->is_from_dynobj())
6105 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6106 elfcpp::R_POWERPC_TPREL);
6110 unsigned int off = got->add_constant(0);
6111 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6112 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6113 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6118 else if (tls_type == tls::TLSOPT_TO_LE)
6120 // no GOT relocs needed for Local Exec.
6127 case elfcpp::R_POWERPC_GOT_TLSLD16:
6128 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6129 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6130 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6132 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6133 if (tls_type == tls::TLSOPT_NONE)
6134 target->tlsld_got_offset(symtab, layout, object);
6135 else if (tls_type == tls::TLSOPT_TO_LE)
6137 // no GOT relocs needed for Local Exec.
6138 if (parameters->options().emit_relocs())
6140 Output_section* os = layout->tls_segment()->first_section();
6141 gold_assert(os != NULL);
6142 os->set_needs_symtab_index();
6150 case elfcpp::R_POWERPC_GOT_DTPREL16:
6151 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6152 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6153 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6155 Output_data_got_powerpc<size, big_endian>* got
6156 = target->got_section(symtab, layout);
6157 if (!gsym->final_value_is_known()
6158 && (gsym->is_from_dynobj()
6159 || gsym->is_undefined()
6160 || gsym->is_preemptible()))
6161 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6162 target->rela_dyn_section(layout),
6163 elfcpp::R_POWERPC_DTPREL);
6165 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
6169 case elfcpp::R_POWERPC_GOT_TPREL16:
6170 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6171 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6172 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6174 const bool final = gsym->final_value_is_known();
6175 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6176 if (tls_type == tls::TLSOPT_NONE)
6178 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6180 Output_data_got_powerpc<size, big_endian>* got
6181 = target->got_section(symtab, layout);
6182 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6183 if (gsym->is_undefined()
6184 || gsym->is_from_dynobj())
6186 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6187 elfcpp::R_POWERPC_TPREL);
6191 unsigned int off = got->add_constant(0);
6192 gsym->set_got_offset(GOT_TYPE_TPREL, off);
6193 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6194 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6199 else if (tls_type == tls::TLSOPT_TO_LE)
6201 // no GOT relocs needed for Local Exec.
6209 unsupported_reloc_global(object, r_type, gsym);
6215 case elfcpp::R_POWERPC_GOT_TLSLD16:
6216 case elfcpp::R_POWERPC_GOT_TLSGD16:
6217 case elfcpp::R_POWERPC_GOT_TPREL16:
6218 case elfcpp::R_POWERPC_GOT_DTPREL16:
6219 case elfcpp::R_POWERPC_GOT16:
6220 case elfcpp::R_PPC64_GOT16_DS:
6221 case elfcpp::R_PPC64_TOC16:
6222 case elfcpp::R_PPC64_TOC16_DS:
6223 ppc_object->set_has_small_toc_reloc();
6229 // Process relocations for gc.
6231 template<int size, bool big_endian>
6233 Target_powerpc<size, big_endian>::gc_process_relocs(
6234 Symbol_table* symtab,
6236 Sized_relobj_file<size, big_endian>* object,
6237 unsigned int data_shndx,
6239 const unsigned char* prelocs,
6241 Output_section* output_section,
6242 bool needs_special_offset_handling,
6243 size_t local_symbol_count,
6244 const unsigned char* plocal_symbols)
6246 typedef Target_powerpc<size, big_endian> Powerpc;
6247 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6248 Powerpc_relobj<size, big_endian>* ppc_object
6249 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6251 ppc_object->set_opd_valid();
6252 if (size == 64 && data_shndx == ppc_object->opd_shndx())
6254 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
6255 for (p = ppc_object->access_from_map()->begin();
6256 p != ppc_object->access_from_map()->end();
6259 Address dst_off = p->first;
6260 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6261 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
6262 for (s = p->second.begin(); s != p->second.end(); ++s)
6264 Object* src_obj = s->first;
6265 unsigned int src_indx = s->second;
6266 symtab->gc()->add_reference(src_obj, src_indx,
6267 ppc_object, dst_indx);
6271 ppc_object->access_from_map()->clear();
6272 ppc_object->process_gc_mark(symtab);
6273 // Don't look at .opd relocs as .opd will reference everything.
6277 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
6278 typename Target_powerpc::Relocatable_size_for_reloc>(
6287 needs_special_offset_handling,
6292 // Handle target specific gc actions when adding a gc reference from
6293 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6294 // and DST_OFF. For powerpc64, this adds a referenc to the code
6295 // section of a function descriptor.
6297 template<int size, bool big_endian>
6299 Target_powerpc<size, big_endian>::do_gc_add_reference(
6300 Symbol_table* symtab,
6302 unsigned int src_shndx,
6304 unsigned int dst_shndx,
6305 Address dst_off) const
6307 if (size != 64 || dst_obj->is_dynamic())
6310 Powerpc_relobj<size, big_endian>* ppc_object
6311 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
6312 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
6314 if (ppc_object->opd_valid())
6316 dst_shndx = ppc_object->get_opd_ent(dst_off);
6317 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
6321 // If we haven't run scan_opd_relocs, we must delay
6322 // processing this function descriptor reference.
6323 ppc_object->add_reference(src_obj, src_shndx, dst_off);
6328 // Add any special sections for this symbol to the gc work list.
6329 // For powerpc64, this adds the code section of a function
6332 template<int size, bool big_endian>
6334 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
6335 Symbol_table* symtab,
6340 Powerpc_relobj<size, big_endian>* ppc_object
6341 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
6343 unsigned int shndx = sym->shndx(&is_ordinary);
6344 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
6346 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
6347 Address dst_off = gsym->value();
6348 if (ppc_object->opd_valid())
6350 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6351 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
6354 ppc_object->add_gc_mark(dst_off);
6359 // For a symbol location in .opd, set LOC to the location of the
6362 template<int size, bool big_endian>
6364 Target_powerpc<size, big_endian>::do_function_location(
6365 Symbol_location* loc) const
6367 if (size == 64 && loc->shndx != 0)
6369 if (loc->object->is_dynamic())
6371 Powerpc_dynobj<size, big_endian>* ppc_object
6372 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
6373 if (loc->shndx == ppc_object->opd_shndx())
6376 Address off = loc->offset - ppc_object->opd_address();
6377 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
6378 loc->offset = dest_off;
6383 const Powerpc_relobj<size, big_endian>* ppc_object
6384 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
6385 if (loc->shndx == ppc_object->opd_shndx())
6388 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
6389 loc->offset = dest_off;
6395 // Scan relocations for a section.
6397 template<int size, bool big_endian>
6399 Target_powerpc<size, big_endian>::scan_relocs(
6400 Symbol_table* symtab,
6402 Sized_relobj_file<size, big_endian>* object,
6403 unsigned int data_shndx,
6404 unsigned int sh_type,
6405 const unsigned char* prelocs,
6407 Output_section* output_section,
6408 bool needs_special_offset_handling,
6409 size_t local_symbol_count,
6410 const unsigned char* plocal_symbols)
6412 typedef Target_powerpc<size, big_endian> Powerpc;
6413 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6415 if (sh_type == elfcpp::SHT_REL)
6417 gold_error(_("%s: unsupported REL reloc section"),
6418 object->name().c_str());
6422 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
6431 needs_special_offset_handling,
6436 // Functor class for processing the global symbol table.
6437 // Removes symbols defined on discarded opd entries.
6439 template<bool big_endian>
6440 class Global_symbol_visitor_opd
6443 Global_symbol_visitor_opd()
6447 operator()(Sized_symbol<64>* sym)
6449 if (sym->has_symtab_index()
6450 || sym->source() != Symbol::FROM_OBJECT
6451 || !sym->in_real_elf())
6454 if (sym->object()->is_dynamic())
6457 Powerpc_relobj<64, big_endian>* symobj
6458 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6459 if (symobj->opd_shndx() == 0)
6463 unsigned int shndx = sym->shndx(&is_ordinary);
6464 if (shndx == symobj->opd_shndx()
6465 && symobj->get_opd_discard(sym->value()))
6467 sym->set_undefined();
6468 sym->set_is_defined_in_discarded_section();
6469 sym->set_symtab_index(-1U);
6474 template<int size, bool big_endian>
6476 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6478 Symbol_table* symtab)
6482 Output_data_save_res<64, big_endian>* savres
6483 = new Output_data_save_res<64, big_endian>(symtab);
6484 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6485 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6486 savres, ORDER_TEXT, false);
6490 // Sort linker created .got section first (for the header), then input
6491 // sections belonging to files using small model code.
6493 template<bool big_endian>
6494 class Sort_toc_sections
6498 operator()(const Output_section::Input_section& is1,
6499 const Output_section::Input_section& is2) const
6501 if (!is1.is_input_section() && is2.is_input_section())
6504 = (is1.is_input_section()
6505 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6506 ->has_small_toc_reloc()));
6508 = (is2.is_input_section()
6509 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6510 ->has_small_toc_reloc()));
6511 return small1 && !small2;
6515 // Finalize the sections.
6517 template<int size, bool big_endian>
6519 Target_powerpc<size, big_endian>::do_finalize_sections(
6521 const Input_objects*,
6522 Symbol_table* symtab)
6524 if (parameters->doing_static_link())
6526 // At least some versions of glibc elf-init.o have a strong
6527 // reference to __rela_iplt marker syms. A weak ref would be
6529 if (this->iplt_ != NULL)
6531 Reloc_section* rel = this->iplt_->rel_plt();
6532 symtab->define_in_output_data("__rela_iplt_start", NULL,
6533 Symbol_table::PREDEFINED, rel, 0, 0,
6534 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6535 elfcpp::STV_HIDDEN, 0, false, true);
6536 symtab->define_in_output_data("__rela_iplt_end", NULL,
6537 Symbol_table::PREDEFINED, rel, 0, 0,
6538 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6539 elfcpp::STV_HIDDEN, 0, true, true);
6543 symtab->define_as_constant("__rela_iplt_start", NULL,
6544 Symbol_table::PREDEFINED, 0, 0,
6545 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6546 elfcpp::STV_HIDDEN, 0, true, false);
6547 symtab->define_as_constant("__rela_iplt_end", NULL,
6548 Symbol_table::PREDEFINED, 0, 0,
6549 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6550 elfcpp::STV_HIDDEN, 0, true, false);
6556 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6557 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6559 if (!parameters->options().relocatable())
6561 this->define_save_restore_funcs(layout, symtab);
6563 // Annoyingly, we need to make these sections now whether or
6564 // not we need them. If we delay until do_relax then we
6565 // need to mess with the relaxation machinery checkpointing.
6566 this->got_section(symtab, layout);
6567 this->make_brlt_section(layout);
6569 if (parameters->options().toc_sort())
6571 Output_section* os = this->got_->output_section();
6572 if (os != NULL && os->input_sections().size() > 1)
6573 std::stable_sort(os->input_sections().begin(),
6574 os->input_sections().end(),
6575 Sort_toc_sections<big_endian>());
6580 // Fill in some more dynamic tags.
6581 Output_data_dynamic* odyn = layout->dynamic_data();
6584 const Reloc_section* rel_plt = (this->plt_ == NULL
6586 : this->plt_->rel_plt());
6587 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6588 this->rela_dyn_, true, size == 32);
6592 if (this->got_ != NULL)
6594 this->got_->finalize_data_size();
6595 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6596 this->got_, this->got_->g_o_t());
6601 if (this->glink_ != NULL)
6603 this->glink_->finalize_data_size();
6604 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6606 (this->glink_->pltresolve_size
6612 // Emit any relocs we saved in an attempt to avoid generating COPY
6614 if (this->copy_relocs_.any_saved_relocs())
6615 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6618 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6622 ok_lo_toc_insn(uint32_t insn)
6624 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6625 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6626 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6627 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6628 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6629 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6630 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6631 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6632 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6633 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6634 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6635 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6636 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6637 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6638 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6640 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6641 && ((insn & 3) == 0 || (insn & 3) == 3))
6642 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6645 // Return the value to use for a branch relocation.
6647 template<int size, bool big_endian>
6649 Target_powerpc<size, big_endian>::symval_for_branch(
6650 const Symbol_table* symtab,
6651 const Sized_symbol<size>* gsym,
6652 Powerpc_relobj<size, big_endian>* object,
6654 unsigned int *dest_shndx)
6656 if (size == 32 || this->abiversion() >= 2)
6660 // If the symbol is defined in an opd section, ie. is a function
6661 // descriptor, use the function descriptor code entry address
6662 Powerpc_relobj<size, big_endian>* symobj = object;
6664 && gsym->source() != Symbol::FROM_OBJECT)
6667 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6668 unsigned int shndx = symobj->opd_shndx();
6671 Address opd_addr = symobj->get_output_section_offset(shndx);
6672 if (opd_addr == invalid_address)
6674 opd_addr += symobj->output_section_address(shndx);
6675 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
6678 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
6679 if (symtab->is_section_folded(symobj, *dest_shndx))
6682 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6683 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6684 *dest_shndx = folded.second;
6686 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6687 if (sec_addr == invalid_address)
6690 sec_addr += symobj->output_section(*dest_shndx)->address();
6691 *value = sec_addr + sec_off;
6696 // Perform a relocation.
6698 template<int size, bool big_endian>
6700 Target_powerpc<size, big_endian>::Relocate::relocate(
6701 const Relocate_info<size, big_endian>* relinfo,
6702 Target_powerpc* target,
6705 const elfcpp::Rela<size, big_endian>& rela,
6706 unsigned int r_type,
6707 const Sized_symbol<size>* gsym,
6708 const Symbol_value<size>* psymval,
6709 unsigned char* view,
6711 section_size_type view_size)
6716 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6718 case Track_tls::NOT_EXPECTED:
6719 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6720 _("__tls_get_addr call lacks marker reloc"));
6722 case Track_tls::EXPECTED:
6723 // We have already complained.
6725 case Track_tls::SKIP:
6727 case Track_tls::NORMAL:
6731 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6732 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6733 Powerpc_relobj<size, big_endian>* const object
6734 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6736 bool has_stub_value = false;
6737 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6739 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
6740 : object->local_has_plt_offset(r_sym))
6741 && (!psymval->is_ifunc_symbol()
6742 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
6746 && target->abiversion() >= 2
6747 && !parameters->options().output_is_position_independent()
6748 && !is_branch_reloc(r_type))
6750 unsigned int off = target->glink_section()->find_global_entry(gsym);
6751 gold_assert(off != (unsigned int)-1);
6752 value = target->glink_section()->global_entry_address() + off;
6756 Stub_table<size, big_endian>* stub_table
6757 = object->stub_table(relinfo->data_shndx);
6758 if (stub_table == NULL)
6760 // This is a ref from a data section to an ifunc symbol.
6761 if (target->stub_tables().size() != 0)
6762 stub_table = target->stub_tables()[0];
6764 gold_assert(stub_table != NULL);
6767 off = stub_table->find_plt_call_entry(object, gsym, r_type,
6768 rela.get_r_addend());
6770 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6771 rela.get_r_addend());
6772 gold_assert(off != invalid_address);
6773 value = stub_table->stub_address() + off;
6775 has_stub_value = true;
6778 if (r_type == elfcpp::R_POWERPC_GOT16
6779 || r_type == elfcpp::R_POWERPC_GOT16_LO
6780 || r_type == elfcpp::R_POWERPC_GOT16_HI
6781 || r_type == elfcpp::R_POWERPC_GOT16_HA
6782 || r_type == elfcpp::R_PPC64_GOT16_DS
6783 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6787 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6788 value = gsym->got_offset(GOT_TYPE_STANDARD);
6792 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6793 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6794 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6796 value -= target->got_section()->got_base_offset(object);
6798 else if (r_type == elfcpp::R_PPC64_TOC)
6800 value = (target->got_section()->output_section()->address()
6801 + object->toc_base_offset());
6803 else if (gsym != NULL
6804 && (r_type == elfcpp::R_POWERPC_REL24
6805 || r_type == elfcpp::R_PPC_PLTREL24)
6810 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6811 Valtype* wv = reinterpret_cast<Valtype*>(view);
6812 bool can_plt_call = false;
6813 if (rela.get_r_offset() + 8 <= view_size)
6815 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6816 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6819 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6821 elfcpp::Swap<32, big_endian>::
6822 writeval(wv + 1, ld_2_1 + target->stk_toc());
6823 can_plt_call = true;
6828 // If we don't have a branch and link followed by a nop,
6829 // we can't go via the plt because there is no place to
6830 // put a toc restoring instruction.
6831 // Unless we know we won't be returning.
6832 if (strcmp(gsym->name(), "__libc_start_main") == 0)
6833 can_plt_call = true;
6837 // g++ as of 20130507 emits self-calls without a
6838 // following nop. This is arguably wrong since we have
6839 // conflicting information. On the one hand a global
6840 // symbol and on the other a local call sequence, but
6841 // don't error for this special case.
6842 // It isn't possible to cheaply verify we have exactly
6843 // such a call. Allow all calls to the same section.
6845 Address code = value;
6846 if (gsym->source() == Symbol::FROM_OBJECT
6847 && gsym->object() == object)
6849 unsigned int dest_shndx = 0;
6850 if (target->abiversion() < 2)
6852 Address addend = rela.get_r_addend();
6853 code = psymval->value(object, addend);
6854 target->symval_for_branch(relinfo->symtab, gsym, object,
6855 &code, &dest_shndx);
6858 if (dest_shndx == 0)
6859 dest_shndx = gsym->shndx(&is_ordinary);
6860 ok = dest_shndx == relinfo->data_shndx;
6864 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6865 _("call lacks nop, can't restore toc; "
6866 "recompile with -fPIC"));
6872 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6873 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6874 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6875 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6877 // First instruction of a global dynamic sequence, arg setup insn.
6878 const bool final = gsym == NULL || gsym->final_value_is_known();
6879 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6880 enum Got_type got_type = GOT_TYPE_STANDARD;
6881 if (tls_type == tls::TLSOPT_NONE)
6882 got_type = GOT_TYPE_TLSGD;
6883 else if (tls_type == tls::TLSOPT_TO_IE)
6884 got_type = GOT_TYPE_TPREL;
6885 if (got_type != GOT_TYPE_STANDARD)
6889 gold_assert(gsym->has_got_offset(got_type));
6890 value = gsym->got_offset(got_type);
6894 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6895 gold_assert(object->local_has_got_offset(r_sym, got_type));
6896 value = object->local_got_offset(r_sym, got_type);
6898 value -= target->got_section()->got_base_offset(object);
6900 if (tls_type == tls::TLSOPT_TO_IE)
6902 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6903 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6905 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6906 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6907 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6909 insn |= 32 << 26; // lwz
6911 insn |= 58 << 26; // ld
6912 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6914 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6915 - elfcpp::R_POWERPC_GOT_TLSGD16);
6917 else if (tls_type == tls::TLSOPT_TO_LE)
6919 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6920 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6922 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6923 Insn insn = addis_3_13;
6926 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6927 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6928 value = psymval->value(object, rela.get_r_addend());
6932 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6934 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6935 r_type = elfcpp::R_POWERPC_NONE;
6939 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6940 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6941 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6942 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6944 // First instruction of a local dynamic sequence, arg setup insn.
6945 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6946 if (tls_type == tls::TLSOPT_NONE)
6948 value = target->tlsld_got_offset();
6949 value -= target->got_section()->got_base_offset(object);
6953 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6954 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6955 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6957 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6958 Insn insn = addis_3_13;
6961 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6962 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6967 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6969 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6970 r_type = elfcpp::R_POWERPC_NONE;
6974 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6975 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6976 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6977 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6979 // Accesses relative to a local dynamic sequence address,
6980 // no optimisation here.
6983 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6984 value = gsym->got_offset(GOT_TYPE_DTPREL);
6988 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6989 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6990 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6992 value -= target->got_section()->got_base_offset(object);
6994 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6995 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6996 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6997 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6999 // First instruction of initial exec sequence.
7000 const bool final = gsym == NULL || gsym->final_value_is_known();
7001 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7002 if (tls_type == tls::TLSOPT_NONE)
7006 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
7007 value = gsym->got_offset(GOT_TYPE_TPREL);
7011 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7012 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
7013 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
7015 value -= target->got_section()->got_base_offset(object);
7019 gold_assert(tls_type == tls::TLSOPT_TO_LE);
7020 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7021 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7023 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7024 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7025 insn &= (1 << 26) - (1 << 21); // extract rt from ld
7030 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7031 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7032 value = psymval->value(object, rela.get_r_addend());
7036 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7038 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7039 r_type = elfcpp::R_POWERPC_NONE;
7043 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7044 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7046 // Second instruction of a global dynamic sequence,
7047 // the __tls_get_addr call
7048 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7049 const bool final = gsym == NULL || gsym->final_value_is_known();
7050 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7051 if (tls_type != tls::TLSOPT_NONE)
7053 if (tls_type == tls::TLSOPT_TO_IE)
7055 Insn* iview = reinterpret_cast<Insn*>(view);
7056 Insn insn = add_3_3_13;
7059 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7060 r_type = elfcpp::R_POWERPC_NONE;
7064 Insn* iview = reinterpret_cast<Insn*>(view);
7065 Insn insn = addi_3_3;
7066 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7067 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7068 view += 2 * big_endian;
7069 value = psymval->value(object, rela.get_r_addend());
7071 this->skip_next_tls_get_addr_call();
7074 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7075 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7077 // Second instruction of a local dynamic sequence,
7078 // the __tls_get_addr call
7079 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7080 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7081 if (tls_type == tls::TLSOPT_TO_LE)
7083 Insn* iview = reinterpret_cast<Insn*>(view);
7084 Insn insn = addi_3_3;
7085 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7086 this->skip_next_tls_get_addr_call();
7087 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7088 view += 2 * big_endian;
7092 else if (r_type == elfcpp::R_POWERPC_TLS)
7094 // Second instruction of an initial exec sequence
7095 const bool final = gsym == NULL || gsym->final_value_is_known();
7096 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7097 if (tls_type == tls::TLSOPT_TO_LE)
7099 Insn* iview = reinterpret_cast<Insn*>(view);
7100 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7101 unsigned int reg = size == 32 ? 2 : 13;
7102 insn = at_tls_transform(insn, reg);
7103 gold_assert(insn != 0);
7104 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7105 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7106 view += 2 * big_endian;
7107 value = psymval->value(object, rela.get_r_addend());
7110 else if (!has_stub_value)
7113 if (r_type != elfcpp::R_PPC_PLTREL24)
7114 addend = rela.get_r_addend();
7115 value = psymval->value(object, addend);
7116 if (size == 64 && is_branch_reloc(r_type))
7118 if (target->abiversion() >= 2)
7121 value += object->ppc64_local_entry_offset(gsym);
7123 value += object->ppc64_local_entry_offset(r_sym);
7127 unsigned int dest_shndx;
7128 target->symval_for_branch(relinfo->symtab, gsym, object,
7129 &value, &dest_shndx);
7132 unsigned int max_branch_offset = 0;
7133 if (r_type == elfcpp::R_POWERPC_REL24
7134 || r_type == elfcpp::R_PPC_PLTREL24
7135 || r_type == elfcpp::R_PPC_LOCAL24PC)
7136 max_branch_offset = 1 << 25;
7137 else if (r_type == elfcpp::R_POWERPC_REL14
7138 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
7139 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
7140 max_branch_offset = 1 << 15;
7141 if (max_branch_offset != 0
7142 && value - address + max_branch_offset >= 2 * max_branch_offset)
7144 Stub_table<size, big_endian>* stub_table
7145 = object->stub_table(relinfo->data_shndx);
7146 if (stub_table != NULL)
7148 Address off = stub_table->find_long_branch_entry(object, value);
7149 if (off != invalid_address)
7151 value = (stub_table->stub_address() + stub_table->plt_size()
7153 has_stub_value = true;
7161 case elfcpp::R_PPC64_REL64:
7162 case elfcpp::R_POWERPC_REL32:
7163 case elfcpp::R_POWERPC_REL24:
7164 case elfcpp::R_PPC_PLTREL24:
7165 case elfcpp::R_PPC_LOCAL24PC:
7166 case elfcpp::R_POWERPC_REL16:
7167 case elfcpp::R_POWERPC_REL16_LO:
7168 case elfcpp::R_POWERPC_REL16_HI:
7169 case elfcpp::R_POWERPC_REL16_HA:
7170 case elfcpp::R_POWERPC_REL14:
7171 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7172 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7176 case elfcpp::R_PPC64_TOC16:
7177 case elfcpp::R_PPC64_TOC16_LO:
7178 case elfcpp::R_PPC64_TOC16_HI:
7179 case elfcpp::R_PPC64_TOC16_HA:
7180 case elfcpp::R_PPC64_TOC16_DS:
7181 case elfcpp::R_PPC64_TOC16_LO_DS:
7182 // Subtract the TOC base address.
7183 value -= (target->got_section()->output_section()->address()
7184 + object->toc_base_offset());
7187 case elfcpp::R_POWERPC_SECTOFF:
7188 case elfcpp::R_POWERPC_SECTOFF_LO:
7189 case elfcpp::R_POWERPC_SECTOFF_HI:
7190 case elfcpp::R_POWERPC_SECTOFF_HA:
7191 case elfcpp::R_PPC64_SECTOFF_DS:
7192 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7194 value -= os->address();
7197 case elfcpp::R_PPC64_TPREL16_DS:
7198 case elfcpp::R_PPC64_TPREL16_LO_DS:
7199 case elfcpp::R_PPC64_TPREL16_HIGH:
7200 case elfcpp::R_PPC64_TPREL16_HIGHA:
7202 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7204 case elfcpp::R_POWERPC_TPREL16:
7205 case elfcpp::R_POWERPC_TPREL16_LO:
7206 case elfcpp::R_POWERPC_TPREL16_HI:
7207 case elfcpp::R_POWERPC_TPREL16_HA:
7208 case elfcpp::R_POWERPC_TPREL:
7209 case elfcpp::R_PPC64_TPREL16_HIGHER:
7210 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7211 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7212 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7213 // tls symbol values are relative to tls_segment()->vaddr()
7217 case elfcpp::R_PPC64_DTPREL16_DS:
7218 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7219 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7220 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7221 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7222 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7224 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7225 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7227 case elfcpp::R_POWERPC_DTPREL16:
7228 case elfcpp::R_POWERPC_DTPREL16_LO:
7229 case elfcpp::R_POWERPC_DTPREL16_HI:
7230 case elfcpp::R_POWERPC_DTPREL16_HA:
7231 case elfcpp::R_POWERPC_DTPREL:
7232 case elfcpp::R_PPC64_DTPREL16_HIGH:
7233 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7234 // tls symbol values are relative to tls_segment()->vaddr()
7235 value -= dtp_offset;
7238 case elfcpp::R_PPC64_ADDR64_LOCAL:
7240 value += object->ppc64_local_entry_offset(gsym);
7242 value += object->ppc64_local_entry_offset(r_sym);
7249 Insn branch_bit = 0;
7252 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7253 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7254 branch_bit = 1 << 21;
7255 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7256 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7258 Insn* iview = reinterpret_cast<Insn*>(view);
7259 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7262 if (this->is_isa_v2)
7264 // Set 'a' bit. This is 0b00010 in BO field for branch
7265 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7266 // for branch on CTR insns (BO == 1a00t or 1a01t).
7267 if ((insn & (0x14 << 21)) == (0x04 << 21))
7269 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7276 // Invert 'y' bit if not the default.
7277 if (static_cast<Signed_address>(value) < 0)
7280 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7290 // Multi-instruction sequences that access the TOC can be
7291 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7292 // to nop; addi rb,r2,x;
7298 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7299 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7300 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7301 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7302 case elfcpp::R_POWERPC_GOT16_HA:
7303 case elfcpp::R_PPC64_TOC16_HA:
7304 if (parameters->options().toc_optimize())
7306 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7307 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7308 if ((insn & ((0x3f << 26) | 0x1f << 16))
7309 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7310 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7311 _("toc optimization is not supported "
7312 "for %#08x instruction"), insn);
7313 else if (value + 0x8000 < 0x10000)
7315 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
7321 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7322 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7323 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7324 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7325 case elfcpp::R_POWERPC_GOT16_LO:
7326 case elfcpp::R_PPC64_GOT16_LO_DS:
7327 case elfcpp::R_PPC64_TOC16_LO:
7328 case elfcpp::R_PPC64_TOC16_LO_DS:
7329 if (parameters->options().toc_optimize())
7331 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7332 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7333 if (!ok_lo_toc_insn(insn))
7334 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7335 _("toc optimization is not supported "
7336 "for %#08x instruction"), insn);
7337 else if (value + 0x8000 < 0x10000)
7339 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
7341 // Transform addic to addi when we change reg.
7342 insn &= ~((0x3f << 26) | (0x1f << 16));
7343 insn |= (14u << 26) | (2 << 16);
7347 insn &= ~(0x1f << 16);
7350 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7357 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
7358 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
7361 case elfcpp::R_POWERPC_ADDR32:
7362 case elfcpp::R_POWERPC_UADDR32:
7364 overflow = Reloc::CHECK_BITFIELD;
7367 case elfcpp::R_POWERPC_REL32:
7369 overflow = Reloc::CHECK_SIGNED;
7372 case elfcpp::R_POWERPC_UADDR16:
7373 overflow = Reloc::CHECK_BITFIELD;
7376 case elfcpp::R_POWERPC_ADDR16:
7377 // We really should have three separate relocations,
7378 // one for 16-bit data, one for insns with 16-bit signed fields,
7379 // and one for insns with 16-bit unsigned fields.
7380 overflow = Reloc::CHECK_BITFIELD;
7381 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
7382 overflow = Reloc::CHECK_LOW_INSN;
7385 case elfcpp::R_POWERPC_ADDR16_HI:
7386 case elfcpp::R_POWERPC_ADDR16_HA:
7387 case elfcpp::R_POWERPC_GOT16_HI:
7388 case elfcpp::R_POWERPC_GOT16_HA:
7389 case elfcpp::R_POWERPC_PLT16_HI:
7390 case elfcpp::R_POWERPC_PLT16_HA:
7391 case elfcpp::R_POWERPC_SECTOFF_HI:
7392 case elfcpp::R_POWERPC_SECTOFF_HA:
7393 case elfcpp::R_PPC64_TOC16_HI:
7394 case elfcpp::R_PPC64_TOC16_HA:
7395 case elfcpp::R_PPC64_PLTGOT16_HI:
7396 case elfcpp::R_PPC64_PLTGOT16_HA:
7397 case elfcpp::R_POWERPC_TPREL16_HI:
7398 case elfcpp::R_POWERPC_TPREL16_HA:
7399 case elfcpp::R_POWERPC_DTPREL16_HI:
7400 case elfcpp::R_POWERPC_DTPREL16_HA:
7401 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7402 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7403 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7404 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7405 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7406 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7407 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7408 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7409 case elfcpp::R_POWERPC_REL16_HI:
7410 case elfcpp::R_POWERPC_REL16_HA:
7412 overflow = Reloc::CHECK_HIGH_INSN;
7415 case elfcpp::R_POWERPC_REL16:
7416 case elfcpp::R_PPC64_TOC16:
7417 case elfcpp::R_POWERPC_GOT16:
7418 case elfcpp::R_POWERPC_SECTOFF:
7419 case elfcpp::R_POWERPC_TPREL16:
7420 case elfcpp::R_POWERPC_DTPREL16:
7421 case elfcpp::R_POWERPC_GOT_TLSGD16:
7422 case elfcpp::R_POWERPC_GOT_TLSLD16:
7423 case elfcpp::R_POWERPC_GOT_TPREL16:
7424 case elfcpp::R_POWERPC_GOT_DTPREL16:
7425 overflow = Reloc::CHECK_LOW_INSN;
7428 case elfcpp::R_POWERPC_ADDR24:
7429 case elfcpp::R_POWERPC_ADDR14:
7430 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7431 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7432 case elfcpp::R_PPC64_ADDR16_DS:
7433 case elfcpp::R_POWERPC_REL24:
7434 case elfcpp::R_PPC_PLTREL24:
7435 case elfcpp::R_PPC_LOCAL24PC:
7436 case elfcpp::R_PPC64_TPREL16_DS:
7437 case elfcpp::R_PPC64_DTPREL16_DS:
7438 case elfcpp::R_PPC64_TOC16_DS:
7439 case elfcpp::R_PPC64_GOT16_DS:
7440 case elfcpp::R_PPC64_SECTOFF_DS:
7441 case elfcpp::R_POWERPC_REL14:
7442 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7443 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7444 overflow = Reloc::CHECK_SIGNED;
7448 if (overflow == Reloc::CHECK_LOW_INSN
7449 || overflow == Reloc::CHECK_HIGH_INSN)
7451 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7452 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7454 overflow = Reloc::CHECK_SIGNED;
7455 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
7456 overflow = Reloc::CHECK_BITFIELD;
7457 else if (overflow == Reloc::CHECK_LOW_INSN
7458 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
7459 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
7460 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
7461 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
7462 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
7463 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
7464 overflow = Reloc::CHECK_UNSIGNED;
7467 typename Powerpc_relocate_functions<size, big_endian>::Status status
7468 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
7471 case elfcpp::R_POWERPC_NONE:
7472 case elfcpp::R_POWERPC_TLS:
7473 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7474 case elfcpp::R_POWERPC_GNU_VTENTRY:
7477 case elfcpp::R_PPC64_ADDR64:
7478 case elfcpp::R_PPC64_REL64:
7479 case elfcpp::R_PPC64_TOC:
7480 case elfcpp::R_PPC64_ADDR64_LOCAL:
7481 Reloc::addr64(view, value);
7484 case elfcpp::R_POWERPC_TPREL:
7485 case elfcpp::R_POWERPC_DTPREL:
7487 Reloc::addr64(view, value);
7489 status = Reloc::addr32(view, value, overflow);
7492 case elfcpp::R_PPC64_UADDR64:
7493 Reloc::addr64_u(view, value);
7496 case elfcpp::R_POWERPC_ADDR32:
7497 status = Reloc::addr32(view, value, overflow);
7500 case elfcpp::R_POWERPC_REL32:
7501 case elfcpp::R_POWERPC_UADDR32:
7502 status = Reloc::addr32_u(view, value, overflow);
7505 case elfcpp::R_POWERPC_ADDR24:
7506 case elfcpp::R_POWERPC_REL24:
7507 case elfcpp::R_PPC_PLTREL24:
7508 case elfcpp::R_PPC_LOCAL24PC:
7509 status = Reloc::addr24(view, value, overflow);
7512 case elfcpp::R_POWERPC_GOT_DTPREL16:
7513 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7516 status = Reloc::addr16_ds(view, value, overflow);
7519 case elfcpp::R_POWERPC_ADDR16:
7520 case elfcpp::R_POWERPC_REL16:
7521 case elfcpp::R_PPC64_TOC16:
7522 case elfcpp::R_POWERPC_GOT16:
7523 case elfcpp::R_POWERPC_SECTOFF:
7524 case elfcpp::R_POWERPC_TPREL16:
7525 case elfcpp::R_POWERPC_DTPREL16:
7526 case elfcpp::R_POWERPC_GOT_TLSGD16:
7527 case elfcpp::R_POWERPC_GOT_TLSLD16:
7528 case elfcpp::R_POWERPC_GOT_TPREL16:
7529 case elfcpp::R_POWERPC_ADDR16_LO:
7530 case elfcpp::R_POWERPC_REL16_LO:
7531 case elfcpp::R_PPC64_TOC16_LO:
7532 case elfcpp::R_POWERPC_GOT16_LO:
7533 case elfcpp::R_POWERPC_SECTOFF_LO:
7534 case elfcpp::R_POWERPC_TPREL16_LO:
7535 case elfcpp::R_POWERPC_DTPREL16_LO:
7536 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7537 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7538 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7539 status = Reloc::addr16(view, value, overflow);
7542 case elfcpp::R_POWERPC_UADDR16:
7543 status = Reloc::addr16_u(view, value, overflow);
7546 case elfcpp::R_PPC64_ADDR16_HIGH:
7547 case elfcpp::R_PPC64_TPREL16_HIGH:
7548 case elfcpp::R_PPC64_DTPREL16_HIGH:
7550 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7552 case elfcpp::R_POWERPC_ADDR16_HI:
7553 case elfcpp::R_POWERPC_REL16_HI:
7554 case elfcpp::R_PPC64_TOC16_HI:
7555 case elfcpp::R_POWERPC_GOT16_HI:
7556 case elfcpp::R_POWERPC_SECTOFF_HI:
7557 case elfcpp::R_POWERPC_TPREL16_HI:
7558 case elfcpp::R_POWERPC_DTPREL16_HI:
7559 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7560 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7561 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7562 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7563 Reloc::addr16_hi(view, value);
7566 case elfcpp::R_PPC64_ADDR16_HIGHA:
7567 case elfcpp::R_PPC64_TPREL16_HIGHA:
7568 case elfcpp::R_PPC64_DTPREL16_HIGHA:
7570 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7572 case elfcpp::R_POWERPC_ADDR16_HA:
7573 case elfcpp::R_POWERPC_REL16_HA:
7574 case elfcpp::R_PPC64_TOC16_HA:
7575 case elfcpp::R_POWERPC_GOT16_HA:
7576 case elfcpp::R_POWERPC_SECTOFF_HA:
7577 case elfcpp::R_POWERPC_TPREL16_HA:
7578 case elfcpp::R_POWERPC_DTPREL16_HA:
7579 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7580 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7581 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7582 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7583 Reloc::addr16_ha(view, value);
7586 case elfcpp::R_PPC64_DTPREL16_HIGHER:
7588 // R_PPC_EMB_NADDR16_LO
7590 case elfcpp::R_PPC64_ADDR16_HIGHER:
7591 case elfcpp::R_PPC64_TPREL16_HIGHER:
7592 Reloc::addr16_hi2(view, value);
7595 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7597 // R_PPC_EMB_NADDR16_HI
7599 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7600 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7601 Reloc::addr16_ha2(view, value);
7604 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7606 // R_PPC_EMB_NADDR16_HA
7608 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7609 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7610 Reloc::addr16_hi3(view, value);
7613 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7617 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7618 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7619 Reloc::addr16_ha3(view, value);
7622 case elfcpp::R_PPC64_DTPREL16_DS:
7623 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7625 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7627 case elfcpp::R_PPC64_TPREL16_DS:
7628 case elfcpp::R_PPC64_TPREL16_LO_DS:
7630 // R_PPC_TLSGD, R_PPC_TLSLD
7632 case elfcpp::R_PPC64_ADDR16_DS:
7633 case elfcpp::R_PPC64_ADDR16_LO_DS:
7634 case elfcpp::R_PPC64_TOC16_DS:
7635 case elfcpp::R_PPC64_TOC16_LO_DS:
7636 case elfcpp::R_PPC64_GOT16_DS:
7637 case elfcpp::R_PPC64_GOT16_LO_DS:
7638 case elfcpp::R_PPC64_SECTOFF_DS:
7639 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7640 status = Reloc::addr16_ds(view, value, overflow);
7643 case elfcpp::R_POWERPC_ADDR14:
7644 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7645 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7646 case elfcpp::R_POWERPC_REL14:
7647 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7648 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7649 status = Reloc::addr14(view, value, overflow);
7652 case elfcpp::R_POWERPC_COPY:
7653 case elfcpp::R_POWERPC_GLOB_DAT:
7654 case elfcpp::R_POWERPC_JMP_SLOT:
7655 case elfcpp::R_POWERPC_RELATIVE:
7656 case elfcpp::R_POWERPC_DTPMOD:
7657 case elfcpp::R_PPC64_JMP_IREL:
7658 case elfcpp::R_POWERPC_IRELATIVE:
7659 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7660 _("unexpected reloc %u in object file"),
7664 case elfcpp::R_PPC_EMB_SDA21:
7669 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7673 case elfcpp::R_PPC_EMB_SDA2I16:
7674 case elfcpp::R_PPC_EMB_SDA2REL:
7677 // R_PPC64_TLSGD, R_PPC64_TLSLD
7680 case elfcpp::R_POWERPC_PLT32:
7681 case elfcpp::R_POWERPC_PLTREL32:
7682 case elfcpp::R_POWERPC_PLT16_LO:
7683 case elfcpp::R_POWERPC_PLT16_HI:
7684 case elfcpp::R_POWERPC_PLT16_HA:
7685 case elfcpp::R_PPC_SDAREL16:
7686 case elfcpp::R_POWERPC_ADDR30:
7687 case elfcpp::R_PPC64_PLT64:
7688 case elfcpp::R_PPC64_PLTREL64:
7689 case elfcpp::R_PPC64_PLTGOT16:
7690 case elfcpp::R_PPC64_PLTGOT16_LO:
7691 case elfcpp::R_PPC64_PLTGOT16_HI:
7692 case elfcpp::R_PPC64_PLTGOT16_HA:
7693 case elfcpp::R_PPC64_PLT16_LO_DS:
7694 case elfcpp::R_PPC64_PLTGOT16_DS:
7695 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7696 case elfcpp::R_PPC_EMB_RELSDA:
7697 case elfcpp::R_PPC_TOC16:
7700 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7701 _("unsupported reloc %u"),
7705 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
7708 && gsym->is_weak_undefined()
7709 && is_branch_reloc(r_type))))
7711 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7712 _("relocation overflow"));
7714 gold_info(_("try relinking with a smaller --stub-group-size"));
7720 // Relocate section data.
7722 template<int size, bool big_endian>
7724 Target_powerpc<size, big_endian>::relocate_section(
7725 const Relocate_info<size, big_endian>* relinfo,
7726 unsigned int sh_type,
7727 const unsigned char* prelocs,
7729 Output_section* output_section,
7730 bool needs_special_offset_handling,
7731 unsigned char* view,
7733 section_size_type view_size,
7734 const Reloc_symbol_changes* reloc_symbol_changes)
7736 typedef Target_powerpc<size, big_endian> Powerpc;
7737 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7738 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7739 Powerpc_comdat_behavior;
7741 gold_assert(sh_type == elfcpp::SHT_RELA);
7743 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7744 Powerpc_relocate, Powerpc_comdat_behavior>(
7750 needs_special_offset_handling,
7754 reloc_symbol_changes);
7757 class Powerpc_scan_relocatable_reloc
7760 // Return the strategy to use for a local symbol which is not a
7761 // section symbol, given the relocation type.
7762 inline Relocatable_relocs::Reloc_strategy
7763 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7765 if (r_type == 0 && r_sym == 0)
7766 return Relocatable_relocs::RELOC_DISCARD;
7767 return Relocatable_relocs::RELOC_COPY;
7770 // Return the strategy to use for a local symbol which is a section
7771 // symbol, given the relocation type.
7772 inline Relocatable_relocs::Reloc_strategy
7773 local_section_strategy(unsigned int, Relobj*)
7775 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7778 // Return the strategy to use for a global symbol, given the
7779 // relocation type, the object, and the symbol index.
7780 inline Relocatable_relocs::Reloc_strategy
7781 global_strategy(unsigned int r_type, Relobj*, unsigned int)
7783 if (r_type == elfcpp::R_PPC_PLTREL24)
7784 return Relocatable_relocs::RELOC_SPECIAL;
7785 return Relocatable_relocs::RELOC_COPY;
7789 // Scan the relocs during a relocatable link.
7791 template<int size, bool big_endian>
7793 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7794 Symbol_table* symtab,
7796 Sized_relobj_file<size, big_endian>* object,
7797 unsigned int data_shndx,
7798 unsigned int sh_type,
7799 const unsigned char* prelocs,
7801 Output_section* output_section,
7802 bool needs_special_offset_handling,
7803 size_t local_symbol_count,
7804 const unsigned char* plocal_symbols,
7805 Relocatable_relocs* rr)
7807 gold_assert(sh_type == elfcpp::SHT_RELA);
7809 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7810 Powerpc_scan_relocatable_reloc>(
7818 needs_special_offset_handling,
7824 // Emit relocations for a section.
7825 // This is a modified version of the function by the same name in
7826 // target-reloc.h. Using relocate_special_relocatable for
7827 // R_PPC_PLTREL24 would require duplication of the entire body of the
7828 // loop, so we may as well duplicate the whole thing.
7830 template<int size, bool big_endian>
7832 Target_powerpc<size, big_endian>::relocate_relocs(
7833 const Relocate_info<size, big_endian>* relinfo,
7834 unsigned int sh_type,
7835 const unsigned char* prelocs,
7837 Output_section* output_section,
7838 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7839 const Relocatable_relocs* rr,
7841 Address view_address,
7843 unsigned char* reloc_view,
7844 section_size_type reloc_view_size)
7846 gold_assert(sh_type == elfcpp::SHT_RELA);
7848 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7850 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7852 const int reloc_size
7853 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7855 Powerpc_relobj<size, big_endian>* const object
7856 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7857 const unsigned int local_count = object->local_symbol_count();
7858 unsigned int got2_shndx = object->got2_shndx();
7859 Address got2_addend = 0;
7860 if (got2_shndx != 0)
7862 got2_addend = object->get_output_section_offset(got2_shndx);
7863 gold_assert(got2_addend != invalid_address);
7866 unsigned char* pwrite = reloc_view;
7867 bool zap_next = false;
7868 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7870 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7871 if (strategy == Relocatable_relocs::RELOC_DISCARD)
7874 Reltype reloc(prelocs);
7875 Reltype_write reloc_write(pwrite);
7877 Address offset = reloc.get_r_offset();
7878 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7879 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7880 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7881 const unsigned int orig_r_sym = r_sym;
7882 typename elfcpp::Elf_types<size>::Elf_Swxword addend
7883 = reloc.get_r_addend();
7884 const Symbol* gsym = NULL;
7888 // We could arrange to discard these and other relocs for
7889 // tls optimised sequences in the strategy methods, but for
7890 // now do as BFD ld does.
7891 r_type = elfcpp::R_POWERPC_NONE;
7895 // Get the new symbol index.
7896 if (r_sym < local_count)
7900 case Relocatable_relocs::RELOC_COPY:
7901 case Relocatable_relocs::RELOC_SPECIAL:
7904 r_sym = object->symtab_index(r_sym);
7905 gold_assert(r_sym != -1U);
7909 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7911 // We are adjusting a section symbol. We need to find
7912 // the symbol table index of the section symbol for
7913 // the output section corresponding to input section
7914 // in which this symbol is defined.
7915 gold_assert(r_sym < local_count);
7917 unsigned int shndx =
7918 object->local_symbol_input_shndx(r_sym, &is_ordinary);
7919 gold_assert(is_ordinary);
7920 Output_section* os = object->output_section(shndx);
7921 gold_assert(os != NULL);
7922 gold_assert(os->needs_symtab_index());
7923 r_sym = os->symtab_index();
7933 gsym = object->global_symbol(r_sym);
7934 gold_assert(gsym != NULL);
7935 if (gsym->is_forwarder())
7936 gsym = relinfo->symtab->resolve_forwards(gsym);
7938 gold_assert(gsym->has_symtab_index());
7939 r_sym = gsym->symtab_index();
7942 // Get the new offset--the location in the output section where
7943 // this relocation should be applied.
7944 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7945 offset += offset_in_output_section;
7948 section_offset_type sot_offset =
7949 convert_types<section_offset_type, Address>(offset);
7950 section_offset_type new_sot_offset =
7951 output_section->output_offset(object, relinfo->data_shndx,
7953 gold_assert(new_sot_offset != -1);
7954 offset = new_sot_offset;
7957 // In an object file, r_offset is an offset within the section.
7958 // In an executable or dynamic object, generated by
7959 // --emit-relocs, r_offset is an absolute address.
7960 if (!parameters->options().relocatable())
7962 offset += view_address;
7963 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7964 offset -= offset_in_output_section;
7967 // Handle the reloc addend based on the strategy.
7968 if (strategy == Relocatable_relocs::RELOC_COPY)
7970 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7972 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7973 addend = psymval->value(object, addend);
7975 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7977 if (addend >= 32768)
7978 addend += got2_addend;
7983 if (!parameters->options().relocatable())
7985 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7986 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7987 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7988 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7990 // First instruction of a global dynamic sequence,
7992 const bool final = gsym == NULL || gsym->final_value_is_known();
7993 switch (this->optimize_tls_gd(final))
7995 case tls::TLSOPT_TO_IE:
7996 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7997 - elfcpp::R_POWERPC_GOT_TLSGD16);
7999 case tls::TLSOPT_TO_LE:
8000 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
8001 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
8002 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8005 r_type = elfcpp::R_POWERPC_NONE;
8006 offset -= 2 * big_endian;
8013 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8014 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8015 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8016 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8018 // First instruction of a local dynamic sequence,
8020 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8022 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8023 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8025 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8026 const Output_section* os = relinfo->layout->tls_segment()
8028 gold_assert(os != NULL);
8029 gold_assert(os->needs_symtab_index());
8030 r_sym = os->symtab_index();
8031 addend = dtp_offset;
8035 r_type = elfcpp::R_POWERPC_NONE;
8036 offset -= 2 * big_endian;
8040 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8041 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8042 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8043 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8045 // First instruction of initial exec sequence.
8046 const bool final = gsym == NULL || gsym->final_value_is_known();
8047 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8049 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8050 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8051 r_type = elfcpp::R_POWERPC_TPREL16_HA;
8054 r_type = elfcpp::R_POWERPC_NONE;
8055 offset -= 2 * big_endian;
8059 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8060 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8062 // Second instruction of a global dynamic sequence,
8063 // the __tls_get_addr call
8064 const bool final = gsym == NULL || gsym->final_value_is_known();
8065 switch (this->optimize_tls_gd(final))
8067 case tls::TLSOPT_TO_IE:
8068 r_type = elfcpp::R_POWERPC_NONE;
8071 case tls::TLSOPT_TO_LE:
8072 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8073 offset += 2 * big_endian;
8080 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8081 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8083 // Second instruction of a local dynamic sequence,
8084 // the __tls_get_addr call
8085 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8087 const Output_section* os = relinfo->layout->tls_segment()
8089 gold_assert(os != NULL);
8090 gold_assert(os->needs_symtab_index());
8091 r_sym = os->symtab_index();
8092 addend = dtp_offset;
8093 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8094 offset += 2 * big_endian;
8098 else if (r_type == elfcpp::R_POWERPC_TLS)
8100 // Second instruction of an initial exec sequence
8101 const bool final = gsym == NULL || gsym->final_value_is_known();
8102 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8104 r_type = elfcpp::R_POWERPC_TPREL16_LO;
8105 offset += 2 * big_endian;
8110 reloc_write.put_r_offset(offset);
8111 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
8112 reloc_write.put_r_addend(addend);
8114 pwrite += reloc_size;
8117 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
8118 == reloc_view_size);
8121 // Return the value to use for a dynamic symbol which requires special
8122 // treatment. This is how we support equality comparisons of function
8123 // pointers across shared library boundaries, as described in the
8124 // processor specific ABI supplement.
8126 template<int size, bool big_endian>
8128 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8132 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8133 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8134 p != this->stub_tables_.end();
8137 Address off = (*p)->find_plt_call_entry(gsym);
8138 if (off != invalid_address)
8139 return (*p)->stub_address() + off;
8142 else if (this->abiversion() >= 2)
8144 unsigned int off = this->glink_section()->find_global_entry(gsym);
8145 if (off != (unsigned int)-1)
8146 return this->glink_section()->global_entry_address() + off;
8151 // Return the PLT address to use for a local symbol.
8152 template<int size, bool big_endian>
8154 Target_powerpc<size, big_endian>::do_plt_address_for_local(
8155 const Relobj* object,
8156 unsigned int symndx) const
8160 const Sized_relobj<size, big_endian>* relobj
8161 = static_cast<const Sized_relobj<size, big_endian>*>(object);
8162 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8163 p != this->stub_tables_.end();
8166 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
8168 if (off != invalid_address)
8169 return (*p)->stub_address() + off;
8175 // Return the PLT address to use for a global symbol.
8176 template<int size, bool big_endian>
8178 Target_powerpc<size, big_endian>::do_plt_address_for_global(
8179 const Symbol* gsym) const
8183 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8184 p != this->stub_tables_.end();
8187 Address off = (*p)->find_plt_call_entry(gsym);
8188 if (off != invalid_address)
8189 return (*p)->stub_address() + off;
8192 else if (this->abiversion() >= 2)
8194 unsigned int off = this->glink_section()->find_global_entry(gsym);
8195 if (off != (unsigned int)-1)
8196 return this->glink_section()->global_entry_address() + off;
8201 // Return the offset to use for the GOT_INDX'th got entry which is
8202 // for a local tls symbol specified by OBJECT, SYMNDX.
8203 template<int size, bool big_endian>
8205 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
8206 const Relobj* object,
8207 unsigned int symndx,
8208 unsigned int got_indx) const
8210 const Powerpc_relobj<size, big_endian>* ppc_object
8211 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
8212 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
8214 for (Got_type got_type = GOT_TYPE_TLSGD;
8215 got_type <= GOT_TYPE_TPREL;
8216 got_type = Got_type(got_type + 1))
8217 if (ppc_object->local_has_got_offset(symndx, got_type))
8219 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
8220 if (got_type == GOT_TYPE_TLSGD)
8222 if (off == got_indx * (size / 8))
8224 if (got_type == GOT_TYPE_TPREL)
8234 // Return the offset to use for the GOT_INDX'th got entry which is
8235 // for global tls symbol GSYM.
8236 template<int size, bool big_endian>
8238 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
8240 unsigned int got_indx) const
8242 if (gsym->type() == elfcpp::STT_TLS)
8244 for (Got_type got_type = GOT_TYPE_TLSGD;
8245 got_type <= GOT_TYPE_TPREL;
8246 got_type = Got_type(got_type + 1))
8247 if (gsym->has_got_offset(got_type))
8249 unsigned int off = gsym->got_offset(got_type);
8250 if (got_type == GOT_TYPE_TLSGD)
8252 if (off == got_indx * (size / 8))
8254 if (got_type == GOT_TYPE_TPREL)
8264 // The selector for powerpc object files.
8266 template<int size, bool big_endian>
8267 class Target_selector_powerpc : public Target_selector
8270 Target_selector_powerpc()
8271 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
8274 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
8275 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
8277 ? (big_endian ? "elf64ppc" : "elf64lppc")
8278 : (big_endian ? "elf32ppc" : "elf32lppc")))
8282 do_instantiate_target()
8283 { return new Target_powerpc<size, big_endian>(); }
8286 Target_selector_powerpc<32, true> target_selector_ppc32;
8287 Target_selector_powerpc<32, false> target_selector_ppc32le;
8288 Target_selector_powerpc<64, true> target_selector_ppc64;
8289 Target_selector_powerpc<64, false> target_selector_ppc64le;
8291 // Instantiate these constants for -O0
8292 template<int size, bool big_endian>
8293 const int Output_data_glink<size, big_endian>::pltresolve_size;
8294 template<int size, bool big_endian>
8295 const typename Output_data_glink<size, big_endian>::Address
8296 Output_data_glink<size, big_endian>::invalid_address;
8297 template<int size, bool big_endian>
8298 const typename Stub_table<size, big_endian>::Address
8299 Stub_table<size, big_endian>::invalid_address;
8300 template<int size, bool big_endian>
8301 const typename Target_powerpc<size, big_endian>::Address
8302 Target_powerpc<size, big_endian>::invalid_address;
8304 } // End anonymous namespace.