1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 // and David Edelsohn <edelsohn@gnu.org>
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
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>
65 template<int size, bool big_endian>
66 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
69 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
70 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
71 typedef Unordered_map<Address, Section_refs> Access_from;
73 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
74 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
75 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
76 special_(0), has_small_toc_reloc_(false), opd_valid_(false),
77 opd_ent_(), access_from_map_(), has14_(), stub_table_()
83 // The .got2 section shndx.
88 return this->special_;
93 // The .opd section shndx.
100 return this->special_;
103 // Init OPD entry arrays.
105 init_opd(size_t opd_size)
107 size_t count = this->opd_ent_ndx(opd_size);
108 this->opd_ent_.resize(count);
111 // Return section and offset of function entry for .opd + R_OFF.
113 get_opd_ent(Address r_off, Address* value = NULL) const
115 size_t ndx = this->opd_ent_ndx(r_off);
116 gold_assert(ndx < this->opd_ent_.size());
117 gold_assert(this->opd_ent_[ndx].shndx != 0);
119 *value = this->opd_ent_[ndx].off;
120 return this->opd_ent_[ndx].shndx;
123 // Set section and offset of function entry for .opd + R_OFF.
125 set_opd_ent(Address r_off, unsigned int shndx, Address value)
127 size_t ndx = this->opd_ent_ndx(r_off);
128 gold_assert(ndx < this->opd_ent_.size());
129 this->opd_ent_[ndx].shndx = shndx;
130 this->opd_ent_[ndx].off = value;
133 // Return discard flag for .opd + R_OFF.
135 get_opd_discard(Address r_off) const
137 size_t ndx = this->opd_ent_ndx(r_off);
138 gold_assert(ndx < this->opd_ent_.size());
139 return this->opd_ent_[ndx].discard;
142 // Set discard flag for .opd + R_OFF.
144 set_opd_discard(Address r_off)
146 size_t ndx = this->opd_ent_ndx(r_off);
147 gold_assert(ndx < this->opd_ent_.size());
148 this->opd_ent_[ndx].discard = true;
153 { return this->opd_valid_; }
157 { this->opd_valid_ = true; }
159 // Examine .rela.opd to build info about function entry points.
161 scan_opd_relocs(size_t reloc_count,
162 const unsigned char* prelocs,
163 const unsigned char* plocal_syms);
165 // Perform the Sized_relobj_file method, then set up opd info from
168 do_read_relocs(Read_relocs_data*);
171 do_find_special_sections(Read_symbols_data* sd);
173 // Adjust this local symbol value. Return false if the symbol
174 // should be discarded from the output file.
176 do_adjust_local_symbol(Symbol_value<size>* lv) const
178 if (size == 64 && this->opd_shndx() != 0)
181 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
183 if (this->get_opd_discard(lv->input_value()))
191 { return &this->access_from_map_; }
193 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
194 // section at DST_OFF.
196 add_reference(Object* src_obj,
197 unsigned int src_indx,
198 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
200 Section_id src_id(src_obj, src_indx);
201 this->access_from_map_[dst_off].insert(src_id);
204 // Add a reference to the code section specified by the .opd entry
207 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
209 size_t ndx = this->opd_ent_ndx(dst_off);
210 if (ndx >= this->opd_ent_.size())
211 this->opd_ent_.resize(ndx + 1);
212 this->opd_ent_[ndx].gc_mark = true;
216 process_gc_mark(Symbol_table* symtab)
218 for (size_t i = 0; i < this->opd_ent_.size(); i++)
219 if (this->opd_ent_[i].gc_mark)
221 unsigned int shndx = this->opd_ent_[i].shndx;
222 symtab->gc()->worklist().push(Section_id(this, shndx));
226 // Return offset in output GOT section that this object will use
227 // as a TOC pointer. Won't be just a constant with multi-toc support.
229 toc_base_offset() const
233 set_has_small_toc_reloc()
234 { has_small_toc_reloc_ = true; }
237 has_small_toc_reloc() const
238 { return has_small_toc_reloc_; }
241 set_has_14bit_branch(unsigned int shndx)
243 if (shndx >= this->has14_.size())
244 this->has14_.resize(shndx + 1);
245 this->has14_[shndx] = true;
249 has_14bit_branch(unsigned int shndx) const
250 { return shndx < this->has14_.size() && this->has14_[shndx]; }
253 set_stub_table(unsigned int shndx, Stub_table<size, big_endian>* stub_table)
255 if (shndx >= this->stub_table_.size())
256 this->stub_table_.resize(shndx + 1);
257 this->stub_table_[shndx] = stub_table;
260 Stub_table<size, big_endian>*
261 stub_table(unsigned int shndx)
263 if (shndx < this->stub_table_.size())
264 return this->stub_table_[shndx];
277 // Return index into opd_ent_ array for .opd entry at OFF.
278 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
279 // apart when the language doesn't use the last 8-byte word, the
280 // environment pointer. Thus dividing the entry section offset by
281 // 16 will give an index into opd_ent_ that works for either layout
282 // of .opd. (It leaves some elements of the vector unused when .opd
283 // entries are spaced 24 bytes apart, but we don't know the spacing
284 // until relocations are processed, and in any case it is possible
285 // for an object to have some entries spaced 16 bytes apart and
286 // others 24 bytes apart.)
288 opd_ent_ndx(size_t off) const
291 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
292 unsigned int special_;
294 // For 64-bit, whether this object uses small model relocs to access
296 bool has_small_toc_reloc_;
298 // Set at the start of gc_process_relocs, when we know opd_ent_
299 // vector is valid. The flag could be made atomic and set in
300 // do_read_relocs with memory_order_release and then tested with
301 // memory_order_acquire, potentially resulting in fewer entries in
305 // The first 8-byte word of an OPD entry gives the address of the
306 // entry point of the function. Relocatable object files have a
307 // relocation on this word. The following vector records the
308 // section and offset specified by these relocations.
309 std::vector<Opd_ent> opd_ent_;
311 // References made to this object's .opd section when running
312 // gc_process_relocs for another object, before the opd_ent_ vector
313 // is valid for this object.
314 Access_from access_from_map_;
316 // Whether input section has a 14-bit branch reloc.
317 std::vector<bool> has14_;
319 // The stub table to use for a given input section.
320 std::vector<Stub_table<size, big_endian>*> stub_table_;
323 template<int size, bool big_endian>
324 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
327 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
329 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
330 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
331 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
332 opd_shndx_(0), opd_ent_()
338 // Call Sized_dynobj::do_read_symbols to read the symbols then
339 // read .opd from a dynamic object, filling in opd_ent_ vector,
341 do_read_symbols(Read_symbols_data*);
343 // The .opd section shndx.
347 return this->opd_shndx_;
350 // The .opd section address.
354 return this->opd_address_;
357 // Init OPD entry arrays.
359 init_opd(size_t opd_size)
361 size_t count = this->opd_ent_ndx(opd_size);
362 this->opd_ent_.resize(count);
365 // Return section and offset of function entry for .opd + R_OFF.
367 get_opd_ent(Address r_off, Address* value = NULL) const
369 size_t ndx = this->opd_ent_ndx(r_off);
370 gold_assert(ndx < this->opd_ent_.size());
371 gold_assert(this->opd_ent_[ndx].shndx != 0);
373 *value = this->opd_ent_[ndx].off;
374 return this->opd_ent_[ndx].shndx;
377 // Set section and offset of function entry for .opd + R_OFF.
379 set_opd_ent(Address r_off, unsigned int shndx, Address value)
381 size_t ndx = this->opd_ent_ndx(r_off);
382 gold_assert(ndx < this->opd_ent_.size());
383 this->opd_ent_[ndx].shndx = shndx;
384 this->opd_ent_[ndx].off = value;
388 // Used to specify extent of executable sections.
391 Sec_info(Address start_, Address len_, unsigned int shndx_)
392 : start(start_), len(len_), shndx(shndx_)
396 operator<(const Sec_info& that) const
397 { return this->start < that.start; }
410 // Return index into opd_ent_ array for .opd entry at OFF.
412 opd_ent_ndx(size_t off) const
415 // For 64-bit the .opd section shndx and address.
416 unsigned int opd_shndx_;
417 Address opd_address_;
419 // The first 8-byte word of an OPD entry gives the address of the
420 // entry point of the function. Records the section and offset
421 // corresponding to the address. Note that in dynamic objects,
422 // offset is *not* relative to the section.
423 std::vector<Opd_ent> opd_ent_;
426 template<int size, bool big_endian>
427 class Target_powerpc : public Sized_target<size, big_endian>
431 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
432 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
433 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
434 static const Address invalid_address = static_cast<Address>(0) - 1;
435 // Offset of tp and dtp pointers from start of TLS block.
436 static const Address tp_offset = 0x7000;
437 static const Address dtp_offset = 0x8000;
440 : Sized_target<size, big_endian>(&powerpc_info),
441 got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
442 glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
443 dynbss_(NULL), tlsld_got_offset_(-1U),
444 stub_tables_(), branch_lookup_table_(), branch_info_(),
445 plt_thread_safe_(false)
449 // Process the relocations to determine unreferenced sections for
450 // garbage collection.
452 gc_process_relocs(Symbol_table* symtab,
454 Sized_relobj_file<size, big_endian>* object,
455 unsigned int data_shndx,
456 unsigned int sh_type,
457 const unsigned char* prelocs,
459 Output_section* output_section,
460 bool needs_special_offset_handling,
461 size_t local_symbol_count,
462 const unsigned char* plocal_symbols);
464 // Scan the relocations to look for symbol adjustments.
466 scan_relocs(Symbol_table* symtab,
468 Sized_relobj_file<size, big_endian>* object,
469 unsigned int data_shndx,
470 unsigned int sh_type,
471 const unsigned char* prelocs,
473 Output_section* output_section,
474 bool needs_special_offset_handling,
475 size_t local_symbol_count,
476 const unsigned char* plocal_symbols);
478 // Map input .toc section to output .got section.
480 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
482 if (size == 64 && strcmp(name, ".toc") == 0)
490 // Provide linker defined save/restore functions.
492 define_save_restore_funcs(Layout*, Symbol_table*);
494 // No stubs unless a final link.
497 { return !parameters->options().relocatable(); }
500 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
503 do_plt_fde_location(const Output_data*, unsigned char*,
504 uint64_t*, off_t*) const;
506 // Stash info about branches, for stub generation.
508 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
509 unsigned int data_shndx, Address r_offset,
510 unsigned int r_type, unsigned int r_sym, Address addend)
512 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
513 this->branch_info_.push_back(info);
514 if (r_type == elfcpp::R_POWERPC_REL14
515 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
516 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
517 ppc_object->set_has_14bit_branch(data_shndx);
520 Stub_table<size, big_endian>*
524 do_define_standard_symbols(Symbol_table*, Layout*);
526 // Finalize the sections.
528 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
530 // Return the value to use for a dynamic which requires special
533 do_dynsym_value(const Symbol*) const;
535 // Return the PLT address to use for a local symbol.
537 do_plt_address_for_local(const Relobj*, unsigned int) const;
539 // Return the PLT address to use for a global symbol.
541 do_plt_address_for_global(const Symbol*) const;
543 // Return the offset to use for the GOT_INDX'th got entry which is
544 // for a local tls symbol specified by OBJECT, SYMNDX.
546 do_tls_offset_for_local(const Relobj* object,
548 unsigned int got_indx) const;
550 // Return the offset to use for the GOT_INDX'th got entry which is
551 // for global tls symbol GSYM.
553 do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
556 do_function_location(Symbol_location*) const;
558 // Relocate a section.
560 relocate_section(const Relocate_info<size, big_endian>*,
561 unsigned int sh_type,
562 const unsigned char* prelocs,
564 Output_section* output_section,
565 bool needs_special_offset_handling,
567 Address view_address,
568 section_size_type view_size,
569 const Reloc_symbol_changes*);
571 // Scan the relocs during a relocatable link.
573 scan_relocatable_relocs(Symbol_table* symtab,
575 Sized_relobj_file<size, big_endian>* object,
576 unsigned int data_shndx,
577 unsigned int sh_type,
578 const unsigned char* prelocs,
580 Output_section* output_section,
581 bool needs_special_offset_handling,
582 size_t local_symbol_count,
583 const unsigned char* plocal_symbols,
584 Relocatable_relocs*);
586 // Emit relocations for a section.
588 relocate_relocs(const Relocate_info<size, big_endian>*,
589 unsigned int sh_type,
590 const unsigned char* prelocs,
592 Output_section* output_section,
593 typename elfcpp::Elf_types<size>::Elf_Off
594 offset_in_output_section,
595 const Relocatable_relocs*,
597 Address view_address,
599 unsigned char* reloc_view,
600 section_size_type reloc_view_size);
602 // Return whether SYM is defined by the ABI.
604 do_is_defined_by_abi(const Symbol* sym) const
606 return strcmp(sym->name(), "__tls_get_addr") == 0;
609 // Return the size of the GOT section.
613 gold_assert(this->got_ != NULL);
614 return this->got_->data_size();
617 // Get the PLT section.
618 const Output_data_plt_powerpc<size, big_endian>*
621 gold_assert(this->plt_ != NULL);
625 // Get the IPLT section.
626 const Output_data_plt_powerpc<size, big_endian>*
629 gold_assert(this->iplt_ != NULL);
633 // Get the .glink section.
634 const Output_data_glink<size, big_endian>*
635 glink_section() const
637 gold_assert(this->glink_ != NULL);
641 bool has_glink() const
642 { return this->glink_ != NULL; }
644 // Get the GOT section.
645 const Output_data_got_powerpc<size, big_endian>*
648 gold_assert(this->got_ != NULL);
652 // Get the GOT section, creating it if necessary.
653 Output_data_got_powerpc<size, big_endian>*
654 got_section(Symbol_table*, Layout*);
657 do_make_elf_object(const std::string&, Input_file*, off_t,
658 const elfcpp::Ehdr<size, big_endian>&);
660 // Return the number of entries in the GOT.
662 got_entry_count() const
664 if (this->got_ == NULL)
666 return this->got_size() / (size / 8);
669 // Return the number of entries in the PLT.
671 plt_entry_count() const;
673 // Return the offset of the first non-reserved PLT entry.
675 first_plt_entry_offset() const;
677 // Return the size of each PLT entry.
679 plt_entry_size() const;
681 // Add any special sections for this symbol to the gc work list.
682 // For powerpc64, this adds the code section of a function
685 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
687 // Handle target specific gc actions when adding a gc reference from
688 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
689 // and DST_OFF. For powerpc64, this adds a referenc to the code
690 // section of a function descriptor.
692 do_gc_add_reference(Symbol_table* symtab,
694 unsigned int src_shndx,
696 unsigned int dst_shndx,
697 Address dst_off) const;
699 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
702 { return this->stub_tables_; }
704 const Output_data_brlt_powerpc<size, big_endian>*
706 { return this->brlt_section_; }
709 add_branch_lookup_table(Address to)
711 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
712 this->branch_lookup_table_.insert(std::make_pair(to, off));
716 find_branch_lookup_table(Address to)
718 typename Branch_lookup_table::const_iterator p
719 = this->branch_lookup_table_.find(to);
720 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
724 write_branch_lookup_table(unsigned char *oview)
726 for (typename Branch_lookup_table::const_iterator p
727 = this->branch_lookup_table_.begin();
728 p != this->branch_lookup_table_.end();
731 elfcpp::Swap<32, big_endian>::writeval(oview + p->second, p->first);
736 plt_thread_safe() const
737 { return this->plt_thread_safe_; }
753 : tls_get_addr_(NOT_EXPECTED),
754 relinfo_(NULL), relnum_(0), r_offset_(0)
759 if (this->tls_get_addr_ != NOT_EXPECTED)
766 if (this->relinfo_ != NULL)
767 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
768 _("missing expected __tls_get_addr call"));
772 expect_tls_get_addr_call(
773 const Relocate_info<size, big_endian>* relinfo,
777 this->tls_get_addr_ = EXPECTED;
778 this->relinfo_ = relinfo;
779 this->relnum_ = relnum;
780 this->r_offset_ = r_offset;
784 expect_tls_get_addr_call()
785 { this->tls_get_addr_ = EXPECTED; }
788 skip_next_tls_get_addr_call()
789 {this->tls_get_addr_ = SKIP; }
792 maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
794 bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
795 || r_type == elfcpp::R_PPC_PLTREL24)
797 && strcmp(gsym->name(), "__tls_get_addr") == 0);
798 Tls_get_addr last_tls = this->tls_get_addr_;
799 this->tls_get_addr_ = NOT_EXPECTED;
800 if (is_tls_call && last_tls != EXPECTED)
802 else if (!is_tls_call && last_tls != NOT_EXPECTED)
811 // What we're up to regarding calls to __tls_get_addr.
812 // On powerpc, the branch and link insn making a call to
813 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
814 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
815 // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
816 // The marker relocation always comes first, and has the same
817 // symbol as the reloc on the insn setting up the __tls_get_addr
818 // argument. This ties the arg setup insn with the call insn,
819 // allowing ld to safely optimize away the call. We check that
820 // every call to __tls_get_addr has a marker relocation, and that
821 // every marker relocation is on a call to __tls_get_addr.
822 Tls_get_addr tls_get_addr_;
823 // Info about the last reloc for error message.
824 const Relocate_info<size, big_endian>* relinfo_;
829 // The class which scans relocations.
830 class Scan : protected Track_tls
833 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
836 : Track_tls(), issued_non_pic_error_(false)
840 get_reference_flags(unsigned int r_type);
843 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
844 Sized_relobj_file<size, big_endian>* object,
845 unsigned int data_shndx,
846 Output_section* output_section,
847 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
848 const elfcpp::Sym<size, big_endian>& lsym,
852 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
853 Sized_relobj_file<size, big_endian>* object,
854 unsigned int data_shndx,
855 Output_section* output_section,
856 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
860 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
862 Sized_relobj_file<size, big_endian>* ,
865 const elfcpp::Rela<size, big_endian>& ,
867 const elfcpp::Sym<size, big_endian>&)
871 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
873 Sized_relobj_file<size, big_endian>* ,
876 const elfcpp::Rela<size,
878 unsigned int , Symbol*)
883 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
884 unsigned int r_type);
887 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
888 unsigned int r_type, Symbol*);
891 generate_tls_call(Symbol_table* symtab, Layout* layout,
892 Target_powerpc* target);
895 check_non_pic(Relobj*, unsigned int r_type);
898 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
899 unsigned int r_type);
901 // Whether we have issued an error about a non-PIC compilation.
902 bool issued_non_pic_error_;
906 symval_for_branch(const Symbol_table* symtab, Address value,
907 const Sized_symbol<size>* gsym,
908 Powerpc_relobj<size, big_endian>* object,
909 unsigned int *dest_shndx);
911 // The class which implements relocation.
912 class Relocate : protected Track_tls
915 // Use 'at' branch hints when true, 'y' when false.
916 // FIXME maybe: set this with an option.
917 static const bool is_isa_v2 = true;
923 // Do a relocation. Return false if the caller should not issue
924 // any warnings about this relocation.
926 relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
927 Output_section*, size_t relnum,
928 const elfcpp::Rela<size, big_endian>&,
929 unsigned int r_type, const Sized_symbol<size>*,
930 const Symbol_value<size>*,
932 typename elfcpp::Elf_types<size>::Elf_Addr,
936 class Relocate_comdat_behavior
939 // Decide what the linker should do for relocations that refer to
940 // discarded comdat sections.
941 inline Comdat_behavior
942 get(const char* name)
944 gold::Default_comdat_behavior default_behavior;
945 Comdat_behavior ret = default_behavior.get(name);
946 if (ret == CB_WARNING)
949 && (strcmp(name, ".fixup") == 0
950 || strcmp(name, ".got2") == 0))
953 && (strcmp(name, ".opd") == 0
954 || strcmp(name, ".toc") == 0
955 || strcmp(name, ".toc1") == 0))
962 // A class which returns the size required for a relocation type,
963 // used while scanning relocs during a relocatable link.
964 class Relocatable_size_for_reloc
968 get_size_for_reloc(unsigned int, Relobj*)
975 // Optimize the TLS relocation type based on what we know about the
976 // symbol. IS_FINAL is true if the final address of this symbol is
977 // known at link time.
979 tls::Tls_optimization
980 optimize_tls_gd(bool is_final)
982 // If we are generating a shared library, then we can't do anything
984 if (parameters->options().shared())
985 return tls::TLSOPT_NONE;
988 return tls::TLSOPT_TO_IE;
989 return tls::TLSOPT_TO_LE;
992 tls::Tls_optimization
995 if (parameters->options().shared())
996 return tls::TLSOPT_NONE;
998 return tls::TLSOPT_TO_LE;
1001 tls::Tls_optimization
1002 optimize_tls_ie(bool is_final)
1004 if (!is_final || parameters->options().shared())
1005 return tls::TLSOPT_NONE;
1007 return tls::TLSOPT_TO_LE;
1012 make_glink_section(Layout*);
1014 // Create the PLT section.
1016 make_plt_section(Symbol_table*, Layout*);
1019 make_iplt_section(Symbol_table*, Layout*);
1022 make_brlt_section(Layout*);
1024 // Create a PLT entry for a global symbol.
1026 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1028 // Create a PLT entry for a local IFUNC symbol.
1030 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1031 Sized_relobj_file<size, big_endian>*,
1035 // Create a GOT entry for local dynamic __tls_get_addr.
1037 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1038 Sized_relobj_file<size, big_endian>* object);
1041 tlsld_got_offset() const
1043 return this->tlsld_got_offset_;
1046 // Get the dynamic reloc section, creating it if necessary.
1048 rela_dyn_section(Layout*);
1050 // Copy a relocation against a global symbol.
1052 copy_reloc(Symbol_table* symtab, Layout* layout,
1053 Sized_relobj_file<size, big_endian>* object,
1054 unsigned int shndx, Output_section* output_section,
1055 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1057 this->copy_relocs_.copy_reloc(symtab, layout,
1058 symtab->get_sized_symbol<size>(sym),
1059 object, shndx, output_section,
1060 reloc, this->rela_dyn_section(layout));
1063 // Look over all the input sections, deciding where to place stub.
1065 group_sections(Layout*, const Task*);
1067 // Sort output sections by address.
1068 struct Sort_sections
1071 operator()(const Output_section* sec1, const Output_section* sec2)
1072 { return sec1->address() < sec2->address(); }
1078 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1079 unsigned int data_shndx,
1081 unsigned int r_type,
1084 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1085 r_type_(r_type), r_sym_(r_sym), addend_(addend)
1091 // If this branch needs a plt call stub, or a long branch stub, make one.
1093 make_stub(Stub_table<size, big_endian>*,
1094 Stub_table<size, big_endian>*,
1095 Symbol_table*) const;
1098 // The branch location..
1099 Powerpc_relobj<size, big_endian>* object_;
1100 unsigned int shndx_;
1102 // ..and the branch type and destination.
1103 unsigned int r_type_;
1104 unsigned int r_sym_;
1108 // Information about this specific target which we pass to the
1109 // general Target structure.
1110 static Target::Target_info powerpc_info;
1112 // The types of GOT entries needed for this platform.
1113 // These values are exposed to the ABI in an incremental link.
1114 // Do not renumber existing values without changing the version
1115 // number of the .gnu_incremental_inputs section.
1119 GOT_TYPE_TLSGD, // double entry for @got@tlsgd
1120 GOT_TYPE_DTPREL, // entry for @got@dtprel
1121 GOT_TYPE_TPREL // entry for @got@tprel
1125 Output_data_got_powerpc<size, big_endian>* got_;
1127 Output_data_plt_powerpc<size, big_endian>* plt_;
1128 // The IPLT section.
1129 Output_data_plt_powerpc<size, big_endian>* iplt_;
1130 // Section holding long branch destinations.
1131 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1132 // The .glink section.
1133 Output_data_glink<size, big_endian>* glink_;
1134 // The dynamic reloc section.
1135 Reloc_section* rela_dyn_;
1136 // Relocs saved to avoid a COPY reloc.
1137 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1138 // Space for variables copied with a COPY reloc.
1139 Output_data_space* dynbss_;
1140 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1141 unsigned int tlsld_got_offset_;
1143 Stub_tables stub_tables_;
1144 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1145 Branch_lookup_table branch_lookup_table_;
1147 typedef std::vector<Branch_info> Branches;
1148 Branches branch_info_;
1150 bool plt_thread_safe_;
1154 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1157 true, // is_big_endian
1158 elfcpp::EM_PPC, // machine_code
1159 false, // has_make_symbol
1160 false, // has_resolve
1161 false, // has_code_fill
1162 true, // is_default_stack_executable
1163 false, // can_icf_inline_merge_sections
1165 "/usr/lib/ld.so.1", // dynamic_linker
1166 0x10000000, // default_text_segment_address
1167 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1168 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1169 false, // isolate_execinstr
1171 elfcpp::SHN_UNDEF, // small_common_shndx
1172 elfcpp::SHN_UNDEF, // large_common_shndx
1173 0, // small_common_section_flags
1174 0, // large_common_section_flags
1175 NULL, // attributes_section
1176 NULL // attributes_vendor
1180 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1183 false, // is_big_endian
1184 elfcpp::EM_PPC, // machine_code
1185 false, // has_make_symbol
1186 false, // has_resolve
1187 false, // has_code_fill
1188 true, // is_default_stack_executable
1189 false, // can_icf_inline_merge_sections
1191 "/usr/lib/ld.so.1", // dynamic_linker
1192 0x10000000, // default_text_segment_address
1193 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1194 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1195 false, // isolate_execinstr
1197 elfcpp::SHN_UNDEF, // small_common_shndx
1198 elfcpp::SHN_UNDEF, // large_common_shndx
1199 0, // small_common_section_flags
1200 0, // large_common_section_flags
1201 NULL, // attributes_section
1202 NULL // attributes_vendor
1206 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1209 true, // is_big_endian
1210 elfcpp::EM_PPC64, // machine_code
1211 false, // has_make_symbol
1212 false, // has_resolve
1213 false, // has_code_fill
1214 true, // is_default_stack_executable
1215 false, // can_icf_inline_merge_sections
1217 "/usr/lib/ld.so.1", // dynamic_linker
1218 0x10000000, // default_text_segment_address
1219 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1220 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1221 false, // isolate_execinstr
1223 elfcpp::SHN_UNDEF, // small_common_shndx
1224 elfcpp::SHN_UNDEF, // large_common_shndx
1225 0, // small_common_section_flags
1226 0, // large_common_section_flags
1227 NULL, // attributes_section
1228 NULL // attributes_vendor
1232 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1235 false, // is_big_endian
1236 elfcpp::EM_PPC64, // machine_code
1237 false, // has_make_symbol
1238 false, // has_resolve
1239 false, // has_code_fill
1240 true, // is_default_stack_executable
1241 false, // can_icf_inline_merge_sections
1243 "/usr/lib/ld.so.1", // dynamic_linker
1244 0x10000000, // default_text_segment_address
1245 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1246 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1247 false, // isolate_execinstr
1249 elfcpp::SHN_UNDEF, // small_common_shndx
1250 elfcpp::SHN_UNDEF, // large_common_shndx
1251 0, // small_common_section_flags
1252 0, // large_common_section_flags
1253 NULL, // attributes_section
1254 NULL // attributes_vendor
1258 is_branch_reloc(unsigned int r_type)
1260 return (r_type == elfcpp::R_POWERPC_REL24
1261 || r_type == elfcpp::R_PPC_PLTREL24
1262 || r_type == elfcpp::R_PPC_LOCAL24PC
1263 || r_type == elfcpp::R_POWERPC_REL14
1264 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1265 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1266 || r_type == elfcpp::R_POWERPC_ADDR24
1267 || r_type == elfcpp::R_POWERPC_ADDR14
1268 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1269 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1272 // If INSN is an opcode that may be used with an @tls operand, return
1273 // the transformed insn for TLS optimisation, otherwise return 0. If
1274 // REG is non-zero only match an insn with RB or RA equal to REG.
1276 at_tls_transform(uint32_t insn, unsigned int reg)
1278 if ((insn & (0x3f << 26)) != 31 << 26)
1282 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1283 rtra = insn & ((1 << 26) - (1 << 16));
1284 else if (((insn >> 16) & 0x1f) == reg)
1285 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1289 if ((insn & (0x3ff << 1)) == 266 << 1)
1292 else if ((insn & (0x1f << 1)) == 23 << 1
1293 && ((insn & (0x1f << 6)) < 14 << 6
1294 || ((insn & (0x1f << 6)) >= 16 << 6
1295 && (insn & (0x1f << 6)) < 24 << 6)))
1296 // load and store indexed -> dform
1297 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1298 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1299 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1300 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1301 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1303 insn = (58 << 26) | 2;
1310 // Modified version of symtab.h class Symbol member
1311 // Given a direct absolute or pc-relative static relocation against
1312 // the global symbol, this function returns whether a dynamic relocation
1317 needs_dynamic_reloc(const Symbol* gsym, int flags)
1319 // No dynamic relocations in a static link!
1320 if (parameters->doing_static_link())
1323 // A reference to an undefined symbol from an executable should be
1324 // statically resolved to 0, and does not need a dynamic relocation.
1325 // This matches gnu ld behavior.
1326 if (gsym->is_undefined() && !parameters->options().shared())
1329 // A reference to an absolute symbol does not need a dynamic relocation.
1330 if (gsym->is_absolute())
1333 // An absolute reference within a position-independent output file
1334 // will need a dynamic relocation.
1335 if ((flags & Symbol::ABSOLUTE_REF)
1336 && parameters->options().output_is_position_independent())
1339 // A function call that can branch to a local PLT entry does not need
1340 // a dynamic relocation.
1341 if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
1344 // A reference to any PLT entry in a non-position-independent executable
1345 // does not need a dynamic relocation.
1346 // Except due to having function descriptors on powerpc64 we don't define
1347 // functions to their plt code in an executable, so this doesn't apply.
1349 && !parameters->options().output_is_position_independent()
1350 && gsym->has_plt_offset())
1353 // A reference to a symbol defined in a dynamic object or to a
1354 // symbol that is preemptible will need a dynamic relocation.
1355 if (gsym->is_from_dynobj()
1356 || gsym->is_undefined()
1357 || gsym->is_preemptible())
1360 // For all other cases, return FALSE.
1364 // Modified version of symtab.h class Symbol member
1365 // Whether we should use the PLT offset associated with a symbol for
1366 // a relocation. FLAGS is a set of Reference_flags.
1370 use_plt_offset(const Symbol* gsym, int flags)
1372 // If the symbol doesn't have a PLT offset, then naturally we
1373 // don't want to use it.
1374 if (!gsym->has_plt_offset())
1377 // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1378 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1381 // If we are going to generate a dynamic relocation, then we will
1382 // wind up using that, so no need to use the PLT entry.
1383 if (needs_dynamic_reloc<size>(gsym, flags))
1386 // If the symbol is from a dynamic object, we need to use the PLT
1388 if (gsym->is_from_dynobj())
1391 // If we are generating a shared object, and this symbol is
1392 // undefined or preemptible, we need to use the PLT entry.
1393 if (parameters->options().shared()
1394 && (gsym->is_undefined() || gsym->is_preemptible()))
1397 // If this is a call to a weak undefined symbol, we need to use
1398 // the PLT entry; the symbol may be defined by a library loaded
1400 if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1403 // Otherwise we can use the regular definition.
1407 template<int size, bool big_endian>
1408 class Powerpc_relocate_functions
1425 typedef Powerpc_relocate_functions<size, big_endian> This;
1426 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1428 template<int valsize>
1430 has_overflow_signed(Address value)
1432 // limit = 1 << (valsize - 1) without shift count exceeding size of type
1433 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1434 limit <<= ((valsize - 1) >> 1);
1435 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1436 return value + limit > (limit << 1) - 1;
1439 template<int valsize>
1441 has_overflow_bitfield(Address value)
1443 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1444 limit <<= ((valsize - 1) >> 1);
1445 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1446 return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1449 template<int valsize>
1450 static inline Status
1451 overflowed(Address value, Overflow_check overflow)
1453 if (overflow == CHECK_SIGNED)
1455 if (has_overflow_signed<valsize>(value))
1456 return STATUS_OVERFLOW;
1458 else if (overflow == CHECK_BITFIELD)
1460 if (has_overflow_bitfield<valsize>(value))
1461 return STATUS_OVERFLOW;
1466 // Do a simple RELA relocation
1467 template<int valsize>
1468 static inline Status
1469 rela(unsigned char* view, Address value, Overflow_check overflow)
1471 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1472 Valtype* wv = reinterpret_cast<Valtype*>(view);
1473 elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1474 return overflowed<valsize>(value, overflow);
1477 template<int valsize>
1478 static inline Status
1479 rela(unsigned char* view,
1480 unsigned int right_shift,
1481 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1483 Overflow_check overflow)
1485 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1486 Valtype* wv = reinterpret_cast<Valtype*>(view);
1487 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1488 Valtype reloc = value >> right_shift;
1491 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1492 return overflowed<valsize>(value >> right_shift, overflow);
1495 // Do a simple RELA relocation, unaligned.
1496 template<int valsize>
1497 static inline Status
1498 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1500 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1501 return overflowed<valsize>(value, overflow);
1504 template<int valsize>
1505 static inline Status
1506 rela_ua(unsigned char* view,
1507 unsigned int right_shift,
1508 typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1510 Overflow_check overflow)
1512 typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1514 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1515 Valtype reloc = value >> right_shift;
1518 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1519 return overflowed<valsize>(value >> right_shift, overflow);
1523 // R_PPC64_ADDR64: (Symbol + Addend)
1525 addr64(unsigned char* view, Address value)
1526 { This::template rela<64>(view, value, CHECK_NONE); }
1528 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1530 addr64_u(unsigned char* view, Address value)
1531 { This::template rela_ua<64>(view, value, CHECK_NONE); }
1533 // R_POWERPC_ADDR32: (Symbol + Addend)
1534 static inline Status
1535 addr32(unsigned char* view, Address value, Overflow_check overflow)
1536 { return This::template rela<32>(view, value, overflow); }
1538 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1539 static inline Status
1540 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1541 { return This::template rela_ua<32>(view, value, overflow); }
1543 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1544 static inline Status
1545 addr24(unsigned char* view, Address value, Overflow_check overflow)
1547 Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1548 if (overflow != CHECK_NONE && (value & 3) != 0)
1549 stat = STATUS_OVERFLOW;
1553 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1554 static inline Status
1555 addr16(unsigned char* view, Address value, Overflow_check overflow)
1556 { return This::template rela<16>(view, value, overflow); }
1558 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1559 static inline Status
1560 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1561 { return This::template rela_ua<16>(view, value, overflow); }
1563 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1564 static inline Status
1565 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1567 Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1568 if (overflow != CHECK_NONE && (value & 3) != 0)
1569 stat = STATUS_OVERFLOW;
1573 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1575 addr16_hi(unsigned char* view, Address value)
1576 { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1578 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1580 addr16_ha(unsigned char* view, Address value)
1581 { This::addr16_hi(view, value + 0x8000); }
1583 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1585 addr16_hi2(unsigned char* view, Address value)
1586 { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1588 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1590 addr16_ha2(unsigned char* view, Address value)
1591 { This::addr16_hi2(view, value + 0x8000); }
1593 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1595 addr16_hi3(unsigned char* view, Address value)
1596 { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1598 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1600 addr16_ha3(unsigned char* view, Address value)
1601 { This::addr16_hi3(view, value + 0x8000); }
1603 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1604 static inline Status
1605 addr14(unsigned char* view, Address value, Overflow_check overflow)
1607 Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1608 if (overflow != CHECK_NONE && (value & 3) != 0)
1609 stat = STATUS_OVERFLOW;
1614 // Stash away the index of .got2 or .opd in a relocatable object, if
1615 // such a section exists.
1617 template<int size, bool big_endian>
1619 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1620 Read_symbols_data* sd)
1622 const unsigned char* const pshdrs = sd->section_headers->data();
1623 const unsigned char* namesu = sd->section_names->data();
1624 const char* names = reinterpret_cast<const char*>(namesu);
1625 section_size_type names_size = sd->section_names_size;
1626 const unsigned char* s;
1628 s = this->template find_shdr<size, big_endian>(pshdrs,
1629 size == 32 ? ".got2" : ".opd",
1630 names, names_size, NULL);
1633 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1634 this->special_ = ndx;
1636 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1639 // Examine .rela.opd to build info about function entry points.
1641 template<int size, bool big_endian>
1643 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1645 const unsigned char* prelocs,
1646 const unsigned char* plocal_syms)
1650 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1652 const int reloc_size
1653 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1654 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1655 Address expected_off = 0;
1656 bool regular = true;
1657 unsigned int opd_ent_size = 0;
1659 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1661 Reltype reloc(prelocs);
1662 typename elfcpp::Elf_types<size>::Elf_WXword r_info
1663 = reloc.get_r_info();
1664 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1665 if (r_type == elfcpp::R_PPC64_ADDR64)
1667 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1668 typename elfcpp::Elf_types<size>::Elf_Addr value;
1671 if (r_sym < this->local_symbol_count())
1673 typename elfcpp::Sym<size, big_endian>
1674 lsym(plocal_syms + r_sym * sym_size);
1675 shndx = lsym.get_st_shndx();
1676 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1677 value = lsym.get_st_value();
1680 shndx = this->symbol_section_and_value(r_sym, &value,
1682 this->set_opd_ent(reloc.get_r_offset(), shndx,
1683 value + reloc.get_r_addend());
1686 expected_off = reloc.get_r_offset();
1687 opd_ent_size = expected_off;
1689 else if (expected_off != reloc.get_r_offset())
1691 expected_off += opd_ent_size;
1693 else if (r_type == elfcpp::R_PPC64_TOC)
1695 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1700 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1701 this->name().c_str(), r_type);
1705 if (reloc_count <= 2)
1706 opd_ent_size = this->section_size(this->opd_shndx());
1707 if (opd_ent_size != 24 && opd_ent_size != 16)
1711 gold_warning(_("%s: .opd is not a regular array of opd entries"),
1712 this->name().c_str());
1718 template<int size, bool big_endian>
1720 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1722 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1725 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1726 p != rd->relocs.end();
1729 if (p->data_shndx == this->opd_shndx())
1731 uint64_t opd_size = this->section_size(this->opd_shndx());
1732 gold_assert(opd_size == static_cast<size_t>(opd_size));
1735 this->init_opd(opd_size);
1736 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1737 rd->local_symbols->data());
1745 // Call Sized_dynobj::do_read_symbols to read the symbols then
1746 // read .opd from a dynamic object, filling in opd_ent_ vector,
1748 template<int size, bool big_endian>
1750 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
1752 Sized_dynobj<size, big_endian>::do_read_symbols(sd);
1755 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
1756 const unsigned char* const pshdrs = sd->section_headers->data();
1757 const unsigned char* namesu = sd->section_names->data();
1758 const char* names = reinterpret_cast<const char*>(namesu);
1759 const unsigned char* s = NULL;
1760 const unsigned char* opd;
1761 section_size_type opd_size;
1763 // Find and read .opd section.
1766 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
1767 sd->section_names_size,
1772 typename elfcpp::Shdr<size, big_endian> shdr(s);
1773 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1774 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1776 this->opd_shndx_ = (s - pshdrs) / shdr_size;
1777 this->opd_address_ = shdr.get_sh_addr();
1778 opd_size = convert_to_section_size_type(shdr.get_sh_size());
1779 opd = this->get_view(shdr.get_sh_offset(), opd_size,
1785 // Build set of executable sections.
1786 // Using a set is probably overkill. There is likely to be only
1787 // a few executable sections, typically .init, .text and .fini,
1788 // and they are generally grouped together.
1789 typedef std::set<Sec_info> Exec_sections;
1790 Exec_sections exec_sections;
1792 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
1794 typename elfcpp::Shdr<size, big_endian> shdr(s);
1795 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
1796 && ((shdr.get_sh_flags()
1797 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1798 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
1799 && shdr.get_sh_size() != 0)
1801 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
1802 shdr.get_sh_size(), i));
1805 if (exec_sections.empty())
1808 // Look over the OPD entries. This is complicated by the fact
1809 // that some binaries will use two-word entries while others
1810 // will use the standard three-word entries. In most cases
1811 // the third word (the environment pointer for languages like
1812 // Pascal) is unused and will be zero. If the third word is
1813 // used it should not be pointing into executable sections,
1815 this->init_opd(opd_size);
1816 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
1818 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
1819 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
1820 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
1822 // Chances are that this is the third word of an OPD entry.
1824 typename Exec_sections::const_iterator e
1825 = exec_sections.upper_bound(Sec_info(val, 0, 0));
1826 if (e != exec_sections.begin())
1829 if (e->start <= val && val < e->start + e->len)
1831 // We have an address in an executable section.
1832 // VAL ought to be the function entry, set it up.
1833 this->set_opd_ent(p - opd, e->shndx, val);
1834 // Skip second word of OPD entry, the TOC pointer.
1838 // If we didn't match any executable sections, we likely
1839 // have a non-zero third word in the OPD entry.
1844 // Set up some symbols.
1846 template<int size, bool big_endian>
1848 Target_powerpc<size, big_endian>::do_define_standard_symbols(
1849 Symbol_table* symtab,
1854 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1855 // undefined when scanning relocs (and thus requires
1856 // non-relative dynamic relocs). The proper value will be
1858 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1859 if (gotsym != NULL && gotsym->is_undefined())
1861 Target_powerpc<size, big_endian>* target =
1862 static_cast<Target_powerpc<size, big_endian>*>(
1863 parameters->sized_target<size, big_endian>());
1864 Output_data_got_powerpc<size, big_endian>* got
1865 = target->got_section(symtab, layout);
1866 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1867 Symbol_table::PREDEFINED,
1871 elfcpp::STV_HIDDEN, 0,
1875 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1876 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
1877 if (sdasym != NULL && sdasym->is_undefined())
1879 Output_data_space* sdata = new Output_data_space(4, "** sdata");
1881 = layout->add_output_section_data(".sdata", 0,
1883 | elfcpp::SHF_WRITE,
1884 sdata, ORDER_SMALL_DATA, false);
1885 symtab->define_in_output_data("_SDA_BASE_", NULL,
1886 Symbol_table::PREDEFINED,
1887 os, 32768, 0, elfcpp::STT_OBJECT,
1888 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
1894 // Set up PowerPC target specific relobj.
1896 template<int size, bool big_endian>
1898 Target_powerpc<size, big_endian>::do_make_elf_object(
1899 const std::string& name,
1900 Input_file* input_file,
1901 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1903 int et = ehdr.get_e_type();
1904 // ET_EXEC files are valid input for --just-symbols/-R,
1905 // and we treat them as relocatable objects.
1906 if (et == elfcpp::ET_REL
1907 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1909 Powerpc_relobj<size, big_endian>* obj =
1910 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1914 else if (et == elfcpp::ET_DYN)
1916 Powerpc_dynobj<size, big_endian>* obj =
1917 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1923 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1928 template<int size, bool big_endian>
1929 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1932 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1933 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1935 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1936 : Output_data_got<size, big_endian>(),
1937 symtab_(symtab), layout_(layout),
1938 header_ent_cnt_(size == 32 ? 3 : 1),
1939 header_index_(size == 32 ? 0x2000 : 0)
1944 // Create a new GOT entry and return its offset.
1946 add_got_entry(Got_entry got_entry)
1948 this->reserve_ent();
1949 return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1952 // Create a pair of new GOT entries and return the offset of the first.
1954 add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1956 this->reserve_ent(2);
1957 return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1962 add_constant_pair(Valtype c1, Valtype c2)
1964 this->reserve_ent(2);
1965 unsigned int got_offset = this->add_constant(c1);
1966 this->add_constant(c2);
1970 // Offset of _GLOBAL_OFFSET_TABLE_.
1974 return this->got_offset(this->header_index_);
1977 // Offset of base used to access the GOT/TOC.
1978 // The got/toc pointer reg will be set to this value.
1980 got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1983 return this->g_o_t();
1985 return (this->output_section()->address()
1986 + object->toc_base_offset()
1990 // Ensure our GOT has a header.
1992 set_final_data_size()
1994 if (this->header_ent_cnt_ != 0)
1995 this->make_header();
1996 Output_data_got<size, big_endian>::set_final_data_size();
1999 // First word of GOT header needs some values that are not
2000 // handled by Output_data_got so poke them in here.
2001 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2003 do_write(Output_file* of)
2006 if (size == 32 && this->layout_->dynamic_data() != NULL)
2007 val = this->layout_->dynamic_section()->address();
2009 val = this->output_section()->address() + 0x8000;
2010 this->replace_constant(this->header_index_, val);
2011 Output_data_got<size, big_endian>::do_write(of);
2016 reserve_ent(unsigned int cnt = 1)
2018 if (this->header_ent_cnt_ == 0)
2020 if (this->num_entries() + cnt > this->header_index_)
2021 this->make_header();
2027 this->header_ent_cnt_ = 0;
2028 this->header_index_ = this->num_entries();
2031 Output_data_got<size, big_endian>::add_constant(0);
2032 Output_data_got<size, big_endian>::add_constant(0);
2033 Output_data_got<size, big_endian>::add_constant(0);
2035 // Define _GLOBAL_OFFSET_TABLE_ at the header
2036 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2039 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2040 sym->set_value(this->g_o_t());
2043 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2044 Symbol_table::PREDEFINED,
2045 this, this->g_o_t(), 0,
2048 elfcpp::STV_HIDDEN, 0,
2052 Output_data_got<size, big_endian>::add_constant(0);
2055 // Stashed pointers.
2056 Symbol_table* symtab_;
2060 unsigned int header_ent_cnt_;
2061 // GOT header index.
2062 unsigned int header_index_;
2065 // Get the GOT section, creating it if necessary.
2067 template<int size, bool big_endian>
2068 Output_data_got_powerpc<size, big_endian>*
2069 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2072 if (this->got_ == NULL)
2074 gold_assert(symtab != NULL && layout != NULL);
2077 = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2079 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2080 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2081 this->got_, ORDER_DATA, false);
2087 // Get the dynamic reloc section, creating it if necessary.
2089 template<int size, bool big_endian>
2090 typename Target_powerpc<size, big_endian>::Reloc_section*
2091 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2093 if (this->rela_dyn_ == NULL)
2095 gold_assert(layout != NULL);
2096 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2097 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2098 elfcpp::SHF_ALLOC, this->rela_dyn_,
2099 ORDER_DYNAMIC_RELOCS, false);
2101 return this->rela_dyn_;
2107 // Determine the stub group size. The group size is the absolute
2108 // value of the parameter --stub-group-size. If --stub-group-size
2109 // is passed a negative value, we restrict stubs to be always before
2110 // the stubbed branches.
2111 Stub_control(int32_t size)
2112 : state_(NO_GROUP), stub_group_size_(abs(size)),
2113 stub14_group_size_(abs(size)),
2114 stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
2115 group_end_addr_(0), owner_(NULL), output_section_(NULL)
2117 if (stub_group_size_ == 1)
2120 if (stubs_always_before_branch_)
2122 stub_group_size_ = 0x1e00000;
2123 stub14_group_size_ = 0x7800;
2127 stub_group_size_ = 0x1c00000;
2128 stub14_group_size_ = 0x7000;
2130 suppress_size_errors_ = true;
2134 // Return true iff input section can be handled by current stub
2137 can_add_to_stub_group(Output_section* o,
2138 const Output_section::Input_section* i,
2141 const Output_section::Input_section*
2147 { return output_section_; }
2153 FINDING_STUB_SECTION,
2158 uint32_t stub_group_size_;
2159 uint32_t stub14_group_size_;
2160 bool stubs_always_before_branch_;
2161 bool suppress_size_errors_;
2162 uint64_t group_end_addr_;
2163 const Output_section::Input_section* owner_;
2164 Output_section* output_section_;
2167 // Return true iff input section can be handled by current stub/
2171 Stub_control::can_add_to_stub_group(Output_section* o,
2172 const Output_section::Input_section* i,
2176 = has14 ? this->stub14_group_size_ : this->stub_group_size_;
2177 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2179 uint64_t start_addr = o->address();
2182 // .init and .fini sections are pasted together to form a single
2183 // function. We can't be adding stubs in the middle of the function.
2184 this_size = o->data_size();
2187 start_addr += i->relobj()->output_section_offset(i->shndx());
2188 this_size = i->data_size();
2190 uint64_t end_addr = start_addr + this_size;
2191 bool toobig = this_size > group_size;
2193 if (toobig && !this->suppress_size_errors_)
2194 gold_warning(_("%s:%s exceeds group size"),
2195 i->relobj()->name().c_str(),
2196 i->relobj()->section_name(i->shndx()).c_str());
2198 if (this->state_ != HAS_STUB_SECTION
2199 && (!whole_sec || this->output_section_ != o))
2202 this->output_section_ = o;
2205 if (this->state_ == NO_GROUP)
2207 this->state_ = FINDING_STUB_SECTION;
2208 this->group_end_addr_ = end_addr;
2210 else if (this->group_end_addr_ - start_addr < group_size)
2212 // Adding this section would make the group larger than GROUP_SIZE.
2213 else if (this->state_ == FINDING_STUB_SECTION
2214 && !this->stubs_always_before_branch_
2217 // But wait, there's more! Input sections up to GROUP_SIZE
2218 // bytes before the stub table can be handled by it too.
2219 this->state_ = HAS_STUB_SECTION;
2220 this->group_end_addr_ = end_addr;
2224 this->state_ = NO_GROUP;
2230 // Look over all the input sections, deciding where to place stubs.
2232 template<int size, bool big_endian>
2234 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2237 Stub_control stub_control(parameters->options().stub_group_size());
2239 // Group input sections and insert stub table
2240 Stub_table<size, big_endian>* stub_table = NULL;
2241 Layout::Section_list section_list;
2242 layout->get_executable_sections(§ion_list);
2243 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2244 for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2245 o != section_list.rend();
2248 typedef Output_section::Input_section_list Input_section_list;
2249 for (Input_section_list::const_reverse_iterator i
2250 = (*o)->input_sections().rbegin();
2251 i != (*o)->input_sections().rend();
2254 if (i->is_input_section())
2256 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2257 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2258 bool has14 = ppcobj->has_14bit_branch(i->shndx());
2259 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2261 stub_table->init(stub_control.owner(),
2262 stub_control.output_section());
2265 if (stub_table == NULL)
2266 stub_table = this->new_stub_table();
2267 ppcobj->set_stub_table(i->shndx(), stub_table);
2271 if (stub_table != NULL)
2272 stub_table->init(stub_control.owner(), stub_control.output_section());
2275 // If this branch needs a plt call stub, or a long branch stub, make one.
2277 template<int size, bool big_endian>
2279 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2280 Stub_table<size, big_endian>* stub_table,
2281 Stub_table<size, big_endian>* ifunc_stub_table,
2282 Symbol_table* symtab) const
2284 Symbol* sym = this->object_->global_symbol(this->r_sym_);
2285 if (sym != NULL && sym->is_forwarder())
2286 sym = symtab->resolve_forwards(sym);
2287 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2289 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(this->r_type_))
2290 : this->object_->local_has_plt_offset(this->r_sym_))
2292 if (stub_table == NULL)
2293 stub_table = this->object_->stub_table(this->shndx_);
2294 if (stub_table == NULL)
2296 // This is a ref from a data section to an ifunc symbol.
2297 stub_table = ifunc_stub_table;
2299 gold_assert(stub_table != NULL);
2301 stub_table->add_plt_call_entry(this->object_, gsym,
2302 this->r_type_, this->addend_);
2304 stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2305 this->r_type_, this->addend_);
2309 unsigned int max_branch_offset;
2310 if (this->r_type_ == elfcpp::R_POWERPC_REL14
2311 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2312 || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2313 max_branch_offset = 1 << 15;
2314 else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2315 || this->r_type_ == elfcpp::R_PPC_PLTREL24
2316 || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2317 max_branch_offset = 1 << 25;
2320 Address from = this->object_->get_output_section_offset(this->shndx_);
2321 gold_assert(from != invalid_address);
2322 from += (this->object_->output_section(this->shndx_)->address()
2327 switch (gsym->source())
2329 case Symbol::FROM_OBJECT:
2331 Object* symobj = gsym->object();
2332 if (symobj->is_dynamic()
2333 || symobj->pluginobj() != NULL)
2336 unsigned int shndx = gsym->shndx(&is_ordinary);
2337 if (shndx == elfcpp::SHN_UNDEF)
2342 case Symbol::IS_UNDEFINED:
2348 Symbol_table::Compute_final_value_status status;
2349 to = symtab->compute_final_value<size>(gsym, &status);
2350 if (status != Symbol_table::CFVS_OK)
2355 const Symbol_value<size>* psymval
2356 = this->object_->local_symbol(this->r_sym_);
2357 Symbol_value<size> symval;
2358 typedef Sized_relobj_file<size, big_endian> ObjType;
2359 typename ObjType::Compute_final_local_value_status status
2360 = this->object_->compute_final_local_value(this->r_sym_, psymval,
2362 if (status != ObjType::CFLV_OK
2363 || !symval.has_output_value())
2365 to = symval.value(this->object_, 0);
2367 to += this->addend_;
2368 if (stub_table == NULL)
2369 stub_table = this->object_->stub_table(this->shndx_);
2370 gold_assert(stub_table != NULL);
2371 if (size == 64 && is_branch_reloc(this->r_type_))
2373 unsigned int dest_shndx;
2374 to = stub_table->targ()->symval_for_branch(symtab, to, gsym,
2378 Address delta = to - from;
2379 if (delta + max_branch_offset >= 2 * max_branch_offset)
2381 stub_table->add_long_branch_entry(this->object_, to);
2386 // Relaxation hook. This is where we do stub generation.
2388 template<int size, bool big_endian>
2390 Target_powerpc<size, big_endian>::do_relax(int pass,
2391 const Input_objects*,
2392 Symbol_table* symtab,
2396 unsigned int prev_brlt_size = 0;
2399 bool thread_safe = parameters->options().plt_thread_safe();
2400 if (size == 64 && !parameters->options().user_set_plt_thread_safe())
2402 static const char* const thread_starter[] =
2406 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2408 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2409 "mq_notify", "create_timer",
2413 "GOMP_parallel_start",
2414 "GOMP_parallel_loop_static_start",
2415 "GOMP_parallel_loop_dynamic_start",
2416 "GOMP_parallel_loop_guided_start",
2417 "GOMP_parallel_loop_runtime_start",
2418 "GOMP_parallel_sections_start",
2421 if (parameters->options().shared())
2425 for (unsigned int i = 0;
2426 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2429 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2430 thread_safe = (sym != NULL
2432 && sym->in_real_elf());
2438 this->plt_thread_safe_ = thread_safe;
2439 this->group_sections(layout, task);
2442 // We need address of stub tables valid for make_stub.
2443 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2444 p != this->stub_tables_.end();
2447 const Powerpc_relobj<size, big_endian>* object
2448 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2449 Address off = object->get_output_section_offset((*p)->shndx());
2450 gold_assert(off != invalid_address);
2451 Output_section* os = (*p)->output_section();
2452 (*p)->set_address_and_size(os, off);
2457 // Clear plt call stubs, long branch stubs and branch lookup table.
2458 prev_brlt_size = this->branch_lookup_table_.size();
2459 this->branch_lookup_table_.clear();
2460 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2461 p != this->stub_tables_.end();
2464 (*p)->clear_stubs();
2468 // Build all the stubs.
2469 Stub_table<size, big_endian>* ifunc_stub_table
2470 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2471 Stub_table<size, big_endian>* one_stub_table
2472 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2473 for (typename Branches::const_iterator b = this->branch_info_.begin();
2474 b != this->branch_info_.end();
2477 b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2480 // Did anything change size?
2481 unsigned int num_huge_branches = this->branch_lookup_table_.size();
2482 bool again = num_huge_branches != prev_brlt_size;
2483 if (size == 64 && num_huge_branches != 0)
2484 this->make_brlt_section(layout);
2485 if (size == 64 && again)
2486 this->brlt_section_->set_current_size(num_huge_branches);
2488 typedef Unordered_set<Output_section*> Output_sections;
2489 Output_sections os_need_update;
2490 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2491 p != this->stub_tables_.end();
2494 if ((*p)->size_update())
2497 (*p)->add_eh_frame(layout);
2498 os_need_update.insert((*p)->output_section());
2502 // Set output section offsets for all input sections in an output
2503 // section that just changed size. Anything past the stubs will
2505 for (typename Output_sections::iterator p = os_need_update.begin();
2506 p != os_need_update.end();
2509 Output_section* os = *p;
2511 typedef Output_section::Input_section_list Input_section_list;
2512 for (Input_section_list::const_iterator i = os->input_sections().begin();
2513 i != os->input_sections().end();
2516 off = align_address(off, i->addralign());
2517 if (i->is_input_section() || i->is_relaxed_input_section())
2518 i->relobj()->set_section_offset(i->shndx(), off);
2519 if (i->is_relaxed_input_section())
2521 Stub_table<size, big_endian>* stub_table
2522 = static_cast<Stub_table<size, big_endian>*>(
2523 i->relaxed_input_section());
2524 off += stub_table->set_address_and_size(os, off);
2527 off += i->data_size();
2529 // If .brlt is part of this output section, then we have just
2530 // done the offset adjustment.
2531 os->clear_section_offsets_need_adjustment();
2536 && num_huge_branches != 0
2537 && parameters->options().output_is_position_independent())
2539 // Fill in the BRLT relocs.
2540 this->brlt_section_->reset_data_size();
2541 for (typename Branch_lookup_table::const_iterator p
2542 = this->branch_lookup_table_.begin();
2543 p != this->branch_lookup_table_.end();
2546 this->brlt_section_->add_reloc(p->first, p->second);
2548 this->brlt_section_->finalize_data_size();
2553 template<int size, bool big_endian>
2555 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2556 unsigned char* oview,
2560 uint64_t address = plt->address();
2561 off_t len = plt->data_size();
2563 if (plt == this->glink_)
2565 // See Output_data_glink::do_write() for glink contents.
2568 // There is one word before __glink_PLTresolve
2572 else if (parameters->options().output_is_position_independent())
2574 // There are two FDEs for a position independent glink.
2575 // The first covers the branch table, the second
2576 // __glink_PLTresolve at the end of glink.
2577 off_t resolve_size = this->glink_->pltresolve_size;
2579 len -= resolve_size;
2582 address += len - resolve_size;
2589 // Must be a stub table.
2590 const Stub_table<size, big_endian>* stub_table
2591 = static_cast<const Stub_table<size, big_endian>*>(plt);
2592 uint64_t stub_address = stub_table->stub_address();
2593 len -= stub_address - address;
2594 address = stub_address;
2597 *paddress = address;
2601 // A class to handle the PLT data.
2603 template<int size, bool big_endian>
2604 class Output_data_plt_powerpc : public Output_section_data_build
2607 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2608 size, big_endian> Reloc_section;
2610 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2611 Reloc_section* plt_rel,
2612 unsigned int reserved_size,
2614 : Output_section_data_build(size == 32 ? 4 : 8),
2617 initial_plt_entry_size_(reserved_size),
2621 // Add an entry to the PLT.
2626 add_ifunc_entry(Symbol*);
2629 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2631 // Return the .rela.plt section data.
2638 // Return the number of PLT entries.
2642 return ((this->current_data_size() - this->initial_plt_entry_size_)
2646 // Return the offset of the first non-reserved PLT entry.
2648 first_plt_entry_offset()
2649 { return this->initial_plt_entry_size_; }
2651 // Return the size of a PLT entry.
2653 get_plt_entry_size()
2654 { return plt_entry_size; }
2658 do_adjust_output_section(Output_section* os)
2663 // Write to a map file.
2665 do_print_to_mapfile(Mapfile* mapfile) const
2666 { mapfile->print_output_data(this, this->name_); }
2669 // The size of an entry in the PLT.
2670 static const int plt_entry_size = size == 32 ? 4 : 24;
2672 // Write out the PLT data.
2674 do_write(Output_file*);
2676 // The reloc section.
2677 Reloc_section* rel_;
2678 // Allows access to .glink for do_write.
2679 Target_powerpc<size, big_endian>* targ_;
2680 // The size of the first reserved entry.
2681 int initial_plt_entry_size_;
2682 // What to report in map file.
2686 // Add an entry to the PLT.
2688 template<int size, bool big_endian>
2690 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
2692 if (!gsym->has_plt_offset())
2694 section_size_type off = this->current_data_size();
2696 off += this->first_plt_entry_offset();
2697 gsym->set_plt_offset(off);
2698 gsym->set_needs_dynsym_entry();
2699 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
2700 this->rel_->add_global(gsym, dynrel, this, off, 0);
2701 off += plt_entry_size;
2702 this->set_current_data_size(off);
2706 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2708 template<int size, bool big_endian>
2710 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
2712 if (!gsym->has_plt_offset())
2714 section_size_type off = this->current_data_size();
2715 gsym->set_plt_offset(off);
2716 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2718 dynrel = elfcpp::R_PPC64_JMP_IREL;
2719 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
2720 off += plt_entry_size;
2721 this->set_current_data_size(off);
2725 // Add an entry for a local ifunc symbol to the IPLT.
2727 template<int size, bool big_endian>
2729 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
2730 Sized_relobj_file<size, big_endian>* relobj,
2731 unsigned int local_sym_index)
2733 if (!relobj->local_has_plt_offset(local_sym_index))
2735 section_size_type off = this->current_data_size();
2736 relobj->set_local_plt_offset(local_sym_index, off);
2737 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2739 dynrel = elfcpp::R_PPC64_JMP_IREL;
2740 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
2742 off += plt_entry_size;
2743 this->set_current_data_size(off);
2747 static const uint32_t add_0_11_11 = 0x7c0b5a14;
2748 static const uint32_t add_2_2_11 = 0x7c425a14;
2749 static const uint32_t add_3_3_2 = 0x7c631214;
2750 static const uint32_t add_3_3_13 = 0x7c636a14;
2751 static const uint32_t add_11_0_11 = 0x7d605a14;
2752 static const uint32_t add_12_2_11 = 0x7d825a14;
2753 static const uint32_t add_12_12_11 = 0x7d8c5a14;
2754 static const uint32_t addi_11_11 = 0x396b0000;
2755 static const uint32_t addi_12_12 = 0x398c0000;
2756 static const uint32_t addi_2_2 = 0x38420000;
2757 static const uint32_t addi_3_2 = 0x38620000;
2758 static const uint32_t addi_3_3 = 0x38630000;
2759 static const uint32_t addis_0_2 = 0x3c020000;
2760 static const uint32_t addis_0_13 = 0x3c0d0000;
2761 static const uint32_t addis_11_11 = 0x3d6b0000;
2762 static const uint32_t addis_11_30 = 0x3d7e0000;
2763 static const uint32_t addis_12_12 = 0x3d8c0000;
2764 static const uint32_t addis_12_2 = 0x3d820000;
2765 static const uint32_t addis_3_2 = 0x3c620000;
2766 static const uint32_t addis_3_13 = 0x3c6d0000;
2767 static const uint32_t b = 0x48000000;
2768 static const uint32_t bcl_20_31 = 0x429f0005;
2769 static const uint32_t bctr = 0x4e800420;
2770 static const uint32_t blr = 0x4e800020;
2771 static const uint32_t blrl = 0x4e800021;
2772 static const uint32_t bnectr_p4 = 0x4ce20420;
2773 static const uint32_t cmpldi_2_0 = 0x28220000;
2774 static const uint32_t cror_15_15_15 = 0x4def7b82;
2775 static const uint32_t cror_31_31_31 = 0x4ffffb82;
2776 static const uint32_t ld_0_1 = 0xe8010000;
2777 static const uint32_t ld_0_12 = 0xe80c0000;
2778 static const uint32_t ld_11_12 = 0xe96c0000;
2779 static const uint32_t ld_11_2 = 0xe9620000;
2780 static const uint32_t ld_2_1 = 0xe8410000;
2781 static const uint32_t ld_2_11 = 0xe84b0000;
2782 static const uint32_t ld_2_12 = 0xe84c0000;
2783 static const uint32_t ld_2_2 = 0xe8420000;
2784 static const uint32_t lfd_0_1 = 0xc8010000;
2785 static const uint32_t li_0_0 = 0x38000000;
2786 static const uint32_t li_12_0 = 0x39800000;
2787 static const uint32_t lis_0_0 = 0x3c000000;
2788 static const uint32_t lis_11 = 0x3d600000;
2789 static const uint32_t lis_12 = 0x3d800000;
2790 static const uint32_t lwz_0_12 = 0x800c0000;
2791 static const uint32_t lwz_11_11 = 0x816b0000;
2792 static const uint32_t lwz_11_30 = 0x817e0000;
2793 static const uint32_t lwz_12_12 = 0x818c0000;
2794 static const uint32_t lwzu_0_12 = 0x840c0000;
2795 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
2796 static const uint32_t mflr_0 = 0x7c0802a6;
2797 static const uint32_t mflr_11 = 0x7d6802a6;
2798 static const uint32_t mflr_12 = 0x7d8802a6;
2799 static const uint32_t mtctr_0 = 0x7c0903a6;
2800 static const uint32_t mtctr_11 = 0x7d6903a6;
2801 static const uint32_t mtctr_12 = 0x7d8903a6;
2802 static const uint32_t mtlr_0 = 0x7c0803a6;
2803 static const uint32_t mtlr_12 = 0x7d8803a6;
2804 static const uint32_t nop = 0x60000000;
2805 static const uint32_t ori_0_0_0 = 0x60000000;
2806 static const uint32_t std_0_1 = 0xf8010000;
2807 static const uint32_t std_0_12 = 0xf80c0000;
2808 static const uint32_t std_2_1 = 0xf8410000;
2809 static const uint32_t stfd_0_1 = 0xd8010000;
2810 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
2811 static const uint32_t sub_11_11_12 = 0x7d6c5850;
2812 static const uint32_t xor_11_11_11 = 0x7d6b5a78;
2814 // Write out the PLT.
2816 template<int size, bool big_endian>
2818 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
2822 const section_size_type offset = this->offset();
2823 const section_size_type oview_size
2824 = convert_to_section_size_type(this->data_size());
2825 unsigned char* const oview = of->get_output_view(offset, oview_size);
2826 unsigned char* pov = oview;
2827 unsigned char* endpov = oview + oview_size;
2829 // The address of the .glink branch table
2830 const Output_data_glink<size, big_endian>* glink
2831 = this->targ_->glink_section();
2832 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
2834 while (pov < endpov)
2836 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
2841 of->write_output_view(offset, oview_size, oview);
2845 // Create the PLT section.
2847 template<int size, bool big_endian>
2849 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
2852 if (this->plt_ == NULL)
2854 if (this->got_ == NULL)
2855 this->got_section(symtab, layout);
2857 if (this->glink_ == NULL)
2858 make_glink_section(layout);
2860 // Ensure that .rela.dyn always appears before .rela.plt This is
2861 // necessary due to how, on PowerPC and some other targets, .rela.dyn
2862 // needs to include .rela.plt in it's range.
2863 this->rela_dyn_section(layout);
2865 Reloc_section* plt_rel = new Reloc_section(false);
2866 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
2867 elfcpp::SHF_ALLOC, plt_rel,
2868 ORDER_DYNAMIC_PLT_RELOCS, false);
2870 = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
2871 size == 32 ? 0 : 24,
2873 layout->add_output_section_data(".plt",
2875 ? elfcpp::SHT_PROGBITS
2876 : elfcpp::SHT_NOBITS),
2877 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2886 // Create the IPLT section.
2888 template<int size, bool big_endian>
2890 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
2893 if (this->iplt_ == NULL)
2895 this->make_plt_section(symtab, layout);
2897 Reloc_section* iplt_rel = new Reloc_section(false);
2898 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
2900 = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
2902 this->plt_->output_section()->add_output_section_data(this->iplt_);
2906 // A section for huge long branch addresses, similar to plt section.
2908 template<int size, bool big_endian>
2909 class Output_data_brlt_powerpc : public Output_section_data_build
2912 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2913 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2914 size, big_endian> Reloc_section;
2916 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
2917 Reloc_section* brlt_rel)
2918 : Output_section_data_build(size == 32 ? 4 : 8),
2923 // Add a reloc for an entry in the BRLT.
2925 add_reloc(Address to, unsigned int off)
2926 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
2928 // Update section and reloc section size.
2930 set_current_size(unsigned int num_branches)
2932 this->reset_address_and_file_offset();
2933 this->set_current_data_size(num_branches * 16);
2934 this->finalize_data_size();
2935 Output_section* os = this->output_section();
2936 os->set_section_offsets_need_adjustment();
2937 if (this->rel_ != NULL)
2939 unsigned int reloc_size
2940 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2941 this->rel_->reset_address_and_file_offset();
2942 this->rel_->set_current_data_size(num_branches * reloc_size);
2943 this->rel_->finalize_data_size();
2944 Output_section* os = this->rel_->output_section();
2945 os->set_section_offsets_need_adjustment();
2951 do_adjust_output_section(Output_section* os)
2956 // Write to a map file.
2958 do_print_to_mapfile(Mapfile* mapfile) const
2959 { mapfile->print_output_data(this, "** BRLT"); }
2962 // Write out the BRLT data.
2964 do_write(Output_file*);
2966 // The reloc section.
2967 Reloc_section* rel_;
2968 Target_powerpc<size, big_endian>* targ_;
2971 // Make the branch lookup table section.
2973 template<int size, bool big_endian>
2975 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
2977 if (size == 64 && this->brlt_section_ == NULL)
2979 Reloc_section* brlt_rel = NULL;
2980 bool is_pic = parameters->options().output_is_position_independent();
2983 // When PIC we can't fill in .brlt (like .plt it can be a
2984 // bss style section) but must initialise at runtime via
2985 // dynamic relocats.
2986 this->rela_dyn_section(layout);
2987 brlt_rel = new Reloc_section(false);
2988 this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
2991 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
2992 if (this->plt_ && is_pic)
2993 this->plt_->output_section()
2994 ->add_output_section_data(this->brlt_section_);
2996 layout->add_output_section_data(".brlt",
2997 (is_pic ? elfcpp::SHT_NOBITS
2998 : elfcpp::SHT_PROGBITS),
2999 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3000 this->brlt_section_,
3001 (is_pic ? ORDER_SMALL_BSS
3002 : ORDER_SMALL_DATA),
3007 // Write out .brlt when non-PIC.
3009 template<int size, bool big_endian>
3011 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3013 if (size == 64 && !parameters->options().output_is_position_independent())
3015 const section_size_type offset = this->offset();
3016 const section_size_type oview_size
3017 = convert_to_section_size_type(this->data_size());
3018 unsigned char* const oview = of->get_output_view(offset, oview_size);
3020 this->targ_->write_branch_lookup_table(oview);
3021 of->write_output_view(offset, oview_size, oview);
3025 static inline uint32_t
3031 static inline uint32_t
3037 static inline uint32_t
3040 return hi(a + 0x8000);
3046 static const unsigned char eh_frame_cie[12];
3050 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3053 'z', 'R', 0, // Augmentation string.
3054 4, // Code alignment.
3055 0x80 - size / 8 , // Data alignment.
3057 1, // Augmentation size.
3058 (elfcpp::DW_EH_PE_pcrel
3059 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
3060 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
3063 // Describe __glink_PLTresolve use of LR, 64-bit version.
3064 static const unsigned char glink_eh_frame_fde_64[] =
3066 0, 0, 0, 0, // Replaced with offset to .glink.
3067 0, 0, 0, 0, // Replaced with size of .glink.
3068 0, // Augmentation size.
3069 elfcpp::DW_CFA_advance_loc + 1,
3070 elfcpp::DW_CFA_register, 65, 12,
3071 elfcpp::DW_CFA_advance_loc + 4,
3072 elfcpp::DW_CFA_restore_extended, 65
3075 // Describe __glink_PLTresolve use of LR, 32-bit version.
3076 static const unsigned char glink_eh_frame_fde_32[] =
3078 0, 0, 0, 0, // Replaced with offset to .glink.
3079 0, 0, 0, 0, // Replaced with size of .glink.
3080 0, // Augmentation size.
3081 elfcpp::DW_CFA_advance_loc + 2,
3082 elfcpp::DW_CFA_register, 65, 0,
3083 elfcpp::DW_CFA_advance_loc + 4,
3084 elfcpp::DW_CFA_restore_extended, 65
3087 static const unsigned char default_fde[] =
3089 0, 0, 0, 0, // Replaced with offset to stubs.
3090 0, 0, 0, 0, // Replaced with size of stubs.
3091 0, // Augmentation size.
3092 elfcpp::DW_CFA_nop, // Pad.
3097 template<bool big_endian>
3099 write_insn(unsigned char* p, uint32_t v)
3101 elfcpp::Swap<32, big_endian>::writeval(p, v);
3104 // Stub_table holds information about plt and long branch stubs.
3105 // Stubs are built in an area following some input section determined
3106 // by group_sections(). This input section is converted to a relaxed
3107 // input section allowing it to be resized to accommodate the stubs
3109 template<int size, bool big_endian>
3110 class Stub_table : public Output_relaxed_input_section
3113 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3114 static const Address invalid_address = static_cast<Address>(0) - 1;
3116 Stub_table(Target_powerpc<size, big_endian>* targ)
3117 : Output_relaxed_input_section(NULL, 0, 0),
3118 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3119 orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3120 branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3123 // Delayed Output_relaxed_input_section init.
3125 init(const Output_section::Input_section*, Output_section*);
3127 // Add a plt call stub.
3129 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3135 add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3140 // Find a given plt call stub.
3142 find_plt_call_entry(const Symbol*) const;
3145 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3146 unsigned int) const;
3149 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3155 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3160 // Add a long branch stub.
3162 add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3165 find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3171 this->plt_call_stubs_.clear();
3172 this->plt_size_ = 0;
3173 this->long_branch_stubs_.clear();
3174 this->branch_size_ = 0;
3178 set_address_and_size(const Output_section* os, Address off)
3180 Address start_off = off;
3181 off += this->orig_data_size_;
3182 Address my_size = this->plt_size_ + this->branch_size_;
3184 off = align_address(off, this->stub_align());
3185 // Include original section size and alignment padding in size
3186 my_size += off - start_off;
3187 this->reset_address_and_file_offset();
3188 this->set_current_data_size(my_size);
3189 this->set_address_and_file_offset(os->address() + start_off,
3190 os->offset() + start_off);
3195 stub_address() const
3197 return align_address(this->address() + this->orig_data_size_,
3198 this->stub_align());
3204 return align_address(this->offset() + this->orig_data_size_,
3205 this->stub_align());
3210 { return this->plt_size_; }
3215 Output_section* os = this->output_section();
3216 if (os->addralign() < this->stub_align())
3218 os->set_addralign(this->stub_align());
3219 // FIXME: get rid of the insane checkpointing.
3220 // We can't increase alignment of the input section to which
3221 // stubs are attached; The input section may be .init which
3222 // is pasted together with other .init sections to form a
3223 // function. Aligning might insert zero padding resulting in
3224 // sigill. However we do need to increase alignment of the
3225 // output section so that the align_address() on offset in
3226 // set_address_and_size() adds the same padding as the
3227 // align_address() on address in stub_address().
3228 // What's more, we need this alignment for the layout done in
3229 // relaxation_loop_body() so that the output section starts at
3230 // a suitably aligned address.
3231 os->checkpoint_set_addralign(this->stub_align());
3233 if (this->last_plt_size_ != this->plt_size_
3234 || this->last_branch_size_ != this->branch_size_)
3236 this->last_plt_size_ = this->plt_size_;
3237 this->last_branch_size_ = this->branch_size_;
3243 // Add .eh_frame info for this stub section. Unlike other linker
3244 // generated .eh_frame this is added late in the link, because we
3245 // only want the .eh_frame info if this particular stub section is
3248 add_eh_frame(Layout* layout)
3250 if (!this->eh_frame_added_)
3252 if (!parameters->options().ld_generated_unwind_info())
3255 // Since we add stub .eh_frame info late, it must be placed
3256 // after all other linker generated .eh_frame info so that
3257 // merge mapping need not be updated for input sections.
3258 // There is no provision to use a different CIE to that used
3260 if (!this->targ_->has_glink())
3263 layout->add_eh_frame_for_plt(this,
3264 Eh_cie<size>::eh_frame_cie,
3265 sizeof (Eh_cie<size>::eh_frame_cie),
3267 sizeof (default_fde));
3268 this->eh_frame_added_ = true;
3272 Target_powerpc<size, big_endian>*
3278 class Plt_stub_ent_hash;
3279 typedef Unordered_map<Plt_stub_ent, unsigned int,
3280 Plt_stub_ent_hash> Plt_stub_entries;
3282 // Alignment of stub section.
3288 unsigned int min_align = 32;
3289 unsigned int user_align = 1 << parameters->options().plt_align();
3290 return std::max(user_align, min_align);
3293 // Return the plt offset for the given call stub.
3295 plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3297 const Symbol* gsym = p->first.sym_;
3300 *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3301 && gsym->can_use_relative_reloc(false));
3302 return gsym->plt_offset();
3307 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3308 unsigned int local_sym_index = p->first.locsym_;
3309 return relobj->local_plt_offset(local_sym_index);
3313 // Size of a given plt call stub.
3315 plt_call_size(typename Plt_stub_entries::const_iterator p) const
3321 Address plt_addr = this->plt_off(p, &is_iplt);
3323 plt_addr += this->targ_->iplt_section()->address();
3325 plt_addr += this->targ_->plt_section()->address();
3326 Address got_addr = this->targ_->got_section()->output_section()->address();
3327 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3328 <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3329 got_addr += ppcobj->toc_base_offset();
3330 Address off = plt_addr - got_addr;
3331 bool static_chain = parameters->options().plt_static_chain();
3332 bool thread_safe = this->targ_->plt_thread_safe();
3333 unsigned int bytes = (4 * 5
3336 + 4 * (ha(off) != 0)
3337 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3338 unsigned int align = 1 << parameters->options().plt_align();
3340 bytes = (bytes + align - 1) & -align;
3344 // Return long branch stub size.
3346 branch_stub_size(Address to)
3349 = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3350 if (to - loc + (1 << 25) < 2 << 25)
3352 if (size == 64 || !parameters->options().output_is_position_independent())
3359 do_write(Output_file*);
3361 // Plt call stub keys.
3365 Plt_stub_ent(const Symbol* sym)
3366 : sym_(sym), object_(0), addend_(0), locsym_(0)
3369 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3370 unsigned int locsym_index)
3371 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3374 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3376 unsigned int r_type,
3378 : sym_(sym), object_(0), addend_(0), locsym_(0)
3381 this->addend_ = addend;
3382 else if (parameters->options().output_is_position_independent()
3383 && r_type == elfcpp::R_PPC_PLTREL24)
3385 this->addend_ = addend;
3386 if (this->addend_ >= 32768)
3387 this->object_ = object;
3391 Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3392 unsigned int locsym_index,
3393 unsigned int r_type,
3395 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3398 this->addend_ = addend;
3399 else if (parameters->options().output_is_position_independent()
3400 && r_type == elfcpp::R_PPC_PLTREL24)
3401 this->addend_ = addend;
3404 bool operator==(const Plt_stub_ent& that) const
3406 return (this->sym_ == that.sym_
3407 && this->object_ == that.object_
3408 && this->addend_ == that.addend_
3409 && this->locsym_ == that.locsym_);
3413 const Sized_relobj_file<size, big_endian>* object_;
3414 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3415 unsigned int locsym_;
3418 class Plt_stub_ent_hash
3421 size_t operator()(const Plt_stub_ent& ent) const
3423 return (reinterpret_cast<uintptr_t>(ent.sym_)
3424 ^ reinterpret_cast<uintptr_t>(ent.object_)
3430 // Long branch stub keys.
3431 class Branch_stub_ent
3434 Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3435 : dest_(to), toc_base_off_(0)
3438 toc_base_off_ = obj->toc_base_offset();
3441 bool operator==(const Branch_stub_ent& that) const
3443 return (this->dest_ == that.dest_
3445 || this->toc_base_off_ == that.toc_base_off_));
3449 unsigned int toc_base_off_;
3452 class Branch_stub_ent_hash
3455 size_t operator()(const Branch_stub_ent& ent) const
3456 { return ent.dest_ ^ ent.toc_base_off_; }
3459 // In a sane world this would be a global.
3460 Target_powerpc<size, big_endian>* targ_;
3461 // Map sym/object/addend to stub offset.
3462 Plt_stub_entries plt_call_stubs_;
3463 // Map destination address to stub offset.
3464 typedef Unordered_map<Branch_stub_ent, unsigned int,
3465 Branch_stub_ent_hash> Branch_stub_entries;
3466 Branch_stub_entries long_branch_stubs_;
3467 // size of input section
3468 section_size_type orig_data_size_;
3470 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3471 // Whether .eh_frame info has been created for this stub section.
3472 bool eh_frame_added_;
3475 // Make a new stub table, and record.
3477 template<int size, bool big_endian>
3478 Stub_table<size, big_endian>*
3479 Target_powerpc<size, big_endian>::new_stub_table()
3481 Stub_table<size, big_endian>* stub_table
3482 = new Stub_table<size, big_endian>(this);
3483 this->stub_tables_.push_back(stub_table);
3487 // Delayed stub table initialisation, because we create the stub table
3488 // before we know to which section it will be attached.
3490 template<int size, bool big_endian>
3492 Stub_table<size, big_endian>::init(
3493 const Output_section::Input_section* owner,
3494 Output_section* output_section)
3496 this->set_relobj(owner->relobj());
3497 this->set_shndx(owner->shndx());
3498 this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3499 this->set_output_section(output_section);
3500 this->orig_data_size_ = owner->current_data_size();
3502 std::vector<Output_relaxed_input_section*> new_relaxed;
3503 new_relaxed.push_back(this);
3504 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3507 // Add a plt call stub, if we do not already have one for this
3508 // sym/object/addend combo.
3510 template<int size, bool big_endian>
3512 Stub_table<size, big_endian>::add_plt_call_entry(
3513 const Sized_relobj_file<size, big_endian>* object,
3515 unsigned int r_type,
3518 Plt_stub_ent ent(object, gsym, r_type, addend);
3519 Address off = this->plt_size_;
3520 std::pair<typename Plt_stub_entries::iterator, bool> p
3521 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3523 this->plt_size_ = off + this->plt_call_size(p.first);
3526 template<int size, bool big_endian>
3528 Stub_table<size, big_endian>::add_plt_call_entry(
3529 const Sized_relobj_file<size, big_endian>* object,
3530 unsigned int locsym_index,
3531 unsigned int r_type,
3534 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3535 Address off = this->plt_size_;
3536 std::pair<typename Plt_stub_entries::iterator, bool> p
3537 = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3539 this->plt_size_ = off + this->plt_call_size(p.first);
3542 // Find a plt call stub.
3544 template<int size, bool big_endian>
3545 typename Stub_table<size, big_endian>::Address
3546 Stub_table<size, big_endian>::find_plt_call_entry(
3547 const Sized_relobj_file<size, big_endian>* object,
3549 unsigned int r_type,
3550 Address addend) const
3552 Plt_stub_ent ent(object, gsym, r_type, addend);
3553 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3554 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3557 template<int size, bool big_endian>
3558 typename Stub_table<size, big_endian>::Address
3559 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3561 Plt_stub_ent ent(gsym);
3562 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3563 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3566 template<int size, bool big_endian>
3567 typename Stub_table<size, big_endian>::Address
3568 Stub_table<size, big_endian>::find_plt_call_entry(
3569 const Sized_relobj_file<size, big_endian>* object,
3570 unsigned int locsym_index,
3571 unsigned int r_type,
3572 Address addend) const
3574 Plt_stub_ent ent(object, locsym_index, r_type, addend);
3575 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3576 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3579 template<int size, bool big_endian>
3580 typename Stub_table<size, big_endian>::Address
3581 Stub_table<size, big_endian>::find_plt_call_entry(
3582 const Sized_relobj_file<size, big_endian>* object,
3583 unsigned int locsym_index) const
3585 Plt_stub_ent ent(object, locsym_index);
3586 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3587 return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3590 // Add a long branch stub if we don't already have one to given
3593 template<int size, bool big_endian>
3595 Stub_table<size, big_endian>::add_long_branch_entry(
3596 const Powerpc_relobj<size, big_endian>* object,
3599 Branch_stub_ent ent(object, to);
3600 Address off = this->branch_size_;
3601 if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3603 unsigned int stub_size = this->branch_stub_size(to);
3604 this->branch_size_ = off + stub_size;
3605 if (size == 64 && stub_size != 4)
3606 this->targ_->add_branch_lookup_table(to);
3610 // Find long branch stub.
3612 template<int size, bool big_endian>
3613 typename Stub_table<size, big_endian>::Address
3614 Stub_table<size, big_endian>::find_long_branch_entry(
3615 const Powerpc_relobj<size, big_endian>* object,
3618 Branch_stub_ent ent(object, to);
3619 typename Branch_stub_entries::const_iterator p
3620 = this->long_branch_stubs_.find(ent);
3621 return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3624 // A class to handle .glink.
3626 template<int size, bool big_endian>
3627 class Output_data_glink : public Output_section_data
3630 static const int pltresolve_size = 16*4;
3632 Output_data_glink(Target_powerpc<size, big_endian>* targ)
3633 : Output_section_data(16), targ_(targ)
3637 add_eh_frame(Layout* layout)
3639 if (!parameters->options().ld_generated_unwind_info())
3643 layout->add_eh_frame_for_plt(this,
3644 Eh_cie<64>::eh_frame_cie,
3645 sizeof (Eh_cie<64>::eh_frame_cie),
3646 glink_eh_frame_fde_64,
3647 sizeof (glink_eh_frame_fde_64));
3650 // 32-bit .glink can use the default since the CIE return
3651 // address reg, LR, is valid.
3652 layout->add_eh_frame_for_plt(this,
3653 Eh_cie<32>::eh_frame_cie,
3654 sizeof (Eh_cie<32>::eh_frame_cie),
3656 sizeof (default_fde));
3657 // Except where LR is used in a PIC __glink_PLTresolve.
3658 if (parameters->options().output_is_position_independent())
3659 layout->add_eh_frame_for_plt(this,
3660 Eh_cie<32>::eh_frame_cie,
3661 sizeof (Eh_cie<32>::eh_frame_cie),
3662 glink_eh_frame_fde_32,
3663 sizeof (glink_eh_frame_fde_32));
3668 // Write to a map file.
3670 do_print_to_mapfile(Mapfile* mapfile) const
3671 { mapfile->print_output_data(this, _("** glink")); }
3675 set_final_data_size();
3679 do_write(Output_file*);
3681 // Allows access to .got and .plt for do_write.
3682 Target_powerpc<size, big_endian>* targ_;
3685 template<int size, bool big_endian>
3687 Output_data_glink<size, big_endian>::set_final_data_size()
3689 unsigned int count = this->targ_->plt_entry_count();
3690 section_size_type total = 0;
3696 // space for branch table
3697 total += 4 * (count - 1);
3699 total += -total & 15;
3700 total += this->pltresolve_size;
3704 total += this->pltresolve_size;
3706 // space for branch table
3709 total += 4 * (count - 0x8000);
3713 this->set_data_size(total);
3716 // Write out plt and long branch stub code.
3718 template<int size, bool big_endian>
3720 Stub_table<size, big_endian>::do_write(Output_file* of)
3722 if (this->plt_call_stubs_.empty()
3723 && this->long_branch_stubs_.empty())
3726 const section_size_type start_off = this->offset();
3727 const section_size_type off = this->stub_offset();
3728 const section_size_type oview_size =
3729 convert_to_section_size_type(this->data_size() - (off - start_off));
3730 unsigned char* const oview = of->get_output_view(off, oview_size);
3735 const Output_data_got_powerpc<size, big_endian>* got
3736 = this->targ_->got_section();
3737 Address got_os_addr = got->output_section()->address();
3739 if (!this->plt_call_stubs_.empty())
3741 // The base address of the .plt section.
3742 Address plt_base = this->targ_->plt_section()->address();
3743 Address iplt_base = invalid_address;
3745 // Write out plt call stubs.
3746 typename Plt_stub_entries::const_iterator cs;
3747 for (cs = this->plt_call_stubs_.begin();
3748 cs != this->plt_call_stubs_.end();
3752 Address pltoff = this->plt_off(cs, &is_iplt);
3753 Address plt_addr = pltoff;
3756 if (iplt_base == invalid_address)
3757 iplt_base = this->targ_->iplt_section()->address();
3758 plt_addr += iplt_base;
3761 plt_addr += plt_base;
3762 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3763 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
3764 Address got_addr = got_os_addr + ppcobj->toc_base_offset();
3765 Address off = plt_addr - got_addr;
3767 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
3768 gold_error(_("%s: linkage table error against `%s'"),
3769 cs->first.object_->name().c_str(),
3770 cs->first.sym_->demangled_name().c_str());
3772 bool static_chain = parameters->options().plt_static_chain();
3773 bool thread_safe = this->targ_->plt_thread_safe();
3774 bool use_fake_dep = false;
3775 Address cmp_branch_off = 0;
3778 unsigned int pltindex
3779 = ((pltoff - this->targ_->first_plt_entry_offset())
3780 / this->targ_->plt_entry_size());
3782 = (this->targ_->glink_section()->pltresolve_size
3784 if (pltindex > 32768)
3785 glinkoff += (pltindex - 32768) * 4;
3787 = this->targ_->glink_section()->address() + glinkoff;
3789 = (this->stub_address() + cs->second + 24
3790 + 4 * (ha(off) != 0)
3791 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
3792 + 4 * static_chain);
3793 cmp_branch_off = to - from;
3794 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
3797 p = oview + cs->second;
3800 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
3801 write_insn<big_endian>(p, addis_12_2 + ha(off)), p += 4;
3802 write_insn<big_endian>(p, ld_11_12 + l(off)), p += 4;
3803 if (ha(off + 8 + 8 * static_chain) != ha(off))
3805 write_insn<big_endian>(p, addi_12_12 + l(off)), p += 4;
3808 write_insn<big_endian>(p, mtctr_11), p += 4;
3811 write_insn<big_endian>(p, xor_11_11_11), p += 4;
3812 write_insn<big_endian>(p, add_12_12_11), p += 4;
3814 write_insn<big_endian>(p, ld_2_12 + l(off + 8)), p += 4;
3816 write_insn<big_endian>(p, ld_11_12 + l(off + 16)), p += 4;
3820 write_insn<big_endian>(p, std_2_1 + 40), p += 4;
3821 write_insn<big_endian>(p, ld_11_2 + l(off)), p += 4;
3822 if (ha(off + 8 + 8 * static_chain) != ha(off))
3824 write_insn<big_endian>(p, addi_2_2 + l(off)), p += 4;
3827 write_insn<big_endian>(p, mtctr_11), p += 4;
3830 write_insn<big_endian>(p, xor_11_11_11), p += 4;
3831 write_insn<big_endian>(p, add_2_2_11), p += 4;
3834 write_insn<big_endian>(p, ld_11_2 + l(off + 16)), p += 4;
3835 write_insn<big_endian>(p, ld_2_2 + l(off + 8)), p += 4;
3837 if (thread_safe && !use_fake_dep)
3839 write_insn<big_endian>(p, cmpldi_2_0), p += 4;
3840 write_insn<big_endian>(p, bnectr_p4), p += 4;
3841 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
3844 write_insn<big_endian>(p, bctr);
3848 // Write out long branch stubs.
3849 typename Branch_stub_entries::const_iterator bs;
3850 for (bs = this->long_branch_stubs_.begin();
3851 bs != this->long_branch_stubs_.end();
3854 p = oview + this->plt_size_ + bs->second;
3855 Address loc = this->stub_address() + this->plt_size_ + bs->second;
3856 Address delta = bs->first.dest_ - loc;
3857 if (delta + (1 << 25) < 2 << 25)
3858 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3862 = this->targ_->find_branch_lookup_table(bs->first.dest_);
3863 gold_assert(brlt_addr != invalid_address);
3864 brlt_addr += this->targ_->brlt_section()->address();
3865 Address got_addr = got_os_addr + bs->first.toc_base_off_;
3866 Address brltoff = brlt_addr - got_addr;
3867 if (ha(brltoff) == 0)
3869 write_insn<big_endian>(p, ld_11_2 + l(brltoff)), p += 4;
3873 write_insn<big_endian>(p, addis_12_2 + ha(brltoff)), p += 4;
3874 write_insn<big_endian>(p, ld_11_12 + l(brltoff)), p += 4;
3876 write_insn<big_endian>(p, mtctr_11), p += 4;
3877 write_insn<big_endian>(p, bctr);
3883 if (!this->plt_call_stubs_.empty())
3885 // The base address of the .plt section.
3886 Address plt_base = this->targ_->plt_section()->address();
3887 Address iplt_base = invalid_address;
3888 // The address of _GLOBAL_OFFSET_TABLE_.
3889 Address g_o_t = invalid_address;
3891 // Write out plt call stubs.
3892 typename Plt_stub_entries::const_iterator cs;
3893 for (cs = this->plt_call_stubs_.begin();
3894 cs != this->plt_call_stubs_.end();
3898 Address plt_addr = this->plt_off(cs, &is_iplt);
3901 if (iplt_base == invalid_address)
3902 iplt_base = this->targ_->iplt_section()->address();
3903 plt_addr += iplt_base;
3906 plt_addr += plt_base;
3908 p = oview + cs->second;
3909 if (parameters->options().output_is_position_independent())
3912 const Powerpc_relobj<size, big_endian>* ppcobj
3913 = (static_cast<const Powerpc_relobj<size, big_endian>*>
3914 (cs->first.object_));
3915 if (ppcobj != NULL && cs->first.addend_ >= 32768)
3917 unsigned int got2 = ppcobj->got2_shndx();
3918 got_addr = ppcobj->get_output_section_offset(got2);
3919 gold_assert(got_addr != invalid_address);
3920 got_addr += (ppcobj->output_section(got2)->address()
3921 + cs->first.addend_);
3925 if (g_o_t == invalid_address)
3927 const Output_data_got_powerpc<size, big_endian>* got
3928 = this->targ_->got_section();
3929 g_o_t = got->address() + got->g_o_t();
3934 Address off = plt_addr - got_addr;
3937 write_insn<big_endian>(p + 0, lwz_11_30 + l(off));
3938 write_insn<big_endian>(p + 4, mtctr_11);
3939 write_insn<big_endian>(p + 8, bctr);
3943 write_insn<big_endian>(p + 0, addis_11_30 + ha(off));
3944 write_insn<big_endian>(p + 4, lwz_11_11 + l(off));
3945 write_insn<big_endian>(p + 8, mtctr_11);
3946 write_insn<big_endian>(p + 12, bctr);
3951 write_insn<big_endian>(p + 0, lis_11 + ha(plt_addr));
3952 write_insn<big_endian>(p + 4, lwz_11_11 + l(plt_addr));
3953 write_insn<big_endian>(p + 8, mtctr_11);
3954 write_insn<big_endian>(p + 12, bctr);
3959 // Write out long branch stubs.
3960 typename Branch_stub_entries::const_iterator bs;
3961 for (bs = this->long_branch_stubs_.begin();
3962 bs != this->long_branch_stubs_.end();
3965 p = oview + this->plt_size_ + bs->second;
3966 Address loc = this->stub_address() + this->plt_size_ + bs->second;
3967 Address delta = bs->first.dest_ - loc;
3968 if (delta + (1 << 25) < 2 << 25)
3969 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3970 else if (!parameters->options().output_is_position_independent())
3972 write_insn<big_endian>(p + 0, lis_12 + ha(bs->first.dest_));
3973 write_insn<big_endian>(p + 4, addi_12_12 + l(bs->first.dest_));
3974 write_insn<big_endian>(p + 8, mtctr_12);
3975 write_insn<big_endian>(p + 12, bctr);
3980 write_insn<big_endian>(p + 0, mflr_0);
3981 write_insn<big_endian>(p + 4, bcl_20_31);
3982 write_insn<big_endian>(p + 8, mflr_12);
3983 write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
3984 write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
3985 write_insn<big_endian>(p + 20, mtlr_0);
3986 write_insn<big_endian>(p + 24, mtctr_12);
3987 write_insn<big_endian>(p + 28, bctr);
3993 // Write out .glink.
3995 template<int size, bool big_endian>
3997 Output_data_glink<size, big_endian>::do_write(Output_file* of)
3999 const section_size_type off = this->offset();
4000 const section_size_type oview_size =
4001 convert_to_section_size_type(this->data_size());
4002 unsigned char* const oview = of->get_output_view(off, oview_size);
4005 // The base address of the .plt section.
4006 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4007 Address plt_base = this->targ_->plt_section()->address();
4011 // Write pltresolve stub.
4013 Address after_bcl = this->address() + 16;
4014 Address pltoff = plt_base - after_bcl;
4016 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
4018 write_insn<big_endian>(p, mflr_12), p += 4;
4019 write_insn<big_endian>(p, bcl_20_31), p += 4;
4020 write_insn<big_endian>(p, mflr_11), p += 4;
4021 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
4022 write_insn<big_endian>(p, mtlr_12), p += 4;
4023 write_insn<big_endian>(p, add_12_2_11), p += 4;
4024 write_insn<big_endian>(p, ld_11_12 + 0), p += 4;
4025 write_insn<big_endian>(p, ld_2_12 + 8), p += 4;
4026 write_insn<big_endian>(p, mtctr_11), p += 4;
4027 write_insn<big_endian>(p, ld_11_12 + 16), p += 4;
4028 write_insn<big_endian>(p, bctr), p += 4;
4029 while (p < oview + this->pltresolve_size)
4030 write_insn<big_endian>(p, nop), p += 4;
4032 // Write lazy link call stubs.
4034 while (p < oview + oview_size)
4038 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
4042 write_insn<big_endian>(p, lis_0_0 + hi(indx)), p += 4;
4043 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
4045 uint32_t branch_off = 8 - (p - oview);
4046 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
4052 const Output_data_got_powerpc<size, big_endian>* got
4053 = this->targ_->got_section();
4054 // The address of _GLOBAL_OFFSET_TABLE_.
4055 Address g_o_t = got->address() + got->g_o_t();
4057 // Write out pltresolve branch table.
4059 unsigned int the_end = oview_size - this->pltresolve_size;
4060 unsigned char* end_p = oview + the_end;
4061 while (p < end_p - 8 * 4)
4062 write_insn<big_endian>(p, b + end_p - p), p += 4;
4064 write_insn<big_endian>(p, nop), p += 4;
4066 // Write out pltresolve call stub.
4067 if (parameters->options().output_is_position_independent())
4069 Address res0_off = 0;
4070 Address after_bcl_off = the_end + 12;
4071 Address bcl_res0 = after_bcl_off - res0_off;
4073 write_insn<big_endian>(p + 0, addis_11_11 + ha(bcl_res0));
4074 write_insn<big_endian>(p + 4, mflr_0);
4075 write_insn<big_endian>(p + 8, bcl_20_31);
4076 write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4077 write_insn<big_endian>(p + 16, mflr_12);
4078 write_insn<big_endian>(p + 20, mtlr_0);
4079 write_insn<big_endian>(p + 24, sub_11_11_12);
4081 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4083 write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4084 if (ha(got_bcl) == ha(got_bcl + 4))
4086 write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4087 write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4091 write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4092 write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4094 write_insn<big_endian>(p + 40, mtctr_0);
4095 write_insn<big_endian>(p + 44, add_0_11_11);
4096 write_insn<big_endian>(p + 48, add_11_0_11);
4097 write_insn<big_endian>(p + 52, bctr);
4098 write_insn<big_endian>(p + 56, nop);
4099 write_insn<big_endian>(p + 60, nop);
4103 Address res0 = this->address();
4105 write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4106 write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4107 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4108 write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4110 write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4111 write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4112 write_insn<big_endian>(p + 16, mtctr_0);
4113 write_insn<big_endian>(p + 20, add_0_11_11);
4114 if (ha(g_o_t + 4) == ha(g_o_t + 8))
4115 write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4117 write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4118 write_insn<big_endian>(p + 28, add_11_0_11);
4119 write_insn<big_endian>(p + 32, bctr);
4120 write_insn<big_endian>(p + 36, nop);
4121 write_insn<big_endian>(p + 40, nop);
4122 write_insn<big_endian>(p + 44, nop);
4123 write_insn<big_endian>(p + 48, nop);
4124 write_insn<big_endian>(p + 52, nop);
4125 write_insn<big_endian>(p + 56, nop);
4126 write_insn<big_endian>(p + 60, nop);
4131 of->write_output_view(off, oview_size, oview);
4135 // A class to handle linker generated save/restore functions.
4137 template<int size, bool big_endian>
4138 class Output_data_save_res : public Output_section_data_build
4141 Output_data_save_res(Symbol_table* symtab);
4144 // Write to a map file.
4146 do_print_to_mapfile(Mapfile* mapfile) const
4147 { mapfile->print_output_data(this, _("** save/restore")); }
4150 do_write(Output_file*);
4153 // The maximum size of save/restore contents.
4154 static const unsigned int savres_max = 218*4;
4157 savres_define(Symbol_table* symtab,
4159 unsigned int lo, unsigned int hi,
4160 unsigned char* write_ent(unsigned char*, int),
4161 unsigned char* write_tail(unsigned char*, int));
4163 unsigned char *contents_;
4166 template<bool big_endian>
4167 static unsigned char*
4168 savegpr0(unsigned char* p, int r)
4170 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4171 write_insn<big_endian>(p, insn);
4175 template<bool big_endian>
4176 static unsigned char*
4177 savegpr0_tail(unsigned char* p, int r)
4179 p = savegpr0<big_endian>(p, r);
4180 uint32_t insn = std_0_1 + 16;
4181 write_insn<big_endian>(p, insn);
4183 write_insn<big_endian>(p, blr);
4187 template<bool big_endian>
4188 static unsigned char*
4189 restgpr0(unsigned char* p, int r)
4191 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4192 write_insn<big_endian>(p, insn);
4196 template<bool big_endian>
4197 static unsigned char*
4198 restgpr0_tail(unsigned char* p, int r)
4200 uint32_t insn = ld_0_1 + 16;
4201 write_insn<big_endian>(p, insn);
4203 p = restgpr0<big_endian>(p, r);
4204 write_insn<big_endian>(p, mtlr_0);
4208 p = restgpr0<big_endian>(p, 30);
4209 p = restgpr0<big_endian>(p, 31);
4211 write_insn<big_endian>(p, blr);
4215 template<bool big_endian>
4216 static unsigned char*
4217 savegpr1(unsigned char* p, int r)
4219 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4220 write_insn<big_endian>(p, insn);
4224 template<bool big_endian>
4225 static unsigned char*
4226 savegpr1_tail(unsigned char* p, int r)
4228 p = savegpr1<big_endian>(p, r);
4229 write_insn<big_endian>(p, blr);
4233 template<bool big_endian>
4234 static unsigned char*
4235 restgpr1(unsigned char* p, int r)
4237 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4238 write_insn<big_endian>(p, insn);
4242 template<bool big_endian>
4243 static unsigned char*
4244 restgpr1_tail(unsigned char* p, int r)
4246 p = restgpr1<big_endian>(p, r);
4247 write_insn<big_endian>(p, blr);
4251 template<bool big_endian>
4252 static unsigned char*
4253 savefpr(unsigned char* p, int r)
4255 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4256 write_insn<big_endian>(p, insn);
4260 template<bool big_endian>
4261 static unsigned char*
4262 savefpr0_tail(unsigned char* p, int r)
4264 p = savefpr<big_endian>(p, r);
4265 write_insn<big_endian>(p, std_0_1 + 16);
4267 write_insn<big_endian>(p, blr);
4271 template<bool big_endian>
4272 static unsigned char*
4273 restfpr(unsigned char* p, int r)
4275 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4276 write_insn<big_endian>(p, insn);
4280 template<bool big_endian>
4281 static unsigned char*
4282 restfpr0_tail(unsigned char* p, int r)
4284 write_insn<big_endian>(p, ld_0_1 + 16);
4286 p = restfpr<big_endian>(p, r);
4287 write_insn<big_endian>(p, mtlr_0);
4291 p = restfpr<big_endian>(p, 30);
4292 p = restfpr<big_endian>(p, 31);
4294 write_insn<big_endian>(p, blr);
4298 template<bool big_endian>
4299 static unsigned char*
4300 savefpr1_tail(unsigned char* p, int r)
4302 p = savefpr<big_endian>(p, r);
4303 write_insn<big_endian>(p, blr);
4307 template<bool big_endian>
4308 static unsigned char*
4309 restfpr1_tail(unsigned char* p, int r)
4311 p = restfpr<big_endian>(p, r);
4312 write_insn<big_endian>(p, blr);
4316 template<bool big_endian>
4317 static unsigned char*
4318 savevr(unsigned char* p, int r)
4320 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4321 write_insn<big_endian>(p, insn);
4323 insn = stvx_0_12_0 + (r << 21);
4324 write_insn<big_endian>(p, insn);
4328 template<bool big_endian>
4329 static unsigned char*
4330 savevr_tail(unsigned char* p, int r)
4332 p = savevr<big_endian>(p, r);
4333 write_insn<big_endian>(p, blr);
4337 template<bool big_endian>
4338 static unsigned char*
4339 restvr(unsigned char* p, int r)
4341 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4342 write_insn<big_endian>(p, insn);
4344 insn = lvx_0_12_0 + (r << 21);
4345 write_insn<big_endian>(p, insn);
4349 template<bool big_endian>
4350 static unsigned char*
4351 restvr_tail(unsigned char* p, int r)
4353 p = restvr<big_endian>(p, r);
4354 write_insn<big_endian>(p, blr);
4359 template<int size, bool big_endian>
4360 Output_data_save_res<size, big_endian>::Output_data_save_res(
4361 Symbol_table* symtab)
4362 : Output_section_data_build(4),
4365 this->savres_define(symtab,
4366 "_savegpr0_", 14, 31,
4367 savegpr0<big_endian>, savegpr0_tail<big_endian>);
4368 this->savres_define(symtab,
4369 "_restgpr0_", 14, 29,
4370 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4371 this->savres_define(symtab,
4372 "_restgpr0_", 30, 31,
4373 restgpr0<big_endian>, restgpr0_tail<big_endian>);
4374 this->savres_define(symtab,
4375 "_savegpr1_", 14, 31,
4376 savegpr1<big_endian>, savegpr1_tail<big_endian>);
4377 this->savres_define(symtab,
4378 "_restgpr1_", 14, 31,
4379 restgpr1<big_endian>, restgpr1_tail<big_endian>);
4380 this->savres_define(symtab,
4381 "_savefpr_", 14, 31,
4382 savefpr<big_endian>, savefpr0_tail<big_endian>);
4383 this->savres_define(symtab,
4384 "_restfpr_", 14, 29,
4385 restfpr<big_endian>, restfpr0_tail<big_endian>);
4386 this->savres_define(symtab,
4387 "_restfpr_", 30, 31,
4388 restfpr<big_endian>, restfpr0_tail<big_endian>);
4389 this->savres_define(symtab,
4391 savefpr<big_endian>, savefpr1_tail<big_endian>);
4392 this->savres_define(symtab,
4394 restfpr<big_endian>, restfpr1_tail<big_endian>);
4395 this->savres_define(symtab,
4397 savevr<big_endian>, savevr_tail<big_endian>);
4398 this->savres_define(symtab,
4400 restvr<big_endian>, restvr_tail<big_endian>);
4403 template<int size, bool big_endian>
4405 Output_data_save_res<size, big_endian>::savres_define(
4406 Symbol_table* symtab,
4408 unsigned int lo, unsigned int hi,
4409 unsigned char* write_ent(unsigned char*, int),
4410 unsigned char* write_tail(unsigned char*, int))
4412 size_t len = strlen(name);
4413 bool writing = false;
4416 memcpy(sym, name, len);
4419 for (unsigned int i = lo; i <= hi; i++)
4421 sym[len + 0] = i / 10 + '0';
4422 sym[len + 1] = i % 10 + '0';
4423 Symbol* gsym = symtab->lookup(sym);
4424 bool refd = gsym != NULL && gsym->is_undefined();
4425 writing = writing || refd;
4428 if (this->contents_ == NULL)
4429 this->contents_ = new unsigned char[this->savres_max];
4431 section_size_type value = this->current_data_size();
4432 unsigned char* p = this->contents_ + value;
4434 p = write_ent(p, i);
4436 p = write_tail(p, i);
4437 section_size_type cur_size = p - this->contents_;
4438 this->set_current_data_size(cur_size);
4440 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4441 this, value, cur_size - value,
4442 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4443 elfcpp::STV_HIDDEN, 0, false, false);
4448 // Write out save/restore.
4450 template<int size, bool big_endian>
4452 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4454 const section_size_type off = this->offset();
4455 const section_size_type oview_size =
4456 convert_to_section_size_type(this->data_size());
4457 unsigned char* const oview = of->get_output_view(off, oview_size);
4458 memcpy(oview, this->contents_, oview_size);
4459 of->write_output_view(off, oview_size, oview);
4463 // Create the glink section.
4465 template<int size, bool big_endian>
4467 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4469 if (this->glink_ == NULL)
4471 this->glink_ = new Output_data_glink<size, big_endian>(this);
4472 this->glink_->add_eh_frame(layout);
4473 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4474 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4475 this->glink_, ORDER_TEXT, false);
4479 // Create a PLT entry for a global symbol.
4481 template<int size, bool big_endian>
4483 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
4487 if (gsym->type() == elfcpp::STT_GNU_IFUNC
4488 && gsym->can_use_relative_reloc(false))
4490 if (this->iplt_ == NULL)
4491 this->make_iplt_section(symtab, layout);
4492 this->iplt_->add_ifunc_entry(gsym);
4496 if (this->plt_ == NULL)
4497 this->make_plt_section(symtab, layout);
4498 this->plt_->add_entry(gsym);
4502 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4504 template<int size, bool big_endian>
4506 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
4507 Symbol_table* symtab,
4509 Sized_relobj_file<size, big_endian>* relobj,
4512 if (this->iplt_ == NULL)
4513 this->make_iplt_section(symtab, layout);
4514 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
4517 // Return the number of entries in the PLT.
4519 template<int size, bool big_endian>
4521 Target_powerpc<size, big_endian>::plt_entry_count() const
4523 if (this->plt_ == NULL)
4525 unsigned int count = this->plt_->entry_count();
4526 if (this->iplt_ != NULL)
4527 count += this->iplt_->entry_count();
4531 // Return the offset of the first non-reserved PLT entry.
4533 template<int size, bool big_endian>
4535 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
4537 return this->plt_->first_plt_entry_offset();
4540 // Return the size of each PLT entry.
4542 template<int size, bool big_endian>
4544 Target_powerpc<size, big_endian>::plt_entry_size() const
4546 return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
4549 // Create a GOT entry for local dynamic __tls_get_addr calls.
4551 template<int size, bool big_endian>
4553 Target_powerpc<size, big_endian>::tlsld_got_offset(
4554 Symbol_table* symtab,
4556 Sized_relobj_file<size, big_endian>* object)
4558 if (this->tlsld_got_offset_ == -1U)
4560 gold_assert(symtab != NULL && layout != NULL && object != NULL);
4561 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4562 Output_data_got_powerpc<size, big_endian>* got
4563 = this->got_section(symtab, layout);
4564 unsigned int got_offset = got->add_constant_pair(0, 0);
4565 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
4567 this->tlsld_got_offset_ = got_offset;
4569 return this->tlsld_got_offset_;
4572 // Get the Reference_flags for a particular relocation.
4574 template<int size, bool big_endian>
4576 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
4580 case elfcpp::R_POWERPC_NONE:
4581 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4582 case elfcpp::R_POWERPC_GNU_VTENTRY:
4583 case elfcpp::R_PPC64_TOC:
4584 // No symbol reference.
4587 case elfcpp::R_PPC64_ADDR64:
4588 case elfcpp::R_PPC64_UADDR64:
4589 case elfcpp::R_POWERPC_ADDR32:
4590 case elfcpp::R_POWERPC_UADDR32:
4591 case elfcpp::R_POWERPC_ADDR16:
4592 case elfcpp::R_POWERPC_UADDR16:
4593 case elfcpp::R_POWERPC_ADDR16_LO:
4594 case elfcpp::R_POWERPC_ADDR16_HI:
4595 case elfcpp::R_POWERPC_ADDR16_HA:
4596 return Symbol::ABSOLUTE_REF;
4598 case elfcpp::R_POWERPC_ADDR24:
4599 case elfcpp::R_POWERPC_ADDR14:
4600 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4601 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4602 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
4604 case elfcpp::R_PPC64_REL64:
4605 case elfcpp::R_POWERPC_REL32:
4606 case elfcpp::R_PPC_LOCAL24PC:
4607 case elfcpp::R_POWERPC_REL16:
4608 case elfcpp::R_POWERPC_REL16_LO:
4609 case elfcpp::R_POWERPC_REL16_HI:
4610 case elfcpp::R_POWERPC_REL16_HA:
4611 return Symbol::RELATIVE_REF;
4613 case elfcpp::R_POWERPC_REL24:
4614 case elfcpp::R_PPC_PLTREL24:
4615 case elfcpp::R_POWERPC_REL14:
4616 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4617 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4618 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
4620 case elfcpp::R_POWERPC_GOT16:
4621 case elfcpp::R_POWERPC_GOT16_LO:
4622 case elfcpp::R_POWERPC_GOT16_HI:
4623 case elfcpp::R_POWERPC_GOT16_HA:
4624 case elfcpp::R_PPC64_GOT16_DS:
4625 case elfcpp::R_PPC64_GOT16_LO_DS:
4626 case elfcpp::R_PPC64_TOC16:
4627 case elfcpp::R_PPC64_TOC16_LO:
4628 case elfcpp::R_PPC64_TOC16_HI:
4629 case elfcpp::R_PPC64_TOC16_HA:
4630 case elfcpp::R_PPC64_TOC16_DS:
4631 case elfcpp::R_PPC64_TOC16_LO_DS:
4633 return Symbol::ABSOLUTE_REF;
4635 case elfcpp::R_POWERPC_GOT_TPREL16:
4636 case elfcpp::R_POWERPC_TLS:
4637 return Symbol::TLS_REF;
4639 case elfcpp::R_POWERPC_COPY:
4640 case elfcpp::R_POWERPC_GLOB_DAT:
4641 case elfcpp::R_POWERPC_JMP_SLOT:
4642 case elfcpp::R_POWERPC_RELATIVE:
4643 case elfcpp::R_POWERPC_DTPMOD:
4645 // Not expected. We will give an error later.
4650 // Report an unsupported relocation against a local symbol.
4652 template<int size, bool big_endian>
4654 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
4655 Sized_relobj_file<size, big_endian>* object,
4656 unsigned int r_type)
4658 gold_error(_("%s: unsupported reloc %u against local symbol"),
4659 object->name().c_str(), r_type);
4662 // We are about to emit a dynamic relocation of type R_TYPE. If the
4663 // dynamic linker does not support it, issue an error.
4665 template<int size, bool big_endian>
4667 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
4668 unsigned int r_type)
4670 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
4672 // These are the relocation types supported by glibc for both 32-bit
4673 // and 64-bit powerpc.
4676 case elfcpp::R_POWERPC_NONE:
4677 case elfcpp::R_POWERPC_RELATIVE:
4678 case elfcpp::R_POWERPC_GLOB_DAT:
4679 case elfcpp::R_POWERPC_DTPMOD:
4680 case elfcpp::R_POWERPC_DTPREL:
4681 case elfcpp::R_POWERPC_TPREL:
4682 case elfcpp::R_POWERPC_JMP_SLOT:
4683 case elfcpp::R_POWERPC_COPY:
4684 case elfcpp::R_POWERPC_IRELATIVE:
4685 case elfcpp::R_POWERPC_ADDR32:
4686 case elfcpp::R_POWERPC_UADDR32:
4687 case elfcpp::R_POWERPC_ADDR24:
4688 case elfcpp::R_POWERPC_ADDR16:
4689 case elfcpp::R_POWERPC_UADDR16:
4690 case elfcpp::R_POWERPC_ADDR16_LO:
4691 case elfcpp::R_POWERPC_ADDR16_HI:
4692 case elfcpp::R_POWERPC_ADDR16_HA:
4693 case elfcpp::R_POWERPC_ADDR14:
4694 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4695 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4696 case elfcpp::R_POWERPC_REL32:
4697 case elfcpp::R_POWERPC_REL24:
4698 case elfcpp::R_POWERPC_TPREL16:
4699 case elfcpp::R_POWERPC_TPREL16_LO:
4700 case elfcpp::R_POWERPC_TPREL16_HI:
4701 case elfcpp::R_POWERPC_TPREL16_HA:
4712 // These are the relocation types supported only on 64-bit.
4713 case elfcpp::R_PPC64_ADDR64:
4714 case elfcpp::R_PPC64_UADDR64:
4715 case elfcpp::R_PPC64_JMP_IREL:
4716 case elfcpp::R_PPC64_ADDR16_DS:
4717 case elfcpp::R_PPC64_ADDR16_LO_DS:
4718 case elfcpp::R_PPC64_ADDR16_HIGHER:
4719 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4720 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4721 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4722 case elfcpp::R_PPC64_REL64:
4723 case elfcpp::R_POWERPC_ADDR30:
4724 case elfcpp::R_PPC64_TPREL16_DS:
4725 case elfcpp::R_PPC64_TPREL16_LO_DS:
4726 case elfcpp::R_PPC64_TPREL16_HIGHER:
4727 case elfcpp::R_PPC64_TPREL16_HIGHEST:
4728 case elfcpp::R_PPC64_TPREL16_HIGHERA:
4729 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4740 // These are the relocation types supported only on 32-bit.
4741 // ??? glibc ld.so doesn't need to support these.
4742 case elfcpp::R_POWERPC_DTPREL16:
4743 case elfcpp::R_POWERPC_DTPREL16_LO:
4744 case elfcpp::R_POWERPC_DTPREL16_HI:
4745 case elfcpp::R_POWERPC_DTPREL16_HA:
4753 // This prevents us from issuing more than one error per reloc
4754 // section. But we can still wind up issuing more than one
4755 // error per object file.
4756 if (this->issued_non_pic_error_)
4758 gold_assert(parameters->options().output_is_position_independent());
4759 object->error(_("requires unsupported dynamic reloc; "
4760 "recompile with -fPIC"));
4761 this->issued_non_pic_error_ = true;
4765 // Return whether we need to make a PLT entry for a relocation of the
4766 // given type against a STT_GNU_IFUNC symbol.
4768 template<int size, bool big_endian>
4770 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4771 Sized_relobj_file<size, big_endian>* object,
4772 unsigned int r_type)
4774 // In non-pic code any reference will resolve to the plt call stub
4775 // for the ifunc symbol.
4776 if (size == 32 && !parameters->options().output_is_position_independent())
4781 // Word size refs from data sections are OK.
4782 case elfcpp::R_POWERPC_ADDR32:
4783 case elfcpp::R_POWERPC_UADDR32:
4788 case elfcpp::R_PPC64_ADDR64:
4789 case elfcpp::R_PPC64_UADDR64:
4794 // GOT refs are good.
4795 case elfcpp::R_POWERPC_GOT16:
4796 case elfcpp::R_POWERPC_GOT16_LO:
4797 case elfcpp::R_POWERPC_GOT16_HI:
4798 case elfcpp::R_POWERPC_GOT16_HA:
4799 case elfcpp::R_PPC64_GOT16_DS:
4800 case elfcpp::R_PPC64_GOT16_LO_DS:
4803 // So are function calls.
4804 case elfcpp::R_POWERPC_ADDR24:
4805 case elfcpp::R_POWERPC_ADDR14:
4806 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4807 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4808 case elfcpp::R_POWERPC_REL24:
4809 case elfcpp::R_PPC_PLTREL24:
4810 case elfcpp::R_POWERPC_REL14:
4811 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4812 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4819 // Anything else is a problem.
4820 // If we are building a static executable, the libc startup function
4821 // responsible for applying indirect function relocations is going
4822 // to complain about the reloc type.
4823 // If we are building a dynamic executable, we will have a text
4824 // relocation. The dynamic loader will set the text segment
4825 // writable and non-executable to apply text relocations. So we'll
4826 // segfault when trying to run the indirection function to resolve
4828 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4829 object->name().c_str(), r_type);
4833 // Scan a relocation for a local symbol.
4835 template<int size, bool big_endian>
4837 Target_powerpc<size, big_endian>::Scan::local(
4838 Symbol_table* symtab,
4840 Target_powerpc<size, big_endian>* target,
4841 Sized_relobj_file<size, big_endian>* object,
4842 unsigned int data_shndx,
4843 Output_section* output_section,
4844 const elfcpp::Rela<size, big_endian>& reloc,
4845 unsigned int r_type,
4846 const elfcpp::Sym<size, big_endian>& lsym,
4849 this->maybe_skip_tls_get_addr_call(r_type, NULL);
4851 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4852 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4854 this->expect_tls_get_addr_call();
4855 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
4856 if (tls_type != tls::TLSOPT_NONE)
4857 this->skip_next_tls_get_addr_call();
4859 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4860 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4862 this->expect_tls_get_addr_call();
4863 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4864 if (tls_type != tls::TLSOPT_NONE)
4865 this->skip_next_tls_get_addr_call();
4868 Powerpc_relobj<size, big_endian>* ppc_object
4869 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4874 && data_shndx == ppc_object->opd_shndx()
4875 && r_type == elfcpp::R_PPC64_ADDR64)
4876 ppc_object->set_opd_discard(reloc.get_r_offset());
4880 // A local STT_GNU_IFUNC symbol may require a PLT entry.
4881 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4882 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
4884 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4885 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4886 r_type, r_sym, reloc.get_r_addend());
4887 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4892 case elfcpp::R_POWERPC_NONE:
4893 case elfcpp::R_POWERPC_GNU_VTINHERIT:
4894 case elfcpp::R_POWERPC_GNU_VTENTRY:
4895 case elfcpp::R_PPC64_TOCSAVE:
4896 case elfcpp::R_PPC_EMB_MRKREF:
4897 case elfcpp::R_POWERPC_TLS:
4900 case elfcpp::R_PPC64_TOC:
4902 Output_data_got_powerpc<size, big_endian>* got
4903 = target->got_section(symtab, layout);
4904 if (parameters->options().output_is_position_independent())
4906 Address off = reloc.get_r_offset();
4908 && data_shndx == ppc_object->opd_shndx()
4909 && ppc_object->get_opd_discard(off - 8))
4912 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4913 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
4914 rela_dyn->add_output_section_relative(got->output_section(),
4915 elfcpp::R_POWERPC_RELATIVE,
4917 object, data_shndx, off,
4918 symobj->toc_base_offset());
4923 case elfcpp::R_PPC64_ADDR64:
4924 case elfcpp::R_PPC64_UADDR64:
4925 case elfcpp::R_POWERPC_ADDR32:
4926 case elfcpp::R_POWERPC_UADDR32:
4927 case elfcpp::R_POWERPC_ADDR24:
4928 case elfcpp::R_POWERPC_ADDR16:
4929 case elfcpp::R_POWERPC_ADDR16_LO:
4930 case elfcpp::R_POWERPC_ADDR16_HI:
4931 case elfcpp::R_POWERPC_ADDR16_HA:
4932 case elfcpp::R_POWERPC_UADDR16:
4933 case elfcpp::R_PPC64_ADDR16_HIGHER:
4934 case elfcpp::R_PPC64_ADDR16_HIGHERA:
4935 case elfcpp::R_PPC64_ADDR16_HIGHEST:
4936 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4937 case elfcpp::R_PPC64_ADDR16_DS:
4938 case elfcpp::R_PPC64_ADDR16_LO_DS:
4939 case elfcpp::R_POWERPC_ADDR14:
4940 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4941 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4942 // If building a shared library (or a position-independent
4943 // executable), we need to create a dynamic relocation for
4945 if (parameters->options().output_is_position_independent()
4946 || (size == 64 && is_ifunc))
4948 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4950 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
4951 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
4953 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4954 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4957 rela_dyn = target->iplt_section()->rel_plt();
4958 dynrel = elfcpp::R_POWERPC_IRELATIVE;
4960 rela_dyn->add_local_relative(object, r_sym, dynrel,
4961 output_section, data_shndx,
4962 reloc.get_r_offset(),
4963 reloc.get_r_addend(), false);
4967 check_non_pic(object, r_type);
4968 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4969 rela_dyn->add_local(object, r_sym, r_type, output_section,
4970 data_shndx, reloc.get_r_offset(),
4971 reloc.get_r_addend());
4976 case elfcpp::R_POWERPC_REL24:
4977 case elfcpp::R_PPC_PLTREL24:
4978 case elfcpp::R_PPC_LOCAL24PC:
4979 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4980 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4981 reloc.get_r_addend());
4984 case elfcpp::R_POWERPC_REL14:
4985 case elfcpp::R_POWERPC_REL14_BRTAKEN:
4986 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4987 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4988 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4989 reloc.get_r_addend());
4992 case elfcpp::R_PPC64_REL64:
4993 case elfcpp::R_POWERPC_REL32:
4994 case elfcpp::R_POWERPC_REL16:
4995 case elfcpp::R_POWERPC_REL16_LO:
4996 case elfcpp::R_POWERPC_REL16_HI:
4997 case elfcpp::R_POWERPC_REL16_HA:
4998 case elfcpp::R_POWERPC_SECTOFF:
4999 case elfcpp::R_POWERPC_TPREL16:
5000 case elfcpp::R_POWERPC_DTPREL16:
5001 case elfcpp::R_POWERPC_SECTOFF_LO:
5002 case elfcpp::R_POWERPC_TPREL16_LO:
5003 case elfcpp::R_POWERPC_DTPREL16_LO:
5004 case elfcpp::R_POWERPC_SECTOFF_HI:
5005 case elfcpp::R_POWERPC_TPREL16_HI:
5006 case elfcpp::R_POWERPC_DTPREL16_HI:
5007 case elfcpp::R_POWERPC_SECTOFF_HA:
5008 case elfcpp::R_POWERPC_TPREL16_HA:
5009 case elfcpp::R_POWERPC_DTPREL16_HA:
5010 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5011 case elfcpp::R_PPC64_TPREL16_HIGHER:
5012 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5013 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5014 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5015 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5016 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5017 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5018 case elfcpp::R_PPC64_TPREL16_DS:
5019 case elfcpp::R_PPC64_TPREL16_LO_DS:
5020 case elfcpp::R_PPC64_DTPREL16_DS:
5021 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5022 case elfcpp::R_PPC64_SECTOFF_DS:
5023 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5024 case elfcpp::R_PPC64_TLSGD:
5025 case elfcpp::R_PPC64_TLSLD:
5028 case elfcpp::R_POWERPC_GOT16:
5029 case elfcpp::R_POWERPC_GOT16_LO:
5030 case elfcpp::R_POWERPC_GOT16_HI:
5031 case elfcpp::R_POWERPC_GOT16_HA:
5032 case elfcpp::R_PPC64_GOT16_DS:
5033 case elfcpp::R_PPC64_GOT16_LO_DS:
5035 // The symbol requires a GOT entry.
5036 Output_data_got_powerpc<size, big_endian>* got
5037 = target->got_section(symtab, layout);
5038 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5040 if (!parameters->options().output_is_position_independent())
5042 if (size == 32 && is_ifunc)
5043 got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5045 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5047 else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5049 // If we are generating a shared object or a pie, this
5050 // symbol's GOT entry will be set by a dynamic relocation.
5052 off = got->add_constant(0);
5053 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5055 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5056 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
5059 rela_dyn = target->iplt_section()->rel_plt();
5060 dynrel = elfcpp::R_POWERPC_IRELATIVE;
5062 rela_dyn->add_local_relative(object, r_sym, dynrel,
5063 got, off, 0, false);
5068 case elfcpp::R_PPC64_TOC16:
5069 case elfcpp::R_PPC64_TOC16_LO:
5070 case elfcpp::R_PPC64_TOC16_HI:
5071 case elfcpp::R_PPC64_TOC16_HA:
5072 case elfcpp::R_PPC64_TOC16_DS:
5073 case elfcpp::R_PPC64_TOC16_LO_DS:
5074 // We need a GOT section.
5075 target->got_section(symtab, layout);
5078 case elfcpp::R_POWERPC_GOT_TLSGD16:
5079 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5080 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5081 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5083 const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5084 if (tls_type == tls::TLSOPT_NONE)
5086 Output_data_got_powerpc<size, big_endian>* got
5087 = target->got_section(symtab, layout);
5088 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5089 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5090 got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5091 rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5093 else if (tls_type == tls::TLSOPT_TO_LE)
5095 // no GOT relocs needed for Local Exec.
5102 case elfcpp::R_POWERPC_GOT_TLSLD16:
5103 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5104 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5105 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5107 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5108 if (tls_type == tls::TLSOPT_NONE)
5109 target->tlsld_got_offset(symtab, layout, object);
5110 else if (tls_type == tls::TLSOPT_TO_LE)
5112 // no GOT relocs needed for Local Exec.
5113 if (parameters->options().emit_relocs())
5115 Output_section* os = layout->tls_segment()->first_section();
5116 gold_assert(os != NULL);
5117 os->set_needs_symtab_index();
5125 case elfcpp::R_POWERPC_GOT_DTPREL16:
5126 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5127 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5128 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5130 Output_data_got_powerpc<size, big_endian>* got
5131 = target->got_section(symtab, layout);
5132 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5133 got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5137 case elfcpp::R_POWERPC_GOT_TPREL16:
5138 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5139 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5140 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5142 const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5143 if (tls_type == tls::TLSOPT_NONE)
5145 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5146 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5148 Output_data_got_powerpc<size, big_endian>* got
5149 = target->got_section(symtab, layout);
5150 unsigned int off = got->add_constant(0);
5151 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5153 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5154 rela_dyn->add_symbolless_local_addend(object, r_sym,
5155 elfcpp::R_POWERPC_TPREL,
5159 else if (tls_type == tls::TLSOPT_TO_LE)
5161 // no GOT relocs needed for Local Exec.
5169 unsupported_reloc_local(object, r_type);
5175 case elfcpp::R_POWERPC_GOT_TLSLD16:
5176 case elfcpp::R_POWERPC_GOT_TLSGD16:
5177 case elfcpp::R_POWERPC_GOT_TPREL16:
5178 case elfcpp::R_POWERPC_GOT_DTPREL16:
5179 case elfcpp::R_POWERPC_GOT16:
5180 case elfcpp::R_PPC64_GOT16_DS:
5181 case elfcpp::R_PPC64_TOC16:
5182 case elfcpp::R_PPC64_TOC16_DS:
5183 ppc_object->set_has_small_toc_reloc();
5189 // Report an unsupported relocation against a global symbol.
5191 template<int size, bool big_endian>
5193 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5194 Sized_relobj_file<size, big_endian>* object,
5195 unsigned int r_type,
5198 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5199 object->name().c_str(), r_type, gsym->demangled_name().c_str());
5202 // Scan a relocation for a global symbol.
5204 template<int size, bool big_endian>
5206 Target_powerpc<size, big_endian>::Scan::global(
5207 Symbol_table* symtab,
5209 Target_powerpc<size, big_endian>* target,
5210 Sized_relobj_file<size, big_endian>* object,
5211 unsigned int data_shndx,
5212 Output_section* output_section,
5213 const elfcpp::Rela<size, big_endian>& reloc,
5214 unsigned int r_type,
5217 if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5220 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5221 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5223 this->expect_tls_get_addr_call();
5224 const bool final = gsym->final_value_is_known();
5225 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5226 if (tls_type != tls::TLSOPT_NONE)
5227 this->skip_next_tls_get_addr_call();
5229 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5230 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5232 this->expect_tls_get_addr_call();
5233 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5234 if (tls_type != tls::TLSOPT_NONE)
5235 this->skip_next_tls_get_addr_call();
5238 Powerpc_relobj<size, big_endian>* ppc_object
5239 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5241 // A STT_GNU_IFUNC symbol may require a PLT entry.
5242 if (gsym->type() == elfcpp::STT_GNU_IFUNC
5243 && this->reloc_needs_plt_for_ifunc(object, r_type))
5245 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5246 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5247 reloc.get_r_addend());
5248 target->make_plt_entry(symtab, layout, gsym);
5253 case elfcpp::R_POWERPC_NONE:
5254 case elfcpp::R_POWERPC_GNU_VTINHERIT:
5255 case elfcpp::R_POWERPC_GNU_VTENTRY:
5256 case elfcpp::R_PPC_LOCAL24PC:
5257 case elfcpp::R_PPC_EMB_MRKREF:
5258 case elfcpp::R_POWERPC_TLS:
5261 case elfcpp::R_PPC64_TOC:
5263 Output_data_got_powerpc<size, big_endian>* got
5264 = target->got_section(symtab, layout);
5265 if (parameters->options().output_is_position_independent())
5267 Address off = reloc.get_r_offset();
5269 && data_shndx == ppc_object->opd_shndx()
5270 && ppc_object->get_opd_discard(off - 8))
5273 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5274 Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5275 if (data_shndx != ppc_object->opd_shndx())
5276 symobj = static_cast
5277 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5278 rela_dyn->add_output_section_relative(got->output_section(),
5279 elfcpp::R_POWERPC_RELATIVE,
5281 object, data_shndx, off,
5282 symobj->toc_base_offset());
5287 case elfcpp::R_PPC64_ADDR64:
5289 && data_shndx == ppc_object->opd_shndx()
5290 && (gsym->is_defined_in_discarded_section()
5291 || gsym->object() != object))
5293 ppc_object->set_opd_discard(reloc.get_r_offset());
5297 case elfcpp::R_PPC64_UADDR64:
5298 case elfcpp::R_POWERPC_ADDR32:
5299 case elfcpp::R_POWERPC_UADDR32:
5300 case elfcpp::R_POWERPC_ADDR24:
5301 case elfcpp::R_POWERPC_ADDR16:
5302 case elfcpp::R_POWERPC_ADDR16_LO:
5303 case elfcpp::R_POWERPC_ADDR16_HI:
5304 case elfcpp::R_POWERPC_ADDR16_HA:
5305 case elfcpp::R_POWERPC_UADDR16:
5306 case elfcpp::R_PPC64_ADDR16_HIGHER:
5307 case elfcpp::R_PPC64_ADDR16_HIGHERA:
5308 case elfcpp::R_PPC64_ADDR16_HIGHEST:
5309 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5310 case elfcpp::R_PPC64_ADDR16_DS:
5311 case elfcpp::R_PPC64_ADDR16_LO_DS:
5312 case elfcpp::R_POWERPC_ADDR14:
5313 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5314 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5316 // Make a PLT entry if necessary.
5317 if (gsym->needs_plt_entry())
5319 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5321 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5322 reloc.get_r_addend());
5323 target->make_plt_entry(symtab, layout, gsym);
5324 // Since this is not a PC-relative relocation, we may be
5325 // taking the address of a function. In that case we need to
5326 // set the entry in the dynamic symbol table to the address of
5327 // the PLT call stub.
5329 && gsym->is_from_dynobj()
5330 && !parameters->options().output_is_position_independent())
5331 gsym->set_needs_dynsym_value();
5333 // Make a dynamic relocation if necessary.
5334 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
5335 || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
5337 if (gsym->may_need_copy_reloc())
5339 target->copy_reloc(symtab, layout, object,
5340 data_shndx, output_section, gsym, reloc);
5342 else if ((size == 32
5343 && r_type == elfcpp::R_POWERPC_ADDR32
5344 && gsym->can_use_relative_reloc(false)
5345 && !(gsym->visibility() == elfcpp::STV_PROTECTED
5346 && parameters->options().shared()))
5348 && r_type == elfcpp::R_PPC64_ADDR64
5349 && (gsym->can_use_relative_reloc(false)
5350 || data_shndx == ppc_object->opd_shndx())))
5352 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5353 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
5354 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
5356 rela_dyn = target->iplt_section()->rel_plt();
5357 dynrel = elfcpp::R_POWERPC_IRELATIVE;
5359 rela_dyn->add_symbolless_global_addend(
5360 gsym, dynrel, output_section, object, data_shndx,
5361 reloc.get_r_offset(), reloc.get_r_addend());
5365 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5366 check_non_pic(object, r_type);
5367 rela_dyn->add_global(gsym, r_type, output_section,
5369 reloc.get_r_offset(),
5370 reloc.get_r_addend());
5376 case elfcpp::R_PPC_PLTREL24:
5377 case elfcpp::R_POWERPC_REL24:
5378 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5379 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5380 reloc.get_r_addend());
5381 if (gsym->needs_plt_entry()
5382 || (!gsym->final_value_is_known()
5383 && (gsym->is_undefined()
5384 || gsym->is_from_dynobj()
5385 || gsym->is_preemptible())))
5386 target->make_plt_entry(symtab, layout, gsym);
5389 case elfcpp::R_PPC64_REL64:
5390 case elfcpp::R_POWERPC_REL32:
5391 // Make a dynamic relocation if necessary.
5392 if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
5394 if (gsym->may_need_copy_reloc())
5396 target->copy_reloc(symtab, layout, object,
5397 data_shndx, output_section, gsym,
5402 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5403 check_non_pic(object, r_type);
5404 rela_dyn->add_global(gsym, r_type, output_section, object,
5405 data_shndx, reloc.get_r_offset(),
5406 reloc.get_r_addend());
5411 case elfcpp::R_POWERPC_REL14:
5412 case elfcpp::R_POWERPC_REL14_BRTAKEN:
5413 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5414 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5415 r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5416 reloc.get_r_addend());
5419 case elfcpp::R_POWERPC_REL16:
5420 case elfcpp::R_POWERPC_REL16_LO:
5421 case elfcpp::R_POWERPC_REL16_HI:
5422 case elfcpp::R_POWERPC_REL16_HA:
5423 case elfcpp::R_POWERPC_SECTOFF:
5424 case elfcpp::R_POWERPC_TPREL16:
5425 case elfcpp::R_POWERPC_DTPREL16:
5426 case elfcpp::R_POWERPC_SECTOFF_LO:
5427 case elfcpp::R_POWERPC_TPREL16_LO:
5428 case elfcpp::R_POWERPC_DTPREL16_LO:
5429 case elfcpp::R_POWERPC_SECTOFF_HI:
5430 case elfcpp::R_POWERPC_TPREL16_HI:
5431 case elfcpp::R_POWERPC_DTPREL16_HI:
5432 case elfcpp::R_POWERPC_SECTOFF_HA:
5433 case elfcpp::R_POWERPC_TPREL16_HA:
5434 case elfcpp::R_POWERPC_DTPREL16_HA:
5435 case elfcpp::R_PPC64_DTPREL16_HIGHER:
5436 case elfcpp::R_PPC64_TPREL16_HIGHER:
5437 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5438 case elfcpp::R_PPC64_TPREL16_HIGHERA:
5439 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5440 case elfcpp::R_PPC64_TPREL16_HIGHEST:
5441 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5442 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5443 case elfcpp::R_PPC64_TPREL16_DS:
5444 case elfcpp::R_PPC64_TPREL16_LO_DS:
5445 case elfcpp::R_PPC64_DTPREL16_DS:
5446 case elfcpp::R_PPC64_DTPREL16_LO_DS:
5447 case elfcpp::R_PPC64_SECTOFF_DS:
5448 case elfcpp::R_PPC64_SECTOFF_LO_DS:
5449 case elfcpp::R_PPC64_TLSGD:
5450 case elfcpp::R_PPC64_TLSLD:
5453 case elfcpp::R_POWERPC_GOT16:
5454 case elfcpp::R_POWERPC_GOT16_LO:
5455 case elfcpp::R_POWERPC_GOT16_HI:
5456 case elfcpp::R_POWERPC_GOT16_HA:
5457 case elfcpp::R_PPC64_GOT16_DS:
5458 case elfcpp::R_PPC64_GOT16_LO_DS:
5460 // The symbol requires a GOT entry.
5461 Output_data_got_powerpc<size, big_endian>* got;
5463 got = target->got_section(symtab, layout);
5464 if (gsym->final_value_is_known())
5466 if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
5467 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5469 got->add_global(gsym, GOT_TYPE_STANDARD);
5471 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
5473 // If we are generating a shared object or a pie, this
5474 // symbol's GOT entry will be set by a dynamic relocation.
5475 unsigned int off = got->add_constant(0);
5476 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
5478 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5479 if (gsym->can_use_relative_reloc(false)
5481 && gsym->visibility() == elfcpp::STV_PROTECTED
5482 && parameters->options().shared()))
5484 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
5485 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
5487 rela_dyn = target->iplt_section()->rel_plt();
5488 dynrel = elfcpp::R_POWERPC_IRELATIVE;
5490 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
5494 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5495 rela_dyn->add_global(gsym, dynrel, got, off, 0);
5501 case elfcpp::R_PPC64_TOC16:
5502 case elfcpp::R_PPC64_TOC16_LO:
5503 case elfcpp::R_PPC64_TOC16_HI:
5504 case elfcpp::R_PPC64_TOC16_HA:
5505 case elfcpp::R_PPC64_TOC16_DS:
5506 case elfcpp::R_PPC64_TOC16_LO_DS:
5507 // We need a GOT section.
5508 target->got_section(symtab, layout);
5511 case elfcpp::R_POWERPC_GOT_TLSGD16:
5512 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5513 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5514 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5516 const bool final = gsym->final_value_is_known();
5517 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5518 if (tls_type == tls::TLSOPT_NONE)
5520 Output_data_got_powerpc<size, big_endian>* got
5521 = target->got_section(symtab, layout);
5522 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
5523 target->rela_dyn_section(layout),
5524 elfcpp::R_POWERPC_DTPMOD,
5525 elfcpp::R_POWERPC_DTPREL);
5527 else if (tls_type == tls::TLSOPT_TO_IE)
5529 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5531 Output_data_got_powerpc<size, big_endian>* got
5532 = target->got_section(symtab, layout);
5533 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5534 if (gsym->is_undefined()
5535 || gsym->is_from_dynobj())
5537 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5538 elfcpp::R_POWERPC_TPREL);
5542 unsigned int off = got->add_constant(0);
5543 gsym->set_got_offset(GOT_TYPE_TPREL, off);
5544 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5545 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5550 else if (tls_type == tls::TLSOPT_TO_LE)
5552 // no GOT relocs needed for Local Exec.
5559 case elfcpp::R_POWERPC_GOT_TLSLD16:
5560 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5561 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5562 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5564 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5565 if (tls_type == tls::TLSOPT_NONE)
5566 target->tlsld_got_offset(symtab, layout, object);
5567 else if (tls_type == tls::TLSOPT_TO_LE)
5569 // no GOT relocs needed for Local Exec.
5570 if (parameters->options().emit_relocs())
5572 Output_section* os = layout->tls_segment()->first_section();
5573 gold_assert(os != NULL);
5574 os->set_needs_symtab_index();
5582 case elfcpp::R_POWERPC_GOT_DTPREL16:
5583 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5584 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5585 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5587 Output_data_got_powerpc<size, big_endian>* got
5588 = target->got_section(symtab, layout);
5589 if (!gsym->final_value_is_known()
5590 && (gsym->is_from_dynobj()
5591 || gsym->is_undefined()
5592 || gsym->is_preemptible()))
5593 got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
5594 target->rela_dyn_section(layout),
5595 elfcpp::R_POWERPC_DTPREL);
5597 got->add_global_tls(gsym, GOT_TYPE_DTPREL);
5601 case elfcpp::R_POWERPC_GOT_TPREL16:
5602 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5603 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5604 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5606 const bool final = gsym->final_value_is_known();
5607 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5608 if (tls_type == tls::TLSOPT_NONE)
5610 if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5612 Output_data_got_powerpc<size, big_endian>* got
5613 = target->got_section(symtab, layout);
5614 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5615 if (gsym->is_undefined()
5616 || gsym->is_from_dynobj())
5618 got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5619 elfcpp::R_POWERPC_TPREL);
5623 unsigned int off = got->add_constant(0);
5624 gsym->set_got_offset(GOT_TYPE_TPREL, off);
5625 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5626 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5631 else if (tls_type == tls::TLSOPT_TO_LE)
5633 // no GOT relocs needed for Local Exec.
5641 unsupported_reloc_global(object, r_type, gsym);
5647 case elfcpp::R_POWERPC_GOT_TLSLD16:
5648 case elfcpp::R_POWERPC_GOT_TLSGD16:
5649 case elfcpp::R_POWERPC_GOT_TPREL16:
5650 case elfcpp::R_POWERPC_GOT_DTPREL16:
5651 case elfcpp::R_POWERPC_GOT16:
5652 case elfcpp::R_PPC64_GOT16_DS:
5653 case elfcpp::R_PPC64_TOC16:
5654 case elfcpp::R_PPC64_TOC16_DS:
5655 ppc_object->set_has_small_toc_reloc();
5661 // Process relocations for gc.
5663 template<int size, bool big_endian>
5665 Target_powerpc<size, big_endian>::gc_process_relocs(
5666 Symbol_table* symtab,
5668 Sized_relobj_file<size, big_endian>* object,
5669 unsigned int data_shndx,
5671 const unsigned char* prelocs,
5673 Output_section* output_section,
5674 bool needs_special_offset_handling,
5675 size_t local_symbol_count,
5676 const unsigned char* plocal_symbols)
5678 typedef Target_powerpc<size, big_endian> Powerpc;
5679 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5680 Powerpc_relobj<size, big_endian>* ppc_object
5681 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5683 ppc_object->set_opd_valid();
5684 if (size == 64 && data_shndx == ppc_object->opd_shndx())
5686 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
5687 for (p = ppc_object->access_from_map()->begin();
5688 p != ppc_object->access_from_map()->end();
5691 Address dst_off = p->first;
5692 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5693 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
5694 for (s = p->second.begin(); s != p->second.end(); ++s)
5696 Object* src_obj = s->first;
5697 unsigned int src_indx = s->second;
5698 symtab->gc()->add_reference(src_obj, src_indx,
5699 ppc_object, dst_indx);
5703 ppc_object->access_from_map()->clear();
5704 ppc_object->process_gc_mark(symtab);
5705 // Don't look at .opd relocs as .opd will reference everything.
5709 gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
5710 typename Target_powerpc::Relocatable_size_for_reloc>(
5719 needs_special_offset_handling,
5724 // Handle target specific gc actions when adding a gc reference from
5725 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5726 // and DST_OFF. For powerpc64, this adds a referenc to the code
5727 // section of a function descriptor.
5729 template<int size, bool big_endian>
5731 Target_powerpc<size, big_endian>::do_gc_add_reference(
5732 Symbol_table* symtab,
5734 unsigned int src_shndx,
5736 unsigned int dst_shndx,
5737 Address dst_off) const
5739 if (size != 64 || dst_obj->is_dynamic())
5742 Powerpc_relobj<size, big_endian>* ppc_object
5743 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
5744 if (dst_shndx == ppc_object->opd_shndx())
5746 if (ppc_object->opd_valid())
5748 dst_shndx = ppc_object->get_opd_ent(dst_off);
5749 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
5753 // If we haven't run scan_opd_relocs, we must delay
5754 // processing this function descriptor reference.
5755 ppc_object->add_reference(src_obj, src_shndx, dst_off);
5760 // Add any special sections for this symbol to the gc work list.
5761 // For powerpc64, this adds the code section of a function
5764 template<int size, bool big_endian>
5766 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
5767 Symbol_table* symtab,
5772 Powerpc_relobj<size, big_endian>* ppc_object
5773 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
5775 unsigned int shndx = sym->shndx(&is_ordinary);
5776 if (is_ordinary && shndx == ppc_object->opd_shndx())
5778 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
5779 Address dst_off = gsym->value();
5780 if (ppc_object->opd_valid())
5782 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5783 symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
5786 ppc_object->add_gc_mark(dst_off);
5791 // For a symbol location in .opd, set LOC to the location of the
5794 template<int size, bool big_endian>
5796 Target_powerpc<size, big_endian>::do_function_location(
5797 Symbol_location* loc) const
5801 if (loc->object->is_dynamic())
5803 Powerpc_dynobj<size, big_endian>* ppc_object
5804 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
5805 if (loc->shndx == ppc_object->opd_shndx())
5808 Address off = loc->offset - ppc_object->opd_address();
5809 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
5810 loc->offset = dest_off;
5815 const Powerpc_relobj<size, big_endian>* ppc_object
5816 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
5817 if (loc->shndx == ppc_object->opd_shndx())
5820 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
5821 loc->offset = dest_off;
5827 // Scan relocations for a section.
5829 template<int size, bool big_endian>
5831 Target_powerpc<size, big_endian>::scan_relocs(
5832 Symbol_table* symtab,
5834 Sized_relobj_file<size, big_endian>* object,
5835 unsigned int data_shndx,
5836 unsigned int sh_type,
5837 const unsigned char* prelocs,
5839 Output_section* output_section,
5840 bool needs_special_offset_handling,
5841 size_t local_symbol_count,
5842 const unsigned char* plocal_symbols)
5844 typedef Target_powerpc<size, big_endian> Powerpc;
5845 typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5847 if (sh_type == elfcpp::SHT_REL)
5849 gold_error(_("%s: unsupported REL reloc section"),
5850 object->name().c_str());
5854 gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
5863 needs_special_offset_handling,
5868 // Functor class for processing the global symbol table.
5869 // Removes symbols defined on discarded opd entries.
5871 template<bool big_endian>
5872 class Global_symbol_visitor_opd
5875 Global_symbol_visitor_opd()
5879 operator()(Sized_symbol<64>* sym)
5881 if (sym->has_symtab_index()
5882 || sym->source() != Symbol::FROM_OBJECT
5883 || !sym->in_real_elf())
5886 if (sym->object()->is_dynamic())
5889 Powerpc_relobj<64, big_endian>* symobj
5890 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
5891 if (symobj->opd_shndx() == 0)
5895 unsigned int shndx = sym->shndx(&is_ordinary);
5896 if (shndx == symobj->opd_shndx()
5897 && symobj->get_opd_discard(sym->value()))
5898 sym->set_symtab_index(-1U);
5902 template<int size, bool big_endian>
5904 Target_powerpc<size, big_endian>::define_save_restore_funcs(
5906 Symbol_table* symtab)
5910 Output_data_save_res<64, big_endian>* savres
5911 = new Output_data_save_res<64, big_endian>(symtab);
5912 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5913 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5914 savres, ORDER_TEXT, false);
5918 // Sort linker created .got section first (for the header), then input
5919 // sections belonging to files using small model code.
5921 template<bool big_endian>
5922 class Sort_toc_sections
5926 operator()(const Output_section::Input_section& is1,
5927 const Output_section::Input_section& is2) const
5929 if (!is1.is_input_section() && is2.is_input_section())
5932 = (is1.is_input_section()
5933 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
5934 ->has_small_toc_reloc()));
5936 = (is2.is_input_section()
5937 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
5938 ->has_small_toc_reloc()));
5939 return small1 && !small2;
5943 // Finalize the sections.
5945 template<int size, bool big_endian>
5947 Target_powerpc<size, big_endian>::do_finalize_sections(
5949 const Input_objects*,
5950 Symbol_table* symtab)
5952 if (parameters->doing_static_link())
5954 // At least some versions of glibc elf-init.o have a strong
5955 // reference to __rela_iplt marker syms. A weak ref would be
5957 if (this->iplt_ != NULL)
5959 Reloc_section* rel = this->iplt_->rel_plt();
5960 symtab->define_in_output_data("__rela_iplt_start", NULL,
5961 Symbol_table::PREDEFINED, rel, 0, 0,
5962 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5963 elfcpp::STV_HIDDEN, 0, false, true);
5964 symtab->define_in_output_data("__rela_iplt_end", NULL,
5965 Symbol_table::PREDEFINED, rel, 0, 0,
5966 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5967 elfcpp::STV_HIDDEN, 0, true, true);
5971 symtab->define_as_constant("__rela_iplt_start", NULL,
5972 Symbol_table::PREDEFINED, 0, 0,
5973 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5974 elfcpp::STV_HIDDEN, 0, true, false);
5975 symtab->define_as_constant("__rela_iplt_end", NULL,
5976 Symbol_table::PREDEFINED, 0, 0,
5977 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5978 elfcpp::STV_HIDDEN, 0, true, false);
5984 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
5985 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
5987 if (!parameters->options().relocatable())
5989 this->define_save_restore_funcs(layout, symtab);
5991 // Annoyingly, we need to make these sections now whether or
5992 // not we need them. If we delay until do_relax then we
5993 // need to mess with the relaxation machinery checkpointing.
5994 this->got_section(symtab, layout);
5995 this->make_brlt_section(layout);
5997 if (parameters->options().toc_sort())
5999 Output_section* os = this->got_->output_section();
6000 if (os != NULL && os->input_sections().size() > 1)
6001 std::stable_sort(os->input_sections().begin(),
6002 os->input_sections().end(),
6003 Sort_toc_sections<big_endian>());
6008 // Fill in some more dynamic tags.
6009 Output_data_dynamic* odyn = layout->dynamic_data();
6012 const Reloc_section* rel_plt = (this->plt_ == NULL
6014 : this->plt_->rel_plt());
6015 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6016 this->rela_dyn_, true, size == 32);
6020 if (this->got_ != NULL)
6022 this->got_->finalize_data_size();
6023 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6024 this->got_, this->got_->g_o_t());
6029 if (this->glink_ != NULL)
6031 this->glink_->finalize_data_size();
6032 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6034 (this->glink_->pltresolve_size
6040 // Emit any relocs we saved in an attempt to avoid generating COPY
6042 if (this->copy_relocs_.any_saved_relocs())
6043 this->copy_relocs_.emit(this->rela_dyn_section(layout));
6046 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6050 ok_lo_toc_insn(uint32_t insn)
6052 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6053 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6054 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6055 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6056 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6057 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6058 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6059 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6060 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6061 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6062 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6063 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6064 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6065 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6066 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6068 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6069 && ((insn & 3) == 0 || (insn & 3) == 3))
6070 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6073 // Return the value to use for a branch relocation.
6075 template<int size, bool big_endian>
6076 typename Target_powerpc<size, big_endian>::Address
6077 Target_powerpc<size, big_endian>::symval_for_branch(
6078 const Symbol_table* symtab,
6080 const Sized_symbol<size>* gsym,
6081 Powerpc_relobj<size, big_endian>* object,
6082 unsigned int *dest_shndx)
6088 // If the symbol is defined in an opd section, ie. is a function
6089 // descriptor, use the function descriptor code entry address
6090 Powerpc_relobj<size, big_endian>* symobj = object;
6092 && gsym->source() != Symbol::FROM_OBJECT)
6095 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6096 unsigned int shndx = symobj->opd_shndx();
6099 Address opd_addr = symobj->get_output_section_offset(shndx);
6100 gold_assert(opd_addr != invalid_address);
6101 opd_addr += symobj->output_section(shndx)->address();
6102 if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
6105 *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
6106 if (symtab->is_section_folded(symobj, *dest_shndx))
6109 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6110 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6111 *dest_shndx = folded.second;
6113 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6114 gold_assert(sec_addr != invalid_address);
6115 sec_addr += symobj->output_section(*dest_shndx)->address();
6116 value = sec_addr + sec_off;
6121 // Perform a relocation.
6123 template<int size, bool big_endian>
6125 Target_powerpc<size, big_endian>::Relocate::relocate(
6126 const Relocate_info<size, big_endian>* relinfo,
6127 Target_powerpc* target,
6130 const elfcpp::Rela<size, big_endian>& rela,
6131 unsigned int r_type,
6132 const Sized_symbol<size>* gsym,
6133 const Symbol_value<size>* psymval,
6134 unsigned char* view,
6136 section_size_type view_size)
6138 switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6140 case Track_tls::NOT_EXPECTED:
6141 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6142 _("__tls_get_addr call lacks marker reloc"));
6144 case Track_tls::EXPECTED:
6145 // We have already complained.
6147 case Track_tls::SKIP:
6149 case Track_tls::NORMAL:
6153 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6154 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6155 Powerpc_relobj<size, big_endian>* const object
6156 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6158 bool has_plt_value = false;
6159 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6161 ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
6162 : object->local_has_plt_offset(r_sym))
6164 Stub_table<size, big_endian>* stub_table
6165 = object->stub_table(relinfo->data_shndx);
6166 if (stub_table == NULL)
6168 // This is a ref from a data section to an ifunc symbol.
6169 if (target->stub_tables().size() != 0)
6170 stub_table = target->stub_tables()[0];
6172 gold_assert(stub_table != NULL);
6175 off = stub_table->find_plt_call_entry(object, gsym, r_type,
6176 rela.get_r_addend());
6178 off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6179 rela.get_r_addend());
6180 gold_assert(off != invalid_address);
6181 value = stub_table->stub_address() + off;
6182 has_plt_value = true;
6185 if (r_type == elfcpp::R_POWERPC_GOT16
6186 || r_type == elfcpp::R_POWERPC_GOT16_LO
6187 || r_type == elfcpp::R_POWERPC_GOT16_HI
6188 || r_type == elfcpp::R_POWERPC_GOT16_HA
6189 || r_type == elfcpp::R_PPC64_GOT16_DS
6190 || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6194 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6195 value = gsym->got_offset(GOT_TYPE_STANDARD);
6199 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6200 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6201 value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6203 value -= target->got_section()->got_base_offset(object);
6205 else if (r_type == elfcpp::R_PPC64_TOC)
6207 value = (target->got_section()->output_section()->address()
6208 + object->toc_base_offset());
6210 else if (gsym != NULL
6211 && (r_type == elfcpp::R_POWERPC_REL24
6212 || r_type == elfcpp::R_PPC_PLTREL24)
6217 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6218 Valtype* wv = reinterpret_cast<Valtype*>(view);
6219 bool can_plt_call = false;
6220 if (rela.get_r_offset() + 8 <= view_size)
6222 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6223 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6226 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6228 elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
6229 can_plt_call = true;
6234 // If we don't have a branch and link followed by a nop,
6235 // we can't go via the plt because there is no place to
6236 // put a toc restoring instruction.
6237 // Unless we know we won't be returning.
6238 if (strcmp(gsym->name(), "__libc_start_main") == 0)
6239 can_plt_call = true;
6243 // This is not an error in one special case: A self
6244 // call. It isn't possible to cheaply verify we have
6245 // such a call so just check for a call to the same
6248 Address code = value;
6249 if (gsym->source() == Symbol::FROM_OBJECT
6250 && gsym->object() == object)
6252 Address addend = rela.get_r_addend();
6253 unsigned int dest_shndx;
6254 Address opdent = psymval->value(object, addend);
6255 code = target->symval_for_branch(relinfo->symtab, opdent,
6256 gsym, object, &dest_shndx);
6258 if (dest_shndx == 0)
6259 dest_shndx = gsym->shndx(&is_ordinary);
6260 ok = dest_shndx == relinfo->data_shndx;
6264 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6265 _("call lacks nop, can't restore toc; "
6266 "recompile with -fPIC"));
6272 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6273 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6274 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6275 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6277 // First instruction of a global dynamic sequence, arg setup insn.
6278 const bool final = gsym == NULL || gsym->final_value_is_known();
6279 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6280 enum Got_type got_type = GOT_TYPE_STANDARD;
6281 if (tls_type == tls::TLSOPT_NONE)
6282 got_type = GOT_TYPE_TLSGD;
6283 else if (tls_type == tls::TLSOPT_TO_IE)
6284 got_type = GOT_TYPE_TPREL;
6285 if (got_type != GOT_TYPE_STANDARD)
6289 gold_assert(gsym->has_got_offset(got_type));
6290 value = gsym->got_offset(got_type);
6294 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6295 gold_assert(object->local_has_got_offset(r_sym, got_type));
6296 value = object->local_got_offset(r_sym, got_type);
6298 value -= target->got_section()->got_base_offset(object);
6300 if (tls_type == tls::TLSOPT_TO_IE)
6302 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6303 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6305 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6306 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6307 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6309 insn |= 32 << 26; // lwz
6311 insn |= 58 << 26; // ld
6312 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6314 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6315 - elfcpp::R_POWERPC_GOT_TLSGD16);
6317 else if (tls_type == tls::TLSOPT_TO_LE)
6319 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6320 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6322 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6323 Insn insn = addis_3_13;
6326 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6327 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6328 value = psymval->value(object, rela.get_r_addend());
6332 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6334 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6335 r_type = elfcpp::R_POWERPC_NONE;
6339 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6340 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6341 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6342 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6344 // First instruction of a local dynamic sequence, arg setup insn.
6345 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6346 if (tls_type == tls::TLSOPT_NONE)
6348 value = target->tlsld_got_offset();
6349 value -= target->got_section()->got_base_offset(object);
6353 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6354 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6355 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6357 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6358 Insn insn = addis_3_13;
6361 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6362 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6367 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6369 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6370 r_type = elfcpp::R_POWERPC_NONE;
6374 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6375 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6376 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6377 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6379 // Accesses relative to a local dynamic sequence address,
6380 // no optimisation here.
6383 gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6384 value = gsym->got_offset(GOT_TYPE_DTPREL);
6388 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6389 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6390 value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6392 value -= target->got_section()->got_base_offset(object);
6394 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6395 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6396 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6397 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6399 // First instruction of initial exec sequence.
6400 const bool final = gsym == NULL || gsym->final_value_is_known();
6401 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6402 if (tls_type == tls::TLSOPT_NONE)
6406 gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
6407 value = gsym->got_offset(GOT_TYPE_TPREL);
6411 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6412 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
6413 value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
6415 value -= target->got_section()->got_base_offset(object);
6419 gold_assert(tls_type == tls::TLSOPT_TO_LE);
6420 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6421 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6423 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6424 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6425 insn &= (1 << 26) - (1 << 21); // extract rt from ld
6430 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6431 r_type = elfcpp::R_POWERPC_TPREL16_HA;
6432 value = psymval->value(object, rela.get_r_addend());
6436 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6438 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6439 r_type = elfcpp::R_POWERPC_NONE;
6443 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6444 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6446 // Second instruction of a global dynamic sequence,
6447 // the __tls_get_addr call
6448 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6449 const bool final = gsym == NULL || gsym->final_value_is_known();
6450 const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6451 if (tls_type != tls::TLSOPT_NONE)
6453 if (tls_type == tls::TLSOPT_TO_IE)
6455 Insn* iview = reinterpret_cast<Insn*>(view);
6456 Insn insn = add_3_3_13;
6459 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6460 r_type = elfcpp::R_POWERPC_NONE;
6464 Insn* iview = reinterpret_cast<Insn*>(view);
6465 Insn insn = addi_3_3;
6466 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6467 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6468 view += 2 * big_endian;
6469 value = psymval->value(object, rela.get_r_addend());
6471 this->skip_next_tls_get_addr_call();
6474 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6475 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6477 // Second instruction of a local dynamic sequence,
6478 // the __tls_get_addr call
6479 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6480 const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6481 if (tls_type == tls::TLSOPT_TO_LE)
6483 Insn* iview = reinterpret_cast<Insn*>(view);
6484 Insn insn = addi_3_3;
6485 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6486 this->skip_next_tls_get_addr_call();
6487 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6488 view += 2 * big_endian;
6492 else if (r_type == elfcpp::R_POWERPC_TLS)
6494 // Second instruction of an initial exec sequence
6495 const bool final = gsym == NULL || gsym->final_value_is_known();
6496 const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6497 if (tls_type == tls::TLSOPT_TO_LE)
6499 Insn* iview = reinterpret_cast<Insn*>(view);
6500 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6501 unsigned int reg = size == 32 ? 2 : 13;
6502 insn = at_tls_transform(insn, reg);
6503 gold_assert(insn != 0);
6504 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6505 r_type = elfcpp::R_POWERPC_TPREL16_LO;
6506 view += 2 * big_endian;
6507 value = psymval->value(object, rela.get_r_addend());
6510 else if (!has_plt_value)
6513 unsigned int dest_shndx;
6514 if (r_type != elfcpp::R_PPC_PLTREL24)
6515 addend = rela.get_r_addend();
6516 value = psymval->value(object, addend);
6517 if (size == 64 && is_branch_reloc(r_type))
6518 value = target->symval_for_branch(relinfo->symtab, value,
6519 gsym, object, &dest_shndx);
6520 unsigned int max_branch_offset = 0;
6521 if (r_type == elfcpp::R_POWERPC_REL24
6522 || r_type == elfcpp::R_PPC_PLTREL24
6523 || r_type == elfcpp::R_PPC_LOCAL24PC)
6524 max_branch_offset = 1 << 25;
6525 else if (r_type == elfcpp::R_POWERPC_REL14
6526 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
6527 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
6528 max_branch_offset = 1 << 15;
6529 if (max_branch_offset != 0
6530 && value - address + max_branch_offset >= 2 * max_branch_offset)
6532 Stub_table<size, big_endian>* stub_table
6533 = object->stub_table(relinfo->data_shndx);
6534 gold_assert(stub_table != NULL);
6535 Address off = stub_table->find_long_branch_entry(object, value);
6536 if (off != invalid_address)
6537 value = stub_table->stub_address() + stub_table->plt_size() + off;
6543 case elfcpp::R_PPC64_REL64:
6544 case elfcpp::R_POWERPC_REL32:
6545 case elfcpp::R_POWERPC_REL24:
6546 case elfcpp::R_PPC_PLTREL24:
6547 case elfcpp::R_PPC_LOCAL24PC:
6548 case elfcpp::R_POWERPC_REL16:
6549 case elfcpp::R_POWERPC_REL16_LO:
6550 case elfcpp::R_POWERPC_REL16_HI:
6551 case elfcpp::R_POWERPC_REL16_HA:
6552 case elfcpp::R_POWERPC_REL14:
6553 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6554 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6558 case elfcpp::R_PPC64_TOC16:
6559 case elfcpp::R_PPC64_TOC16_LO:
6560 case elfcpp::R_PPC64_TOC16_HI:
6561 case elfcpp::R_PPC64_TOC16_HA:
6562 case elfcpp::R_PPC64_TOC16_DS:
6563 case elfcpp::R_PPC64_TOC16_LO_DS:
6564 // Subtract the TOC base address.
6565 value -= (target->got_section()->output_section()->address()
6566 + object->toc_base_offset());
6569 case elfcpp::R_POWERPC_SECTOFF:
6570 case elfcpp::R_POWERPC_SECTOFF_LO:
6571 case elfcpp::R_POWERPC_SECTOFF_HI:
6572 case elfcpp::R_POWERPC_SECTOFF_HA:
6573 case elfcpp::R_PPC64_SECTOFF_DS:
6574 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6576 value -= os->address();
6579 case elfcpp::R_PPC64_TPREL16_DS:
6580 case elfcpp::R_PPC64_TPREL16_LO_DS:
6582 // R_PPC_TLSGD and R_PPC_TLSLD
6584 case elfcpp::R_POWERPC_TPREL16:
6585 case elfcpp::R_POWERPC_TPREL16_LO:
6586 case elfcpp::R_POWERPC_TPREL16_HI:
6587 case elfcpp::R_POWERPC_TPREL16_HA:
6588 case elfcpp::R_POWERPC_TPREL:
6589 case elfcpp::R_PPC64_TPREL16_HIGHER:
6590 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6591 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6592 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6593 // tls symbol values are relative to tls_segment()->vaddr()
6597 case elfcpp::R_PPC64_DTPREL16_DS:
6598 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6599 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6600 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6601 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6602 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6604 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6605 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6607 case elfcpp::R_POWERPC_DTPREL16:
6608 case elfcpp::R_POWERPC_DTPREL16_LO:
6609 case elfcpp::R_POWERPC_DTPREL16_HI:
6610 case elfcpp::R_POWERPC_DTPREL16_HA:
6611 case elfcpp::R_POWERPC_DTPREL:
6612 // tls symbol values are relative to tls_segment()->vaddr()
6613 value -= dtp_offset;
6620 Insn branch_bit = 0;
6623 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6624 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6625 branch_bit = 1 << 21;
6626 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6627 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6629 Insn* iview = reinterpret_cast<Insn*>(view);
6630 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6633 if (this->is_isa_v2)
6635 // Set 'a' bit. This is 0b00010 in BO field for branch
6636 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6637 // for branch on CTR insns (BO == 1a00t or 1a01t).
6638 if ((insn & (0x14 << 21)) == (0x04 << 21))
6640 else if ((insn & (0x14 << 21)) == (0x10 << 21))
6647 // Invert 'y' bit if not the default.
6648 if (static_cast<Signed_address>(value) < 0)
6651 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6661 // Multi-instruction sequences that access the TOC can be
6662 // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6663 // to nop; addi rb,r2,x;
6669 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6670 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6671 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6672 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6673 case elfcpp::R_POWERPC_GOT16_HA:
6674 case elfcpp::R_PPC64_TOC16_HA:
6675 if (parameters->options().toc_optimize())
6677 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6678 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6679 if ((insn & ((0x3f << 26) | 0x1f << 16))
6680 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6681 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6682 _("toc optimization is not supported "
6683 "for %#08x instruction"), insn);
6684 else if (value + 0x8000 < 0x10000)
6686 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
6692 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6693 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6694 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6695 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6696 case elfcpp::R_POWERPC_GOT16_LO:
6697 case elfcpp::R_PPC64_GOT16_LO_DS:
6698 case elfcpp::R_PPC64_TOC16_LO:
6699 case elfcpp::R_PPC64_TOC16_LO_DS:
6700 if (parameters->options().toc_optimize())
6702 Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6703 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6704 if (!ok_lo_toc_insn(insn))
6705 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6706 _("toc optimization is not supported "
6707 "for %#08x instruction"), insn);
6708 else if (value + 0x8000 < 0x10000)
6710 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
6712 // Transform addic to addi when we change reg.
6713 insn &= ~((0x3f << 26) | (0x1f << 16));
6714 insn |= (14u << 26) | (2 << 16);
6718 insn &= ~(0x1f << 16);
6721 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6728 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
6731 case elfcpp::R_POWERPC_ADDR32:
6732 case elfcpp::R_POWERPC_UADDR32:
6734 overflow = Reloc::CHECK_BITFIELD;
6737 case elfcpp::R_POWERPC_REL32:
6739 overflow = Reloc::CHECK_SIGNED;
6742 case elfcpp::R_POWERPC_ADDR24:
6743 case elfcpp::R_POWERPC_ADDR16:
6744 case elfcpp::R_POWERPC_UADDR16:
6745 case elfcpp::R_PPC64_ADDR16_DS:
6746 case elfcpp::R_POWERPC_ADDR14:
6747 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6748 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6749 overflow = Reloc::CHECK_BITFIELD;
6752 case elfcpp::R_POWERPC_REL24:
6753 case elfcpp::R_PPC_PLTREL24:
6754 case elfcpp::R_PPC_LOCAL24PC:
6755 case elfcpp::R_POWERPC_REL16:
6756 case elfcpp::R_PPC64_TOC16:
6757 case elfcpp::R_POWERPC_GOT16:
6758 case elfcpp::R_POWERPC_SECTOFF:
6759 case elfcpp::R_POWERPC_TPREL16:
6760 case elfcpp::R_POWERPC_DTPREL16:
6761 case elfcpp::R_PPC64_TPREL16_DS:
6762 case elfcpp::R_PPC64_DTPREL16_DS:
6763 case elfcpp::R_PPC64_TOC16_DS:
6764 case elfcpp::R_PPC64_GOT16_DS:
6765 case elfcpp::R_PPC64_SECTOFF_DS:
6766 case elfcpp::R_POWERPC_REL14:
6767 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6768 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6769 case elfcpp::R_POWERPC_GOT_TLSGD16:
6770 case elfcpp::R_POWERPC_GOT_TLSLD16:
6771 case elfcpp::R_POWERPC_GOT_TPREL16:
6772 case elfcpp::R_POWERPC_GOT_DTPREL16:
6773 overflow = Reloc::CHECK_SIGNED;
6777 typename Powerpc_relocate_functions<size, big_endian>::Status status
6778 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
6781 case elfcpp::R_POWERPC_NONE:
6782 case elfcpp::R_POWERPC_TLS:
6783 case elfcpp::R_POWERPC_GNU_VTINHERIT:
6784 case elfcpp::R_POWERPC_GNU_VTENTRY:
6785 case elfcpp::R_PPC_EMB_MRKREF:
6788 case elfcpp::R_PPC64_ADDR64:
6789 case elfcpp::R_PPC64_REL64:
6790 case elfcpp::R_PPC64_TOC:
6791 Reloc::addr64(view, value);
6794 case elfcpp::R_POWERPC_TPREL:
6795 case elfcpp::R_POWERPC_DTPREL:
6797 Reloc::addr64(view, value);
6799 status = Reloc::addr32(view, value, overflow);
6802 case elfcpp::R_PPC64_UADDR64:
6803 Reloc::addr64_u(view, value);
6806 case elfcpp::R_POWERPC_ADDR32:
6807 status = Reloc::addr32(view, value, overflow);
6810 case elfcpp::R_POWERPC_REL32:
6811 case elfcpp::R_POWERPC_UADDR32:
6812 status = Reloc::addr32_u(view, value, overflow);
6815 case elfcpp::R_POWERPC_ADDR24:
6816 case elfcpp::R_POWERPC_REL24:
6817 case elfcpp::R_PPC_PLTREL24:
6818 case elfcpp::R_PPC_LOCAL24PC:
6819 status = Reloc::addr24(view, value, overflow);
6822 case elfcpp::R_POWERPC_GOT_DTPREL16:
6823 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6826 status = Reloc::addr16_ds(view, value, overflow);
6829 case elfcpp::R_POWERPC_ADDR16:
6830 case elfcpp::R_POWERPC_REL16:
6831 case elfcpp::R_PPC64_TOC16:
6832 case elfcpp::R_POWERPC_GOT16:
6833 case elfcpp::R_POWERPC_SECTOFF:
6834 case elfcpp::R_POWERPC_TPREL16:
6835 case elfcpp::R_POWERPC_DTPREL16:
6836 case elfcpp::R_POWERPC_GOT_TLSGD16:
6837 case elfcpp::R_POWERPC_GOT_TLSLD16:
6838 case elfcpp::R_POWERPC_GOT_TPREL16:
6839 case elfcpp::R_POWERPC_ADDR16_LO:
6840 case elfcpp::R_POWERPC_REL16_LO:
6841 case elfcpp::R_PPC64_TOC16_LO:
6842 case elfcpp::R_POWERPC_GOT16_LO:
6843 case elfcpp::R_POWERPC_SECTOFF_LO:
6844 case elfcpp::R_POWERPC_TPREL16_LO:
6845 case elfcpp::R_POWERPC_DTPREL16_LO:
6846 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6847 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6848 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6849 status = Reloc::addr16(view, value, overflow);
6852 case elfcpp::R_POWERPC_UADDR16:
6853 status = Reloc::addr16_u(view, value, overflow);
6856 case elfcpp::R_POWERPC_ADDR16_HI:
6857 case elfcpp::R_POWERPC_REL16_HI:
6858 case elfcpp::R_PPC64_TOC16_HI:
6859 case elfcpp::R_POWERPC_GOT16_HI:
6860 case elfcpp::R_POWERPC_SECTOFF_HI:
6861 case elfcpp::R_POWERPC_TPREL16_HI:
6862 case elfcpp::R_POWERPC_DTPREL16_HI:
6863 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6864 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6865 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6866 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6867 Reloc::addr16_hi(view, value);
6870 case elfcpp::R_POWERPC_ADDR16_HA:
6871 case elfcpp::R_POWERPC_REL16_HA:
6872 case elfcpp::R_PPC64_TOC16_HA:
6873 case elfcpp::R_POWERPC_GOT16_HA:
6874 case elfcpp::R_POWERPC_SECTOFF_HA:
6875 case elfcpp::R_POWERPC_TPREL16_HA:
6876 case elfcpp::R_POWERPC_DTPREL16_HA:
6877 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6878 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6879 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6880 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6881 Reloc::addr16_ha(view, value);
6884 case elfcpp::R_PPC64_DTPREL16_HIGHER:
6886 // R_PPC_EMB_NADDR16_LO
6888 case elfcpp::R_PPC64_ADDR16_HIGHER:
6889 case elfcpp::R_PPC64_TPREL16_HIGHER:
6890 Reloc::addr16_hi2(view, value);
6893 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6895 // R_PPC_EMB_NADDR16_HI
6897 case elfcpp::R_PPC64_ADDR16_HIGHERA:
6898 case elfcpp::R_PPC64_TPREL16_HIGHERA:
6899 Reloc::addr16_ha2(view, value);
6902 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6904 // R_PPC_EMB_NADDR16_HA
6906 case elfcpp::R_PPC64_ADDR16_HIGHEST:
6907 case elfcpp::R_PPC64_TPREL16_HIGHEST:
6908 Reloc::addr16_hi3(view, value);
6911 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6915 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6916 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6917 Reloc::addr16_ha3(view, value);
6920 case elfcpp::R_PPC64_DTPREL16_DS:
6921 case elfcpp::R_PPC64_DTPREL16_LO_DS:
6923 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6925 case elfcpp::R_PPC64_TPREL16_DS:
6926 case elfcpp::R_PPC64_TPREL16_LO_DS:
6928 // R_PPC_TLSGD, R_PPC_TLSLD
6930 case elfcpp::R_PPC64_ADDR16_DS:
6931 case elfcpp::R_PPC64_ADDR16_LO_DS:
6932 case elfcpp::R_PPC64_TOC16_DS:
6933 case elfcpp::R_PPC64_TOC16_LO_DS:
6934 case elfcpp::R_PPC64_GOT16_DS:
6935 case elfcpp::R_PPC64_GOT16_LO_DS:
6936 case elfcpp::R_PPC64_SECTOFF_DS:
6937 case elfcpp::R_PPC64_SECTOFF_LO_DS:
6938 status = Reloc::addr16_ds(view, value, overflow);
6941 case elfcpp::R_POWERPC_ADDR14:
6942 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6943 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6944 case elfcpp::R_POWERPC_REL14:
6945 case elfcpp::R_POWERPC_REL14_BRTAKEN:
6946 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6947 status = Reloc::addr14(view, value, overflow);
6950 case elfcpp::R_POWERPC_COPY:
6951 case elfcpp::R_POWERPC_GLOB_DAT:
6952 case elfcpp::R_POWERPC_JMP_SLOT:
6953 case elfcpp::R_POWERPC_RELATIVE:
6954 case elfcpp::R_POWERPC_DTPMOD:
6955 case elfcpp::R_PPC64_JMP_IREL:
6956 case elfcpp::R_POWERPC_IRELATIVE:
6957 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6958 _("unexpected reloc %u in object file"),
6962 case elfcpp::R_PPC_EMB_SDA21:
6967 // R_PPC64_TOCSAVE. For the time being this can be ignored.
6971 case elfcpp::R_PPC_EMB_SDA2I16:
6972 case elfcpp::R_PPC_EMB_SDA2REL:
6975 // R_PPC64_TLSGD, R_PPC64_TLSLD
6978 case elfcpp::R_POWERPC_PLT32:
6979 case elfcpp::R_POWERPC_PLTREL32:
6980 case elfcpp::R_POWERPC_PLT16_LO:
6981 case elfcpp::R_POWERPC_PLT16_HI:
6982 case elfcpp::R_POWERPC_PLT16_HA:
6983 case elfcpp::R_PPC_SDAREL16:
6984 case elfcpp::R_POWERPC_ADDR30:
6985 case elfcpp::R_PPC64_PLT64:
6986 case elfcpp::R_PPC64_PLTREL64:
6987 case elfcpp::R_PPC64_PLTGOT16:
6988 case elfcpp::R_PPC64_PLTGOT16_LO:
6989 case elfcpp::R_PPC64_PLTGOT16_HI:
6990 case elfcpp::R_PPC64_PLTGOT16_HA:
6991 case elfcpp::R_PPC64_PLT16_LO_DS:
6992 case elfcpp::R_PPC64_PLTGOT16_DS:
6993 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
6994 case elfcpp::R_PPC_EMB_RELSEC16:
6995 case elfcpp::R_PPC_EMB_RELST_LO:
6996 case elfcpp::R_PPC_EMB_RELST_HI:
6997 case elfcpp::R_PPC_EMB_RELST_HA:
6998 case elfcpp::R_PPC_EMB_BIT_FLD:
6999 case elfcpp::R_PPC_EMB_RELSDA:
7000 case elfcpp::R_PPC_TOC16:
7003 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7004 _("unsupported reloc %u"),
7008 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
7009 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7010 _("relocation overflow"));
7015 // Relocate section data.
7017 template<int size, bool big_endian>
7019 Target_powerpc<size, big_endian>::relocate_section(
7020 const Relocate_info<size, big_endian>* relinfo,
7021 unsigned int sh_type,
7022 const unsigned char* prelocs,
7024 Output_section* output_section,
7025 bool needs_special_offset_handling,
7026 unsigned char* view,
7028 section_size_type view_size,
7029 const Reloc_symbol_changes* reloc_symbol_changes)
7031 typedef Target_powerpc<size, big_endian> Powerpc;
7032 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7033 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7034 Powerpc_comdat_behavior;
7036 gold_assert(sh_type == elfcpp::SHT_RELA);
7038 gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7039 Powerpc_relocate, Powerpc_comdat_behavior>(
7045 needs_special_offset_handling,
7049 reloc_symbol_changes);
7052 class Powerpc_scan_relocatable_reloc
7055 // Return the strategy to use for a local symbol which is not a
7056 // section symbol, given the relocation type.
7057 inline Relocatable_relocs::Reloc_strategy
7058 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7060 if (r_type == 0 && r_sym == 0)
7061 return Relocatable_relocs::RELOC_DISCARD;
7062 return Relocatable_relocs::RELOC_COPY;
7065 // Return the strategy to use for a local symbol which is a section
7066 // symbol, given the relocation type.
7067 inline Relocatable_relocs::Reloc_strategy
7068 local_section_strategy(unsigned int, Relobj*)
7070 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7073 // Return the strategy to use for a global symbol, given the
7074 // relocation type, the object, and the symbol index.
7075 inline Relocatable_relocs::Reloc_strategy
7076 global_strategy(unsigned int r_type, Relobj*, unsigned int)
7078 if (r_type == elfcpp::R_PPC_PLTREL24)
7079 return Relocatable_relocs::RELOC_SPECIAL;
7080 return Relocatable_relocs::RELOC_COPY;
7084 // Scan the relocs during a relocatable link.
7086 template<int size, bool big_endian>
7088 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7089 Symbol_table* symtab,
7091 Sized_relobj_file<size, big_endian>* object,
7092 unsigned int data_shndx,
7093 unsigned int sh_type,
7094 const unsigned char* prelocs,
7096 Output_section* output_section,
7097 bool needs_special_offset_handling,
7098 size_t local_symbol_count,
7099 const unsigned char* plocal_symbols,
7100 Relocatable_relocs* rr)
7102 gold_assert(sh_type == elfcpp::SHT_RELA);
7104 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7105 Powerpc_scan_relocatable_reloc>(
7113 needs_special_offset_handling,
7119 // Emit relocations for a section.
7120 // This is a modified version of the function by the same name in
7121 // target-reloc.h. Using relocate_special_relocatable for
7122 // R_PPC_PLTREL24 would require duplication of the entire body of the
7123 // loop, so we may as well duplicate the whole thing.
7125 template<int size, bool big_endian>
7127 Target_powerpc<size, big_endian>::relocate_relocs(
7128 const Relocate_info<size, big_endian>* relinfo,
7129 unsigned int sh_type,
7130 const unsigned char* prelocs,
7132 Output_section* output_section,
7133 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7134 const Relocatable_relocs* rr,
7136 Address view_address,
7138 unsigned char* reloc_view,
7139 section_size_type reloc_view_size)
7141 gold_assert(sh_type == elfcpp::SHT_RELA);
7143 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7145 typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7147 const int reloc_size
7148 = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7150 Powerpc_relobj<size, big_endian>* const object
7151 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7152 const unsigned int local_count = object->local_symbol_count();
7153 unsigned int got2_shndx = object->got2_shndx();
7154 Address got2_addend = 0;
7155 if (got2_shndx != 0)
7157 got2_addend = object->get_output_section_offset(got2_shndx);
7158 gold_assert(got2_addend != invalid_address);
7161 unsigned char* pwrite = reloc_view;
7162 bool zap_next = false;
7163 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7165 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7166 if (strategy == Relocatable_relocs::RELOC_DISCARD)
7169 Reltype reloc(prelocs);
7170 Reltype_write reloc_write(pwrite);
7172 Address offset = reloc.get_r_offset();
7173 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7174 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7175 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7176 const unsigned int orig_r_sym = r_sym;
7177 typename elfcpp::Elf_types<size>::Elf_Swxword addend
7178 = reloc.get_r_addend();
7179 const Symbol* gsym = NULL;
7183 // We could arrange to discard these and other relocs for
7184 // tls optimised sequences in the strategy methods, but for
7185 // now do as BFD ld does.
7186 r_type = elfcpp::R_POWERPC_NONE;
7190 // Get the new symbol index.
7191 if (r_sym < local_count)
7195 case Relocatable_relocs::RELOC_COPY:
7196 case Relocatable_relocs::RELOC_SPECIAL:
7199 r_sym = object->symtab_index(r_sym);
7200 gold_assert(r_sym != -1U);
7204 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7206 // We are adjusting a section symbol. We need to find
7207 // the symbol table index of the section symbol for
7208 // the output section corresponding to input section
7209 // in which this symbol is defined.
7210 gold_assert(r_sym < local_count);
7212 unsigned int shndx =
7213 object->local_symbol_input_shndx(r_sym, &is_ordinary);
7214 gold_assert(is_ordinary);
7215 Output_section* os = object->output_section(shndx);
7216 gold_assert(os != NULL);
7217 gold_assert(os->needs_symtab_index());
7218 r_sym = os->symtab_index();
7228 gsym = object->global_symbol(r_sym);
7229 gold_assert(gsym != NULL);
7230 if (gsym->is_forwarder())
7231 gsym = relinfo->symtab->resolve_forwards(gsym);
7233 gold_assert(gsym->has_symtab_index());
7234 r_sym = gsym->symtab_index();
7237 // Get the new offset--the location in the output section where
7238 // this relocation should be applied.
7239 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7240 offset += offset_in_output_section;
7243 section_offset_type sot_offset =
7244 convert_types<section_offset_type, Address>(offset);
7245 section_offset_type new_sot_offset =
7246 output_section->output_offset(object, relinfo->data_shndx,
7248 gold_assert(new_sot_offset != -1);
7249 offset = new_sot_offset;
7252 // In an object file, r_offset is an offset within the section.
7253 // In an executable or dynamic object, generated by
7254 // --emit-relocs, r_offset is an absolute address.
7255 if (!parameters->options().relocatable())
7257 offset += view_address;
7258 if (static_cast<Address>(offset_in_output_section) != invalid_address)
7259 offset -= offset_in_output_section;
7262 // Handle the reloc addend based on the strategy.
7263 if (strategy == Relocatable_relocs::RELOC_COPY)
7265 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7267 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7268 addend = psymval->value(object, addend);
7270 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7272 if (addend >= 32768)
7273 addend += got2_addend;
7278 if (!parameters->options().relocatable())
7280 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7281 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7282 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7283 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7285 // First instruction of a global dynamic sequence,
7287 const bool final = gsym == NULL || gsym->final_value_is_known();
7288 switch (this->optimize_tls_gd(final))
7290 case tls::TLSOPT_TO_IE:
7291 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7292 - elfcpp::R_POWERPC_GOT_TLSGD16);
7294 case tls::TLSOPT_TO_LE:
7295 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7296 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7297 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7300 r_type = elfcpp::R_POWERPC_NONE;
7301 offset -= 2 * big_endian;
7308 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7309 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7310 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7311 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7313 // First instruction of a local dynamic sequence,
7315 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7317 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7318 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7320 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7321 const Output_section* os = relinfo->layout->tls_segment()
7323 gold_assert(os != NULL);
7324 gold_assert(os->needs_symtab_index());
7325 r_sym = os->symtab_index();
7326 addend = dtp_offset;
7330 r_type = elfcpp::R_POWERPC_NONE;
7331 offset -= 2 * big_endian;
7335 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7336 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7337 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7338 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7340 // First instruction of initial exec sequence.
7341 const bool final = gsym == NULL || gsym->final_value_is_known();
7342 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7344 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7345 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7346 r_type = elfcpp::R_POWERPC_TPREL16_HA;
7349 r_type = elfcpp::R_POWERPC_NONE;
7350 offset -= 2 * big_endian;
7354 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7355 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7357 // Second instruction of a global dynamic sequence,
7358 // the __tls_get_addr call
7359 const bool final = gsym == NULL || gsym->final_value_is_known();
7360 switch (this->optimize_tls_gd(final))
7362 case tls::TLSOPT_TO_IE:
7363 r_type = elfcpp::R_POWERPC_NONE;
7366 case tls::TLSOPT_TO_LE:
7367 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7368 offset += 2 * big_endian;
7375 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7376 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7378 // Second instruction of a local dynamic sequence,
7379 // the __tls_get_addr call
7380 if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7382 const Output_section* os = relinfo->layout->tls_segment()
7384 gold_assert(os != NULL);
7385 gold_assert(os->needs_symtab_index());
7386 r_sym = os->symtab_index();
7387 addend = dtp_offset;
7388 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7389 offset += 2 * big_endian;
7393 else if (r_type == elfcpp::R_POWERPC_TLS)
7395 // Second instruction of an initial exec sequence
7396 const bool final = gsym == NULL || gsym->final_value_is_known();
7397 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7399 r_type = elfcpp::R_POWERPC_TPREL16_LO;
7400 offset += 2 * big_endian;
7405 reloc_write.put_r_offset(offset);
7406 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
7407 reloc_write.put_r_addend(addend);
7409 pwrite += reloc_size;
7412 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
7413 == reloc_view_size);
7416 // Return the value to use for a dynamic symbol which requires special
7417 // treatment. This is how we support equality comparisons of function
7418 // pointers across shared library boundaries, as described in the
7419 // processor specific ABI supplement.
7421 template<int size, bool big_endian>
7423 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7427 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7428 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7429 p != this->stub_tables_.end();
7432 Address off = (*p)->find_plt_call_entry(gsym);
7433 if (off != invalid_address)
7434 return (*p)->stub_address() + off;
7440 // Return the PLT address to use for a local symbol.
7441 template<int size, bool big_endian>
7443 Target_powerpc<size, big_endian>::do_plt_address_for_local(
7444 const Relobj* object,
7445 unsigned int symndx) const
7449 const Sized_relobj<size, big_endian>* relobj
7450 = static_cast<const Sized_relobj<size, big_endian>*>(object);
7451 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7452 p != this->stub_tables_.end();
7455 Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
7457 if (off != invalid_address)
7458 return (*p)->stub_address() + off;
7464 // Return the PLT address to use for a global symbol.
7465 template<int size, bool big_endian>
7467 Target_powerpc<size, big_endian>::do_plt_address_for_global(
7468 const Symbol* gsym) const
7472 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7473 p != this->stub_tables_.end();
7476 Address off = (*p)->find_plt_call_entry(gsym);
7477 if (off != invalid_address)
7478 return (*p)->stub_address() + off;
7484 // Return the offset to use for the GOT_INDX'th got entry which is
7485 // for a local tls symbol specified by OBJECT, SYMNDX.
7486 template<int size, bool big_endian>
7488 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
7489 const Relobj* object,
7490 unsigned int symndx,
7491 unsigned int got_indx) const
7493 const Powerpc_relobj<size, big_endian>* ppc_object
7494 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
7495 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
7497 for (Got_type got_type = GOT_TYPE_TLSGD;
7498 got_type <= GOT_TYPE_TPREL;
7499 got_type = Got_type(got_type + 1))
7500 if (ppc_object->local_has_got_offset(symndx, got_type))
7502 unsigned int off = ppc_object->local_got_offset(symndx, got_type);
7503 if (got_type == GOT_TYPE_TLSGD)
7505 if (off == got_indx * (size / 8))
7507 if (got_type == GOT_TYPE_TPREL)
7517 // Return the offset to use for the GOT_INDX'th got entry which is
7518 // for global tls symbol GSYM.
7519 template<int size, bool big_endian>
7521 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
7523 unsigned int got_indx) const
7525 if (gsym->type() == elfcpp::STT_TLS)
7527 for (Got_type got_type = GOT_TYPE_TLSGD;
7528 got_type <= GOT_TYPE_TPREL;
7529 got_type = Got_type(got_type + 1))
7530 if (gsym->has_got_offset(got_type))
7532 unsigned int off = gsym->got_offset(got_type);
7533 if (got_type == GOT_TYPE_TLSGD)
7535 if (off == got_indx * (size / 8))
7537 if (got_type == GOT_TYPE_TPREL)
7547 // The selector for powerpc object files.
7549 template<int size, bool big_endian>
7550 class Target_selector_powerpc : public Target_selector
7553 Target_selector_powerpc()
7554 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
7557 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
7558 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
7560 ? (big_endian ? "elf64ppc" : "elf64lppc")
7561 : (big_endian ? "elf32ppc" : "elf32lppc")))
7565 do_instantiate_target()
7566 { return new Target_powerpc<size, big_endian>(); }
7569 Target_selector_powerpc<32, true> target_selector_ppc32;
7570 Target_selector_powerpc<32, false> target_selector_ppc32le;
7571 Target_selector_powerpc<64, true> target_selector_ppc64;
7572 Target_selector_powerpc<64, false> target_selector_ppc64le;
7574 // Instantiate these constants for -O0
7575 template<int size, bool big_endian>
7576 const int Output_data_glink<size, big_endian>::pltresolve_size;
7577 template<int size, bool big_endian>
7578 const typename Stub_table<size, big_endian>::Address
7579 Stub_table<size, big_endian>::invalid_address;
7580 template<int size, bool big_endian>
7581 const typename Target_powerpc<size, big_endian>::Address
7582 Target_powerpc<size, big_endian>::invalid_address;
7584 } // End anonymous namespace.