1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012, 2013 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_()
86 // The .got2 section shndx.
91 return this->special_;
96 // The .opd section shndx.
103 return this->special_;
106 // Init OPD entry arrays.
108 init_opd(size_t opd_size)
110 size_t count = this->opd_ent_ndx(opd_size);
111 this->opd_ent_.resize(count);
114 // Return section and offset of function entry for .opd + R_OFF.
116 get_opd_ent(Address r_off, Address* value = NULL) const
118 size_t ndx = this->opd_ent_ndx(r_off);
119 gold_assert(ndx < this->opd_ent_.size());
120 gold_assert(this->opd_ent_[ndx].shndx != 0);
122 *value = this->opd_ent_[ndx].off;
123 return this->opd_ent_[ndx].shndx;
126 // Set section and offset of function entry for .opd + R_OFF.
128 set_opd_ent(Address r_off, unsigned int shndx, Address value)
130 size_t ndx = this->opd_ent_ndx(r_off);
131 gold_assert(ndx < this->opd_ent_.size());
132 this->opd_ent_[ndx].shndx = shndx;
133 this->opd_ent_[ndx].off = value;
136 // Return discard flag for .opd + R_OFF.
138 get_opd_discard(Address r_off) const
140 size_t ndx = this->opd_ent_ndx(r_off);
141 gold_assert(ndx < this->opd_ent_.size());
142 return this->opd_ent_[ndx].discard;
145 // Set discard flag for .opd + R_OFF.
147 set_opd_discard(Address r_off)
149 size_t ndx = this->opd_ent_ndx(r_off);
150 gold_assert(ndx < this->opd_ent_.size());
151 this->opd_ent_[ndx].discard = true;
156 { return this->opd_valid_; }
160 { this->opd_valid_ = true; }
162 // Examine .rela.opd to build info about function entry points.
164 scan_opd_relocs(size_t reloc_count,
165 const unsigned char* prelocs,
166 const unsigned char* plocal_syms);
168 // Perform the Sized_relobj_file method, then set up opd info from
171 do_read_relocs(Read_relocs_data*);
174 do_find_special_sections(Read_symbols_data* sd);
176 // Adjust this local symbol value. Return false if the symbol
177 // should be discarded from the output file.
179 do_adjust_local_symbol(Symbol_value<size>* lv) const
181 if (size == 64 && this->opd_shndx() != 0)
184 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
186 if (this->get_opd_discard(lv->input_value()))
194 { return &this->access_from_map_; }
196 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
197 // section at DST_OFF.
199 add_reference(Object* src_obj,
200 unsigned int src_indx,
201 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
203 Section_id src_id(src_obj, src_indx);
204 this->access_from_map_[dst_off].insert(src_id);
207 // Add a reference to the code section specified by the .opd entry
210 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
212 size_t ndx = this->opd_ent_ndx(dst_off);
213 if (ndx >= this->opd_ent_.size())
214 this->opd_ent_.resize(ndx + 1);
215 this->opd_ent_[ndx].gc_mark = true;
219 process_gc_mark(Symbol_table* symtab)
221 for (size_t i = 0; i < this->opd_ent_.size(); i++)
222 if (this->opd_ent_[i].gc_mark)
224 unsigned int shndx = this->opd_ent_[i].shndx;
225 symtab->gc()->worklist().push(Section_id(this, shndx));
229 // Return offset in output GOT section that this object will use
230 // as a TOC pointer. Won't be just a constant with multi-toc support.
232 toc_base_offset() const
236 set_has_small_toc_reloc()
237 { has_small_toc_reloc_ = true; }
240 has_small_toc_reloc() const
241 { return has_small_toc_reloc_; }
244 set_has_14bit_branch(unsigned int shndx)
246 if (shndx >= this->has14_.size())
247 this->has14_.resize(shndx + 1);
248 this->has14_[shndx] = true;
252 has_14bit_branch(unsigned int shndx) const
253 { return shndx < this->has14_.size() && this->has14_[shndx]; }
256 set_stub_table(unsigned int shndx, Stub_table<size, big_endian>* stub_table)
258 if (shndx >= this->stub_table_.size())
259 this->stub_table_.resize(shndx + 1);
260 this->stub_table_[shndx] = stub_table;
263 Stub_table<size, big_endian>*
264 stub_table(unsigned int shndx)
266 if (shndx < this->stub_table_.size())
267 return this->stub_table_[shndx];
280 // Return index into opd_ent_ array for .opd entry at OFF.
281 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
282 // apart when the language doesn't use the last 8-byte word, the
283 // environment pointer. Thus dividing the entry section offset by
284 // 16 will give an index into opd_ent_ that works for either layout
285 // of .opd. (It leaves some elements of the vector unused when .opd
286 // entries are spaced 24 bytes apart, but we don't know the spacing
287 // until relocations are processed, and in any case it is possible
288 // for an object to have some entries spaced 16 bytes apart and
289 // others 24 bytes apart.)
291 opd_ent_ndx(size_t off) const
294 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
295 unsigned int special_;
297 // For 64-bit, whether this object uses small model relocs to access
299 bool has_small_toc_reloc_;
301 // Set at the start of gc_process_relocs, when we know opd_ent_
302 // vector is valid. The flag could be made atomic and set in
303 // do_read_relocs with memory_order_release and then tested with
304 // memory_order_acquire, potentially resulting in fewer entries in
308 // The first 8-byte word of an OPD entry gives the address of the
309 // entry point of the function. Relocatable object files have a
310 // relocation on this word. The following vector records the
311 // section and offset specified by these relocations.
312 std::vector<Opd_ent> opd_ent_;
314 // References made to this object's .opd section when running
315 // gc_process_relocs for another object, before the opd_ent_ vector
316 // is valid for this object.
317 Access_from access_from_map_;
319 // Whether input section has a 14-bit branch reloc.
320 std::vector<bool> has14_;
322 // The stub table to use for a given input section.
323 std::vector<Stub_table<size, big_endian>*> stub_table_;
326 template<int size, bool big_endian>
327 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
330 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
332 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
333 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
334 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
335 opd_shndx_(0), opd_ent_()
341 // Call Sized_dynobj::do_read_symbols to read the symbols then
342 // read .opd from a dynamic object, filling in opd_ent_ vector,
344 do_read_symbols(Read_symbols_data*);
346 // The .opd section shndx.
350 return this->opd_shndx_;
353 // The .opd section address.
357 return this->opd_address_;
360 // Init OPD entry arrays.
362 init_opd(size_t opd_size)
364 size_t count = this->opd_ent_ndx(opd_size);
365 this->opd_ent_.resize(count);
368 // Return section and offset of function entry for .opd + R_OFF.
370 get_opd_ent(Address r_off, Address* value = NULL) const
372 size_t ndx = this->opd_ent_ndx(r_off);
373 gold_assert(ndx < this->opd_ent_.size());
374 gold_assert(this->opd_ent_[ndx].shndx != 0);
376 *value = this->opd_ent_[ndx].off;
377 return this->opd_ent_[ndx].shndx;
380 // Set section and offset of function entry for .opd + R_OFF.
382 set_opd_ent(Address r_off, unsigned int shndx, Address value)
384 size_t ndx = this->opd_ent_ndx(r_off);
385 gold_assert(ndx < this->opd_ent_.size());
386 this->opd_ent_[ndx].shndx = shndx;
387 this->opd_ent_[ndx].off = value;
391 // Used to specify extent of executable sections.
394 Sec_info(Address start_, Address len_, unsigned int shndx_)
395 : start(start_), len(len_), shndx(shndx_)
399 operator<(const Sec_info& that) const
400 { return this->start < that.start; }
413 // Return index into opd_ent_ array for .opd entry at OFF.
415 opd_ent_ndx(size_t off) const
418 // For 64-bit the .opd section shndx and address.
419 unsigned int opd_shndx_;
420 Address opd_address_;
422 // The first 8-byte word of an OPD entry gives the address of the
423 // entry point of the function. Records the section and offset
424 // corresponding to the address. Note that in dynamic objects,
425 // offset is *not* relative to the section.
426 std::vector<Opd_ent> opd_ent_;
429 template<int size, bool big_endian>
430 class Target_powerpc : public Sized_target<size, big_endian>
434 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
435 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
436 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
437 static const Address invalid_address = static_cast<Address>(0) - 1;
438 // Offset of tp and dtp pointers from start of TLS block.
439 static const Address tp_offset = 0x7000;
440 static const Address dtp_offset = 0x8000;
443 : Sized_target<size, big_endian>(&powerpc_info),
444 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
445 glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
446 dynbss_(NULL), tlsld_got_offset_(-1U),
447 stub_tables_(), branch_lookup_table_(), branch_info_(),
448 plt_thread_safe_(false)
452 // Process the relocations to determine unreferenced sections for
453 // garbage collection.
455 gc_process_relocs(Symbol_table* symtab,
457 Sized_relobj_file<size, big_endian>* object,
458 unsigned int data_shndx,
459 unsigned int sh_type,
460 const unsigned char* prelocs,
462 Output_section* output_section,
463 bool needs_special_offset_handling,
464 size_t local_symbol_count,
465 const unsigned char* plocal_symbols);
467 // Scan the relocations to look for symbol adjustments.
469 scan_relocs(Symbol_table* symtab,
471 Sized_relobj_file<size, big_endian>* object,
472 unsigned int data_shndx,
473 unsigned int sh_type,
474 const unsigned char* prelocs,
476 Output_section* output_section,
477 bool needs_special_offset_handling,
478 size_t local_symbol_count,
479 const unsigned char* plocal_symbols);
481 // Map input .toc section to output .got section.
483 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
485 if (size == 64 && strcmp(name, ".toc") == 0)
493 // Provide linker defined save/restore functions.
495 define_save_restore_funcs(Layout*, Symbol_table*);
497 // No stubs unless a final link.
500 { return !parameters->options().relocatable(); }
503 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
506 do_plt_fde_location(const Output_data*, unsigned char*,
507 uint64_t*, off_t*) const;
509 // Stash info about branches, for stub generation.
511 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
512 unsigned int data_shndx, Address r_offset,
513 unsigned int r_type, unsigned int r_sym, Address addend)
515 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
516 this->branch_info_.push_back(info);
517 if (r_type == elfcpp::R_POWERPC_REL14
518 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
519 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
520 ppc_object->set_has_14bit_branch(data_shndx);
523 Stub_table<size, big_endian>*
527 do_define_standard_symbols(Symbol_table*, Layout*);
529 // Finalize the sections.
531 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
533 // Return the value to use for a dynamic which requires special
536 do_dynsym_value(const Symbol*) const;
538 // Return the PLT address to use for a local symbol.
540 do_plt_address_for_local(const Relobj*, unsigned int) const;
542 // Return the PLT address to use for a global symbol.
544 do_plt_address_for_global(const Symbol*) const;
546 // Return the offset to use for the GOT_INDX'th got entry which is
547 // for a local tls symbol specified by OBJECT, SYMNDX.
549 do_tls_offset_for_local(const Relobj* object,
551 unsigned int got_indx) const;
553 // Return the offset to use for the GOT_INDX'th got entry which is
554 // for global tls symbol GSYM.
556 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
559 do_function_location(Symbol_location*) const;
562 do_can_check_for_function_pointers() const
565 // Relocate a section.
567 relocate_section(const Relocate_info<size, big_endian>*,
568 unsigned int sh_type,
569 const unsigned char* prelocs,
571 Output_section* output_section,
572 bool needs_special_offset_handling,
574 Address view_address,
575 section_size_type view_size,
576 const Reloc_symbol_changes*);
578 // Scan the relocs during a relocatable link.
580 scan_relocatable_relocs(Symbol_table* symtab,
582 Sized_relobj_file<size, big_endian>* object,
583 unsigned int data_shndx,
584 unsigned int sh_type,
585 const unsigned char* prelocs,
587 Output_section* output_section,
588 bool needs_special_offset_handling,
589 size_t local_symbol_count,
590 const unsigned char* plocal_symbols,
591 Relocatable_relocs*);
593 // Emit relocations for a section.
595 relocate_relocs(const Relocate_info<size, big_endian>*,
596 unsigned int sh_type,
597 const unsigned char* prelocs,
599 Output_section* output_section,
600 typename elfcpp::Elf_types<size>::Elf_Off
601 offset_in_output_section,
602 const Relocatable_relocs*,
604 Address view_address,
606 unsigned char* reloc_view,
607 section_size_type reloc_view_size);
609 // Return whether SYM is defined by the ABI.
611 do_is_defined_by_abi(const Symbol* sym) const
613 return strcmp(sym->name(), "__tls_get_addr") == 0;
616 // Return the size of the GOT section.
620 gold_assert(this->got_ != NULL);
621 return this->got_->data_size();
624 // Get the PLT section.
625 const Output_data_plt_powerpc<size, big_endian>*
628 gold_assert(this->plt_ != NULL);
632 // Get the IPLT section.
633 const Output_data_plt_powerpc<size, big_endian>*
636 gold_assert(this->iplt_ != NULL);
640 // Get the .glink section.
641 const Output_data_glink<size, big_endian>*
642 glink_section() const
644 gold_assert(this->glink_ != NULL);
648 bool has_glink() const
649 { return this->glink_ != NULL; }
651 // Get the GOT section.
652 const Output_data_got_powerpc<size, big_endian>*
655 gold_assert(this->got_ != NULL);
659 // Get the GOT section, creating it if necessary.
660 Output_data_got_powerpc<size, big_endian>*
661 got_section(Symbol_table*, Layout*);
664 do_make_elf_object(const std::string&, Input_file*, off_t,
665 const elfcpp::Ehdr<size, big_endian>&);
667 // Return the number of entries in the GOT.
669 got_entry_count() const
671 if (this->got_ == NULL)
673 return this->got_size() / (size / 8);
676 // Return the number of entries in the PLT.
678 plt_entry_count() const;
680 // Return the offset of the first non-reserved PLT entry.
682 first_plt_entry_offset() const;
684 // Return the size of each PLT entry.
686 plt_entry_size() const;
688 // Add any special sections for this symbol to the gc work list.
689 // For powerpc64, this adds the code section of a function
692 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
694 // Handle target specific gc actions when adding a gc reference from
695 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
696 // and DST_OFF. For powerpc64, this adds a referenc to the code
697 // section of a function descriptor.
699 do_gc_add_reference(Symbol_table* symtab,
701 unsigned int src_shndx,
703 unsigned int dst_shndx,
704 Address dst_off) const;
706 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
709 { return this->stub_tables_; }
711 const Output_data_brlt_powerpc<size, big_endian>*
713 { return this->brlt_section_; }
716 add_branch_lookup_table(Address to)
718 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
719 this->branch_lookup_table_.insert(std::make_pair(to, off));
723 find_branch_lookup_table(Address to)
725 typename Branch_lookup_table::const_iterator p
726 = this->branch_lookup_table_.find(to);
727 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
731 write_branch_lookup_table(unsigned char *oview)
733 for (typename Branch_lookup_table::const_iterator p
734 = this->branch_lookup_table_.begin();
735 p != this->branch_lookup_table_.end();
738 elfcpp::Swap<32, big_endian>::writeval(oview + p->second, p->first);
743 plt_thread_safe() const
744 { return this->plt_thread_safe_; }
760 : tls_get_addr_(NOT_EXPECTED),
761 relinfo_(NULL), relnum_(0), r_offset_(0)
766 if (this->tls_get_addr_ != NOT_EXPECTED)
773 if (this->relinfo_ != NULL)
774 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
775 _("missing expected __tls_get_addr call"));
779 expect_tls_get_addr_call(
780 const Relocate_info<size, big_endian>* relinfo,
784 this->tls_get_addr_ = EXPECTED;
785 this->relinfo_ = relinfo;
786 this->relnum_ = relnum;
787 this->r_offset_ = r_offset;
791 expect_tls_get_addr_call()
792 { this->tls_get_addr_ = EXPECTED; }
795 skip_next_tls_get_addr_call()
796 {this->tls_get_addr_ = SKIP; }
799 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
801 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
802 || r_type == elfcpp::R_PPC_PLTREL24)
804 && strcmp(gsym->name(), "__tls_get_addr") == 0);
805 Tls_get_addr last_tls = this->tls_get_addr_;
806 this->tls_get_addr_ = NOT_EXPECTED;
807 if (is_tls_call && last_tls != EXPECTED)
809 else if (!is_tls_call && last_tls != NOT_EXPECTED)
818 // What we're up to regarding calls to __tls_get_addr.
819 // On powerpc, the branch and link insn making a call to
820 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
821 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
822 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
823 // The marker relocation always comes first, and has the same
824 // symbol as the reloc on the insn setting up the __tls_get_addr
825 // argument. This ties the arg setup insn with the call insn,
826 // allowing ld to safely optimize away the call. We check that
827 // every call to __tls_get_addr has a marker relocation, and that
828 // every marker relocation is on a call to __tls_get_addr.
829 Tls_get_addr tls_get_addr_;
830 // Info about the last reloc for error message.
831 const Relocate_info<size, big_endian>* relinfo_;
836 // The class which scans relocations.
837 class Scan : protected Track_tls
840 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
843 : Track_tls(), issued_non_pic_error_(false)
847 get_reference_flags(unsigned int r_type);
850 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
851 Sized_relobj_file<size, big_endian>* object,
852 unsigned int data_shndx,
853 Output_section* output_section,
854 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
855 const elfcpp::Sym<size, big_endian>& lsym,
859 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
860 Sized_relobj_file<size, big_endian>* object,
861 unsigned int data_shndx,
862 Output_section* output_section,
863 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
867 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
869 Sized_relobj_file<size, big_endian>* ,
872 const elfcpp::Rela<size, big_endian>& ,
874 const elfcpp::Sym<size, big_endian>&)
876 // PowerPC64 .opd is not folded, so any identical function text
877 // may be folded and we'll still keep function addresses distinct.
878 // That means no reloc is of concern here.
881 // For 32-bit, conservatively assume anything but calls to
882 // function code might be taking the address of the function.
883 return !is_branch_reloc(r_type);
887 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
889 Sized_relobj_file<size, big_endian>* ,
892 const elfcpp::Rela<size, big_endian>& ,
899 return !is_branch_reloc(r_type);
903 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
904 unsigned int r_type, bool report_err);
908 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
909 unsigned int r_type);
912 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
913 unsigned int r_type, Symbol*);
916 generate_tls_call(Symbol_table* symtab, Layout* layout,
917 Target_powerpc* target);
920 check_non_pic(Relobj*, unsigned int r_type);
922 // Whether we have issued an error about a non-PIC compilation.
923 bool issued_non_pic_error_;
927 symval_for_branch(const Symbol_table* symtab, Address value,
928 const Sized_symbol<size>* gsym,
929 Powerpc_relobj<size, big_endian>* object,
930 unsigned int *dest_shndx);
932 // The class which implements relocation.
933 class Relocate : protected Track_tls
936 // Use 'at' branch hints when true, 'y' when false.
937 // FIXME maybe: set this with an option.
938 static const bool is_isa_v2 = true;
944 // Do a relocation. Return false if the caller should not issue
945 // any warnings about this relocation.
947 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
948 Output_section*, size_t relnum,
949 const elfcpp::Rela<size, big_endian>&,
950 unsigned int r_type, const Sized_symbol<size>*,
951 const Symbol_value<size>*,
953 typename elfcpp::Elf_types<size>::Elf_Addr,
957 class Relocate_comdat_behavior
960 // Decide what the linker should do for relocations that refer to
961 // discarded comdat sections.
962 inline Comdat_behavior
963 get(const char* name)
965 gold::Default_comdat_behavior default_behavior;
966 Comdat_behavior ret = default_behavior.get(name);
967 if (ret == CB_WARNING)
970 && (strcmp(name, ".fixup") == 0
971 || strcmp(name, ".got2") == 0))
974 && (strcmp(name, ".opd") == 0
975 || strcmp(name, ".toc") == 0
976 || strcmp(name, ".toc1") == 0))
983 // A class which returns the size required for a relocation type,
984 // used while scanning relocs during a relocatable link.
985 class Relocatable_size_for_reloc
989 get_size_for_reloc(unsigned int, Relobj*)
996 // Optimize the TLS relocation type based on what we know about the
997 // symbol. IS_FINAL is true if the final address of this symbol is
998 // known at link time.
1000 tls::Tls_optimization
1001 optimize_tls_gd(bool is_final)
1003 // If we are generating a shared library, then we can't do anything
1005 if (parameters->options().shared())
1006 return tls::TLSOPT_NONE;
1009 return tls::TLSOPT_TO_IE;
1010 return tls::TLSOPT_TO_LE;
1013 tls::Tls_optimization
1016 if (parameters->options().shared())
1017 return tls::TLSOPT_NONE;
1019 return tls::TLSOPT_TO_LE;
1022 tls::Tls_optimization
1023 optimize_tls_ie(bool is_final)
1025 if (!is_final || parameters->options().shared())
1026 return tls::TLSOPT_NONE;
1028 return tls::TLSOPT_TO_LE;
1033 make_glink_section(Layout*);
1035 // Create the PLT section.
1037 make_plt_section(Symbol_table*, Layout*);
1040 make_iplt_section(Symbol_table*, Layout*);
1043 make_brlt_section(Layout*);
1045 // Create a PLT entry for a global symbol.
1047 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1049 // Create a PLT entry for a local IFUNC symbol.
1051 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1052 Sized_relobj_file<size, big_endian>*,
1056 // Create a GOT entry for local dynamic __tls_get_addr.
1058 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1059 Sized_relobj_file<size, big_endian>* object);
1062 tlsld_got_offset() const
1064 return this->tlsld_got_offset_;
1067 // Get the dynamic reloc section, creating it if necessary.
1069 rela_dyn_section(Layout*);
1071 // Similarly, but for ifunc symbols get the one for ifunc.
1073 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1075 // Copy a relocation against a global symbol.
1077 copy_reloc(Symbol_table* symtab, Layout* layout,
1078 Sized_relobj_file<size, big_endian>* object,
1079 unsigned int shndx, Output_section* output_section,
1080 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1082 this->copy_relocs_.copy_reloc(symtab, layout,
1083 symtab->get_sized_symbol<size>(sym),
1084 object, shndx, output_section,
1085 reloc, this->rela_dyn_section(layout));
1088 // Look over all the input sections, deciding where to place stubs.
1090 group_sections(Layout*, const Task*);
1092 // Sort output sections by address.
1093 struct Sort_sections
1096 operator()(const Output_section* sec1, const Output_section* sec2)
1097 { return sec1->address() < sec2->address(); }
1103 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1104 unsigned int data_shndx,
1106 unsigned int r_type,
1109 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1110 r_type_(r_type), r_sym_(r_sym), addend_(addend)
1116 // If this branch needs a plt call stub, or a long branch stub, make one.
1118 make_stub(Stub_table<size, big_endian>*,
1119 Stub_table<size, big_endian>*,
1120 Symbol_table*) const;
1123 // The branch location..
1124 Powerpc_relobj<size, big_endian>* object_;
1125 unsigned int shndx_;
1127 // ..and the branch type and destination.
1128 unsigned int r_type_;
1129 unsigned int r_sym_;
1133 // Information about this specific target which we pass to the
1134 // general Target structure.
1135 static Target::Target_info powerpc_info;
1137 // The types of GOT entries needed for this platform.
1138 // These values are exposed to the ABI in an incremental link.
1139 // Do not renumber existing values without changing the version
1140 // number of the .gnu_incremental_inputs section.
1144 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1145 GOT_TYPE_DTPREL, // entry for @got@dtprel
1146 GOT_TYPE_TPREL // entry for @got@tprel
1150 Output_data_got_powerpc<size, big_endian>* got_;
1151 // The PLT section. This is a container for a table of addresses,
1152 // and their relocations. Each address in the PLT has a dynamic
1153 // relocation (R_*_JMP_SLOT) and each address will have a
1154 // corresponding entry in .glink for lazy resolution of the PLT.
1155 // ppc32 initialises the PLT to point at the .glink entry, while
1156 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1157 // linker adds a stub that loads the PLT entry into ctr then
1158 // branches to ctr. There may be more than one stub for each PLT
1159 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1160 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1161 Output_data_plt_powerpc<size, big_endian>* plt_;
1162 // The IPLT section. Like plt_, this is a container for a table of
1163 // addresses and their relocations, specifically for STT_GNU_IFUNC
1164 // functions that resolve locally (STT_GNU_IFUNC functions that
1165 // don't resolve locally go in PLT). Unlike plt_, these have no
1166 // entry in .glink for lazy resolution, and the relocation section
1167 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1168 // the relocation section may contain relocations against
1169 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1170 // relocation section will appear at the end of other dynamic
1171 // relocations, so that ld.so applies these relocations after other
1172 // dynamic relocations. In a static executable, the relocation
1173 // section is emitted and marked with __rela_iplt_start and
1174 // __rela_iplt_end symbols.
1175 Output_data_plt_powerpc<size, big_endian>* iplt_;
1176 // Section holding long branch destinations.
1177 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1178 // The .glink section.
1179 Output_data_glink<size, big_endian>* glink_;
1180 // The dynamic reloc section.
1181 Reloc_section* rela_dyn_;
1182 // Relocs saved to avoid a COPY reloc.
1183 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1184 // Space for variables copied with a COPY reloc.
1185 Output_data_space* dynbss_;
1186 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1187 unsigned int tlsld_got_offset_;
1189 Stub_tables stub_tables_;
1190 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1191 Branch_lookup_table branch_lookup_table_;
1193 typedef std::vector<Branch_info> Branches;
1194 Branches branch_info_;
1196 bool plt_thread_safe_;
1200 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1203 true, // is_big_endian
1204 elfcpp::EM_PPC, // machine_code
1205 false, // has_make_symbol
1206 false, // has_resolve
1207 false, // has_code_fill
1208 true, // is_default_stack_executable
1209 false, // can_icf_inline_merge_sections
1211 "/usr/lib/ld.so.1", // dynamic_linker
1212 0x10000000, // default_text_segment_address
1213 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1214 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1215 false, // isolate_execinstr
1217 elfcpp::SHN_UNDEF, // small_common_shndx
1218 elfcpp::SHN_UNDEF, // large_common_shndx
1219 0, // small_common_section_flags
1220 0, // large_common_section_flags
1221 NULL, // attributes_section
1222 NULL // attributes_vendor
1226 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1229 false, // is_big_endian
1230 elfcpp::EM_PPC, // machine_code
1231 false, // has_make_symbol
1232 false, // has_resolve
1233 false, // has_code_fill
1234 true, // is_default_stack_executable
1235 false, // can_icf_inline_merge_sections
1237 "/usr/lib/ld.so.1", // dynamic_linker
1238 0x10000000, // default_text_segment_address
1239 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1240 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1241 false, // isolate_execinstr
1243 elfcpp::SHN_UNDEF, // small_common_shndx
1244 elfcpp::SHN_UNDEF, // large_common_shndx
1245 0, // small_common_section_flags
1246 0, // large_common_section_flags
1247 NULL, // attributes_section
1248 NULL // attributes_vendor
1252 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1255 true, // is_big_endian
1256 elfcpp::EM_PPC64, // machine_code
1257 false, // has_make_symbol
1258 false, // has_resolve
1259 false, // has_code_fill
1260 true, // is_default_stack_executable
1261 false, // can_icf_inline_merge_sections
1263 "/usr/lib/ld.so.1", // dynamic_linker
1264 0x10000000, // default_text_segment_address
1265 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1266 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1267 false, // isolate_execinstr
1269 elfcpp::SHN_UNDEF, // small_common_shndx
1270 elfcpp::SHN_UNDEF, // large_common_shndx
1271 0, // small_common_section_flags
1272 0, // large_common_section_flags
1273 NULL, // attributes_section
1274 NULL // attributes_vendor
1278 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1281 false, // is_big_endian
1282 elfcpp::EM_PPC64, // machine_code
1283 false, // has_make_symbol
1284 false, // has_resolve
1285 false, // has_code_fill
1286 true, // is_default_stack_executable
1287 false, // can_icf_inline_merge_sections
1289 "/usr/lib/ld.so.1", // dynamic_linker
1290 0x10000000, // default_text_segment_address
1291 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1292 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1293 false, // isolate_execinstr
1295 elfcpp::SHN_UNDEF, // small_common_shndx
1296 elfcpp::SHN_UNDEF, // large_common_shndx
1297 0, // small_common_section_flags
1298 0, // large_common_section_flags
1299 NULL, // attributes_section
1300 NULL // attributes_vendor
1304 is_branch_reloc(unsigned int r_type)
1306 return (r_type == elfcpp::R_POWERPC_REL24
1307 || r_type == elfcpp::R_PPC_PLTREL24
1308 || r_type == elfcpp::R_PPC_LOCAL24PC
1309 || r_type == elfcpp::R_POWERPC_REL14
1310 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1311 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1312 || r_type == elfcpp::R_POWERPC_ADDR24
1313 || r_type == elfcpp::R_POWERPC_ADDR14
1314 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1315 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1318 // If INSN is an opcode that may be used with an @tls operand, return
1319 // the transformed insn for TLS optimisation, otherwise return 0. If
1320 // REG is non-zero only match an insn with RB or RA equal to REG.
1322 at_tls_transform(uint32_t insn, unsigned int reg)
1324 if ((insn & (0x3f << 26)) != 31 << 26)
1328 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1329 rtra = insn & ((1 << 26) - (1 << 16));
1330 else if (((insn >> 16) & 0x1f) == reg)
1331 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1335 if ((insn & (0x3ff << 1)) == 266 << 1)
1338 else if ((insn & (0x1f << 1)) == 23 << 1
1339 && ((insn & (0x1f << 6)) < 14 << 6
1340 || ((insn & (0x1f << 6)) >= 16 << 6
1341 && (insn & (0x1f << 6)) < 24 << 6)))
1342 // load and store indexed -> dform
1343 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1344 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1345 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1346 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1347 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1349 insn = (58 << 26) | 2;
1356 // Modified version of symtab.h class Symbol member
1357 // Given a direct absolute or pc-relative static relocation against
1358 // the global symbol, this function returns whether a dynamic relocation
1363 needs_dynamic_reloc(const Symbol* gsym, int flags)
1365 // No dynamic relocations in a static link!
1366 if (parameters->doing_static_link())
1369 // A reference to an undefined symbol from an executable should be
1370 // statically resolved to 0, and does not need a dynamic relocation.
1371 // This matches gnu ld behavior.
1372 if (gsym->is_undefined() && !parameters->options().shared())
1375 // A reference to an absolute symbol does not need a dynamic relocation.
1376 if (gsym->is_absolute())
1379 // An absolute reference within a position-independent output file
1380 // will need a dynamic relocation.
1381 if ((flags & Symbol::ABSOLUTE_REF)
1382 && parameters->options().output_is_position_independent())
1385 // A function call that can branch to a local PLT entry does not need
1386 // a dynamic relocation.
1387 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
1390 // A reference to any PLT entry in a non-position-independent executable
1391 // does not need a dynamic relocation.
1392 // Except due to having function descriptors on powerpc64 we don't define
1393 // functions to their plt code in an executable, so this doesn't apply.
1395 && !parameters->options().output_is_position_independent()
1396 && gsym->has_plt_offset())
1399 // A reference to a symbol defined in a dynamic object or to a
1400 // symbol that is preemptible will need a dynamic relocation.
1401 if (gsym->is_from_dynobj()
1402 || gsym->is_undefined()
1403 || gsym->is_preemptible())
1406 // For all other cases, return FALSE.
1410 // Modified version of symtab.h class Symbol member
1411 // Whether we should use the PLT offset associated with a symbol for
1412 // a relocation. FLAGS is a set of Reference_flags.
1416 use_plt_offset(const Symbol* gsym, int flags)
1418 // If the symbol doesn't have a PLT offset, then naturally we
1419 // don't want to use it.
1420 if (!gsym->has_plt_offset())
1423 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1424 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1427 // If we are going to generate a dynamic relocation, then we will
1428 // wind up using that, so no need to use the PLT entry.
1429 if (needs_dynamic_reloc<size>(gsym, flags))
1432 // If the symbol is from a dynamic object, we need to use the PLT
1434 if (gsym->is_from_dynobj())
1437 // If we are generating a shared object, and this symbol is
1438 // undefined or preemptible, we need to use the PLT entry.
1439 if (parameters->options().shared()
1440 && (gsym->is_undefined() || gsym->is_preemptible()))
1443 // If this is a call to a weak undefined symbol, we need to use
1444 // the PLT entry; the symbol may be defined by a library loaded
1446 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1449 // Otherwise we can use the regular definition.
1453 template<int size, bool big_endian>
1454 class Powerpc_relocate_functions
1471 typedef Powerpc_relocate_functions<size, big_endian> This;
1472 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1474 template<int valsize>
1476 has_overflow_signed(Address value)
1478 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1479 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1480 limit <<= ((valsize - 1) >> 1);
1481 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1482 return value + limit > (limit << 1) - 1;
1485 template<int valsize>
1487 has_overflow_bitfield(Address value)
1489 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1490 limit <<= ((valsize - 1) >> 1);
1491 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1492 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1495 template<int valsize>
1496 static inline Status
1497 overflowed(Address value, Overflow_check overflow)
1499 if (overflow == CHECK_SIGNED)
1501 if (has_overflow_signed<valsize>(value))
1502 return STATUS_OVERFLOW;
1504 else if (overflow == CHECK_BITFIELD)
1506 if (has_overflow_bitfield<valsize>(value))
1507 return STATUS_OVERFLOW;
1512 // Do a simple RELA relocation
1513 template<int valsize>
1514 static inline Status
1515 rela(unsigned char* view, Address value, Overflow_check overflow)
1517 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1518 Valtype* wv = reinterpret_cast<Valtype*>(view);
1519 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1520 return overflowed<valsize>(value, overflow);
1523 template<int valsize>
1524 static inline Status
1525 rela(unsigned char* view,
1526 unsigned int right_shift,
1527 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1529 Overflow_check overflow)
1531 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1532 Valtype* wv = reinterpret_cast<Valtype*>(view);
1533 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1534 Valtype reloc = value >> right_shift;
1537 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1538 return overflowed<valsize>(value >> right_shift, overflow);
1541 // Do a simple RELA relocation, unaligned.
1542 template<int valsize>
1543 static inline Status
1544 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1546 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1547 return overflowed<valsize>(value, overflow);
1550 template<int valsize>
1551 static inline Status
1552 rela_ua(unsigned char* view,
1553 unsigned int right_shift,
1554 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1556 Overflow_check overflow)
1558 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1560 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1561 Valtype reloc = value >> right_shift;
1564 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1565 return overflowed<valsize>(value >> right_shift, overflow);
1569 // R_PPC64_ADDR64: (Symbol + Addend)
1571 addr64(unsigned char* view, Address value)
1572 { This::template rela<64>(view, value, CHECK_NONE); }
1574 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1576 addr64_u(unsigned char* view, Address value)
1577 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1579 // R_POWERPC_ADDR32: (Symbol + Addend)
1580 static inline Status
1581 addr32(unsigned char* view, Address value, Overflow_check overflow)
1582 { return This::template rela<32>(view, value, overflow); }
1584 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1585 static inline Status
1586 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1587 { return This::template rela_ua<32>(view, value, overflow); }
1589 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1590 static inline Status
1591 addr24(unsigned char* view, Address value, Overflow_check overflow)
1593 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1594 if (overflow != CHECK_NONE && (value & 3) != 0)
1595 stat = STATUS_OVERFLOW;
1599 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1600 static inline Status
1601 addr16(unsigned char* view, Address value, Overflow_check overflow)
1602 { return This::template rela<16>(view, value, overflow); }
1604 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1605 static inline Status
1606 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1607 { return This::template rela_ua<16>(view, value, overflow); }
1609 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1610 static inline Status
1611 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1613 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1614 if (overflow != CHECK_NONE && (value & 3) != 0)
1615 stat = STATUS_OVERFLOW;
1619 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1621 addr16_hi(unsigned char* view, Address value)
1622 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1624 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1626 addr16_ha(unsigned char* view, Address value)
1627 { This::addr16_hi(view, value + 0x8000); }
1629 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1631 addr16_hi2(unsigned char* view, Address value)
1632 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1634 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1636 addr16_ha2(unsigned char* view, Address value)
1637 { This::addr16_hi2(view, value + 0x8000); }
1639 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1641 addr16_hi3(unsigned char* view, Address value)
1642 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1644 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1646 addr16_ha3(unsigned char* view, Address value)
1647 { This::addr16_hi3(view, value + 0x8000); }
1649 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1650 static inline Status
1651 addr14(unsigned char* view, Address value, Overflow_check overflow)
1653 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1654 if (overflow != CHECK_NONE && (value & 3) != 0)
1655 stat = STATUS_OVERFLOW;
1660 // Stash away the index of .got2 or .opd in a relocatable object, if
1661 // such a section exists.
1663 template<int size, bool big_endian>
1665 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1666 Read_symbols_data* sd)
1668 const unsigned char* const pshdrs = sd->section_headers->data();
1669 const unsigned char* namesu = sd->section_names->data();
1670 const char* names = reinterpret_cast<const char*>(namesu);
1671 section_size_type names_size = sd->section_names_size;
1672 const unsigned char* s;
1674 s = this->template find_shdr<size, big_endian>(pshdrs,
1675 size == 32 ? ".got2" : ".opd",
1676 names, names_size, NULL);
1679 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1680 this->special_ = ndx;
1682 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1685 // Examine .rela.opd to build info about function entry points.
1687 template<int size, bool big_endian>
1689 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1691 const unsigned char* prelocs,
1692 const unsigned char* plocal_syms)
1696 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1698 const int reloc_size
1699 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1700 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1701 Address expected_off = 0;
1702 bool regular = true;
1703 unsigned int opd_ent_size = 0;
1705 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1707 Reltype reloc(prelocs);
1708 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1709 = reloc.get_r_info();
1710 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1711 if (r_type == elfcpp::R_PPC64_ADDR64)
1713 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1714 typename elfcpp::Elf_types<size>::Elf_Addr value;
1717 if (r_sym < this->local_symbol_count())
1719 typename elfcpp::Sym<size, big_endian>
1720 lsym(plocal_syms + r_sym * sym_size);
1721 shndx = lsym.get_st_shndx();
1722 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1723 value = lsym.get_st_value();
1726 shndx = this->symbol_section_and_value(r_sym, &value,
1728 this->set_opd_ent(reloc.get_r_offset(), shndx,
1729 value + reloc.get_r_addend());
1732 expected_off = reloc.get_r_offset();
1733 opd_ent_size = expected_off;
1735 else if (expected_off != reloc.get_r_offset())
1737 expected_off += opd_ent_size;
1739 else if (r_type == elfcpp::R_PPC64_TOC)
1741 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1746 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1747 this->name().c_str(), r_type);
1751 if (reloc_count <= 2)
1752 opd_ent_size = this->section_size(this->opd_shndx());
1753 if (opd_ent_size != 24 && opd_ent_size != 16)
1757 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1758 this->name().c_str());
1764 template<int size, bool big_endian>
1766 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1768 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1771 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1772 p != rd->relocs.end();
1775 if (p->data_shndx == this->opd_shndx())
1777 uint64_t opd_size = this->section_size(this->opd_shndx());
1778 gold_assert(opd_size == static_cast<size_t>(opd_size));
1781 this->init_opd(opd_size);
1782 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1783 rd->local_symbols->data());
1791 // Call Sized_dynobj::do_read_symbols to read the symbols then
1792 // read .opd from a dynamic object, filling in opd_ent_ vector,
1794 template<int size, bool big_endian>
1796 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1798 Sized_dynobj<size, big_endian>::do_read_symbols(sd);
1801 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1802 const unsigned char* const pshdrs = sd->section_headers->data();
1803 const unsigned char* namesu = sd->section_names->data();
1804 const char* names = reinterpret_cast<const char*>(namesu);
1805 const unsigned char* s = NULL;
1806 const unsigned char* opd;
1807 section_size_type opd_size;
1809 // Find and read .opd section.
1812 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
1813 sd->section_names_size,
1818 typename elfcpp::Shdr<size, big_endian> shdr(s);
1819 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1820 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1822 this->opd_shndx_ = (s - pshdrs) / shdr_size;
1823 this->opd_address_ = shdr.get_sh_addr();
1824 opd_size = convert_to_section_size_type(shdr.get_sh_size());
1825 opd = this->get_view(shdr.get_sh_offset(), opd_size,
1831 // Build set of executable sections.
1832 // Using a set is probably overkill. There is likely to be only
1833 // a few executable sections, typically .init, .text and .fini,
1834 // and they are generally grouped together.
1835 typedef std::set<Sec_info> Exec_sections;
1836 Exec_sections exec_sections;
1838 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
1840 typename elfcpp::Shdr<size, big_endian> shdr(s);
1841 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1842 && ((shdr.get_sh_flags()
1843 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1844 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1845 && shdr.get_sh_size() != 0)
1847 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
1848 shdr.get_sh_size(), i));
1851 if (exec_sections.empty())
1854 // Look over the OPD entries. This is complicated by the fact
1855 // that some binaries will use two-word entries while others
1856 // will use the standard three-word entries. In most cases
1857 // the third word (the environment pointer for languages like
1858 // Pascal) is unused and will be zero. If the third word is
1859 // used it should not be pointing into executable sections,
1861 this->init_opd(opd_size);
1862 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
1864 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1865 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
1866 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
1868 // Chances are that this is the third word of an OPD entry.
1870 typename Exec_sections::const_iterator e
1871 = exec_sections.upper_bound(Sec_info(val, 0, 0));
1872 if (e != exec_sections.begin())
1875 if (e->start <= val && val < e->start + e->len)
1877 // We have an address in an executable section.
1878 // VAL ought to be the function entry, set it up.
1879 this->set_opd_ent(p - opd, e->shndx, val);
1880 // Skip second word of OPD entry, the TOC pointer.
1884 // If we didn't match any executable sections, we likely
1885 // have a non-zero third word in the OPD entry.
1890 // Set up some symbols.
1892 template<int size, bool big_endian>
1894 Target_powerpc<size, big_endian>::do_define_standard_symbols(
1895 Symbol_table* symtab,
1900 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1901 // undefined when scanning relocs (and thus requires
1902 // non-relative dynamic relocs). The proper value will be
1904 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1905 if (gotsym != NULL && gotsym->is_undefined())
1907 Target_powerpc<size, big_endian>* target =
1908 static_cast<Target_powerpc<size, big_endian>*>(
1909 parameters->sized_target<size, big_endian>());
1910 Output_data_got_powerpc<size, big_endian>* got
1911 = target->got_section(symtab, layout);
1912 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1913 Symbol_table::PREDEFINED,
1917 elfcpp::STV_HIDDEN, 0,
1921 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1922 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
1923 if (sdasym != NULL && sdasym->is_undefined())
1925 Output_data_space* sdata = new Output_data_space(4, "** sdata");
1927 = layout->add_output_section_data(".sdata", 0,
1929 | elfcpp::SHF_WRITE,
1930 sdata, ORDER_SMALL_DATA, false);
1931 symtab->define_in_output_data("_SDA_BASE_", NULL,
1932 Symbol_table::PREDEFINED,
1933 os, 32768, 0, elfcpp::STT_OBJECT,
1934 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
1940 // Set up PowerPC target specific relobj.
1942 template<int size, bool big_endian>
1944 Target_powerpc<size, big_endian>::do_make_elf_object(
1945 const std::string& name,
1946 Input_file* input_file,
1947 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1949 int et = ehdr.get_e_type();
1950 // ET_EXEC files are valid input for --just-symbols/-R,
1951 // and we treat them as relocatable objects.
1952 if (et == elfcpp::ET_REL
1953 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1955 Powerpc_relobj<size, big_endian>* obj =
1956 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1960 else if (et == elfcpp::ET_DYN)
1962 Powerpc_dynobj<size, big_endian>* obj =
1963 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1969 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1974 template<int size, bool big_endian>
1975 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1978 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1979 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1981 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1982 : Output_data_got<size, big_endian>(),
1983 symtab_(symtab), layout_(layout),
1984 header_ent_cnt_(size == 32 ? 3 : 1),
1985 header_index_(size == 32 ? 0x2000 : 0)
1990 // Create a new GOT entry and return its offset.
1992 add_got_entry(Got_entry got_entry)
1994 this->reserve_ent();
1995 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1998 // Create a pair of new GOT entries and return the offset of the first.
2000 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
2002 this->reserve_ent(2);
2003 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
2008 add_constant_pair(Valtype c1, Valtype c2)
2010 this->reserve_ent(2);
2011 unsigned int got_offset = this->add_constant(c1);
2012 this->add_constant(c2);
2016 // Offset of _GLOBAL_OFFSET_TABLE_.
2020 return this->got_offset(this->header_index_);
2023 // Offset of base used to access the GOT/TOC.
2024 // The got/toc pointer reg will be set to this value.
2026 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2029 return this->g_o_t();
2031 return (this->output_section()->address()
2032 + object->toc_base_offset()
2036 // Ensure our GOT has a header.
2038 set_final_data_size()
2040 if (this->header_ent_cnt_ != 0)
2041 this->make_header();
2042 Output_data_got<size, big_endian>::set_final_data_size();
2045 // First word of GOT header needs some values that are not
2046 // handled by Output_data_got so poke them in here.
2047 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2049 do_write(Output_file* of)
2052 if (size == 32 && this->layout_->dynamic_data() != NULL)
2053 val = this->layout_->dynamic_section()->address();
2055 val = this->output_section()->address() + 0x8000;
2056 this->replace_constant(this->header_index_, val);
2057 Output_data_got<size, big_endian>::do_write(of);
2062 reserve_ent(unsigned int cnt = 1)
2064 if (this->header_ent_cnt_ == 0)
2066 if (this->num_entries() + cnt > this->header_index_)
2067 this->make_header();
2073 this->header_ent_cnt_ = 0;
2074 this->header_index_ = this->num_entries();
2077 Output_data_got<size, big_endian>::add_constant(0);
2078 Output_data_got<size, big_endian>::add_constant(0);
2079 Output_data_got<size, big_endian>::add_constant(0);
2081 // Define _GLOBAL_OFFSET_TABLE_ at the header
2082 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2085 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2086 sym->set_value(this->g_o_t());
2089 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2090 Symbol_table::PREDEFINED,
2091 this, this->g_o_t(), 0,
2094 elfcpp::STV_HIDDEN, 0,
2098 Output_data_got<size, big_endian>::add_constant(0);
2101 // Stashed pointers.
2102 Symbol_table* symtab_;
2106 unsigned int header_ent_cnt_;
2107 // GOT header index.
2108 unsigned int header_index_;
2111 // Get the GOT section, creating it if necessary.
2113 template<int size, bool big_endian>
2114 Output_data_got_powerpc<size, big_endian>*
2115 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2118 if (this->got_ == NULL)
2120 gold_assert(symtab != NULL && layout != NULL);
2123 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2125 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2126 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2127 this->got_, ORDER_DATA, false);
2133 // Get the dynamic reloc section, creating it if necessary.
2135 template<int size, bool big_endian>
2136 typename Target_powerpc<size, big_endian>::Reloc_section*
2137 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2139 if (this->rela_dyn_ == NULL)
2141 gold_assert(layout != NULL);
2142 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2143 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2144 elfcpp::SHF_ALLOC, this->rela_dyn_,
2145 ORDER_DYNAMIC_RELOCS, false);
2147 return this->rela_dyn_;
2150 // Similarly, but for ifunc symbols get the one for ifunc.
2152 template<int size, bool big_endian>
2153 typename Target_powerpc<size, big_endian>::Reloc_section*
2154 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2159 return this->rela_dyn_section(layout);
2161 if (this->iplt_ == NULL)
2162 this->make_iplt_section(symtab, layout);
2163 return this->iplt_->rel_plt();
2169 // Determine the stub group size. The group size is the absolute
2170 // value of the parameter --stub-group-size. If --stub-group-size
2171 // is passed a negative value, we restrict stubs to be always before
2172 // the stubbed branches.
2173 Stub_control(int32_t size)
2174 : state_(NO_GROUP), stub_group_size_(abs(size)),
2175 stub14_group_size_(abs(size)),
2176 stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
2177 group_end_addr_(0), owner_(NULL), output_section_(NULL)
2179 if (stub_group_size_ == 1)
2182 if (stubs_always_before_branch_)
2184 stub_group_size_ = 0x1e00000;
2185 stub14_group_size_ = 0x7800;
2189 stub_group_size_ = 0x1c00000;
2190 stub14_group_size_ = 0x7000;
2192 suppress_size_errors_ = true;
2196 // Return true iff input section can be handled by current stub
2199 can_add_to_stub_group(Output_section* o,
2200 const Output_section::Input_section* i,
2203 const Output_section::Input_section*
2209 { return output_section_; }
2215 FINDING_STUB_SECTION,
2220 uint32_t stub_group_size_;
2221 uint32_t stub14_group_size_;
2222 bool stubs_always_before_branch_;
2223 bool suppress_size_errors_;
2224 uint64_t group_end_addr_;
2225 const Output_section::Input_section* owner_;
2226 Output_section* output_section_;
2229 // Return true iff input section can be handled by current stub
2233 Stub_control::can_add_to_stub_group(Output_section* o,
2234 const Output_section::Input_section* i,
2238 = has14 ? this->stub14_group_size_ : this->stub_group_size_;
2239 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2241 uint64_t start_addr = o->address();
2244 // .init and .fini sections are pasted together to form a single
2245 // function. We can't be adding stubs in the middle of the function.
2246 this_size = o->data_size();
2249 start_addr += i->relobj()->output_section_offset(i->shndx());
2250 this_size = i->data_size();
2252 uint64_t end_addr = start_addr + this_size;
2253 bool toobig = this_size > group_size;
2255 if (toobig && !this->suppress_size_errors_)
2256 gold_warning(_("%s:%s exceeds group size"),
2257 i->relobj()->name().c_str(),
2258 i->relobj()->section_name(i->shndx()).c_str());
2260 if (this->state_ != HAS_STUB_SECTION
2261 && (!whole_sec || this->output_section_ != o)
2262 && (this->state_ == NO_GROUP
2263 || this->group_end_addr_ - end_addr < group_size))
2266 this->output_section_ = o;
2269 if (this->state_ == NO_GROUP)
2271 this->state_ = FINDING_STUB_SECTION;
2272 this->group_end_addr_ = end_addr;
2274 else if (this->group_end_addr_ - start_addr < group_size)
2276 // Adding this section would make the group larger than GROUP_SIZE.
2277 else if (this->state_ == FINDING_STUB_SECTION
2278 && !this->stubs_always_before_branch_
2281 // But wait, there's more! Input sections up to GROUP_SIZE
2282 // bytes before the stub table can be handled by it too.
2283 this->state_ = HAS_STUB_SECTION;
2284 this->group_end_addr_ = end_addr;
2288 this->state_ = NO_GROUP;
2294 // Look over all the input sections, deciding where to place stubs.
2296 template<int size, bool big_endian>
2298 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2301 Stub_control stub_control(parameters->options().stub_group_size());
2303 // Group input sections and insert stub table
2304 Stub_table<size, big_endian>* stub_table = NULL;
2305 Layout::Section_list section_list;
2306 layout->get_executable_sections(§ion_list);
2307 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2308 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2309 o != section_list.rend();
2312 typedef Output_section::Input_section_list Input_section_list;
2313 for (Input_section_list::const_reverse_iterator i
2314 = (*o)->input_sections().rbegin();
2315 i != (*o)->input_sections().rend();
2318 if (i->is_input_section())
2320 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2321 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2322 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2323 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2325 stub_table->init(stub_control.owner(),
2326 stub_control.output_section());
2329 if (stub_table == NULL)
2330 stub_table = this->new_stub_table();
2331 ppcobj->set_stub_table(i->shndx(), stub_table);
2335 if (stub_table != NULL)
2337 const Output_section::Input_section* i = stub_control.owner();
2338 if (!i->is_input_section())
2340 // Corner case. A new stub group was made for the first
2341 // section (last one looked at here) for some reason, but
2342 // the first section is already being used as the owner for
2343 // a stub table for following sections. Force it into that
2345 gold_assert(this->stub_tables_.size() >= 2);
2346 this->stub_tables_.pop_back();
2348 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2349 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2350 ppcobj->set_stub_table(i->shndx(), this->stub_tables_.back());
2353 stub_table->init(i, stub_control.output_section());
2357 // If this branch needs a plt call stub, or a long branch stub, make one.
2359 template<int size, bool big_endian>
2361 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2362 Stub_table<size, big_endian>* stub_table,
2363 Stub_table<size, big_endian>* ifunc_stub_table,
2364 Symbol_table* symtab) const
2366 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2367 if (sym != NULL && sym->is_forwarder())
2368 sym = symtab->resolve_forwards(sym);
2369 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2371 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(this->r_type_))
2372 : this->object_->local_has_plt_offset(this->r_sym_))
2374 if (stub_table == NULL)
2375 stub_table = this->object_->stub_table(this->shndx_);
2376 if (stub_table == NULL)
2378 // This is a ref from a data section to an ifunc symbol.
2379 stub_table = ifunc_stub_table;
2381 gold_assert(stub_table != NULL);
2383 stub_table->add_plt_call_entry(this->object_, gsym,
2384 this->r_type_, this->addend_);
2386 stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2387 this->r_type_, this->addend_);
2391 unsigned int max_branch_offset;
2392 if (this->r_type_ == elfcpp::R_POWERPC_REL14
2393 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2394 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2395 max_branch_offset = 1 << 15;
2396 else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2397 || this->r_type_ == elfcpp::R_PPC_PLTREL24
2398 || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2399 max_branch_offset = 1 << 25;
2402 Address from = this->object_->get_output_section_offset(this->shndx_);
2403 gold_assert(from != invalid_address);
2404 from += (this->object_->output_section(this->shndx_)->address()
2409 switch (gsym->source())
2411 case Symbol::FROM_OBJECT:
2413 Object* symobj = gsym->object();
2414 if (symobj->is_dynamic()
2415 || symobj->pluginobj() != NULL)
2418 unsigned int shndx = gsym->shndx(&is_ordinary);
2419 if (shndx == elfcpp::SHN_UNDEF)
2424 case Symbol::IS_UNDEFINED:
2430 Symbol_table::Compute_final_value_status status;
2431 to = symtab->compute_final_value<size>(gsym, &status);
2432 if (status != Symbol_table::CFVS_OK)
2437 const Symbol_value<size>* psymval
2438 = this->object_->local_symbol(this->r_sym_);
2439 Symbol_value<size> symval;
2440 typedef Sized_relobj_file<size, big_endian> ObjType;
2441 typename ObjType::Compute_final_local_value_status status
2442 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2444 if (status != ObjType::CFLV_OK
2445 || !symval.has_output_value())
2447 to = symval.value(this->object_, 0);
2449 to += this->addend_;
2450 if (stub_table == NULL)
2451 stub_table = this->object_->stub_table(this->shndx_);
2452 if (size == 64 && is_branch_reloc(this->r_type_))
2454 unsigned int dest_shndx;
2455 Target_powerpc<size, big_endian>* target =
2456 static_cast<Target_powerpc<size, big_endian>*>(
2457 parameters->sized_target<size, big_endian>());
2458 to = target->symval_for_branch(symtab, to, gsym,
2459 this->object_, &dest_shndx);
2461 Address delta = to - from;
2462 if (delta + max_branch_offset >= 2 * max_branch_offset)
2464 if (stub_table == NULL)
2466 gold_warning(_("%s:%s: branch in non-executable section,"
2467 " no long branch stub for you"),
2468 this->object_->name().c_str(),
2469 this->object_->section_name(this->shndx_).c_str());
2472 stub_table->add_long_branch_entry(this->object_, to);
2477 // Relaxation hook. This is where we do stub generation.
2479 template<int size, bool big_endian>
2481 Target_powerpc<size, big_endian>::do_relax(int pass,
2482 const Input_objects*,
2483 Symbol_table* symtab,
2487 unsigned int prev_brlt_size = 0;
2490 bool thread_safe = parameters->options().plt_thread_safe();
2491 if (size == 64 && !parameters->options().user_set_plt_thread_safe())
2493 static const char* const thread_starter[] =
2497 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2499 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2500 "mq_notify", "create_timer",
2504 "GOMP_parallel_start",
2505 "GOMP_parallel_loop_static_start",
2506 "GOMP_parallel_loop_dynamic_start",
2507 "GOMP_parallel_loop_guided_start",
2508 "GOMP_parallel_loop_runtime_start",
2509 "GOMP_parallel_sections_start",
2512 if (parameters->options().shared())
2516 for (unsigned int i = 0;
2517 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2520 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2521 thread_safe = (sym != NULL
2523 && sym->in_real_elf());
2529 this->plt_thread_safe_ = thread_safe;
2530 this->group_sections(layout, task);
2533 // We need address of stub tables valid for make_stub.
2534 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2535 p != this->stub_tables_.end();
2538 const Powerpc_relobj<size, big_endian>* object
2539 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2540 Address off = object->get_output_section_offset((*p)->shndx());
2541 gold_assert(off != invalid_address);
2542 Output_section* os = (*p)->output_section();
2543 (*p)->set_address_and_size(os, off);
2548 // Clear plt call stubs, long branch stubs and branch lookup table.
2549 prev_brlt_size = this->branch_lookup_table_.size();
2550 this->branch_lookup_table_.clear();
2551 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2552 p != this->stub_tables_.end();
2555 (*p)->clear_stubs();
2559 // Build all the stubs.
2560 Stub_table<size, big_endian>* ifunc_stub_table
2561 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2562 Stub_table<size, big_endian>* one_stub_table
2563 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2564 for (typename Branches::const_iterator b = this->branch_info_.begin();
2565 b != this->branch_info_.end();
2568 b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2571 // Did anything change size?
2572 unsigned int num_huge_branches = this->branch_lookup_table_.size();
2573 bool again = num_huge_branches != prev_brlt_size;
2574 if (size == 64 && num_huge_branches != 0)
2575 this->make_brlt_section(layout);
2576 if (size == 64 && again)
2577 this->brlt_section_->set_current_size(num_huge_branches);
2579 typedef Unordered_set<Output_section*> Output_sections;
2580 Output_sections os_need_update;
2581 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2582 p != this->stub_tables_.end();
2585 if ((*p)->size_update())
2588 (*p)->add_eh_frame(layout);
2589 os_need_update.insert((*p)->output_section());
2593 // Set output section offsets for all input sections in an output
2594 // section that just changed size. Anything past the stubs will
2596 for (typename Output_sections::iterator p = os_need_update.begin();
2597 p != os_need_update.end();
2600 Output_section* os = *p;
2602 typedef Output_section::Input_section_list Input_section_list;
2603 for (Input_section_list::const_iterator i = os->input_sections().begin();
2604 i != os->input_sections().end();
2607 off = align_address(off, i->addralign());
2608 if (i->is_input_section() || i->is_relaxed_input_section())
2609 i->relobj()->set_section_offset(i->shndx(), off);
2610 if (i->is_relaxed_input_section())
2612 Stub_table<size, big_endian>* stub_table
2613 = static_cast<Stub_table<size, big_endian>*>(
2614 i->relaxed_input_section());
2615 off += stub_table->set_address_and_size(os, off);
2618 off += i->data_size();
2620 // If .branch_lt is part of this output section, then we have
2621 // just done the offset adjustment.
2622 os->clear_section_offsets_need_adjustment();
2627 && num_huge_branches != 0
2628 && parameters->options().output_is_position_independent())
2630 // Fill in the BRLT relocs.
2631 this->brlt_section_->reset_data_size();
2632 for (typename Branch_lookup_table::const_iterator p
2633 = this->branch_lookup_table_.begin();
2634 p != this->branch_lookup_table_.end();
2637 this->brlt_section_->add_reloc(p->first, p->second);
2639 this->brlt_section_->finalize_data_size();
2644 template<int size, bool big_endian>
2646 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2647 unsigned char* oview,
2651 uint64_t address = plt->address();
2652 off_t len = plt->data_size();
2654 if (plt == this->glink_)
2656 // See Output_data_glink::do_write() for glink contents.
2659 // There is one word before __glink_PLTresolve
2663 else if (parameters->options().output_is_position_independent())
2665 // There are two FDEs for a position independent glink.
2666 // The first covers the branch table, the second
2667 // __glink_PLTresolve at the end of glink.
2668 off_t resolve_size = this->glink_->pltresolve_size;
2670 len -= resolve_size;
2673 address += len - resolve_size;
2680 // Must be a stub table.
2681 const Stub_table<size, big_endian>* stub_table
2682 = static_cast<const Stub_table<size, big_endian>*>(plt);
2683 uint64_t stub_address = stub_table->stub_address();
2684 len -= stub_address - address;
2685 address = stub_address;
2688 *paddress = address;
2692 // A class to handle the PLT data.
2694 template<int size, bool big_endian>
2695 class Output_data_plt_powerpc : public Output_section_data_build
2698 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2699 size, big_endian> Reloc_section;
2701 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2702 Reloc_section* plt_rel,
2703 unsigned int reserved_size,
2705 : Output_section_data_build(size == 32 ? 4 : 8),
2708 initial_plt_entry_size_(reserved_size),
2712 // Add an entry to the PLT.
2717 add_ifunc_entry(Symbol*);
2720 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2722 // Return the .rela.plt section data.
2729 // Return the number of PLT entries.
2733 if (this->current_data_size() == 0)
2735 return ((this->current_data_size() - this->initial_plt_entry_size_)
2739 // Return the offset of the first non-reserved PLT entry.
2741 first_plt_entry_offset()
2742 { return this->initial_plt_entry_size_; }
2744 // Return the size of a PLT entry.
2746 get_plt_entry_size()
2747 { return plt_entry_size; }
2751 do_adjust_output_section(Output_section* os)
2756 // Write to a map file.
2758 do_print_to_mapfile(Mapfile* mapfile) const
2759 { mapfile->print_output_data(this, this->name_); }
2762 // The size of an entry in the PLT.
2763 static const int plt_entry_size = size == 32 ? 4 : 24;
2765 // Write out the PLT data.
2767 do_write(Output_file*);
2769 // The reloc section.
2770 Reloc_section* rel_;
2771 // Allows access to .glink for do_write.
2772 Target_powerpc<size, big_endian>* targ_;
2773 // The size of the first reserved entry.
2774 int initial_plt_entry_size_;
2775 // What to report in map file.
2779 // Add an entry to the PLT.
2781 template<int size, bool big_endian>
2783 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
2785 if (!gsym->has_plt_offset())
2787 section_size_type off = this->current_data_size();
2789 off += this->first_plt_entry_offset();
2790 gsym->set_plt_offset(off);
2791 gsym->set_needs_dynsym_entry();
2792 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
2793 this->rel_->add_global(gsym, dynrel, this, off, 0);
2794 off += plt_entry_size;
2795 this->set_current_data_size(off);
2799 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2801 template<int size, bool big_endian>
2803 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
2805 if (!gsym->has_plt_offset())
2807 section_size_type off = this->current_data_size();
2808 gsym->set_plt_offset(off);
2809 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2811 dynrel = elfcpp::R_PPC64_JMP_IREL;
2812 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
2813 off += plt_entry_size;
2814 this->set_current_data_size(off);
2818 // Add an entry for a local ifunc symbol to the IPLT.
2820 template<int size, bool big_endian>
2822 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
2823 Sized_relobj_file<size, big_endian>* relobj,
2824 unsigned int local_sym_index)
2826 if (!relobj->local_has_plt_offset(local_sym_index))
2828 section_size_type off = this->current_data_size();
2829 relobj->set_local_plt_offset(local_sym_index, off);
2830 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2832 dynrel = elfcpp::R_PPC64_JMP_IREL;
2833 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
2835 off += plt_entry_size;
2836 this->set_current_data_size(off);
2840 static const uint32_t add_0_11_11 = 0x7c0b5a14;
2841 static const uint32_t add_2_2_11 = 0x7c425a14;
2842 static const uint32_t add_3_3_2 = 0x7c631214;
2843 static const uint32_t add_3_3_13 = 0x7c636a14;
2844 static const uint32_t add_11_0_11 = 0x7d605a14;
2845 static const uint32_t add_12_2_11 = 0x7d825a14;
2846 static const uint32_t add_12_12_11 = 0x7d8c5a14;
2847 static const uint32_t addi_11_11 = 0x396b0000;
2848 static const uint32_t addi_12_12 = 0x398c0000;
2849 static const uint32_t addi_2_2 = 0x38420000;
2850 static const uint32_t addi_3_2 = 0x38620000;
2851 static const uint32_t addi_3_3 = 0x38630000;
2852 static const uint32_t addis_0_2 = 0x3c020000;
2853 static const uint32_t addis_0_13 = 0x3c0d0000;
2854 static const uint32_t addis_11_11 = 0x3d6b0000;
2855 static const uint32_t addis_11_30 = 0x3d7e0000;
2856 static const uint32_t addis_12_12 = 0x3d8c0000;
2857 static const uint32_t addis_12_2 = 0x3d820000;
2858 static const uint32_t addis_3_2 = 0x3c620000;
2859 static const uint32_t addis_3_13 = 0x3c6d0000;
2860 static const uint32_t b = 0x48000000;
2861 static const uint32_t bcl_20_31 = 0x429f0005;
2862 static const uint32_t bctr = 0x4e800420;
2863 static const uint32_t blr = 0x4e800020;
2864 static const uint32_t blrl = 0x4e800021;
2865 static const uint32_t bnectr_p4 = 0x4ce20420;
2866 static const uint32_t cmpldi_2_0 = 0x28220000;
2867 static const uint32_t cror_15_15_15 = 0x4def7b82;
2868 static const uint32_t cror_31_31_31 = 0x4ffffb82;
2869 static const uint32_t ld_0_1 = 0xe8010000;
2870 static const uint32_t ld_0_12 = 0xe80c0000;
2871 static const uint32_t ld_11_12 = 0xe96c0000;
2872 static const uint32_t ld_11_2 = 0xe9620000;
2873 static const uint32_t ld_2_1 = 0xe8410000;
2874 static const uint32_t ld_2_11 = 0xe84b0000;
2875 static const uint32_t ld_2_12 = 0xe84c0000;
2876 static const uint32_t ld_2_2 = 0xe8420000;
2877 static const uint32_t lfd_0_1 = 0xc8010000;
2878 static const uint32_t li_0_0 = 0x38000000;
2879 static const uint32_t li_12_0 = 0x39800000;
2880 static const uint32_t lis_0_0 = 0x3c000000;
2881 static const uint32_t lis_11 = 0x3d600000;
2882 static const uint32_t lis_12 = 0x3d800000;
2883 static const uint32_t lwz_0_12 = 0x800c0000;
2884 static const uint32_t lwz_11_11 = 0x816b0000;
2885 static const uint32_t lwz_11_30 = 0x817e0000;
2886 static const uint32_t lwz_12_12 = 0x818c0000;
2887 static const uint32_t lwzu_0_12 = 0x840c0000;
2888 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
2889 static const uint32_t mflr_0 = 0x7c0802a6;
2890 static const uint32_t mflr_11 = 0x7d6802a6;
2891 static const uint32_t mflr_12 = 0x7d8802a6;
2892 static const uint32_t mtctr_0 = 0x7c0903a6;
2893 static const uint32_t mtctr_11 = 0x7d6903a6;
2894 static const uint32_t mtctr_12 = 0x7d8903a6;
2895 static const uint32_t mtlr_0 = 0x7c0803a6;
2896 static const uint32_t mtlr_12 = 0x7d8803a6;
2897 static const uint32_t nop = 0x60000000;
2898 static const uint32_t ori_0_0_0 = 0x60000000;
2899 static const uint32_t std_0_1 = 0xf8010000;
2900 static const uint32_t std_0_12 = 0xf80c0000;
2901 static const uint32_t std_2_1 = 0xf8410000;
2902 static const uint32_t stfd_0_1 = 0xd8010000;
2903 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
2904 static const uint32_t sub_11_11_12 = 0x7d6c5850;
2905 static const uint32_t xor_11_11_11 = 0x7d6b5a78;
2907 // Write out the PLT.
2909 template<int size, bool big_endian>
2911 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
2913 if (size == 32 && this->name_[3] != 'I')
2915 const section_size_type offset = this->offset();
2916 const section_size_type oview_size
2917 = convert_to_section_size_type(this->data_size());
2918 unsigned char* const oview = of->get_output_view(offset, oview_size);
2919 unsigned char* pov = oview;
2920 unsigned char* endpov = oview + oview_size;
2922 // The address of the .glink branch table
2923 const Output_data_glink<size, big_endian>* glink
2924 = this->targ_->glink_section();
2925 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
2927 while (pov < endpov)
2929 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
2934 of->write_output_view(offset, oview_size, oview);
2938 // Create the PLT section.
2940 template<int size, bool big_endian>
2942 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
2945 if (this->plt_ == NULL)
2947 if (this->got_ == NULL)
2948 this->got_section(symtab, layout);
2950 if (this->glink_ == NULL)
2951 make_glink_section(layout);
2953 // Ensure that .rela.dyn always appears before .rela.plt This is
2954 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2955 // needs to include .rela.plt in its range.
2956 this->rela_dyn_section(layout);
2958 Reloc_section* plt_rel = new Reloc_section(false);
2959 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
2960 elfcpp::SHF_ALLOC, plt_rel,
2961 ORDER_DYNAMIC_PLT_RELOCS, false);
2963 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
2964 size == 32 ? 0 : 24,
2966 layout->add_output_section_data(".plt",
2968 ? elfcpp::SHT_PROGBITS
2969 : elfcpp::SHT_NOBITS),
2970 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2979 // Create the IPLT section.
2981 template<int size, bool big_endian>
2983 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
2986 if (this->iplt_ == NULL)
2988 this->make_plt_section(symtab, layout);
2990 Reloc_section* iplt_rel = new Reloc_section(false);
2991 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
2993 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
2995 this->plt_->output_section()->add_output_section_data(this->iplt_);
2999 // A section for huge long branch addresses, similar to plt section.
3001 template<int size, bool big_endian>
3002 class Output_data_brlt_powerpc : public Output_section_data_build
3005 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3006 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3007 size, big_endian> Reloc_section;
3009 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3010 Reloc_section* brlt_rel)
3011 : Output_section_data_build(size == 32 ? 4 : 8),
3016 // Add a reloc for an entry in the BRLT.
3018 add_reloc(Address to, unsigned int off)
3019 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3021 // Update section and reloc section size.
3023 set_current_size(unsigned int num_branches)
3025 this->reset_address_and_file_offset();
3026 this->set_current_data_size(num_branches * 16);
3027 this->finalize_data_size();
3028 Output_section* os = this->output_section();
3029 os->set_section_offsets_need_adjustment();
3030 if (this->rel_ != NULL)
3032 unsigned int reloc_size
3033 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3034 this->rel_->reset_address_and_file_offset();
3035 this->rel_->set_current_data_size(num_branches * reloc_size);
3036 this->rel_->finalize_data_size();
3037 Output_section* os = this->rel_->output_section();
3038 os->set_section_offsets_need_adjustment();
3044 do_adjust_output_section(Output_section* os)
3049 // Write to a map file.
3051 do_print_to_mapfile(Mapfile* mapfile) const
3052 { mapfile->print_output_data(this, "** BRLT"); }
3055 // Write out the BRLT data.
3057 do_write(Output_file*);
3059 // The reloc section.
3060 Reloc_section* rel_;
3061 Target_powerpc<size, big_endian>* targ_;
3064 // Make the branch lookup table section.
3066 template<int size, bool big_endian>
3068 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3070 if (size == 64 && this->brlt_section_ == NULL)
3072 Reloc_section* brlt_rel = NULL;
3073 bool is_pic = parameters->options().output_is_position_independent();
3076 // When PIC we can't fill in .branch_lt (like .plt it can be
3077 // a bss style section) but must initialise at runtime via
3078 // dynamic relocats.
3079 this->rela_dyn_section(layout);
3080 brlt_rel = new Reloc_section(false);
3081 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3084 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3085 if (this->plt_ && is_pic)
3086 this->plt_->output_section()
3087 ->add_output_section_data(this->brlt_section_);
3089 layout->add_output_section_data(".branch_lt",
3090 (is_pic ? elfcpp::SHT_NOBITS
3091 : elfcpp::SHT_PROGBITS),
3092 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3093 this->brlt_section_,
3094 (is_pic ? ORDER_SMALL_BSS
3095 : ORDER_SMALL_DATA),
3100 // Write out .branch_lt when non-PIC.
3102 template<int size, bool big_endian>
3104 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3106 if (size == 64 && !parameters->options().output_is_position_independent())
3108 const section_size_type offset = this->offset();
3109 const section_size_type oview_size
3110 = convert_to_section_size_type(this->data_size());
3111 unsigned char* const oview = of->get_output_view(offset, oview_size);
3113 this->targ_->write_branch_lookup_table(oview);
3114 of->write_output_view(offset, oview_size, oview);
3118 static inline uint32_t
3124 static inline uint32_t
3130 static inline uint32_t
3133 return hi(a + 0x8000);
3139 static const unsigned char eh_frame_cie[12];
3143 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3146 'z', 'R', 0, // Augmentation string.
3147 4, // Code alignment.
3148 0x80 - size / 8 , // Data alignment.
3150 1, // Augmentation size.
3151 (elfcpp::DW_EH_PE_pcrel
3152 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3153 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3156 // Describe __glink_PLTresolve use of LR, 64-bit version.
3157 static const unsigned char glink_eh_frame_fde_64[] =
3159 0, 0, 0, 0, // Replaced with offset to .glink.
3160 0, 0, 0, 0, // Replaced with size of .glink.
3161 0, // Augmentation size.
3162 elfcpp::DW_CFA_advance_loc + 1,
3163 elfcpp::DW_CFA_register, 65, 12,
3164 elfcpp::DW_CFA_advance_loc + 4,
3165 elfcpp::DW_CFA_restore_extended, 65
3168 // Describe __glink_PLTresolve use of LR, 32-bit version.
3169 static const unsigned char glink_eh_frame_fde_32[] =
3171 0, 0, 0, 0, // Replaced with offset to .glink.
3172 0, 0, 0, 0, // Replaced with size of .glink.
3173 0, // Augmentation size.
3174 elfcpp::DW_CFA_advance_loc + 2,
3175 elfcpp::DW_CFA_register, 65, 0,
3176 elfcpp::DW_CFA_advance_loc + 4,
3177 elfcpp::DW_CFA_restore_extended, 65
3180 static const unsigned char default_fde[] =
3182 0, 0, 0, 0, // Replaced with offset to stubs.
3183 0, 0, 0, 0, // Replaced with size of stubs.
3184 0, // Augmentation size.
3185 elfcpp::DW_CFA_nop, // Pad.
3190 template<bool big_endian>
3192 write_insn(unsigned char* p, uint32_t v)
3194 elfcpp::Swap<32, big_endian>::writeval(p, v);
3197 // Stub_table holds information about plt and long branch stubs.
3198 // Stubs are built in an area following some input section determined
3199 // by group_sections(). This input section is converted to a relaxed
3200 // input section allowing it to be resized to accommodate the stubs
3202 template<int size, bool big_endian>
3203 class Stub_table : public Output_relaxed_input_section
3206 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3207 static const Address invalid_address = static_cast<Address>(0) - 1;
3209 Stub_table(Target_powerpc<size, big_endian>* targ)
3210 : Output_relaxed_input_section(NULL, 0, 0),
3211 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3212 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3213 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3216 // Delayed Output_relaxed_input_section init.
3218 init(const Output_section::Input_section*, Output_section*);
3220 // Add a plt call stub.
3222 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3228 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3233 // Find a given plt call stub.
3235 find_plt_call_entry(const Symbol*) const;
3238 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3239 unsigned int) const;
3242 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3248 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3253 // Add a long branch stub.
3255 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3258 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3264 this->plt_call_stubs_.clear();
3265 this->plt_size_ = 0;
3266 this->long_branch_stubs_.clear();
3267 this->branch_size_ = 0;
3271 set_address_and_size(const Output_section* os, Address off)
3273 Address start_off = off;
3274 off += this->orig_data_size_;
3275 Address my_size = this->plt_size_ + this->branch_size_;
3277 off = align_address(off, this->stub_align());
3278 // Include original section size and alignment padding in size
3279 my_size += off - start_off;
3280 this->reset_address_and_file_offset();
3281 this->set_current_data_size(my_size);
3282 this->set_address_and_file_offset(os->address() + start_off,
3283 os->offset() + start_off);
3288 stub_address() const
3290 return align_address(this->address() + this->orig_data_size_,
3291 this->stub_align());
3297 return align_address(this->offset() + this->orig_data_size_,
3298 this->stub_align());
3303 { return this->plt_size_; }
3308 Output_section* os = this->output_section();
3309 if (os->addralign() < this->stub_align())
3311 os->set_addralign(this->stub_align());
3312 // FIXME: get rid of the insane checkpointing.
3313 // We can't increase alignment of the input section to which
3314 // stubs are attached; The input section may be .init which
3315 // is pasted together with other .init sections to form a
3316 // function. Aligning might insert zero padding resulting in
3317 // sigill. However we do need to increase alignment of the
3318 // output section so that the align_address() on offset in
3319 // set_address_and_size() adds the same padding as the
3320 // align_address() on address in stub_address().
3321 // What's more, we need this alignment for the layout done in
3322 // relaxation_loop_body() so that the output section starts at
3323 // a suitably aligned address.
3324 os->checkpoint_set_addralign(this->stub_align());
3326 if (this->last_plt_size_ != this->plt_size_
3327 || this->last_branch_size_ != this->branch_size_)
3329 this->last_plt_size_ = this->plt_size_;
3330 this->last_branch_size_ = this->branch_size_;
3336 // Add .eh_frame info for this stub section. Unlike other linker
3337 // generated .eh_frame this is added late in the link, because we
3338 // only want the .eh_frame info if this particular stub section is
3341 add_eh_frame(Layout* layout)
3343 if (!this->eh_frame_added_)
3345 if (!parameters->options().ld_generated_unwind_info())
3348 // Since we add stub .eh_frame info late, it must be placed
3349 // after all other linker generated .eh_frame info so that
3350 // merge mapping need not be updated for input sections.
3351 // There is no provision to use a different CIE to that used
3353 if (!this->targ_->has_glink())
3356 layout->add_eh_frame_for_plt(this,
3357 Eh_cie<size>::eh_frame_cie,
3358 sizeof (Eh_cie<size>::eh_frame_cie),
3360 sizeof (default_fde));
3361 this->eh_frame_added_ = true;
3365 Target_powerpc<size, big_endian>*
3371 class Plt_stub_ent_hash;
3372 typedef Unordered_map<Plt_stub_ent, unsigned int,
3373 Plt_stub_ent_hash> Plt_stub_entries;
3375 // Alignment of stub section.
3381 unsigned int min_align = 32;
3382 unsigned int user_align = 1 << parameters->options().plt_align();
3383 return std::max(user_align, min_align);
3386 // Return the plt offset for the given call stub.
3388 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3390 const Symbol* gsym = p->first.sym_;
3393 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3394 && gsym->can_use_relative_reloc(false));
3395 return gsym->plt_offset();
3400 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3401 unsigned int local_sym_index = p->first.locsym_;
3402 return relobj->local_plt_offset(local_sym_index);
3406 // Size of a given plt call stub.
3408 plt_call_size(typename Plt_stub_entries::const_iterator p) const
3414 Address plt_addr = this->plt_off(p, &is_iplt);
3416 plt_addr += this->targ_->iplt_section()->address();
3418 plt_addr += this->targ_->plt_section()->address();
3419 Address got_addr = this->targ_->got_section()->output_section()->address();
3420 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3421 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3422 got_addr += ppcobj->toc_base_offset();
3423 Address off = plt_addr - got_addr;
3424 bool static_chain = parameters->options().plt_static_chain();
3425 bool thread_safe = this->targ_->plt_thread_safe();
3426 unsigned int bytes = (4 * 5
3429 + 4 * (ha(off) != 0)
3430 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3431 unsigned int align = 1 << parameters->options().plt_align();
3433 bytes = (bytes + align - 1) & -align;
3437 // Return long branch stub size.
3439 branch_stub_size(Address to)
3442 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3443 if (to - loc + (1 << 25) < 2 << 25)
3445 if (size == 64 || !parameters->options().output_is_position_independent())
3452 do_write(Output_file*);
3454 // Plt call stub keys.
3458 Plt_stub_ent(const Symbol* sym)
3459 : sym_(sym), object_(0), addend_(0), locsym_(0)
3462 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3463 unsigned int locsym_index)
3464 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3467 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3469 unsigned int r_type,
3471 : sym_(sym), object_(0), addend_(0), locsym_(0)
3474 this->addend_ = addend;
3475 else if (parameters->options().output_is_position_independent()
3476 && r_type == elfcpp::R_PPC_PLTREL24)
3478 this->addend_ = addend;
3479 if (this->addend_ >= 32768)
3480 this->object_ = object;
3484 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3485 unsigned int locsym_index,
3486 unsigned int r_type,
3488 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3491 this->addend_ = addend;
3492 else if (parameters->options().output_is_position_independent()
3493 && r_type == elfcpp::R_PPC_PLTREL24)
3494 this->addend_ = addend;
3497 bool operator==(const Plt_stub_ent& that) const
3499 return (this->sym_ == that.sym_
3500 && this->object_ == that.object_
3501 && this->addend_ == that.addend_
3502 && this->locsym_ == that.locsym_);
3506 const Sized_relobj_file<size, big_endian>* object_;
3507 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3508 unsigned int locsym_;
3511 class Plt_stub_ent_hash
3514 size_t operator()(const Plt_stub_ent& ent) const
3516 return (reinterpret_cast<uintptr_t>(ent.sym_)
3517 ^ reinterpret_cast<uintptr_t>(ent.object_)
3523 // Long branch stub keys.
3524 class Branch_stub_ent
3527 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3528 : dest_(to), toc_base_off_(0)
3531 toc_base_off_ = obj->toc_base_offset();
3534 bool operator==(const Branch_stub_ent& that) const
3536 return (this->dest_ == that.dest_
3538 || this->toc_base_off_ == that.toc_base_off_));
3542 unsigned int toc_base_off_;
3545 class Branch_stub_ent_hash
3548 size_t operator()(const Branch_stub_ent& ent) const
3549 { return ent.dest_ ^ ent.toc_base_off_; }
3552 // In a sane world this would be a global.
3553 Target_powerpc<size, big_endian>* targ_;
3554 // Map sym/object/addend to stub offset.
3555 Plt_stub_entries plt_call_stubs_;
3556 // Map destination address to stub offset.
3557 typedef Unordered_map<Branch_stub_ent, unsigned int,
3558 Branch_stub_ent_hash> Branch_stub_entries;
3559 Branch_stub_entries long_branch_stubs_;
3560 // size of input section
3561 section_size_type orig_data_size_;
3563 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3564 // Whether .eh_frame info has been created for this stub section.
3565 bool eh_frame_added_;
3568 // Make a new stub table, and record.
3570 template<int size, bool big_endian>
3571 Stub_table<size, big_endian>*
3572 Target_powerpc<size, big_endian>::new_stub_table()
3574 Stub_table<size, big_endian>* stub_table
3575 = new Stub_table<size, big_endian>(this);
3576 this->stub_tables_.push_back(stub_table);
3580 // Delayed stub table initialisation, because we create the stub table
3581 // before we know to which section it will be attached.
3583 template<int size, bool big_endian>
3585 Stub_table<size, big_endian>::init(
3586 const Output_section::Input_section* owner,
3587 Output_section* output_section)
3589 this->set_relobj(owner->relobj());
3590 this->set_shndx(owner->shndx());
3591 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3592 this->set_output_section(output_section);
3593 this->orig_data_size_ = owner->current_data_size();
3595 std::vector<Output_relaxed_input_section*> new_relaxed;
3596 new_relaxed.push_back(this);
3597 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3600 // Add a plt call stub, if we do not already have one for this
3601 // sym/object/addend combo.
3603 template<int size, bool big_endian>
3605 Stub_table<size, big_endian>::add_plt_call_entry(
3606 const Sized_relobj_file<size, big_endian>* object,
3608 unsigned int r_type,
3611 Plt_stub_ent ent(object, gsym, r_type, addend);
3612 Address off = this->plt_size_;
3613 std::pair<typename Plt_stub_entries::iterator, bool> p
3614 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3616 this->plt_size_ = off + this->plt_call_size(p.first);
3619 template<int size, bool big_endian>
3621 Stub_table<size, big_endian>::add_plt_call_entry(
3622 const Sized_relobj_file<size, big_endian>* object,
3623 unsigned int locsym_index,
3624 unsigned int r_type,
3627 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3628 Address off = this->plt_size_;
3629 std::pair<typename Plt_stub_entries::iterator, bool> p
3630 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3632 this->plt_size_ = off + this->plt_call_size(p.first);
3635 // Find a plt call stub.
3637 template<int size, bool big_endian>
3638 typename Stub_table<size, big_endian>::Address
3639 Stub_table<size, big_endian>::find_plt_call_entry(
3640 const Sized_relobj_file<size, big_endian>* object,
3642 unsigned int r_type,
3643 Address addend) const
3645 Plt_stub_ent ent(object, gsym, r_type, addend);
3646 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3647 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3650 template<int size, bool big_endian>
3651 typename Stub_table<size, big_endian>::Address
3652 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3654 Plt_stub_ent ent(gsym);
3655 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3656 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3659 template<int size, bool big_endian>
3660 typename Stub_table<size, big_endian>::Address
3661 Stub_table<size, big_endian>::find_plt_call_entry(
3662 const Sized_relobj_file<size, big_endian>* object,
3663 unsigned int locsym_index,
3664 unsigned int r_type,
3665 Address addend) const
3667 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3668 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3669 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3672 template<int size, bool big_endian>
3673 typename Stub_table<size, big_endian>::Address
3674 Stub_table<size, big_endian>::find_plt_call_entry(
3675 const Sized_relobj_file<size, big_endian>* object,
3676 unsigned int locsym_index) const
3678 Plt_stub_ent ent(object, locsym_index);
3679 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3680 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3683 // Add a long branch stub if we don't already have one to given
3686 template<int size, bool big_endian>
3688 Stub_table<size, big_endian>::add_long_branch_entry(
3689 const Powerpc_relobj<size, big_endian>* object,
3692 Branch_stub_ent ent(object, to);
3693 Address off = this->branch_size_;
3694 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3696 unsigned int stub_size = this->branch_stub_size(to);
3697 this->branch_size_ = off + stub_size;
3698 if (size == 64 && stub_size != 4)
3699 this->targ_->add_branch_lookup_table(to);
3703 // Find long branch stub.
3705 template<int size, bool big_endian>
3706 typename Stub_table<size, big_endian>::Address
3707 Stub_table<size, big_endian>::find_long_branch_entry(
3708 const Powerpc_relobj<size, big_endian>* object,
3711 Branch_stub_ent ent(object, to);
3712 typename Branch_stub_entries::const_iterator p
3713 = this->long_branch_stubs_.find(ent);
3714 return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3717 // A class to handle .glink.
3719 template<int size, bool big_endian>
3720 class Output_data_glink : public Output_section_data
3723 static const int pltresolve_size = 16*4;
3725 Output_data_glink(Target_powerpc<size, big_endian>* targ)
3726 : Output_section_data(16), targ_(targ)
3730 add_eh_frame(Layout* layout)
3732 if (!parameters->options().ld_generated_unwind_info())
3736 layout->add_eh_frame_for_plt(this,
3737 Eh_cie<64>::eh_frame_cie,
3738 sizeof (Eh_cie<64>::eh_frame_cie),
3739 glink_eh_frame_fde_64,
3740 sizeof (glink_eh_frame_fde_64));
3743 // 32-bit .glink can use the default since the CIE return
3744 // address reg, LR, is valid.
3745 layout->add_eh_frame_for_plt(this,
3746 Eh_cie<32>::eh_frame_cie,
3747 sizeof (Eh_cie<32>::eh_frame_cie),
3749 sizeof (default_fde));
3750 // Except where LR is used in a PIC __glink_PLTresolve.
3751 if (parameters->options().output_is_position_independent())
3752 layout->add_eh_frame_for_plt(this,
3753 Eh_cie<32>::eh_frame_cie,
3754 sizeof (Eh_cie<32>::eh_frame_cie),
3755 glink_eh_frame_fde_32,
3756 sizeof (glink_eh_frame_fde_32));
3761 // Write to a map file.
3763 do_print_to_mapfile(Mapfile* mapfile) const
3764 { mapfile->print_output_data(this, _("** glink")); }
3768 set_final_data_size();
3772 do_write(Output_file*);
3774 // Allows access to .got and .plt for do_write.
3775 Target_powerpc<size, big_endian>* targ_;
3778 template<int size, bool big_endian>
3780 Output_data_glink<size, big_endian>::set_final_data_size()
3782 unsigned int count = this->targ_->plt_entry_count();
3783 section_size_type total = 0;
3789 // space for branch table
3790 total += 4 * (count - 1);
3792 total += -total & 15;
3793 total += this->pltresolve_size;
3797 total += this->pltresolve_size;
3799 // space for branch table
3802 total += 4 * (count - 0x8000);
3806 this->set_data_size(total);
3809 // Write out plt and long branch stub code.
3811 template<int size, bool big_endian>
3813 Stub_table<size, big_endian>::do_write(Output_file* of)
3815 if (this->plt_call_stubs_.empty()
3816 && this->long_branch_stubs_.empty())
3819 const section_size_type start_off = this->offset();
3820 const section_size_type off = this->stub_offset();
3821 const section_size_type oview_size =
3822 convert_to_section_size_type(this->data_size() - (off - start_off));
3823 unsigned char* const oview = of->get_output_view(off, oview_size);
3828 const Output_data_got_powerpc<size, big_endian>* got
3829 = this->targ_->got_section();
3830 Address got_os_addr = got->output_section()->address();
3832 if (!this->plt_call_stubs_.empty())
3834 // The base address of the .plt section.
3835 Address plt_base = this->targ_->plt_section()->address();
3836 Address iplt_base = invalid_address;
3838 // Write out plt call stubs.
3839 typename Plt_stub_entries::const_iterator cs;
3840 for (cs = this->plt_call_stubs_.begin();
3841 cs != this->plt_call_stubs_.end();
3845 Address pltoff = this->plt_off(cs, &is_iplt);
3846 Address plt_addr = pltoff;
3849 if (iplt_base == invalid_address)
3850 iplt_base = this->targ_->iplt_section()->address();
3851 plt_addr += iplt_base;
3854 plt_addr += plt_base;
3855 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3856 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
3857 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
3858 Address off = plt_addr - got_addr;
3860 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
3861 gold_error(_("%s: linkage table error against `%s'"),
3862 cs->first.object_->name().c_str(),
3863 cs->first.sym_->demangled_name().c_str());
3865 bool static_chain = parameters->options().plt_static_chain();
3866 bool thread_safe = this->targ_->plt_thread_safe();
3867 bool use_fake_dep = false;
3868 Address cmp_branch_off = 0;
3871 unsigned int pltindex
3872 = ((pltoff - this->targ_->first_plt_entry_offset())
3873 / this->targ_->plt_entry_size());
3875 = (this->targ_->glink_section()->pltresolve_size
3877 if (pltindex > 32768)
3878 glinkoff += (pltindex - 32768) * 4;
3880 = this->targ_->glink_section()->address() + glinkoff;
3882 = (this->stub_address() + cs->second + 24
3883 + 4 * (ha(off) != 0)
3884 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
3885 + 4 * static_chain);
3886 cmp_branch_off = to - from;
3887 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
3890 p = oview + cs->second;
3893 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
3894 write_insn<big_endian>(p, addis_12_2 + ha(off)), p += 4;
3895 write_insn<big_endian>(p, ld_11_12 + l(off)), p += 4;
3896 if (ha(off + 8 + 8 * static_chain) != ha(off))
3898 write_insn<big_endian>(p, addi_12_12 + l(off)), p += 4;
3901 write_insn<big_endian>(p, mtctr_11), p += 4;
3904 write_insn<big_endian>(p, xor_11_11_11), p += 4;
3905 write_insn<big_endian>(p, add_12_12_11), p += 4;
3907 write_insn<big_endian>(p, ld_2_12 + l(off + 8)), p += 4;
3909 write_insn<big_endian>(p, ld_11_12 + l(off + 16)), p += 4;
3913 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
3914 write_insn<big_endian>(p, ld_11_2 + l(off)), p += 4;
3915 if (ha(off + 8 + 8 * static_chain) != ha(off))
3917 write_insn<big_endian>(p, addi_2_2 + l(off)), p += 4;
3920 write_insn<big_endian>(p, mtctr_11), p += 4;
3923 write_insn<big_endian>(p, xor_11_11_11), p += 4;
3924 write_insn<big_endian>(p, add_2_2_11), p += 4;
3927 write_insn<big_endian>(p, ld_11_2 + l(off + 16)), p += 4;
3928 write_insn<big_endian>(p, ld_2_2 + l(off + 8)), p += 4;
3930 if (thread_safe && !use_fake_dep)
3932 write_insn<big_endian>(p, cmpldi_2_0), p += 4;
3933 write_insn<big_endian>(p, bnectr_p4), p += 4;
3934 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
3937 write_insn<big_endian>(p, bctr);
3941 // Write out long branch stubs.
3942 typename Branch_stub_entries::const_iterator bs;
3943 for (bs = this->long_branch_stubs_.begin();
3944 bs != this->long_branch_stubs_.end();
3947 p = oview + this->plt_size_ + bs->second;
3948 Address loc = this->stub_address() + this->plt_size_ + bs->second;
3949 Address delta = bs->first.dest_ - loc;
3950 if (delta + (1 << 25) < 2 << 25)
3951 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3955 = this->targ_->find_branch_lookup_table(bs->first.dest_);
3956 gold_assert(brlt_addr != invalid_address);
3957 brlt_addr += this->targ_->brlt_section()->address();
3958 Address got_addr = got_os_addr + bs->first.toc_base_off_;
3959 Address brltoff = brlt_addr - got_addr;
3960 if (ha(brltoff) == 0)
3962 write_insn<big_endian>(p, ld_11_2 + l(brltoff)), p += 4;
3966 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
3967 write_insn<big_endian>(p, ld_11_12 + l(brltoff)), p += 4;
3969 write_insn<big_endian>(p, mtctr_11), p += 4;
3970 write_insn<big_endian>(p, bctr);
3976 if (!this->plt_call_stubs_.empty())
3978 // The base address of the .plt section.
3979 Address plt_base = this->targ_->plt_section()->address();
3980 Address iplt_base = invalid_address;
3981 // The address of _GLOBAL_OFFSET_TABLE_.
3982 Address g_o_t = invalid_address;
3984 // Write out plt call stubs.
3985 typename Plt_stub_entries::const_iterator cs;
3986 for (cs = this->plt_call_stubs_.begin();
3987 cs != this->plt_call_stubs_.end();
3991 Address plt_addr = this->plt_off(cs, &is_iplt);
3994 if (iplt_base == invalid_address)
3995 iplt_base = this->targ_->iplt_section()->address();
3996 plt_addr += iplt_base;
3999 plt_addr += plt_base;
4001 p = oview + cs->second;
4002 if (parameters->options().output_is_position_independent())
4005 const Powerpc_relobj<size, big_endian>* ppcobj
4006 = (static_cast<const Powerpc_relobj<size, big_endian>*>
4007 (cs->first.object_));
4008 if (ppcobj != NULL && cs->first.addend_ >= 32768)
4010 unsigned int got2 = ppcobj->got2_shndx();
4011 got_addr = ppcobj->get_output_section_offset(got2);
4012 gold_assert(got_addr != invalid_address);
4013 got_addr += (ppcobj->output_section(got2)->address()
4014 + cs->first.addend_);
4018 if (g_o_t == invalid_address)
4020 const Output_data_got_powerpc<size, big_endian>* got
4021 = this->targ_->got_section();
4022 g_o_t = got->address() + got->g_o_t();
4027 Address off = plt_addr - got_addr;
4030 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
4031 write_insn<big_endian>(p + 4, mtctr_11);
4032 write_insn<big_endian>(p + 8, bctr);
4036 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
4037 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
4038 write_insn<big_endian>(p + 8, mtctr_11);
4039 write_insn<big_endian>(p + 12, bctr);
4044 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
4045 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
4046 write_insn<big_endian>(p + 8, mtctr_11);
4047 write_insn<big_endian>(p + 12, bctr);
4052 // Write out long branch stubs.
4053 typename Branch_stub_entries::const_iterator bs;
4054 for (bs = this->long_branch_stubs_.begin();
4055 bs != this->long_branch_stubs_.end();
4058 p = oview + this->plt_size_ + bs->second;
4059 Address loc = this->stub_address() + this->plt_size_ + bs->second;
4060 Address delta = bs->first.dest_ - loc;
4061 if (delta + (1 << 25) < 2 << 25)
4062 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4063 else if (!parameters->options().output_is_position_independent())
4065 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
4066 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
4067 write_insn<big_endian>(p + 8, mtctr_12);
4068 write_insn<big_endian>(p + 12, bctr);
4073 write_insn<big_endian>(p + 0, mflr_0);
4074 write_insn<big_endian>(p + 4, bcl_20_31);
4075 write_insn<big_endian>(p + 8, mflr_12);
4076 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4077 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4078 write_insn<big_endian>(p + 20, mtlr_0);
4079 write_insn<big_endian>(p + 24, mtctr_12);
4080 write_insn<big_endian>(p + 28, bctr);
4086 // Write out .glink.
4088 template<int size, bool big_endian>
4090 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4092 const section_size_type off = this->offset();
4093 const section_size_type oview_size =
4094 convert_to_section_size_type(this->data_size());
4095 unsigned char* const oview = of->get_output_view(off, oview_size);
4098 // The base address of the .plt section.
4099 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4100 Address plt_base = this->targ_->plt_section()->address();
4104 // Write pltresolve stub.
4106 Address after_bcl = this->address() + 16;
4107 Address pltoff = plt_base - after_bcl;
4109 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
4111 write_insn<big_endian>(p, mflr_12), p += 4;
4112 write_insn<big_endian>(p, bcl_20_31), p += 4;
4113 write_insn<big_endian>(p, mflr_11), p += 4;
4114 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4115 write_insn<big_endian>(p, mtlr_12), p += 4;
4116 write_insn<big_endian>(p, add_12_2_11), p += 4;
4117 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
4118 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
4119 write_insn<big_endian>(p, mtctr_11), p += 4;
4120 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
4121 write_insn<big_endian>(p, bctr), p += 4;
4122 while (p < oview + this->pltresolve_size)
4123 write_insn<big_endian>(p, nop), p += 4;
4125 // Write lazy link call stubs.
4127 while (p < oview + oview_size)
4131 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4135 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
4136 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4138 uint32_t branch_off = 8 - (p - oview);
4139 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4145 const Output_data_got_powerpc<size, big_endian>* got
4146 = this->targ_->got_section();
4147 // The address of _GLOBAL_OFFSET_TABLE_.
4148 Address g_o_t = got->address() + got->g_o_t();
4150 // Write out pltresolve branch table.
4152 unsigned int the_end = oview_size - this->pltresolve_size;
4153 unsigned char* end_p = oview + the_end;
4154 while (p < end_p - 8 * 4)
4155 write_insn<big_endian>(p, b + end_p - p), p += 4;
4157 write_insn<big_endian>(p, nop), p += 4;
4159 // Write out pltresolve call stub.
4160 if (parameters->options().output_is_position_independent())
4162 Address res0_off = 0;
4163 Address after_bcl_off = the_end + 12;
4164 Address bcl_res0 = after_bcl_off - res0_off;
4166 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
4167 write_insn<big_endian>(p + 4, mflr_0);
4168 write_insn<big_endian>(p + 8, bcl_20_31);
4169 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4170 write_insn<big_endian>(p + 16, mflr_12);
4171 write_insn<big_endian>(p + 20, mtlr_0);
4172 write_insn<big_endian>(p + 24, sub_11_11_12);
4174 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4176 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4177 if (ha(got_bcl) == ha(got_bcl + 4))
4179 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4180 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4184 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4185 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4187 write_insn<big_endian>(p + 40, mtctr_0);
4188 write_insn<big_endian>(p + 44, add_0_11_11);
4189 write_insn<big_endian>(p + 48, add_11_0_11);
4190 write_insn<big_endian>(p + 52, bctr);
4191 write_insn<big_endian>(p + 56, nop);
4192 write_insn<big_endian>(p + 60, nop);
4196 Address res0 = this->address();
4198 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4199 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4200 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4201 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4203 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4204 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4205 write_insn<big_endian>(p + 16, mtctr_0);
4206 write_insn<big_endian>(p + 20, add_0_11_11);
4207 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4208 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4210 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4211 write_insn<big_endian>(p + 28, add_11_0_11);
4212 write_insn<big_endian>(p + 32, bctr);
4213 write_insn<big_endian>(p + 36, nop);
4214 write_insn<big_endian>(p + 40, nop);
4215 write_insn<big_endian>(p + 44, nop);
4216 write_insn<big_endian>(p + 48, nop);
4217 write_insn<big_endian>(p + 52, nop);
4218 write_insn<big_endian>(p + 56, nop);
4219 write_insn<big_endian>(p + 60, nop);
4224 of->write_output_view(off, oview_size, oview);
4228 // A class to handle linker generated save/restore functions.
4230 template<int size, bool big_endian>
4231 class Output_data_save_res : public Output_section_data_build
4234 Output_data_save_res(Symbol_table* symtab);
4237 // Write to a map file.
4239 do_print_to_mapfile(Mapfile* mapfile) const
4240 { mapfile->print_output_data(this, _("** save/restore")); }
4243 do_write(Output_file*);
4246 // The maximum size of save/restore contents.
4247 static const unsigned int savres_max = 218*4;
4250 savres_define(Symbol_table* symtab,
4252 unsigned int lo, unsigned int hi,
4253 unsigned char* write_ent(unsigned char*, int),
4254 unsigned char* write_tail(unsigned char*, int));
4256 unsigned char *contents_;
4259 template<bool big_endian>
4260 static unsigned char*
4261 savegpr0(unsigned char* p, int r)
4263 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4264 write_insn<big_endian>(p, insn);
4268 template<bool big_endian>
4269 static unsigned char*
4270 savegpr0_tail(unsigned char* p, int r)
4272 p = savegpr0<big_endian>(p, r);
4273 uint32_t insn = std_0_1 + 16;
4274 write_insn<big_endian>(p, insn);
4276 write_insn<big_endian>(p, blr);
4280 template<bool big_endian>
4281 static unsigned char*
4282 restgpr0(unsigned char* p, int r)
4284 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4285 write_insn<big_endian>(p, insn);
4289 template<bool big_endian>
4290 static unsigned char*
4291 restgpr0_tail(unsigned char* p, int r)
4293 uint32_t insn = ld_0_1 + 16;
4294 write_insn<big_endian>(p, insn);
4296 p = restgpr0<big_endian>(p, r);
4297 write_insn<big_endian>(p, mtlr_0);
4301 p = restgpr0<big_endian>(p, 30);
4302 p = restgpr0<big_endian>(p, 31);
4304 write_insn<big_endian>(p, blr);
4308 template<bool big_endian>
4309 static unsigned char*
4310 savegpr1(unsigned char* p, int r)
4312 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4313 write_insn<big_endian>(p, insn);
4317 template<bool big_endian>
4318 static unsigned char*
4319 savegpr1_tail(unsigned char* p, int r)
4321 p = savegpr1<big_endian>(p, r);
4322 write_insn<big_endian>(p, blr);
4326 template<bool big_endian>
4327 static unsigned char*
4328 restgpr1(unsigned char* p, int r)
4330 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4331 write_insn<big_endian>(p, insn);
4335 template<bool big_endian>
4336 static unsigned char*
4337 restgpr1_tail(unsigned char* p, int r)
4339 p = restgpr1<big_endian>(p, r);
4340 write_insn<big_endian>(p, blr);
4344 template<bool big_endian>
4345 static unsigned char*
4346 savefpr(unsigned char* p, int r)
4348 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4349 write_insn<big_endian>(p, insn);
4353 template<bool big_endian>
4354 static unsigned char*
4355 savefpr0_tail(unsigned char* p, int r)
4357 p = savefpr<big_endian>(p, r);
4358 write_insn<big_endian>(p, std_0_1 + 16);
4360 write_insn<big_endian>(p, blr);
4364 template<bool big_endian>
4365 static unsigned char*
4366 restfpr(unsigned char* p, int r)
4368 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4369 write_insn<big_endian>(p, insn);
4373 template<bool big_endian>
4374 static unsigned char*
4375 restfpr0_tail(unsigned char* p, int r)
4377 write_insn<big_endian>(p, ld_0_1 + 16);
4379 p = restfpr<big_endian>(p, r);
4380 write_insn<big_endian>(p, mtlr_0);
4384 p = restfpr<big_endian>(p, 30);
4385 p = restfpr<big_endian>(p, 31);
4387 write_insn<big_endian>(p, blr);
4391 template<bool big_endian>
4392 static unsigned char*
4393 savefpr1_tail(unsigned char* p, int r)
4395 p = savefpr<big_endian>(p, r);
4396 write_insn<big_endian>(p, blr);
4400 template<bool big_endian>
4401 static unsigned char*
4402 restfpr1_tail(unsigned char* p, int r)
4404 p = restfpr<big_endian>(p, r);
4405 write_insn<big_endian>(p, blr);
4409 template<bool big_endian>
4410 static unsigned char*
4411 savevr(unsigned char* p, int r)
4413 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4414 write_insn<big_endian>(p, insn);
4416 insn = stvx_0_12_0 + (r << 21);
4417 write_insn<big_endian>(p, insn);
4421 template<bool big_endian>
4422 static unsigned char*
4423 savevr_tail(unsigned char* p, int r)
4425 p = savevr<big_endian>(p, r);
4426 write_insn<big_endian>(p, blr);
4430 template<bool big_endian>
4431 static unsigned char*
4432 restvr(unsigned char* p, int r)
4434 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4435 write_insn<big_endian>(p, insn);
4437 insn = lvx_0_12_0 + (r << 21);
4438 write_insn<big_endian>(p, insn);
4442 template<bool big_endian>
4443 static unsigned char*
4444 restvr_tail(unsigned char* p, int r)
4446 p = restvr<big_endian>(p, r);
4447 write_insn<big_endian>(p, blr);
4452 template<int size, bool big_endian>
4453 Output_data_save_res<size, big_endian>::Output_data_save_res(
4454 Symbol_table* symtab)
4455 : Output_section_data_build(4),
4458 this->savres_define(symtab,
4459 "_savegpr0_", 14, 31,
4460 savegpr0<big_endian>, savegpr0_tail<big_endian>);
4461 this->savres_define(symtab,
4462 "_restgpr0_", 14, 29,
4463 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4464 this->savres_define(symtab,
4465 "_restgpr0_", 30, 31,
4466 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4467 this->savres_define(symtab,
4468 "_savegpr1_", 14, 31,
4469 savegpr1<big_endian>, savegpr1_tail<big_endian>);
4470 this->savres_define(symtab,
4471 "_restgpr1_", 14, 31,
4472 restgpr1<big_endian>, restgpr1_tail<big_endian>);
4473 this->savres_define(symtab,
4474 "_savefpr_", 14, 31,
4475 savefpr<big_endian>, savefpr0_tail<big_endian>);
4476 this->savres_define(symtab,
4477 "_restfpr_", 14, 29,
4478 restfpr<big_endian>, restfpr0_tail<big_endian>);
4479 this->savres_define(symtab,
4480 "_restfpr_", 30, 31,
4481 restfpr<big_endian>, restfpr0_tail<big_endian>);
4482 this->savres_define(symtab,
4484 savefpr<big_endian>, savefpr1_tail<big_endian>);
4485 this->savres_define(symtab,
4487 restfpr<big_endian>, restfpr1_tail<big_endian>);
4488 this->savres_define(symtab,
4490 savevr<big_endian>, savevr_tail<big_endian>);
4491 this->savres_define(symtab,
4493 restvr<big_endian>, restvr_tail<big_endian>);
4496 template<int size, bool big_endian>
4498 Output_data_save_res<size, big_endian>::savres_define(
4499 Symbol_table* symtab,
4501 unsigned int lo, unsigned int hi,
4502 unsigned char* write_ent(unsigned char*, int),
4503 unsigned char* write_tail(unsigned char*, int))
4505 size_t len = strlen(name);
4506 bool writing = false;
4509 memcpy(sym, name, len);
4512 for (unsigned int i = lo; i <= hi; i++)
4514 sym[len + 0] = i / 10 + '0';
4515 sym[len + 1] = i % 10 + '0';
4516 Symbol* gsym = symtab->lookup(sym);
4517 bool refd = gsym != NULL && gsym->is_undefined();
4518 writing = writing || refd;
4521 if (this->contents_ == NULL)
4522 this->contents_ = new unsigned char[this->savres_max];
4524 section_size_type value = this->current_data_size();
4525 unsigned char* p = this->contents_ + value;
4527 p = write_ent(p, i);
4529 p = write_tail(p, i);
4530 section_size_type cur_size = p - this->contents_;
4531 this->set_current_data_size(cur_size);
4533 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4534 this, value, cur_size - value,
4535 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4536 elfcpp::STV_HIDDEN, 0, false, false);
4541 // Write out save/restore.
4543 template<int size, bool big_endian>
4545 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4547 const section_size_type off = this->offset();
4548 const section_size_type oview_size =
4549 convert_to_section_size_type(this->data_size());
4550 unsigned char* const oview = of->get_output_view(off, oview_size);
4551 memcpy(oview, this->contents_, oview_size);
4552 of->write_output_view(off, oview_size, oview);
4556 // Create the glink section.
4558 template<int size, bool big_endian>
4560 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4562 if (this->glink_ == NULL)
4564 this->glink_ = new Output_data_glink<size, big_endian>(this);
4565 this->glink_->add_eh_frame(layout);
4566 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4567 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4568 this->glink_, ORDER_TEXT, false);
4572 // Create a PLT entry for a global symbol.
4574 template<int size, bool big_endian>
4576 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
4580 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4581 && gsym->can_use_relative_reloc(false))
4583 if (this->iplt_ == NULL)
4584 this->make_iplt_section(symtab, layout);
4585 this->iplt_->add_ifunc_entry(gsym);
4589 if (this->plt_ == NULL)
4590 this->make_plt_section(symtab, layout);
4591 this->plt_->add_entry(gsym);
4595 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4597 template<int size, bool big_endian>
4599 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
4600 Symbol_table* symtab,
4602 Sized_relobj_file<size, big_endian>* relobj,
4605 if (this->iplt_ == NULL)
4606 this->make_iplt_section(symtab, layout);
4607 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
4610 // Return the number of entries in the PLT.
4612 template<int size, bool big_endian>
4614 Target_powerpc<size, big_endian>::plt_entry_count() const
4616 if (this->plt_ == NULL)
4618 return this->plt_->entry_count();
4621 // Return the offset of the first non-reserved PLT entry.
4623 template<int size, bool big_endian>
4625 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
4627 return this->plt_->first_plt_entry_offset();
4630 // Return the size of each PLT entry.
4632 template<int size, bool big_endian>
4634 Target_powerpc<size, big_endian>::plt_entry_size() const
4636 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
4639 // Create a GOT entry for local dynamic __tls_get_addr calls.
4641 template<int size, bool big_endian>
4643 Target_powerpc<size, big_endian>::tlsld_got_offset(
4644 Symbol_table* symtab,
4646 Sized_relobj_file<size, big_endian>* object)
4648 if (this->tlsld_got_offset_ == -1U)
4650 gold_assert(symtab != NULL && layout != NULL && object != NULL);
4651 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4652 Output_data_got_powerpc<size, big_endian>* got
4653 = this->got_section(symtab, layout);
4654 unsigned int got_offset = got->add_constant_pair(0, 0);
4655 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
4657 this->tlsld_got_offset_ = got_offset;
4659 return this->tlsld_got_offset_;
4662 // Get the Reference_flags for a particular relocation.
4664 template<int size, bool big_endian>
4666 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
4670 case elfcpp::R_POWERPC_NONE:
4671 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4672 case elfcpp::R_POWERPC_GNU_VTENTRY:
4673 case elfcpp::R_PPC64_TOC:
4674 // No symbol reference.
4677 case elfcpp::R_PPC64_ADDR64:
4678 case elfcpp::R_PPC64_UADDR64:
4679 case elfcpp::R_POWERPC_ADDR32:
4680 case elfcpp::R_POWERPC_UADDR32:
4681 case elfcpp::R_POWERPC_ADDR16:
4682 case elfcpp::R_POWERPC_UADDR16:
4683 case elfcpp::R_POWERPC_ADDR16_LO:
4684 case elfcpp::R_POWERPC_ADDR16_HI:
4685 case elfcpp::R_POWERPC_ADDR16_HA:
4686 return Symbol::ABSOLUTE_REF;
4688 case elfcpp::R_POWERPC_ADDR24:
4689 case elfcpp::R_POWERPC_ADDR14:
4690 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4691 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4692 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
4694 case elfcpp::R_PPC64_REL64:
4695 case elfcpp::R_POWERPC_REL32:
4696 case elfcpp::R_PPC_LOCAL24PC:
4697 case elfcpp::R_POWERPC_REL16:
4698 case elfcpp::R_POWERPC_REL16_LO:
4699 case elfcpp::R_POWERPC_REL16_HI:
4700 case elfcpp::R_POWERPC_REL16_HA:
4701 return Symbol::RELATIVE_REF;
4703 case elfcpp::R_POWERPC_REL24:
4704 case elfcpp::R_PPC_PLTREL24:
4705 case elfcpp::R_POWERPC_REL14:
4706 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4707 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4708 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
4710 case elfcpp::R_POWERPC_GOT16:
4711 case elfcpp::R_POWERPC_GOT16_LO:
4712 case elfcpp::R_POWERPC_GOT16_HI:
4713 case elfcpp::R_POWERPC_GOT16_HA:
4714 case elfcpp::R_PPC64_GOT16_DS:
4715 case elfcpp::R_PPC64_GOT16_LO_DS:
4716 case elfcpp::R_PPC64_TOC16:
4717 case elfcpp::R_PPC64_TOC16_LO:
4718 case elfcpp::R_PPC64_TOC16_HI:
4719 case elfcpp::R_PPC64_TOC16_HA:
4720 case elfcpp::R_PPC64_TOC16_DS:
4721 case elfcpp::R_PPC64_TOC16_LO_DS:
4723 return Symbol::ABSOLUTE_REF;
4725 case elfcpp::R_POWERPC_GOT_TPREL16:
4726 case elfcpp::R_POWERPC_TLS:
4727 return Symbol::TLS_REF;
4729 case elfcpp::R_POWERPC_COPY:
4730 case elfcpp::R_POWERPC_GLOB_DAT:
4731 case elfcpp::R_POWERPC_JMP_SLOT:
4732 case elfcpp::R_POWERPC_RELATIVE:
4733 case elfcpp::R_POWERPC_DTPMOD:
4735 // Not expected. We will give an error later.
4740 // Report an unsupported relocation against a local symbol.
4742 template<int size, bool big_endian>
4744 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
4745 Sized_relobj_file<size, big_endian>* object,
4746 unsigned int r_type)
4748 gold_error(_("%s: unsupported reloc %u against local symbol"),
4749 object->name().c_str(), r_type);
4752 // We are about to emit a dynamic relocation of type R_TYPE. If the
4753 // dynamic linker does not support it, issue an error.
4755 template<int size, bool big_endian>
4757 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
4758 unsigned int r_type)
4760 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
4762 // These are the relocation types supported by glibc for both 32-bit
4763 // and 64-bit powerpc.
4766 case elfcpp::R_POWERPC_NONE:
4767 case elfcpp::R_POWERPC_RELATIVE:
4768 case elfcpp::R_POWERPC_GLOB_DAT:
4769 case elfcpp::R_POWERPC_DTPMOD:
4770 case elfcpp::R_POWERPC_DTPREL:
4771 case elfcpp::R_POWERPC_TPREL:
4772 case elfcpp::R_POWERPC_JMP_SLOT:
4773 case elfcpp::R_POWERPC_COPY:
4774 case elfcpp::R_POWERPC_IRELATIVE:
4775 case elfcpp::R_POWERPC_ADDR32:
4776 case elfcpp::R_POWERPC_UADDR32:
4777 case elfcpp::R_POWERPC_ADDR24:
4778 case elfcpp::R_POWERPC_ADDR16:
4779 case elfcpp::R_POWERPC_UADDR16:
4780 case elfcpp::R_POWERPC_ADDR16_LO:
4781 case elfcpp::R_POWERPC_ADDR16_HI:
4782 case elfcpp::R_POWERPC_ADDR16_HA:
4783 case elfcpp::R_POWERPC_ADDR14:
4784 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4785 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4786 case elfcpp::R_POWERPC_REL32:
4787 case elfcpp::R_POWERPC_REL24:
4788 case elfcpp::R_POWERPC_TPREL16:
4789 case elfcpp::R_POWERPC_TPREL16_LO:
4790 case elfcpp::R_POWERPC_TPREL16_HI:
4791 case elfcpp::R_POWERPC_TPREL16_HA:
4802 // These are the relocation types supported only on 64-bit.
4803 case elfcpp::R_PPC64_ADDR64:
4804 case elfcpp::R_PPC64_UADDR64:
4805 case elfcpp::R_PPC64_JMP_IREL:
4806 case elfcpp::R_PPC64_ADDR16_DS:
4807 case elfcpp::R_PPC64_ADDR16_LO_DS:
4808 case elfcpp::R_PPC64_ADDR16_HIGHER:
4809 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4810 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4811 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4812 case elfcpp::R_PPC64_REL64:
4813 case elfcpp::R_POWERPC_ADDR30:
4814 case elfcpp::R_PPC64_TPREL16_DS:
4815 case elfcpp::R_PPC64_TPREL16_LO_DS:
4816 case elfcpp::R_PPC64_TPREL16_HIGHER:
4817 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4818 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4819 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4830 // These are the relocation types supported only on 32-bit.
4831 // ??? glibc ld.so doesn't need to support these.
4832 case elfcpp::R_POWERPC_DTPREL16:
4833 case elfcpp::R_POWERPC_DTPREL16_LO:
4834 case elfcpp::R_POWERPC_DTPREL16_HI:
4835 case elfcpp::R_POWERPC_DTPREL16_HA:
4843 // This prevents us from issuing more than one error per reloc
4844 // section. But we can still wind up issuing more than one
4845 // error per object file.
4846 if (this->issued_non_pic_error_)
4848 gold_assert(parameters->options().output_is_position_independent());
4849 object->error(_("requires unsupported dynamic reloc; "
4850 "recompile with -fPIC"));
4851 this->issued_non_pic_error_ = true;
4855 // Return whether we need to make a PLT entry for a relocation of the
4856 // given type against a STT_GNU_IFUNC symbol.
4858 template<int size, bool big_endian>
4860 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4861 Sized_relobj_file<size, big_endian>* object,
4862 unsigned int r_type,
4865 // In non-pic code any reference will resolve to the plt call stub
4866 // for the ifunc symbol.
4867 if (size == 32 && !parameters->options().output_is_position_independent())
4872 // Word size refs from data sections are OK, but don't need a PLT entry.
4873 case elfcpp::R_POWERPC_ADDR32:
4874 case elfcpp::R_POWERPC_UADDR32:
4879 case elfcpp::R_PPC64_ADDR64:
4880 case elfcpp::R_PPC64_UADDR64:
4885 // GOT refs are good, but also don't need a PLT entry.
4886 case elfcpp::R_POWERPC_GOT16:
4887 case elfcpp::R_POWERPC_GOT16_LO:
4888 case elfcpp::R_POWERPC_GOT16_HI:
4889 case elfcpp::R_POWERPC_GOT16_HA:
4890 case elfcpp::R_PPC64_GOT16_DS:
4891 case elfcpp::R_PPC64_GOT16_LO_DS:
4894 // Function calls are good, and these do need a PLT entry.
4895 case elfcpp::R_POWERPC_ADDR24:
4896 case elfcpp::R_POWERPC_ADDR14:
4897 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4898 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4899 case elfcpp::R_POWERPC_REL24:
4900 case elfcpp::R_PPC_PLTREL24:
4901 case elfcpp::R_POWERPC_REL14:
4902 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4903 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4910 // Anything else is a problem.
4911 // If we are building a static executable, the libc startup function
4912 // responsible for applying indirect function relocations is going
4913 // to complain about the reloc type.
4914 // If we are building a dynamic executable, we will have a text
4915 // relocation. The dynamic loader will set the text segment
4916 // writable and non-executable to apply text relocations. So we'll
4917 // segfault when trying to run the indirection function to resolve
4920 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4921 object->name().c_str(), r_type);
4925 // Scan a relocation for a local symbol.
4927 template<int size, bool big_endian>
4929 Target_powerpc<size, big_endian>::Scan::local(
4930 Symbol_table* symtab,
4932 Target_powerpc<size, big_endian>* target,
4933 Sized_relobj_file<size, big_endian>* object,
4934 unsigned int data_shndx,
4935 Output_section* output_section,
4936 const elfcpp::Rela<size, big_endian>& reloc,
4937 unsigned int r_type,
4938 const elfcpp::Sym<size, big_endian>& lsym,
4941 this->maybe_skip_tls_get_addr_call(r_type, NULL);
4943 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4944 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4946 this->expect_tls_get_addr_call();
4947 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
4948 if (tls_type != tls::TLSOPT_NONE)
4949 this->skip_next_tls_get_addr_call();
4951 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4952 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4954 this->expect_tls_get_addr_call();
4955 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4956 if (tls_type != tls::TLSOPT_NONE)
4957 this->skip_next_tls_get_addr_call();
4960 Powerpc_relobj<size, big_endian>* ppc_object
4961 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4966 && data_shndx == ppc_object->opd_shndx()
4967 && r_type == elfcpp::R_PPC64_ADDR64)
4968 ppc_object->set_opd_discard(reloc.get_r_offset());
4972 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4973 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4974 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
4976 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4977 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4978 r_type, r_sym, reloc.get_r_addend());
4979 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4984 case elfcpp::R_POWERPC_NONE:
4985 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4986 case elfcpp::R_POWERPC_GNU_VTENTRY:
4987 case elfcpp::R_PPC64_TOCSAVE:
4988 case elfcpp::R_PPC_EMB_MRKREF:
4989 case elfcpp::R_POWERPC_TLS:
4992 case elfcpp::R_PPC64_TOC:
4994 Output_data_got_powerpc<size, big_endian>* got
4995 = target->got_section(symtab, layout);
4996 if (parameters->options().output_is_position_independent())
4998 Address off = reloc.get_r_offset();
5000 && data_shndx == ppc_object->opd_shndx()
5001 && ppc_object->get_opd_discard(off - 8))
5004 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5005 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5006 rela_dyn->add_output_section_relative(got->output_section(),
5007 elfcpp::R_POWERPC_RELATIVE,
5009 object, data_shndx, off,
5010 symobj->toc_base_offset());
5015 case elfcpp::R_PPC64_ADDR64:
5016 case elfcpp::R_PPC64_UADDR64:
5017 case elfcpp::R_POWERPC_ADDR32:
5018 case elfcpp::R_POWERPC_UADDR32:
5019 case elfcpp::R_POWERPC_ADDR24:
5020 case elfcpp::R_POWERPC_ADDR16:
5021 case elfcpp::R_POWERPC_ADDR16_LO:
5022 case elfcpp::R_POWERPC_ADDR16_HI:
5023 case elfcpp::R_POWERPC_ADDR16_HA:
5024 case elfcpp::R_POWERPC_UADDR16:
5025 case elfcpp::R_PPC64_ADDR16_HIGHER:
5026 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5027 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5028 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5029 case elfcpp::R_PPC64_ADDR16_DS:
5030 case elfcpp::R_PPC64_ADDR16_LO_DS:
5031 case elfcpp::R_POWERPC_ADDR14:
5032 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5033 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5034 // If building a shared library (or a position-independent
5035 // executable), we need to create a dynamic relocation for
5037 if (parameters->options().output_is_position_independent()
5038 || (size == 64 && is_ifunc))
5040 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5042 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5043 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5045 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5046 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5047 : elfcpp::R_POWERPC_RELATIVE);
5048 rela_dyn->add_local_relative(object, r_sym, dynrel,
5049 output_section, data_shndx,
5050 reloc.get_r_offset(),
5051 reloc.get_r_addend(), false);
5055 check_non_pic(object, r_type);
5056 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5057 rela_dyn->add_local(object, r_sym, r_type, output_section,
5058 data_shndx, reloc.get_r_offset(),
5059 reloc.get_r_addend());
5064 case elfcpp::R_POWERPC_REL24:
5065 case elfcpp::R_PPC_PLTREL24:
5066 case elfcpp::R_PPC_LOCAL24PC:
5067 case elfcpp::R_POWERPC_REL14:
5068 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5069 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5071 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5072 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5073 reloc.get_r_addend());
5076 case elfcpp::R_PPC64_REL64:
5077 case elfcpp::R_POWERPC_REL32:
5078 case elfcpp::R_POWERPC_REL16:
5079 case elfcpp::R_POWERPC_REL16_LO:
5080 case elfcpp::R_POWERPC_REL16_HI:
5081 case elfcpp::R_POWERPC_REL16_HA:
5082 case elfcpp::R_POWERPC_SECTOFF:
5083 case elfcpp::R_POWERPC_TPREL16:
5084 case elfcpp::R_POWERPC_DTPREL16:
5085 case elfcpp::R_POWERPC_SECTOFF_LO:
5086 case elfcpp::R_POWERPC_TPREL16_LO:
5087 case elfcpp::R_POWERPC_DTPREL16_LO:
5088 case elfcpp::R_POWERPC_SECTOFF_HI:
5089 case elfcpp::R_POWERPC_TPREL16_HI:
5090 case elfcpp::R_POWERPC_DTPREL16_HI:
5091 case elfcpp::R_POWERPC_SECTOFF_HA:
5092 case elfcpp::R_POWERPC_TPREL16_HA:
5093 case elfcpp::R_POWERPC_DTPREL16_HA:
5094 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5095 case elfcpp::R_PPC64_TPREL16_HIGHER:
5096 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5097 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5098 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5099 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5100 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5101 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5102 case elfcpp::R_PPC64_TPREL16_DS:
5103 case elfcpp::R_PPC64_TPREL16_LO_DS:
5104 case elfcpp::R_PPC64_DTPREL16_DS:
5105 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5106 case elfcpp::R_PPC64_SECTOFF_DS:
5107 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5108 case elfcpp::R_PPC64_TLSGD:
5109 case elfcpp::R_PPC64_TLSLD:
5112 case elfcpp::R_POWERPC_GOT16:
5113 case elfcpp::R_POWERPC_GOT16_LO:
5114 case elfcpp::R_POWERPC_GOT16_HI:
5115 case elfcpp::R_POWERPC_GOT16_HA:
5116 case elfcpp::R_PPC64_GOT16_DS:
5117 case elfcpp::R_PPC64_GOT16_LO_DS:
5119 // The symbol requires a GOT entry.
5120 Output_data_got_powerpc<size, big_endian>* got
5121 = target->got_section(symtab, layout);
5122 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5124 if (!parameters->options().output_is_position_independent())
5126 if (size == 32 && is_ifunc)
5127 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5129 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5131 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5133 // If we are generating a shared object or a pie, this
5134 // symbol's GOT entry will be set by a dynamic relocation.
5136 off = got->add_constant(0);
5137 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5139 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5141 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5142 : elfcpp::R_POWERPC_RELATIVE);
5143 rela_dyn->add_local_relative(object, r_sym, dynrel,
5144 got, off, 0, false);
5149 case elfcpp::R_PPC64_TOC16:
5150 case elfcpp::R_PPC64_TOC16_LO:
5151 case elfcpp::R_PPC64_TOC16_HI:
5152 case elfcpp::R_PPC64_TOC16_HA:
5153 case elfcpp::R_PPC64_TOC16_DS:
5154 case elfcpp::R_PPC64_TOC16_LO_DS:
5155 // We need a GOT section.
5156 target->got_section(symtab, layout);
5159 case elfcpp::R_POWERPC_GOT_TLSGD16:
5160 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5161 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5162 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5164 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5165 if (tls_type == tls::TLSOPT_NONE)
5167 Output_data_got_powerpc<size, big_endian>* got
5168 = target->got_section(symtab, layout);
5169 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5170 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5171 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5172 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5174 else if (tls_type == tls::TLSOPT_TO_LE)
5176 // no GOT relocs needed for Local Exec.
5183 case elfcpp::R_POWERPC_GOT_TLSLD16:
5184 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5185 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5186 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5188 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5189 if (tls_type == tls::TLSOPT_NONE)
5190 target->tlsld_got_offset(symtab, layout, object);
5191 else if (tls_type == tls::TLSOPT_TO_LE)
5193 // no GOT relocs needed for Local Exec.
5194 if (parameters->options().emit_relocs())
5196 Output_section* os = layout->tls_segment()->first_section();
5197 gold_assert(os != NULL);
5198 os->set_needs_symtab_index();
5206 case elfcpp::R_POWERPC_GOT_DTPREL16:
5207 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5208 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5209 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5211 Output_data_got_powerpc<size, big_endian>* got
5212 = target->got_section(symtab, layout);
5213 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5214 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5218 case elfcpp::R_POWERPC_GOT_TPREL16:
5219 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5220 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5221 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5223 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5224 if (tls_type == tls::TLSOPT_NONE)
5226 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5227 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5229 Output_data_got_powerpc<size, big_endian>* got
5230 = target->got_section(symtab, layout);
5231 unsigned int off = got->add_constant(0);
5232 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5234 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5235 rela_dyn->add_symbolless_local_addend(object, r_sym,
5236 elfcpp::R_POWERPC_TPREL,
5240 else if (tls_type == tls::TLSOPT_TO_LE)
5242 // no GOT relocs needed for Local Exec.
5250 unsupported_reloc_local(object, r_type);
5256 case elfcpp::R_POWERPC_GOT_TLSLD16:
5257 case elfcpp::R_POWERPC_GOT_TLSGD16:
5258 case elfcpp::R_POWERPC_GOT_TPREL16:
5259 case elfcpp::R_POWERPC_GOT_DTPREL16:
5260 case elfcpp::R_POWERPC_GOT16:
5261 case elfcpp::R_PPC64_GOT16_DS:
5262 case elfcpp::R_PPC64_TOC16:
5263 case elfcpp::R_PPC64_TOC16_DS:
5264 ppc_object->set_has_small_toc_reloc();
5270 // Report an unsupported relocation against a global symbol.
5272 template<int size, bool big_endian>
5274 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5275 Sized_relobj_file<size, big_endian>* object,
5276 unsigned int r_type,
5279 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5280 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5283 // Scan a relocation for a global symbol.
5285 template<int size, bool big_endian>
5287 Target_powerpc<size, big_endian>::Scan::global(
5288 Symbol_table* symtab,
5290 Target_powerpc<size, big_endian>* target,
5291 Sized_relobj_file<size, big_endian>* object,
5292 unsigned int data_shndx,
5293 Output_section* output_section,
5294 const elfcpp::Rela<size, big_endian>& reloc,
5295 unsigned int r_type,
5298 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5301 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5302 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5304 this->expect_tls_get_addr_call();
5305 const bool final = gsym->final_value_is_known();
5306 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5307 if (tls_type != tls::TLSOPT_NONE)
5308 this->skip_next_tls_get_addr_call();
5310 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5311 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5313 this->expect_tls_get_addr_call();
5314 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5315 if (tls_type != tls::TLSOPT_NONE)
5316 this->skip_next_tls_get_addr_call();
5319 Powerpc_relobj<size, big_endian>* ppc_object
5320 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5322 // A STT_GNU_IFUNC symbol may require a PLT entry.
5323 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5324 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
5326 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5327 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5328 reloc.get_r_addend());
5329 target->make_plt_entry(symtab, layout, gsym);
5334 case elfcpp::R_POWERPC_NONE:
5335 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5336 case elfcpp::R_POWERPC_GNU_VTENTRY:
5337 case elfcpp::R_PPC_LOCAL24PC:
5338 case elfcpp::R_PPC_EMB_MRKREF:
5339 case elfcpp::R_POWERPC_TLS:
5342 case elfcpp::R_PPC64_TOC:
5344 Output_data_got_powerpc<size, big_endian>* got
5345 = target->got_section(symtab, layout);
5346 if (parameters->options().output_is_position_independent())
5348 Address off = reloc.get_r_offset();
5350 && data_shndx == ppc_object->opd_shndx()
5351 && ppc_object->get_opd_discard(off - 8))
5354 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5355 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5356 if (data_shndx != ppc_object->opd_shndx())
5357 symobj = static_cast
5358 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5359 rela_dyn->add_output_section_relative(got->output_section(),
5360 elfcpp::R_POWERPC_RELATIVE,
5362 object, data_shndx, off,
5363 symobj->toc_base_offset());
5368 case elfcpp::R_PPC64_ADDR64:
5370 && data_shndx == ppc_object->opd_shndx()
5371 && (gsym->is_defined_in_discarded_section()
5372 || gsym->object() != object))
5374 ppc_object->set_opd_discard(reloc.get_r_offset());
5378 case elfcpp::R_PPC64_UADDR64:
5379 case elfcpp::R_POWERPC_ADDR32:
5380 case elfcpp::R_POWERPC_UADDR32:
5381 case elfcpp::R_POWERPC_ADDR24:
5382 case elfcpp::R_POWERPC_ADDR16:
5383 case elfcpp::R_POWERPC_ADDR16_LO:
5384 case elfcpp::R_POWERPC_ADDR16_HI:
5385 case elfcpp::R_POWERPC_ADDR16_HA:
5386 case elfcpp::R_POWERPC_UADDR16:
5387 case elfcpp::R_PPC64_ADDR16_HIGHER:
5388 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5389 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5390 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5391 case elfcpp::R_PPC64_ADDR16_DS:
5392 case elfcpp::R_PPC64_ADDR16_LO_DS:
5393 case elfcpp::R_POWERPC_ADDR14:
5394 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5395 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5397 // Make a PLT entry if necessary.
5398 if (gsym->needs_plt_entry())
5402 target->push_branch(ppc_object, data_shndx,
5403 reloc.get_r_offset(), r_type,
5404 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5405 reloc.get_r_addend());
5406 target->make_plt_entry(symtab, layout, gsym);
5408 // Since this is not a PC-relative relocation, we may be
5409 // taking the address of a function. In that case we need to
5410 // set the entry in the dynamic symbol table to the address of
5411 // the PLT call stub.
5413 && gsym->is_from_dynobj()
5414 && !parameters->options().output_is_position_independent())
5415 gsym->set_needs_dynsym_value();
5417 // Make a dynamic relocation if necessary.
5418 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
5419 || (size == 64 && is_ifunc))
5421 if (gsym->may_need_copy_reloc())
5423 target->copy_reloc(symtab, layout, object,
5424 data_shndx, output_section, gsym, reloc);
5426 else if ((size == 32
5427 && r_type == elfcpp::R_POWERPC_ADDR32
5428 && gsym->can_use_relative_reloc(false)
5429 && !(gsym->visibility() == elfcpp::STV_PROTECTED
5430 && parameters->options().shared()))
5432 && r_type == elfcpp::R_PPC64_ADDR64
5433 && (gsym->can_use_relative_reloc(false)
5434 || data_shndx == ppc_object->opd_shndx())))
5436 Reloc_section* rela_dyn
5437 = target->rela_dyn_section(symtab, layout, is_ifunc);
5438 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5439 : elfcpp::R_POWERPC_RELATIVE);
5440 rela_dyn->add_symbolless_global_addend(
5441 gsym, dynrel, output_section, object, data_shndx,
5442 reloc.get_r_offset(), reloc.get_r_addend());
5446 Reloc_section* rela_dyn
5447 = target->rela_dyn_section(symtab, layout, is_ifunc);
5448 check_non_pic(object, r_type);
5449 rela_dyn->add_global(gsym, r_type, output_section,
5451 reloc.get_r_offset(),
5452 reloc.get_r_addend());
5458 case elfcpp::R_PPC_PLTREL24:
5459 case elfcpp::R_POWERPC_REL24:
5462 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5464 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5465 reloc.get_r_addend());
5466 if (gsym->needs_plt_entry()
5467 || (!gsym->final_value_is_known()
5468 && (gsym->is_undefined()
5469 || gsym->is_from_dynobj()
5470 || gsym->is_preemptible())))
5471 target->make_plt_entry(symtab, layout, gsym);
5475 case elfcpp::R_PPC64_REL64:
5476 case elfcpp::R_POWERPC_REL32:
5477 // Make a dynamic relocation if necessary.
5478 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
5480 if (gsym->may_need_copy_reloc())
5482 target->copy_reloc(symtab, layout, object,
5483 data_shndx, output_section, gsym,
5488 Reloc_section* rela_dyn
5489 = target->rela_dyn_section(symtab, layout, is_ifunc);
5490 check_non_pic(object, r_type);
5491 rela_dyn->add_global(gsym, r_type, output_section, object,
5492 data_shndx, reloc.get_r_offset(),
5493 reloc.get_r_addend());
5498 case elfcpp::R_POWERPC_REL14:
5499 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5500 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5502 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5503 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5504 reloc.get_r_addend());
5507 case elfcpp::R_POWERPC_REL16:
5508 case elfcpp::R_POWERPC_REL16_LO:
5509 case elfcpp::R_POWERPC_REL16_HI:
5510 case elfcpp::R_POWERPC_REL16_HA:
5511 case elfcpp::R_POWERPC_SECTOFF:
5512 case elfcpp::R_POWERPC_TPREL16:
5513 case elfcpp::R_POWERPC_DTPREL16:
5514 case elfcpp::R_POWERPC_SECTOFF_LO:
5515 case elfcpp::R_POWERPC_TPREL16_LO:
5516 case elfcpp::R_POWERPC_DTPREL16_LO:
5517 case elfcpp::R_POWERPC_SECTOFF_HI:
5518 case elfcpp::R_POWERPC_TPREL16_HI:
5519 case elfcpp::R_POWERPC_DTPREL16_HI:
5520 case elfcpp::R_POWERPC_SECTOFF_HA:
5521 case elfcpp::R_POWERPC_TPREL16_HA:
5522 case elfcpp::R_POWERPC_DTPREL16_HA:
5523 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5524 case elfcpp::R_PPC64_TPREL16_HIGHER:
5525 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5526 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5527 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5528 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5529 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5530 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5531 case elfcpp::R_PPC64_TPREL16_DS:
5532 case elfcpp::R_PPC64_TPREL16_LO_DS:
5533 case elfcpp::R_PPC64_DTPREL16_DS:
5534 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5535 case elfcpp::R_PPC64_SECTOFF_DS:
5536 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5537 case elfcpp::R_PPC64_TLSGD:
5538 case elfcpp::R_PPC64_TLSLD:
5541 case elfcpp::R_POWERPC_GOT16:
5542 case elfcpp::R_POWERPC_GOT16_LO:
5543 case elfcpp::R_POWERPC_GOT16_HI:
5544 case elfcpp::R_POWERPC_GOT16_HA:
5545 case elfcpp::R_PPC64_GOT16_DS:
5546 case elfcpp::R_PPC64_GOT16_LO_DS:
5548 // The symbol requires a GOT entry.
5549 Output_data_got_powerpc<size, big_endian>* got;
5551 got = target->got_section(symtab, layout);
5552 if (gsym->final_value_is_known())
5554 if (size == 32 && is_ifunc)
5555 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5557 got->add_global(gsym, GOT_TYPE_STANDARD);
5559 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
5561 // If we are generating a shared object or a pie, this
5562 // symbol's GOT entry will be set by a dynamic relocation.
5563 unsigned int off = got->add_constant(0);
5564 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
5566 Reloc_section* rela_dyn
5567 = target->rela_dyn_section(symtab, layout, is_ifunc);
5569 if (gsym->can_use_relative_reloc(false)
5571 && gsym->visibility() == elfcpp::STV_PROTECTED
5572 && parameters->options().shared()))
5574 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5575 : elfcpp::R_POWERPC_RELATIVE);
5576 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
5580 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5581 rela_dyn->add_global(gsym, dynrel, got, off, 0);
5587 case elfcpp::R_PPC64_TOC16:
5588 case elfcpp::R_PPC64_TOC16_LO:
5589 case elfcpp::R_PPC64_TOC16_HI:
5590 case elfcpp::R_PPC64_TOC16_HA:
5591 case elfcpp::R_PPC64_TOC16_DS:
5592 case elfcpp::R_PPC64_TOC16_LO_DS:
5593 // We need a GOT section.
5594 target->got_section(symtab, layout);
5597 case elfcpp::R_POWERPC_GOT_TLSGD16:
5598 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5599 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5600 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5602 const bool final = gsym->final_value_is_known();
5603 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5604 if (tls_type == tls::TLSOPT_NONE)
5606 Output_data_got_powerpc<size, big_endian>* got
5607 = target->got_section(symtab, layout);
5608 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5609 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
5610 elfcpp::R_POWERPC_DTPMOD,
5611 elfcpp::R_POWERPC_DTPREL);
5613 else if (tls_type == tls::TLSOPT_TO_IE)
5615 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5617 Output_data_got_powerpc<size, big_endian>* got
5618 = target->got_section(symtab, layout);
5619 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5620 if (gsym->is_undefined()
5621 || gsym->is_from_dynobj())
5623 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5624 elfcpp::R_POWERPC_TPREL);
5628 unsigned int off = got->add_constant(0);
5629 gsym->set_got_offset(GOT_TYPE_TPREL, off);
5630 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5631 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5636 else if (tls_type == tls::TLSOPT_TO_LE)
5638 // no GOT relocs needed for Local Exec.
5645 case elfcpp::R_POWERPC_GOT_TLSLD16:
5646 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5647 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5648 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5650 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5651 if (tls_type == tls::TLSOPT_NONE)
5652 target->tlsld_got_offset(symtab, layout, object);
5653 else if (tls_type == tls::TLSOPT_TO_LE)
5655 // no GOT relocs needed for Local Exec.
5656 if (parameters->options().emit_relocs())
5658 Output_section* os = layout->tls_segment()->first_section();
5659 gold_assert(os != NULL);
5660 os->set_needs_symtab_index();
5668 case elfcpp::R_POWERPC_GOT_DTPREL16:
5669 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5670 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5671 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5673 Output_data_got_powerpc<size, big_endian>* got
5674 = target->got_section(symtab, layout);
5675 if (!gsym->final_value_is_known()
5676 && (gsym->is_from_dynobj()
5677 || gsym->is_undefined()
5678 || gsym->is_preemptible()))
5679 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
5680 target->rela_dyn_section(layout),
5681 elfcpp::R_POWERPC_DTPREL);
5683 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
5687 case elfcpp::R_POWERPC_GOT_TPREL16:
5688 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5689 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5690 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5692 const bool final = gsym->final_value_is_known();
5693 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5694 if (tls_type == tls::TLSOPT_NONE)
5696 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5698 Output_data_got_powerpc<size, big_endian>* got
5699 = target->got_section(symtab, layout);
5700 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5701 if (gsym->is_undefined()
5702 || gsym->is_from_dynobj())
5704 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5705 elfcpp::R_POWERPC_TPREL);
5709 unsigned int off = got->add_constant(0);
5710 gsym->set_got_offset(GOT_TYPE_TPREL, off);
5711 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5712 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5717 else if (tls_type == tls::TLSOPT_TO_LE)
5719 // no GOT relocs needed for Local Exec.
5727 unsupported_reloc_global(object, r_type, gsym);
5733 case elfcpp::R_POWERPC_GOT_TLSLD16:
5734 case elfcpp::R_POWERPC_GOT_TLSGD16:
5735 case elfcpp::R_POWERPC_GOT_TPREL16:
5736 case elfcpp::R_POWERPC_GOT_DTPREL16:
5737 case elfcpp::R_POWERPC_GOT16:
5738 case elfcpp::R_PPC64_GOT16_DS:
5739 case elfcpp::R_PPC64_TOC16:
5740 case elfcpp::R_PPC64_TOC16_DS:
5741 ppc_object->set_has_small_toc_reloc();
5747 // Process relocations for gc.
5749 template<int size, bool big_endian>
5751 Target_powerpc<size, big_endian>::gc_process_relocs(
5752 Symbol_table* symtab,
5754 Sized_relobj_file<size, big_endian>* object,
5755 unsigned int data_shndx,
5757 const unsigned char* prelocs,
5759 Output_section* output_section,
5760 bool needs_special_offset_handling,
5761 size_t local_symbol_count,
5762 const unsigned char* plocal_symbols)
5764 typedef Target_powerpc<size, big_endian> Powerpc;
5765 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5766 Powerpc_relobj<size, big_endian>* ppc_object
5767 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5769 ppc_object->set_opd_valid();
5770 if (size == 64 && data_shndx == ppc_object->opd_shndx())
5772 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
5773 for (p = ppc_object->access_from_map()->begin();
5774 p != ppc_object->access_from_map()->end();
5777 Address dst_off = p->first;
5778 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5779 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
5780 for (s = p->second.begin(); s != p->second.end(); ++s)
5782 Object* src_obj = s->first;
5783 unsigned int src_indx = s->second;
5784 symtab->gc()->add_reference(src_obj, src_indx,
5785 ppc_object, dst_indx);
5789 ppc_object->access_from_map()->clear();
5790 ppc_object->process_gc_mark(symtab);
5791 // Don't look at .opd relocs as .opd will reference everything.
5795 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
5796 typename Target_powerpc::Relocatable_size_for_reloc>(
5805 needs_special_offset_handling,
5810 // Handle target specific gc actions when adding a gc reference from
5811 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5812 // and DST_OFF. For powerpc64, this adds a referenc to the code
5813 // section of a function descriptor.
5815 template<int size, bool big_endian>
5817 Target_powerpc<size, big_endian>::do_gc_add_reference(
5818 Symbol_table* symtab,
5820 unsigned int src_shndx,
5822 unsigned int dst_shndx,
5823 Address dst_off) const
5825 if (size != 64 || dst_obj->is_dynamic())
5828 Powerpc_relobj<size, big_endian>* ppc_object
5829 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
5830 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
5832 if (ppc_object->opd_valid())
5834 dst_shndx = ppc_object->get_opd_ent(dst_off);
5835 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
5839 // If we haven't run scan_opd_relocs, we must delay
5840 // processing this function descriptor reference.
5841 ppc_object->add_reference(src_obj, src_shndx, dst_off);
5846 // Add any special sections for this symbol to the gc work list.
5847 // For powerpc64, this adds the code section of a function
5850 template<int size, bool big_endian>
5852 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
5853 Symbol_table* symtab,
5858 Powerpc_relobj<size, big_endian>* ppc_object
5859 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
5861 unsigned int shndx = sym->shndx(&is_ordinary);
5862 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
5864 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
5865 Address dst_off = gsym->value();
5866 if (ppc_object->opd_valid())
5868 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5869 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
5872 ppc_object->add_gc_mark(dst_off);
5877 // For a symbol location in .opd, set LOC to the location of the
5880 template<int size, bool big_endian>
5882 Target_powerpc<size, big_endian>::do_function_location(
5883 Symbol_location* loc) const
5885 if (size == 64 && loc->shndx != 0)
5887 if (loc->object->is_dynamic())
5889 Powerpc_dynobj<size, big_endian>* ppc_object
5890 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
5891 if (loc->shndx == ppc_object->opd_shndx())
5894 Address off = loc->offset - ppc_object->opd_address();
5895 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
5896 loc->offset = dest_off;
5901 const Powerpc_relobj<size, big_endian>* ppc_object
5902 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
5903 if (loc->shndx == ppc_object->opd_shndx())
5906 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
5907 loc->offset = dest_off;
5913 // Scan relocations for a section.
5915 template<int size, bool big_endian>
5917 Target_powerpc<size, big_endian>::scan_relocs(
5918 Symbol_table* symtab,
5920 Sized_relobj_file<size, big_endian>* object,
5921 unsigned int data_shndx,
5922 unsigned int sh_type,
5923 const unsigned char* prelocs,
5925 Output_section* output_section,
5926 bool needs_special_offset_handling,
5927 size_t local_symbol_count,
5928 const unsigned char* plocal_symbols)
5930 typedef Target_powerpc<size, big_endian> Powerpc;
5931 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5933 if (sh_type == elfcpp::SHT_REL)
5935 gold_error(_("%s: unsupported REL reloc section"),
5936 object->name().c_str());
5940 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
5949 needs_special_offset_handling,
5954 // Functor class for processing the global symbol table.
5955 // Removes symbols defined on discarded opd entries.
5957 template<bool big_endian>
5958 class Global_symbol_visitor_opd
5961 Global_symbol_visitor_opd()
5965 operator()(Sized_symbol<64>* sym)
5967 if (sym->has_symtab_index()
5968 || sym->source() != Symbol::FROM_OBJECT
5969 || !sym->in_real_elf())
5972 if (sym->object()->is_dynamic())
5975 Powerpc_relobj<64, big_endian>* symobj
5976 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
5977 if (symobj->opd_shndx() == 0)
5981 unsigned int shndx = sym->shndx(&is_ordinary);
5982 if (shndx == symobj->opd_shndx()
5983 && symobj->get_opd_discard(sym->value()))
5984 sym->set_symtab_index(-1U);
5988 template<int size, bool big_endian>
5990 Target_powerpc<size, big_endian>::define_save_restore_funcs(
5992 Symbol_table* symtab)
5996 Output_data_save_res<64, big_endian>* savres
5997 = new Output_data_save_res<64, big_endian>(symtab);
5998 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5999 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6000 savres, ORDER_TEXT, false);
6004 // Sort linker created .got section first (for the header), then input
6005 // sections belonging to files using small model code.
6007 template<bool big_endian>
6008 class Sort_toc_sections
6012 operator()(const Output_section::Input_section& is1,
6013 const Output_section::Input_section& is2) const
6015 if (!is1.is_input_section() && is2.is_input_section())
6018 = (is1.is_input_section()
6019 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6020 ->has_small_toc_reloc()));
6022 = (is2.is_input_section()
6023 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6024 ->has_small_toc_reloc()));
6025 return small1 && !small2;
6029 // Finalize the sections.
6031 template<int size, bool big_endian>
6033 Target_powerpc<size, big_endian>::do_finalize_sections(
6035 const Input_objects*,
6036 Symbol_table* symtab)
6038 if (parameters->doing_static_link())
6040 // At least some versions of glibc elf-init.o have a strong
6041 // reference to __rela_iplt marker syms. A weak ref would be
6043 if (this->iplt_ != NULL)
6045 Reloc_section* rel = this->iplt_->rel_plt();
6046 symtab->define_in_output_data("__rela_iplt_start", NULL,
6047 Symbol_table::PREDEFINED, rel, 0, 0,
6048 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6049 elfcpp::STV_HIDDEN, 0, false, true);
6050 symtab->define_in_output_data("__rela_iplt_end", NULL,
6051 Symbol_table::PREDEFINED, rel, 0, 0,
6052 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6053 elfcpp::STV_HIDDEN, 0, true, true);
6057 symtab->define_as_constant("__rela_iplt_start", NULL,
6058 Symbol_table::PREDEFINED, 0, 0,
6059 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6060 elfcpp::STV_HIDDEN, 0, true, false);
6061 symtab->define_as_constant("__rela_iplt_end", NULL,
6062 Symbol_table::PREDEFINED, 0, 0,
6063 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6064 elfcpp::STV_HIDDEN, 0, true, false);
6070 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6071 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6073 if (!parameters->options().relocatable())
6075 this->define_save_restore_funcs(layout, symtab);
6077 // Annoyingly, we need to make these sections now whether or
6078 // not we need them. If we delay until do_relax then we
6079 // need to mess with the relaxation machinery checkpointing.
6080 this->got_section(symtab, layout);
6081 this->make_brlt_section(layout);
6083 if (parameters->options().toc_sort())
6085 Output_section* os = this->got_->output_section();
6086 if (os != NULL && os->input_sections().size() > 1)
6087 std::stable_sort(os->input_sections().begin(),
6088 os->input_sections().end(),
6089 Sort_toc_sections<big_endian>());
6094 // Fill in some more dynamic tags.
6095 Output_data_dynamic* odyn = layout->dynamic_data();
6098 const Reloc_section* rel_plt = (this->plt_ == NULL
6100 : this->plt_->rel_plt());
6101 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6102 this->rela_dyn_, true, size == 32);
6106 if (this->got_ != NULL)
6108 this->got_->finalize_data_size();
6109 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6110 this->got_, this->got_->g_o_t());
6115 if (this->glink_ != NULL)
6117 this->glink_->finalize_data_size();
6118 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6120 (this->glink_->pltresolve_size
6126 // Emit any relocs we saved in an attempt to avoid generating COPY
6128 if (this->copy_relocs_.any_saved_relocs())
6129 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6132 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6136 ok_lo_toc_insn(uint32_t insn)
6138 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6139 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6140 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6141 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6142 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6143 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6144 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6145 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6146 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6147 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6148 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6149 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6150 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6151 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6152 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6154 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6155 && ((insn & 3) == 0 || (insn & 3) == 3))
6156 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6159 // Return the value to use for a branch relocation.
6161 template<int size, bool big_endian>
6162 typename Target_powerpc<size, big_endian>::Address
6163 Target_powerpc<size, big_endian>::symval_for_branch(
6164 const Symbol_table* symtab,
6166 const Sized_symbol<size>* gsym,
6167 Powerpc_relobj<size, big_endian>* object,
6168 unsigned int *dest_shndx)
6174 // If the symbol is defined in an opd section, ie. is a function
6175 // descriptor, use the function descriptor code entry address
6176 Powerpc_relobj<size, big_endian>* symobj = object;
6178 && gsym->source() != Symbol::FROM_OBJECT)
6181 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6182 unsigned int shndx = symobj->opd_shndx();
6185 Address opd_addr = symobj->get_output_section_offset(shndx);
6186 if (opd_addr == invalid_address)
6188 opd_addr += symobj->output_section(shndx)->address();
6189 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
6192 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
6193 if (symtab->is_section_folded(symobj, *dest_shndx))
6196 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6197 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6198 *dest_shndx = folded.second;
6200 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6201 gold_assert(sec_addr != invalid_address);
6202 sec_addr += symobj->output_section(*dest_shndx)->address();
6203 value = sec_addr + sec_off;
6208 // Perform a relocation.
6210 template<int size, bool big_endian>
6212 Target_powerpc<size, big_endian>::Relocate::relocate(
6213 const Relocate_info<size, big_endian>* relinfo,
6214 Target_powerpc* target,
6217 const elfcpp::Rela<size, big_endian>& rela,
6218 unsigned int r_type,
6219 const Sized_symbol<size>* gsym,
6220 const Symbol_value<size>* psymval,
6221 unsigned char* view,
6223 section_size_type view_size)
6228 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6230 case Track_tls::NOT_EXPECTED:
6231 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6232 _("__tls_get_addr call lacks marker reloc"));
6234 case Track_tls::EXPECTED:
6235 // We have already complained.
6237 case Track_tls::SKIP:
6239 case Track_tls::NORMAL:
6243 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6244 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6245 Powerpc_relobj<size, big_endian>* const object
6246 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6248 bool has_plt_value = false;
6249 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6251 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
6252 : object->local_has_plt_offset(r_sym))
6253 && (!psymval->is_ifunc_symbol()
6254 || Scan::reloc_needs_plt_for_ifunc(object, r_type, false)))
6256 Stub_table<size, big_endian>* stub_table
6257 = object->stub_table(relinfo->data_shndx);
6258 if (stub_table == NULL)
6260 // This is a ref from a data section to an ifunc symbol.
6261 if (target->stub_tables().size() != 0)
6262 stub_table = target->stub_tables()[0];
6264 gold_assert(stub_table != NULL);
6267 off = stub_table->find_plt_call_entry(object, gsym, r_type,
6268 rela.get_r_addend());
6270 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6271 rela.get_r_addend());
6272 gold_assert(off != invalid_address);
6273 value = stub_table->stub_address() + off;
6274 has_plt_value = true;
6277 if (r_type == elfcpp::R_POWERPC_GOT16
6278 || r_type == elfcpp::R_POWERPC_GOT16_LO
6279 || r_type == elfcpp::R_POWERPC_GOT16_HI
6280 || r_type == elfcpp::R_POWERPC_GOT16_HA
6281 || r_type == elfcpp::R_PPC64_GOT16_DS
6282 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6286 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6287 value = gsym->got_offset(GOT_TYPE_STANDARD);
6291 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6292 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6293 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6295 value -= target->got_section()->got_base_offset(object);
6297 else if (r_type == elfcpp::R_PPC64_TOC)
6299 value = (target->got_section()->output_section()->address()
6300 + object->toc_base_offset());
6302 else if (gsym != NULL
6303 && (r_type == elfcpp::R_POWERPC_REL24
6304 || r_type == elfcpp::R_PPC_PLTREL24)
6309 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6310 Valtype* wv = reinterpret_cast<Valtype*>(view);
6311 bool can_plt_call = false;
6312 if (rela.get_r_offset() + 8 <= view_size)
6314 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6315 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6318 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6320 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
6321 can_plt_call = true;
6326 // If we don't have a branch and link followed by a nop,
6327 // we can't go via the plt because there is no place to
6328 // put a toc restoring instruction.
6329 // Unless we know we won't be returning.
6330 if (strcmp(gsym->name(), "__libc_start_main") == 0)
6331 can_plt_call = true;
6335 // This is not an error in one special case: A self
6336 // call. It isn't possible to cheaply verify we have
6337 // such a call so just check for a call to the same
6340 Address code = value;
6341 if (gsym->source() == Symbol::FROM_OBJECT
6342 && gsym->object() == object)
6344 Address addend = rela.get_r_addend();
6345 unsigned int dest_shndx;
6346 Address opdent = psymval->value(object, addend);
6347 code = target->symval_for_branch(relinfo->symtab, opdent,
6348 gsym, object, &dest_shndx);
6350 if (dest_shndx == 0)
6351 dest_shndx = gsym->shndx(&is_ordinary);
6352 ok = dest_shndx == relinfo->data_shndx;
6356 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6357 _("call lacks nop, can't restore toc; "
6358 "recompile with -fPIC"));
6364 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6365 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6366 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6367 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6369 // First instruction of a global dynamic sequence, arg setup insn.
6370 const bool final = gsym == NULL || gsym->final_value_is_known();
6371 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6372 enum Got_type got_type = GOT_TYPE_STANDARD;
6373 if (tls_type == tls::TLSOPT_NONE)
6374 got_type = GOT_TYPE_TLSGD;
6375 else if (tls_type == tls::TLSOPT_TO_IE)
6376 got_type = GOT_TYPE_TPREL;
6377 if (got_type != GOT_TYPE_STANDARD)
6381 gold_assert(gsym->has_got_offset(got_type));
6382 value = gsym->got_offset(got_type);
6386 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6387 gold_assert(object->local_has_got_offset(r_sym, got_type));
6388 value = object->local_got_offset(r_sym, got_type);
6390 value -= target->got_section()->got_base_offset(object);
6392 if (tls_type == tls::TLSOPT_TO_IE)
6394 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6395 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6397 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6398 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6399 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6401 insn |= 32 << 26; // lwz
6403 insn |= 58 << 26; // ld
6404 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6406 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6407 - elfcpp::R_POWERPC_GOT_TLSGD16);
6409 else if (tls_type == tls::TLSOPT_TO_LE)
6411 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6412 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6414 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6415 Insn insn = addis_3_13;
6418 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6419 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6420 value = psymval->value(object, rela.get_r_addend());
6424 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6426 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6427 r_type = elfcpp::R_POWERPC_NONE;
6431 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6432 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6433 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6434 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6436 // First instruction of a local dynamic sequence, arg setup insn.
6437 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6438 if (tls_type == tls::TLSOPT_NONE)
6440 value = target->tlsld_got_offset();
6441 value -= target->got_section()->got_base_offset(object);
6445 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6446 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6447 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6449 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6450 Insn insn = addis_3_13;
6453 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6454 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6459 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6461 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6462 r_type = elfcpp::R_POWERPC_NONE;
6466 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6467 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6468 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6469 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6471 // Accesses relative to a local dynamic sequence address,
6472 // no optimisation here.
6475 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6476 value = gsym->got_offset(GOT_TYPE_DTPREL);
6480 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6481 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6482 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6484 value -= target->got_section()->got_base_offset(object);
6486 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6487 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6488 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6489 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6491 // First instruction of initial exec sequence.
6492 const bool final = gsym == NULL || gsym->final_value_is_known();
6493 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6494 if (tls_type == tls::TLSOPT_NONE)
6498 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
6499 value = gsym->got_offset(GOT_TYPE_TPREL);
6503 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6504 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
6505 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
6507 value -= target->got_section()->got_base_offset(object);
6511 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6512 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6513 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6515 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6516 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6517 insn &= (1 << 26) - (1 << 21); // extract rt from ld
6522 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6523 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6524 value = psymval->value(object, rela.get_r_addend());
6528 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6530 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6531 r_type = elfcpp::R_POWERPC_NONE;
6535 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6536 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6538 // Second instruction of a global dynamic sequence,
6539 // the __tls_get_addr call
6540 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6541 const bool final = gsym == NULL || gsym->final_value_is_known();
6542 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6543 if (tls_type != tls::TLSOPT_NONE)
6545 if (tls_type == tls::TLSOPT_TO_IE)
6547 Insn* iview = reinterpret_cast<Insn*>(view);
6548 Insn insn = add_3_3_13;
6551 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6552 r_type = elfcpp::R_POWERPC_NONE;
6556 Insn* iview = reinterpret_cast<Insn*>(view);
6557 Insn insn = addi_3_3;
6558 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6559 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6560 view += 2 * big_endian;
6561 value = psymval->value(object, rela.get_r_addend());
6563 this->skip_next_tls_get_addr_call();
6566 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6567 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6569 // Second instruction of a local dynamic sequence,
6570 // the __tls_get_addr call
6571 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6572 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6573 if (tls_type == tls::TLSOPT_TO_LE)
6575 Insn* iview = reinterpret_cast<Insn*>(view);
6576 Insn insn = addi_3_3;
6577 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6578 this->skip_next_tls_get_addr_call();
6579 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6580 view += 2 * big_endian;
6584 else if (r_type == elfcpp::R_POWERPC_TLS)
6586 // Second instruction of an initial exec sequence
6587 const bool final = gsym == NULL || gsym->final_value_is_known();
6588 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6589 if (tls_type == tls::TLSOPT_TO_LE)
6591 Insn* iview = reinterpret_cast<Insn*>(view);
6592 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6593 unsigned int reg = size == 32 ? 2 : 13;
6594 insn = at_tls_transform(insn, reg);
6595 gold_assert(insn != 0);
6596 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6597 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6598 view += 2 * big_endian;
6599 value = psymval->value(object, rela.get_r_addend());
6602 else if (!has_plt_value)
6605 unsigned int dest_shndx;
6606 if (r_type != elfcpp::R_PPC_PLTREL24)
6607 addend = rela.get_r_addend();
6608 value = psymval->value(object, addend);
6609 if (size == 64 && is_branch_reloc(r_type))
6610 value = target->symval_for_branch(relinfo->symtab, value,
6611 gsym, object, &dest_shndx);
6612 unsigned int max_branch_offset = 0;
6613 if (r_type == elfcpp::R_POWERPC_REL24
6614 || r_type == elfcpp::R_PPC_PLTREL24
6615 || r_type == elfcpp::R_PPC_LOCAL24PC)
6616 max_branch_offset = 1 << 25;
6617 else if (r_type == elfcpp::R_POWERPC_REL14
6618 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
6619 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
6620 max_branch_offset = 1 << 15;
6621 if (max_branch_offset != 0
6622 && value - address + max_branch_offset >= 2 * max_branch_offset)
6624 Stub_table<size, big_endian>* stub_table
6625 = object->stub_table(relinfo->data_shndx);
6626 if (stub_table != NULL)
6628 Address off = stub_table->find_long_branch_entry(object, value);
6629 if (off != invalid_address)
6630 value = (stub_table->stub_address() + stub_table->plt_size()
6638 case elfcpp::R_PPC64_REL64:
6639 case elfcpp::R_POWERPC_REL32:
6640 case elfcpp::R_POWERPC_REL24:
6641 case elfcpp::R_PPC_PLTREL24:
6642 case elfcpp::R_PPC_LOCAL24PC:
6643 case elfcpp::R_POWERPC_REL16:
6644 case elfcpp::R_POWERPC_REL16_LO:
6645 case elfcpp::R_POWERPC_REL16_HI:
6646 case elfcpp::R_POWERPC_REL16_HA:
6647 case elfcpp::R_POWERPC_REL14:
6648 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6649 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6653 case elfcpp::R_PPC64_TOC16:
6654 case elfcpp::R_PPC64_TOC16_LO:
6655 case elfcpp::R_PPC64_TOC16_HI:
6656 case elfcpp::R_PPC64_TOC16_HA:
6657 case elfcpp::R_PPC64_TOC16_DS:
6658 case elfcpp::R_PPC64_TOC16_LO_DS:
6659 // Subtract the TOC base address.
6660 value -= (target->got_section()->output_section()->address()
6661 + object->toc_base_offset());
6664 case elfcpp::R_POWERPC_SECTOFF:
6665 case elfcpp::R_POWERPC_SECTOFF_LO:
6666 case elfcpp::R_POWERPC_SECTOFF_HI:
6667 case elfcpp::R_POWERPC_SECTOFF_HA:
6668 case elfcpp::R_PPC64_SECTOFF_DS:
6669 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6671 value -= os->address();
6674 case elfcpp::R_PPC64_TPREL16_DS:
6675 case elfcpp::R_PPC64_TPREL16_LO_DS:
6677 // R_PPC_TLSGD and R_PPC_TLSLD
6679 case elfcpp::R_POWERPC_TPREL16:
6680 case elfcpp::R_POWERPC_TPREL16_LO:
6681 case elfcpp::R_POWERPC_TPREL16_HI:
6682 case elfcpp::R_POWERPC_TPREL16_HA:
6683 case elfcpp::R_POWERPC_TPREL:
6684 case elfcpp::R_PPC64_TPREL16_HIGHER:
6685 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6686 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6687 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6688 // tls symbol values are relative to tls_segment()->vaddr()
6692 case elfcpp::R_PPC64_DTPREL16_DS:
6693 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6694 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6695 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6696 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6697 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6699 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6700 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6702 case elfcpp::R_POWERPC_DTPREL16:
6703 case elfcpp::R_POWERPC_DTPREL16_LO:
6704 case elfcpp::R_POWERPC_DTPREL16_HI:
6705 case elfcpp::R_POWERPC_DTPREL16_HA:
6706 case elfcpp::R_POWERPC_DTPREL:
6707 // tls symbol values are relative to tls_segment()->vaddr()
6708 value -= dtp_offset;
6715 Insn branch_bit = 0;
6718 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6719 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6720 branch_bit = 1 << 21;
6721 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6722 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6724 Insn* iview = reinterpret_cast<Insn*>(view);
6725 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6728 if (this->is_isa_v2)
6730 // Set 'a' bit. This is 0b00010 in BO field for branch
6731 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6732 // for branch on CTR insns (BO == 1a00t or 1a01t).
6733 if ((insn & (0x14 << 21)) == (0x04 << 21))
6735 else if ((insn & (0x14 << 21)) == (0x10 << 21))
6742 // Invert 'y' bit if not the default.
6743 if (static_cast<Signed_address>(value) < 0)
6746 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6756 // Multi-instruction sequences that access the TOC can be
6757 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6758 // to nop; addi rb,r2,x;
6764 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6765 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6766 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6767 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6768 case elfcpp::R_POWERPC_GOT16_HA:
6769 case elfcpp::R_PPC64_TOC16_HA:
6770 if (parameters->options().toc_optimize())
6772 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6773 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6774 if ((insn & ((0x3f << 26) | 0x1f << 16))
6775 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6776 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6777 _("toc optimization is not supported "
6778 "for %#08x instruction"), insn);
6779 else if (value + 0x8000 < 0x10000)
6781 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
6787 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6788 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6789 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6790 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6791 case elfcpp::R_POWERPC_GOT16_LO:
6792 case elfcpp::R_PPC64_GOT16_LO_DS:
6793 case elfcpp::R_PPC64_TOC16_LO:
6794 case elfcpp::R_PPC64_TOC16_LO_DS:
6795 if (parameters->options().toc_optimize())
6797 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6798 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6799 if (!ok_lo_toc_insn(insn))
6800 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6801 _("toc optimization is not supported "
6802 "for %#08x instruction"), insn);
6803 else if (value + 0x8000 < 0x10000)
6805 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
6807 // Transform addic to addi when we change reg.
6808 insn &= ~((0x3f << 26) | (0x1f << 16));
6809 insn |= (14u << 26) | (2 << 16);
6813 insn &= ~(0x1f << 16);
6816 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6823 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
6826 case elfcpp::R_POWERPC_ADDR32:
6827 case elfcpp::R_POWERPC_UADDR32:
6829 overflow = Reloc::CHECK_BITFIELD;
6832 case elfcpp::R_POWERPC_REL32:
6834 overflow = Reloc::CHECK_SIGNED;
6837 case elfcpp::R_POWERPC_ADDR24:
6838 case elfcpp::R_POWERPC_ADDR16:
6839 case elfcpp::R_POWERPC_UADDR16:
6840 case elfcpp::R_PPC64_ADDR16_DS:
6841 case elfcpp::R_POWERPC_ADDR14:
6842 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6843 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6844 overflow = Reloc::CHECK_BITFIELD;
6847 case elfcpp::R_POWERPC_REL24:
6848 case elfcpp::R_PPC_PLTREL24:
6849 case elfcpp::R_PPC_LOCAL24PC:
6850 case elfcpp::R_POWERPC_REL16:
6851 case elfcpp::R_PPC64_TOC16:
6852 case elfcpp::R_POWERPC_GOT16:
6853 case elfcpp::R_POWERPC_SECTOFF:
6854 case elfcpp::R_POWERPC_TPREL16:
6855 case elfcpp::R_POWERPC_DTPREL16:
6856 case elfcpp::R_PPC64_TPREL16_DS:
6857 case elfcpp::R_PPC64_DTPREL16_DS:
6858 case elfcpp::R_PPC64_TOC16_DS:
6859 case elfcpp::R_PPC64_GOT16_DS:
6860 case elfcpp::R_PPC64_SECTOFF_DS:
6861 case elfcpp::R_POWERPC_REL14:
6862 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6863 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6864 case elfcpp::R_POWERPC_GOT_TLSGD16:
6865 case elfcpp::R_POWERPC_GOT_TLSLD16:
6866 case elfcpp::R_POWERPC_GOT_TPREL16:
6867 case elfcpp::R_POWERPC_GOT_DTPREL16:
6868 overflow = Reloc::CHECK_SIGNED;
6872 typename Powerpc_relocate_functions<size, big_endian>::Status status
6873 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
6876 case elfcpp::R_POWERPC_NONE:
6877 case elfcpp::R_POWERPC_TLS:
6878 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6879 case elfcpp::R_POWERPC_GNU_VTENTRY:
6880 case elfcpp::R_PPC_EMB_MRKREF:
6883 case elfcpp::R_PPC64_ADDR64:
6884 case elfcpp::R_PPC64_REL64:
6885 case elfcpp::R_PPC64_TOC:
6886 Reloc::addr64(view, value);
6889 case elfcpp::R_POWERPC_TPREL:
6890 case elfcpp::R_POWERPC_DTPREL:
6892 Reloc::addr64(view, value);
6894 status = Reloc::addr32(view, value, overflow);
6897 case elfcpp::R_PPC64_UADDR64:
6898 Reloc::addr64_u(view, value);
6901 case elfcpp::R_POWERPC_ADDR32:
6902 status = Reloc::addr32(view, value, overflow);
6905 case elfcpp::R_POWERPC_REL32:
6906 case elfcpp::R_POWERPC_UADDR32:
6907 status = Reloc::addr32_u(view, value, overflow);
6910 case elfcpp::R_POWERPC_ADDR24:
6911 case elfcpp::R_POWERPC_REL24:
6912 case elfcpp::R_PPC_PLTREL24:
6913 case elfcpp::R_PPC_LOCAL24PC:
6914 status = Reloc::addr24(view, value, overflow);
6917 case elfcpp::R_POWERPC_GOT_DTPREL16:
6918 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6921 status = Reloc::addr16_ds(view, value, overflow);
6924 case elfcpp::R_POWERPC_ADDR16:
6925 case elfcpp::R_POWERPC_REL16:
6926 case elfcpp::R_PPC64_TOC16:
6927 case elfcpp::R_POWERPC_GOT16:
6928 case elfcpp::R_POWERPC_SECTOFF:
6929 case elfcpp::R_POWERPC_TPREL16:
6930 case elfcpp::R_POWERPC_DTPREL16:
6931 case elfcpp::R_POWERPC_GOT_TLSGD16:
6932 case elfcpp::R_POWERPC_GOT_TLSLD16:
6933 case elfcpp::R_POWERPC_GOT_TPREL16:
6934 case elfcpp::R_POWERPC_ADDR16_LO:
6935 case elfcpp::R_POWERPC_REL16_LO:
6936 case elfcpp::R_PPC64_TOC16_LO:
6937 case elfcpp::R_POWERPC_GOT16_LO:
6938 case elfcpp::R_POWERPC_SECTOFF_LO:
6939 case elfcpp::R_POWERPC_TPREL16_LO:
6940 case elfcpp::R_POWERPC_DTPREL16_LO:
6941 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6942 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6943 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6944 status = Reloc::addr16(view, value, overflow);
6947 case elfcpp::R_POWERPC_UADDR16:
6948 status = Reloc::addr16_u(view, value, overflow);
6951 case elfcpp::R_POWERPC_ADDR16_HI:
6952 case elfcpp::R_POWERPC_REL16_HI:
6953 case elfcpp::R_PPC64_TOC16_HI:
6954 case elfcpp::R_POWERPC_GOT16_HI:
6955 case elfcpp::R_POWERPC_SECTOFF_HI:
6956 case elfcpp::R_POWERPC_TPREL16_HI:
6957 case elfcpp::R_POWERPC_DTPREL16_HI:
6958 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6959 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6960 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6961 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6962 Reloc::addr16_hi(view, value);
6965 case elfcpp::R_POWERPC_ADDR16_HA:
6966 case elfcpp::R_POWERPC_REL16_HA:
6967 case elfcpp::R_PPC64_TOC16_HA:
6968 case elfcpp::R_POWERPC_GOT16_HA:
6969 case elfcpp::R_POWERPC_SECTOFF_HA:
6970 case elfcpp::R_POWERPC_TPREL16_HA:
6971 case elfcpp::R_POWERPC_DTPREL16_HA:
6972 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6973 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6974 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6975 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6976 Reloc::addr16_ha(view, value);
6979 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6981 // R_PPC_EMB_NADDR16_LO
6983 case elfcpp::R_PPC64_ADDR16_HIGHER:
6984 case elfcpp::R_PPC64_TPREL16_HIGHER:
6985 Reloc::addr16_hi2(view, value);
6988 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6990 // R_PPC_EMB_NADDR16_HI
6992 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6993 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6994 Reloc::addr16_ha2(view, value);
6997 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6999 // R_PPC_EMB_NADDR16_HA
7001 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7002 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7003 Reloc::addr16_hi3(view, value);
7006 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7010 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7011 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7012 Reloc::addr16_ha3(view, value);
7015 case elfcpp::R_PPC64_DTPREL16_DS:
7016 case elfcpp::R_PPC64_DTPREL16_LO_DS:
7018 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7020 case elfcpp::R_PPC64_TPREL16_DS:
7021 case elfcpp::R_PPC64_TPREL16_LO_DS:
7023 // R_PPC_TLSGD, R_PPC_TLSLD
7025 case elfcpp::R_PPC64_ADDR16_DS:
7026 case elfcpp::R_PPC64_ADDR16_LO_DS:
7027 case elfcpp::R_PPC64_TOC16_DS:
7028 case elfcpp::R_PPC64_TOC16_LO_DS:
7029 case elfcpp::R_PPC64_GOT16_DS:
7030 case elfcpp::R_PPC64_GOT16_LO_DS:
7031 case elfcpp::R_PPC64_SECTOFF_DS:
7032 case elfcpp::R_PPC64_SECTOFF_LO_DS:
7033 status = Reloc::addr16_ds(view, value, overflow);
7036 case elfcpp::R_POWERPC_ADDR14:
7037 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7038 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7039 case elfcpp::R_POWERPC_REL14:
7040 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7041 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7042 status = Reloc::addr14(view, value, overflow);
7045 case elfcpp::R_POWERPC_COPY:
7046 case elfcpp::R_POWERPC_GLOB_DAT:
7047 case elfcpp::R_POWERPC_JMP_SLOT:
7048 case elfcpp::R_POWERPC_RELATIVE:
7049 case elfcpp::R_POWERPC_DTPMOD:
7050 case elfcpp::R_PPC64_JMP_IREL:
7051 case elfcpp::R_POWERPC_IRELATIVE:
7052 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7053 _("unexpected reloc %u in object file"),
7057 case elfcpp::R_PPC_EMB_SDA21:
7062 // R_PPC64_TOCSAVE. For the time being this can be ignored.
7066 case elfcpp::R_PPC_EMB_SDA2I16:
7067 case elfcpp::R_PPC_EMB_SDA2REL:
7070 // R_PPC64_TLSGD, R_PPC64_TLSLD
7073 case elfcpp::R_POWERPC_PLT32:
7074 case elfcpp::R_POWERPC_PLTREL32:
7075 case elfcpp::R_POWERPC_PLT16_LO:
7076 case elfcpp::R_POWERPC_PLT16_HI:
7077 case elfcpp::R_POWERPC_PLT16_HA:
7078 case elfcpp::R_PPC_SDAREL16:
7079 case elfcpp::R_POWERPC_ADDR30:
7080 case elfcpp::R_PPC64_PLT64:
7081 case elfcpp::R_PPC64_PLTREL64:
7082 case elfcpp::R_PPC64_PLTGOT16:
7083 case elfcpp::R_PPC64_PLTGOT16_LO:
7084 case elfcpp::R_PPC64_PLTGOT16_HI:
7085 case elfcpp::R_PPC64_PLTGOT16_HA:
7086 case elfcpp::R_PPC64_PLT16_LO_DS:
7087 case elfcpp::R_PPC64_PLTGOT16_DS:
7088 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7089 case elfcpp::R_PPC_EMB_RELSEC16:
7090 case elfcpp::R_PPC_EMB_RELST_LO:
7091 case elfcpp::R_PPC_EMB_RELST_HI:
7092 case elfcpp::R_PPC_EMB_RELST_HA:
7093 case elfcpp::R_PPC_EMB_BIT_FLD:
7094 case elfcpp::R_PPC_EMB_RELSDA:
7095 case elfcpp::R_PPC_TOC16:
7098 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7099 _("unsupported reloc %u"),
7103 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
7104 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7105 _("relocation overflow"));
7110 // Relocate section data.
7112 template<int size, bool big_endian>
7114 Target_powerpc<size, big_endian>::relocate_section(
7115 const Relocate_info<size, big_endian>* relinfo,
7116 unsigned int sh_type,
7117 const unsigned char* prelocs,
7119 Output_section* output_section,
7120 bool needs_special_offset_handling,
7121 unsigned char* view,
7123 section_size_type view_size,
7124 const Reloc_symbol_changes* reloc_symbol_changes)
7126 typedef Target_powerpc<size, big_endian> Powerpc;
7127 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7128 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7129 Powerpc_comdat_behavior;
7131 gold_assert(sh_type == elfcpp::SHT_RELA);
7133 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7134 Powerpc_relocate, Powerpc_comdat_behavior>(
7140 needs_special_offset_handling,
7144 reloc_symbol_changes);
7147 class Powerpc_scan_relocatable_reloc
7150 // Return the strategy to use for a local symbol which is not a
7151 // section symbol, given the relocation type.
7152 inline Relocatable_relocs::Reloc_strategy
7153 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7155 if (r_type == 0 && r_sym == 0)
7156 return Relocatable_relocs::RELOC_DISCARD;
7157 return Relocatable_relocs::RELOC_COPY;
7160 // Return the strategy to use for a local symbol which is a section
7161 // symbol, given the relocation type.
7162 inline Relocatable_relocs::Reloc_strategy
7163 local_section_strategy(unsigned int, Relobj*)
7165 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7168 // Return the strategy to use for a global symbol, given the
7169 // relocation type, the object, and the symbol index.
7170 inline Relocatable_relocs::Reloc_strategy
7171 global_strategy(unsigned int r_type, Relobj*, unsigned int)
7173 if (r_type == elfcpp::R_PPC_PLTREL24)
7174 return Relocatable_relocs::RELOC_SPECIAL;
7175 return Relocatable_relocs::RELOC_COPY;
7179 // Scan the relocs during a relocatable link.
7181 template<int size, bool big_endian>
7183 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7184 Symbol_table* symtab,
7186 Sized_relobj_file<size, big_endian>* object,
7187 unsigned int data_shndx,
7188 unsigned int sh_type,
7189 const unsigned char* prelocs,
7191 Output_section* output_section,
7192 bool needs_special_offset_handling,
7193 size_t local_symbol_count,
7194 const unsigned char* plocal_symbols,
7195 Relocatable_relocs* rr)
7197 gold_assert(sh_type == elfcpp::SHT_RELA);
7199 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7200 Powerpc_scan_relocatable_reloc>(
7208 needs_special_offset_handling,
7214 // Emit relocations for a section.
7215 // This is a modified version of the function by the same name in
7216 // target-reloc.h. Using relocate_special_relocatable for
7217 // R_PPC_PLTREL24 would require duplication of the entire body of the
7218 // loop, so we may as well duplicate the whole thing.
7220 template<int size, bool big_endian>
7222 Target_powerpc<size, big_endian>::relocate_relocs(
7223 const Relocate_info<size, big_endian>* relinfo,
7224 unsigned int sh_type,
7225 const unsigned char* prelocs,
7227 Output_section* output_section,
7228 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7229 const Relocatable_relocs* rr,
7231 Address view_address,
7233 unsigned char* reloc_view,
7234 section_size_type reloc_view_size)
7236 gold_assert(sh_type == elfcpp::SHT_RELA);
7238 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7240 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7242 const int reloc_size
7243 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7245 Powerpc_relobj<size, big_endian>* const object
7246 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7247 const unsigned int local_count = object->local_symbol_count();
7248 unsigned int got2_shndx = object->got2_shndx();
7249 Address got2_addend = 0;
7250 if (got2_shndx != 0)
7252 got2_addend = object->get_output_section_offset(got2_shndx);
7253 gold_assert(got2_addend != invalid_address);
7256 unsigned char* pwrite = reloc_view;
7257 bool zap_next = false;
7258 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7260 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7261 if (strategy == Relocatable_relocs::RELOC_DISCARD)
7264 Reltype reloc(prelocs);
7265 Reltype_write reloc_write(pwrite);
7267 Address offset = reloc.get_r_offset();
7268 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7269 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7270 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7271 const unsigned int orig_r_sym = r_sym;
7272 typename elfcpp::Elf_types<size>::Elf_Swxword addend
7273 = reloc.get_r_addend();
7274 const Symbol* gsym = NULL;
7278 // We could arrange to discard these and other relocs for
7279 // tls optimised sequences in the strategy methods, but for
7280 // now do as BFD ld does.
7281 r_type = elfcpp::R_POWERPC_NONE;
7285 // Get the new symbol index.
7286 if (r_sym < local_count)
7290 case Relocatable_relocs::RELOC_COPY:
7291 case Relocatable_relocs::RELOC_SPECIAL:
7294 r_sym = object->symtab_index(r_sym);
7295 gold_assert(r_sym != -1U);
7299 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7301 // We are adjusting a section symbol. We need to find
7302 // the symbol table index of the section symbol for
7303 // the output section corresponding to input section
7304 // in which this symbol is defined.
7305 gold_assert(r_sym < local_count);
7307 unsigned int shndx =
7308 object->local_symbol_input_shndx(r_sym, &is_ordinary);
7309 gold_assert(is_ordinary);
7310 Output_section* os = object->output_section(shndx);
7311 gold_assert(os != NULL);
7312 gold_assert(os->needs_symtab_index());
7313 r_sym = os->symtab_index();
7323 gsym = object->global_symbol(r_sym);
7324 gold_assert(gsym != NULL);
7325 if (gsym->is_forwarder())
7326 gsym = relinfo->symtab->resolve_forwards(gsym);
7328 gold_assert(gsym->has_symtab_index());
7329 r_sym = gsym->symtab_index();
7332 // Get the new offset--the location in the output section where
7333 // this relocation should be applied.
7334 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7335 offset += offset_in_output_section;
7338 section_offset_type sot_offset =
7339 convert_types<section_offset_type, Address>(offset);
7340 section_offset_type new_sot_offset =
7341 output_section->output_offset(object, relinfo->data_shndx,
7343 gold_assert(new_sot_offset != -1);
7344 offset = new_sot_offset;
7347 // In an object file, r_offset is an offset within the section.
7348 // In an executable or dynamic object, generated by
7349 // --emit-relocs, r_offset is an absolute address.
7350 if (!parameters->options().relocatable())
7352 offset += view_address;
7353 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7354 offset -= offset_in_output_section;
7357 // Handle the reloc addend based on the strategy.
7358 if (strategy == Relocatable_relocs::RELOC_COPY)
7360 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7362 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7363 addend = psymval->value(object, addend);
7365 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7367 if (addend >= 32768)
7368 addend += got2_addend;
7373 if (!parameters->options().relocatable())
7375 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7376 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7377 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7378 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7380 // First instruction of a global dynamic sequence,
7382 const bool final = gsym == NULL || gsym->final_value_is_known();
7383 switch (this->optimize_tls_gd(final))
7385 case tls::TLSOPT_TO_IE:
7386 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7387 - elfcpp::R_POWERPC_GOT_TLSGD16);
7389 case tls::TLSOPT_TO_LE:
7390 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7391 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7392 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7395 r_type = elfcpp::R_POWERPC_NONE;
7396 offset -= 2 * big_endian;
7403 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7404 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7405 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7406 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7408 // First instruction of a local dynamic sequence,
7410 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7412 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7413 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7415 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7416 const Output_section* os = relinfo->layout->tls_segment()
7418 gold_assert(os != NULL);
7419 gold_assert(os->needs_symtab_index());
7420 r_sym = os->symtab_index();
7421 addend = dtp_offset;
7425 r_type = elfcpp::R_POWERPC_NONE;
7426 offset -= 2 * big_endian;
7430 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7431 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7432 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7433 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7435 // First instruction of initial exec sequence.
7436 const bool final = gsym == NULL || gsym->final_value_is_known();
7437 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7439 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7440 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7441 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7444 r_type = elfcpp::R_POWERPC_NONE;
7445 offset -= 2 * big_endian;
7449 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7450 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7452 // Second instruction of a global dynamic sequence,
7453 // the __tls_get_addr call
7454 const bool final = gsym == NULL || gsym->final_value_is_known();
7455 switch (this->optimize_tls_gd(final))
7457 case tls::TLSOPT_TO_IE:
7458 r_type = elfcpp::R_POWERPC_NONE;
7461 case tls::TLSOPT_TO_LE:
7462 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7463 offset += 2 * big_endian;
7470 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7471 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7473 // Second instruction of a local dynamic sequence,
7474 // the __tls_get_addr call
7475 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7477 const Output_section* os = relinfo->layout->tls_segment()
7479 gold_assert(os != NULL);
7480 gold_assert(os->needs_symtab_index());
7481 r_sym = os->symtab_index();
7482 addend = dtp_offset;
7483 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7484 offset += 2 * big_endian;
7488 else if (r_type == elfcpp::R_POWERPC_TLS)
7490 // Second instruction of an initial exec sequence
7491 const bool final = gsym == NULL || gsym->final_value_is_known();
7492 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7494 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7495 offset += 2 * big_endian;
7500 reloc_write.put_r_offset(offset);
7501 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
7502 reloc_write.put_r_addend(addend);
7504 pwrite += reloc_size;
7507 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
7508 == reloc_view_size);
7511 // Return the value to use for a dynamic symbol which requires special
7512 // treatment. This is how we support equality comparisons of function
7513 // pointers across shared library boundaries, as described in the
7514 // processor specific ABI supplement.
7516 template<int size, bool big_endian>
7518 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7522 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7523 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7524 p != this->stub_tables_.end();
7527 Address off = (*p)->find_plt_call_entry(gsym);
7528 if (off != invalid_address)
7529 return (*p)->stub_address() + off;
7535 // Return the PLT address to use for a local symbol.
7536 template<int size, bool big_endian>
7538 Target_powerpc<size, big_endian>::do_plt_address_for_local(
7539 const Relobj* object,
7540 unsigned int symndx) const
7544 const Sized_relobj<size, big_endian>* relobj
7545 = static_cast<const Sized_relobj<size, big_endian>*>(object);
7546 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7547 p != this->stub_tables_.end();
7550 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
7552 if (off != invalid_address)
7553 return (*p)->stub_address() + off;
7559 // Return the PLT address to use for a global symbol.
7560 template<int size, bool big_endian>
7562 Target_powerpc<size, big_endian>::do_plt_address_for_global(
7563 const Symbol* gsym) const
7567 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7568 p != this->stub_tables_.end();
7571 Address off = (*p)->find_plt_call_entry(gsym);
7572 if (off != invalid_address)
7573 return (*p)->stub_address() + off;
7579 // Return the offset to use for the GOT_INDX'th got entry which is
7580 // for a local tls symbol specified by OBJECT, SYMNDX.
7581 template<int size, bool big_endian>
7583 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
7584 const Relobj* object,
7585 unsigned int symndx,
7586 unsigned int got_indx) const
7588 const Powerpc_relobj<size, big_endian>* ppc_object
7589 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
7590 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
7592 for (Got_type got_type = GOT_TYPE_TLSGD;
7593 got_type <= GOT_TYPE_TPREL;
7594 got_type = Got_type(got_type + 1))
7595 if (ppc_object->local_has_got_offset(symndx, got_type))
7597 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
7598 if (got_type == GOT_TYPE_TLSGD)
7600 if (off == got_indx * (size / 8))
7602 if (got_type == GOT_TYPE_TPREL)
7612 // Return the offset to use for the GOT_INDX'th got entry which is
7613 // for global tls symbol GSYM.
7614 template<int size, bool big_endian>
7616 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
7618 unsigned int got_indx) const
7620 if (gsym->type() == elfcpp::STT_TLS)
7622 for (Got_type got_type = GOT_TYPE_TLSGD;
7623 got_type <= GOT_TYPE_TPREL;
7624 got_type = Got_type(got_type + 1))
7625 if (gsym->has_got_offset(got_type))
7627 unsigned int off = gsym->got_offset(got_type);
7628 if (got_type == GOT_TYPE_TLSGD)
7630 if (off == got_indx * (size / 8))
7632 if (got_type == GOT_TYPE_TPREL)
7642 // The selector for powerpc object files.
7644 template<int size, bool big_endian>
7645 class Target_selector_powerpc : public Target_selector
7648 Target_selector_powerpc()
7649 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
7652 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
7653 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
7655 ? (big_endian ? "elf64ppc" : "elf64lppc")
7656 : (big_endian ? "elf32ppc" : "elf32lppc")))
7660 do_instantiate_target()
7661 { return new Target_powerpc<size, big_endian>(); }
7664 Target_selector_powerpc<32, true> target_selector_ppc32;
7665 Target_selector_powerpc<32, false> target_selector_ppc32le;
7666 Target_selector_powerpc<64, true> target_selector_ppc64;
7667 Target_selector_powerpc<64, false> target_selector_ppc64le;
7669 // Instantiate these constants for -O0
7670 template<int size, bool big_endian>
7671 const int Output_data_glink<size, big_endian>::pltresolve_size;
7672 template<int size, bool big_endian>
7673 const typename Stub_table<size, big_endian>::Address
7674 Stub_table<size, big_endian>::invalid_address;
7675 template<int size, bool big_endian>
7676 const typename Target_powerpc<size, big_endian>::Address
7677 Target_powerpc<size, big_endian>::invalid_address;
7679 } // End anonymous namespace.