[gdb/testsuite] Add can_compile rust
[binutils-gdb.git] / gold / powerpc.cc
bloba847dadf9003839ab3875effc3cef829b2ab3aa9
1 // powerpc.cc -- powerpc target support for gold.
3 // Copyright (C) 2008-2023 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.
24 #include "gold.h"
26 #include <set>
27 #include <algorithm>
28 #include "elfcpp.h"
29 #include "dwarf.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "powerpc.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "copy-relocs.h"
38 #include "target.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
41 #include "tls.h"
42 #include "errors.h"
43 #include "gc.h"
44 #include "attributes.h"
46 namespace
49 using namespace gold;
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
60 template<int size, bool big_endian>
61 class Output_data_glink;
63 template<int size, bool big_endian>
64 class Stub_table;
66 template<int size, bool big_endian>
67 class Output_data_save_res;
69 template<int size, bool big_endian>
70 class Target_powerpc;
72 struct Stub_table_owner
74 Stub_table_owner()
75 : output_section(NULL), owner(NULL)
76 { }
78 Output_section* output_section;
79 const Output_section::Input_section* owner;
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
94 public:
95 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
96 typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97 typedef Unordered_map<Address, Section_refs> Access_from;
99 Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101 : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
102 uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103 has_small_toc_reloc_(false), opd_valid_(false),
104 e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105 access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106 attributes_section_data_(NULL)
108 this->set_abiversion(0);
111 ~Powerpc_relobj()
112 { delete this->attributes_section_data_; }
114 // Read the symbols then set up st_other vector.
115 void
116 do_read_symbols(Read_symbols_data*);
118 // Arrange to always relocate .toc first.
119 virtual void
120 do_relocate_sections(
121 const Symbol_table* symtab, const Layout* layout,
122 const unsigned char* pshdrs, Output_file* of,
123 typename Sized_relobj_file<size, big_endian>::Views* pviews);
125 // The .toc section index.
126 unsigned int
127 toc_shndx() const
129 return this->toc_;
132 // Mark .toc entry at OFF as not optimizable.
133 void
134 set_no_toc_opt(Address off)
136 if (this->no_toc_opt_.empty())
137 this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 / (size / 8));
139 off /= size / 8;
140 if (off < this->no_toc_opt_.size())
141 this->no_toc_opt_[off] = true;
144 // Mark the entire .toc as not optimizable.
145 void
146 set_no_toc_opt()
148 this->no_toc_opt_.resize(1);
149 this->no_toc_opt_[0] = true;
152 // Return true if code using the .toc entry at OFF should not be edited.
153 bool
154 no_toc_opt(Address off) const
156 if (this->no_toc_opt_.empty())
157 return false;
158 off /= size / 8;
159 if (off >= this->no_toc_opt_.size())
160 return true;
161 return this->no_toc_opt_[off];
164 // The .got2 section shndx.
165 unsigned int
166 got2_shndx() const
168 if (size == 32)
169 return this->special_;
170 else
171 return 0;
174 // The .opd section shndx.
175 unsigned int
176 opd_shndx() const
178 if (size == 32)
179 return 0;
180 else
181 return this->special_;
184 // Init OPD entry arrays.
185 void
186 init_opd(size_t opd_size)
188 size_t count = this->opd_ent_ndx(opd_size);
189 this->opd_ent_.resize(count);
192 // Return section and offset of function entry for .opd + R_OFF.
193 unsigned int
194 get_opd_ent(Address r_off, Address* value = NULL) const
196 size_t ndx = this->opd_ent_ndx(r_off);
197 gold_assert(ndx < this->opd_ent_.size());
198 gold_assert(this->opd_ent_[ndx].shndx != 0);
199 if (value != NULL)
200 *value = this->opd_ent_[ndx].off;
201 return this->opd_ent_[ndx].shndx;
204 // Set section and offset of function entry for .opd + R_OFF.
205 void
206 set_opd_ent(Address r_off, unsigned int shndx, Address value)
208 size_t ndx = this->opd_ent_ndx(r_off);
209 gold_assert(ndx < this->opd_ent_.size());
210 this->opd_ent_[ndx].shndx = shndx;
211 this->opd_ent_[ndx].off = value;
214 // Return discard flag for .opd + R_OFF.
215 bool
216 get_opd_discard(Address r_off) const
218 size_t ndx = this->opd_ent_ndx(r_off);
219 gold_assert(ndx < this->opd_ent_.size());
220 return this->opd_ent_[ndx].discard;
223 // Set discard flag for .opd + R_OFF.
224 void
225 set_opd_discard(Address r_off)
227 size_t ndx = this->opd_ent_ndx(r_off);
228 gold_assert(ndx < this->opd_ent_.size());
229 this->opd_ent_[ndx].discard = true;
232 bool
233 opd_valid() const
234 { return this->opd_valid_; }
236 void
237 set_opd_valid()
238 { this->opd_valid_ = true; }
240 // Examine .rela.opd to build info about function entry points.
241 void
242 scan_opd_relocs(size_t reloc_count,
243 const unsigned char* prelocs,
244 const unsigned char* plocal_syms);
246 // Returns true if a code sequence loading a TOC entry can be
247 // converted into code calculating a TOC pointer relative offset.
248 bool
249 make_toc_relative(Target_powerpc<size, big_endian>* target,
250 Address* value);
252 bool
253 make_got_relative(Target_powerpc<size, big_endian>* target,
254 const Symbol_value<size>* psymval,
255 Address addend,
256 Address* value);
258 // Perform the Sized_relobj_file method, then set up opd info from
259 // .opd relocs.
260 void
261 do_read_relocs(Read_relocs_data*);
263 bool
264 do_find_special_sections(Read_symbols_data* sd);
266 // Adjust this local symbol value. Return false if the symbol
267 // should be discarded from the output file.
268 bool
269 do_adjust_local_symbol(Symbol_value<size>* lv) const
271 if (size == 64 && this->opd_shndx() != 0)
273 bool is_ordinary;
274 if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 return true;
276 if (this->get_opd_discard(lv->input_value()))
277 return false;
279 return true;
282 Access_from*
283 access_from_map()
284 { return &this->access_from_map_; }
286 // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287 // section at DST_OFF.
288 void
289 add_reference(Relobj* src_obj,
290 unsigned int src_indx,
291 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
293 Section_id src_id(src_obj, src_indx);
294 this->access_from_map_[dst_off].insert(src_id);
297 // Add a reference to the code section specified by the .opd entry
298 // at DST_OFF
299 void
300 add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
302 size_t ndx = this->opd_ent_ndx(dst_off);
303 if (ndx >= this->opd_ent_.size())
304 this->opd_ent_.resize(ndx + 1);
305 this->opd_ent_[ndx].gc_mark = true;
308 void
309 process_gc_mark(Symbol_table* symtab)
311 for (size_t i = 0; i < this->opd_ent_.size(); i++)
312 if (this->opd_ent_[i].gc_mark)
314 unsigned int shndx = this->opd_ent_[i].shndx;
315 symtab->gc()->worklist().push_back(Section_id(this, shndx));
319 void
320 set_has_small_toc_reloc()
321 { has_small_toc_reloc_ = true; }
323 bool
324 has_small_toc_reloc() const
325 { return has_small_toc_reloc_; }
327 void
328 set_has_14bit_branch(unsigned int shndx)
330 if (shndx >= this->has14_.size())
331 this->has14_.resize(shndx + 1);
332 this->has14_[shndx] = true;
335 bool
336 has_14bit_branch(unsigned int shndx) const
337 { return shndx < this->has14_.size() && this->has14_[shndx]; }
339 void
340 set_stub_table(unsigned int shndx, unsigned int stub_index)
342 if (shndx >= this->stub_table_index_.size())
343 this->stub_table_index_.resize(shndx + 1, -1);
344 this->stub_table_index_[shndx] = stub_index;
347 Stub_table<size, big_endian>*
348 stub_table(unsigned int shndx)
350 if (shndx < this->stub_table_index_.size())
352 Target_powerpc<size, big_endian>* target
353 = static_cast<Target_powerpc<size, big_endian>*>(
354 parameters->sized_target<size, big_endian>());
355 unsigned int indx = this->stub_table_index_[shndx];
356 if (indx < target->stub_tables().size())
357 return target->stub_tables()[indx];
359 return NULL;
362 void
363 clear_stub_table()
365 this->stub_table_index_.clear();
368 uint32_t
369 uniq() const
370 { return this->uniq_; }
373 abiversion() const
374 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
376 // Set ABI version for input and output
377 void
378 set_abiversion(int ver);
380 unsigned int
381 st_other (unsigned int symndx) const
383 return this->st_other_[symndx];
386 unsigned int
387 ppc64_local_entry_offset(const Symbol* sym) const
388 { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
390 unsigned int
391 ppc64_local_entry_offset(unsigned int symndx) const
392 { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
394 bool
395 ppc64_needs_toc(const Symbol* sym) const
396 { return sym->nonvis() > 1 << 3; }
398 bool
399 ppc64_needs_toc(unsigned int symndx) const
400 { return this->st_other_[symndx] > 1 << 5; }
402 // The contents of the .gnu.attributes section if there is one.
403 const Attributes_section_data*
404 attributes_section_data() const
405 { return this->attributes_section_data_; }
407 private:
408 struct Opd_ent
410 unsigned int shndx;
411 bool discard : 1;
412 bool gc_mark : 1;
413 Address off;
416 // Return index into opd_ent_ array for .opd entry at OFF.
417 // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418 // apart when the language doesn't use the last 8-byte word, the
419 // environment pointer. Thus dividing the entry section offset by
420 // 16 will give an index into opd_ent_ that works for either layout
421 // of .opd. (It leaves some elements of the vector unused when .opd
422 // entries are spaced 24 bytes apart, but we don't know the spacing
423 // until relocations are processed, and in any case it is possible
424 // for an object to have some entries spaced 16 bytes apart and
425 // others 24 bytes apart.)
426 size_t
427 opd_ent_ndx(size_t off) const
428 { return off >> 4;}
430 // Per object unique identifier
431 uint32_t uniq_;
433 // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434 unsigned int special_;
436 // For 64-bit the .rela.toc and .toc section shdnx.
437 unsigned int relatoc_;
438 unsigned int toc_;
440 // For 64-bit, whether this object uses small model relocs to access
441 // the toc.
442 bool has_small_toc_reloc_;
444 // Set at the start of gc_process_relocs, when we know opd_ent_
445 // vector is valid. The flag could be made atomic and set in
446 // do_read_relocs with memory_order_release and then tested with
447 // memory_order_acquire, potentially resulting in fewer entries in
448 // access_from_map_.
449 bool opd_valid_;
451 // Header e_flags
452 elfcpp::Elf_Word e_flags_;
454 // For 64-bit, an array with one entry per 64-bit word in the .toc
455 // section, set if accesses using that word cannot be optimised.
456 std::vector<bool> no_toc_opt_;
458 // The first 8-byte word of an OPD entry gives the address of the
459 // entry point of the function. Relocatable object files have a
460 // relocation on this word. The following vector records the
461 // section and offset specified by these relocations.
462 std::vector<Opd_ent> opd_ent_;
464 // References made to this object's .opd section when running
465 // gc_process_relocs for another object, before the opd_ent_ vector
466 // is valid for this object.
467 Access_from access_from_map_;
469 // Whether input section has a 14-bit branch reloc.
470 std::vector<bool> has14_;
472 // The stub table to use for a given input section.
473 std::vector<unsigned int> stub_table_index_;
475 // ELF st_other field for local symbols.
476 std::vector<unsigned char> st_other_;
478 // Object attributes if there is a .gnu.attributes section.
479 Attributes_section_data* attributes_section_data_;
482 template<int size, bool big_endian>
483 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
485 public:
486 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
488 Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490 : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
491 opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492 attributes_section_data_(NULL)
494 this->set_abiversion(0);
497 ~Powerpc_dynobj()
498 { delete this->attributes_section_data_; }
500 // Call Sized_dynobj::do_read_symbols to read the symbols then
501 // read .opd from a dynamic object, filling in opd_ent_ vector,
502 void
503 do_read_symbols(Read_symbols_data*);
505 // The .opd section shndx.
506 unsigned int
507 opd_shndx() const
509 return this->opd_shndx_;
512 // The .opd section address.
513 Address
514 opd_address() const
516 return this->opd_address_;
519 // Init OPD entry arrays.
520 void
521 init_opd(size_t opd_size)
523 size_t count = this->opd_ent_ndx(opd_size);
524 this->opd_ent_.resize(count);
527 // Return section and offset of function entry for .opd + R_OFF.
528 unsigned int
529 get_opd_ent(Address r_off, Address* value = NULL) const
531 size_t ndx = this->opd_ent_ndx(r_off);
532 gold_assert(ndx < this->opd_ent_.size());
533 gold_assert(this->opd_ent_[ndx].shndx != 0);
534 if (value != NULL)
535 *value = this->opd_ent_[ndx].off;
536 return this->opd_ent_[ndx].shndx;
539 // Set section and offset of function entry for .opd + R_OFF.
540 void
541 set_opd_ent(Address r_off, unsigned int shndx, Address value)
543 size_t ndx = this->opd_ent_ndx(r_off);
544 gold_assert(ndx < this->opd_ent_.size());
545 this->opd_ent_[ndx].shndx = shndx;
546 this->opd_ent_[ndx].off = value;
550 abiversion() const
551 { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
553 // Set ABI version for input and output.
554 void
555 set_abiversion(int ver);
557 // The contents of the .gnu.attributes section if there is one.
558 const Attributes_section_data*
559 attributes_section_data() const
560 { return this->attributes_section_data_; }
562 private:
563 // Used to specify extent of executable sections.
564 struct Sec_info
566 Sec_info(Address start_, Address len_, unsigned int shndx_)
567 : start(start_), len(len_), shndx(shndx_)
570 bool
571 operator<(const Sec_info& that) const
572 { return this->start < that.start; }
574 Address start;
575 Address len;
576 unsigned int shndx;
579 struct Opd_ent
581 unsigned int shndx;
582 Address off;
585 // Return index into opd_ent_ array for .opd entry at OFF.
586 size_t
587 opd_ent_ndx(size_t off) const
588 { return off >> 4;}
590 // For 64-bit the .opd section shndx and address.
591 unsigned int opd_shndx_;
592 Address opd_address_;
594 // Header e_flags
595 elfcpp::Elf_Word e_flags_;
597 // The first 8-byte word of an OPD entry gives the address of the
598 // entry point of the function. Records the section and offset
599 // corresponding to the address. Note that in dynamic objects,
600 // offset is *not* relative to the section.
601 std::vector<Opd_ent> opd_ent_;
603 // Object attributes if there is a .gnu.attributes section.
604 Attributes_section_data* attributes_section_data_;
607 // Powerpc_copy_relocs class. Needed to peek at dynamic relocs the
608 // base class will emit.
610 template<int sh_type, int size, bool big_endian>
611 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
613 public:
614 Powerpc_copy_relocs()
615 : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
618 // Emit any saved relocations which turn out to be needed. This is
619 // called after all the relocs have been scanned.
620 void
621 emit(Output_data_reloc<sh_type, true, size, big_endian>*);
624 // The types of GOT entries needed for this platform.
625 // These values are exposed to the ABI in an incremental link, but
626 // powerpc does not support incremental linking as yet.
627 enum Got_type
629 GOT_TYPE_STANDARD = 0,
630 GOT_TYPE_TLSGD = 1, // double entry for @got@tlsgd
631 GOT_TYPE_DTPREL = 2, // entry for @got@dtprel
632 GOT_TYPE_TPREL = 3, // entry for @got@tprel
633 GOT_TYPE_SMALL = 4,
634 GOT_TYPE_SMALL_TLSGD = 5,
635 GOT_TYPE_SMALL_DTPREL = 6,
636 GOT_TYPE_SMALL_TPREL = 7
639 // gsym->needs_plt_entry purpose is to decide whether a non-branch
640 // reloc should reference a plt entry. It can't be used to decide
641 // whether branches need a plt entry. In fact the call to
642 // needs_plt_entry here is not needed; All cases where it might
643 // return true ought to be covered already. However, since this
644 // function is used to decide between plt_ and lplt_ sections in
645 // plt_off, make certain that every case where make_plt_entry puts
646 // entries in plt_ is covered here.
647 static bool
648 branch_needs_plt_entry(const Symbol* gsym)
650 return (((!gsym->is_defined()
651 || gsym->is_from_dynobj()
652 || gsym->is_preemptible())
653 && !gsym->final_value_is_known())
654 || gsym->needs_plt_entry());
657 template<int size, bool big_endian>
658 class Target_powerpc : public Sized_target<size, big_endian>
660 public:
661 typedef
662 Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
663 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
664 typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
665 typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
666 static const Address invalid_address = static_cast<Address>(0) - 1;
667 // Offset of tp and dtp pointers from start of TLS block.
668 static const Address tp_offset = 0x7000;
669 static const Address dtp_offset = 0x8000;
671 Target_powerpc()
672 : Sized_target<size, big_endian>(&powerpc_info),
673 got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
674 brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
675 tlsld_got_offset_(-1U),
676 stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
677 power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
678 plt_localentry0_init_(false), has_localentry0_(false),
679 has_tls_get_addr_opt_(false), no_tprel_opt_(false),
680 relax_failed_(false), relax_fail_count_(0),
681 stub_group_size_(0), savres_section_(0),
682 tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
683 attributes_section_data_(NULL),
684 last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
688 // Process the relocations to determine unreferenced sections for
689 // garbage collection.
690 void
691 gc_process_relocs(Symbol_table* symtab,
692 Layout* layout,
693 Sized_relobj_file<size, big_endian>* object,
694 unsigned int data_shndx,
695 unsigned int sh_type,
696 const unsigned char* prelocs,
697 size_t reloc_count,
698 Output_section* output_section,
699 bool needs_special_offset_handling,
700 size_t local_symbol_count,
701 const unsigned char* plocal_symbols);
703 // Scan the relocations to look for symbol adjustments.
704 void
705 scan_relocs(Symbol_table* symtab,
706 Layout* layout,
707 Sized_relobj_file<size, big_endian>* object,
708 unsigned int data_shndx,
709 unsigned int sh_type,
710 const unsigned char* prelocs,
711 size_t reloc_count,
712 Output_section* output_section,
713 bool needs_special_offset_handling,
714 size_t local_symbol_count,
715 const unsigned char* plocal_symbols);
717 // Map input .toc section to output .got section.
718 const char*
719 do_output_section_name(const Relobj*, const char* name, size_t* plen) const
721 if (size == 64 && strcmp(name, ".toc") == 0)
723 *plen = 4;
724 return ".got";
726 return NULL;
729 // Provide linker defined save/restore functions.
730 void
731 define_save_restore_funcs(Layout*, Symbol_table*);
733 // No stubs unless a final link.
734 bool
735 do_may_relax() const
736 { return !parameters->options().relocatable(); }
738 bool
739 do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
741 void
742 do_plt_fde_location(const Output_data*, unsigned char*,
743 uint64_t*, off_t*) const;
745 // Stash info about branches, for stub generation.
746 void
747 push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
748 unsigned int data_shndx, Address r_offset,
749 unsigned int r_type, unsigned int r_sym, Address addend)
751 Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
752 this->branch_info_.push_back(info);
753 if (r_type == elfcpp::R_POWERPC_REL14
754 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
755 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
756 ppc_object->set_has_14bit_branch(data_shndx);
759 // Return whether the last branch is a plt call, and if so, mark the
760 // branch as having an R_PPC64_TOCSAVE.
761 bool
762 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
763 unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
765 return (size == 64
766 && !this->branch_info_.empty()
767 && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
768 r_offset, this, symtab));
771 // Say the given location, that of a nop in a function prologue with
772 // an R_PPC64_TOCSAVE reloc, will be used to save r2.
773 // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
774 void
775 add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
776 unsigned int shndx, Address offset)
778 Symbol_location loc;
779 loc.object = ppc_object;
780 loc.shndx = shndx;
781 loc.offset = offset;
782 this->tocsave_loc_.insert(loc);
785 // Accessor
786 const Tocsave_loc*
787 tocsave_loc() const
789 return &this->tocsave_loc_;
792 void
793 do_define_standard_symbols(Symbol_table*, Layout*);
795 // Finalize the sections.
796 void
797 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
799 // Return the value to use for a dynamic which requires special
800 // treatment.
801 uint64_t
802 do_dynsym_value(const Symbol*) const;
804 // Return the PLT address to use for a local symbol.
805 uint64_t
806 do_plt_address_for_local(const Relobj*, unsigned int) const;
808 // Return the PLT address to use for a global symbol.
809 uint64_t
810 do_plt_address_for_global(const Symbol*) const;
812 // Return the offset to use for the GOT_INDX'th got entry which is
813 // for a local tls symbol specified by OBJECT, SYMNDX.
814 int64_t
815 do_tls_offset_for_local(const Relobj* object,
816 unsigned int symndx,
817 Output_data_got_base* got,
818 unsigned int got_indx,
819 uint64_t addend) const;
821 // Return the offset to use for the GOT_INDX'th got entry which is
822 // for global tls symbol GSYM.
823 int64_t
824 do_tls_offset_for_global(Symbol* gsym,
825 Output_data_got_base* got, unsigned int got_indx,
826 uint64_t addend) const;
828 void
829 do_function_location(Symbol_location*) const;
831 bool
832 do_can_check_for_function_pointers() const
833 { return true; }
835 // Adjust -fsplit-stack code which calls non-split-stack code.
836 void
837 do_calls_non_split(Relobj* object, unsigned int shndx,
838 section_offset_type fnoffset, section_size_type fnsize,
839 const unsigned char* prelocs, size_t reloc_count,
840 unsigned char* view, section_size_type view_size,
841 std::string* from, std::string* to) const;
843 // Relocate a section.
844 void
845 relocate_section(const Relocate_info<size, big_endian>*,
846 unsigned int sh_type,
847 const unsigned char* prelocs,
848 size_t reloc_count,
849 Output_section* output_section,
850 bool needs_special_offset_handling,
851 unsigned char* view,
852 Address view_address,
853 section_size_type view_size,
854 const Reloc_symbol_changes*);
856 // Scan the relocs during a relocatable link.
857 void
858 scan_relocatable_relocs(Symbol_table* symtab,
859 Layout* layout,
860 Sized_relobj_file<size, big_endian>* object,
861 unsigned int data_shndx,
862 unsigned int sh_type,
863 const unsigned char* prelocs,
864 size_t reloc_count,
865 Output_section* output_section,
866 bool needs_special_offset_handling,
867 size_t local_symbol_count,
868 const unsigned char* plocal_symbols,
869 Relocatable_relocs*);
871 // Scan the relocs for --emit-relocs.
872 void
873 emit_relocs_scan(Symbol_table* symtab,
874 Layout* layout,
875 Sized_relobj_file<size, big_endian>* object,
876 unsigned int data_shndx,
877 unsigned int sh_type,
878 const unsigned char* prelocs,
879 size_t reloc_count,
880 Output_section* output_section,
881 bool needs_special_offset_handling,
882 size_t local_symbol_count,
883 const unsigned char* plocal_syms,
884 Relocatable_relocs* rr);
886 // Emit relocations for a section.
887 void
888 relocate_relocs(const Relocate_info<size, big_endian>*,
889 unsigned int sh_type,
890 const unsigned char* prelocs,
891 size_t reloc_count,
892 Output_section* output_section,
893 typename elfcpp::Elf_types<size>::Elf_Off
894 offset_in_output_section,
895 unsigned char*,
896 Address view_address,
897 section_size_type,
898 unsigned char* reloc_view,
899 section_size_type reloc_view_size);
901 // Return whether SYM is defined by the ABI.
902 bool
903 do_is_defined_by_abi(const Symbol* sym) const
905 return strcmp(sym->name(), "__tls_get_addr") == 0;
908 // Return the size of the GOT section, for incremental linking
909 section_size_type
910 got_size() const
912 gold_assert(this->got_ != NULL);
913 return this->got_->data_size() + (this->biggot_
914 ? this->biggot_->data_size() : 0);
917 // Get the PLT section.
918 const Output_data_plt_powerpc<size, big_endian>*
919 plt_section() const
921 gold_assert(this->plt_ != NULL);
922 return this->plt_;
925 // Get the IPLT section.
926 const Output_data_plt_powerpc<size, big_endian>*
927 iplt_section() const
929 gold_assert(this->iplt_ != NULL);
930 return this->iplt_;
933 // Get the LPLT section.
934 const Output_data_plt_powerpc<size, big_endian>*
935 lplt_section() const
937 return this->lplt_;
940 // Return the plt offset and section for the given global sym.
941 Address
942 plt_off(const Symbol* gsym,
943 const Output_data_plt_powerpc<size, big_endian>** sec) const
945 if (gsym->type() == elfcpp::STT_GNU_IFUNC
946 && gsym->can_use_relative_reloc(false))
947 *sec = this->iplt_section();
948 else if (branch_needs_plt_entry(gsym))
949 *sec = this->plt_section();
950 else
951 *sec = this->lplt_section();
952 return gsym->plt_offset();
955 // Return the plt offset and section for the given local sym.
956 Address
957 plt_off(const Sized_relobj_file<size, big_endian>* relobj,
958 unsigned int local_sym_index,
959 const Output_data_plt_powerpc<size, big_endian>** sec) const
961 const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
962 if (lsym->is_ifunc_symbol())
963 *sec = this->iplt_section();
964 else
965 *sec = this->lplt_section();
966 return relobj->local_plt_offset(local_sym_index);
969 // Get the .glink section.
970 const Output_data_glink<size, big_endian>*
971 glink_section() const
973 gold_assert(this->glink_ != NULL);
974 return this->glink_;
977 Output_data_glink<size, big_endian>*
978 glink_section()
980 gold_assert(this->glink_ != NULL);
981 return this->glink_;
984 bool has_glink() const
985 { return this->glink_ != NULL; }
987 // Get the GOT section.
988 const Output_data_got_powerpc<size, big_endian>*
989 got_section(Got_type got_type) const
991 gold_assert(this->got_ != NULL);
992 if (size == 32 || (got_type & GOT_TYPE_SMALL))
993 return this->got_;
994 gold_assert(this->biggot_ != NULL);
995 return this->biggot_;
998 // Get the GOT section, creating it if necessary.
999 Output_data_got_powerpc<size, big_endian>*
1000 got_section(Symbol_table*, Layout*, Got_type);
1002 // The toc/got pointer reg will be set to this value.
1003 Address
1004 toc_pointer() const
1006 return this->got_->address() + this->got_->g_o_t();
1009 // Offset of base used to access the GOT/TOC relative to the GOT section.
1010 Address
1011 got_base_offset(Got_type got_type) const
1013 if (size == 32 || (got_type & GOT_TYPE_SMALL))
1014 return this->got_->g_o_t();
1015 return this->toc_pointer() - this->biggot_->address();
1018 Object*
1019 do_make_elf_object(const std::string&, Input_file*, off_t,
1020 const elfcpp::Ehdr<size, big_endian>&);
1022 // Return the number of entries in the GOT.
1023 unsigned int
1024 got_entry_count() const
1026 if (this->got_ == NULL)
1027 return 0;
1028 return this->got_size() / (size / 8);
1031 // Return the number of entries in the PLT.
1032 unsigned int
1033 plt_entry_count() const;
1035 // Return the offset of the first non-reserved PLT entry.
1036 unsigned int
1037 first_plt_entry_offset() const
1039 if (size == 32)
1040 return 0;
1041 if (this->abiversion() >= 2)
1042 return 16;
1043 return 24;
1046 // Return the size of each PLT entry.
1047 unsigned int
1048 plt_entry_size() const
1050 if (size == 32)
1051 return 4;
1052 if (this->abiversion() >= 2)
1053 return 8;
1054 return 24;
1057 Output_data_save_res<size, big_endian>*
1058 savres_section() const
1060 return this->savres_section_;
1063 // Add any special sections for this symbol to the gc work list.
1064 // For powerpc64, this adds the code section of a function
1065 // descriptor.
1066 void
1067 do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1069 // Handle target specific gc actions when adding a gc reference from
1070 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1071 // and DST_OFF. For powerpc64, this adds a referenc to the code
1072 // section of a function descriptor.
1073 void
1074 do_gc_add_reference(Symbol_table* symtab,
1075 Relobj* src_obj,
1076 unsigned int src_shndx,
1077 Relobj* dst_obj,
1078 unsigned int dst_shndx,
1079 Address dst_off) const;
1081 typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1082 const Stub_tables&
1083 stub_tables() const
1084 { return this->stub_tables_; }
1086 const Output_data_brlt_powerpc<size, big_endian>*
1087 brlt_section() const
1088 { return this->brlt_section_; }
1090 void
1091 add_branch_lookup_table(Address to)
1093 unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1094 this->branch_lookup_table_.insert(std::make_pair(to, off));
1097 Address
1098 find_branch_lookup_table(Address to)
1100 typename Branch_lookup_table::const_iterator p
1101 = this->branch_lookup_table_.find(to);
1102 return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1105 void
1106 write_branch_lookup_table(unsigned char *oview)
1108 for (typename Branch_lookup_table::const_iterator p
1109 = this->branch_lookup_table_.begin();
1110 p != this->branch_lookup_table_.end();
1111 ++p)
1113 elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1117 // Wrapper used after relax to define a local symbol in output data,
1118 // from the end if value < 0.
1119 void
1120 define_local(Symbol_table* symtab, const char* name,
1121 Output_data* od, Address value, unsigned int symsize)
1123 Symbol* sym
1124 = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1125 od, value, symsize, elfcpp::STT_NOTYPE,
1126 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1127 static_cast<Signed_address>(value) < 0,
1128 false);
1129 // We are creating this symbol late, so need to fix up things
1130 // done early in Layout::finalize.
1131 sym->set_dynsym_index(-1U);
1134 void
1135 set_power10_relocs()
1137 this->power10_relocs_ = true;
1140 bool
1141 power10_stubs() const
1143 return (this->power10_relocs_
1144 && (parameters->options().power10_stubs_enum()
1145 != General_options::POWER10_STUBS_NO));
1148 bool
1149 power10_stubs_auto() const
1151 return (parameters->options().power10_stubs_enum()
1152 == General_options::POWER10_STUBS_AUTO);
1155 bool
1156 plt_thread_safe() const
1157 { return this->plt_thread_safe_; }
1159 bool
1160 plt_localentry0() const
1161 { return this->plt_localentry0_; }
1163 bool
1164 has_localentry0() const
1165 { return this->has_localentry0_; }
1167 void
1168 set_has_localentry0()
1170 this->has_localentry0_ = true;
1173 bool
1174 is_elfv2_localentry0(const Symbol* gsym) const
1176 return (size == 64
1177 && this->abiversion() >= 2
1178 && this->plt_localentry0()
1179 && gsym->type() == elfcpp::STT_FUNC
1180 && gsym->is_defined()
1181 && gsym->nonvis() >> 3 == 0
1182 && !gsym->non_zero_localentry());
1185 bool
1186 is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1187 unsigned int r_sym) const
1189 const Powerpc_relobj<size, big_endian>* ppc_object
1190 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1192 if (size == 64
1193 && this->abiversion() >= 2
1194 && this->plt_localentry0()
1195 && ppc_object->st_other(r_sym) >> 5 == 0)
1197 const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1198 bool is_ordinary;
1199 if (!psymval->is_ifunc_symbol()
1200 && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1201 && is_ordinary)
1202 return true;
1204 return false;
1207 bool
1208 tprel_opt() const
1209 { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
1211 void
1212 set_no_tprel_opt()
1213 { this->no_tprel_opt_ = true; }
1215 // Remember any symbols seen with non-zero localentry, even those
1216 // not providing a definition
1217 bool
1218 resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1219 const char*)
1221 if (size == 64)
1223 unsigned char st_other = sym.get_st_other();
1224 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1225 to->set_non_zero_localentry();
1227 // We haven't resolved anything, continue normal processing.
1228 return false;
1232 abiversion() const
1233 { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1235 void
1236 set_abiversion(int ver)
1238 elfcpp::Elf_Word flags = this->processor_specific_flags();
1239 flags &= ~elfcpp::EF_PPC64_ABI;
1240 flags |= ver & elfcpp::EF_PPC64_ABI;
1241 this->set_processor_specific_flags(flags);
1244 Symbol*
1245 tls_get_addr_opt() const
1246 { return this->tls_get_addr_opt_; }
1248 Symbol*
1249 tls_get_addr() const
1250 { return this->tls_get_addr_; }
1252 // If optimizing __tls_get_addr calls, whether this is the
1253 // "__tls_get_addr" symbol.
1254 bool
1255 is_tls_get_addr_opt(const Symbol* gsym) const
1257 return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1258 || gsym == this->tls_get_addr_opt_);
1261 bool
1262 replace_tls_get_addr(const Symbol* gsym) const
1263 { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1265 void
1266 set_has_tls_get_addr_opt()
1267 { this->has_tls_get_addr_opt_ = true; }
1269 // Offset to toc save stack slot
1271 stk_toc() const
1272 { return this->abiversion() < 2 ? 40 : 24; }
1274 // Offset to linker save stack slot. ELFv2 doesn't have a linker word,
1275 // so use the CR save slot. Used only by __tls_get_addr call stub,
1276 // relying on __tls_get_addr not saving CR itself.
1278 stk_linker() const
1279 { return this->abiversion() < 2 ? 32 : 8; }
1281 // Merge object attributes from input object with those in the output.
1282 void
1283 merge_object_attributes(const Object*, const Attributes_section_data*);
1285 bool
1286 symval_for_branch(const Symbol_table* symtab,
1287 const Sized_symbol<size>* gsym,
1288 Powerpc_relobj<size, big_endian>* object,
1289 Address *value, unsigned int *dest_shndx);
1291 private:
1293 class Track_tls
1295 public:
1296 enum Tls_get_addr
1298 NOT_EXPECTED = 0,
1299 EXPECTED = 1,
1300 SKIP = 2,
1301 NORMAL = 3
1304 Track_tls()
1305 : tls_get_addr_state_(NOT_EXPECTED),
1306 relinfo_(NULL), relnum_(0), r_offset_(0)
1309 ~Track_tls()
1311 if (this->tls_get_addr_state_ != NOT_EXPECTED)
1312 this->missing();
1315 void
1316 missing(void)
1318 if (this->relinfo_ != NULL)
1319 gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1320 _("missing expected __tls_get_addr call"));
1323 void
1324 expect_tls_get_addr_call(
1325 const Relocate_info<size, big_endian>* relinfo,
1326 size_t relnum,
1327 Address r_offset)
1329 this->tls_get_addr_state_ = EXPECTED;
1330 this->relinfo_ = relinfo;
1331 this->relnum_ = relnum;
1332 this->r_offset_ = r_offset;
1335 void
1336 expect_tls_get_addr_call()
1337 { this->tls_get_addr_state_ = EXPECTED; }
1339 void
1340 skip_next_tls_get_addr_call()
1341 {this->tls_get_addr_state_ = SKIP; }
1343 Tls_get_addr
1344 maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1345 unsigned int r_type, const Symbol* gsym)
1347 bool is_tls_call
1348 = ((r_type == elfcpp::R_POWERPC_REL24
1349 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1350 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
1351 || r_type == elfcpp::R_PPC_PLTREL24
1352 || is_plt16_reloc<size>(r_type)
1353 || r_type == elfcpp::R_PPC64_PLT_PCREL34
1354 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1355 || r_type == elfcpp::R_POWERPC_PLTSEQ
1356 || r_type == elfcpp::R_POWERPC_PLTCALL
1357 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1358 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1359 && gsym != NULL
1360 && (gsym == target->tls_get_addr()
1361 || gsym == target->tls_get_addr_opt()));
1362 Tls_get_addr last_tls = this->tls_get_addr_state_;
1363 this->tls_get_addr_state_ = NOT_EXPECTED;
1364 if (is_tls_call && last_tls != EXPECTED)
1365 return last_tls;
1366 else if (!is_tls_call && last_tls != NOT_EXPECTED)
1368 this->missing();
1369 return EXPECTED;
1371 return NORMAL;
1374 private:
1375 // What we're up to regarding calls to __tls_get_addr.
1376 // On powerpc, the branch and link insn making a call to
1377 // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1378 // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1379 // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1380 // The marker relocation always comes first, and has the same
1381 // symbol as the reloc on the insn setting up the __tls_get_addr
1382 // argument. This ties the arg setup insn with the call insn,
1383 // allowing ld to safely optimize away the call. We check that
1384 // every call to __tls_get_addr has a marker relocation, and that
1385 // every marker relocation is on a call to __tls_get_addr.
1386 Tls_get_addr tls_get_addr_state_;
1387 // Info about the last reloc for error message.
1388 const Relocate_info<size, big_endian>* relinfo_;
1389 size_t relnum_;
1390 Address r_offset_;
1393 // The class which scans relocations.
1394 class Scan : protected Track_tls
1396 public:
1397 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1399 Scan()
1400 : Track_tls(), issued_non_pic_error_(false)
1403 static inline int
1404 get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1406 inline void
1407 local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1408 Sized_relobj_file<size, big_endian>* object,
1409 unsigned int data_shndx,
1410 Output_section* output_section,
1411 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1412 const elfcpp::Sym<size, big_endian>& lsym,
1413 bool is_discarded);
1415 inline void
1416 global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1417 Sized_relobj_file<size, big_endian>* object,
1418 unsigned int data_shndx,
1419 Output_section* output_section,
1420 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1421 Symbol* gsym);
1423 inline bool
1424 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1425 Target_powerpc* ,
1426 Sized_relobj_file<size, big_endian>* relobj,
1427 unsigned int ,
1428 Output_section* ,
1429 const elfcpp::Rela<size, big_endian>& ,
1430 unsigned int r_type,
1431 const elfcpp::Sym<size, big_endian>&)
1433 // PowerPC64 .opd is not folded, so any identical function text
1434 // may be folded and we'll still keep function addresses distinct.
1435 // That means no reloc is of concern here.
1436 if (size == 64)
1438 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1439 <Powerpc_relobj<size, big_endian>*>(relobj);
1440 if (ppcobj->abiversion() == 1)
1441 return false;
1443 // For 32-bit and ELFv2, conservatively assume anything but calls to
1444 // function code might be taking the address of the function.
1445 return !is_branch_reloc<size>(r_type);
1448 inline bool
1449 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1450 Target_powerpc* ,
1451 Sized_relobj_file<size, big_endian>* relobj,
1452 unsigned int ,
1453 Output_section* ,
1454 const elfcpp::Rela<size, big_endian>& ,
1455 unsigned int r_type,
1456 Symbol*)
1458 // As above.
1459 if (size == 64)
1461 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1462 <Powerpc_relobj<size, big_endian>*>(relobj);
1463 if (ppcobj->abiversion() == 1)
1464 return false;
1466 return !is_branch_reloc<size>(r_type);
1469 static bool
1470 reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1471 Sized_relobj_file<size, big_endian>* object,
1472 unsigned int r_type, bool report_err);
1474 private:
1475 static void
1476 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1477 unsigned int r_type);
1479 static void
1480 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1481 unsigned int r_type, Symbol*);
1483 static void
1484 generate_tls_call(Symbol_table* symtab, Layout* layout,
1485 Target_powerpc* target);
1487 void
1488 check_non_pic(Relobj*, unsigned int r_type);
1490 // Whether we have issued an error about a non-PIC compilation.
1491 bool issued_non_pic_error_;
1494 // The class which implements relocation.
1495 class Relocate : protected Track_tls
1497 public:
1498 // Use 'at' branch hints when true, 'y' when false.
1499 // FIXME maybe: set this with an option.
1500 static const bool is_isa_v2 = true;
1502 Relocate()
1503 : Track_tls()
1506 // Do a relocation. Return false if the caller should not issue
1507 // any warnings about this relocation.
1508 inline bool
1509 relocate(const Relocate_info<size, big_endian>*, unsigned int,
1510 Target_powerpc*, Output_section*, size_t, const unsigned char*,
1511 const Sized_symbol<size>*, const Symbol_value<size>*,
1512 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1513 section_size_type);
1516 class Relocate_comdat_behavior
1518 public:
1519 // Decide what the linker should do for relocations that refer to
1520 // discarded comdat sections.
1521 inline Comdat_behavior
1522 get(const char* name)
1524 gold::Default_comdat_behavior default_behavior;
1525 Comdat_behavior ret = default_behavior.get(name);
1526 if (ret == CB_ERROR)
1528 if (size == 32
1529 && (strcmp(name, ".fixup") == 0
1530 || strcmp(name, ".got2") == 0))
1531 ret = CB_IGNORE;
1532 if (size == 64
1533 && (strcmp(name, ".opd") == 0
1534 || strcmp(name, ".toc") == 0
1535 || strcmp(name, ".toc1") == 0))
1536 ret = CB_IGNORE;
1538 return ret;
1542 // Optimize the TLS relocation type based on what we know about the
1543 // symbol. IS_FINAL is true if the final address of this symbol is
1544 // known at link time.
1546 tls::Tls_optimization
1547 optimize_tls_gd(bool is_final)
1549 // If we are generating a shared library, then we can't do anything
1550 // in the linker.
1551 if (parameters->options().shared()
1552 || !parameters->options().tls_optimize())
1553 return tls::TLSOPT_NONE;
1555 if (!is_final)
1556 return tls::TLSOPT_TO_IE;
1557 return tls::TLSOPT_TO_LE;
1560 tls::Tls_optimization
1561 optimize_tls_ld()
1563 if (parameters->options().shared()
1564 || !parameters->options().tls_optimize())
1565 return tls::TLSOPT_NONE;
1567 return tls::TLSOPT_TO_LE;
1570 tls::Tls_optimization
1571 optimize_tls_ie(bool is_final)
1573 if (!is_final
1574 || parameters->options().shared()
1575 || !parameters->options().tls_optimize())
1576 return tls::TLSOPT_NONE;
1578 return tls::TLSOPT_TO_LE;
1581 // Create glink.
1582 void
1583 make_glink_section(Layout*);
1585 // Create the PLT section.
1586 void
1587 make_plt_section(Symbol_table*, Layout*);
1589 void
1590 make_iplt_section(Symbol_table*, Layout*);
1592 void
1593 make_lplt_section(Symbol_table*, Layout*);
1595 void
1596 make_brlt_section(Layout*);
1598 // Create a PLT entry for a global symbol.
1599 void
1600 make_plt_entry(Symbol_table*, Layout*, Symbol*);
1602 // Create a PLT entry for a local IFUNC symbol.
1603 void
1604 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1605 Sized_relobj_file<size, big_endian>*,
1606 unsigned int);
1608 // Create a PLT entry for a local non-IFUNC symbol.
1609 void
1610 make_local_plt_entry(Symbol_table*, Layout*,
1611 Sized_relobj_file<size, big_endian>*,
1612 unsigned int);
1614 void
1615 make_local_plt_entry(Symbol_table*, Layout*, Symbol*);
1617 // Create a GOT entry for local dynamic __tls_get_addr.
1618 unsigned int
1619 tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1620 Sized_relobj_file<size, big_endian>* object);
1622 unsigned int
1623 tlsld_got_offset() const
1625 return this->tlsld_got_offset_;
1628 // Get the dynamic reloc section, creating it if necessary.
1629 Reloc_section*
1630 rela_dyn_section(Layout*);
1632 // Similarly, but for ifunc symbols get the one for ifunc.
1633 Reloc_section*
1634 rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1636 // Copy a relocation against a global symbol.
1637 void
1638 copy_reloc(Symbol_table* symtab, Layout* layout,
1639 Sized_relobj_file<size, big_endian>* object,
1640 unsigned int shndx, Output_section* output_section,
1641 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1643 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1644 this->copy_relocs_.copy_reloc(symtab, layout,
1645 symtab->get_sized_symbol<size>(sym),
1646 object, shndx, output_section,
1647 r_type, reloc.get_r_offset(),
1648 reloc.get_r_addend(),
1649 this->rela_dyn_section(layout));
1652 // Look over all the input sections, deciding where to place stubs.
1653 void
1654 group_sections(Layout*, const Task*, bool);
1656 // Sort output sections by address.
1657 struct Sort_sections
1659 bool
1660 operator()(const Output_section* sec1, const Output_section* sec2)
1661 { return sec1->address() < sec2->address(); }
1664 class Branch_info
1666 public:
1667 Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1668 unsigned int data_shndx,
1669 Address r_offset,
1670 unsigned int r_type,
1671 unsigned int r_sym,
1672 Address addend)
1673 : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1674 r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1677 ~Branch_info()
1680 // Return whether this branch is going via a plt call stub, and if
1681 // so, mark it as having an R_PPC64_TOCSAVE.
1682 bool
1683 mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1684 unsigned int shndx, Address offset,
1685 Target_powerpc* target, Symbol_table* symtab);
1687 // If this branch needs a plt call stub, or a long branch stub, make one.
1688 bool
1689 make_stub(Stub_table<size, big_endian>*,
1690 Stub_table<size, big_endian>*,
1691 Symbol_table*) const;
1693 private:
1694 // The branch location..
1695 Powerpc_relobj<size, big_endian>* object_;
1696 unsigned int shndx_;
1697 Address offset_;
1698 // ..and the branch type and destination.
1699 unsigned int r_type_ : 31;
1700 unsigned int tocsave_ : 1;
1701 unsigned int r_sym_;
1702 Address addend_;
1705 // Information about this specific target which we pass to the
1706 // general Target structure.
1707 static Target::Target_info powerpc_info;
1709 // The small GOT section used by ppc32, and by ppc64 for entries that
1710 // must be addresseed +/-32k from the got pointer.
1711 Output_data_got_powerpc<size, big_endian>* got_;
1712 // Another GOT section used for entries that can be addressed +/- 2G
1713 // from the got pointer.
1714 Output_data_got_powerpc<size, big_endian>* biggot_;
1716 // The PLT section. This is a container for a table of addresses,
1717 // and their relocations. Each address in the PLT has a dynamic
1718 // relocation (R_*_JMP_SLOT) and each address will have a
1719 // corresponding entry in .glink for lazy resolution of the PLT.
1720 // ppc32 initialises the PLT to point at the .glink entry, while
1721 // ppc64 leaves this to ld.so. To make a call via the PLT, the
1722 // linker adds a stub that loads the PLT entry into ctr then
1723 // branches to ctr. There may be more than one stub for each PLT
1724 // entry. DT_JMPREL points at the first PLT dynamic relocation and
1725 // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1726 Output_data_plt_powerpc<size, big_endian>* plt_;
1727 // The IPLT section. Like plt_, this is a container for a table of
1728 // addresses and their relocations, specifically for STT_GNU_IFUNC
1729 // functions that resolve locally (STT_GNU_IFUNC functions that
1730 // don't resolve locally go in PLT). Unlike plt_, these have no
1731 // entry in .glink for lazy resolution, and the relocation section
1732 // does not have a 1-1 correspondence with IPLT addresses. In fact,
1733 // the relocation section may contain relocations against
1734 // STT_GNU_IFUNC symbols at locations outside of IPLT. The
1735 // relocation section will appear at the end of other dynamic
1736 // relocations, so that ld.so applies these relocations after other
1737 // dynamic relocations. In a static executable, the relocation
1738 // section is emitted and marked with __rela_iplt_start and
1739 // __rela_iplt_end symbols.
1740 Output_data_plt_powerpc<size, big_endian>* iplt_;
1741 // A PLT style section for local, non-ifunc symbols
1742 Output_data_plt_powerpc<size, big_endian>* lplt_;
1743 // Section holding long branch destinations.
1744 Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1745 // The .glink section.
1746 Output_data_glink<size, big_endian>* glink_;
1747 // The dynamic reloc section.
1748 Reloc_section* rela_dyn_;
1749 // Relocs saved to avoid a COPY reloc.
1750 Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1751 // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1752 unsigned int tlsld_got_offset_;
1754 Stub_tables stub_tables_;
1755 typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1756 Branch_lookup_table branch_lookup_table_;
1758 typedef std::vector<Branch_info> Branches;
1759 Branches branch_info_;
1760 Tocsave_loc tocsave_loc_;
1762 bool power10_relocs_;
1763 bool plt_thread_safe_;
1764 bool plt_localentry0_;
1765 bool plt_localentry0_init_;
1766 bool has_localentry0_;
1767 bool has_tls_get_addr_opt_;
1768 bool no_tprel_opt_;
1770 bool relax_failed_;
1771 int relax_fail_count_;
1772 int32_t stub_group_size_;
1774 Output_data_save_res<size, big_endian> *savres_section_;
1776 // The "__tls_get_addr" symbol, if present
1777 Symbol* tls_get_addr_;
1778 // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1779 Symbol* tls_get_addr_opt_;
1781 // Attributes in output.
1782 Attributes_section_data* attributes_section_data_;
1784 // Last input file to change various attribute tags
1785 const char* last_fp_;
1786 const char* last_ld_;
1787 const char* last_vec_;
1788 const char* last_struct_;
1791 template<>
1792 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1794 32, // size
1795 true, // is_big_endian
1796 elfcpp::EM_PPC, // machine_code
1797 false, // has_make_symbol
1798 false, // has_resolve
1799 false, // has_code_fill
1800 true, // is_default_stack_executable
1801 false, // can_icf_inline_merge_sections
1802 '\0', // wrap_char
1803 "/usr/lib/ld.so.1", // dynamic_linker
1804 0x10000000, // default_text_segment_address
1805 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1806 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1807 false, // isolate_execinstr
1808 0, // rosegment_gap
1809 elfcpp::SHN_UNDEF, // small_common_shndx
1810 elfcpp::SHN_UNDEF, // large_common_shndx
1811 0, // small_common_section_flags
1812 0, // large_common_section_flags
1813 NULL, // attributes_section
1814 NULL, // attributes_vendor
1815 "_start", // entry_symbol_name
1816 32, // hash_entry_size
1817 elfcpp::SHT_PROGBITS, // unwind_section_type
1820 template<>
1821 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1823 32, // size
1824 false, // is_big_endian
1825 elfcpp::EM_PPC, // machine_code
1826 false, // has_make_symbol
1827 false, // has_resolve
1828 false, // has_code_fill
1829 true, // is_default_stack_executable
1830 false, // can_icf_inline_merge_sections
1831 '\0', // wrap_char
1832 "/usr/lib/ld.so.1", // dynamic_linker
1833 0x10000000, // default_text_segment_address
1834 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1835 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1836 false, // isolate_execinstr
1837 0, // rosegment_gap
1838 elfcpp::SHN_UNDEF, // small_common_shndx
1839 elfcpp::SHN_UNDEF, // large_common_shndx
1840 0, // small_common_section_flags
1841 0, // large_common_section_flags
1842 NULL, // attributes_section
1843 NULL, // attributes_vendor
1844 "_start", // entry_symbol_name
1845 32, // hash_entry_size
1846 elfcpp::SHT_PROGBITS, // unwind_section_type
1849 template<>
1850 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1852 64, // size
1853 true, // is_big_endian
1854 elfcpp::EM_PPC64, // machine_code
1855 false, // has_make_symbol
1856 true, // has_resolve
1857 false, // has_code_fill
1858 false, // is_default_stack_executable
1859 false, // can_icf_inline_merge_sections
1860 '\0', // wrap_char
1861 "/usr/lib/ld.so.1", // dynamic_linker
1862 0x10000000, // default_text_segment_address
1863 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1864 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1865 false, // isolate_execinstr
1866 0, // rosegment_gap
1867 elfcpp::SHN_UNDEF, // small_common_shndx
1868 elfcpp::SHN_UNDEF, // large_common_shndx
1869 0, // small_common_section_flags
1870 0, // large_common_section_flags
1871 NULL, // attributes_section
1872 NULL, // attributes_vendor
1873 "_start", // entry_symbol_name
1874 32, // hash_entry_size
1875 elfcpp::SHT_PROGBITS, // unwind_section_type
1878 template<>
1879 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1881 64, // size
1882 false, // is_big_endian
1883 elfcpp::EM_PPC64, // machine_code
1884 false, // has_make_symbol
1885 true, // has_resolve
1886 false, // has_code_fill
1887 false, // is_default_stack_executable
1888 false, // can_icf_inline_merge_sections
1889 '\0', // wrap_char
1890 "/usr/lib/ld.so.1", // dynamic_linker
1891 0x10000000, // default_text_segment_address
1892 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
1893 4 * 1024, // common_pagesize (overridable by -z common-page-size)
1894 false, // isolate_execinstr
1895 0, // rosegment_gap
1896 elfcpp::SHN_UNDEF, // small_common_shndx
1897 elfcpp::SHN_UNDEF, // large_common_shndx
1898 0, // small_common_section_flags
1899 0, // large_common_section_flags
1900 NULL, // attributes_section
1901 NULL, // attributes_vendor
1902 "_start", // entry_symbol_name
1903 32, // hash_entry_size
1904 elfcpp::SHT_PROGBITS, // unwind_section_type
1907 template<int size>
1908 inline bool
1909 is_branch_reloc(unsigned int r_type)
1911 return (r_type == elfcpp::R_POWERPC_REL24
1912 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1913 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
1914 || r_type == elfcpp::R_PPC_PLTREL24
1915 || r_type == elfcpp::R_PPC_LOCAL24PC
1916 || r_type == elfcpp::R_POWERPC_REL14
1917 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1918 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1919 || r_type == elfcpp::R_POWERPC_ADDR24
1920 || r_type == elfcpp::R_POWERPC_ADDR14
1921 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1922 || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1925 // Reloc resolves to plt entry.
1926 template<int size>
1927 inline bool
1928 is_plt16_reloc(unsigned int r_type)
1930 return (r_type == elfcpp::R_POWERPC_PLT16_LO
1931 || r_type == elfcpp::R_POWERPC_PLT16_HI
1932 || r_type == elfcpp::R_POWERPC_PLT16_HA
1933 || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1936 // GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
1937 inline bool
1938 is_got_reloc(unsigned int r_type)
1940 return (r_type == elfcpp::R_POWERPC_GOT16
1941 || r_type == elfcpp::R_POWERPC_GOT16_LO
1942 || r_type == elfcpp::R_POWERPC_GOT16_HI
1943 || r_type == elfcpp::R_POWERPC_GOT16_HA
1944 || r_type == elfcpp::R_PPC64_GOT16_DS
1945 || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1946 || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1949 // If INSN is an opcode that may be used with an @tls operand, return
1950 // the transformed insn for TLS optimisation, otherwise return 0. If
1951 // REG is non-zero only match an insn with RB or RA equal to REG.
1952 uint32_t
1953 at_tls_transform(uint32_t insn, unsigned int reg)
1955 if ((insn & (0x3f << 26)) != 31 << 26)
1956 return 0;
1958 unsigned int rtra;
1959 if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1960 rtra = insn & ((1 << 26) - (1 << 16));
1961 else if (((insn >> 16) & 0x1f) == reg)
1962 rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1963 else
1964 return 0;
1966 if ((insn & (0x3ff << 1)) == 266 << 1)
1967 // add -> addi
1968 insn = 14 << 26;
1969 else if ((insn & (0x1f << 1)) == 23 << 1
1970 && ((insn & (0x1f << 6)) < 14 << 6
1971 || ((insn & (0x1f << 6)) >= 16 << 6
1972 && (insn & (0x1f << 6)) < 24 << 6)))
1973 // load and store indexed -> dform
1974 insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1975 else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1976 // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1977 insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1978 else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1979 // lwax -> lwa
1980 insn = (58 << 26) | 2;
1981 else
1982 return 0;
1983 insn |= rtra;
1984 return insn;
1988 template<int size, bool big_endian>
1989 class Powerpc_relocate_functions
1991 public:
1992 enum Overflow_check
1994 CHECK_NONE,
1995 CHECK_SIGNED,
1996 CHECK_UNSIGNED,
1997 CHECK_BITFIELD,
1998 CHECK_LOW_INSN,
1999 CHECK_HIGH_INSN
2002 enum Status
2004 STATUS_OK,
2005 STATUS_OVERFLOW
2008 private:
2009 typedef Powerpc_relocate_functions<size, big_endian> This;
2010 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2011 typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
2013 template<int valsize>
2014 static inline bool
2015 has_overflow_signed(Address value)
2017 // limit = 1 << (valsize - 1) without shift count exceeding size of type
2018 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2019 limit <<= ((valsize - 1) >> 1);
2020 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2021 return value + limit > (limit << 1) - 1;
2024 template<int valsize>
2025 static inline bool
2026 has_overflow_unsigned(Address value)
2028 Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2029 limit <<= ((valsize - 1) >> 1);
2030 limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2031 return value > (limit << 1) - 1;
2034 template<int valsize>
2035 static inline bool
2036 has_overflow_bitfield(Address value)
2038 return (has_overflow_unsigned<valsize>(value)
2039 && has_overflow_signed<valsize>(value));
2042 template<int valsize>
2043 static inline Status
2044 overflowed(Address value, Overflow_check overflow)
2046 if (overflow == CHECK_SIGNED)
2048 if (has_overflow_signed<valsize>(value))
2049 return STATUS_OVERFLOW;
2051 else if (overflow == CHECK_UNSIGNED)
2053 if (has_overflow_unsigned<valsize>(value))
2054 return STATUS_OVERFLOW;
2056 else if (overflow == CHECK_BITFIELD)
2058 if (has_overflow_bitfield<valsize>(value))
2059 return STATUS_OVERFLOW;
2061 return STATUS_OK;
2064 // Do a simple RELA relocation
2065 template<int fieldsize, int valsize>
2066 static inline Status
2067 rela(unsigned char* view, Address value, Overflow_check overflow)
2069 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2070 Valtype* wv = reinterpret_cast<Valtype*>(view);
2071 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
2072 return overflowed<valsize>(value, overflow);
2075 template<int fieldsize, int valsize>
2076 static inline Status
2077 rela(unsigned char* view,
2078 unsigned int right_shift,
2079 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2080 Address value,
2081 Overflow_check overflow)
2083 typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2084 Valtype* wv = reinterpret_cast<Valtype*>(view);
2085 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
2086 if (overflow == CHECK_SIGNED)
2087 value = static_cast<SignedAddress>(value) >> right_shift;
2088 else
2089 value = value >> right_shift;
2090 Valtype reloc = value;
2091 val &= ~dst_mask;
2092 reloc &= dst_mask;
2093 elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2094 return overflowed<valsize>(value, overflow);
2097 // Do a simple RELA relocation, unaligned.
2098 template<int fieldsize, int valsize>
2099 static inline Status
2100 rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2102 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2103 return overflowed<valsize>(value, overflow);
2106 template<int fieldsize, int valsize>
2107 static inline Status
2108 rela_ua(unsigned char* view,
2109 unsigned int right_shift,
2110 typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2111 Address value,
2112 Overflow_check overflow)
2114 typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2115 Valtype;
2116 Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2117 if (overflow == CHECK_SIGNED)
2118 value = static_cast<SignedAddress>(value) >> right_shift;
2119 else
2120 value = value >> right_shift;
2121 Valtype reloc = value;
2122 val &= ~dst_mask;
2123 reloc &= dst_mask;
2124 elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2125 return overflowed<valsize>(value, overflow);
2128 public:
2129 // R_PPC64_ADDR64: (Symbol + Addend)
2130 static inline void
2131 addr64(unsigned char* view, Address value)
2132 { This::template rela<64,64>(view, value, CHECK_NONE); }
2134 // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2135 static inline void
2136 addr64_u(unsigned char* view, Address value)
2137 { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2139 // R_POWERPC_ADDR32: (Symbol + Addend)
2140 static inline Status
2141 addr32(unsigned char* view, Address value, Overflow_check overflow)
2142 { return This::template rela<32,32>(view, value, overflow); }
2144 // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2145 static inline Status
2146 addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2147 { return This::template rela_ua<32,32>(view, value, overflow); }
2149 // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2150 static inline Status
2151 addr24(unsigned char* view, Address value, Overflow_check overflow)
2153 Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2154 value, overflow);
2155 if (overflow != CHECK_NONE && (value & 3) != 0)
2156 stat = STATUS_OVERFLOW;
2157 return stat;
2160 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2161 static inline Status
2162 addr16(unsigned char* view, Address value, Overflow_check overflow)
2163 { return This::template rela<16,16>(view, value, overflow); }
2165 // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2166 static inline Status
2167 addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2168 { return This::template rela_ua<16,16>(view, value, overflow); }
2170 // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2171 static inline Status
2172 addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2174 Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2175 if ((value & 3) != 0)
2176 stat = STATUS_OVERFLOW;
2177 return stat;
2180 // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2181 static inline Status
2182 addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2184 Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2185 if ((value & 15) != 0)
2186 stat = STATUS_OVERFLOW;
2187 return stat;
2190 // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2191 static inline void
2192 addr16_hi(unsigned char* view, Address value)
2193 { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2195 // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2196 static inline void
2197 addr16_ha(unsigned char* view, Address value)
2198 { This::addr16_hi(view, value + 0x8000); }
2200 // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2201 static inline void
2202 addr16_hi2(unsigned char* view, Address value)
2203 { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2205 // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2206 static inline void
2207 addr16_ha2(unsigned char* view, Address value)
2208 { This::addr16_hi2(view, value + 0x8000); }
2210 // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2211 static inline void
2212 addr16_hi3(unsigned char* view, Address value)
2213 { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2215 // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2216 static inline void
2217 addr16_ha3(unsigned char* view, Address value)
2218 { This::addr16_hi3(view, value + 0x8000); }
2220 // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2221 static inline Status
2222 addr14(unsigned char* view, Address value, Overflow_check overflow)
2224 Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2225 if (overflow != CHECK_NONE && (value & 3) != 0)
2226 stat = STATUS_OVERFLOW;
2227 return stat;
2230 // R_POWERPC_REL16DX_HA
2231 static inline Status
2232 addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2234 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2235 Valtype* wv = reinterpret_cast<Valtype*>(view);
2236 Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2237 value += 0x8000;
2238 value = static_cast<SignedAddress>(value) >> 16;
2239 val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2240 elfcpp::Swap<32, big_endian>::writeval(wv, val);
2241 return overflowed<16>(value, overflow);
2244 // R_PPC64_D34
2245 static inline Status
2246 addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2248 Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2249 value, overflow);
2250 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2251 return stat;
2254 // R_PPC64_D34_HI30
2255 static inline void
2256 addr34_hi(unsigned char *view, uint64_t value)
2257 { This::addr34(view, value >> 34, CHECK_NONE);}
2259 // R_PPC64_D34_HA30
2260 static inline void
2261 addr34_ha(unsigned char *view, uint64_t value)
2262 { This::addr34_hi(view, value + (1ULL << 33));}
2264 // R_PPC64_D28
2265 static inline Status
2266 addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2268 Status stat = This::template rela<32,12>(view, 16, 0xfff,
2269 value, overflow);
2270 This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2271 return stat;
2274 // R_PPC64_ADDR16_HIGHER34
2275 static inline void
2276 addr16_higher34(unsigned char* view, uint64_t value)
2277 { This::addr16(view, value >> 34, CHECK_NONE); }
2279 // R_PPC64_ADDR16_HIGHERA34
2280 static inline void
2281 addr16_highera34(unsigned char* view, uint64_t value)
2282 { This::addr16_higher34(view, value + (1ULL << 33)); }
2284 // R_PPC64_ADDR16_HIGHEST34
2285 static inline void
2286 addr16_highest34(unsigned char* view, uint64_t value)
2287 { This::addr16(view, value >> 50, CHECK_NONE); }
2289 // R_PPC64_ADDR16_HIGHESTA34
2290 static inline void
2291 addr16_highesta34(unsigned char* view, uint64_t value)
2292 { This::addr16_highest34(view, value + (1ULL << 33)); }
2295 // Set ABI version for input and output.
2297 template<int size, bool big_endian>
2298 void
2299 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2301 this->e_flags_ |= ver;
2302 if (this->abiversion() != 0)
2304 Target_powerpc<size, big_endian>* target =
2305 static_cast<Target_powerpc<size, big_endian>*>(
2306 parameters->sized_target<size, big_endian>());
2307 if (target->abiversion() == 0)
2308 target->set_abiversion(this->abiversion());
2309 else if (target->abiversion() != this->abiversion())
2310 gold_error(_("%s: ABI version %d is not compatible "
2311 "with ABI version %d output"),
2312 this->name().c_str(),
2313 this->abiversion(), target->abiversion());
2318 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2319 // relocatable object, if such sections exists.
2321 template<int size, bool big_endian>
2322 bool
2323 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2324 Read_symbols_data* sd)
2326 const unsigned char* const pshdrs = sd->section_headers->data();
2327 const unsigned char* namesu = sd->section_names->data();
2328 const char* names = reinterpret_cast<const char*>(namesu);
2329 section_size_type names_size = sd->section_names_size;
2330 const unsigned char* s;
2332 s = this->template find_shdr<size, big_endian>(pshdrs,
2333 size == 32 ? ".got2" : ".opd",
2334 names, names_size, NULL);
2335 if (s != NULL)
2337 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2338 this->special_ = ndx;
2339 if (size == 64)
2341 if (this->abiversion() == 0)
2342 this->set_abiversion(1);
2343 else if (this->abiversion() > 1)
2344 gold_error(_("%s: .opd invalid in abiv%d"),
2345 this->name().c_str(), this->abiversion());
2348 if (size == 64)
2350 s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2351 names, names_size, NULL);
2352 if (s != NULL)
2354 unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2355 this->relatoc_ = ndx;
2356 typename elfcpp::Shdr<size, big_endian> shdr(s);
2357 this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2360 return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2363 // Examine .rela.opd to build info about function entry points.
2365 template<int size, bool big_endian>
2366 void
2367 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2368 size_t reloc_count,
2369 const unsigned char* prelocs,
2370 const unsigned char* plocal_syms)
2372 if (size == 64)
2374 typedef typename elfcpp::Rela<size, big_endian> Reltype;
2375 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2376 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2377 Address expected_off = 0;
2378 bool regular = true;
2379 unsigned int opd_ent_size = 0;
2381 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2383 Reltype reloc(prelocs);
2384 typename elfcpp::Elf_types<size>::Elf_WXword r_info
2385 = reloc.get_r_info();
2386 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2387 if (r_type == elfcpp::R_PPC64_ADDR64)
2389 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2390 typename elfcpp::Elf_types<size>::Elf_Addr value;
2391 bool is_ordinary;
2392 unsigned int shndx;
2393 if (r_sym < this->local_symbol_count())
2395 typename elfcpp::Sym<size, big_endian>
2396 lsym(plocal_syms + r_sym * sym_size);
2397 shndx = lsym.get_st_shndx();
2398 shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2399 value = lsym.get_st_value();
2401 else
2402 shndx = this->symbol_section_and_value(r_sym, &value,
2403 &is_ordinary);
2404 this->set_opd_ent(reloc.get_r_offset(), shndx,
2405 value + reloc.get_r_addend());
2406 if (i == 2)
2408 expected_off = reloc.get_r_offset();
2409 opd_ent_size = expected_off;
2411 else if (expected_off != reloc.get_r_offset())
2412 regular = false;
2413 expected_off += opd_ent_size;
2415 else if (r_type == elfcpp::R_PPC64_TOC)
2417 if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2418 regular = false;
2420 else
2422 gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2423 this->name().c_str(), r_type);
2424 regular = false;
2427 if (reloc_count <= 2)
2428 opd_ent_size = this->section_size(this->opd_shndx());
2429 if (opd_ent_size != 24 && opd_ent_size != 16)
2430 regular = false;
2431 if (!regular)
2433 gold_warning(_("%s: .opd is not a regular array of opd entries"),
2434 this->name().c_str());
2435 opd_ent_size = 0;
2440 // Returns true if a code sequence loading the TOC entry at VALUE
2441 // relative to the TOC pointer can be converted into code calculating
2442 // a TOC pointer relative offset.
2443 // If so, the TOC pointer relative offset is stored to VALUE.
2445 template<int size, bool big_endian>
2446 bool
2447 Powerpc_relobj<size, big_endian>::make_toc_relative(
2448 Target_powerpc<size, big_endian>* target,
2449 Address* value)
2451 if (size != 64)
2452 return false;
2454 // With -mcmodel=medium code it is quite possible to have
2455 // toc-relative relocs referring to objects outside the TOC.
2456 // Don't try to look at a non-existent TOC.
2457 if (this->toc_shndx() == 0
2458 || this->output_section(this->toc_shndx()) == 0)
2459 return false;
2461 // Convert VALUE back to an address by adding got_base (see below),
2462 // then to an offset in the TOC by subtracting the TOC output
2463 // section address and the TOC output offset.
2464 Address off = (*value + target->toc_pointer()
2465 - this->output_section(this->toc_shndx())->address()
2466 - this->output_section_offset(this->toc_shndx()));
2467 // Is this offset in the TOC? -mcmodel=medium code may be using
2468 // TOC relative access to variables outside the TOC. Those of
2469 // course can't be optimized. We also don't try to optimize code
2470 // that is using a different object's TOC.
2471 if (off >= this->section_size(this->toc_shndx()))
2472 return false;
2474 if (this->no_toc_opt(off))
2475 return false;
2477 section_size_type vlen;
2478 unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2479 Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2480 // The TOC pointer
2481 Address got_base = target->toc_pointer();
2482 addr -= got_base;
2483 if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2484 return false;
2486 *value = addr;
2487 return true;
2490 template<int size, bool big_endian>
2491 bool
2492 Powerpc_relobj<size, big_endian>::make_got_relative(
2493 Target_powerpc<size, big_endian>* target,
2494 const Symbol_value<size>* psymval,
2495 Address addend,
2496 Address* value)
2498 Address addr = psymval->value(this, addend);
2499 Address got_base = target->toc_pointer();
2500 addr -= got_base;
2501 if (addr + 0x80008000 > 0xffffffff)
2502 return false;
2504 *value = addr;
2505 return true;
2508 // Perform the Sized_relobj_file method, then set up opd info from
2509 // .opd relocs.
2511 template<int size, bool big_endian>
2512 void
2513 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2515 Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2516 if (size == 64)
2518 for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2519 p != rd->relocs.end();
2520 ++p)
2522 if (p->data_shndx == this->opd_shndx())
2524 uint64_t opd_size = this->section_size(this->opd_shndx());
2525 gold_assert(opd_size == static_cast<size_t>(opd_size));
2526 if (opd_size != 0)
2528 this->init_opd(opd_size);
2529 this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2530 rd->local_symbols->data());
2532 break;
2538 // Read the symbols then set up st_other vector.
2540 template<int size, bool big_endian>
2541 void
2542 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2544 this->base_read_symbols(sd);
2545 if (this->input_file()->format() != Input_file::FORMAT_ELF)
2546 return;
2547 if (size == 64)
2549 const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2550 const unsigned char* const pshdrs = sd->section_headers->data();
2551 const unsigned int loccount = this->do_local_symbol_count();
2552 if (loccount != 0)
2554 this->st_other_.resize(loccount);
2555 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2556 off_t locsize = loccount * sym_size;
2557 const unsigned int symtab_shndx = this->symtab_shndx();
2558 const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2559 typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2560 const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2561 locsize, true, false);
2562 psyms += sym_size;
2563 for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2565 elfcpp::Sym<size, big_endian> sym(psyms);
2566 unsigned char st_other = sym.get_st_other();
2567 this->st_other_[i] = st_other;
2568 if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2570 if (this->abiversion() == 0)
2571 this->set_abiversion(2);
2572 else if (this->abiversion() < 2)
2573 gold_error(_("%s: local symbol %d has invalid st_other"
2574 " for ABI version 1"),
2575 this->name().c_str(), i);
2581 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2582 const unsigned char* ps = sd->section_headers->data() + shdr_size;
2583 bool merge_attributes = false;
2584 for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2586 elfcpp::Shdr<size, big_endian> shdr(ps);
2587 switch (shdr.get_sh_type())
2589 case elfcpp::SHT_GNU_ATTRIBUTES:
2591 gold_assert(this->attributes_section_data_ == NULL);
2592 section_offset_type section_offset = shdr.get_sh_offset();
2593 section_size_type section_size =
2594 convert_to_section_size_type(shdr.get_sh_size());
2595 const unsigned char* view =
2596 this->get_view(section_offset, section_size, true, false);
2597 this->attributes_section_data_ =
2598 new Attributes_section_data(view, section_size);
2600 break;
2602 case elfcpp::SHT_SYMTAB:
2604 // Sometimes an object has no contents except the section
2605 // name string table and an empty symbol table with the
2606 // undefined symbol. We don't want to merge
2607 // processor-specific flags from such an object.
2608 const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2609 elfcpp::Elf_sizes<size>::sym_size;
2610 if (shdr.get_sh_size() > sym_size)
2611 merge_attributes = true;
2613 break;
2615 case elfcpp::SHT_STRTAB:
2616 break;
2618 default:
2619 merge_attributes = true;
2620 break;
2624 if (!merge_attributes)
2626 // Should rarely happen.
2627 delete this->attributes_section_data_;
2628 this->attributes_section_data_ = NULL;
2632 template<int size, bool big_endian>
2633 void
2634 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2636 this->e_flags_ |= ver;
2637 if (this->abiversion() != 0)
2639 Target_powerpc<size, big_endian>* target =
2640 static_cast<Target_powerpc<size, big_endian>*>(
2641 parameters->sized_target<size, big_endian>());
2642 if (target->abiversion() == 0)
2643 target->set_abiversion(this->abiversion());
2644 else if (target->abiversion() != this->abiversion())
2645 gold_error(_("%s: ABI version %d is not compatible "
2646 "with ABI version %d output"),
2647 this->name().c_str(),
2648 this->abiversion(), target->abiversion());
2653 // Call Sized_dynobj::base_read_symbols to read the symbols then
2654 // read .opd from a dynamic object, filling in opd_ent_ vector,
2656 template<int size, bool big_endian>
2657 void
2658 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2660 this->base_read_symbols(sd);
2661 const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2662 const unsigned char* ps =
2663 sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2664 for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2666 elfcpp::Shdr<size, big_endian> shdr(ps);
2667 if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2669 section_offset_type section_offset = shdr.get_sh_offset();
2670 section_size_type section_size =
2671 convert_to_section_size_type(shdr.get_sh_size());
2672 const unsigned char* view =
2673 this->get_view(section_offset, section_size, true, false);
2674 this->attributes_section_data_ =
2675 new Attributes_section_data(view, section_size);
2676 break;
2679 if (size == 64)
2681 const unsigned char* const pshdrs = sd->section_headers->data();
2682 const unsigned char* namesu = sd->section_names->data();
2683 const char* names = reinterpret_cast<const char*>(namesu);
2684 const unsigned char* s = NULL;
2685 const unsigned char* opd;
2686 section_size_type opd_size;
2688 // Find and read .opd section.
2689 while (1)
2691 s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2692 sd->section_names_size,
2694 if (s == NULL)
2695 return;
2697 typename elfcpp::Shdr<size, big_endian> shdr(s);
2698 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2699 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2701 if (this->abiversion() == 0)
2702 this->set_abiversion(1);
2703 else if (this->abiversion() > 1)
2704 gold_error(_("%s: .opd invalid in abiv%d"),
2705 this->name().c_str(), this->abiversion());
2707 this->opd_shndx_ = (s - pshdrs) / shdr_size;
2708 this->opd_address_ = shdr.get_sh_addr();
2709 opd_size = convert_to_section_size_type(shdr.get_sh_size());
2710 opd = this->get_view(shdr.get_sh_offset(), opd_size,
2711 true, false);
2712 break;
2716 // Build set of executable sections.
2717 // Using a set is probably overkill. There is likely to be only
2718 // a few executable sections, typically .init, .text and .fini,
2719 // and they are generally grouped together.
2720 typedef std::set<Sec_info> Exec_sections;
2721 Exec_sections exec_sections;
2722 s = pshdrs;
2723 for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2725 typename elfcpp::Shdr<size, big_endian> shdr(s);
2726 if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2727 && ((shdr.get_sh_flags()
2728 & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2729 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2730 && shdr.get_sh_size() != 0)
2732 exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2733 shdr.get_sh_size(), i));
2736 if (exec_sections.empty())
2737 return;
2739 // Look over the OPD entries. This is complicated by the fact
2740 // that some binaries will use two-word entries while others
2741 // will use the standard three-word entries. In most cases
2742 // the third word (the environment pointer for languages like
2743 // Pascal) is unused and will be zero. If the third word is
2744 // used it should not be pointing into executable sections,
2745 // I think.
2746 this->init_opd(opd_size);
2747 for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2749 typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2750 const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2751 Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2752 if (val == 0)
2753 // Chances are that this is the third word of an OPD entry.
2754 continue;
2755 typename Exec_sections::const_iterator e
2756 = exec_sections.upper_bound(Sec_info(val, 0, 0));
2757 if (e != exec_sections.begin())
2759 --e;
2760 if (e->start <= val && val < e->start + e->len)
2762 // We have an address in an executable section.
2763 // VAL ought to be the function entry, set it up.
2764 this->set_opd_ent(p - opd, e->shndx, val);
2765 // Skip second word of OPD entry, the TOC pointer.
2766 p += 8;
2769 // If we didn't match any executable sections, we likely
2770 // have a non-zero third word in the OPD entry.
2775 // Relocate sections.
2777 template<int size, bool big_endian>
2778 void
2779 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2780 const Symbol_table* symtab, const Layout* layout,
2781 const unsigned char* pshdrs, Output_file* of,
2782 typename Sized_relobj_file<size, big_endian>::Views* pviews)
2784 unsigned int start = 1;
2785 if (size == 64
2786 && this->relatoc_ != 0
2787 && !parameters->options().relocatable())
2789 // Relocate .toc first.
2790 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2791 this->relatoc_, this->relatoc_);
2792 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2793 1, this->relatoc_ - 1);
2794 start = this->relatoc_ + 1;
2796 this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2797 start, this->shnum() - 1);
2800 // Set up some symbols.
2802 template<int size, bool big_endian>
2803 void
2804 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2805 Symbol_table* symtab,
2806 Layout* layout)
2808 if (size == 32)
2810 // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2811 // undefined when scanning relocs (and thus requires
2812 // non-relative dynamic relocs). The proper value will be
2813 // updated later.
2814 Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2815 if (gotsym != NULL && gotsym->is_undefined())
2817 Target_powerpc<size, big_endian>* target =
2818 static_cast<Target_powerpc<size, big_endian>*>(
2819 parameters->sized_target<size, big_endian>());
2820 Output_data_got_powerpc<size, big_endian>* got
2821 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2822 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2823 Symbol_table::PREDEFINED,
2824 got, 0, 0,
2825 elfcpp::STT_OBJECT,
2826 elfcpp::STB_LOCAL,
2827 elfcpp::STV_HIDDEN, 0,
2828 false, false);
2831 // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2832 Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2833 if (sdasym != NULL && sdasym->is_undefined())
2835 Output_data_space* sdata = new Output_data_space(4, "** sdata");
2836 Output_section* os
2837 = layout->add_output_section_data(".sdata", 0,
2838 elfcpp::SHF_ALLOC
2839 | elfcpp::SHF_WRITE,
2840 sdata, ORDER_SMALL_DATA, false);
2841 symtab->define_in_output_data("_SDA_BASE_", NULL,
2842 Symbol_table::PREDEFINED,
2843 os, 32768, 0, elfcpp::STT_OBJECT,
2844 elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2845 0, false, false);
2848 else
2850 // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2851 Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2852 if (gotsym != NULL && gotsym->is_undefined())
2854 Target_powerpc<size, big_endian>* target =
2855 static_cast<Target_powerpc<size, big_endian>*>(
2856 parameters->sized_target<size, big_endian>());
2857 Output_data_got_powerpc<size, big_endian>* got
2858 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2859 symtab->define_in_output_data(".TOC.", NULL,
2860 Symbol_table::PREDEFINED,
2861 got, 0x8000, 0,
2862 elfcpp::STT_OBJECT,
2863 elfcpp::STB_LOCAL,
2864 elfcpp::STV_HIDDEN, 0,
2865 false, false);
2869 this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2870 if (parameters->options().tls_get_addr_optimize()
2871 && this->tls_get_addr_ != NULL
2872 && this->tls_get_addr_->in_reg())
2873 this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2874 if (this->tls_get_addr_opt_ != NULL)
2876 if (this->tls_get_addr_->is_undefined()
2877 || this->tls_get_addr_->is_from_dynobj())
2879 // Make it seem as if references to __tls_get_addr are
2880 // really to __tls_get_addr_opt, so the latter symbol is
2881 // made dynamic, not the former.
2882 this->tls_get_addr_->clear_in_reg();
2883 this->tls_get_addr_opt_->set_in_reg();
2885 // We have a non-dynamic definition for __tls_get_addr.
2886 // Make __tls_get_addr_opt the same, if it does not already have
2887 // a non-dynamic definition.
2888 else if (this->tls_get_addr_opt_->is_undefined()
2889 || this->tls_get_addr_opt_->is_from_dynobj())
2891 Sized_symbol<size>* from
2892 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2893 Sized_symbol<size>* to
2894 = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2895 symtab->clone<size>(to, from);
2900 // Set up PowerPC target specific relobj.
2902 template<int size, bool big_endian>
2903 Object*
2904 Target_powerpc<size, big_endian>::do_make_elf_object(
2905 const std::string& name,
2906 Input_file* input_file,
2907 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2909 int et = ehdr.get_e_type();
2910 // ET_EXEC files are valid input for --just-symbols/-R,
2911 // and we treat them as relocatable objects.
2912 if (et == elfcpp::ET_REL
2913 || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2915 Powerpc_relobj<size, big_endian>* obj =
2916 new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2917 obj->setup();
2918 return obj;
2920 else if (et == elfcpp::ET_DYN)
2922 Powerpc_dynobj<size, big_endian>* obj =
2923 new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2924 obj->setup();
2925 return obj;
2927 else
2929 gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2930 return NULL;
2934 template<int size, bool big_endian>
2935 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2937 public:
2938 typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2939 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2941 Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
2942 Got_type got_type)
2943 : Output_data_got<size, big_endian>(),
2944 symtab_(symtab), layout_(layout),
2945 header_ent_cnt_(size == 32 ? 3 : 1),
2946 header_index_(size == 32 ? 0x2000 : -1u)
2948 if (size == 64)
2949 this->set_addralign(256);
2950 if (size == 64 && (got_type & GOT_TYPE_SMALL))
2951 this->make_header();
2954 // Override all the Output_data_got methods we use so as to first call
2955 // reserve_ent().
2956 bool
2957 add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
2959 this->reserve_ent();
2960 return Output_data_got<size, big_endian>::add_global(gsym, got_type,
2961 addend);
2964 bool
2965 add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
2967 this->reserve_ent();
2968 return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
2969 addend);
2972 bool
2973 add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
2974 { return this->add_global_plt(gsym, got_type, addend); }
2976 void
2977 add_global_with_rel(Symbol* gsym, unsigned int got_type,
2978 Output_data_reloc_generic* rel_dyn,
2979 unsigned int r_type, uint64_t addend)
2981 this->reserve_ent();
2982 Output_data_got<size, big_endian>::
2983 add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
2986 void
2987 add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2988 Output_data_reloc_generic* rel_dyn,
2989 unsigned int r_type_1, unsigned int r_type_2,
2990 uint64_t addend)
2992 if (gsym->has_got_offset(got_type))
2993 return;
2995 this->reserve_ent(2);
2996 Output_data_got<size, big_endian>::
2997 add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
2998 addend);
3001 bool
3002 add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
3003 uint64_t addend)
3005 this->reserve_ent();
3006 return Output_data_got<size, big_endian>::add_local(object, sym_index,
3007 got_type, addend);
3010 bool
3011 add_local_plt(Relobj* object, unsigned int sym_index,
3012 unsigned int got_type, uint64_t addend)
3014 this->reserve_ent();
3015 return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
3016 got_type, addend);
3019 bool
3020 add_local_tls(Relobj* object, unsigned int sym_index,
3021 unsigned int got_type, uint64_t addend)
3022 { return this->add_local_plt(object, sym_index, got_type, addend); }
3024 void
3025 add_local_tls_pair(Relobj* object, unsigned int sym_index,
3026 unsigned int got_type,
3027 Output_data_reloc_generic* rel_dyn,
3028 unsigned int r_type, uint64_t addend)
3030 if (object->local_has_got_offset(sym_index, got_type, addend))
3031 return;
3033 this->reserve_ent(2);
3034 Output_data_got<size, big_endian>::
3035 add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
3038 unsigned int
3039 add_constant(Valtype constant)
3041 this->reserve_ent();
3042 return Output_data_got<size, big_endian>::add_constant(constant);
3045 unsigned int
3046 add_constant_pair(Valtype c1, Valtype c2)
3048 this->reserve_ent(2);
3049 return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
3052 // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
3053 unsigned int
3054 g_o_t() const
3056 if (size == 32)
3057 return this->got_offset(this->header_index_);
3058 else if (this->header_index_ != -1u)
3059 return this->got_offset(this->header_index_) + 0x8000;
3060 else
3061 gold_unreachable();
3064 // Ensure our GOT has a header.
3065 void
3066 set_final_data_size()
3068 if (size == 32 && this->header_ent_cnt_ != 0)
3069 this->make_header();
3070 Output_data_got<size, big_endian>::set_final_data_size();
3073 // First word of GOT header needs some values that are not
3074 // handled by Output_data_got so poke them in here.
3075 // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3076 void
3077 do_write(Output_file* of)
3079 if (this->header_index_ != -1u)
3081 Valtype val = 0;
3082 if (size == 32 && this->layout_->dynamic_data() != NULL)
3083 val = this->layout_->dynamic_section()->address();
3084 if (size == 64)
3085 val = this->address() + this->g_o_t();
3086 this->replace_constant(this->header_index_, val);
3088 Output_data_got<size, big_endian>::do_write(of);
3091 private:
3092 void
3093 reserve_ent(unsigned int cnt = 1)
3095 if (size != 32 || this->header_ent_cnt_ == 0)
3096 return;
3097 if (this->num_entries() + cnt > this->header_index_)
3098 this->make_header();
3101 void
3102 make_header()
3104 this->header_ent_cnt_ = 0;
3105 this->header_index_ = this->num_entries();
3106 if (size == 32)
3108 Output_data_got<size, big_endian>::add_constant(0);
3109 Output_data_got<size, big_endian>::add_constant(0);
3110 Output_data_got<size, big_endian>::add_constant(0);
3112 // Define _GLOBAL_OFFSET_TABLE_ at the header
3113 Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3114 if (gotsym != NULL)
3116 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3117 sym->set_value(this->g_o_t());
3119 else
3120 this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3121 Symbol_table::PREDEFINED,
3122 this, this->g_o_t(), 0,
3123 elfcpp::STT_OBJECT,
3124 elfcpp::STB_LOCAL,
3125 elfcpp::STV_HIDDEN, 0,
3126 false, false);
3128 else
3129 Output_data_got<size, big_endian>::add_constant(0);
3132 // Stashed pointers.
3133 Symbol_table* symtab_;
3134 Layout* layout_;
3136 // GOT header size.
3137 unsigned int header_ent_cnt_;
3138 // GOT header index.
3139 unsigned int header_index_;
3142 // Get the GOT section, creating it if necessary.
3144 template<int size, bool big_endian>
3145 Output_data_got_powerpc<size, big_endian>*
3146 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3147 Layout* layout,
3148 Got_type got_type)
3150 if (this->got_ == NULL)
3152 gold_assert(symtab != NULL && layout != NULL);
3154 this->got_
3155 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3156 GOT_TYPE_SMALL);
3158 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3159 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3160 this->got_, ORDER_DATA, false);
3163 if (size == 32 || (got_type & GOT_TYPE_SMALL))
3164 return this->got_;
3166 if (this->biggot_ == NULL)
3168 this->biggot_
3169 = new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3170 GOT_TYPE_STANDARD);
3172 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3173 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3174 this->biggot_, ORDER_DATA, false);
3177 return this->biggot_;
3180 // Get the dynamic reloc section, creating it if necessary.
3182 template<int size, bool big_endian>
3183 typename Target_powerpc<size, big_endian>::Reloc_section*
3184 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3186 if (this->rela_dyn_ == NULL)
3188 gold_assert(layout != NULL);
3189 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3190 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3191 elfcpp::SHF_ALLOC, this->rela_dyn_,
3192 ORDER_DYNAMIC_RELOCS, false);
3194 return this->rela_dyn_;
3197 // Similarly, but for ifunc symbols get the one for ifunc.
3199 template<int size, bool big_endian>
3200 typename Target_powerpc<size, big_endian>::Reloc_section*
3201 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3202 Layout* layout,
3203 bool for_ifunc)
3205 if (!for_ifunc)
3206 return this->rela_dyn_section(layout);
3208 if (this->iplt_ == NULL)
3209 this->make_iplt_section(symtab, layout);
3210 return this->iplt_->rel_plt();
3213 class Stub_control
3215 public:
3216 // Determine the stub group size. The group size is the absolute
3217 // value of the parameter --stub-group-size. If --stub-group-size
3218 // is passed a negative value, we restrict stubs to be always after
3219 // the stubbed branches.
3220 Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3221 : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3222 suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3223 state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3224 owner_(NULL), output_section_(NULL)
3228 // Return true iff input section can be handled by current stub
3229 // group.
3230 bool
3231 can_add_to_stub_group(Output_section* o,
3232 const Output_section::Input_section* i,
3233 bool has14);
3235 const Output_section::Input_section*
3236 owner()
3237 { return owner_; }
3239 Output_section*
3240 output_section()
3241 { return output_section_; }
3243 void
3244 set_output_and_owner(Output_section* o,
3245 const Output_section::Input_section* i)
3247 this->output_section_ = o;
3248 this->owner_ = i;
3251 private:
3252 typedef enum
3254 // Initial state.
3255 NO_GROUP,
3256 // Adding group sections before the stubs.
3257 FINDING_STUB_SECTION,
3258 // Adding group sections after the stubs.
3259 HAS_STUB_SECTION
3260 } State;
3262 uint32_t stub_group_size_;
3263 bool stubs_always_after_branch_;
3264 bool suppress_size_errors_;
3265 // True if a stub group can serve multiple output sections.
3266 bool multi_os_;
3267 State state_;
3268 // Current max size of group. Starts at stub_group_size_ but is
3269 // reduced to stub_group_size_/1024 on seeing a section with
3270 // external conditional branches.
3271 uint32_t group_size_;
3272 uint64_t group_start_addr_;
3273 // owner_ and output_section_ specify the section to which stubs are
3274 // attached. The stubs are placed at the end of this section.
3275 const Output_section::Input_section* owner_;
3276 Output_section* output_section_;
3279 // Return true iff input section can be handled by current stub
3280 // group. Sections are presented to this function in order,
3281 // so the first section is the head of the group.
3283 bool
3284 Stub_control::can_add_to_stub_group(Output_section* o,
3285 const Output_section::Input_section* i,
3286 bool has14)
3288 bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3289 uint64_t this_size;
3290 uint64_t start_addr = o->address();
3292 if (whole_sec)
3293 // .init and .fini sections are pasted together to form a single
3294 // function. We can't be adding stubs in the middle of the function.
3295 this_size = o->data_size();
3296 else
3298 start_addr += i->relobj()->output_section_offset(i->shndx());
3299 this_size = i->data_size();
3302 uint64_t end_addr = start_addr + this_size;
3303 uint32_t group_size = this->stub_group_size_;
3304 if (has14)
3305 this->group_size_ = group_size = group_size >> 10;
3307 if (this_size > group_size && !this->suppress_size_errors_)
3308 gold_warning(_("%s:%s exceeds group size"),
3309 i->relobj()->name().c_str(),
3310 i->relobj()->section_name(i->shndx()).c_str());
3312 gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3313 has14 ? " 14bit" : "",
3314 i->relobj()->name().c_str(),
3315 i->relobj()->section_name(i->shndx()).c_str(),
3316 (long long) this_size,
3317 (this->state_ == NO_GROUP
3318 ? this_size
3319 : (long long) end_addr - this->group_start_addr_));
3321 if (this->state_ == NO_GROUP)
3323 // Only here on very first use of Stub_control
3324 this->owner_ = i;
3325 this->output_section_ = o;
3326 this->state_ = FINDING_STUB_SECTION;
3327 this->group_size_ = group_size;
3328 this->group_start_addr_ = start_addr;
3329 return true;
3331 else if (!this->multi_os_ && this->output_section_ != o)
3333 else if (this->state_ == HAS_STUB_SECTION)
3335 // Can we add this section, which is after the stubs, to the
3336 // group?
3337 if (end_addr - this->group_start_addr_ <= this->group_size_)
3338 return true;
3340 else if (this->state_ == FINDING_STUB_SECTION)
3342 if ((whole_sec && this->output_section_ == o)
3343 || end_addr - this->group_start_addr_ <= this->group_size_)
3345 // Stubs are added at the end of "owner_".
3346 this->owner_ = i;
3347 this->output_section_ = o;
3348 return true;
3350 // The group before the stubs has reached maximum size.
3351 // Now see about adding sections after the stubs to the
3352 // group. If the current section has a 14-bit branch and
3353 // the group before the stubs exceeds group_size_ (because
3354 // they didn't have 14-bit branches), don't add sections
3355 // after the stubs: The size of stubs for such a large
3356 // group may exceed the reach of a 14-bit branch.
3357 if (!this->stubs_always_after_branch_
3358 && this_size <= this->group_size_
3359 && start_addr - this->group_start_addr_ <= this->group_size_)
3361 gold_debug(DEBUG_TARGET, "adding after stubs");
3362 this->state_ = HAS_STUB_SECTION;
3363 this->group_start_addr_ = start_addr;
3364 return true;
3367 else
3368 gold_unreachable();
3370 gold_debug(DEBUG_TARGET,
3371 !this->multi_os_ && this->output_section_ != o
3372 ? "nope, new output section\n"
3373 : "nope, didn't fit\n");
3375 // The section fails to fit in the current group. Set up a few
3376 // things for the next group. owner_ and output_section_ will be
3377 // set later after we've retrieved those values for the current
3378 // group.
3379 this->state_ = FINDING_STUB_SECTION;
3380 this->group_size_ = group_size;
3381 this->group_start_addr_ = start_addr;
3382 return false;
3385 // Look over all the input sections, deciding where to place stubs.
3387 template<int size, bool big_endian>
3388 void
3389 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3390 const Task*,
3391 bool no_size_errors)
3393 Stub_control stub_control(this->stub_group_size_, no_size_errors,
3394 parameters->options().stub_group_multi());
3396 // Group input sections and insert stub table
3397 Stub_table_owner* table_owner = NULL;
3398 std::vector<Stub_table_owner*> tables;
3399 Layout::Section_list section_list;
3400 layout->get_executable_sections(&section_list);
3401 std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3402 for (Layout::Section_list::iterator o = section_list.begin();
3403 o != section_list.end();
3404 ++o)
3406 typedef Output_section::Input_section_list Input_section_list;
3407 for (Input_section_list::const_iterator i
3408 = (*o)->input_sections().begin();
3409 i != (*o)->input_sections().end();
3410 ++i)
3412 if (i->is_input_section()
3413 || i->is_relaxed_input_section())
3415 Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3416 <Powerpc_relobj<size, big_endian>*>(i->relobj());
3417 bool has14 = ppcobj->has_14bit_branch(i->shndx());
3418 if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3420 table_owner->output_section = stub_control.output_section();
3421 table_owner->owner = stub_control.owner();
3422 stub_control.set_output_and_owner(*o, &*i);
3423 table_owner = NULL;
3425 if (table_owner == NULL)
3427 table_owner = new Stub_table_owner;
3428 tables.push_back(table_owner);
3430 ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3434 if (table_owner != NULL)
3436 table_owner->output_section = stub_control.output_section();
3437 table_owner->owner = stub_control.owner();;
3439 for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3440 t != tables.end();
3441 ++t)
3443 Stub_table<size, big_endian>* stub_table;
3445 if ((*t)->owner->is_input_section())
3446 stub_table = new Stub_table<size, big_endian>(this,
3447 (*t)->output_section,
3448 (*t)->owner,
3449 this->stub_tables_.size());
3450 else if ((*t)->owner->is_relaxed_input_section())
3451 stub_table = static_cast<Stub_table<size, big_endian>*>(
3452 (*t)->owner->relaxed_input_section());
3453 else
3454 gold_unreachable();
3455 this->stub_tables_.push_back(stub_table);
3456 delete *t;
3460 template<int size>
3461 static unsigned long
3462 max_branch_delta (unsigned int r_type)
3464 if (r_type == elfcpp::R_POWERPC_REL14
3465 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3466 || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3467 return 1L << 15;
3468 if (r_type == elfcpp::R_POWERPC_REL24
3469 || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3470 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
3471 || r_type == elfcpp::R_PPC_PLTREL24
3472 || r_type == elfcpp::R_PPC_LOCAL24PC)
3473 return 1L << 25;
3474 return 0;
3477 // Return whether this branch is going via a plt call stub.
3479 template<int size, bool big_endian>
3480 bool
3481 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3482 Powerpc_relobj<size, big_endian>* ppc_object,
3483 unsigned int shndx,
3484 Address offset,
3485 Target_powerpc* target,
3486 Symbol_table* symtab)
3488 if (this->object_ != ppc_object
3489 || this->shndx_ != shndx
3490 || this->offset_ != offset)
3491 return false;
3493 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3494 if (sym != NULL && sym->is_forwarder())
3495 sym = symtab->resolve_forwards(sym);
3496 if (target->replace_tls_get_addr(sym))
3497 sym = target->tls_get_addr_opt();
3498 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3499 if (gsym != NULL
3500 ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3501 && !target->is_elfv2_localentry0(gsym))
3502 : (this->object_->local_has_plt_offset(this->r_sym_)
3503 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3505 this->tocsave_ = 1;
3506 return true;
3508 return false;
3511 // If this branch needs a plt call stub, or a long branch stub, make one.
3513 template<int size, bool big_endian>
3514 bool
3515 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3516 Stub_table<size, big_endian>* stub_table,
3517 Stub_table<size, big_endian>* ifunc_stub_table,
3518 Symbol_table* symtab) const
3520 Symbol* sym = this->object_->global_symbol(this->r_sym_);
3521 Target_powerpc<size, big_endian>* target =
3522 static_cast<Target_powerpc<size, big_endian>*>(
3523 parameters->sized_target<size, big_endian>());
3524 if (sym != NULL && sym->is_forwarder())
3525 sym = symtab->resolve_forwards(sym);
3526 if (target->replace_tls_get_addr(sym))
3527 sym = target->tls_get_addr_opt();
3528 const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3529 bool ok = true;
3531 if (gsym != NULL
3532 ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3533 : this->object_->local_has_plt_offset(this->r_sym_))
3535 if (size == 64
3536 && gsym != NULL
3537 && target->abiversion() >= 2
3538 && !parameters->options().output_is_position_independent()
3539 && !is_branch_reloc<size>(this->r_type_))
3540 target->glink_section()->add_global_entry(gsym);
3541 else
3543 if (stub_table == NULL
3544 && !(size == 32
3545 && gsym != NULL
3546 && !parameters->options().output_is_position_independent()
3547 && !is_branch_reloc<size>(this->r_type_)))
3548 stub_table = this->object_->stub_table(this->shndx_);
3549 if (stub_table == NULL)
3551 // This is a ref from a data section to an ifunc symbol,
3552 // or a non-branch reloc for which we always want to use
3553 // one set of stubs for resolving function addresses.
3554 stub_table = ifunc_stub_table;
3556 gold_assert(stub_table != NULL);
3557 Address from = this->object_->get_output_section_offset(this->shndx_);
3558 if (from != invalid_address)
3559 from += (this->object_->output_section(this->shndx_)->address()
3560 + this->offset_);
3561 if (gsym != NULL)
3562 ok = stub_table->add_plt_call_entry(from,
3563 this->object_, gsym,
3564 this->r_type_, this->addend_,
3565 this->tocsave_);
3566 else
3567 ok = stub_table->add_plt_call_entry(from,
3568 this->object_, this->r_sym_,
3569 this->r_type_, this->addend_,
3570 this->tocsave_);
3573 else
3575 Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3576 if (max_branch_offset == 0)
3577 return true;
3578 Address from = this->object_->get_output_section_offset(this->shndx_);
3579 gold_assert(from != invalid_address);
3580 from += (this->object_->output_section(this->shndx_)->address()
3581 + this->offset_);
3582 Address to;
3583 unsigned int other = 0;
3584 if (gsym != NULL)
3586 switch (gsym->source())
3588 case Symbol::FROM_OBJECT:
3590 Object* symobj = gsym->object();
3591 if (symobj->is_dynamic()
3592 || symobj->pluginobj() != NULL)
3593 return true;
3594 bool is_ordinary;
3595 unsigned int shndx = gsym->shndx(&is_ordinary);
3596 if (shndx == elfcpp::SHN_UNDEF)
3597 return true;
3599 break;
3601 case Symbol::IS_UNDEFINED:
3602 return true;
3604 default:
3605 break;
3607 Symbol_table::Compute_final_value_status status;
3608 to = symtab->compute_final_value<size>(gsym, &status);
3609 if (status != Symbol_table::CFVS_OK)
3610 return true;
3611 if (size == 64)
3612 other = gsym->nonvis() >> 3;
3614 else
3616 const Symbol_value<size>* psymval
3617 = this->object_->local_symbol(this->r_sym_);
3618 Symbol_value<size> symval;
3619 if (psymval->is_section_symbol())
3620 symval.set_is_section_symbol();
3621 typedef Sized_relobj_file<size, big_endian> ObjType;
3622 typename ObjType::Compute_final_local_value_status status
3623 = this->object_->compute_final_local_value(this->r_sym_, psymval,
3624 &symval, symtab);
3625 if (status != ObjType::CFLV_OK
3626 || !symval.has_output_value())
3627 return true;
3628 to = symval.value(this->object_, 0);
3629 if (size == 64)
3630 other = this->object_->st_other(this->r_sym_) >> 5;
3632 if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3633 to += this->addend_;
3634 if (stub_table == NULL)
3635 stub_table = this->object_->stub_table(this->shndx_);
3636 if (size == 64 && target->abiversion() < 2)
3638 unsigned int dest_shndx;
3639 if (!target->symval_for_branch(symtab, gsym, this->object_,
3640 &to, &dest_shndx))
3641 return true;
3643 unsigned int local_ent = 0;
3644 if (size == 64
3645 && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC
3646 && this->r_type_ != elfcpp::R_PPC64_REL24_P9NOTOC)
3647 local_ent = elfcpp::ppc64_decode_local_entry(other);
3648 Address delta = to + local_ent - from;
3649 if (delta + max_branch_offset >= 2 * max_branch_offset
3650 || (size == 64
3651 && (this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3652 || this->r_type_ == elfcpp::R_PPC64_REL24_P9NOTOC)
3653 && (gsym != NULL
3654 ? this->object_->ppc64_needs_toc(gsym)
3655 : this->object_->ppc64_needs_toc(this->r_sym_))))
3657 if (stub_table == NULL)
3659 gold_warning(_("%s:%s: branch in non-executable section,"
3660 " no long branch stub for you"),
3661 this->object_->name().c_str(),
3662 this->object_->section_name(this->shndx_).c_str());
3663 return true;
3665 bool save_res = (size == 64
3666 && gsym != NULL
3667 && gsym->source() == Symbol::IN_OUTPUT_DATA
3668 && gsym->output_data() == target->savres_section());
3669 ok = stub_table->add_long_branch_entry(this->r_type_,
3670 from, to, other, save_res);
3673 if (!ok)
3674 gold_debug(DEBUG_TARGET,
3675 "branch at %s:%s+%#lx\n"
3676 "can't reach stub attached to %s:%s",
3677 this->object_->name().c_str(),
3678 this->object_->section_name(this->shndx_).c_str(),
3679 (unsigned long) this->offset_,
3680 stub_table->relobj()->name().c_str(),
3681 stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3683 return ok;
3686 // Relaxation hook. This is where we do stub generation.
3688 template<int size, bool big_endian>
3689 bool
3690 Target_powerpc<size, big_endian>::do_relax(int pass,
3691 const Input_objects*,
3692 Symbol_table* symtab,
3693 Layout* layout,
3694 const Task* task)
3696 unsigned int prev_brlt_size = 0;
3697 if (pass == 1)
3699 bool thread_safe
3700 = this->abiversion() < 2 && parameters->options().plt_thread_safe();
3701 if (size == 64
3702 && this->abiversion() < 2
3703 && !thread_safe
3704 && !parameters->options().user_set_plt_thread_safe())
3706 static const char* const thread_starter[] =
3708 "pthread_create",
3709 /* libstdc++ */
3710 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3711 /* librt */
3712 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3713 "mq_notify", "create_timer",
3714 /* libanl */
3715 "getaddrinfo_a",
3716 /* libgomp */
3717 "GOMP_parallel",
3718 "GOMP_parallel_start",
3719 "GOMP_parallel_loop_static",
3720 "GOMP_parallel_loop_static_start",
3721 "GOMP_parallel_loop_dynamic",
3722 "GOMP_parallel_loop_dynamic_start",
3723 "GOMP_parallel_loop_guided",
3724 "GOMP_parallel_loop_guided_start",
3725 "GOMP_parallel_loop_runtime",
3726 "GOMP_parallel_loop_runtime_start",
3727 "GOMP_parallel_sections",
3728 "GOMP_parallel_sections_start",
3729 /* libgo */
3730 "__go_go",
3733 if (parameters->options().shared())
3734 thread_safe = true;
3735 else
3737 for (unsigned int i = 0;
3738 i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3739 i++)
3741 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3742 thread_safe = (sym != NULL
3743 && sym->in_reg()
3744 && sym->in_real_elf());
3745 if (thread_safe)
3746 break;
3750 this->plt_thread_safe_ = thread_safe;
3753 if (pass == 1)
3755 this->stub_group_size_ = parameters->options().stub_group_size();
3756 bool no_size_errors = true;
3757 if (this->stub_group_size_ == 1)
3758 this->stub_group_size_ = 0x1c00000;
3759 else if (this->stub_group_size_ == -1)
3760 this->stub_group_size_ = -0x1e00000;
3761 else
3762 no_size_errors = false;
3763 this->group_sections(layout, task, no_size_errors);
3765 else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3767 this->branch_lookup_table_.clear();
3768 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3769 p != this->stub_tables_.end();
3770 ++p)
3772 (*p)->clear_stubs(true);
3774 this->stub_tables_.clear();
3775 this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3776 gold_info(_("%s: stub group size is too large; retrying with %#x"),
3777 program_name, this->stub_group_size_);
3778 this->group_sections(layout, task, true);
3781 // We need address of stub tables valid for make_stub.
3782 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3783 p != this->stub_tables_.end();
3784 ++p)
3786 const Powerpc_relobj<size, big_endian>* object
3787 = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3788 Address off = object->get_output_section_offset((*p)->shndx());
3789 gold_assert(off != invalid_address);
3790 Output_section* os = (*p)->output_section();
3791 (*p)->set_address_and_size(os, off);
3794 if (pass != 1)
3796 // Clear plt call stubs, long branch stubs and branch lookup table.
3797 prev_brlt_size = this->branch_lookup_table_.size();
3798 this->branch_lookup_table_.clear();
3799 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3800 p != this->stub_tables_.end();
3801 ++p)
3803 (*p)->clear_stubs(false);
3807 // Build all the stubs.
3808 this->relax_failed_ = false;
3809 Stub_table<size, big_endian>* ifunc_stub_table
3810 = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3811 Stub_table<size, big_endian>* one_stub_table
3812 = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3813 for (typename Branches::const_iterator b = this->branch_info_.begin();
3814 b != this->branch_info_.end();
3815 b++)
3817 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3818 && !this->relax_failed_)
3820 this->relax_failed_ = true;
3821 this->relax_fail_count_++;
3822 if (this->relax_fail_count_ < 3)
3823 return true;
3826 bool do_resize = false;
3827 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3828 p != this->stub_tables_.end();
3829 ++p)
3830 if ((*p)->need_resize())
3832 do_resize = true;
3833 break;
3835 if (do_resize)
3837 this->branch_lookup_table_.clear();
3838 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3839 p != this->stub_tables_.end();
3840 ++p)
3841 (*p)->set_resizing(true);
3842 for (typename Branches::const_iterator b = this->branch_info_.begin();
3843 b != this->branch_info_.end();
3844 b++)
3846 if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3847 && !this->relax_failed_)
3849 this->relax_failed_ = true;
3850 this->relax_fail_count_++;
3851 if (this->relax_fail_count_ < 3)
3852 return true;
3855 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3856 p != this->stub_tables_.end();
3857 ++p)
3858 (*p)->set_resizing(false);
3861 // Did anything change size?
3862 unsigned int num_huge_branches = this->branch_lookup_table_.size();
3863 bool again = num_huge_branches != prev_brlt_size;
3864 if (size == 64 && num_huge_branches != 0)
3865 this->make_brlt_section(layout);
3866 if (size == 64 && again)
3867 this->brlt_section_->set_current_size(num_huge_branches);
3869 for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3870 p != this->stub_tables_.rend();
3871 ++p)
3872 (*p)->remove_eh_frame(layout);
3874 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3875 p != this->stub_tables_.end();
3876 ++p)
3877 (*p)->add_eh_frame(layout);
3879 typedef Unordered_set<Output_section*> Output_sections;
3880 Output_sections os_need_update;
3881 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3882 p != this->stub_tables_.end();
3883 ++p)
3885 if ((*p)->size_update())
3887 again = true;
3888 os_need_update.insert((*p)->output_section());
3892 // Set output section offsets for all input sections in an output
3893 // section that just changed size. Anything past the stubs will
3894 // need updating.
3895 for (typename Output_sections::iterator p = os_need_update.begin();
3896 p != os_need_update.end();
3897 p++)
3899 Output_section* os = *p;
3900 Address off = 0;
3901 typedef Output_section::Input_section_list Input_section_list;
3902 for (Input_section_list::const_iterator i = os->input_sections().begin();
3903 i != os->input_sections().end();
3904 ++i)
3906 off = align_address(off, i->addralign());
3907 if (i->is_input_section() || i->is_relaxed_input_section())
3908 i->relobj()->set_section_offset(i->shndx(), off);
3909 if (i->is_relaxed_input_section())
3911 Stub_table<size, big_endian>* stub_table
3912 = static_cast<Stub_table<size, big_endian>*>(
3913 i->relaxed_input_section());
3914 Address stub_table_size = stub_table->set_address_and_size(os, off);
3915 off += stub_table_size;
3916 // After a few iterations, set current stub table size
3917 // as min size threshold, so later stub tables can only
3918 // grow in size.
3919 if (pass >= 4)
3920 stub_table->set_min_size_threshold(stub_table_size);
3922 else
3923 off += i->data_size();
3925 // If .branch_lt is part of this output section, then we have
3926 // just done the offset adjustment.
3927 os->clear_section_offsets_need_adjustment();
3930 if (size == 64
3931 && !again
3932 && num_huge_branches != 0
3933 && parameters->options().output_is_position_independent())
3935 // Fill in the BRLT relocs.
3936 this->brlt_section_->reset_brlt_sizes();
3937 for (typename Branch_lookup_table::const_iterator p
3938 = this->branch_lookup_table_.begin();
3939 p != this->branch_lookup_table_.end();
3940 ++p)
3942 this->brlt_section_->add_reloc(p->first, p->second);
3944 this->brlt_section_->finalize_brlt_sizes();
3947 if (!again
3948 && (parameters->options().user_set_emit_stub_syms()
3949 ? parameters->options().emit_stub_syms()
3950 : (size == 64
3951 || parameters->options().output_is_position_independent()
3952 || parameters->options().emit_relocs())))
3954 for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3955 p != this->stub_tables_.end();
3956 ++p)
3957 (*p)->define_stub_syms(symtab);
3959 if (this->glink_ != NULL)
3961 int stub_size = this->glink_->pltresolve_size();
3962 Address value = -stub_size;
3963 if (size == 64)
3965 value = 8;
3966 stub_size -= 8;
3968 this->define_local(symtab, "__glink_PLTresolve",
3969 this->glink_, value, stub_size);
3971 if (size != 64)
3972 this->define_local(symtab, "__glink", this->glink_, 0, 0);
3976 return again;
3979 template<int size, bool big_endian>
3980 void
3981 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3982 unsigned char* oview,
3983 uint64_t* paddress,
3984 off_t* plen) const
3986 uint64_t address = plt->address();
3987 off_t len = plt->data_size();
3989 if (plt == this->glink_)
3991 // See Output_data_glink::do_write() for glink contents.
3992 if (len == 0)
3994 // Static linking may need stubs, to support ifunc and long
3995 // branches. We need to create an output section for
3996 // .eh_frame early in the link process, to have a place to
3997 // attach stub .eh_frame info. We also need to have
3998 // registered a CIE that matches the stub CIE. Both of
3999 // these requirements are satisfied by creating an FDE and
4000 // CIE for .glink, even though static linking will leave
4001 // .glink zero length.
4002 // ??? Hopefully generating an FDE with a zero address range
4003 // won't confuse anything that consumes .eh_frame info.
4005 else if (size == 64)
4007 // There is one word before __glink_PLTresolve
4008 address += 8;
4009 len -= 8;
4011 else if (parameters->options().output_is_position_independent())
4013 // There are two FDEs for a position independent glink.
4014 // The first covers the branch table, the second
4015 // __glink_PLTresolve at the end of glink.
4016 off_t resolve_size = this->glink_->pltresolve_size();
4017 if (oview[9] == elfcpp::DW_CFA_nop)
4018 len -= resolve_size;
4019 else
4021 address += len - resolve_size;
4022 len = resolve_size;
4026 else
4028 // Must be a stub table.
4029 const Stub_table<size, big_endian>* stub_table
4030 = static_cast<const Stub_table<size, big_endian>*>(plt);
4031 uint64_t stub_address = stub_table->stub_address();
4032 len -= stub_address - address;
4033 address = stub_address;
4036 *paddress = address;
4037 *plen = len;
4040 // A class to handle the PLT data.
4042 template<int size, bool big_endian>
4043 class Output_data_plt_powerpc : public Output_section_data_build
4045 public:
4046 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4047 size, big_endian> Reloc_section;
4049 Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4050 Symbol_table* symtab,
4051 Reloc_section* plt_rel,
4052 const char* name)
4053 : Output_section_data_build(size == 32 ? 4 : 8),
4054 rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
4057 // Add an entry to the PLT.
4058 void
4059 add_entry(Symbol*, bool = false);
4061 void
4062 add_ifunc_entry(Symbol*);
4064 void
4065 add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4067 void
4068 add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4070 // Return the .rela.plt section data.
4071 Reloc_section*
4072 rel_plt() const
4074 return this->rel_;
4077 // Return the number of PLT entries.
4078 unsigned int
4079 entry_count() const
4081 if (this->current_data_size() == 0)
4082 return 0;
4083 return ((this->current_data_size() - this->first_plt_entry_offset())
4084 / this->plt_entry_size());
4087 protected:
4088 void
4089 do_adjust_output_section(Output_section* os)
4091 os->set_entsize(0);
4094 // Write to a map file.
4095 void
4096 do_print_to_mapfile(Mapfile* mapfile) const
4097 { mapfile->print_output_data(this, this->name_); }
4099 private:
4100 struct Local_plt_ent
4102 Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
4103 { rsym_ = rsym; u.obj_ = obj; }
4104 Local_plt_ent(Symbol* sym)
4105 { rsym_ = -1u; u.gsym_ = sym; }
4106 ~Local_plt_ent()
4109 unsigned int rsym_;
4110 union
4112 Sized_relobj_file<size, big_endian>* obj_;
4113 Symbol* gsym_;
4114 } u;
4117 // Return the offset of the first non-reserved PLT entry.
4118 unsigned int
4119 first_plt_entry_offset() const
4121 // IPLT and LPLT have no reserved entry.
4122 if (this->name_[3] == 'I' || this->name_[3] == 'L')
4123 return 0;
4124 return this->targ_->first_plt_entry_offset();
4127 // Return the size of each PLT entry.
4128 unsigned int
4129 plt_entry_size() const
4131 return this->targ_->plt_entry_size();
4134 // Write out the PLT data.
4135 void
4136 do_write(Output_file*);
4138 // The reloc section.
4139 Reloc_section* rel_;
4140 // Allows access to .glink for do_write.
4141 Target_powerpc<size, big_endian>* targ_;
4142 Symbol_table* symtab_;
4143 // What to report in map file.
4144 const char *name_;
4146 std::vector<Local_plt_ent> sym_ents_;
4149 // Add an entry to the PLT.
4151 template<int size, bool big_endian>
4152 void
4153 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
4154 bool is_local)
4156 if (!gsym->has_plt_offset())
4158 section_size_type off = this->current_data_size();
4159 if (off == 0)
4160 off += this->first_plt_entry_offset();
4161 gsym->set_plt_offset(off);
4162 if (this->rel_)
4164 if (is_local)
4166 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4167 if (size == 64 && this->targ_->abiversion() < 2)
4168 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4169 this->rel_->add_symbolless_global_addend(gsym, dynrel,
4170 this, off, 0);
4172 else
4174 gsym->set_needs_dynsym_entry();
4175 unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4176 this->rel_->add_global(gsym, dynrel, this, off, 0);
4179 off += this->plt_entry_size();
4180 this->set_current_data_size(off);
4181 if (is_local)
4183 Local_plt_ent sym(gsym);
4184 this->sym_ents_.push_back(sym);
4189 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4191 template<int size, bool big_endian>
4192 void
4193 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4195 if (!gsym->has_plt_offset())
4197 section_size_type off = this->current_data_size();
4198 gsym->set_plt_offset(off);
4199 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4200 if (size == 64 && this->targ_->abiversion() < 2)
4201 dynrel = elfcpp::R_PPC64_JMP_IREL;
4202 this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4203 off += this->plt_entry_size();
4204 this->set_current_data_size(off);
4208 // Add an entry for a local symbol to the PLT.
4210 template<int size, bool big_endian>
4211 void
4212 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4213 Sized_relobj_file<size, big_endian>* relobj,
4214 unsigned int local_sym_index)
4216 if (!relobj->local_has_plt_offset(local_sym_index))
4218 section_size_type off = this->current_data_size();
4219 relobj->set_local_plt_offset(local_sym_index, off);
4220 if (this->rel_)
4222 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4223 if (size == 64 && this->targ_->abiversion() < 2)
4224 dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4225 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4226 dynrel, this, off, 0);
4228 off += this->plt_entry_size();
4229 this->set_current_data_size(off);
4230 Local_plt_ent sym(relobj, local_sym_index);
4231 this->sym_ents_.push_back(sym);
4235 // Add an entry for a local ifunc symbol to the IPLT.
4237 template<int size, bool big_endian>
4238 void
4239 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4240 Sized_relobj_file<size, big_endian>* relobj,
4241 unsigned int local_sym_index)
4243 if (!relobj->local_has_plt_offset(local_sym_index))
4245 section_size_type off = this->current_data_size();
4246 relobj->set_local_plt_offset(local_sym_index, off);
4247 unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4248 if (size == 64 && this->targ_->abiversion() < 2)
4249 dynrel = elfcpp::R_PPC64_JMP_IREL;
4250 this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4251 this, off, 0);
4252 off += this->plt_entry_size();
4253 this->set_current_data_size(off);
4257 static const uint32_t add_0_11_11 = 0x7c0b5a14;
4258 static const uint32_t add_2_2_11 = 0x7c425a14;
4259 static const uint32_t add_2_2_12 = 0x7c426214;
4260 static const uint32_t add_3_3_2 = 0x7c631214;
4261 static const uint32_t add_3_3_13 = 0x7c636a14;
4262 static const uint32_t add_3_12_2 = 0x7c6c1214;
4263 static const uint32_t add_3_12_13 = 0x7c6c6a14;
4264 static const uint32_t add_11_0_11 = 0x7d605a14;
4265 static const uint32_t add_11_2_11 = 0x7d625a14;
4266 static const uint32_t add_11_11_2 = 0x7d6b1214;
4267 static const uint32_t add_12_11_12 = 0x7d8b6214;
4268 static const uint32_t addi_0_12 = 0x380c0000;
4269 static const uint32_t addi_2_2 = 0x38420000;
4270 static const uint32_t addi_3_3 = 0x38630000;
4271 static const uint32_t addi_11_11 = 0x396b0000;
4272 static const uint32_t addi_12_1 = 0x39810000;
4273 static const uint32_t addi_12_11 = 0x398b0000;
4274 static const uint32_t addi_12_12 = 0x398c0000;
4275 static const uint32_t addis_0_2 = 0x3c020000;
4276 static const uint32_t addis_0_13 = 0x3c0d0000;
4277 static const uint32_t addis_2_12 = 0x3c4c0000;
4278 static const uint32_t addis_11_2 = 0x3d620000;
4279 static const uint32_t addis_11_11 = 0x3d6b0000;
4280 static const uint32_t addis_11_30 = 0x3d7e0000;
4281 static const uint32_t addis_12_1 = 0x3d810000;
4282 static const uint32_t addis_12_2 = 0x3d820000;
4283 static const uint32_t addis_12_11 = 0x3d8b0000;
4284 static const uint32_t addis_12_12 = 0x3d8c0000;
4285 static const uint32_t b = 0x48000000;
4286 static const uint32_t bcl_20_31 = 0x429f0005;
4287 static const uint32_t bctr = 0x4e800420;
4288 static const uint32_t bctrl = 0x4e800421;
4289 static const uint32_t beqlr = 0x4d820020;
4290 static const uint32_t blr = 0x4e800020;
4291 static const uint32_t bnectr_p4 = 0x4ce20420;
4292 static const uint32_t cmpld_7_12_0 = 0x7fac0040;
4293 static const uint32_t cmpldi_2_0 = 0x28220000;
4294 static const uint32_t cmpdi_11_0 = 0x2c2b0000;
4295 static const uint32_t cmpwi_11_0 = 0x2c0b0000;
4296 static const uint32_t cror_15_15_15 = 0x4def7b82;
4297 static const uint32_t cror_31_31_31 = 0x4ffffb82;
4298 static const uint32_t ld_0_1 = 0xe8010000;
4299 static const uint32_t ld_0_11 = 0xe80b0000;
4300 static const uint32_t ld_0_12 = 0xe80c0000;
4301 static const uint32_t ld_2_1 = 0xe8410000;
4302 static const uint32_t ld_2_2 = 0xe8420000;
4303 static const uint32_t ld_2_11 = 0xe84b0000;
4304 static const uint32_t ld_2_12 = 0xe84c0000;
4305 static const uint32_t ld_11_1 = 0xe9610000;
4306 static const uint32_t ld_11_2 = 0xe9620000;
4307 static const uint32_t ld_11_3 = 0xe9630000;
4308 static const uint32_t ld_11_11 = 0xe96b0000;
4309 static const uint32_t ld_12_2 = 0xe9820000;
4310 static const uint32_t ld_12_3 = 0xe9830000;
4311 static const uint32_t ld_12_11 = 0xe98b0000;
4312 static const uint32_t ld_12_12 = 0xe98c0000;
4313 static const uint32_t ldx_12_11_12 = 0x7d8b602a;
4314 static const uint32_t lfd_0_1 = 0xc8010000;
4315 static const uint32_t li_0_0 = 0x38000000;
4316 static const uint32_t li_11_0 = 0x39600000;
4317 static const uint32_t li_12_0 = 0x39800000;
4318 static const uint32_t lis_0 = 0x3c000000;
4319 static const uint32_t lis_2 = 0x3c400000;
4320 static const uint32_t lis_11 = 0x3d600000;
4321 static const uint32_t lis_12 = 0x3d800000;
4322 static const uint32_t lvx_0_12_0 = 0x7c0c00ce;
4323 static const uint32_t lwz_0_12 = 0x800c0000;
4324 static const uint32_t lwz_11_3 = 0x81630000;
4325 static const uint32_t lwz_11_11 = 0x816b0000;
4326 static const uint32_t lwz_11_30 = 0x817e0000;
4327 static const uint32_t lwz_12_3 = 0x81830000;
4328 static const uint32_t lwz_12_12 = 0x818c0000;
4329 static const uint32_t lwzu_0_12 = 0x840c0000;
4330 static const uint32_t mflr_0 = 0x7c0802a6;
4331 static const uint32_t mflr_11 = 0x7d6802a6;
4332 static const uint32_t mflr_12 = 0x7d8802a6;
4333 static const uint32_t mr_0_3 = 0x7c601b78;
4334 static const uint32_t mr_3_0 = 0x7c030378;
4335 static const uint32_t mtctr_0 = 0x7c0903a6;
4336 static const uint32_t mtctr_11 = 0x7d6903a6;
4337 static const uint32_t mtctr_12 = 0x7d8903a6;
4338 static const uint32_t mtlr_0 = 0x7c0803a6;
4339 static const uint32_t mtlr_11 = 0x7d6803a6;
4340 static const uint32_t mtlr_12 = 0x7d8803a6;
4341 static const uint32_t nop = 0x60000000;
4342 static const uint32_t ori_0_0_0 = 0x60000000;
4343 static const uint32_t ori_11_11_0 = 0x616b0000;
4344 static const uint32_t ori_12_12_0 = 0x618c0000;
4345 static const uint32_t oris_12_12_0 = 0x658c0000;
4346 static const uint32_t sldi_11_11_34 = 0x796b1746;
4347 static const uint32_t sldi_12_12_32 = 0x799c07c6;
4348 static const uint32_t srdi_0_0_2 = 0x7800f082;
4349 static const uint32_t std_0_1 = 0xf8010000;
4350 static const uint32_t std_0_12 = 0xf80c0000;
4351 static const uint32_t std_2_1 = 0xf8410000;
4352 static const uint32_t std_11_1 = 0xf9610000;
4353 static const uint32_t stfd_0_1 = 0xd8010000;
4354 static const uint32_t stvx_0_12_0 = 0x7c0c01ce;
4355 static const uint32_t sub_11_11_12 = 0x7d6c5850;
4356 static const uint32_t sub_12_12_11 = 0x7d8b6050;
4357 static const uint32_t xor_2_12_12 = 0x7d826278;
4358 static const uint32_t xor_11_12_12 = 0x7d8b6278;
4360 static const uint64_t paddi_12_pc = 0x0610000039800000ULL;
4361 static const uint64_t pld_12_pc = 0x04100000e5800000ULL;
4362 static const uint64_t pnop = 0x0700000000000000ULL;
4364 // Write out the PLT.
4366 template<int size, bool big_endian>
4367 void
4368 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4370 if (!this->sym_ents_.empty()
4371 && !parameters->options().output_is_position_independent())
4373 const section_size_type offset = this->offset();
4374 const section_size_type oview_size
4375 = convert_to_section_size_type(this->data_size());
4376 unsigned char* const oview = of->get_output_view(offset, oview_size);
4377 unsigned char* pov = oview;
4378 unsigned char* endpov = oview + oview_size;
4380 for (typename std::vector<Local_plt_ent>::iterator e
4381 = this->sym_ents_.begin();
4382 e != this->sym_ents_.end();
4383 e++)
4385 typename elfcpp::Elf_types<size>::Elf_Addr val;
4386 Sized_symbol<size>* gsym = NULL;
4387 Powerpc_relobj<size, big_endian>* obj = NULL;
4388 if (e->rsym_ == -1u)
4390 gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
4391 val = gsym->value();
4393 else
4395 obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
4396 val = obj->local_symbol(e->rsym_)->value(obj, 0);
4398 if (this->targ_->abiversion() >= 2)
4400 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4401 pov += size / 8;
4403 else
4405 unsigned int shndx;
4406 this->targ_->symval_for_branch(this->symtab_, gsym, obj,
4407 &val, &shndx);
4408 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4409 pov += size / 8;
4410 val = this->targ_->toc_pointer();
4411 elfcpp::Swap<size, big_endian>::writeval(pov, val);
4412 pov += size / 8;
4413 if (this->plt_entry_size() > 16)
4415 elfcpp::Swap<size, big_endian>::writeval(pov, 0);
4416 pov += size / 8;
4420 gold_assert(pov == endpov);
4423 if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4425 const section_size_type offset = this->offset();
4426 const section_size_type oview_size
4427 = convert_to_section_size_type(this->data_size());
4428 unsigned char* const oview = of->get_output_view(offset, oview_size);
4429 unsigned char* pov = oview;
4430 unsigned char* endpov = oview + oview_size;
4432 // The address of the .glink branch table
4433 const Output_data_glink<size, big_endian>* glink
4434 = this->targ_->glink_section();
4435 elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4437 while (pov < endpov)
4439 elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4440 pov += 4;
4441 branch_tab += 4;
4444 of->write_output_view(offset, oview_size, oview);
4448 // Create the PLT section.
4450 template<int size, bool big_endian>
4451 void
4452 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4453 Layout* layout)
4455 if (this->plt_ == NULL)
4457 if (this->got_ == NULL)
4458 this->got_section(symtab, layout, GOT_TYPE_SMALL);
4460 if (this->glink_ == NULL)
4461 make_glink_section(layout);
4463 // Ensure that .rela.dyn always appears before .rela.plt This is
4464 // necessary due to how, on PowerPC and some other targets, .rela.dyn
4465 // needs to include .rela.plt in its range.
4466 this->rela_dyn_section(layout);
4468 Reloc_section* plt_rel = new Reloc_section(false);
4469 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4470 elfcpp::SHF_ALLOC, plt_rel,
4471 ORDER_DYNAMIC_PLT_RELOCS, false);
4472 this->plt_
4473 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
4474 "** PLT");
4475 layout->add_output_section_data(".plt",
4476 (size == 32
4477 ? elfcpp::SHT_PROGBITS
4478 : elfcpp::SHT_NOBITS),
4479 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4480 this->plt_,
4481 (size == 32
4482 ? ORDER_SMALL_DATA
4483 : ORDER_SMALL_BSS),
4484 false);
4486 Output_section* rela_plt_os = plt_rel->output_section();
4487 rela_plt_os->set_info_section(this->plt_->output_section());
4491 // Create the IPLT section.
4493 template<int size, bool big_endian>
4494 void
4495 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4496 Layout* layout)
4498 if (this->iplt_ == NULL)
4500 this->make_plt_section(symtab, layout);
4501 this->make_lplt_section(symtab, layout);
4503 Reloc_section* iplt_rel = new Reloc_section(false);
4504 if (this->rela_dyn_->output_section())
4505 this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4506 this->iplt_
4507 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
4508 "** IPLT");
4509 if (this->plt_->output_section())
4510 this->plt_->output_section()->add_output_section_data(this->iplt_);
4514 // Create the LPLT section.
4516 template<int size, bool big_endian>
4517 void
4518 Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
4519 Layout* layout)
4521 if (this->lplt_ == NULL)
4523 Reloc_section* lplt_rel = NULL;
4524 if (parameters->options().output_is_position_independent())
4525 lplt_rel = this->rela_dyn_section(layout);
4526 this->lplt_
4527 = new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
4528 "** LPLT");
4529 this->make_brlt_section(layout);
4530 if (this->brlt_section_ && this->brlt_section_->output_section())
4531 this->brlt_section_->output_section()
4532 ->add_output_section_data(this->lplt_);
4533 else
4534 layout->add_output_section_data(".branch_lt",
4535 elfcpp::SHT_PROGBITS,
4536 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4537 this->lplt_,
4538 ORDER_RELRO,
4539 true);
4543 // A section for huge long branch addresses, similar to plt section.
4545 template<int size, bool big_endian>
4546 class Output_data_brlt_powerpc : public Output_section_data_build
4548 public:
4549 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4550 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4551 size, big_endian> Reloc_section;
4553 Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4554 Reloc_section* brlt_rel)
4555 : Output_section_data_build(size == 32 ? 4 : 8),
4556 rel_(brlt_rel),
4557 targ_(targ)
4560 void
4561 reset_brlt_sizes()
4563 this->reset_data_size();
4564 this->rel_->reset_data_size();
4567 void
4568 finalize_brlt_sizes()
4570 this->finalize_data_size();
4571 this->rel_->finalize_data_size();
4574 // Add a reloc for an entry in the BRLT.
4575 void
4576 add_reloc(Address to, unsigned int off)
4577 { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4579 // Update section and reloc section size.
4580 void
4581 set_current_size(unsigned int num_branches)
4583 this->reset_address_and_file_offset();
4584 this->set_current_data_size(num_branches * 16);
4585 this->finalize_data_size();
4586 Output_section* os = this->output_section();
4587 os->set_section_offsets_need_adjustment();
4588 if (this->rel_ != NULL)
4590 const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4591 this->rel_->reset_address_and_file_offset();
4592 this->rel_->set_current_data_size(num_branches * reloc_size);
4593 this->rel_->finalize_data_size();
4594 Output_section* os = this->rel_->output_section();
4595 os->set_section_offsets_need_adjustment();
4599 protected:
4600 void
4601 do_adjust_output_section(Output_section* os)
4603 os->set_entsize(0);
4606 // Write to a map file.
4607 void
4608 do_print_to_mapfile(Mapfile* mapfile) const
4609 { mapfile->print_output_data(this, "** BRLT"); }
4611 private:
4612 // Write out the BRLT data.
4613 void
4614 do_write(Output_file*);
4616 // The reloc section.
4617 Reloc_section* rel_;
4618 Target_powerpc<size, big_endian>* targ_;
4621 // Make the branch lookup table section.
4623 template<int size, bool big_endian>
4624 void
4625 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4627 if (size == 64 && this->brlt_section_ == NULL)
4629 Reloc_section* brlt_rel = NULL;
4630 bool is_pic = parameters->options().output_is_position_independent();
4631 if (is_pic)
4633 // When PIC we can't fill in .branch_lt but must initialise at
4634 // runtime via dynamic relocations.
4635 brlt_rel = this->rela_dyn_section(layout);
4637 this->brlt_section_
4638 = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4639 if (this->plt_ && is_pic && this->plt_->output_section())
4640 this->plt_->output_section()
4641 ->add_output_section_data(this->brlt_section_);
4642 else
4643 layout->add_output_section_data(".branch_lt",
4644 elfcpp::SHT_PROGBITS,
4645 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4646 this->brlt_section_,
4647 ORDER_RELRO,
4648 true);
4652 // Write out .branch_lt when non-PIC.
4654 template<int size, bool big_endian>
4655 void
4656 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4658 if (size == 64 && !parameters->options().output_is_position_independent())
4660 const section_size_type offset = this->offset();
4661 const section_size_type oview_size
4662 = convert_to_section_size_type(this->data_size());
4663 unsigned char* const oview = of->get_output_view(offset, oview_size);
4665 this->targ_->write_branch_lookup_table(oview);
4666 of->write_output_view(offset, oview_size, oview);
4670 static inline uint32_t
4671 l(uint32_t a)
4673 return a & 0xffff;
4676 static inline uint32_t
4677 hi(uint32_t a)
4679 return l(a >> 16);
4682 static inline uint32_t
4683 ha(uint32_t a)
4685 return hi(a + 0x8000);
4688 static inline uint64_t
4689 d34(uint64_t v)
4691 return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4694 static inline uint64_t
4695 ha34(uint64_t v)
4697 return (v + (1ULL << 33)) >> 34;
4700 template<int size>
4701 struct Eh_cie
4703 static const unsigned char eh_frame_cie[12];
4706 template<int size>
4707 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4709 1, // CIE version.
4710 'z', 'R', 0, // Augmentation string.
4711 4, // Code alignment.
4712 0x80 - size / 8 , // Data alignment.
4713 65, // RA reg.
4714 1, // Augmentation size.
4715 (elfcpp::DW_EH_PE_pcrel
4716 | elfcpp::DW_EH_PE_sdata4), // FDE encoding.
4717 elfcpp::DW_CFA_def_cfa, 1, 0 // def_cfa: r1 offset 0.
4720 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4721 static const unsigned char glink_eh_frame_fde_64v1[] =
4723 0, 0, 0, 0, // Replaced with offset to .glink.
4724 0, 0, 0, 0, // Replaced with size of .glink.
4725 0, // Augmentation size.
4726 elfcpp::DW_CFA_advance_loc + 2,
4727 elfcpp::DW_CFA_register, 65, 12,
4728 elfcpp::DW_CFA_advance_loc + 4,
4729 elfcpp::DW_CFA_restore_extended, 65
4732 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4733 static const unsigned char glink_eh_frame_fde_64v2[] =
4735 0, 0, 0, 0, // Replaced with offset to .glink.
4736 0, 0, 0, 0, // Replaced with size of .glink.
4737 0, // Augmentation size.
4738 elfcpp::DW_CFA_advance_loc + 2,
4739 elfcpp::DW_CFA_register, 65, 0,
4740 elfcpp::DW_CFA_advance_loc + 2,
4741 elfcpp::DW_CFA_restore_extended, 65
4744 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4746 0, 0, 0, 0, // Replaced with offset to .glink.
4747 0, 0, 0, 0, // Replaced with size of .glink.
4748 0, // Augmentation size.
4749 elfcpp::DW_CFA_advance_loc + 3,
4750 elfcpp::DW_CFA_register, 65, 0,
4751 elfcpp::DW_CFA_advance_loc + 2,
4752 elfcpp::DW_CFA_restore_extended, 65
4755 // Describe __glink_PLTresolve use of LR, 32-bit version.
4756 static const unsigned char glink_eh_frame_fde_32[] =
4758 0, 0, 0, 0, // Replaced with offset to .glink.
4759 0, 0, 0, 0, // Replaced with size of .glink.
4760 0, // Augmentation size.
4761 elfcpp::DW_CFA_advance_loc + 2,
4762 elfcpp::DW_CFA_register, 65, 0,
4763 elfcpp::DW_CFA_advance_loc + 4,
4764 elfcpp::DW_CFA_restore_extended, 65
4767 static const unsigned char default_fde[] =
4769 0, 0, 0, 0, // Replaced with offset to stubs.
4770 0, 0, 0, 0, // Replaced with size of stubs.
4771 0, // Augmentation size.
4772 elfcpp::DW_CFA_nop, // Pad.
4773 elfcpp::DW_CFA_nop,
4774 elfcpp::DW_CFA_nop
4777 template<bool big_endian>
4778 static inline void
4779 write_insn(unsigned char* p, uint32_t v)
4781 elfcpp::Swap<32, big_endian>::writeval(p, v);
4784 template<int size>
4785 static inline unsigned int
4786 param_plt_align()
4788 if (!parameters->options().user_set_plt_align())
4789 return size == 64 ? 32 : 8;
4790 return 1 << parameters->options().plt_align();
4793 // Stub_table holds information about plt and long branch stubs.
4794 // Stubs are built in an area following some input section determined
4795 // by group_sections(). This input section is converted to a relaxed
4796 // input section allowing it to be resized to accommodate the stubs
4798 template<int size, bool big_endian>
4799 class Stub_table : public Output_relaxed_input_section
4801 public:
4802 struct Plt_stub_ent
4804 Plt_stub_ent(unsigned int off, unsigned int indx)
4805 : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
4806 toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
4809 unsigned int off_;
4810 unsigned int indx_;
4811 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4812 // toc stub, p9off_ is offset to p9notoc stub
4813 unsigned int tocoff_ : 8;
4814 unsigned int p9off_ : 8;
4815 // The size of the toc stub, used to locate blr on tls_get_addr stub.
4816 unsigned int tsize_ : 8;
4817 // Stub revision management
4818 unsigned int iter_ : 1;
4819 // The three types of stubs.
4820 unsigned int toc_ : 1;
4821 unsigned int notoc_ : 1;
4822 unsigned int p9notoc_ : 1;
4823 // Each with a possible variant saving r2 first
4824 unsigned int r2save_ : 1;
4825 // Handy cached info from symbol
4826 unsigned int localentry0_ : 1;
4828 struct Branch_stub_ent
4830 Branch_stub_ent(unsigned int off)
4831 : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
4832 p9notoc_(0), save_res_(0), other_(0)
4835 unsigned int off_;
4836 // off_ points at p10 notoc stub, tocoff_ is offset from there to
4837 // toc stub, p9off_ is offset to p9notoc stub
4838 unsigned int tocoff_ : 8;
4839 unsigned int p9off_ : 8;
4840 // Stub revision management
4841 unsigned int iter_ : 1;
4842 // Four types of stubs.
4843 unsigned int toc_ : 1;
4844 unsigned int notoc_ : 1;
4845 unsigned int p9notoc_ : 1;
4846 unsigned int save_res_ : 1;
4847 // Handy cached info from symbol
4848 unsigned int other_ : 3;
4850 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4851 static const Address invalid_address = static_cast<Address>(0) - 1;
4853 Stub_table(Target_powerpc<size, big_endian>* targ,
4854 Output_section* output_section,
4855 const Output_section::Input_section* owner,
4856 uint32_t id)
4857 : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4858 owner->relobj()
4859 ->section_addralign(owner->shndx())),
4860 targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4861 orig_data_size_(owner->current_data_size()),
4862 plt_size_(0), last_plt_size_(0),
4863 branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4864 need_save_res_(false), need_resize_(false), resizing_(false),
4865 uniq_(id)
4867 this->set_output_section(output_section);
4869 std::vector<Output_relaxed_input_section*> new_relaxed;
4870 new_relaxed.push_back(this);
4871 output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4874 // Add a plt call stub.
4875 bool
4876 add_plt_call_entry(Address,
4877 const Sized_relobj_file<size, big_endian>*,
4878 const Symbol*,
4879 unsigned int,
4880 Address,
4881 bool);
4883 bool
4884 add_plt_call_entry(Address,
4885 const Sized_relobj_file<size, big_endian>*,
4886 unsigned int,
4887 unsigned int,
4888 Address,
4889 bool);
4891 // Find a given plt call stub.
4892 const Plt_stub_ent*
4893 find_plt_call_entry(const Symbol*) const;
4895 const Plt_stub_ent*
4896 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4897 unsigned int) const;
4899 const Plt_stub_ent*
4900 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4901 const Symbol*,
4902 unsigned int,
4903 Address) const;
4905 const Plt_stub_ent*
4906 find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4907 unsigned int,
4908 unsigned int,
4909 Address) const;
4911 // Add a long branch stub.
4912 bool
4913 add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
4915 const Branch_stub_ent*
4916 find_long_branch_entry(Address) const;
4918 bool
4919 can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4921 Address max_branch_offset = max_branch_delta<size>(r_type);
4922 if (max_branch_offset == 0)
4923 return true;
4924 gold_assert(from != invalid_address);
4925 Address loc = off + this->stub_address();
4926 return loc - from + max_branch_offset < 2 * max_branch_offset;
4929 void
4930 clear_stubs(bool all)
4932 this->plt_call_stubs_.clear();
4933 this->plt_size_ = 0;
4934 this->long_branch_stubs_.clear();
4935 this->branch_size_ = 0;
4936 this->need_save_res_ = false;
4937 if (all)
4939 this->last_plt_size_ = 0;
4940 this->last_branch_size_ = 0;
4944 bool
4945 need_resize() const
4946 { return need_resize_; }
4948 void
4949 set_resizing(bool val)
4951 this->resizing_ = val;
4952 if (val)
4954 this->need_resize_ = false;
4955 this->plt_size_ = 0;
4956 this->branch_size_ = 0;
4957 this->need_save_res_ = false;
4961 Address
4962 set_address_and_size(const Output_section* os, Address off)
4964 Address start_off = off;
4965 off += this->orig_data_size_;
4966 Address my_size = this->plt_size_ + this->branch_size_;
4967 if (this->need_save_res_)
4968 my_size += this->targ_->savres_section()->data_size();
4969 if (my_size != 0)
4970 off = align_address(off, this->stub_align());
4971 // Include original section size and alignment padding in size
4972 my_size += off - start_off;
4973 // Ensure new size is always larger than min size
4974 // threshold. Alignment requirement is included in "my_size", so
4975 // increase "my_size" does not invalidate alignment.
4976 if (my_size < this->min_size_threshold_)
4977 my_size = this->min_size_threshold_;
4978 this->reset_address_and_file_offset();
4979 this->set_current_data_size(my_size);
4980 this->set_address_and_file_offset(os->address() + start_off,
4981 os->offset() + start_off);
4982 return my_size;
4985 Address
4986 stub_address() const
4988 return align_address(this->address() + this->orig_data_size_,
4989 this->stub_align());
4992 Address
4993 stub_offset() const
4995 return align_address(this->offset() + this->orig_data_size_,
4996 this->stub_align());
4999 section_size_type
5000 plt_size() const
5001 { return this->plt_size_; }
5003 section_size_type
5004 branch_size() const
5005 { return this->branch_size_; }
5007 void
5008 set_min_size_threshold(Address min_size)
5009 { this->min_size_threshold_ = min_size; }
5011 void
5012 define_stub_syms(Symbol_table*);
5014 bool
5015 size_update()
5017 Output_section* os = this->output_section();
5018 if (os->addralign() < this->stub_align())
5020 os->set_addralign(this->stub_align());
5021 // FIXME: get rid of the insane checkpointing.
5022 // We can't increase alignment of the input section to which
5023 // stubs are attached; The input section may be .init which
5024 // is pasted together with other .init sections to form a
5025 // function. Aligning might insert zero padding resulting in
5026 // sigill. However we do need to increase alignment of the
5027 // output section so that the align_address() on offset in
5028 // set_address_and_size() adds the same padding as the
5029 // align_address() on address in stub_address().
5030 // What's more, we need this alignment for the layout done in
5031 // relaxation_loop_body() so that the output section starts at
5032 // a suitably aligned address.
5033 os->checkpoint_set_addralign(this->stub_align());
5035 if (this->last_plt_size_ != this->plt_size_
5036 || this->last_branch_size_ != this->branch_size_)
5038 this->last_plt_size_ = this->plt_size_;
5039 this->last_branch_size_ = this->branch_size_;
5040 return true;
5042 return false;
5045 // Add .eh_frame info for this stub section.
5046 void
5047 add_eh_frame(Layout* layout);
5049 // Remove .eh_frame info for this stub section.
5050 void
5051 remove_eh_frame(Layout* layout);
5053 Target_powerpc<size, big_endian>*
5054 targ() const
5055 { return targ_; }
5057 private:
5058 class Plt_stub_key;
5059 class Plt_stub_key_hash;
5060 typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
5061 Plt_stub_key_hash> Plt_stub_entries;
5062 class Branch_stub_key;
5063 class Branch_stub_key_hash;
5064 typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
5065 Branch_stub_key_hash> Branch_stub_entries;
5067 // Alignment of stub section.
5068 unsigned int
5069 stub_align() const
5071 unsigned int min_align = size == 64 ? 32 : 16;
5072 unsigned int user_align = 1 << parameters->options().plt_align();
5073 return std::max(user_align, min_align);
5076 // Return the plt offset for the given call stub.
5077 Address
5078 plt_off(typename Plt_stub_entries::const_iterator p,
5079 const Output_data_plt_powerpc<size, big_endian>** sec) const
5081 const Symbol* gsym = p->first.sym_;
5082 if (gsym != NULL)
5083 return this->targ_->plt_off(gsym, sec);
5084 else
5086 const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
5087 unsigned int local_sym_index = p->first.locsym_;
5088 return this->targ_->plt_off(relobj, local_sym_index, sec);
5092 // Size of a given plt call stub.
5093 unsigned int
5094 plt_call_size(typename Plt_stub_entries::iterator p) const;
5096 unsigned int
5097 plt_call_align(unsigned int bytes) const
5099 unsigned int align = param_plt_align<size>();
5100 return (bytes + align - 1) & -align;
5103 // Return long branch stub size.
5104 unsigned int
5105 branch_stub_size(typename Branch_stub_entries::iterator p,
5106 bool* need_lt);
5108 void
5109 build_tls_opt_head(unsigned char** pp, bool save_lr);
5111 void
5112 build_tls_opt_tail(unsigned char* p);
5114 void
5115 plt_error(const Plt_stub_key& p);
5117 // Write out stubs.
5118 void
5119 do_write(Output_file*);
5121 // Plt call stub keys.
5122 class Plt_stub_key
5124 public:
5125 Plt_stub_key(const Symbol* sym)
5126 : sym_(sym), object_(0), addend_(0), locsym_(0)
5129 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5130 unsigned int locsym_index)
5131 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5134 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5135 const Symbol* sym,
5136 unsigned int r_type,
5137 Address addend)
5138 : sym_(sym), object_(0), addend_(0), locsym_(0)
5140 if (size != 32)
5141 this->addend_ = addend;
5142 else if (parameters->options().output_is_position_independent()
5143 && (r_type == elfcpp::R_PPC_PLTREL24
5144 || r_type == elfcpp::R_POWERPC_PLTCALL))
5146 this->addend_ = addend;
5147 if (this->addend_ >= 32768)
5148 this->object_ = object;
5152 Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5153 unsigned int locsym_index,
5154 unsigned int r_type,
5155 Address addend)
5156 : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5158 if (size != 32)
5159 this->addend_ = addend;
5160 else if (parameters->options().output_is_position_independent()
5161 && (r_type == elfcpp::R_PPC_PLTREL24
5162 || r_type == elfcpp::R_POWERPC_PLTCALL))
5163 this->addend_ = addend;
5166 bool operator==(const Plt_stub_key& that) const
5168 return (this->sym_ == that.sym_
5169 && this->object_ == that.object_
5170 && this->addend_ == that.addend_
5171 && this->locsym_ == that.locsym_);
5174 const Symbol* sym_;
5175 const Sized_relobj_file<size, big_endian>* object_;
5176 typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5177 unsigned int locsym_;
5180 class Plt_stub_key_hash
5182 public:
5183 size_t operator()(const Plt_stub_key& ent) const
5185 return (reinterpret_cast<uintptr_t>(ent.sym_)
5186 ^ reinterpret_cast<uintptr_t>(ent.object_)
5187 ^ ent.addend_
5188 ^ ent.locsym_);
5192 // Long branch stub keys.
5193 class Branch_stub_key
5195 public:
5196 Branch_stub_key(Address to)
5197 : dest_(to)
5200 bool operator==(const Branch_stub_key& that) const
5202 return this->dest_ == that.dest_;
5205 Address dest_;
5208 class Branch_stub_key_hash
5210 public:
5211 size_t operator()(const Branch_stub_key& key) const
5212 { return key.dest_; }
5215 // In a sane world this would be a global.
5216 Target_powerpc<size, big_endian>* targ_;
5217 // Map sym/object/addend to stub offset.
5218 Plt_stub_entries plt_call_stubs_;
5219 // Map destination address to stub offset.
5220 Branch_stub_entries long_branch_stubs_;
5221 // size of input section
5222 section_size_type orig_data_size_;
5223 // size of stubs
5224 section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5225 // Some rare cases cause (PR/20529) fluctuation in stub table
5226 // size, which leads to an endless relax loop. This is to be fixed
5227 // by, after the first few iterations, allowing only increase of
5228 // stub table size. This variable sets the minimal possible size of
5229 // a stub table, it is zero for the first few iterations, then
5230 // increases monotonically.
5231 Address min_size_threshold_;
5232 // Set if this stub group needs a copy of out-of-line register
5233 // save/restore functions.
5234 bool need_save_res_;
5235 // Set when notoc_/r2save_ changes after sizing a stub
5236 bool need_resize_;
5237 // Set when resizing stubs
5238 bool resizing_;
5239 // Per stub table unique identifier.
5240 uint32_t uniq_;
5243 // Add a plt call stub, if we do not already have one for this
5244 // sym/object/addend combo.
5246 template<int size, bool big_endian>
5247 bool
5248 Stub_table<size, big_endian>::add_plt_call_entry(
5249 Address from,
5250 const Sized_relobj_file<size, big_endian>* object,
5251 const Symbol* gsym,
5252 unsigned int r_type,
5253 Address addend,
5254 bool tocsave)
5256 Plt_stub_key key(object, gsym, r_type, addend);
5257 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5258 std::pair<typename Plt_stub_entries::iterator, bool> p
5259 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5260 if (size == 64)
5262 if (p.second
5263 && this->targ_->is_elfv2_localentry0(gsym))
5265 p.first->second.localentry0_ = 1;
5266 this->targ_->set_has_localentry0();
5268 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5269 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5271 if (this->targ_->power10_stubs()
5272 && (!this->targ_->power10_stubs_auto()
5273 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5275 if (!p.second && !p.first->second.notoc_)
5276 this->need_resize_ = true;
5277 p.first->second.notoc_ = 1;
5279 else
5281 if (!p.second && !p.first->second.p9notoc_)
5282 this->need_resize_ = true;
5283 p.first->second.p9notoc_ = 1;
5286 else
5288 if (!p.second && !p.first->second.toc_)
5289 this->need_resize_ = true;
5290 p.first->second.toc_ = 1;
5291 if (!tocsave && !p.first->second.localentry0_)
5293 if (!p.second && !p.first->second.r2save_)
5294 this->need_resize_ = true;
5295 p.first->second.r2save_ = 1;
5299 if (p.second || (this->resizing_ && !p.first->second.iter_))
5301 if (this->resizing_)
5303 p.first->second.iter_ = 1;
5304 p.first->second.off_ = this->plt_size_;
5306 this->plt_size_ += this->plt_call_size(p.first);
5307 if (this->targ_->is_tls_get_addr_opt(gsym))
5308 this->targ_->set_has_tls_get_addr_opt();
5310 return this->can_reach_stub(from, p.first->second.off_, r_type);
5313 template<int size, bool big_endian>
5314 bool
5315 Stub_table<size, big_endian>::add_plt_call_entry(
5316 Address from,
5317 const Sized_relobj_file<size, big_endian>* object,
5318 unsigned int locsym_index,
5319 unsigned int r_type,
5320 Address addend,
5321 bool tocsave)
5323 Plt_stub_key key(object, locsym_index, r_type, addend);
5324 Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5325 std::pair<typename Plt_stub_entries::iterator, bool> p
5326 = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5327 if (size == 64)
5329 if (p.second
5330 && this->targ_->is_elfv2_localentry0(object, locsym_index))
5332 p.first->second.localentry0_ = 1;
5333 this->targ_->set_has_localentry0();
5335 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5336 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5338 if (this->targ_->power10_stubs()
5339 && (!this->targ_->power10_stubs_auto()
5340 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5342 if (!p.second && !p.first->second.notoc_)
5343 this->need_resize_ = true;
5344 p.first->second.notoc_ = 1;
5346 else
5348 if (!p.second && !p.first->second.p9notoc_)
5349 this->need_resize_ = true;
5350 p.first->second.p9notoc_ = 1;
5353 else
5355 if (!p.second && !p.first->second.toc_)
5356 this->need_resize_ = true;
5357 p.first->second.toc_ = 1;
5358 if (!tocsave && !p.first->second.localentry0_)
5360 if (!p.second && !p.first->second.r2save_)
5361 this->need_resize_ = true;
5362 p.first->second.r2save_ = 1;
5366 if (p.second || (this->resizing_ && !p.first->second.iter_))
5368 if (this->resizing_)
5370 p.first->second.iter_ = 1;
5371 p.first->second.off_ = this->plt_size_;
5373 this->plt_size_ += this->plt_call_size(p.first);
5375 return this->can_reach_stub(from, p.first->second.off_, r_type);
5378 // Find a plt call stub.
5380 template<int size, bool big_endian>
5381 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5382 Stub_table<size, big_endian>::find_plt_call_entry(
5383 const Sized_relobj_file<size, big_endian>* object,
5384 const Symbol* gsym,
5385 unsigned int r_type,
5386 Address addend) const
5388 Plt_stub_key key(object, gsym, r_type, addend);
5389 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5390 if (p == this->plt_call_stubs_.end())
5391 return NULL;
5392 return &p->second;
5395 template<int size, bool big_endian>
5396 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5397 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5399 Plt_stub_key key(gsym);
5400 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5401 if (p == this->plt_call_stubs_.end())
5402 return NULL;
5403 return &p->second;
5406 template<int size, bool big_endian>
5407 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5408 Stub_table<size, big_endian>::find_plt_call_entry(
5409 const Sized_relobj_file<size, big_endian>* object,
5410 unsigned int locsym_index,
5411 unsigned int r_type,
5412 Address addend) const
5414 Plt_stub_key key(object, locsym_index, r_type, addend);
5415 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5416 if (p == this->plt_call_stubs_.end())
5417 return NULL;
5418 return &p->second;
5421 template<int size, bool big_endian>
5422 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5423 Stub_table<size, big_endian>::find_plt_call_entry(
5424 const Sized_relobj_file<size, big_endian>* object,
5425 unsigned int locsym_index) const
5427 Plt_stub_key key(object, locsym_index);
5428 typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5429 if (p == this->plt_call_stubs_.end())
5430 return NULL;
5431 return &p->second;
5434 // Add a long branch stub if we don't already have one to given
5435 // destination.
5437 template<int size, bool big_endian>
5438 bool
5439 Stub_table<size, big_endian>::add_long_branch_entry(
5440 unsigned int r_type,
5441 Address from,
5442 Address to,
5443 unsigned int other,
5444 bool save_res)
5446 Branch_stub_key key(to);
5447 Branch_stub_ent ent(this->branch_size_);
5448 std::pair<typename Branch_stub_entries::iterator, bool> p
5449 = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5450 if (save_res)
5452 if (!p.second && !p.first->second.save_res_)
5453 this->need_resize_ = true;
5454 p.first->second.save_res_ = true;
5456 else if (size == 64
5457 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
5458 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
5460 if (this->targ_->power10_stubs()
5461 && (!this->targ_->power10_stubs_auto()
5462 || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5464 if (!p.second && !p.first->second.notoc_)
5465 this->need_resize_ = true;
5466 p.first->second.notoc_ = true;
5468 else
5470 if (!p.second && !p.first->second.p9notoc_)
5471 this->need_resize_ = true;
5472 p.first->second.p9notoc_ = true;
5475 else
5477 if (!p.second && !p.first->second.toc_)
5478 this->need_resize_ = true;
5479 p.first->second.toc_ = true;
5481 if (size == 64 && p.first->second.other_ == 0)
5482 p.first->second.other_ = other;
5483 if (p.second || (this->resizing_ && !p.first->second.iter_))
5485 if (this->resizing_)
5487 p.first->second.iter_ = 1;
5488 p.first->second.off_ = this->branch_size_;
5490 if (save_res)
5491 this->need_save_res_ = true;
5492 else
5494 bool need_lt = false;
5495 unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5496 this->branch_size_ += stub_size;
5497 if (size == 64 && need_lt)
5498 this->targ_->add_branch_lookup_table(to);
5501 return this->can_reach_stub(from, p.first->second.off_, r_type);
5504 // Find long branch stub offset.
5506 template<int size, bool big_endian>
5507 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5508 Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
5510 Branch_stub_key key(to);
5511 typename Branch_stub_entries::const_iterator p
5512 = this->long_branch_stubs_.find(key);
5513 if (p == this->long_branch_stubs_.end())
5514 return NULL;
5515 return &p->second;
5518 template<bool big_endian>
5519 static void
5520 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5522 delta /= 4;
5523 if (delta < 64)
5524 fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5525 else if (delta < 256)
5527 fde.push_back(elfcpp::DW_CFA_advance_loc1);
5528 fde.push_back(delta);
5530 else if (delta < 65536)
5532 fde.resize(fde.size() + 3);
5533 unsigned char *p = &*fde.end() - 3;
5534 *p++ = elfcpp::DW_CFA_advance_loc2;
5535 elfcpp::Swap<16, big_endian>::writeval(p, delta);
5537 else
5539 fde.resize(fde.size() + 5);
5540 unsigned char *p = &*fde.end() - 5;
5541 *p++ = elfcpp::DW_CFA_advance_loc4;
5542 elfcpp::Swap<32, big_endian>::writeval(p, delta);
5546 template<typename T>
5547 static bool
5548 stub_sort(T s1, T s2)
5550 return s1->second.off_ < s2->second.off_;
5553 // Add .eh_frame info for this stub section. Unlike other linker
5554 // generated .eh_frame this is added late in the link, because we
5555 // only want the .eh_frame info if this particular stub section is
5556 // non-empty.
5558 template<int size, bool big_endian>
5559 void
5560 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5562 if (size != 64
5563 || !parameters->options().ld_generated_unwind_info())
5564 return;
5566 // Since we add stub .eh_frame info late, it must be placed
5567 // after all other linker generated .eh_frame info so that
5568 // merge mapping need not be updated for input sections.
5569 // There is no provision to use a different CIE to that used
5570 // by .glink.
5571 if (!this->targ_->has_glink())
5572 return;
5574 typedef typename Plt_stub_entries::iterator plt_iter;
5575 std::vector<plt_iter> calls;
5576 if (!this->plt_call_stubs_.empty())
5577 for (plt_iter cs = this->plt_call_stubs_.begin();
5578 cs != this->plt_call_stubs_.end();
5579 ++cs)
5580 if (cs->second.p9notoc_
5581 || (cs->second.toc_
5582 && cs->second.r2save_
5583 && !cs->second.localentry0_
5584 && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
5585 calls.push_back(cs);
5586 if (calls.size() > 1)
5587 std::stable_sort(calls.begin(), calls.end(),
5588 stub_sort<plt_iter>);
5590 typedef typename Branch_stub_entries::const_iterator branch_iter;
5591 std::vector<branch_iter> branches;
5592 if (!this->long_branch_stubs_.empty()
5593 && !this->targ_->power10_stubs())
5594 for (branch_iter bs = this->long_branch_stubs_.begin();
5595 bs != this->long_branch_stubs_.end();
5596 ++bs)
5597 if (bs->second.notoc_)
5598 branches.push_back(bs);
5599 if (branches.size() > 1)
5600 std::stable_sort(branches.begin(), branches.end(),
5601 stub_sort<branch_iter>);
5603 if (calls.empty() && branches.empty())
5604 return;
5606 unsigned int last_eh_loc = 0;
5607 // offset pcrel sdata4, size udata4, and augmentation size byte.
5608 std::vector<unsigned char> fde(9, 0);
5610 for (unsigned int i = 0; i < calls.size(); i++)
5612 plt_iter cs = calls[i];
5613 unsigned int off = cs->second.off_;
5614 // The __tls_get_addr_opt call stub needs to describe where
5615 // it saves LR, to support exceptions that might be thrown
5616 // from __tls_get_addr, and to support asynchronous exceptions.
5617 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5619 off += 7 * 4;
5620 if (cs->second.toc_
5621 && cs->second.r2save_
5622 && !cs->second.localentry0_)
5624 off += cs->second.tocoff_ + 2 * 4;
5625 eh_advance<big_endian>(fde, off - last_eh_loc);
5626 fde.resize(fde.size() + 6);
5627 unsigned char* p = &*fde.end() - 6;
5628 *p++ = elfcpp::DW_CFA_offset_extended_sf;
5629 *p++ = 65;
5630 *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5631 unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
5632 *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5633 *p++ = elfcpp::DW_CFA_restore_extended;
5634 *p++ = 65;
5635 last_eh_loc = off + delta;
5636 off = cs->second.off_ + 7 * 4;
5639 // notoc stubs also should describe LR changes, to support
5640 // asynchronous exceptions.
5641 if (cs->second.p9notoc_)
5643 off += cs->second.p9off_;
5644 off += (cs->second.r2save_ ? 4 : 0) + 8;
5645 eh_advance<big_endian>(fde, off - last_eh_loc);
5646 fde.resize(fde.size() + 6);
5647 unsigned char* p = &*fde.end() - 6;
5648 *p++ = elfcpp::DW_CFA_register;
5649 *p++ = 65;
5650 *p++ = 12;
5651 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5652 *p++ = elfcpp::DW_CFA_restore_extended;
5653 *p++ = 65;
5654 last_eh_loc = off + 8;
5658 for (unsigned int i = 0; i < branches.size(); i++)
5660 branch_iter bs = branches[i];
5661 unsigned int off = bs->second.off_ + 8;
5662 eh_advance<big_endian>(fde, off - last_eh_loc);
5663 fde.resize(fde.size() + 6);
5664 unsigned char* p = &*fde.end() - 6;
5665 *p++ = elfcpp::DW_CFA_register;
5666 *p++ = 65;
5667 *p++ = 12;
5668 *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5669 *p++ = elfcpp::DW_CFA_restore_extended;
5670 *p++ = 65;
5671 last_eh_loc = off + 8;
5674 layout->add_eh_frame_for_plt(this,
5675 Eh_cie<size>::eh_frame_cie,
5676 sizeof (Eh_cie<size>::eh_frame_cie),
5677 &*fde.begin(), fde.size());
5680 template<int size, bool big_endian>
5681 void
5682 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5684 if (size == 64
5685 && parameters->options().ld_generated_unwind_info()
5686 && this->targ_->has_glink())
5687 layout->remove_eh_frame_for_plt(this,
5688 Eh_cie<size>::eh_frame_cie,
5689 sizeof (Eh_cie<size>::eh_frame_cie));
5692 // A class to handle .glink.
5694 template<int size, bool big_endian>
5695 class Output_data_glink : public Output_section_data
5697 public:
5698 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5699 static const Address invalid_address = static_cast<Address>(0) - 1;
5701 Output_data_glink(Target_powerpc<size, big_endian>* targ)
5702 : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5703 end_branch_table_(), ge_size_(0)
5706 void
5707 add_eh_frame(Layout* layout);
5709 void
5710 add_global_entry(const Symbol*);
5712 Address
5713 find_global_entry(const Symbol*) const;
5715 unsigned int
5716 global_entry_align(unsigned int off) const
5718 unsigned int align = param_plt_align<size>();
5719 return (off + align - 1) & -align;
5722 unsigned int
5723 global_entry_off() const
5725 return this->global_entry_align(this->end_branch_table_);
5728 Address
5729 global_entry_address() const
5731 gold_assert(this->is_data_size_valid());
5732 return this->address() + this->global_entry_off();
5736 pltresolve_size() const
5738 if (size == 64)
5739 return (8
5740 + (this->targ_->abiversion() < 2 ? 11 * 4
5741 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5742 return 16 * 4;
5745 protected:
5746 // Write to a map file.
5747 void
5748 do_print_to_mapfile(Mapfile* mapfile) const
5749 { mapfile->print_output_data(this, _("** glink")); }
5751 private:
5752 void
5753 set_final_data_size();
5755 // Write out .glink
5756 void
5757 do_write(Output_file*);
5759 // Allows access to .got and .plt for do_write.
5760 Target_powerpc<size, big_endian>* targ_;
5762 // Map sym to stub offset.
5763 typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5764 Global_entry_stub_entries global_entry_stubs_;
5766 unsigned int end_branch_table_, ge_size_;
5769 template<int size, bool big_endian>
5770 void
5771 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5773 if (!parameters->options().ld_generated_unwind_info())
5774 return;
5776 if (size == 64)
5778 if (this->targ_->abiversion() < 2)
5779 layout->add_eh_frame_for_plt(this,
5780 Eh_cie<64>::eh_frame_cie,
5781 sizeof (Eh_cie<64>::eh_frame_cie),
5782 glink_eh_frame_fde_64v1,
5783 sizeof (glink_eh_frame_fde_64v1));
5784 else if (this->targ_->has_localentry0())
5785 layout->add_eh_frame_for_plt(this,
5786 Eh_cie<64>::eh_frame_cie,
5787 sizeof (Eh_cie<64>::eh_frame_cie),
5788 glink_eh_frame_fde_64v2_localentry0,
5789 sizeof (glink_eh_frame_fde_64v2));
5790 else
5791 layout->add_eh_frame_for_plt(this,
5792 Eh_cie<64>::eh_frame_cie,
5793 sizeof (Eh_cie<64>::eh_frame_cie),
5794 glink_eh_frame_fde_64v2,
5795 sizeof (glink_eh_frame_fde_64v2));
5797 else
5799 // 32-bit .glink can use the default since the CIE return
5800 // address reg, LR, is valid.
5801 layout->add_eh_frame_for_plt(this,
5802 Eh_cie<32>::eh_frame_cie,
5803 sizeof (Eh_cie<32>::eh_frame_cie),
5804 default_fde,
5805 sizeof (default_fde));
5806 // Except where LR is used in a PIC __glink_PLTresolve.
5807 if (parameters->options().output_is_position_independent())
5808 layout->add_eh_frame_for_plt(this,
5809 Eh_cie<32>::eh_frame_cie,
5810 sizeof (Eh_cie<32>::eh_frame_cie),
5811 glink_eh_frame_fde_32,
5812 sizeof (glink_eh_frame_fde_32));
5816 template<int size, bool big_endian>
5817 void
5818 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5820 unsigned int off = this->global_entry_align(this->ge_size_);
5821 std::pair<typename Global_entry_stub_entries::iterator, bool> p
5822 = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5823 if (p.second)
5824 this->ge_size_ = off + 16;
5827 template<int size, bool big_endian>
5828 typename Output_data_glink<size, big_endian>::Address
5829 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5831 typename Global_entry_stub_entries::const_iterator p
5832 = this->global_entry_stubs_.find(gsym);
5833 return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5836 template<int size, bool big_endian>
5837 void
5838 Output_data_glink<size, big_endian>::set_final_data_size()
5840 unsigned int count = this->targ_->plt_entry_count();
5841 section_size_type total = 0;
5843 if (count != 0)
5845 if (size == 32)
5847 // space for branch table
5848 total += 4 * (count - 1);
5850 total += -total & 15;
5851 total += this->pltresolve_size();
5853 else
5855 total += this->pltresolve_size();
5857 // space for branch table
5858 total += 4 * count;
5859 if (this->targ_->abiversion() < 2)
5861 total += 4 * count;
5862 if (count > 0x8000)
5863 total += 4 * (count - 0x8000);
5867 this->end_branch_table_ = total;
5868 total = this->global_entry_align(total);
5869 total += this->ge_size_;
5871 this->set_data_size(total);
5874 // Define symbols on stubs, identifying the stub.
5876 template<int size, bool big_endian>
5877 void
5878 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5880 if (!this->plt_call_stubs_.empty())
5882 // The key for the plt call stub hash table includes addresses,
5883 // therefore traversal order depends on those addresses, which
5884 // can change between runs if gold is a PIE. Unfortunately the
5885 // output .symtab ordering depends on the order in which symbols
5886 // are added to the linker symtab. We want reproducible output
5887 // so must sort the call stub symbols.
5888 typedef typename Plt_stub_entries::iterator plt_iter;
5889 std::vector<plt_iter> sorted;
5890 sorted.resize(this->plt_call_stubs_.size());
5892 for (plt_iter cs = this->plt_call_stubs_.begin();
5893 cs != this->plt_call_stubs_.end();
5894 ++cs)
5895 sorted[cs->second.indx_] = cs;
5897 for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5899 plt_iter cs = sorted[i];
5900 char add[10];
5901 add[0] = 0;
5902 if (cs->first.addend_ != 0)
5903 sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5904 char obj[10];
5905 obj[0] = 0;
5906 if (cs->first.object_)
5908 const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5909 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5910 sprintf(obj, "%x:", ppcobj->uniq());
5912 char localname[9];
5913 const char *symname;
5914 if (cs->first.sym_ == NULL)
5916 sprintf(localname, "%x", cs->first.locsym_);
5917 symname = localname;
5919 else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5920 symname = this->targ_->tls_get_addr_opt()->name();
5921 else
5922 symname = cs->first.sym_->name();
5923 char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5924 sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5925 Address value
5926 = this->stub_address() - this->address() + cs->second.off_;
5927 unsigned int stub_size = this->plt_call_size(cs);
5928 this->targ_->define_local(symtab, name, this, value, stub_size);
5932 typedef typename Branch_stub_entries::iterator branch_iter;
5933 for (branch_iter bs = this->long_branch_stubs_.begin();
5934 bs != this->long_branch_stubs_.end();
5935 ++bs)
5937 if (bs->second.save_res_)
5938 continue;
5940 char* name = new char[8 + 13 + 16 + 1];
5941 sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5942 static_cast<unsigned long long>(bs->first.dest_));
5943 Address value = (this->stub_address() - this->address()
5944 + this->plt_size_ + bs->second.off_);
5945 bool need_lt = false;
5946 unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5947 this->targ_->define_local(symtab, name, this, value, stub_size);
5951 // Emit the start of a __tls_get_addr_opt plt call stub.
5953 template<int size, bool big_endian>
5954 void
5955 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5956 bool save_lr)
5958 unsigned char* p = *pp;
5959 if (size == 64)
5961 write_insn<big_endian>(p, ld_11_3 + 0);
5962 p += 4;
5963 write_insn<big_endian>(p, ld_12_3 + 8);
5964 p += 4;
5965 write_insn<big_endian>(p, mr_0_3);
5966 p += 4;
5967 write_insn<big_endian>(p, cmpdi_11_0);
5968 p += 4;
5969 write_insn<big_endian>(p, add_3_12_13);
5970 p += 4;
5971 write_insn<big_endian>(p, beqlr);
5972 p += 4;
5973 write_insn<big_endian>(p, mr_3_0);
5974 p += 4;
5975 if (save_lr)
5977 write_insn<big_endian>(p, mflr_11);
5978 p += 4;
5979 write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5980 p += 4;
5983 else
5985 write_insn<big_endian>(p, lwz_11_3 + 0);
5986 p += 4;
5987 write_insn<big_endian>(p, lwz_12_3 + 4);
5988 p += 4;
5989 write_insn<big_endian>(p, mr_0_3);
5990 p += 4;
5991 write_insn<big_endian>(p, cmpwi_11_0);
5992 p += 4;
5993 write_insn<big_endian>(p, add_3_12_2);
5994 p += 4;
5995 write_insn<big_endian>(p, beqlr);
5996 p += 4;
5997 write_insn<big_endian>(p, mr_3_0);
5998 p += 4;
5999 write_insn<big_endian>(p, nop);
6000 p += 4;
6002 *pp = p;
6005 // Emit the tail of a __tls_get_addr_opt plt call stub.
6007 template<int size, bool big_endian>
6008 void
6009 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
6011 write_insn<big_endian>(p, bctrl);
6012 p += 4;
6013 write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
6014 p += 4;
6015 write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
6016 p += 4;
6017 write_insn<big_endian>(p, mtlr_11);
6018 p += 4;
6019 write_insn<big_endian>(p, blr);
6022 // Emit pc-relative plt call stub code.
6024 template<bool big_endian>
6025 static unsigned char*
6026 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
6028 uint64_t insn;
6029 if (off - odd + (1ULL << 33) < 1ULL << 34)
6031 off -= odd;
6032 if (odd)
6034 write_insn<big_endian>(p, nop);
6035 p += 4;
6037 if (load)
6038 insn = pld_12_pc;
6039 else
6040 insn = paddi_12_pc;
6041 insn |= d34(off);
6042 write_insn<big_endian>(p, insn >> 32);
6043 p += 4;
6044 write_insn<big_endian>(p, insn & 0xffffffff);
6046 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6048 off -= 8 - odd;
6049 write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
6050 p += 4;
6051 if (!odd)
6053 write_insn<big_endian>(p, sldi_11_11_34);
6054 p += 4;
6056 insn = paddi_12_pc | d34(off);
6057 write_insn<big_endian>(p, insn >> 32);
6058 p += 4;
6059 write_insn<big_endian>(p, insn & 0xffffffff);
6060 p += 4;
6061 if (odd)
6063 write_insn<big_endian>(p, sldi_11_11_34);
6064 p += 4;
6066 if (load)
6067 write_insn<big_endian>(p, ldx_12_11_12);
6068 else
6069 write_insn<big_endian>(p, add_12_11_12);
6071 else
6073 off -= odd + 8;
6074 write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
6075 p += 4;
6076 write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
6077 p += 4;
6078 if (odd)
6080 write_insn<big_endian>(p, sldi_11_11_34);
6081 p += 4;
6083 insn = paddi_12_pc | d34(off);
6084 write_insn<big_endian>(p, insn >> 32);
6085 p += 4;
6086 write_insn<big_endian>(p, insn & 0xffffffff);
6087 p += 4;
6088 if (!odd)
6090 write_insn<big_endian>(p, sldi_11_11_34);
6091 p += 4;
6093 if (load)
6094 write_insn<big_endian>(p, ldx_12_11_12);
6095 else
6096 write_insn<big_endian>(p, add_12_11_12);
6098 p += 4;
6099 return p;
6102 // Gets the address of a label (1:) in r11 and builds an offset in r12,
6103 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
6104 // mflr %r12
6105 // bcl 20,31,1f
6106 // 1: mflr %r11
6107 // mtlr %r12
6108 // lis %r12,xxx-1b@highest
6109 // ori %r12,%r12,xxx-1b@higher
6110 // sldi %r12,%r12,32
6111 // oris %r12,%r12,xxx-1b@high
6112 // ori %r12,%r12,xxx-1b@l
6113 // add/ldx %r12,%r11,%r12
6115 template<bool big_endian>
6116 static unsigned char*
6117 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
6119 write_insn<big_endian>(p, mflr_12);
6120 p += 4;
6121 write_insn<big_endian>(p, bcl_20_31);
6122 p += 4;
6123 write_insn<big_endian>(p, mflr_11);
6124 p += 4;
6125 write_insn<big_endian>(p, mtlr_12);
6126 p += 4;
6127 if (off + 0x8000 < 0x10000)
6129 if (load)
6130 write_insn<big_endian>(p, ld_12_11 + l(off));
6131 else
6132 write_insn<big_endian>(p, addi_12_11 + l(off));
6134 else if (off + 0x80008000ULL < 0x100000000ULL)
6136 write_insn<big_endian>(p, addis_12_11 + ha(off));
6137 p += 4;
6138 if (load)
6139 write_insn<big_endian>(p, ld_12_12 + l(off));
6140 else
6141 write_insn<big_endian>(p, addi_12_12 + l(off));
6143 else
6145 if (off + 0x800000000000ULL < 0x1000000000000ULL)
6147 write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6148 p += 4;
6150 else
6152 write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6153 p += 4;
6154 if (((off >> 32) & 0xffff) != 0)
6156 write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6157 p += 4;
6160 if (((off >> 32) & 0xffffffffULL) != 0)
6162 write_insn<big_endian>(p, sldi_12_12_32);
6163 p += 4;
6165 if (hi(off) != 0)
6167 write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6168 p += 4;
6170 if (l(off) != 0)
6172 write_insn<big_endian>(p, ori_12_12_0 + l(off));
6173 p += 4;
6175 if (load)
6176 write_insn<big_endian>(p, ldx_12_11_12);
6177 else
6178 write_insn<big_endian>(p, add_12_11_12);
6180 p += 4;
6181 return p;
6184 // Size of a given plt call stub.
6186 template<int size, bool big_endian>
6187 unsigned int
6188 Stub_table<size, big_endian>::plt_call_size(
6189 typename Plt_stub_entries::iterator p) const
6191 if (size == 32)
6193 unsigned int bytes = 4 * 4;
6194 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6195 bytes = 12 * 4;
6196 return this->plt_call_align(bytes);
6199 const Output_data_plt_powerpc<size, big_endian>* plt;
6200 uint64_t plt_addr = this->plt_off(p, &plt);
6201 plt_addr += plt->address();
6202 if (this->targ_->power10_stubs()
6203 && this->targ_->power10_stubs_auto())
6205 unsigned int bytes = 0;
6206 if (p->second.notoc_)
6208 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6209 bytes = 7 * 4;
6210 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6211 uint64_t odd = from & 4;
6212 uint64_t off = plt_addr - from;
6213 if (off - odd + (1ULL << 33) < 1ULL << 34)
6214 bytes += odd + 4 * 4;
6215 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6216 bytes += 7 * 4;
6217 else
6218 bytes += 8 * 4;
6219 bytes = this->plt_call_align(bytes);
6221 if (p->second.toc_)
6223 p->second.tocoff_ = bytes;
6224 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6226 bytes += 7 * 4;
6227 if (p->second.r2save_ && !p->second.localentry0_)
6228 bytes += 2 * 4 + 4 * 4;
6230 if (p->second.r2save_)
6231 bytes += 4;
6232 uint64_t got_addr = this->targ_->toc_pointer();
6233 uint64_t off = plt_addr - got_addr;
6234 bytes += 3 * 4 + 4 * (ha(off) != 0);
6235 p->second.tsize_ = bytes - p->second.tocoff_;
6236 bytes = this->plt_call_align(bytes);
6238 if (p->second.p9notoc_)
6240 p->second.p9off_ = bytes;
6241 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6242 bytes += 7 * 4;
6243 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6244 uint64_t off = plt_addr - from;
6245 if (off + 0x8000 < 0x10000)
6246 bytes += 7 * 4;
6247 else if (off + 0x80008000ULL < 0x100000000ULL)
6248 bytes += 8 * 4;
6249 else
6251 bytes += 8 * 4;
6252 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6253 && ((off >> 32) & 0xffff) != 0)
6254 bytes += 4;
6255 if (((off >> 32) & 0xffffffffULL) != 0)
6256 bytes += 4;
6257 if (hi(off) != 0)
6258 bytes += 4;
6259 if (l(off) != 0)
6260 bytes += 4;
6262 bytes = this->plt_call_align(bytes);
6264 return bytes;
6266 else
6268 unsigned int bytes = 0;
6269 unsigned int tail = 0;
6270 if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6272 bytes = 7 * 4;
6273 if (p->second.r2save_ && !p->second.localentry0_)
6275 bytes = 9 * 4;
6276 tail = 4 * 4;
6280 if (p->second.r2save_)
6281 bytes += 4;
6283 if (this->targ_->power10_stubs())
6285 uint64_t from = this->stub_address() + p->second.off_ + bytes;
6286 uint64_t odd = from & 4;
6287 uint64_t off = plt_addr - from;
6288 if (off - odd + (1ULL << 33) < 1ULL << 34)
6289 bytes += odd + 4 * 4;
6290 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6291 bytes += 7 * 4;
6292 else
6293 bytes += 8 * 4;
6294 return this->plt_call_align(bytes + tail);
6297 if (p->second.p9notoc_)
6299 uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6300 uint64_t off = plt_addr - from;
6301 if (off + 0x8000 < 0x10000)
6302 bytes += 7 * 4;
6303 else if (off + 0x80008000ULL < 0x100000000ULL)
6304 bytes += 8 * 4;
6305 else
6307 bytes += 8 * 4;
6308 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6309 && ((off >> 32) & 0xffff) != 0)
6310 bytes += 4;
6311 if (((off >> 32) & 0xffffffffULL) != 0)
6312 bytes += 4;
6313 if (hi(off) != 0)
6314 bytes += 4;
6315 if (l(off) != 0)
6316 bytes += 4;
6318 return this->plt_call_align(bytes + tail);
6321 uint64_t got_addr = this->targ_->toc_pointer();
6322 uint64_t off = plt_addr - got_addr;
6323 bytes += 3 * 4 + 4 * (ha(off) != 0);
6324 if (this->targ_->abiversion() < 2)
6326 bool static_chain = parameters->options().plt_static_chain();
6327 bool thread_safe = this->targ_->plt_thread_safe();
6328 bytes += (4
6329 + 4 * static_chain
6330 + 8 * thread_safe
6331 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6333 return this->plt_call_align(bytes + tail);
6337 // Return long branch stub size.
6339 template<int size, bool big_endian>
6340 unsigned int
6341 Stub_table<size, big_endian>::branch_stub_size(
6342 typename Branch_stub_entries::iterator p,
6343 bool* need_lt)
6345 Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6346 if (size == 32)
6348 if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6349 return 4;
6350 if (parameters->options().output_is_position_independent())
6351 return 32;
6352 return 16;
6355 uint64_t off = p->first.dest_ - loc;
6356 unsigned int bytes = 0;
6357 if (p->second.notoc_)
6359 if (this->targ_->power10_stubs())
6361 Address odd = loc & 4;
6362 if (off + (1 << 25) < 2 << 25)
6363 bytes = odd + 12;
6364 else if (off - odd + (1ULL << 33) < 1ULL << 34)
6365 bytes = odd + 16;
6366 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6367 bytes = 28;
6368 else
6369 bytes = 32;
6370 if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6371 return bytes;
6372 p->second.tocoff_ = bytes;
6374 else
6376 off -= 8;
6377 if (off + 0x8000 < 0x10000)
6378 return 24;
6379 if (off + 0x80008000ULL < 0x100000000ULL)
6381 if (off + 24 + (1 << 25) < 2 << 25)
6382 return 28;
6383 return 32;
6386 bytes = 32;
6387 if (off + 0x800000000000ULL >= 0x1000000000000ULL
6388 && ((off >> 32) & 0xffff) != 0)
6389 bytes += 4;
6390 if (((off >> 32) & 0xffffffffULL) != 0)
6391 bytes += 4;
6392 if (hi(off) != 0)
6393 bytes += 4;
6394 if (l(off) != 0)
6395 bytes += 4;
6396 return bytes;
6400 off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6401 if (off + (1 << 25) < 2 << 25)
6402 return bytes + 4;
6403 if (!this->targ_->power10_stubs()
6404 || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6405 *need_lt = true;
6406 return bytes + 16;
6409 template<int size, bool big_endian>
6410 void
6411 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6413 if (p.sym_)
6414 gold_error(_("linkage table error against `%s'"),
6415 p.sym_->demangled_name().c_str());
6416 else
6417 gold_error(_("linkage table error against `%s:[local %u]'"),
6418 p.object_->name().c_str(),
6419 p.locsym_);
6422 // Write out plt and long branch stub code.
6424 template<int size, bool big_endian>
6425 void
6426 Stub_table<size, big_endian>::do_write(Output_file* of)
6428 if (this->plt_call_stubs_.empty()
6429 && this->long_branch_stubs_.empty())
6430 return;
6432 const section_size_type start_off = this->offset();
6433 const section_size_type off = this->stub_offset();
6434 const section_size_type oview_size =
6435 convert_to_section_size_type(this->data_size() - (off - start_off));
6436 unsigned char* const oview = of->get_output_view(off, oview_size);
6437 unsigned char* p;
6439 if (size == 64
6440 && this->targ_->power10_stubs())
6442 if (!this->plt_call_stubs_.empty())
6444 // Write out plt call stubs.
6445 typename Plt_stub_entries::const_iterator cs;
6446 for (cs = this->plt_call_stubs_.begin();
6447 cs != this->plt_call_stubs_.end();
6448 ++cs)
6450 p = oview + cs->second.off_;
6451 const Output_data_plt_powerpc<size, big_endian>* plt;
6452 Address pltoff = this->plt_off(cs, &plt);
6453 Address plt_addr = pltoff + plt->address();
6454 if (this->targ_->power10_stubs_auto())
6456 if (cs->second.notoc_)
6458 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6459 this->build_tls_opt_head(&p, false);
6460 Address from = this->stub_address() + (p - oview);
6461 Address delta = plt_addr - from;
6462 p = build_power10_offset<big_endian>(p, delta, from & 4,
6463 true);
6464 write_insn<big_endian>(p, mtctr_12);
6465 p += 4;
6466 write_insn<big_endian>(p, bctr);
6467 p += 4;
6468 p = oview + this->plt_call_align(p - oview);
6470 if (cs->second.toc_)
6472 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6474 bool save_lr
6475 = cs->second.r2save_ && !cs->second.localentry0_;
6476 this->build_tls_opt_head(&p, save_lr);
6478 Address got_addr = this->targ_->toc_pointer();
6479 Address off = plt_addr - got_addr;
6481 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6482 this->plt_error(cs->first);
6484 if (cs->second.r2save_)
6486 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6487 p += 4;
6489 if (ha(off) != 0)
6491 write_insn<big_endian>(p, addis_12_2 + ha(off));
6492 p += 4;
6493 write_insn<big_endian>(p, ld_12_12 + l(off));
6494 p += 4;
6496 else
6498 write_insn<big_endian>(p, ld_12_2 + l(off));
6499 p += 4;
6501 write_insn<big_endian>(p, mtctr_12);
6502 p += 4;
6503 if (cs->second.r2save_
6504 && !cs->second.localentry0_
6505 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6506 this->build_tls_opt_tail(p);
6507 else
6508 write_insn<big_endian>(p, bctr);
6510 if (cs->second.p9notoc_)
6512 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6513 this->build_tls_opt_head(&p, false);
6514 Address from = this->stub_address() + (p - oview);
6515 Address delta = plt_addr - from;
6516 p = build_notoc_offset<big_endian>(p, delta, true);
6517 write_insn<big_endian>(p, mtctr_12);
6518 p += 4;
6519 write_insn<big_endian>(p, bctr);
6520 p += 4;
6521 p = oview + this->plt_call_align(p - oview);
6524 else
6526 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6528 bool save_lr
6529 = cs->second.r2save_ && !cs->second.localentry0_;
6530 this->build_tls_opt_head(&p, save_lr);
6532 if (cs->second.r2save_)
6534 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6535 p += 4;
6537 Address from = this->stub_address() + (p - oview);
6538 Address delta = plt_addr - from;
6539 p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6540 write_insn<big_endian>(p, mtctr_12);
6541 p += 4;
6542 if (cs->second.r2save_
6543 && !cs->second.localentry0_
6544 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6545 this->build_tls_opt_tail(p);
6546 else
6547 write_insn<big_endian>(p, bctr);
6552 // Write out long branch stubs.
6553 typename Branch_stub_entries::const_iterator bs;
6554 for (bs = this->long_branch_stubs_.begin();
6555 bs != this->long_branch_stubs_.end();
6556 ++bs)
6558 if (bs->second.save_res_)
6559 continue;
6560 Address off = this->plt_size_ + bs->second.off_;
6561 p = oview + off;
6562 Address loc = this->stub_address() + off;
6563 Address delta = bs->first.dest_ - loc;
6564 if (this->targ_->power10_stubs_auto())
6566 if (bs->second.notoc_)
6568 unsigned char* startp = p;
6569 p = build_power10_offset<big_endian>(p, delta,
6570 loc & 4, false);
6571 delta -= p - startp;
6572 startp = p;
6573 if (delta + (1 << 25) < 2 << 25)
6574 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6575 else
6577 write_insn<big_endian>(p, mtctr_12);
6578 p += 4;
6579 write_insn<big_endian>(p, bctr);
6581 p += 4;
6582 delta -= p - startp;
6584 if (bs->second.toc_)
6586 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6587 if (delta + (1 << 25) >= 2 << 25)
6589 Address brlt_addr
6590 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6591 gold_assert(brlt_addr != invalid_address);
6592 brlt_addr += this->targ_->brlt_section()->address();
6593 Address got_addr = this->targ_->toc_pointer();
6594 Address brltoff = brlt_addr - got_addr;
6595 if (ha(brltoff) == 0)
6597 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6598 p += 4;
6600 else
6602 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6603 p += 4;
6604 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6605 p += 4;
6608 if (delta + (1 << 25) < 2 << 25)
6609 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6610 else
6612 write_insn<big_endian>(p, mtctr_12);
6613 p += 4;
6614 write_insn<big_endian>(p, bctr);
6617 if (bs->second.p9notoc_)
6619 unsigned char* startp = p;
6620 p = build_notoc_offset<big_endian>(p, delta, false);
6621 delta -= p - startp;
6622 startp = p;
6623 if (delta + (1 << 25) < 2 << 25)
6624 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6625 else
6627 write_insn<big_endian>(p, mtctr_12);
6628 p += 4;
6629 write_insn<big_endian>(p, bctr);
6631 p += 4;
6632 delta -= p - startp;
6635 else
6637 if (!bs->second.notoc_)
6638 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6639 if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6641 unsigned char* startp = p;
6642 p = build_power10_offset<big_endian>(p, delta,
6643 loc & 4, false);
6644 delta -= p - startp;
6646 if (delta + (1 << 25) < 2 << 25)
6647 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6648 else
6650 write_insn<big_endian>(p, mtctr_12);
6651 p += 4;
6652 write_insn<big_endian>(p, bctr);
6657 else if (size == 64)
6660 if (!this->plt_call_stubs_.empty()
6661 && this->targ_->abiversion() >= 2)
6663 // Write out plt call stubs for ELFv2.
6664 typename Plt_stub_entries::const_iterator cs;
6665 for (cs = this->plt_call_stubs_.begin();
6666 cs != this->plt_call_stubs_.end();
6667 ++cs)
6669 const Output_data_plt_powerpc<size, big_endian>* plt;
6670 Address pltoff = this->plt_off(cs, &plt);
6671 Address plt_addr = pltoff + plt->address();
6673 p = oview + cs->second.off_;
6674 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6676 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6677 this->build_tls_opt_head(&p, save_lr);
6679 if (cs->second.r2save_)
6681 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6682 p += 4;
6684 if (cs->second.p9notoc_)
6686 Address from = this->stub_address() + (p - oview) + 8;
6687 Address off = plt_addr - from;
6688 p = build_notoc_offset<big_endian>(p, off, true);
6690 else
6692 Address got_addr = this->targ_->toc_pointer();
6693 Address off = plt_addr - got_addr;
6695 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6696 this->plt_error(cs->first);
6698 if (ha(off) != 0)
6700 write_insn<big_endian>(p, addis_12_2 + ha(off));
6701 p += 4;
6702 write_insn<big_endian>(p, ld_12_12 + l(off));
6703 p += 4;
6705 else
6707 write_insn<big_endian>(p, ld_12_2 + l(off));
6708 p += 4;
6711 write_insn<big_endian>(p, mtctr_12);
6712 p += 4;
6713 if (cs->second.r2save_
6714 && !cs->second.localentry0_
6715 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6716 this->build_tls_opt_tail(p);
6717 else
6718 write_insn<big_endian>(p, bctr);
6721 else if (!this->plt_call_stubs_.empty())
6723 // Write out plt call stubs for ELFv1.
6724 typename Plt_stub_entries::const_iterator cs;
6725 for (cs = this->plt_call_stubs_.begin();
6726 cs != this->plt_call_stubs_.end();
6727 ++cs)
6729 const Output_data_plt_powerpc<size, big_endian>* plt;
6730 Address pltoff = this->plt_off(cs, &plt);
6731 Address plt_addr = pltoff + plt->address();
6732 Address got_addr = this->targ_->toc_pointer();
6733 Address off = plt_addr - got_addr;
6735 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6736 || cs->second.notoc_)
6737 this->plt_error(cs->first);
6739 bool static_chain = parameters->options().plt_static_chain();
6740 bool thread_safe = this->targ_->plt_thread_safe();
6741 bool use_fake_dep = false;
6742 Address cmp_branch_off = 0;
6743 if (thread_safe)
6745 unsigned int pltindex
6746 = ((pltoff - this->targ_->first_plt_entry_offset())
6747 / this->targ_->plt_entry_size());
6748 Address glinkoff
6749 = (this->targ_->glink_section()->pltresolve_size()
6750 + pltindex * 8);
6751 if (pltindex > 32768)
6752 glinkoff += (pltindex - 32768) * 4;
6753 Address to
6754 = this->targ_->glink_section()->address() + glinkoff;
6755 Address from
6756 = (this->stub_address() + cs->second.off_ + 20
6757 + 4 * cs->second.r2save_
6758 + 4 * (ha(off) != 0)
6759 + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6760 + 4 * static_chain);
6761 cmp_branch_off = to - from;
6762 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6765 p = oview + cs->second.off_;
6766 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6768 bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6769 this->build_tls_opt_head(&p, save_lr);
6770 use_fake_dep = thread_safe;
6772 if (cs->second.r2save_)
6774 write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6775 p += 4;
6777 if (ha(off) != 0)
6779 write_insn<big_endian>(p, addis_11_2 + ha(off));
6780 p += 4;
6781 write_insn<big_endian>(p, ld_12_11 + l(off));
6782 p += 4;
6783 if (ha(off + 8 + 8 * static_chain) != ha(off))
6785 write_insn<big_endian>(p, addi_11_11 + l(off));
6786 p += 4;
6787 off = 0;
6789 write_insn<big_endian>(p, mtctr_12);
6790 p += 4;
6791 if (use_fake_dep)
6793 write_insn<big_endian>(p, xor_2_12_12);
6794 p += 4;
6795 write_insn<big_endian>(p, add_11_11_2);
6796 p += 4;
6798 write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6799 p += 4;
6800 if (static_chain)
6802 write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6803 p += 4;
6806 else
6808 write_insn<big_endian>(p, ld_12_2 + l(off));
6809 p += 4;
6810 if (ha(off + 8 + 8 * static_chain) != ha(off))
6812 write_insn<big_endian>(p, addi_2_2 + l(off));
6813 p += 4;
6814 off = 0;
6816 write_insn<big_endian>(p, mtctr_12);
6817 p += 4;
6818 if (use_fake_dep)
6820 write_insn<big_endian>(p, xor_11_12_12);
6821 p += 4;
6822 write_insn<big_endian>(p, add_2_2_11);
6823 p += 4;
6825 if (static_chain)
6827 write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6828 p += 4;
6830 write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6831 p += 4;
6833 if (cs->second.r2save_
6834 && !cs->second.localentry0_
6835 && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6836 this->build_tls_opt_tail(p);
6837 else if (thread_safe && !use_fake_dep)
6839 write_insn<big_endian>(p, cmpldi_2_0);
6840 p += 4;
6841 write_insn<big_endian>(p, bnectr_p4);
6842 p += 4;
6843 write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6845 else
6846 write_insn<big_endian>(p, bctr);
6850 // Write out long branch stubs.
6851 typename Branch_stub_entries::const_iterator bs;
6852 for (bs = this->long_branch_stubs_.begin();
6853 bs != this->long_branch_stubs_.end();
6854 ++bs)
6856 if (bs->second.save_res_)
6857 continue;
6858 Address off = this->plt_size_ + bs->second.off_;
6859 p = oview + off;
6860 Address loc = this->stub_address() + off;
6861 Address delta = bs->first.dest_ - loc;
6862 if (!bs->second.p9notoc_)
6863 delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6864 if (bs->second.p9notoc_)
6866 unsigned char* startp = p;
6867 p = build_notoc_offset<big_endian>(p, off, false);
6868 delta -= p - startp;
6870 else if (delta + (1 << 25) >= 2 << 25)
6872 Address brlt_addr
6873 = this->targ_->find_branch_lookup_table(bs->first.dest_);
6874 gold_assert(brlt_addr != invalid_address);
6875 brlt_addr += this->targ_->brlt_section()->address();
6876 Address got_addr = this->targ_->toc_pointer();
6877 Address brltoff = brlt_addr - got_addr;
6878 if (ha(brltoff) == 0)
6880 write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6881 p += 4;
6883 else
6885 write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6886 p += 4;
6887 write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6888 p += 4;
6891 if (delta + (1 << 25) < 2 << 25)
6892 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6893 else
6895 write_insn<big_endian>(p, mtctr_12);
6896 p += 4;
6897 write_insn<big_endian>(p, bctr);
6901 else // size == 32
6903 if (!this->plt_call_stubs_.empty())
6905 // The address of _GLOBAL_OFFSET_TABLE_.
6906 Address g_o_t = invalid_address;
6908 // Write out plt call stubs.
6909 typename Plt_stub_entries::const_iterator cs;
6910 for (cs = this->plt_call_stubs_.begin();
6911 cs != this->plt_call_stubs_.end();
6912 ++cs)
6914 const Output_data_plt_powerpc<size, big_endian>* plt;
6915 Address plt_addr = this->plt_off(cs, &plt);
6916 plt_addr += plt->address();
6918 p = oview + cs->second.off_;
6919 if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6920 this->build_tls_opt_head(&p, false);
6921 if (parameters->options().output_is_position_independent())
6923 Address got_addr;
6924 const Powerpc_relobj<size, big_endian>* ppcobj
6925 = (static_cast<const Powerpc_relobj<size, big_endian>*>
6926 (cs->first.object_));
6927 if (ppcobj != NULL && cs->first.addend_ >= 32768)
6929 unsigned int got2 = ppcobj->got2_shndx();
6930 got_addr = ppcobj->get_output_section_offset(got2);
6931 gold_assert(got_addr != invalid_address);
6932 got_addr += (ppcobj->output_section(got2)->address()
6933 + cs->first.addend_);
6935 else
6937 if (g_o_t == invalid_address)
6938 g_o_t = this->targ_->toc_pointer();
6939 got_addr = g_o_t;
6942 Address off = plt_addr - got_addr;
6943 if (ha(off) == 0)
6944 write_insn<big_endian>(p, lwz_11_30 + l(off));
6945 else
6947 write_insn<big_endian>(p, addis_11_30 + ha(off));
6948 p += 4;
6949 write_insn<big_endian>(p, lwz_11_11 + l(off));
6952 else
6954 write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6955 p += 4;
6956 write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6958 p += 4;
6959 write_insn<big_endian>(p, mtctr_11);
6960 p += 4;
6961 write_insn<big_endian>(p, bctr);
6965 // Write out long branch stubs.
6966 typename Branch_stub_entries::const_iterator bs;
6967 for (bs = this->long_branch_stubs_.begin();
6968 bs != this->long_branch_stubs_.end();
6969 ++bs)
6971 if (bs->second.save_res_)
6972 continue;
6973 Address off = this->plt_size_ + bs->second.off_;
6974 p = oview + off;
6975 Address loc = this->stub_address() + off;
6976 Address delta = bs->first.dest_ - loc;
6977 if (delta + (1 << 25) < 2 << 25)
6978 write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6979 else if (!parameters->options().output_is_position_independent())
6981 write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6982 p += 4;
6983 write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6985 else
6987 delta -= 8;
6988 write_insn<big_endian>(p, mflr_0);
6989 p += 4;
6990 write_insn<big_endian>(p, bcl_20_31);
6991 p += 4;
6992 write_insn<big_endian>(p, mflr_12);
6993 p += 4;
6994 write_insn<big_endian>(p, addis_12_12 + ha(delta));
6995 p += 4;
6996 write_insn<big_endian>(p, addi_12_12 + l(delta));
6997 p += 4;
6998 write_insn<big_endian>(p, mtlr_0);
7000 p += 4;
7001 write_insn<big_endian>(p, mtctr_12);
7002 p += 4;
7003 write_insn<big_endian>(p, bctr);
7006 if (this->need_save_res_)
7008 p = oview + this->plt_size_ + this->branch_size_;
7009 memcpy (p, this->targ_->savres_section()->contents(),
7010 this->targ_->savres_section()->data_size());
7014 // Write out .glink.
7016 template<int size, bool big_endian>
7017 void
7018 Output_data_glink<size, big_endian>::do_write(Output_file* of)
7020 const section_size_type off = this->offset();
7021 const section_size_type oview_size =
7022 convert_to_section_size_type(this->data_size());
7023 unsigned char* const oview = of->get_output_view(off, oview_size);
7024 unsigned char* p;
7026 // The base address of the .plt section.
7027 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
7028 Address plt_base = this->targ_->plt_section()->address();
7030 if (size == 64)
7032 if (this->end_branch_table_ != 0)
7034 // Write pltresolve stub.
7035 p = oview;
7036 Address after_bcl = this->address() + 16;
7037 Address pltoff = plt_base - after_bcl;
7039 elfcpp::Swap<64, big_endian>::writeval(p, pltoff), p += 8;
7041 if (this->targ_->abiversion() < 2)
7043 write_insn<big_endian>(p, mflr_12), p += 4;
7044 write_insn<big_endian>(p, bcl_20_31), p += 4;
7045 write_insn<big_endian>(p, mflr_11), p += 4;
7046 write_insn<big_endian>(p, ld_2_11 + l(-16)), p += 4;
7047 write_insn<big_endian>(p, mtlr_12), p += 4;
7048 write_insn<big_endian>(p, add_11_2_11), p += 4;
7049 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7050 write_insn<big_endian>(p, ld_2_11 + 8), p += 4;
7051 write_insn<big_endian>(p, mtctr_12), p += 4;
7052 write_insn<big_endian>(p, ld_11_11 + 16), p += 4;
7054 else
7056 if (this->targ_->has_localentry0())
7058 write_insn<big_endian>(p, std_2_1 + 24), p += 4;
7060 write_insn<big_endian>(p, mflr_0), p += 4;
7061 write_insn<big_endian>(p, bcl_20_31), p += 4;
7062 write_insn<big_endian>(p, mflr_11), p += 4;
7063 write_insn<big_endian>(p, mtlr_0), p += 4;
7064 if (this->targ_->has_localentry0())
7066 write_insn<big_endian>(p, ld_0_11 + l(-20)), p += 4;
7068 else
7070 write_insn<big_endian>(p, ld_0_11 + l(-16)), p += 4;
7072 write_insn<big_endian>(p, sub_12_12_11), p += 4;
7073 write_insn<big_endian>(p, add_11_0_11), p += 4;
7074 write_insn<big_endian>(p, addi_0_12 + l(-44)), p += 4;
7075 write_insn<big_endian>(p, ld_12_11 + 0), p += 4;
7076 write_insn<big_endian>(p, srdi_0_0_2), p += 4;
7077 write_insn<big_endian>(p, mtctr_12), p += 4;
7078 write_insn<big_endian>(p, ld_11_11 + 8), p += 4;
7080 write_insn<big_endian>(p, bctr), p += 4;
7081 gold_assert(p == oview + this->pltresolve_size());
7083 // Write lazy link call stubs.
7084 uint32_t indx = 0;
7085 while (p < oview + this->end_branch_table_)
7087 if (this->targ_->abiversion() < 2)
7089 if (indx < 0x8000)
7091 write_insn<big_endian>(p, li_0_0 + indx), p += 4;
7093 else
7095 write_insn<big_endian>(p, lis_0 + hi(indx)), p += 4;
7096 write_insn<big_endian>(p, ori_0_0_0 + l(indx)), p += 4;
7099 uint32_t branch_off = 8 - (p - oview);
7100 write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)), p += 4;
7101 indx++;
7105 Address plt_base = this->targ_->plt_section()->address();
7106 Address iplt_base = invalid_address;
7107 unsigned int global_entry_off = this->global_entry_off();
7108 Address global_entry_base = this->address() + global_entry_off;
7109 typename Global_entry_stub_entries::const_iterator ge;
7110 for (ge = this->global_entry_stubs_.begin();
7111 ge != this->global_entry_stubs_.end();
7112 ++ge)
7114 p = oview + global_entry_off + ge->second;
7115 Address plt_addr = ge->first->plt_offset();
7116 if (ge->first->type() == elfcpp::STT_GNU_IFUNC
7117 && ge->first->can_use_relative_reloc(false))
7119 if (iplt_base == invalid_address)
7120 iplt_base = this->targ_->iplt_section()->address();
7121 plt_addr += iplt_base;
7123 else
7124 plt_addr += plt_base;
7125 Address my_addr = global_entry_base + ge->second;
7126 Address off = plt_addr - my_addr;
7128 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
7129 gold_error(_("linkage table error against `%s'"),
7130 ge->first->demangled_name().c_str());
7132 write_insn<big_endian>(p, addis_12_12 + ha(off)), p += 4;
7133 write_insn<big_endian>(p, ld_12_12 + l(off)), p += 4;
7134 write_insn<big_endian>(p, mtctr_12), p += 4;
7135 write_insn<big_endian>(p, bctr);
7138 else
7140 // The address of _GLOBAL_OFFSET_TABLE_.
7141 Address g_o_t = this->targ_->toc_pointer();
7143 // Write out pltresolve branch table.
7144 p = oview;
7145 unsigned int the_end = oview_size - this->pltresolve_size();
7146 unsigned char* end_p = oview + the_end;
7147 while (p < end_p - 8 * 4)
7148 write_insn<big_endian>(p, b + end_p - p), p += 4;
7149 while (p < end_p)
7150 write_insn<big_endian>(p, nop), p += 4;
7152 // Write out pltresolve call stub.
7153 end_p = oview + oview_size;
7154 if (parameters->options().output_is_position_independent())
7156 Address res0_off = 0;
7157 Address after_bcl_off = the_end + 12;
7158 Address bcl_res0 = after_bcl_off - res0_off;
7160 write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
7161 p += 4;
7162 write_insn<big_endian>(p, mflr_0);
7163 p += 4;
7164 write_insn<big_endian>(p, bcl_20_31);
7165 p += 4;
7166 write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7167 p += 4;
7168 write_insn<big_endian>(p, mflr_12);
7169 p += 4;
7170 write_insn<big_endian>(p, mtlr_0);
7171 p += 4;
7172 write_insn<big_endian>(p, sub_11_11_12);
7173 p += 4;
7175 Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
7177 write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7178 p += 4;
7179 if (ha(got_bcl) == ha(got_bcl + 4))
7181 write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7182 p += 4;
7183 write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
7185 else
7187 write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7188 p += 4;
7189 write_insn<big_endian>(p, lwz_12_12 + 4);
7191 p += 4;
7192 write_insn<big_endian>(p, mtctr_0);
7193 p += 4;
7194 write_insn<big_endian>(p, add_0_11_11);
7195 p += 4;
7196 write_insn<big_endian>(p, add_11_0_11);
7198 else
7200 Address res0 = this->address();
7202 write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7203 p += 4;
7204 write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7205 p += 4;
7206 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7207 write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7208 else
7209 write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7210 p += 4;
7211 write_insn<big_endian>(p, addi_11_11 + l(-res0));
7212 p += 4;
7213 write_insn<big_endian>(p, mtctr_0);
7214 p += 4;
7215 write_insn<big_endian>(p, add_0_11_11);
7216 p += 4;
7217 if (ha(g_o_t + 4) == ha(g_o_t + 8))
7218 write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7219 else
7220 write_insn<big_endian>(p, lwz_12_12 + 4);
7221 p += 4;
7222 write_insn<big_endian>(p, add_11_0_11);
7224 p += 4;
7225 write_insn<big_endian>(p, bctr);
7226 p += 4;
7227 while (p < end_p)
7229 write_insn<big_endian>(p, nop);
7230 p += 4;
7234 of->write_output_view(off, oview_size, oview);
7238 // A class to handle linker generated save/restore functions.
7240 template<int size, bool big_endian>
7241 class Output_data_save_res : public Output_section_data_build
7243 public:
7244 Output_data_save_res(Symbol_table* symtab);
7246 const unsigned char*
7247 contents() const
7249 return contents_;
7252 protected:
7253 // Write to a map file.
7254 void
7255 do_print_to_mapfile(Mapfile* mapfile) const
7256 { mapfile->print_output_data(this, _("** save/restore")); }
7258 void
7259 do_write(Output_file*);
7261 private:
7262 // The maximum size of save/restore contents.
7263 static const unsigned int savres_max = 218*4;
7265 void
7266 savres_define(Symbol_table* symtab,
7267 const char *name,
7268 unsigned int lo, unsigned int hi,
7269 unsigned char* write_ent(unsigned char*, int),
7270 unsigned char* write_tail(unsigned char*, int));
7272 unsigned char *contents_;
7275 template<bool big_endian>
7276 static unsigned char*
7277 savegpr0(unsigned char* p, int r)
7279 uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7280 write_insn<big_endian>(p, insn);
7281 return p + 4;
7284 template<bool big_endian>
7285 static unsigned char*
7286 savegpr0_tail(unsigned char* p, int r)
7288 p = savegpr0<big_endian>(p, r);
7289 uint32_t insn = std_0_1 + 16;
7290 write_insn<big_endian>(p, insn);
7291 p = p + 4;
7292 write_insn<big_endian>(p, blr);
7293 return p + 4;
7296 template<bool big_endian>
7297 static unsigned char*
7298 restgpr0(unsigned char* p, int r)
7300 uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7301 write_insn<big_endian>(p, insn);
7302 return p + 4;
7305 template<bool big_endian>
7306 static unsigned char*
7307 restgpr0_tail(unsigned char* p, int r)
7309 uint32_t insn = ld_0_1 + 16;
7310 write_insn<big_endian>(p, insn);
7311 p = p + 4;
7312 p = restgpr0<big_endian>(p, r);
7313 write_insn<big_endian>(p, mtlr_0);
7314 p = p + 4;
7315 if (r == 29)
7317 p = restgpr0<big_endian>(p, 30);
7318 p = restgpr0<big_endian>(p, 31);
7320 write_insn<big_endian>(p, blr);
7321 return p + 4;
7324 template<bool big_endian>
7325 static unsigned char*
7326 savegpr1(unsigned char* p, int r)
7328 uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7329 write_insn<big_endian>(p, insn);
7330 return p + 4;
7333 template<bool big_endian>
7334 static unsigned char*
7335 savegpr1_tail(unsigned char* p, int r)
7337 p = savegpr1<big_endian>(p, r);
7338 write_insn<big_endian>(p, blr);
7339 return p + 4;
7342 template<bool big_endian>
7343 static unsigned char*
7344 restgpr1(unsigned char* p, int r)
7346 uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7347 write_insn<big_endian>(p, insn);
7348 return p + 4;
7351 template<bool big_endian>
7352 static unsigned char*
7353 restgpr1_tail(unsigned char* p, int r)
7355 p = restgpr1<big_endian>(p, r);
7356 write_insn<big_endian>(p, blr);
7357 return p + 4;
7360 template<bool big_endian>
7361 static unsigned char*
7362 savefpr(unsigned char* p, int r)
7364 uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7365 write_insn<big_endian>(p, insn);
7366 return p + 4;
7369 template<bool big_endian>
7370 static unsigned char*
7371 savefpr0_tail(unsigned char* p, int r)
7373 p = savefpr<big_endian>(p, r);
7374 write_insn<big_endian>(p, std_0_1 + 16);
7375 p = p + 4;
7376 write_insn<big_endian>(p, blr);
7377 return p + 4;
7380 template<bool big_endian>
7381 static unsigned char*
7382 restfpr(unsigned char* p, int r)
7384 uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7385 write_insn<big_endian>(p, insn);
7386 return p + 4;
7389 template<bool big_endian>
7390 static unsigned char*
7391 restfpr0_tail(unsigned char* p, int r)
7393 write_insn<big_endian>(p, ld_0_1 + 16);
7394 p = p + 4;
7395 p = restfpr<big_endian>(p, r);
7396 write_insn<big_endian>(p, mtlr_0);
7397 p = p + 4;
7398 if (r == 29)
7400 p = restfpr<big_endian>(p, 30);
7401 p = restfpr<big_endian>(p, 31);
7403 write_insn<big_endian>(p, blr);
7404 return p + 4;
7407 template<bool big_endian>
7408 static unsigned char*
7409 savefpr1_tail(unsigned char* p, int r)
7411 p = savefpr<big_endian>(p, r);
7412 write_insn<big_endian>(p, blr);
7413 return p + 4;
7416 template<bool big_endian>
7417 static unsigned char*
7418 restfpr1_tail(unsigned char* p, int r)
7420 p = restfpr<big_endian>(p, r);
7421 write_insn<big_endian>(p, blr);
7422 return p + 4;
7425 template<bool big_endian>
7426 static unsigned char*
7427 savevr(unsigned char* p, int r)
7429 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7430 write_insn<big_endian>(p, insn);
7431 p = p + 4;
7432 insn = stvx_0_12_0 + (r << 21);
7433 write_insn<big_endian>(p, insn);
7434 return p + 4;
7437 template<bool big_endian>
7438 static unsigned char*
7439 savevr_tail(unsigned char* p, int r)
7441 p = savevr<big_endian>(p, r);
7442 write_insn<big_endian>(p, blr);
7443 return p + 4;
7446 template<bool big_endian>
7447 static unsigned char*
7448 restvr(unsigned char* p, int r)
7450 uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7451 write_insn<big_endian>(p, insn);
7452 p = p + 4;
7453 insn = lvx_0_12_0 + (r << 21);
7454 write_insn<big_endian>(p, insn);
7455 return p + 4;
7458 template<bool big_endian>
7459 static unsigned char*
7460 restvr_tail(unsigned char* p, int r)
7462 p = restvr<big_endian>(p, r);
7463 write_insn<big_endian>(p, blr);
7464 return p + 4;
7468 template<int size, bool big_endian>
7469 Output_data_save_res<size, big_endian>::Output_data_save_res(
7470 Symbol_table* symtab)
7471 : Output_section_data_build(4),
7472 contents_(NULL)
7474 this->savres_define(symtab,
7475 "_savegpr0_", 14, 31,
7476 savegpr0<big_endian>, savegpr0_tail<big_endian>);
7477 this->savres_define(symtab,
7478 "_restgpr0_", 14, 29,
7479 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7480 this->savres_define(symtab,
7481 "_restgpr0_", 30, 31,
7482 restgpr0<big_endian>, restgpr0_tail<big_endian>);
7483 this->savres_define(symtab,
7484 "_savegpr1_", 14, 31,
7485 savegpr1<big_endian>, savegpr1_tail<big_endian>);
7486 this->savres_define(symtab,
7487 "_restgpr1_", 14, 31,
7488 restgpr1<big_endian>, restgpr1_tail<big_endian>);
7489 this->savres_define(symtab,
7490 "_savefpr_", 14, 31,
7491 savefpr<big_endian>, savefpr0_tail<big_endian>);
7492 this->savres_define(symtab,
7493 "_restfpr_", 14, 29,
7494 restfpr<big_endian>, restfpr0_tail<big_endian>);
7495 this->savres_define(symtab,
7496 "_restfpr_", 30, 31,
7497 restfpr<big_endian>, restfpr0_tail<big_endian>);
7498 this->savres_define(symtab,
7499 "._savef", 14, 31,
7500 savefpr<big_endian>, savefpr1_tail<big_endian>);
7501 this->savres_define(symtab,
7502 "._restf", 14, 31,
7503 restfpr<big_endian>, restfpr1_tail<big_endian>);
7504 this->savres_define(symtab,
7505 "_savevr_", 20, 31,
7506 savevr<big_endian>, savevr_tail<big_endian>);
7507 this->savres_define(symtab,
7508 "_restvr_", 20, 31,
7509 restvr<big_endian>, restvr_tail<big_endian>);
7512 template<int size, bool big_endian>
7513 void
7514 Output_data_save_res<size, big_endian>::savres_define(
7515 Symbol_table* symtab,
7516 const char *name,
7517 unsigned int lo, unsigned int hi,
7518 unsigned char* write_ent(unsigned char*, int),
7519 unsigned char* write_tail(unsigned char*, int))
7521 size_t len = strlen(name);
7522 bool writing = false;
7523 char sym[16];
7525 memcpy(sym, name, len);
7526 sym[len + 2] = 0;
7528 for (unsigned int i = lo; i <= hi; i++)
7530 sym[len + 0] = i / 10 + '0';
7531 sym[len + 1] = i % 10 + '0';
7532 Symbol* gsym = symtab->lookup(sym);
7533 bool refd = gsym != NULL && gsym->is_undefined();
7534 writing = writing || refd;
7535 if (writing)
7537 if (this->contents_ == NULL)
7538 this->contents_ = new unsigned char[this->savres_max];
7540 section_size_type value = this->current_data_size();
7541 unsigned char* p = this->contents_ + value;
7542 if (i != hi)
7543 p = write_ent(p, i);
7544 else
7545 p = write_tail(p, i);
7546 section_size_type cur_size = p - this->contents_;
7547 this->set_current_data_size(cur_size);
7548 if (refd)
7549 symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7550 this, value, cur_size - value,
7551 elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7552 elfcpp::STV_HIDDEN, 0, false, false);
7557 // Write out save/restore.
7559 template<int size, bool big_endian>
7560 void
7561 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7563 const section_size_type off = this->offset();
7564 const section_size_type oview_size =
7565 convert_to_section_size_type(this->data_size());
7566 unsigned char* const oview = of->get_output_view(off, oview_size);
7567 memcpy(oview, this->contents_, oview_size);
7568 of->write_output_view(off, oview_size, oview);
7572 // Create the glink section.
7574 template<int size, bool big_endian>
7575 void
7576 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7578 if (this->glink_ == NULL)
7580 this->glink_ = new Output_data_glink<size, big_endian>(this);
7581 this->glink_->add_eh_frame(layout);
7582 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7583 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7584 this->glink_, ORDER_TEXT, false);
7588 // Create a PLT entry for a global symbol.
7590 template<int size, bool big_endian>
7591 void
7592 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7593 Layout* layout,
7594 Symbol* gsym)
7596 if (gsym->type() == elfcpp::STT_GNU_IFUNC
7597 && gsym->can_use_relative_reloc(false))
7599 if (this->iplt_ == NULL)
7600 this->make_iplt_section(symtab, layout);
7601 this->iplt_->add_ifunc_entry(gsym);
7603 else
7605 if (this->plt_ == NULL)
7606 this->make_plt_section(symtab, layout);
7607 this->plt_->add_entry(gsym);
7611 // Make a PLT entry for a local symbol.
7613 template<int size, bool big_endian>
7614 void
7615 Target_powerpc<size, big_endian>::make_local_plt_entry(
7616 Symbol_table* symtab,
7617 Layout* layout,
7618 Sized_relobj_file<size, big_endian>* relobj,
7619 unsigned int r_sym)
7621 if (this->lplt_ == NULL)
7622 this->make_lplt_section(symtab, layout);
7623 this->lplt_->add_local_entry(relobj, r_sym);
7626 template<int size, bool big_endian>
7627 void
7628 Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
7629 Layout* layout,
7630 Symbol* gsym)
7632 if (this->lplt_ == NULL)
7633 this->make_lplt_section(symtab, layout);
7634 this->lplt_->add_entry(gsym, true);
7637 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7639 template<int size, bool big_endian>
7640 void
7641 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7642 Symbol_table* symtab,
7643 Layout* layout,
7644 Sized_relobj_file<size, big_endian>* relobj,
7645 unsigned int r_sym)
7647 if (this->iplt_ == NULL)
7648 this->make_iplt_section(symtab, layout);
7649 this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7652 // Return the number of entries in the PLT.
7654 template<int size, bool big_endian>
7655 unsigned int
7656 Target_powerpc<size, big_endian>::plt_entry_count() const
7658 if (this->plt_ == NULL)
7659 return 0;
7660 return this->plt_->entry_count();
7663 // Create a GOT entry for local dynamic __tls_get_addr calls.
7665 template<int size, bool big_endian>
7666 unsigned int
7667 Target_powerpc<size, big_endian>::tlsld_got_offset(
7668 Symbol_table* symtab,
7669 Layout* layout,
7670 Sized_relobj_file<size, big_endian>* object)
7672 if (this->tlsld_got_offset_ == -1U)
7674 gold_assert(symtab != NULL && layout != NULL && object != NULL);
7675 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7676 Output_data_got_powerpc<size, big_endian>* got
7677 = this->got_section(symtab, layout, GOT_TYPE_SMALL);
7678 unsigned int got_offset = got->add_constant_pair(0, 0);
7679 rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7680 got_offset, 0);
7681 this->tlsld_got_offset_ = got_offset;
7683 return this->tlsld_got_offset_;
7686 // Get the Reference_flags for a particular relocation.
7688 template<int size, bool big_endian>
7690 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7691 unsigned int r_type,
7692 const Target_powerpc* target)
7694 int ref = 0;
7696 switch (r_type)
7698 case elfcpp::R_PPC64_TOC:
7699 if (size != 64)
7700 break;
7701 // Fall through.
7702 case elfcpp::R_POWERPC_NONE:
7703 case elfcpp::R_POWERPC_GNU_VTINHERIT:
7704 case elfcpp::R_POWERPC_GNU_VTENTRY:
7705 // No symbol reference.
7706 break;
7708 case elfcpp::R_PPC64_ADDR64:
7709 case elfcpp::R_PPC64_UADDR64:
7710 case elfcpp::R_PPC64_ADDR16_HIGHER34:
7711 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7712 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7713 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7714 case elfcpp::R_PPC64_D34:
7715 case elfcpp::R_PPC64_D34_LO:
7716 case elfcpp::R_PPC64_D34_HI30:
7717 case elfcpp::R_PPC64_D34_HA30:
7718 case elfcpp::R_PPC64_D28:
7719 if (size != 64)
7720 break;
7721 // Fall through.
7722 case elfcpp::R_POWERPC_ADDR32:
7723 case elfcpp::R_POWERPC_UADDR32:
7724 case elfcpp::R_POWERPC_ADDR16:
7725 case elfcpp::R_POWERPC_UADDR16:
7726 case elfcpp::R_POWERPC_ADDR16_LO:
7727 case elfcpp::R_POWERPC_ADDR16_HI:
7728 case elfcpp::R_POWERPC_ADDR16_HA:
7729 ref = Symbol::ABSOLUTE_REF;
7730 break;
7732 case elfcpp::R_POWERPC_ADDR24:
7733 case elfcpp::R_POWERPC_ADDR14:
7734 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7735 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7736 ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7737 break;
7739 case elfcpp::R_PPC_LOCAL24PC:
7740 if (size != 32)
7741 break;
7742 // Fall through.
7743 ref = Symbol::RELATIVE_REF;
7744 break;
7746 case elfcpp::R_PPC64_REL64:
7747 case elfcpp::R_PPC64_REL16_HIGH:
7748 case elfcpp::R_PPC64_REL16_HIGHA:
7749 case elfcpp::R_PPC64_REL16_HIGHER:
7750 case elfcpp::R_PPC64_REL16_HIGHERA:
7751 case elfcpp::R_PPC64_REL16_HIGHEST:
7752 case elfcpp::R_PPC64_REL16_HIGHESTA:
7753 case elfcpp::R_PPC64_PCREL34:
7754 case elfcpp::R_PPC64_REL16_HIGHER34:
7755 case elfcpp::R_PPC64_REL16_HIGHERA34:
7756 case elfcpp::R_PPC64_REL16_HIGHEST34:
7757 case elfcpp::R_PPC64_REL16_HIGHESTA34:
7758 case elfcpp::R_PPC64_PCREL28:
7759 if (size != 64)
7760 break;
7761 // Fall through.
7762 case elfcpp::R_POWERPC_REL32:
7763 case elfcpp::R_POWERPC_REL16:
7764 case elfcpp::R_POWERPC_REL16_LO:
7765 case elfcpp::R_POWERPC_REL16_HI:
7766 case elfcpp::R_POWERPC_REL16_HA:
7767 ref = Symbol::RELATIVE_REF;
7768 break;
7770 case elfcpp::R_PPC_PLTREL24:
7771 if (size != 32)
7772 break;
7773 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7774 break;
7776 case elfcpp::R_PPC64_REL24_NOTOC:
7777 case elfcpp::R_PPC64_REL24_P9NOTOC:
7778 case elfcpp::R_PPC64_PLT16_LO_DS:
7779 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7780 case elfcpp::R_PPC64_PLTCALL_NOTOC:
7781 case elfcpp::R_PPC64_PLT_PCREL34:
7782 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7783 if (size != 64)
7784 break;
7785 // Fall through.
7786 case elfcpp::R_POWERPC_REL24:
7787 case elfcpp::R_POWERPC_REL14:
7788 case elfcpp::R_POWERPC_REL14_BRTAKEN:
7789 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7790 case elfcpp::R_POWERPC_PLT16_LO:
7791 case elfcpp::R_POWERPC_PLT16_HI:
7792 case elfcpp::R_POWERPC_PLT16_HA:
7793 case elfcpp::R_POWERPC_PLTSEQ:
7794 case elfcpp::R_POWERPC_PLTCALL:
7795 ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7796 break;
7798 case elfcpp::R_PPC64_GOT16_DS:
7799 case elfcpp::R_PPC64_GOT16_LO_DS:
7800 case elfcpp::R_PPC64_GOT_PCREL34:
7801 case elfcpp::R_PPC64_TOC16:
7802 case elfcpp::R_PPC64_TOC16_LO:
7803 case elfcpp::R_PPC64_TOC16_HI:
7804 case elfcpp::R_PPC64_TOC16_HA:
7805 case elfcpp::R_PPC64_TOC16_DS:
7806 case elfcpp::R_PPC64_TOC16_LO_DS:
7807 if (size != 64)
7808 break;
7809 // Fall through.
7810 case elfcpp::R_POWERPC_GOT16:
7811 case elfcpp::R_POWERPC_GOT16_LO:
7812 case elfcpp::R_POWERPC_GOT16_HI:
7813 case elfcpp::R_POWERPC_GOT16_HA:
7814 ref = Symbol::RELATIVE_REF;
7815 break;
7817 case elfcpp::R_PPC64_TLSGD:
7818 case elfcpp::R_PPC64_TLSLD:
7819 case elfcpp::R_PPC64_TPREL34:
7820 case elfcpp::R_PPC64_DTPREL34:
7821 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7822 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7823 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7824 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7825 if (size != 64)
7826 break;
7827 // Fall through.
7828 case elfcpp::R_POWERPC_GOT_TPREL16:
7829 case elfcpp::R_POWERPC_TLS:
7830 ref = Symbol::TLS_REF;
7831 break;
7833 case elfcpp::R_POWERPC_COPY:
7834 case elfcpp::R_POWERPC_GLOB_DAT:
7835 case elfcpp::R_POWERPC_JMP_SLOT:
7836 case elfcpp::R_POWERPC_RELATIVE:
7837 case elfcpp::R_POWERPC_DTPMOD:
7838 default:
7839 // Not expected. We will give an error later.
7840 break;
7843 if (size == 64 && target->abiversion() < 2)
7844 ref |= Symbol::FUNC_DESC_ABI;
7845 return ref;
7848 // Report an unsupported relocation against a local symbol.
7850 template<int size, bool big_endian>
7851 void
7852 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7853 Sized_relobj_file<size, big_endian>* object,
7854 unsigned int r_type)
7856 gold_error(_("%s: unsupported reloc %u against local symbol"),
7857 object->name().c_str(), r_type);
7860 // We are about to emit a dynamic relocation of type R_TYPE. If the
7861 // dynamic linker does not support it, issue an error.
7863 template<int size, bool big_endian>
7864 void
7865 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7866 unsigned int r_type)
7868 gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7870 // These are the relocation types supported by glibc for both 32-bit
7871 // and 64-bit powerpc.
7872 switch (r_type)
7874 case elfcpp::R_POWERPC_NONE:
7875 case elfcpp::R_POWERPC_RELATIVE:
7876 case elfcpp::R_POWERPC_GLOB_DAT:
7877 case elfcpp::R_POWERPC_DTPMOD:
7878 case elfcpp::R_POWERPC_DTPREL:
7879 case elfcpp::R_POWERPC_TPREL:
7880 case elfcpp::R_POWERPC_JMP_SLOT:
7881 case elfcpp::R_POWERPC_COPY:
7882 case elfcpp::R_POWERPC_IRELATIVE:
7883 case elfcpp::R_POWERPC_ADDR32:
7884 case elfcpp::R_POWERPC_UADDR32:
7885 case elfcpp::R_POWERPC_ADDR24:
7886 case elfcpp::R_POWERPC_ADDR16:
7887 case elfcpp::R_POWERPC_UADDR16:
7888 case elfcpp::R_POWERPC_ADDR16_LO:
7889 case elfcpp::R_POWERPC_ADDR16_HI:
7890 case elfcpp::R_POWERPC_ADDR16_HA:
7891 case elfcpp::R_POWERPC_ADDR14:
7892 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7893 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7894 case elfcpp::R_POWERPC_REL32:
7895 case elfcpp::R_POWERPC_TPREL16:
7896 case elfcpp::R_POWERPC_TPREL16_LO:
7897 case elfcpp::R_POWERPC_TPREL16_HI:
7898 case elfcpp::R_POWERPC_TPREL16_HA:
7899 return;
7901 default:
7902 break;
7905 if (size == 64)
7907 switch (r_type)
7909 // These are the relocation types supported only on 64-bit.
7910 case elfcpp::R_PPC64_ADDR64:
7911 case elfcpp::R_PPC64_UADDR64:
7912 case elfcpp::R_PPC64_JMP_IREL:
7913 case elfcpp::R_PPC64_ADDR16_DS:
7914 case elfcpp::R_PPC64_ADDR16_LO_DS:
7915 case elfcpp::R_PPC64_ADDR16_HIGH:
7916 case elfcpp::R_PPC64_ADDR16_HIGHA:
7917 case elfcpp::R_PPC64_ADDR16_HIGHER:
7918 case elfcpp::R_PPC64_ADDR16_HIGHEST:
7919 case elfcpp::R_PPC64_ADDR16_HIGHERA:
7920 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7921 case elfcpp::R_PPC64_REL64:
7922 case elfcpp::R_POWERPC_ADDR30:
7923 case elfcpp::R_PPC64_TPREL16_DS:
7924 case elfcpp::R_PPC64_TPREL16_LO_DS:
7925 case elfcpp::R_PPC64_TPREL16_HIGH:
7926 case elfcpp::R_PPC64_TPREL16_HIGHA:
7927 case elfcpp::R_PPC64_TPREL16_HIGHER:
7928 case elfcpp::R_PPC64_TPREL16_HIGHEST:
7929 case elfcpp::R_PPC64_TPREL16_HIGHERA:
7930 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7931 return;
7933 default:
7934 break;
7937 else
7939 switch (r_type)
7941 // These are the relocation types supported only on 32-bit.
7942 // ??? glibc ld.so doesn't need to support these.
7943 case elfcpp::R_POWERPC_REL24:
7944 case elfcpp::R_POWERPC_DTPREL16:
7945 case elfcpp::R_POWERPC_DTPREL16_LO:
7946 case elfcpp::R_POWERPC_DTPREL16_HI:
7947 case elfcpp::R_POWERPC_DTPREL16_HA:
7948 return;
7950 default:
7951 break;
7955 // This prevents us from issuing more than one error per reloc
7956 // section. But we can still wind up issuing more than one
7957 // error per object file.
7958 if (this->issued_non_pic_error_)
7959 return;
7960 gold_assert(parameters->options().output_is_position_independent());
7961 object->error(_("requires unsupported dynamic reloc; "
7962 "recompile with -fPIC"));
7963 this->issued_non_pic_error_ = true;
7964 return;
7967 // Return whether we need to make a PLT entry for a relocation of the
7968 // given type against a STT_GNU_IFUNC symbol.
7970 template<int size, bool big_endian>
7971 bool
7972 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7973 Target_powerpc<size, big_endian>* target,
7974 Sized_relobj_file<size, big_endian>* object,
7975 unsigned int r_type,
7976 bool report_err)
7978 // In non-pic code any reference will resolve to the plt call stub
7979 // for the ifunc symbol.
7980 if ((size == 32 || target->abiversion() >= 2)
7981 && !parameters->options().output_is_position_independent())
7982 return true;
7984 switch (r_type)
7986 // Word size refs from data sections are OK, but don't need a PLT entry.
7987 case elfcpp::R_POWERPC_ADDR32:
7988 case elfcpp::R_POWERPC_UADDR32:
7989 if (size == 32)
7990 return false;
7991 break;
7993 case elfcpp::R_PPC64_ADDR64:
7994 case elfcpp::R_PPC64_UADDR64:
7995 if (size == 64)
7996 return false;
7997 break;
7999 // GOT refs are good, but also don't need a PLT entry.
8000 case elfcpp::R_POWERPC_GOT16:
8001 case elfcpp::R_POWERPC_GOT16_LO:
8002 case elfcpp::R_POWERPC_GOT16_HI:
8003 case elfcpp::R_POWERPC_GOT16_HA:
8004 case elfcpp::R_PPC64_GOT16_DS:
8005 case elfcpp::R_PPC64_GOT16_LO_DS:
8006 case elfcpp::R_PPC64_GOT_PCREL34:
8007 return false;
8009 // PLT relocs are OK and need a PLT entry.
8010 case elfcpp::R_POWERPC_PLT16_LO:
8011 case elfcpp::R_POWERPC_PLT16_HI:
8012 case elfcpp::R_POWERPC_PLT16_HA:
8013 case elfcpp::R_PPC64_PLT16_LO_DS:
8014 case elfcpp::R_POWERPC_PLTSEQ:
8015 case elfcpp::R_POWERPC_PLTCALL:
8016 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8017 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8018 case elfcpp::R_PPC64_PLT_PCREL34:
8019 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8020 return true;
8021 break;
8023 // Function calls are good, and these do need a PLT entry.
8024 case elfcpp::R_PPC64_REL24_NOTOC:
8025 if (size == 32)
8026 break;
8027 // Fall through.
8028 case elfcpp::R_PPC64_REL24_P9NOTOC:
8029 case elfcpp::R_POWERPC_ADDR24:
8030 case elfcpp::R_POWERPC_ADDR14:
8031 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8032 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8033 case elfcpp::R_POWERPC_REL24:
8034 case elfcpp::R_PPC_PLTREL24:
8035 case elfcpp::R_POWERPC_REL14:
8036 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8037 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8038 return true;
8040 default:
8041 break;
8044 // Anything else is a problem.
8045 // If we are building a static executable, the libc startup function
8046 // responsible for applying indirect function relocations is going
8047 // to complain about the reloc type.
8048 // If we are building a dynamic executable, we will have a text
8049 // relocation. The dynamic loader will set the text segment
8050 // writable and non-executable to apply text relocations. So we'll
8051 // segfault when trying to run the indirection function to resolve
8052 // the reloc.
8053 if (report_err)
8054 gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
8055 object->name().c_str(), r_type);
8056 return false;
8059 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
8060 // reloc.
8062 static bool
8063 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
8065 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8066 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8067 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8068 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8069 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8070 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8071 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8072 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8073 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8074 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8075 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8076 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8077 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8078 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8079 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8080 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8081 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8082 /* Exclude lfqu by testing reloc. If relocs are ever
8083 defined for the reduced D field in psq_lu then those
8084 will need testing too. */
8085 && r_type != elfcpp::R_PPC64_TOC16_LO
8086 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8087 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8088 && (insn & 1) == 0)
8089 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8090 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8091 /* Exclude stfqu. psq_stu as above for psq_lu. */
8092 && r_type != elfcpp::R_PPC64_TOC16_LO
8093 && r_type != elfcpp::R_POWERPC_GOT16_LO)
8094 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8095 && (insn & 1) == 0));
8098 // Scan a relocation for a local symbol.
8100 template<int size, bool big_endian>
8101 inline void
8102 Target_powerpc<size, big_endian>::Scan::local(
8103 Symbol_table* symtab,
8104 Layout* layout,
8105 Target_powerpc<size, big_endian>* target,
8106 Sized_relobj_file<size, big_endian>* object,
8107 unsigned int data_shndx,
8108 Output_section* output_section,
8109 const elfcpp::Rela<size, big_endian>& reloc,
8110 unsigned int r_type,
8111 const elfcpp::Sym<size, big_endian>& lsym,
8112 bool is_discarded)
8114 Powerpc_relobj<size, big_endian>* ppc_object
8115 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8117 this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
8119 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8120 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8122 this->expect_tls_get_addr_call();
8123 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8124 if (tls_type != tls::TLSOPT_NONE)
8125 this->skip_next_tls_get_addr_call();
8127 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8128 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8130 this->expect_tls_get_addr_call();
8131 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8132 if (tls_type != tls::TLSOPT_NONE)
8133 this->skip_next_tls_get_addr_call();
8136 if (is_discarded)
8138 if (size == 64
8139 && data_shndx == ppc_object->opd_shndx()
8140 && r_type == elfcpp::R_PPC64_ADDR64)
8141 ppc_object->set_opd_discard(reloc.get_r_offset());
8142 return;
8145 // A local STT_GNU_IFUNC symbol may require a PLT entry.
8146 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
8147 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8149 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8150 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8151 r_type, r_sym, reloc.get_r_addend());
8152 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
8155 switch (r_type)
8157 case elfcpp::R_POWERPC_NONE:
8158 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8159 case elfcpp::R_POWERPC_GNU_VTENTRY:
8160 case elfcpp::R_POWERPC_TLS:
8161 case elfcpp::R_PPC64_ENTRY:
8162 case elfcpp::R_POWERPC_PLTSEQ:
8163 case elfcpp::R_POWERPC_PLTCALL:
8164 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8165 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8166 case elfcpp::R_PPC64_PCREL_OPT:
8167 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8168 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8169 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8170 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8171 case elfcpp::R_PPC64_REL16_HIGHER34:
8172 case elfcpp::R_PPC64_REL16_HIGHERA34:
8173 case elfcpp::R_PPC64_REL16_HIGHEST34:
8174 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8175 case elfcpp::R_PPC64_D34:
8176 case elfcpp::R_PPC64_D34_LO:
8177 case elfcpp::R_PPC64_D34_HI30:
8178 case elfcpp::R_PPC64_D34_HA30:
8179 case elfcpp::R_PPC64_D28:
8180 case elfcpp::R_PPC64_PCREL34:
8181 case elfcpp::R_PPC64_PCREL28:
8182 case elfcpp::R_PPC64_TPREL34:
8183 case elfcpp::R_PPC64_DTPREL34:
8184 break;
8186 case elfcpp::R_PPC64_TOC:
8188 Output_data_got_powerpc<size, big_endian>* got
8189 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8190 if (parameters->options().output_is_position_independent())
8192 Address off = reloc.get_r_offset();
8193 if (size == 64
8194 && target->abiversion() < 2
8195 && data_shndx == ppc_object->opd_shndx()
8196 && ppc_object->get_opd_discard(off - 8))
8197 break;
8199 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8200 Address got_off = got->g_o_t();
8201 rela_dyn->add_output_section_relative(got->output_section(),
8202 elfcpp::R_POWERPC_RELATIVE,
8203 output_section,
8204 object, data_shndx, off,
8205 got_off);
8208 break;
8210 case elfcpp::R_PPC64_ADDR64:
8211 case elfcpp::R_PPC64_UADDR64:
8212 case elfcpp::R_POWERPC_ADDR32:
8213 case elfcpp::R_POWERPC_UADDR32:
8214 case elfcpp::R_POWERPC_ADDR24:
8215 case elfcpp::R_POWERPC_ADDR16:
8216 case elfcpp::R_POWERPC_ADDR16_LO:
8217 case elfcpp::R_POWERPC_ADDR16_HI:
8218 case elfcpp::R_POWERPC_ADDR16_HA:
8219 case elfcpp::R_POWERPC_UADDR16:
8220 case elfcpp::R_PPC64_ADDR16_HIGH:
8221 case elfcpp::R_PPC64_ADDR16_HIGHA:
8222 case elfcpp::R_PPC64_ADDR16_HIGHER:
8223 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8224 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8225 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8226 case elfcpp::R_PPC64_ADDR16_DS:
8227 case elfcpp::R_PPC64_ADDR16_LO_DS:
8228 case elfcpp::R_POWERPC_ADDR14:
8229 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8230 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8231 // If building a shared library (or a position-independent
8232 // executable), we need to create a dynamic relocation for
8233 // this location.
8234 if (parameters->options().output_is_position_independent()
8235 || (size == 64 && is_ifunc && target->abiversion() < 2))
8237 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8238 is_ifunc);
8239 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8240 if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8241 || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
8243 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8244 : elfcpp::R_POWERPC_RELATIVE);
8245 rela_dyn->add_local_relative(object, r_sym, dynrel,
8246 output_section, data_shndx,
8247 reloc.get_r_offset(),
8248 reloc.get_r_addend(), false);
8250 else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8252 check_non_pic(object, r_type);
8253 rela_dyn->add_local(object, r_sym, r_type, output_section,
8254 data_shndx, reloc.get_r_offset(),
8255 reloc.get_r_addend());
8257 else
8259 gold_assert(lsym.get_st_value() == 0);
8260 unsigned int shndx = lsym.get_st_shndx();
8261 bool is_ordinary;
8262 shndx = object->adjust_sym_shndx(r_sym, shndx,
8263 &is_ordinary);
8264 if (!is_ordinary)
8265 object->error(_("section symbol %u has bad shndx %u"),
8266 r_sym, shndx);
8267 else
8268 rela_dyn->add_local_section(object, shndx, r_type,
8269 output_section, data_shndx,
8270 reloc.get_r_offset());
8273 break;
8275 case elfcpp::R_PPC64_PLT_PCREL34:
8276 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8277 case elfcpp::R_POWERPC_PLT16_LO:
8278 case elfcpp::R_POWERPC_PLT16_HI:
8279 case elfcpp::R_POWERPC_PLT16_HA:
8280 case elfcpp::R_PPC64_PLT16_LO_DS:
8281 if (!is_ifunc)
8283 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8284 target->make_local_plt_entry(symtab, layout, object, r_sym);
8286 break;
8288 case elfcpp::R_PPC64_REL24_NOTOC:
8289 if (size == 32)
8290 break;
8291 // Fall through.
8292 case elfcpp::R_PPC64_REL24_P9NOTOC:
8293 case elfcpp::R_POWERPC_REL24:
8294 case elfcpp::R_PPC_PLTREL24:
8295 case elfcpp::R_PPC_LOCAL24PC:
8296 case elfcpp::R_POWERPC_REL14:
8297 case elfcpp::R_POWERPC_REL14_BRTAKEN:
8298 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8299 if (!is_ifunc)
8301 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8302 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8303 r_type, r_sym, reloc.get_r_addend());
8305 break;
8307 case elfcpp::R_PPC64_TOCSAVE:
8308 // R_PPC64_TOCSAVE follows a call instruction to indicate the
8309 // caller has already saved r2 and thus a plt call stub need not
8310 // save r2.
8311 if (size == 64
8312 && target->mark_pltcall(ppc_object, data_shndx,
8313 reloc.get_r_offset() - 4, symtab))
8315 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8316 unsigned int shndx = lsym.get_st_shndx();
8317 bool is_ordinary;
8318 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8319 if (!is_ordinary)
8320 object->error(_("tocsave symbol %u has bad shndx %u"),
8321 r_sym, shndx);
8322 else
8323 target->add_tocsave(ppc_object, shndx,
8324 lsym.get_st_value() + reloc.get_r_addend());
8326 break;
8328 case elfcpp::R_PPC64_REL64:
8329 case elfcpp::R_POWERPC_REL32:
8330 case elfcpp::R_POWERPC_REL16:
8331 case elfcpp::R_POWERPC_REL16_LO:
8332 case elfcpp::R_POWERPC_REL16_HI:
8333 case elfcpp::R_POWERPC_REL16_HA:
8334 case elfcpp::R_POWERPC_REL16DX_HA:
8335 case elfcpp::R_PPC64_REL16_HIGH:
8336 case elfcpp::R_PPC64_REL16_HIGHA:
8337 case elfcpp::R_PPC64_REL16_HIGHER:
8338 case elfcpp::R_PPC64_REL16_HIGHERA:
8339 case elfcpp::R_PPC64_REL16_HIGHEST:
8340 case elfcpp::R_PPC64_REL16_HIGHESTA:
8341 case elfcpp::R_POWERPC_SECTOFF:
8342 case elfcpp::R_POWERPC_SECTOFF_LO:
8343 case elfcpp::R_POWERPC_SECTOFF_HI:
8344 case elfcpp::R_POWERPC_SECTOFF_HA:
8345 case elfcpp::R_PPC64_SECTOFF_DS:
8346 case elfcpp::R_PPC64_SECTOFF_LO_DS:
8347 case elfcpp::R_POWERPC_TPREL16:
8348 case elfcpp::R_POWERPC_TPREL16_LO:
8349 case elfcpp::R_POWERPC_TPREL16_HI:
8350 case elfcpp::R_POWERPC_TPREL16_HA:
8351 case elfcpp::R_PPC64_TPREL16_DS:
8352 case elfcpp::R_PPC64_TPREL16_LO_DS:
8353 case elfcpp::R_PPC64_TPREL16_HIGH:
8354 case elfcpp::R_PPC64_TPREL16_HIGHA:
8355 case elfcpp::R_PPC64_TPREL16_HIGHER:
8356 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8357 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8358 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8359 case elfcpp::R_POWERPC_DTPREL16:
8360 case elfcpp::R_POWERPC_DTPREL16_LO:
8361 case elfcpp::R_POWERPC_DTPREL16_HI:
8362 case elfcpp::R_POWERPC_DTPREL16_HA:
8363 case elfcpp::R_PPC64_DTPREL16_DS:
8364 case elfcpp::R_PPC64_DTPREL16_LO_DS:
8365 case elfcpp::R_PPC64_DTPREL16_HIGH:
8366 case elfcpp::R_PPC64_DTPREL16_HIGHA:
8367 case elfcpp::R_PPC64_DTPREL16_HIGHER:
8368 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8369 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8370 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8371 case elfcpp::R_PPC64_TLSGD:
8372 case elfcpp::R_PPC64_TLSLD:
8373 case elfcpp::R_PPC64_ADDR64_LOCAL:
8374 break;
8376 case elfcpp::R_PPC64_GOT_PCREL34:
8377 case elfcpp::R_POWERPC_GOT16:
8378 case elfcpp::R_POWERPC_GOT16_LO:
8379 case elfcpp::R_POWERPC_GOT16_HI:
8380 case elfcpp::R_POWERPC_GOT16_HA:
8381 case elfcpp::R_PPC64_GOT16_DS:
8382 case elfcpp::R_PPC64_GOT16_LO_DS:
8384 // The symbol requires a GOT entry.
8385 Got_type got_type = ((size == 32
8386 || r_type == elfcpp::R_POWERPC_GOT16
8387 || r_type == elfcpp::R_PPC64_GOT16_DS)
8388 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
8389 Output_data_got_powerpc<size, big_endian>* got
8390 = target->got_section(symtab, layout, got_type);
8391 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8392 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8394 if (!parameters->options().output_is_position_independent())
8396 if (is_ifunc
8397 && (size == 32 || target->abiversion() >= 2))
8398 got->add_local_plt(object, r_sym, got_type, addend);
8399 else
8400 got->add_local(object, r_sym, got_type, addend);
8402 else if (!object->local_has_got_offset(r_sym, got_type, addend))
8404 // If we are generating a shared object or a pie, this
8405 // symbol's GOT entry will be set by a dynamic relocation.
8406 unsigned int off;
8407 off = got->add_constant(0);
8408 object->set_local_got_offset(r_sym, got_type, off, addend);
8410 Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8411 is_ifunc);
8412 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8413 : elfcpp::R_POWERPC_RELATIVE);
8414 rela_dyn->add_local_relative(object, r_sym, dynrel,
8415 got, off, addend, false);
8418 break;
8420 case elfcpp::R_PPC64_TOC16:
8421 case elfcpp::R_PPC64_TOC16_LO:
8422 case elfcpp::R_PPC64_TOC16_HI:
8423 case elfcpp::R_PPC64_TOC16_HA:
8424 case elfcpp::R_PPC64_TOC16_DS:
8425 case elfcpp::R_PPC64_TOC16_LO_DS:
8426 // We need a GOT section.
8427 target->got_section(symtab, layout, GOT_TYPE_SMALL);
8428 break;
8430 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8431 case elfcpp::R_POWERPC_GOT_TLSGD16:
8432 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8433 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8434 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8436 tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8437 if (tls_type == tls::TLSOPT_NONE)
8439 Got_type got_type = ((size == 32
8440 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8441 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
8442 Output_data_got_powerpc<size, big_endian>* got
8443 = target->got_section(symtab, layout, got_type);
8444 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8445 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8446 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8447 got->add_local_tls_pair(object, r_sym, got_type,
8448 rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8449 addend);
8451 else if (tls_type == tls::TLSOPT_TO_LE)
8453 // no GOT relocs needed for Local Exec.
8455 else
8456 gold_unreachable();
8458 break;
8460 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8461 case elfcpp::R_POWERPC_GOT_TLSLD16:
8462 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8463 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8464 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8466 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8467 if (tls_type == tls::TLSOPT_NONE)
8468 target->tlsld_got_offset(symtab, layout, object);
8469 else if (tls_type == tls::TLSOPT_TO_LE)
8471 // no GOT relocs needed for Local Exec.
8472 if (parameters->options().emit_relocs())
8474 Output_section* os = layout->tls_segment()->first_section();
8475 gold_assert(os != NULL);
8476 os->set_needs_symtab_index();
8479 else
8480 gold_unreachable();
8482 break;
8484 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8485 case elfcpp::R_POWERPC_GOT_DTPREL16:
8486 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8487 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8488 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8490 Got_type got_type = ((size == 32
8491 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8492 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
8493 Output_data_got_powerpc<size, big_endian>* got
8494 = target->got_section(symtab, layout, got_type);
8495 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8496 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8497 got->add_local_tls(object, r_sym, got_type, addend);
8499 break;
8501 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8502 case elfcpp::R_POWERPC_GOT_TPREL16:
8503 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8504 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8505 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8507 tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8508 if (tls_type == tls::TLSOPT_NONE)
8510 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8511 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8512 Got_type got_type = ((size == 32
8513 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8514 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8515 if (!object->local_has_got_offset(r_sym, got_type, addend))
8517 Output_data_got_powerpc<size, big_endian>* got
8518 = target->got_section(symtab, layout, got_type);
8519 unsigned int off = got->add_constant(0);
8520 object->set_local_got_offset(r_sym, got_type, off, addend);
8522 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8523 rela_dyn->add_symbolless_local_addend(object, r_sym,
8524 elfcpp::R_POWERPC_TPREL,
8525 got, off, addend);
8528 else if (tls_type == tls::TLSOPT_TO_LE)
8530 // no GOT relocs needed for Local Exec.
8532 else
8533 gold_unreachable();
8535 break;
8537 default:
8538 unsupported_reloc_local(object, r_type);
8539 break;
8542 if (size == 64
8543 && parameters->options().toc_optimize())
8545 if (data_shndx == ppc_object->toc_shndx())
8547 bool ok = true;
8548 if (r_type != elfcpp::R_PPC64_ADDR64
8549 || (is_ifunc && target->abiversion() < 2))
8550 ok = false;
8551 else if (parameters->options().output_is_position_independent())
8553 if (is_ifunc)
8554 ok = false;
8555 else
8557 unsigned int shndx = lsym.get_st_shndx();
8558 if (shndx >= elfcpp::SHN_LORESERVE
8559 && shndx != elfcpp::SHN_XINDEX)
8560 ok = false;
8563 if (!ok)
8564 ppc_object->set_no_toc_opt(reloc.get_r_offset());
8567 enum {no_check, check_lo, check_ha} insn_check;
8568 switch (r_type)
8570 default:
8571 insn_check = no_check;
8572 break;
8574 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8575 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8576 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8577 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8578 case elfcpp::R_POWERPC_GOT16_HA:
8579 case elfcpp::R_PPC64_TOC16_HA:
8580 insn_check = check_ha;
8581 break;
8583 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8584 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8585 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8586 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8587 case elfcpp::R_POWERPC_GOT16_LO:
8588 case elfcpp::R_PPC64_GOT16_LO_DS:
8589 case elfcpp::R_PPC64_TOC16_LO:
8590 case elfcpp::R_PPC64_TOC16_LO_DS:
8591 insn_check = check_lo;
8592 break;
8595 section_size_type slen;
8596 const unsigned char* view = NULL;
8597 if (insn_check != no_check)
8599 view = ppc_object->section_contents(data_shndx, &slen, false);
8600 section_size_type off =
8601 convert_to_section_size_type(reloc.get_r_offset()) & -4;
8602 if (off < slen)
8604 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8605 if (insn_check == check_lo
8606 ? !ok_lo_toc_insn(insn, r_type)
8607 : ((insn & ((0x3f << 26) | 0x1f << 16))
8608 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8610 ppc_object->set_no_toc_opt();
8611 gold_warning(_("%s: toc optimization is not supported "
8612 "for %#08x instruction"),
8613 ppc_object->name().c_str(), insn);
8618 switch (r_type)
8620 default:
8621 break;
8622 case elfcpp::R_PPC64_TOC16:
8623 case elfcpp::R_PPC64_TOC16_LO:
8624 case elfcpp::R_PPC64_TOC16_HI:
8625 case elfcpp::R_PPC64_TOC16_HA:
8626 case elfcpp::R_PPC64_TOC16_DS:
8627 case elfcpp::R_PPC64_TOC16_LO_DS:
8628 unsigned int shndx = lsym.get_st_shndx();
8629 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8630 bool is_ordinary;
8631 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8632 if (is_ordinary && shndx == ppc_object->toc_shndx())
8634 Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8635 if (dst_off < ppc_object->section_size(shndx))
8637 bool ok = false;
8638 if (r_type == elfcpp::R_PPC64_TOC16_HA)
8639 ok = true;
8640 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8642 // Need to check that the insn is a ld
8643 if (!view)
8644 view = ppc_object->section_contents(data_shndx,
8645 &slen,
8646 false);
8647 section_size_type off =
8648 (convert_to_section_size_type(reloc.get_r_offset())
8649 + (big_endian ? -2 : 3));
8650 if (off < slen
8651 && (view[off] & (0x3f << 2)) == 58u << 2)
8652 ok = true;
8654 if (!ok)
8655 ppc_object->set_no_toc_opt(dst_off);
8658 break;
8662 if (size == 32)
8664 switch (r_type)
8666 case elfcpp::R_POWERPC_REL32:
8667 if (ppc_object->got2_shndx() != 0
8668 && parameters->options().output_is_position_independent())
8670 unsigned int shndx = lsym.get_st_shndx();
8671 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8672 bool is_ordinary;
8673 shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8674 if (is_ordinary && shndx == ppc_object->got2_shndx()
8675 && (ppc_object->section_flags(data_shndx)
8676 & elfcpp::SHF_EXECINSTR) != 0)
8677 gold_error(_("%s: unsupported -mbss-plt code"),
8678 ppc_object->name().c_str());
8680 break;
8681 default:
8682 break;
8686 switch (r_type)
8688 case elfcpp::R_POWERPC_GOT_TLSLD16:
8689 case elfcpp::R_POWERPC_GOT_TLSGD16:
8690 case elfcpp::R_POWERPC_GOT_TPREL16:
8691 case elfcpp::R_POWERPC_GOT_DTPREL16:
8692 case elfcpp::R_POWERPC_GOT16:
8693 case elfcpp::R_PPC64_GOT16_DS:
8694 case elfcpp::R_PPC64_TOC16:
8695 case elfcpp::R_PPC64_TOC16_DS:
8696 ppc_object->set_has_small_toc_reloc();
8697 break;
8698 default:
8699 break;
8702 switch (r_type)
8704 case elfcpp::R_PPC64_TPREL16_DS:
8705 case elfcpp::R_PPC64_TPREL16_LO_DS:
8706 case elfcpp::R_PPC64_TPREL16_HIGH:
8707 case elfcpp::R_PPC64_TPREL16_HIGHA:
8708 case elfcpp::R_PPC64_TPREL16_HIGHER:
8709 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8710 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8711 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8712 case elfcpp::R_PPC64_TPREL34:
8713 if (size != 64)
8714 break;
8715 // Fall through.
8716 case elfcpp::R_POWERPC_TPREL16:
8717 case elfcpp::R_POWERPC_TPREL16_LO:
8718 case elfcpp::R_POWERPC_TPREL16_HI:
8719 case elfcpp::R_POWERPC_TPREL16_HA:
8720 layout->set_has_static_tls();
8721 break;
8722 default:
8723 break;
8726 switch (r_type)
8728 case elfcpp::R_POWERPC_TPREL16_HA:
8729 if (target->tprel_opt())
8731 section_size_type slen;
8732 const unsigned char* view = NULL;
8733 view = ppc_object->section_contents(data_shndx, &slen, false);
8734 section_size_type off
8735 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8736 if (off < slen)
8738 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8739 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8740 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8741 target->set_no_tprel_opt();
8744 break;
8746 case elfcpp::R_PPC64_TPREL16_HIGH:
8747 case elfcpp::R_PPC64_TPREL16_HIGHA:
8748 case elfcpp::R_PPC64_TPREL16_HIGHER:
8749 case elfcpp::R_PPC64_TPREL16_HIGHERA:
8750 case elfcpp::R_PPC64_TPREL16_HIGHEST:
8751 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8752 if (size != 64)
8753 break;
8754 // Fall through.
8755 case elfcpp::R_POWERPC_TPREL16_HI:
8756 target->set_no_tprel_opt();
8757 break;
8758 default:
8759 break;
8762 switch (r_type)
8764 case elfcpp::R_PPC64_D34:
8765 case elfcpp::R_PPC64_D34_LO:
8766 case elfcpp::R_PPC64_D34_HI30:
8767 case elfcpp::R_PPC64_D34_HA30:
8768 case elfcpp::R_PPC64_D28:
8769 case elfcpp::R_PPC64_PCREL34:
8770 case elfcpp::R_PPC64_PCREL28:
8771 case elfcpp::R_PPC64_TPREL34:
8772 case elfcpp::R_PPC64_DTPREL34:
8773 case elfcpp::R_PPC64_PLT_PCREL34:
8774 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8775 case elfcpp::R_PPC64_GOT_PCREL34:
8776 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8777 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8778 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8779 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8780 target->set_power10_relocs();
8781 break;
8782 default:
8783 break;
8787 // Report an unsupported relocation against a global symbol.
8789 template<int size, bool big_endian>
8790 void
8791 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8792 Sized_relobj_file<size, big_endian>* object,
8793 unsigned int r_type,
8794 Symbol* gsym)
8796 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8797 object->name().c_str(), r_type, gsym->demangled_name().c_str());
8800 // Scan a relocation for a global symbol.
8802 template<int size, bool big_endian>
8803 inline void
8804 Target_powerpc<size, big_endian>::Scan::global(
8805 Symbol_table* symtab,
8806 Layout* layout,
8807 Target_powerpc<size, big_endian>* target,
8808 Sized_relobj_file<size, big_endian>* object,
8809 unsigned int data_shndx,
8810 Output_section* output_section,
8811 const elfcpp::Rela<size, big_endian>& reloc,
8812 unsigned int r_type,
8813 Symbol* gsym)
8815 Powerpc_relobj<size, big_endian>* ppc_object
8816 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8818 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8820 case Track_tls::SKIP:
8821 return;
8822 default:
8823 break;
8826 if (target->replace_tls_get_addr(gsym))
8827 // Change a __tls_get_addr reference to __tls_get_addr_opt
8828 // so dynamic relocs are emitted against the latter symbol.
8829 gsym = target->tls_get_addr_opt();
8831 if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8832 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8834 this->expect_tls_get_addr_call();
8835 bool final = gsym->final_value_is_known();
8836 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8837 if (tls_type != tls::TLSOPT_NONE)
8838 this->skip_next_tls_get_addr_call();
8840 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8841 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8843 this->expect_tls_get_addr_call();
8844 tls::Tls_optimization tls_type = target->optimize_tls_ld();
8845 if (tls_type != tls::TLSOPT_NONE)
8846 this->skip_next_tls_get_addr_call();
8849 // A STT_GNU_IFUNC symbol may require a PLT entry.
8850 bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8851 bool pushed_ifunc = false;
8852 if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8854 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8855 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8856 r_type, r_sym, reloc.get_r_addend());
8857 target->make_plt_entry(symtab, layout, gsym);
8858 pushed_ifunc = true;
8861 switch (r_type)
8863 case elfcpp::R_POWERPC_NONE:
8864 case elfcpp::R_POWERPC_GNU_VTINHERIT:
8865 case elfcpp::R_POWERPC_GNU_VTENTRY:
8866 case elfcpp::R_PPC_LOCAL24PC:
8867 case elfcpp::R_POWERPC_TLS:
8868 case elfcpp::R_PPC64_ENTRY:
8869 case elfcpp::R_POWERPC_PLTSEQ:
8870 case elfcpp::R_POWERPC_PLTCALL:
8871 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8872 case elfcpp::R_PPC64_PLTCALL_NOTOC:
8873 case elfcpp::R_PPC64_PCREL_OPT:
8874 case elfcpp::R_PPC64_ADDR16_HIGHER34:
8875 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8876 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8877 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8878 case elfcpp::R_PPC64_REL16_HIGHER34:
8879 case elfcpp::R_PPC64_REL16_HIGHERA34:
8880 case elfcpp::R_PPC64_REL16_HIGHEST34:
8881 case elfcpp::R_PPC64_REL16_HIGHESTA34:
8882 case elfcpp::R_PPC64_D34:
8883 case elfcpp::R_PPC64_D34_LO:
8884 case elfcpp::R_PPC64_D34_HI30:
8885 case elfcpp::R_PPC64_D34_HA30:
8886 case elfcpp::R_PPC64_D28:
8887 case elfcpp::R_PPC64_PCREL34:
8888 case elfcpp::R_PPC64_PCREL28:
8889 case elfcpp::R_PPC64_TPREL34:
8890 case elfcpp::R_PPC64_DTPREL34:
8891 break;
8893 case elfcpp::R_PPC64_TOC:
8895 Output_data_got_powerpc<size, big_endian>* got
8896 = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8897 if (parameters->options().output_is_position_independent())
8899 Address off = reloc.get_r_offset();
8900 if (size == 64
8901 && data_shndx == ppc_object->opd_shndx()
8902 && ppc_object->get_opd_discard(off - 8))
8903 break;
8905 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8906 Address got_off = got->g_o_t();
8907 rela_dyn->add_output_section_relative(got->output_section(),
8908 elfcpp::R_POWERPC_RELATIVE,
8909 output_section,
8910 object, data_shndx, off,
8911 got_off);
8914 break;
8916 case elfcpp::R_PPC64_ADDR64:
8917 if (size == 64
8918 && target->abiversion() < 2
8919 && data_shndx == ppc_object->opd_shndx()
8920 && (gsym->is_defined_in_discarded_section()
8921 || gsym->object() != object))
8923 ppc_object->set_opd_discard(reloc.get_r_offset());
8924 break;
8926 // Fall through.
8927 case elfcpp::R_PPC64_UADDR64:
8928 case elfcpp::R_POWERPC_ADDR32:
8929 case elfcpp::R_POWERPC_UADDR32:
8930 case elfcpp::R_POWERPC_ADDR24:
8931 case elfcpp::R_POWERPC_ADDR16:
8932 case elfcpp::R_POWERPC_ADDR16_LO:
8933 case elfcpp::R_POWERPC_ADDR16_HI:
8934 case elfcpp::R_POWERPC_ADDR16_HA:
8935 case elfcpp::R_POWERPC_UADDR16:
8936 case elfcpp::R_PPC64_ADDR16_HIGH:
8937 case elfcpp::R_PPC64_ADDR16_HIGHA:
8938 case elfcpp::R_PPC64_ADDR16_HIGHER:
8939 case elfcpp::R_PPC64_ADDR16_HIGHERA:
8940 case elfcpp::R_PPC64_ADDR16_HIGHEST:
8941 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8942 case elfcpp::R_PPC64_ADDR16_DS:
8943 case elfcpp::R_PPC64_ADDR16_LO_DS:
8944 case elfcpp::R_POWERPC_ADDR14:
8945 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8946 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8948 // Make a PLT entry if necessary.
8949 if (gsym->needs_plt_entry())
8951 // Since this is not a PC-relative relocation, we may be
8952 // taking the address of a function. In that case we need to
8953 // set the entry in the dynamic symbol table to the address of
8954 // the PLT call stub.
8955 bool need_ifunc_plt = false;
8956 if ((size == 32 || target->abiversion() >= 2)
8957 && gsym->is_from_dynobj()
8958 && !parameters->options().output_is_position_independent())
8960 gsym->set_needs_dynsym_value();
8961 need_ifunc_plt = true;
8963 if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8965 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8966 target->push_branch(ppc_object, data_shndx,
8967 reloc.get_r_offset(), r_type, r_sym,
8968 reloc.get_r_addend());
8969 target->make_plt_entry(symtab, layout, gsym);
8972 // Make a dynamic relocation if necessary.
8973 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8974 || (size == 64 && is_ifunc && target->abiversion() < 2))
8976 if (!parameters->options().output_is_position_independent()
8977 && gsym->may_need_copy_reloc())
8979 target->copy_reloc(symtab, layout, object,
8980 data_shndx, output_section, gsym, reloc);
8982 else if ((((size == 32
8983 && r_type == elfcpp::R_POWERPC_ADDR32)
8984 || (size == 64
8985 && r_type == elfcpp::R_PPC64_ADDR64
8986 && target->abiversion() >= 2))
8987 && gsym->can_use_relative_reloc(false)
8988 && !(gsym->visibility() == elfcpp::STV_PROTECTED
8989 && parameters->options().shared()))
8990 || (size == 64
8991 && r_type == elfcpp::R_PPC64_ADDR64
8992 && target->abiversion() < 2
8993 && (gsym->can_use_relative_reloc(false)
8994 || data_shndx == ppc_object->opd_shndx())))
8996 Reloc_section* rela_dyn
8997 = target->rela_dyn_section(symtab, layout, is_ifunc);
8998 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8999 : elfcpp::R_POWERPC_RELATIVE);
9000 rela_dyn->add_symbolless_global_addend(
9001 gsym, dynrel, output_section, object, data_shndx,
9002 reloc.get_r_offset(), reloc.get_r_addend());
9004 else
9006 Reloc_section* rela_dyn
9007 = target->rela_dyn_section(symtab, layout, is_ifunc);
9008 check_non_pic(object, r_type);
9009 rela_dyn->add_global(gsym, r_type, output_section,
9010 object, data_shndx,
9011 reloc.get_r_offset(),
9012 reloc.get_r_addend());
9014 if (size == 64
9015 && parameters->options().toc_optimize()
9016 && data_shndx == ppc_object->toc_shndx())
9017 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9021 break;
9023 case elfcpp::R_PPC64_PLT_PCREL34:
9024 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9025 case elfcpp::R_POWERPC_PLT16_LO:
9026 case elfcpp::R_POWERPC_PLT16_HI:
9027 case elfcpp::R_POWERPC_PLT16_HA:
9028 case elfcpp::R_PPC64_PLT16_LO_DS:
9029 if (!pushed_ifunc)
9031 if (branch_needs_plt_entry(gsym))
9032 target->make_plt_entry(symtab, layout, gsym);
9033 else
9034 target->make_local_plt_entry(symtab, layout, gsym);
9036 break;
9038 case elfcpp::R_PPC64_REL24_NOTOC:
9039 if (size == 32)
9040 break;
9041 // Fall through.
9042 case elfcpp::R_PPC64_REL24_P9NOTOC:
9043 case elfcpp::R_PPC_PLTREL24:
9044 case elfcpp::R_POWERPC_REL24:
9045 if (!is_ifunc)
9047 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9048 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9049 r_type, r_sym, reloc.get_r_addend());
9050 if (branch_needs_plt_entry(gsym))
9051 target->make_plt_entry(symtab, layout, gsym);
9053 // Fall through.
9055 case elfcpp::R_PPC64_REL64:
9056 case elfcpp::R_POWERPC_REL32:
9057 // Make a dynamic relocation if necessary.
9058 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
9060 if (!parameters->options().output_is_position_independent()
9061 && gsym->may_need_copy_reloc())
9063 target->copy_reloc(symtab, layout, object,
9064 data_shndx, output_section, gsym,
9065 reloc);
9067 else
9069 Reloc_section* rela_dyn
9070 = target->rela_dyn_section(symtab, layout, is_ifunc);
9071 check_non_pic(object, r_type);
9072 rela_dyn->add_global(gsym, r_type, output_section, object,
9073 data_shndx, reloc.get_r_offset(),
9074 reloc.get_r_addend());
9077 break;
9079 case elfcpp::R_POWERPC_REL14:
9080 case elfcpp::R_POWERPC_REL14_BRTAKEN:
9081 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9082 if (!is_ifunc)
9084 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9085 target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9086 r_type, r_sym, reloc.get_r_addend());
9088 break;
9090 case elfcpp::R_PPC64_TOCSAVE:
9091 // R_PPC64_TOCSAVE follows a call instruction to indicate the
9092 // caller has already saved r2 and thus a plt call stub need not
9093 // save r2.
9094 if (size == 64
9095 && target->mark_pltcall(ppc_object, data_shndx,
9096 reloc.get_r_offset() - 4, symtab))
9098 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9099 bool is_ordinary;
9100 unsigned int shndx = gsym->shndx(&is_ordinary);
9101 if (!is_ordinary)
9102 object->error(_("tocsave symbol %u has bad shndx %u"),
9103 r_sym, shndx);
9104 else
9106 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9107 target->add_tocsave(ppc_object, shndx,
9108 sym->value() + reloc.get_r_addend());
9111 break;
9113 case elfcpp::R_POWERPC_REL16:
9114 case elfcpp::R_POWERPC_REL16_LO:
9115 case elfcpp::R_POWERPC_REL16_HI:
9116 case elfcpp::R_POWERPC_REL16_HA:
9117 case elfcpp::R_POWERPC_REL16DX_HA:
9118 case elfcpp::R_PPC64_REL16_HIGH:
9119 case elfcpp::R_PPC64_REL16_HIGHA:
9120 case elfcpp::R_PPC64_REL16_HIGHER:
9121 case elfcpp::R_PPC64_REL16_HIGHERA:
9122 case elfcpp::R_PPC64_REL16_HIGHEST:
9123 case elfcpp::R_PPC64_REL16_HIGHESTA:
9124 case elfcpp::R_POWERPC_SECTOFF:
9125 case elfcpp::R_POWERPC_SECTOFF_LO:
9126 case elfcpp::R_POWERPC_SECTOFF_HI:
9127 case elfcpp::R_POWERPC_SECTOFF_HA:
9128 case elfcpp::R_PPC64_SECTOFF_DS:
9129 case elfcpp::R_PPC64_SECTOFF_LO_DS:
9130 case elfcpp::R_POWERPC_TPREL16:
9131 case elfcpp::R_POWERPC_TPREL16_LO:
9132 case elfcpp::R_POWERPC_TPREL16_HI:
9133 case elfcpp::R_POWERPC_TPREL16_HA:
9134 case elfcpp::R_PPC64_TPREL16_DS:
9135 case elfcpp::R_PPC64_TPREL16_LO_DS:
9136 case elfcpp::R_PPC64_TPREL16_HIGH:
9137 case elfcpp::R_PPC64_TPREL16_HIGHA:
9138 case elfcpp::R_PPC64_TPREL16_HIGHER:
9139 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9140 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9141 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9142 case elfcpp::R_POWERPC_DTPREL16:
9143 case elfcpp::R_POWERPC_DTPREL16_LO:
9144 case elfcpp::R_POWERPC_DTPREL16_HI:
9145 case elfcpp::R_POWERPC_DTPREL16_HA:
9146 case elfcpp::R_PPC64_DTPREL16_DS:
9147 case elfcpp::R_PPC64_DTPREL16_LO_DS:
9148 case elfcpp::R_PPC64_DTPREL16_HIGH:
9149 case elfcpp::R_PPC64_DTPREL16_HIGHA:
9150 case elfcpp::R_PPC64_DTPREL16_HIGHER:
9151 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9152 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9153 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9154 case elfcpp::R_PPC64_TLSGD:
9155 case elfcpp::R_PPC64_TLSLD:
9156 case elfcpp::R_PPC64_ADDR64_LOCAL:
9157 break;
9159 case elfcpp::R_PPC64_GOT_PCREL34:
9160 case elfcpp::R_POWERPC_GOT16:
9161 case elfcpp::R_POWERPC_GOT16_LO:
9162 case elfcpp::R_POWERPC_GOT16_HI:
9163 case elfcpp::R_POWERPC_GOT16_HA:
9164 case elfcpp::R_PPC64_GOT16_DS:
9165 case elfcpp::R_PPC64_GOT16_LO_DS:
9167 // The symbol requires a GOT entry.
9168 Output_data_got_powerpc<size, big_endian>* got;
9169 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9170 Got_type got_type = ((size == 32
9171 || r_type == elfcpp::R_POWERPC_GOT16
9172 || r_type == elfcpp::R_PPC64_GOT16_DS)
9173 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
9175 got = target->got_section(symtab, layout, got_type);
9176 if (gsym->final_value_is_known())
9178 if (is_ifunc
9179 && (size == 32 || target->abiversion() >= 2))
9180 got->add_global_plt(gsym, got_type, addend);
9181 else
9182 got->add_global(gsym, got_type, addend);
9184 else if (!gsym->has_got_offset(got_type, addend))
9186 // If we are generating a shared object or a pie, this
9187 // symbol's GOT entry will be set by a dynamic relocation.
9188 unsigned int off = got->add_constant(0);
9189 gsym->set_got_offset(got_type, off, addend);
9191 Reloc_section* rela_dyn
9192 = target->rela_dyn_section(symtab, layout, is_ifunc);
9194 if (gsym->can_use_relative_reloc(false)
9195 && !((size == 32
9196 || target->abiversion() >= 2)
9197 && gsym->visibility() == elfcpp::STV_PROTECTED
9198 && parameters->options().shared()))
9200 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9201 : elfcpp::R_POWERPC_RELATIVE);
9202 rela_dyn->add_global_relative(gsym, dynrel, got, off,
9203 addend, false);
9205 else
9207 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
9208 rela_dyn->add_global(gsym, dynrel, got, off, addend);
9212 break;
9214 case elfcpp::R_PPC64_TOC16:
9215 case elfcpp::R_PPC64_TOC16_LO:
9216 case elfcpp::R_PPC64_TOC16_HI:
9217 case elfcpp::R_PPC64_TOC16_HA:
9218 case elfcpp::R_PPC64_TOC16_DS:
9219 case elfcpp::R_PPC64_TOC16_LO_DS:
9220 // We need a GOT section.
9221 target->got_section(symtab, layout, GOT_TYPE_SMALL);
9222 break;
9224 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9225 case elfcpp::R_POWERPC_GOT_TLSGD16:
9226 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9227 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9228 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9230 bool final = gsym->final_value_is_known();
9231 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
9232 if (tls_type == tls::TLSOPT_NONE)
9234 Got_type got_type = ((size == 32
9235 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9236 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
9237 Output_data_got_powerpc<size, big_endian>* got
9238 = target->got_section(symtab, layout, got_type);
9239 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9240 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9241 got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
9242 elfcpp::R_POWERPC_DTPMOD,
9243 elfcpp::R_POWERPC_DTPREL,
9244 addend);
9246 else if (tls_type == tls::TLSOPT_TO_IE)
9248 Got_type got_type = ((size == 32
9249 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9250 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9251 if (!gsym->has_got_offset(got_type))
9253 Output_data_got_powerpc<size, big_endian>* got
9254 = target->got_section(symtab, layout, got_type);
9255 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9256 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9257 if (gsym->is_undefined()
9258 || gsym->is_from_dynobj())
9260 got->add_global_with_rel(gsym, got_type, rela_dyn,
9261 elfcpp::R_POWERPC_TPREL, addend);
9263 else
9265 unsigned int off = got->add_constant(0);
9266 gsym->set_got_offset(got_type, off);
9267 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9268 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9269 got, off, addend);
9273 else if (tls_type == tls::TLSOPT_TO_LE)
9275 // no GOT relocs needed for Local Exec.
9277 else
9278 gold_unreachable();
9280 break;
9282 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9283 case elfcpp::R_POWERPC_GOT_TLSLD16:
9284 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9285 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9286 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9288 tls::Tls_optimization tls_type = target->optimize_tls_ld();
9289 if (tls_type == tls::TLSOPT_NONE)
9290 target->tlsld_got_offset(symtab, layout, object);
9291 else if (tls_type == tls::TLSOPT_TO_LE)
9293 // no GOT relocs needed for Local Exec.
9294 if (parameters->options().emit_relocs())
9296 Output_section* os = layout->tls_segment()->first_section();
9297 gold_assert(os != NULL);
9298 os->set_needs_symtab_index();
9301 else
9302 gold_unreachable();
9304 break;
9306 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9307 case elfcpp::R_POWERPC_GOT_DTPREL16:
9308 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9309 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9310 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9312 Got_type got_type = ((size == 32
9313 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9314 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
9315 Output_data_got_powerpc<size, big_endian>* got
9316 = target->got_section(symtab, layout, got_type);
9317 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9318 if (!gsym->final_value_is_known()
9319 && (gsym->is_from_dynobj()
9320 || gsym->is_undefined()
9321 || gsym->is_preemptible()))
9322 got->add_global_with_rel(gsym, got_type,
9323 target->rela_dyn_section(layout),
9324 elfcpp::R_POWERPC_DTPREL, addend);
9325 else
9326 got->add_global_tls(gsym, got_type, addend);
9328 break;
9330 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9331 case elfcpp::R_POWERPC_GOT_TPREL16:
9332 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9333 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9334 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9336 bool final = gsym->final_value_is_known();
9337 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9338 if (tls_type == tls::TLSOPT_NONE)
9340 Got_type got_type = ((size == 32
9341 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9342 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9343 if (!gsym->has_got_offset(got_type))
9345 Output_data_got_powerpc<size, big_endian>* got
9346 = target->got_section(symtab, layout, got_type);
9347 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9348 uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9349 if (gsym->is_undefined()
9350 || gsym->is_from_dynobj())
9352 got->add_global_with_rel(gsym, got_type, rela_dyn,
9353 elfcpp::R_POWERPC_TPREL, addend);
9355 else
9357 unsigned int off = got->add_constant(0);
9358 gsym->set_got_offset(got_type, off);
9359 unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9360 rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9361 got, off, addend);
9365 else if (tls_type == tls::TLSOPT_TO_LE)
9367 // no GOT relocs needed for Local Exec.
9369 else
9370 gold_unreachable();
9372 break;
9374 default:
9375 unsupported_reloc_global(object, r_type, gsym);
9376 break;
9379 if (size == 64
9380 && parameters->options().toc_optimize())
9382 if (data_shndx == ppc_object->toc_shndx())
9384 bool ok = true;
9385 if (r_type != elfcpp::R_PPC64_ADDR64
9386 || (is_ifunc && target->abiversion() < 2))
9387 ok = false;
9388 else if (parameters->options().output_is_position_independent()
9389 && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9390 ok = false;
9391 if (!ok)
9392 ppc_object->set_no_toc_opt(reloc.get_r_offset());
9395 enum {no_check, check_lo, check_ha} insn_check;
9396 switch (r_type)
9398 default:
9399 insn_check = no_check;
9400 break;
9402 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9403 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9404 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9405 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9406 case elfcpp::R_POWERPC_GOT16_HA:
9407 case elfcpp::R_PPC64_TOC16_HA:
9408 insn_check = check_ha;
9409 break;
9411 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9412 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9413 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9414 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9415 case elfcpp::R_POWERPC_GOT16_LO:
9416 case elfcpp::R_PPC64_GOT16_LO_DS:
9417 case elfcpp::R_PPC64_TOC16_LO:
9418 case elfcpp::R_PPC64_TOC16_LO_DS:
9419 insn_check = check_lo;
9420 break;
9423 section_size_type slen;
9424 const unsigned char* view = NULL;
9425 if (insn_check != no_check)
9427 view = ppc_object->section_contents(data_shndx, &slen, false);
9428 section_size_type off =
9429 convert_to_section_size_type(reloc.get_r_offset()) & -4;
9430 if (off < slen)
9432 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9433 if (insn_check == check_lo
9434 ? !ok_lo_toc_insn(insn, r_type)
9435 : ((insn & ((0x3f << 26) | 0x1f << 16))
9436 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9438 ppc_object->set_no_toc_opt();
9439 gold_warning(_("%s: toc optimization is not supported "
9440 "for %#08x instruction"),
9441 ppc_object->name().c_str(), insn);
9446 switch (r_type)
9448 default:
9449 break;
9450 case elfcpp::R_PPC64_TOC16:
9451 case elfcpp::R_PPC64_TOC16_LO:
9452 case elfcpp::R_PPC64_TOC16_HI:
9453 case elfcpp::R_PPC64_TOC16_HA:
9454 case elfcpp::R_PPC64_TOC16_DS:
9455 case elfcpp::R_PPC64_TOC16_LO_DS:
9456 if (gsym->source() == Symbol::FROM_OBJECT
9457 && !gsym->object()->is_dynamic())
9459 Powerpc_relobj<size, big_endian>* sym_object
9460 = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9461 bool is_ordinary;
9462 unsigned int shndx = gsym->shndx(&is_ordinary);
9463 if (shndx == sym_object->toc_shndx())
9465 Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9466 Address dst_off = sym->value() + reloc.get_r_addend();
9467 if (dst_off < sym_object->section_size(shndx))
9469 bool ok = false;
9470 if (r_type == elfcpp::R_PPC64_TOC16_HA)
9471 ok = true;
9472 else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9474 // Need to check that the insn is a ld
9475 if (!view)
9476 view = ppc_object->section_contents(data_shndx,
9477 &slen,
9478 false);
9479 section_size_type off =
9480 (convert_to_section_size_type(reloc.get_r_offset())
9481 + (big_endian ? -2 : 3));
9482 if (off < slen
9483 && (view[off] & (0x3f << 2)) == (58u << 2))
9484 ok = true;
9486 if (!ok)
9487 sym_object->set_no_toc_opt(dst_off);
9491 break;
9495 if (size == 32)
9497 switch (r_type)
9499 case elfcpp::R_PPC_LOCAL24PC:
9500 if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9501 gold_error(_("%s: unsupported -mbss-plt code"),
9502 ppc_object->name().c_str());
9503 break;
9504 default:
9505 break;
9509 switch (r_type)
9511 case elfcpp::R_POWERPC_GOT_TLSLD16:
9512 case elfcpp::R_POWERPC_GOT_TLSGD16:
9513 case elfcpp::R_POWERPC_GOT_TPREL16:
9514 case elfcpp::R_POWERPC_GOT_DTPREL16:
9515 case elfcpp::R_POWERPC_GOT16:
9516 case elfcpp::R_PPC64_GOT16_DS:
9517 case elfcpp::R_PPC64_TOC16:
9518 case elfcpp::R_PPC64_TOC16_DS:
9519 ppc_object->set_has_small_toc_reloc();
9520 break;
9521 default:
9522 break;
9525 switch (r_type)
9527 case elfcpp::R_PPC64_TPREL16_DS:
9528 case elfcpp::R_PPC64_TPREL16_LO_DS:
9529 case elfcpp::R_PPC64_TPREL16_HIGH:
9530 case elfcpp::R_PPC64_TPREL16_HIGHA:
9531 case elfcpp::R_PPC64_TPREL16_HIGHER:
9532 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9533 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9534 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9535 case elfcpp::R_PPC64_TPREL34:
9536 if (size != 64)
9537 break;
9538 // Fall through.
9539 case elfcpp::R_POWERPC_TPREL16:
9540 case elfcpp::R_POWERPC_TPREL16_LO:
9541 case elfcpp::R_POWERPC_TPREL16_HI:
9542 case elfcpp::R_POWERPC_TPREL16_HA:
9543 layout->set_has_static_tls();
9544 break;
9545 default:
9546 break;
9549 switch (r_type)
9551 case elfcpp::R_POWERPC_TPREL16_HA:
9552 if (target->tprel_opt())
9554 section_size_type slen;
9555 const unsigned char* view = NULL;
9556 view = ppc_object->section_contents(data_shndx, &slen, false);
9557 section_size_type off
9558 = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9559 if (off < slen)
9561 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9562 if ((insn & ((0x3fu << 26) | 0x1f << 16))
9563 != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9564 target->set_no_tprel_opt();
9567 break;
9569 case elfcpp::R_PPC64_TPREL16_HIGH:
9570 case elfcpp::R_PPC64_TPREL16_HIGHA:
9571 case elfcpp::R_PPC64_TPREL16_HIGHER:
9572 case elfcpp::R_PPC64_TPREL16_HIGHERA:
9573 case elfcpp::R_PPC64_TPREL16_HIGHEST:
9574 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9575 if (size != 64)
9576 break;
9577 // Fall through.
9578 case elfcpp::R_POWERPC_TPREL16_HI:
9579 target->set_no_tprel_opt();
9580 break;
9581 default:
9582 break;
9585 switch (r_type)
9587 case elfcpp::R_PPC64_D34:
9588 case elfcpp::R_PPC64_D34_LO:
9589 case elfcpp::R_PPC64_D34_HI30:
9590 case elfcpp::R_PPC64_D34_HA30:
9591 case elfcpp::R_PPC64_D28:
9592 case elfcpp::R_PPC64_PCREL34:
9593 case elfcpp::R_PPC64_PCREL28:
9594 case elfcpp::R_PPC64_TPREL34:
9595 case elfcpp::R_PPC64_DTPREL34:
9596 case elfcpp::R_PPC64_PLT_PCREL34:
9597 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9598 case elfcpp::R_PPC64_GOT_PCREL34:
9599 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9600 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9601 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9602 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9603 target->set_power10_relocs();
9604 break;
9605 default:
9606 break;
9610 // Process relocations for gc.
9612 template<int size, bool big_endian>
9613 void
9614 Target_powerpc<size, big_endian>::gc_process_relocs(
9615 Symbol_table* symtab,
9616 Layout* layout,
9617 Sized_relobj_file<size, big_endian>* object,
9618 unsigned int data_shndx,
9619 unsigned int,
9620 const unsigned char* prelocs,
9621 size_t reloc_count,
9622 Output_section* output_section,
9623 bool needs_special_offset_handling,
9624 size_t local_symbol_count,
9625 const unsigned char* plocal_symbols)
9627 typedef Target_powerpc<size, big_endian> Powerpc;
9628 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9629 Classify_reloc;
9631 Powerpc_relobj<size, big_endian>* ppc_object
9632 = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9633 if (size == 64)
9634 ppc_object->set_opd_valid();
9635 if (size == 64 && data_shndx == ppc_object->opd_shndx())
9637 typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9638 for (p = ppc_object->access_from_map()->begin();
9639 p != ppc_object->access_from_map()->end();
9640 ++p)
9642 Address dst_off = p->first;
9643 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9644 typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9645 for (s = p->second.begin(); s != p->second.end(); ++s)
9647 Relobj* src_obj = s->first;
9648 unsigned int src_indx = s->second;
9649 symtab->gc()->add_reference(src_obj, src_indx,
9650 ppc_object, dst_indx);
9652 p->second.clear();
9654 ppc_object->access_from_map()->clear();
9655 ppc_object->process_gc_mark(symtab);
9656 // Don't look at .opd relocs as .opd will reference everything.
9657 return;
9660 gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9661 symtab,
9662 layout,
9663 this,
9664 object,
9665 data_shndx,
9666 prelocs,
9667 reloc_count,
9668 output_section,
9669 needs_special_offset_handling,
9670 local_symbol_count,
9671 plocal_symbols);
9674 // Handle target specific gc actions when adding a gc reference from
9675 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9676 // and DST_OFF. For powerpc64, this adds a referenc to the code
9677 // section of a function descriptor.
9679 template<int size, bool big_endian>
9680 void
9681 Target_powerpc<size, big_endian>::do_gc_add_reference(
9682 Symbol_table* symtab,
9683 Relobj* src_obj,
9684 unsigned int src_shndx,
9685 Relobj* dst_obj,
9686 unsigned int dst_shndx,
9687 Address dst_off) const
9689 if (size != 64 || dst_obj->is_dynamic())
9690 return;
9692 Powerpc_relobj<size, big_endian>* ppc_object
9693 = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9694 if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9696 if (ppc_object->opd_valid())
9698 dst_shndx = ppc_object->get_opd_ent(dst_off);
9699 symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9701 else
9703 // If we haven't run scan_opd_relocs, we must delay
9704 // processing this function descriptor reference.
9705 ppc_object->add_reference(src_obj, src_shndx, dst_off);
9710 // Add any special sections for this symbol to the gc work list.
9711 // For powerpc64, this adds the code section of a function
9712 // descriptor.
9714 template<int size, bool big_endian>
9715 void
9716 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9717 Symbol_table* symtab,
9718 Symbol* sym) const
9720 if (size == 64 && sym->object()->pluginobj() == NULL)
9722 Powerpc_relobj<size, big_endian>* ppc_object
9723 = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9724 bool is_ordinary;
9725 unsigned int shndx = sym->shndx(&is_ordinary);
9726 if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9728 Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9729 Address dst_off = gsym->value();
9730 if (ppc_object->opd_valid())
9732 unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9733 symtab->gc()->worklist().push_back(Section_id(ppc_object,
9734 dst_indx));
9736 else
9737 ppc_object->add_gc_mark(dst_off);
9742 // For a symbol location in .opd, set LOC to the location of the
9743 // function entry.
9745 template<int size, bool big_endian>
9746 void
9747 Target_powerpc<size, big_endian>::do_function_location(
9748 Symbol_location* loc) const
9750 if (size == 64 && loc->shndx != 0)
9752 if (loc->object->is_dynamic())
9754 Powerpc_dynobj<size, big_endian>* ppc_object
9755 = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9756 if (loc->shndx == ppc_object->opd_shndx())
9758 Address dest_off;
9759 Address off = loc->offset - ppc_object->opd_address();
9760 loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9761 loc->offset = dest_off;
9764 else
9766 const Powerpc_relobj<size, big_endian>* ppc_object
9767 = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9768 if (loc->shndx == ppc_object->opd_shndx())
9770 Address dest_off;
9771 loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9772 loc->offset = dest_off;
9778 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9779 // compiled with -fsplit-stack. The function calls non-split-stack
9780 // code. Change the function to ensure it has enough stack space to
9781 // call some random function.
9783 template<int size, bool big_endian>
9784 void
9785 Target_powerpc<size, big_endian>::do_calls_non_split(
9786 Relobj* object,
9787 unsigned int shndx,
9788 section_offset_type fnoffset,
9789 section_size_type fnsize,
9790 const unsigned char* prelocs,
9791 size_t reloc_count,
9792 unsigned char* view,
9793 section_size_type view_size,
9794 std::string* from,
9795 std::string* to) const
9797 // 32-bit not supported.
9798 if (size == 32)
9800 // warn
9801 Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9802 prelocs, reloc_count, view, view_size,
9803 from, to);
9804 return;
9807 // The function always starts with
9808 // ld %r0,-0x7000-64(%r13) # tcbhead_t.__private_ss
9809 // addis %r12,%r1,-allocate@ha
9810 // addi %r12,%r12,-allocate@l
9811 // cmpld %r12,%r0
9812 // but note that the addis or addi may be replaced with a nop
9814 unsigned char *entry = view + fnoffset;
9815 uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9817 if ((insn & 0xffff0000) == addis_2_12)
9819 /* Skip ELFv2 global entry code. */
9820 entry += 8;
9821 insn = elfcpp::Swap<32, big_endian>::readval(entry);
9824 unsigned char *pinsn = entry;
9825 bool ok = false;
9826 const uint32_t ld_private_ss = 0xe80d8fc0;
9827 if (insn == ld_private_ss)
9829 int32_t allocate = 0;
9830 while (1)
9832 pinsn += 4;
9833 insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9834 if ((insn & 0xffff0000) == addis_12_1)
9835 allocate += (insn & 0xffff) << 16;
9836 else if ((insn & 0xffff0000) == addi_12_1
9837 || (insn & 0xffff0000) == addi_12_12)
9838 allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9839 else if (insn != nop)
9840 break;
9842 if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9844 int extra = parameters->options().split_stack_adjust_size();
9845 allocate -= extra;
9846 if (allocate >= 0 || extra < 0)
9848 object->error(_("split-stack stack size overflow at "
9849 "section %u offset %0zx"),
9850 shndx, static_cast<size_t>(fnoffset));
9851 return;
9853 pinsn = entry + 4;
9854 insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9855 if (insn != addis_12_1)
9857 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9858 pinsn += 4;
9859 insn = addi_12_12 | (allocate & 0xffff);
9860 if (insn != addi_12_12)
9862 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9863 pinsn += 4;
9866 else
9868 insn = addi_12_1 | (allocate & 0xffff);
9869 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9870 pinsn += 4;
9872 if (pinsn != entry + 12)
9873 elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9875 ok = true;
9879 if (!ok)
9881 if (!object->has_no_split_stack())
9882 object->error(_("failed to match split-stack sequence at "
9883 "section %u offset %0zx"),
9884 shndx, static_cast<size_t>(fnoffset));
9888 // Scan relocations for a section.
9890 template<int size, bool big_endian>
9891 void
9892 Target_powerpc<size, big_endian>::scan_relocs(
9893 Symbol_table* symtab,
9894 Layout* layout,
9895 Sized_relobj_file<size, big_endian>* object,
9896 unsigned int data_shndx,
9897 unsigned int sh_type,
9898 const unsigned char* prelocs,
9899 size_t reloc_count,
9900 Output_section* output_section,
9901 bool needs_special_offset_handling,
9902 size_t local_symbol_count,
9903 const unsigned char* plocal_symbols)
9905 typedef Target_powerpc<size, big_endian> Powerpc;
9906 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9907 Classify_reloc;
9909 if (!this->plt_localentry0_init_)
9911 bool plt_localentry0 = false;
9912 if (size == 64
9913 && this->abiversion() >= 2)
9915 if (parameters->options().user_set_plt_localentry())
9916 plt_localentry0 = parameters->options().plt_localentry();
9917 if (plt_localentry0
9918 && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9919 gold_warning(_("--plt-localentry is especially dangerous without "
9920 "ld.so support to detect ABI violations"));
9922 this->plt_localentry0_ = plt_localentry0;
9923 this->plt_localentry0_init_ = true;
9926 if (sh_type == elfcpp::SHT_REL)
9928 gold_error(_("%s: unsupported REL reloc section"),
9929 object->name().c_str());
9930 return;
9933 gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9934 symtab,
9935 layout,
9936 this,
9937 object,
9938 data_shndx,
9939 prelocs,
9940 reloc_count,
9941 output_section,
9942 needs_special_offset_handling,
9943 local_symbol_count,
9944 plocal_symbols);
9946 if (this->plt_localentry0_ && this->power10_relocs_)
9948 gold_warning(_("--plt-localentry is incompatible with "
9949 "power10 pc-relative code"));
9950 this->plt_localentry0_ = false;
9954 // Functor class for processing the global symbol table.
9955 // Removes symbols defined on discarded opd entries.
9957 template<bool big_endian>
9958 class Global_symbol_visitor_opd
9960 public:
9961 Global_symbol_visitor_opd()
9964 void
9965 operator()(Sized_symbol<64>* sym)
9967 if (sym->has_symtab_index()
9968 || sym->source() != Symbol::FROM_OBJECT
9969 || !sym->in_real_elf())
9970 return;
9972 if (sym->object()->is_dynamic())
9973 return;
9975 Powerpc_relobj<64, big_endian>* symobj
9976 = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9977 if (symobj->opd_shndx() == 0)
9978 return;
9980 bool is_ordinary;
9981 unsigned int shndx = sym->shndx(&is_ordinary);
9982 if (shndx == symobj->opd_shndx()
9983 && symobj->get_opd_discard(sym->value()))
9985 sym->set_undefined();
9986 sym->set_visibility(elfcpp::STV_DEFAULT);
9987 sym->set_is_defined_in_discarded_section();
9988 sym->set_symtab_index(-1U);
9993 template<int size, bool big_endian>
9994 void
9995 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9996 Layout* layout,
9997 Symbol_table* symtab)
9999 if (size == 64)
10001 Output_data_save_res<size, big_endian>* savres
10002 = new Output_data_save_res<size, big_endian>(symtab);
10003 this->savres_section_ = savres;
10004 layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
10005 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
10006 savres, ORDER_TEXT, false);
10010 // Sort linker created .got section first (for the header), then input
10011 // sections belonging to files using small model code.
10013 template<bool big_endian>
10014 class Sort_toc_sections
10016 const Output_section_data*
10017 small_got_section() const
10019 return (static_cast<Target_powerpc<64, big_endian>*>(
10020 parameters->sized_target<64, big_endian>())
10021 ->got_section(GOT_TYPE_SMALL));
10025 rank(const Output_section::Input_section& isec) const
10027 if (!isec.is_input_section())
10029 if (isec.output_section_data() == this->small_got_section())
10030 return 0;
10031 return 2;
10033 if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
10034 ->has_small_toc_reloc())
10035 return 1;
10036 return 3;
10039 public:
10040 bool
10041 operator()(const Output_section::Input_section& is1,
10042 const Output_section::Input_section& is2) const
10044 return rank(is1) < rank(is2);
10048 // Finalize the sections.
10050 template<int size, bool big_endian>
10051 void
10052 Target_powerpc<size, big_endian>::do_finalize_sections(
10053 Layout* layout,
10054 const Input_objects* input_objects,
10055 Symbol_table* symtab)
10057 if (parameters->doing_static_link())
10059 // At least some versions of glibc elf-init.o have a strong
10060 // reference to __rela_iplt marker syms. A weak ref would be
10061 // better..
10062 if (this->iplt_ != NULL)
10064 Reloc_section* rel = this->iplt_->rel_plt();
10065 symtab->define_in_output_data("__rela_iplt_start", NULL,
10066 Symbol_table::PREDEFINED, rel, 0, 0,
10067 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10068 elfcpp::STV_HIDDEN, 0, false, true);
10069 symtab->define_in_output_data("__rela_iplt_end", NULL,
10070 Symbol_table::PREDEFINED, rel, 0, 0,
10071 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10072 elfcpp::STV_HIDDEN, 0, true, true);
10074 else
10076 symtab->define_as_constant("__rela_iplt_start", NULL,
10077 Symbol_table::PREDEFINED, 0, 0,
10078 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10079 elfcpp::STV_HIDDEN, 0, true, false);
10080 symtab->define_as_constant("__rela_iplt_end", NULL,
10081 Symbol_table::PREDEFINED, 0, 0,
10082 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10083 elfcpp::STV_HIDDEN, 0, true, false);
10087 if (size == 64)
10089 typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
10090 symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
10092 if (!parameters->options().relocatable())
10094 this->define_save_restore_funcs(layout, symtab);
10096 // Annoyingly, we need to make these sections now whether or
10097 // not we need them. If we delay until do_relax then we
10098 // need to mess with the relaxation machinery checkpointing.
10099 this->got_section(symtab, layout, GOT_TYPE_STANDARD);
10100 this->make_brlt_section(layout);
10102 // FIXME, maybe. Here we could run through all the got
10103 // entries in the small got section, removing any duplicates
10104 // found in the big got section and renumbering offsets.
10106 if (parameters->options().toc_sort())
10108 Output_section* os = this->got_->output_section();
10109 if (os != NULL && os->input_sections().size() > 1)
10110 std::stable_sort(os->input_sections().begin(),
10111 os->input_sections().end(),
10112 Sort_toc_sections<big_endian>());
10117 // Fill in some more dynamic tags.
10118 Output_data_dynamic* odyn = layout->dynamic_data();
10119 if (odyn != NULL)
10121 const Reloc_section* rel_plt = (this->plt_ == NULL
10122 ? NULL
10123 : this->plt_->rel_plt());
10124 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
10125 this->rela_dyn_, true, size == 32);
10127 if (size == 32)
10129 if (this->got_ != NULL)
10131 this->got_->finalize_data_size();
10132 odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
10133 this->got_, this->got_->g_o_t());
10135 if (this->has_tls_get_addr_opt_)
10136 odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
10138 else
10140 if (this->glink_ != NULL)
10142 this->glink_->finalize_data_size();
10143 odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
10144 this->glink_,
10145 (this->glink_->pltresolve_size()
10146 - 32));
10148 if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
10149 odyn->add_constant(elfcpp::DT_PPC64_OPT,
10150 ((this->has_localentry0_
10151 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
10152 | (this->has_tls_get_addr_opt_
10153 ? elfcpp::PPC64_OPT_TLS : 0)));
10157 // Emit any relocs we saved in an attempt to avoid generating COPY
10158 // relocs.
10159 if (this->copy_relocs_.any_saved_relocs())
10160 this->copy_relocs_.emit(this->rela_dyn_section(layout));
10162 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
10163 p != input_objects->relobj_end();
10164 ++p)
10166 Powerpc_relobj<size, big_endian>* ppc_relobj
10167 = static_cast<Powerpc_relobj<size, big_endian>*>(*p);
10168 if (ppc_relobj->attributes_section_data())
10169 this->merge_object_attributes(ppc_relobj,
10170 ppc_relobj->attributes_section_data());
10172 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
10173 p != input_objects->dynobj_end();
10174 ++p)
10176 Powerpc_dynobj<size, big_endian>* ppc_dynobj
10177 = static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
10178 if (ppc_dynobj->attributes_section_data())
10179 this->merge_object_attributes(ppc_dynobj,
10180 ppc_dynobj->attributes_section_data());
10183 // Create a .gnu.attributes section if we have merged any attributes
10184 // from inputs.
10185 if (this->attributes_section_data_ != NULL
10186 && this->attributes_section_data_->size() != 0)
10188 Output_attributes_section_data* attributes_section
10189 = new Output_attributes_section_data(*this->attributes_section_data_);
10190 layout->add_output_section_data(".gnu.attributes",
10191 elfcpp::SHT_GNU_ATTRIBUTES, 0,
10192 attributes_section, ORDER_INVALID, false);
10196 // Merge object attributes from input file called NAME with those of the
10197 // output. The input object attributes are in the object pointed by PASD.
10199 template<int size, bool big_endian>
10200 void
10201 Target_powerpc<size, big_endian>::merge_object_attributes(
10202 const Object* obj,
10203 const Attributes_section_data* pasd)
10205 // Return if there is no attributes section data.
10206 if (pasd == NULL)
10207 return;
10209 // Create output object attributes.
10210 if (this->attributes_section_data_ == NULL)
10211 this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
10213 const int vendor = Object_attribute::OBJ_ATTR_GNU;
10214 const Object_attribute* in_attr = pasd->known_attributes(vendor);
10215 Object_attribute* out_attr
10216 = this->attributes_section_data_->known_attributes(vendor);
10218 const char* name = obj->name().c_str();
10219 const char* err;
10220 const char* first;
10221 const char* second;
10222 int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10223 int in_fp = in_attr[tag].int_value() & 0xf;
10224 int out_fp = out_attr[tag].int_value() & 0xf;
10225 bool warn_only = obj->is_dynamic();
10226 if (in_fp != out_fp)
10228 err = NULL;
10229 if ((in_fp & 3) == 0)
10231 else if ((out_fp & 3) == 0)
10233 if (!warn_only)
10235 out_fp |= in_fp & 3;
10236 out_attr[tag].set_int_value(out_fp);
10237 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10238 this->last_fp_ = name;
10241 else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10243 err = N_("%s uses hard float, %s uses soft float");
10244 first = this->last_fp_;
10245 second = name;
10247 else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10249 err = N_("%s uses hard float, %s uses soft float");
10250 first = name;
10251 second = this->last_fp_;
10253 else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10255 err = N_("%s uses double-precision hard float, "
10256 "%s uses single-precision hard float");
10257 first = this->last_fp_;
10258 second = name;
10260 else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10262 err = N_("%s uses double-precision hard float, "
10263 "%s uses single-precision hard float");
10264 first = name;
10265 second = this->last_fp_;
10268 if (err || (in_fp & 0xc) == 0)
10270 else if ((out_fp & 0xc) == 0)
10272 if (!warn_only)
10274 out_fp |= in_fp & 0xc;
10275 out_attr[tag].set_int_value(out_fp);
10276 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10277 this->last_ld_ = name;
10280 else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10282 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10283 first = name;
10284 second = this->last_ld_;
10286 else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10288 err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10289 first = this->last_ld_;
10290 second = name;
10292 else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10294 err = N_("%s uses IBM long double, %s uses IEEE long double");
10295 first = this->last_ld_;
10296 second = name;
10298 else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10300 err = N_("%s uses IBM long double, %s uses IEEE long double");
10301 first = name;
10302 second = this->last_ld_;
10305 if (err)
10307 if (parameters->options().warn_mismatch())
10309 if (warn_only)
10310 gold_warning(_(err), first, second);
10311 else
10312 gold_error(_(err), first, second);
10314 // Arrange for this attribute to be deleted. It's better to
10315 // say "don't know" about a file than to wrongly claim compliance.
10316 if (!warn_only)
10317 out_attr[tag].set_type(0);
10321 if (size == 32)
10323 tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10324 int in_vec = in_attr[tag].int_value() & 3;
10325 int out_vec = out_attr[tag].int_value() & 3;
10326 if (in_vec != out_vec)
10328 err = NULL;
10329 if (in_vec == 0)
10331 else if (out_vec == 0)
10333 out_vec = in_vec;
10334 out_attr[tag].set_int_value(out_vec);
10335 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10336 this->last_vec_ = name;
10338 // For now, allow generic to transition to AltiVec or SPE
10339 // without a warning. If GCC marked files with their stack
10340 // alignment and used don't-care markings for files which are
10341 // not affected by the vector ABI, we could warn about this
10342 // case too. */
10343 else if (in_vec == 1)
10345 else if (out_vec == 1)
10347 out_vec = in_vec;
10348 out_attr[tag].set_int_value(out_vec);
10349 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10350 this->last_vec_ = name;
10352 else if (out_vec < in_vec)
10354 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10355 first = this->last_vec_;
10356 second = name;
10358 else if (out_vec > in_vec)
10360 err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10361 first = name;
10362 second = this->last_vec_;
10364 if (err)
10366 if (parameters->options().warn_mismatch())
10367 gold_error(_(err), first, second);
10368 out_attr[tag].set_type(0);
10372 tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10373 int in_struct = in_attr[tag].int_value() & 3;
10374 int out_struct = out_attr[tag].int_value() & 3;
10375 if (in_struct != out_struct)
10377 err = NULL;
10378 if (in_struct == 0 || in_struct == 3)
10380 else if (out_struct == 0)
10382 out_struct = in_struct;
10383 out_attr[tag].set_int_value(out_struct);
10384 out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10385 this->last_struct_ = name;
10387 else if (out_struct < in_struct)
10389 err = N_("%s uses r3/r4 for small structure returns, "
10390 "%s uses memory");
10391 first = this->last_struct_;
10392 second = name;
10394 else if (out_struct > in_struct)
10396 err = N_("%s uses r3/r4 for small structure returns, "
10397 "%s uses memory");
10398 first = name;
10399 second = this->last_struct_;
10401 if (err)
10403 if (parameters->options().warn_mismatch())
10404 gold_error(_(err), first, second);
10405 out_attr[tag].set_type(0);
10410 // Merge Tag_compatibility attributes and any common GNU ones.
10411 this->attributes_section_data_->merge(name, pasd);
10414 // Emit any saved relocs, and mark toc entries using any of these
10415 // relocs as not optimizable.
10417 template<int sh_type, int size, bool big_endian>
10418 void
10419 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10420 Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10422 if (size == 64
10423 && parameters->options().toc_optimize())
10425 for (typename Copy_relocs<sh_type, size, big_endian>::
10426 Copy_reloc_entries::iterator p = this->entries_.begin();
10427 p != this->entries_.end();
10428 ++p)
10430 typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10431 entry = *p;
10433 // If the symbol is no longer defined in a dynamic object,
10434 // then we emitted a COPY relocation. If it is still
10435 // dynamic then we'll need dynamic relocations and thus
10436 // can't optimize toc entries.
10437 if (entry.sym_->is_from_dynobj())
10439 Powerpc_relobj<size, big_endian>* ppc_object
10440 = static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10441 if (entry.shndx_ == ppc_object->toc_shndx())
10442 ppc_object->set_no_toc_opt(entry.address_);
10447 Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10450 // Return the value to use for a branch relocation.
10452 template<int size, bool big_endian>
10453 bool
10454 Target_powerpc<size, big_endian>::symval_for_branch(
10455 const Symbol_table* symtab,
10456 const Sized_symbol<size>* gsym,
10457 Powerpc_relobj<size, big_endian>* object,
10458 Address *value,
10459 unsigned int *dest_shndx)
10461 if (size == 32 || this->abiversion() >= 2)
10462 gold_unreachable();
10463 *dest_shndx = 0;
10465 // If the symbol is defined in an opd section, ie. is a function
10466 // descriptor, use the function descriptor code entry address
10467 Powerpc_relobj<size, big_endian>* symobj = object;
10468 if (gsym != NULL
10469 && (gsym->source() != Symbol::FROM_OBJECT
10470 || gsym->object()->is_dynamic()))
10471 return true;
10472 if (gsym != NULL)
10473 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10474 unsigned int shndx = symobj->opd_shndx();
10475 if (shndx == 0)
10476 return true;
10477 Address opd_addr = symobj->get_output_section_offset(shndx);
10478 if (opd_addr == invalid_address)
10479 return true;
10480 opd_addr += symobj->output_section_address(shndx);
10481 if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10483 Address sec_off;
10484 *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10485 if (symtab->is_section_folded(symobj, *dest_shndx))
10487 Section_id folded
10488 = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10489 symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10490 *dest_shndx = folded.second;
10492 Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10493 if (sec_addr == invalid_address)
10494 return false;
10496 sec_addr += symobj->output_section(*dest_shndx)->address();
10497 *value = sec_addr + sec_off;
10499 return true;
10502 template<int size>
10503 static bool
10504 relative_value_is_known(const Sized_symbol<size>* gsym)
10506 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10507 return false;
10509 if (gsym->is_from_dynobj()
10510 || gsym->is_undefined()
10511 || gsym->is_preemptible())
10512 return false;
10514 if (gsym->is_absolute())
10515 return !parameters->options().output_is_position_independent();
10517 return true;
10520 template<int size>
10521 static bool
10522 relative_value_is_known(const Symbol_value<size>* psymval)
10524 if (psymval->is_ifunc_symbol())
10525 return false;
10527 bool is_ordinary;
10528 unsigned int shndx = psymval->input_shndx(&is_ordinary);
10530 return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10533 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10534 // pld ra,symbol@got@pcrel
10535 // load/store rt,0(ra)
10536 // or
10537 // pla ra,symbol@pcrel
10538 // load/store rt,0(ra)
10539 // may be translated to
10540 // pload/pstore rt,symbol@pcrel
10541 // nop.
10542 // This function returns true if the optimization is possible, placing
10543 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10545 // On entry to this function, the linker has already determined that
10546 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10547 // while *PINSN2 is the second instruction.
10549 inline bool
10550 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10552 uint32_t insn2 = *pinsn2 >> 32;
10553 uint64_t i1new;
10555 // Check that regs match.
10556 if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10557 return false;
10559 switch ((insn2 >> 26) & 63)
10561 default:
10562 return false;
10564 case 32: // lwz
10565 case 34: // lbz
10566 case 36: // stw
10567 case 38: // stb
10568 case 40: // lhz
10569 case 42: // lha
10570 case 44: // sth
10571 case 48: // lfs
10572 case 50: // lfd
10573 case 52: // stfs
10574 case 54: // stfd
10575 // These are the PMLS cases, where we just need to tack a prefix
10576 // on the insn. Check that the D field is zero.
10577 if ((insn2 & 0xffff) != 0)
10578 return false;
10579 i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10580 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10581 break;
10583 case 58: // lwa, ld
10584 if ((insn2 & 0xfffd) != 0)
10585 return false;
10586 i1new = ((1ULL << 58) | (1ULL << 52)
10587 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10588 | (insn2 & (31ULL << 21)));
10589 break;
10591 case 57: // lxsd, lxssp
10592 if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10593 return false;
10594 i1new = ((1ULL << 58) | (1ULL << 52)
10595 | ((40ULL | (insn2 & 3)) << 26)
10596 | (insn2 & (31ULL << 21)));
10597 break;
10599 case 61: // stxsd, stxssp, lxv, stxv
10600 if ((insn2 & 3) == 0)
10601 return false;
10602 else if ((insn2 & 3) >= 2)
10604 if ((insn2 & 0xfffc) != 0)
10605 return false;
10606 i1new = ((1ULL << 58) | (1ULL << 52)
10607 | ((44ULL | (insn2 & 3)) << 26)
10608 | (insn2 & (31ULL << 21)));
10610 else
10612 if ((insn2 & 0xfff0) != 0)
10613 return false;
10614 i1new = ((1ULL << 58) | (1ULL << 52)
10615 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10616 | (insn2 & (31ULL << 21)));
10618 break;
10620 case 56: // lq
10621 if ((insn2 & 0xffff) != 0)
10622 return false;
10623 i1new = ((1ULL << 58) | (1ULL << 52)
10624 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10625 break;
10627 case 62: // std, stq
10628 if ((insn2 & 0xfffd) != 0)
10629 return false;
10630 i1new = ((1ULL << 58) | (1ULL << 52)
10631 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10632 | (insn2 & (31ULL << 21)));
10633 break;
10636 *pinsn1 = i1new;
10637 *pinsn2 = (uint64_t) nop << 32;
10638 return true;
10641 // Perform a relocation.
10643 template<int size, bool big_endian>
10644 inline bool
10645 Target_powerpc<size, big_endian>::Relocate::relocate(
10646 const Relocate_info<size, big_endian>* relinfo,
10647 unsigned int,
10648 Target_powerpc* target,
10649 Output_section* os,
10650 size_t relnum,
10651 const unsigned char* preloc,
10652 const Sized_symbol<size>* gsym,
10653 const Symbol_value<size>* psymval,
10654 unsigned char* view,
10655 Address address,
10656 section_size_type view_size)
10658 typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10659 typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10660 typedef typename elfcpp::Rela<size, big_endian> Reltype;
10662 if (view == NULL)
10663 return true;
10665 if (target->replace_tls_get_addr(gsym))
10666 gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10668 const elfcpp::Rela<size, big_endian> rela(preloc);
10669 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10670 Powerpc_relobj<size, big_endian>* const object
10671 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10672 switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10674 case Track_tls::NOT_EXPECTED:
10675 // No warning. This will result in really old code without tls
10676 // marker relocs being mis-optimised, but there shouldn't be too
10677 // much of that code around. The problem with warning is that
10678 // glibc and libphobos both construct direct calls to
10679 // __tls_get_addr in a way that is harmless.
10680 break;
10681 case Track_tls::EXPECTED:
10682 // We have already complained.
10683 break;
10684 case Track_tls::SKIP:
10685 if (is_plt16_reloc<size>(r_type)
10686 || r_type == elfcpp::R_POWERPC_PLTSEQ
10687 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10689 Insn* iview = reinterpret_cast<Insn*>(view);
10690 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10692 else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10694 Insn* iview = reinterpret_cast<Insn*>(view);
10695 elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10697 else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10698 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10700 Insn* iview = reinterpret_cast<Insn*>(view);
10701 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10702 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10704 return true;
10705 case Track_tls::NORMAL:
10706 break;
10709 // Offset from start of insn to d-field reloc.
10710 const int d_offset = big_endian ? 2 : 0;
10712 Address value = 0;
10713 bool has_stub_value = false;
10714 bool localentry0 = false;
10715 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10716 bool pltcall_to_direct = false;
10718 if (is_plt16_reloc<size>(r_type)
10719 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10720 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10721 || r_type == elfcpp::R_POWERPC_PLTSEQ
10722 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
10723 || r_type == elfcpp::R_POWERPC_PLTCALL
10724 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10726 // It would be possible to replace inline plt calls with direct
10727 // calls if the PLTCALL is in range. The only difficulty is
10728 // that the decision depends on the PLTCALL reloc, and we don't
10729 // know the address of that instruction when processing others
10730 // in the sequence. So the decision needs to be made in
10731 // do_relax().
10732 pltcall_to_direct = !(gsym != NULL
10733 ? gsym->has_plt_offset()
10734 : object->local_has_plt_offset(r_sym));
10736 else if ((gsym != NULL
10737 ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10738 : psymval->is_ifunc_symbol() && object->local_has_plt_offset(r_sym))
10739 && !is_got_reloc(r_type)
10740 && (!psymval->is_ifunc_symbol()
10741 || Scan::reloc_needs_plt_for_ifunc(target, object, r_type,
10742 false)))
10744 if (size == 64
10745 && gsym != NULL
10746 && target->abiversion() >= 2
10747 && !parameters->options().output_is_position_independent()
10748 && !is_branch_reloc<size>(r_type))
10750 Address off = target->glink_section()->find_global_entry(gsym);
10751 if (off != invalid_address)
10753 value = target->glink_section()->global_entry_address() + off;
10754 has_stub_value = true;
10757 else
10759 Stub_table<size, big_endian>* stub_table = NULL;
10760 if (target->stub_tables().size() == 1)
10761 stub_table = target->stub_tables()[0];
10762 if (stub_table == NULL
10763 && !(size == 32
10764 && gsym != NULL
10765 && !parameters->options().output_is_position_independent()
10766 && !is_branch_reloc<size>(r_type)))
10767 stub_table = object->stub_table(relinfo->data_shndx);
10768 if (stub_table == NULL)
10770 // This is a ref from a data section to an ifunc symbol,
10771 // or a non-branch reloc for which we always want to use
10772 // one set of stubs for resolving function addresses.
10773 if (target->stub_tables().size() != 0)
10774 stub_table = target->stub_tables()[0];
10776 if (stub_table != NULL)
10778 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10779 if (gsym != NULL)
10780 ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10781 rela.get_r_addend());
10782 else
10783 ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10784 rela.get_r_addend());
10785 if (ent != NULL)
10787 value = stub_table->stub_address() + ent->off_;
10788 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10789 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10790 size_t reloc_count = shdr.get_sh_size() / reloc_size;
10791 if (size == 64)
10793 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10795 if (!ent->notoc_)
10796 value += ent->p9off_;
10798 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10799 value += ent->p9off_;
10800 else
10801 value += ent->tocoff_;
10803 if (size == 64
10804 && ent->r2save_
10805 && !(gsym != NULL
10806 && target->is_tls_get_addr_opt(gsym)))
10808 if (r_type == elfcpp::R_PPC64_REL24_NOTOC
10809 || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10811 if (!(target->power10_stubs()
10812 && target->power10_stubs_auto()))
10813 value += 4;
10815 else if (relnum < reloc_count - 1)
10817 Reltype next_rela(preloc + reloc_size);
10818 if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10819 == elfcpp::R_PPC64_TOCSAVE
10820 && (next_rela.get_r_offset()
10821 == rela.get_r_offset() + 4))
10822 value += 4;
10825 localentry0 = ent->localentry0_;
10826 has_stub_value = true;
10830 // We don't care too much about bogus debug references to
10831 // non-local functions, but otherwise there had better be a plt
10832 // call stub or global entry stub as appropriate.
10833 gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10836 if (!pltcall_to_direct && (is_plt16_reloc<size>(r_type)
10837 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10838 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10840 const Output_data_plt_powerpc<size, big_endian>* plt;
10841 if (gsym)
10842 value = target->plt_off(gsym, &plt);
10843 else
10844 value = target->plt_off(object, r_sym, &plt);
10845 value += plt->address();
10847 if (size == 64)
10849 if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10850 && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10851 value -= target->toc_pointer();
10853 else if (parameters->options().output_is_position_independent())
10855 if (rela.get_r_addend() >= 32768)
10857 unsigned int got2 = object->got2_shndx();
10858 value -= (object->get_output_section_offset(got2)
10859 + object->output_section(got2)->address()
10860 + rela.get_r_addend());
10862 else
10863 value -= target->toc_pointer();
10866 else if (pltcall_to_direct
10867 && (is_plt16_reloc<size>(r_type)
10868 || r_type == elfcpp::R_POWERPC_PLTSEQ
10869 || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10871 Insn* iview = reinterpret_cast<Insn*>(view);
10872 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10873 r_type = elfcpp::R_POWERPC_NONE;
10875 else if (pltcall_to_direct
10876 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10877 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10879 Insn* iview = reinterpret_cast<Insn*>(view);
10880 elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10881 elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10882 r_type = elfcpp::R_POWERPC_NONE;
10884 else if (is_got_reloc(r_type))
10886 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10887 Got_type got_type = ((size == 32
10888 || r_type == elfcpp::R_POWERPC_GOT16
10889 || r_type == elfcpp::R_PPC64_GOT16_DS)
10890 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10891 if (gsym != NULL)
10892 value = gsym->got_offset(got_type, addend);
10893 else
10894 value = object->local_got_offset(r_sym, got_type, addend);
10895 if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10896 value += target->got_section(got_type)->address();
10897 else
10898 value -= target->got_base_offset(got_type);
10900 else if (r_type == elfcpp::R_PPC64_TOC)
10902 value = target->toc_pointer();
10904 else if (gsym != NULL
10905 && (r_type == elfcpp::R_POWERPC_REL24
10906 || r_type == elfcpp::R_PPC_PLTREL24)
10907 && has_stub_value)
10909 if (size == 64)
10911 typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10912 Valtype* wv = reinterpret_cast<Valtype*>(view);
10913 bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10914 if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10916 Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10917 Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10918 if ((insn & 1) != 0
10919 && (insn2 == nop
10920 || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10922 elfcpp::Swap<32, big_endian>::
10923 writeval(wv + 1, ld_2_1 + target->stk_toc());
10924 can_plt_call = true;
10927 if (!can_plt_call)
10929 // If we don't have a branch and link followed by a nop,
10930 // we can't go via the plt because there is no place to
10931 // put a toc restoring instruction.
10932 // Unless we know we won't be returning.
10933 if (strcmp(gsym->name(), "__libc_start_main") == 0)
10934 can_plt_call = true;
10936 if (!can_plt_call)
10938 // g++ as of 20130507 emits self-calls without a
10939 // following nop. This is arguably wrong since we have
10940 // conflicting information. On the one hand a global
10941 // symbol and on the other a local call sequence, but
10942 // don't error for this special case.
10943 // It isn't possible to cheaply verify we have exactly
10944 // such a call. Allow all calls to the same section.
10945 bool ok = false;
10946 Address code = value;
10947 if (gsym->source() == Symbol::FROM_OBJECT
10948 && gsym->object() == object)
10950 unsigned int dest_shndx = 0;
10951 if (target->abiversion() < 2)
10953 Address addend = rela.get_r_addend();
10954 code = psymval->value(object, addend);
10955 target->symval_for_branch(relinfo->symtab, gsym, object,
10956 &code, &dest_shndx);
10958 bool is_ordinary;
10959 if (dest_shndx == 0)
10960 dest_shndx = gsym->shndx(&is_ordinary);
10961 ok = dest_shndx == relinfo->data_shndx;
10963 if (!ok)
10965 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10966 _("call lacks nop, can't restore toc; "
10967 "recompile with -fPIC"));
10968 value = code;
10973 else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10974 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10975 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10976 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10977 || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10979 // First instruction of a global dynamic sequence, arg setup insn.
10980 bool final = gsym == NULL || gsym->final_value_is_known();
10981 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10982 Got_type got_type = ((size == 32
10983 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10984 ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10985 if (tls_type == tls::TLSOPT_NONE)
10986 got_type = Got_type(got_type | GOT_TYPE_TLSGD);
10987 else if (tls_type == tls::TLSOPT_TO_IE)
10988 got_type = Got_type(got_type | GOT_TYPE_TPREL);
10989 if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
10991 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10992 if (gsym != NULL)
10993 value = gsym->got_offset(got_type, addend);
10994 else
10995 value = object->local_got_offset(r_sym, got_type, addend);
10996 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10997 value += target->got_section(got_type)->address();
10998 else
10999 value -= target->got_base_offset(got_type);
11001 if (tls_type == tls::TLSOPT_TO_IE)
11003 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11005 Insn* iview = reinterpret_cast<Insn*>(view);
11006 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11007 pinsn <<= 32;
11008 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11009 // pla -> pld
11010 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
11011 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11012 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11013 pinsn & 0xffffffff);
11014 r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
11016 else
11018 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11019 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11021 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11022 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11023 insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
11024 if (size == 32)
11025 insn |= 32 << 26; // lwz
11026 else
11027 insn |= 58 << 26; // ld
11028 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11030 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
11031 - elfcpp::R_POWERPC_GOT_TLSGD16);
11034 else if (tls_type == tls::TLSOPT_TO_LE)
11036 if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11038 Insn* iview = reinterpret_cast<Insn*>(view);
11039 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11040 pinsn <<= 32;
11041 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11042 // pla pcrel -> paddi r13
11043 pinsn += (-1ULL << 52) + (13ULL << 16);
11044 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11045 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11046 pinsn & 0xffffffff);
11047 r_type = elfcpp::R_PPC64_TPREL34;
11048 value = psymval->value(object, rela.get_r_addend());
11050 else
11052 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11053 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11055 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11056 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11057 insn &= (1 << 26) - (1 << 21); // extract rt
11058 if (size == 32)
11059 insn |= addis_0_2;
11060 else
11061 insn |= addis_0_13;
11062 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11063 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11064 value = psymval->value(object, rela.get_r_addend());
11066 else
11068 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11069 Insn insn = nop;
11070 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11071 r_type = elfcpp::R_POWERPC_NONE;
11076 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11077 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11078 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
11079 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
11080 || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11082 // First instruction of a local dynamic sequence, arg setup insn.
11083 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11084 if (tls_type == tls::TLSOPT_NONE)
11086 value = target->tlsld_got_offset();
11087 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11088 value += target->got_section(GOT_TYPE_SMALL)->address();
11089 else
11090 value -= target->got_base_offset(GOT_TYPE_SMALL);
11092 else
11094 gold_assert(tls_type == tls::TLSOPT_TO_LE);
11095 if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11097 Insn* iview = reinterpret_cast<Insn*>(view);
11098 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11099 pinsn <<= 32;
11100 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11101 // pla pcrel -> paddi r13
11102 pinsn += (-1ULL << 52) + (13ULL << 16);
11103 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11104 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11105 pinsn & 0xffffffff);
11106 r_type = elfcpp::R_PPC64_TPREL34;
11107 value = dtp_offset;
11109 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11110 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
11112 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11113 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11114 insn &= (1 << 26) - (1 << 21); // extract rt
11115 if (size == 32)
11116 insn |= addis_0_2;
11117 else
11118 insn |= addis_0_13;
11119 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11120 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11121 value = dtp_offset;
11123 else
11125 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11126 Insn insn = nop;
11127 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11128 r_type = elfcpp::R_POWERPC_NONE;
11132 else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
11133 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
11134 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
11135 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
11136 || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11138 // Accesses relative to a local dynamic sequence address,
11139 // no optimisation here.
11140 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11141 Got_type got_type = ((size == 32
11142 || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
11143 ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
11144 if (gsym != NULL)
11145 value = gsym->got_offset(got_type, addend);
11146 else
11147 value = object->local_got_offset(r_sym, got_type, addend);
11148 if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11149 value += target->got_section(got_type)->address();
11150 else
11151 value -= target->got_base_offset(got_type);
11153 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11154 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11155 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
11156 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
11157 || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11159 // First instruction of initial exec sequence.
11160 bool final = gsym == NULL || gsym->final_value_is_known();
11161 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11162 if (tls_type == tls::TLSOPT_NONE)
11164 uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11165 Got_type got_type = ((size == 32
11166 || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
11167 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
11168 if (gsym != NULL)
11169 value = gsym->got_offset(got_type, addend);
11170 else
11171 value = object->local_got_offset(r_sym, got_type, addend);
11172 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11173 value += target->got_section(got_type)->address();
11174 else
11175 value -= target->got_base_offset(got_type);
11177 else
11179 gold_assert(tls_type == tls::TLSOPT_TO_LE);
11180 if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11182 Insn* iview = reinterpret_cast<Insn*>(view);
11183 uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11184 pinsn <<= 32;
11185 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11186 // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
11187 pinsn += ((2ULL << 56) + (-1ULL << 52)
11188 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
11189 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11190 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11191 pinsn & 0xffffffff);
11192 r_type = elfcpp::R_PPC64_TPREL34;
11193 value = psymval->value(object, rela.get_r_addend());
11195 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11196 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
11198 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11199 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11200 insn &= (1 << 26) - (1 << 21); // extract rt from ld
11201 if (size == 32)
11202 insn |= addis_0_2;
11203 else
11204 insn |= addis_0_13;
11205 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11206 r_type = elfcpp::R_POWERPC_TPREL16_HA;
11207 value = psymval->value(object, rela.get_r_addend());
11209 else
11211 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11212 Insn insn = nop;
11213 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11214 r_type = elfcpp::R_POWERPC_NONE;
11218 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11219 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11221 // Second instruction of a global dynamic sequence,
11222 // the __tls_get_addr call
11223 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11224 bool final = gsym == NULL || gsym->final_value_is_known();
11225 tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
11226 if (tls_type != tls::TLSOPT_NONE)
11228 if (tls_type == tls::TLSOPT_TO_IE)
11230 Insn* iview = reinterpret_cast<Insn*>(view);
11231 Insn insn = add_3_3_13;
11232 if (size == 32)
11233 insn = add_3_3_2;
11234 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11235 r_type = elfcpp::R_POWERPC_NONE;
11237 else
11239 bool is_pcrel = false;
11240 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11241 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11242 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11243 if (relnum < reloc_count - 1)
11245 Reltype next_rela(preloc + reloc_size);
11246 unsigned int r_type2
11247 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11248 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11249 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11250 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11251 && next_rela.get_r_offset() == rela.get_r_offset())
11252 is_pcrel = true;
11254 Insn* iview = reinterpret_cast<Insn*>(view);
11255 if (is_pcrel)
11257 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11258 r_type = elfcpp::R_POWERPC_NONE;
11260 else
11262 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11263 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11264 view += d_offset;
11265 value = psymval->value(object, rela.get_r_addend());
11268 this->skip_next_tls_get_addr_call();
11271 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11272 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11274 // Second instruction of a local dynamic sequence,
11275 // the __tls_get_addr call
11276 this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11277 tls::Tls_optimization tls_type = target->optimize_tls_ld();
11278 if (tls_type == tls::TLSOPT_TO_LE)
11280 bool is_pcrel = false;
11281 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11282 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11283 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11284 if (relnum < reloc_count - 1)
11286 Reltype next_rela(preloc + reloc_size);
11287 unsigned int r_type2
11288 = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11289 if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11290 || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11291 || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11292 && next_rela.get_r_offset() == rela.get_r_offset())
11293 is_pcrel = true;
11295 Insn* iview = reinterpret_cast<Insn*>(view);
11296 if (is_pcrel)
11298 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11299 r_type = elfcpp::R_POWERPC_NONE;
11301 else
11303 elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11304 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11305 view += d_offset;
11306 value = dtp_offset;
11308 this->skip_next_tls_get_addr_call();
11311 else if (r_type == elfcpp::R_POWERPC_TLS)
11313 // Second instruction of an initial exec sequence
11314 bool final = gsym == NULL || gsym->final_value_is_known();
11315 tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11316 if (tls_type == tls::TLSOPT_TO_LE)
11318 Address roff = rela.get_r_offset() & 3;
11319 Insn* iview = reinterpret_cast<Insn*>(view - roff);
11320 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11321 unsigned int reg = size == 32 ? 2 : 13;
11322 insn = at_tls_transform(insn, reg);
11323 gold_assert(insn != 0);
11324 if (roff == 0)
11326 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11327 r_type = elfcpp::R_POWERPC_TPREL16_LO;
11328 view += d_offset;
11329 value = psymval->value(object, rela.get_r_addend());
11331 else if (roff == 1)
11333 // For pcrel IE to LE we already have the full offset
11334 // and thus don't need an addi here. A nop or mr will do.
11335 if ((insn & (0x3f << 26)) == 14 << 26)
11337 // Extract regs from addi rt,ra,si.
11338 unsigned int rt = (insn >> 21) & 0x1f;
11339 unsigned int ra = (insn >> 16) & 0x1f;
11340 if (rt == ra)
11341 insn = nop;
11342 else
11344 // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11345 insn = (rt << 16) | (ra << 21) | (ra << 11);
11346 insn |= (31u << 26) | (444u << 1);
11349 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11350 r_type = elfcpp::R_POWERPC_NONE;
11354 else if (!has_stub_value)
11356 if (pltcall_to_direct && (r_type == elfcpp::R_POWERPC_PLTCALL
11357 || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11359 // PLTCALL without plt entry => convert to direct call
11360 Insn* iview = reinterpret_cast<Insn*>(view);
11361 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11362 insn = (insn & 1) | b;
11363 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11364 if (size == 32)
11365 r_type = elfcpp::R_PPC_PLTREL24;
11366 else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11367 r_type = elfcpp::R_PPC64_REL24_NOTOC;
11368 else
11369 r_type = elfcpp::R_POWERPC_REL24;
11371 Address addend = 0;
11372 if (!(size == 32
11373 && (r_type == elfcpp::R_PPC_PLTREL24
11374 || r_type == elfcpp::R_POWERPC_PLT16_LO
11375 || r_type == elfcpp::R_POWERPC_PLT16_HI
11376 || r_type == elfcpp::R_POWERPC_PLT16_HA)))
11377 addend = rela.get_r_addend();
11378 value = psymval->value(object, addend);
11379 unsigned int local_ent = 0;
11380 if (size == 64 && is_branch_reloc<size>(r_type))
11382 if (target->abiversion() >= 2)
11384 if (gsym != NULL)
11385 local_ent = object->ppc64_local_entry_offset(gsym);
11386 else
11387 local_ent = object->ppc64_local_entry_offset(r_sym);
11389 else
11391 unsigned int dest_shndx;
11392 target->symval_for_branch(relinfo->symtab, gsym, object,
11393 &value, &dest_shndx);
11396 Address max_branch = max_branch_delta<size>(r_type);
11397 if (max_branch != 0
11398 && (value + local_ent - address + max_branch >= 2 * max_branch
11399 || (size == 64
11400 && (r_type == elfcpp::R_PPC64_REL24_NOTOC
11401 || r_type == elfcpp::R_PPC64_REL24_NOTOC)
11402 && (gsym != NULL
11403 ? object->ppc64_needs_toc(gsym)
11404 : object->ppc64_needs_toc(r_sym)))))
11406 Stub_table<size, big_endian>* stub_table
11407 = object->stub_table(relinfo->data_shndx);
11408 if (stub_table != NULL)
11410 const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11411 = stub_table->find_long_branch_entry(value);
11412 if (ent != NULL)
11414 if (ent->save_res_)
11415 value = (value - target->savres_section()->address()
11416 + stub_table->stub_address()
11417 + stub_table->plt_size()
11418 + stub_table->branch_size());
11419 else
11421 value = (stub_table->stub_address()
11422 + stub_table->plt_size()
11423 + ent->off_);
11424 if (size == 64)
11426 if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
11428 if (!ent->notoc_)
11429 value += ent->p9off_;
11431 else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
11432 value += ent->p9off_;
11433 else
11434 value += ent->tocoff_;
11437 has_stub_value = true;
11441 if (!has_stub_value)
11442 value += local_ent;
11445 switch (r_type)
11447 case elfcpp::R_PPC64_REL24_NOTOC:
11448 if (size == 32)
11449 break;
11450 // Fall through.
11451 case elfcpp::R_PPC64_REL24_P9NOTOC:
11452 case elfcpp::R_PPC64_REL64:
11453 case elfcpp::R_POWERPC_REL32:
11454 case elfcpp::R_POWERPC_REL24:
11455 case elfcpp::R_PPC_PLTREL24:
11456 case elfcpp::R_PPC_LOCAL24PC:
11457 case elfcpp::R_POWERPC_REL16:
11458 case elfcpp::R_POWERPC_REL16_LO:
11459 case elfcpp::R_POWERPC_REL16_HI:
11460 case elfcpp::R_POWERPC_REL16_HA:
11461 case elfcpp::R_POWERPC_REL16DX_HA:
11462 case elfcpp::R_PPC64_REL16_HIGH:
11463 case elfcpp::R_PPC64_REL16_HIGHA:
11464 case elfcpp::R_PPC64_REL16_HIGHER:
11465 case elfcpp::R_PPC64_REL16_HIGHERA:
11466 case elfcpp::R_PPC64_REL16_HIGHEST:
11467 case elfcpp::R_PPC64_REL16_HIGHESTA:
11468 case elfcpp::R_POWERPC_REL14:
11469 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11470 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11471 case elfcpp::R_PPC64_PCREL34:
11472 case elfcpp::R_PPC64_GOT_PCREL34:
11473 case elfcpp::R_PPC64_PLT_PCREL34:
11474 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11475 case elfcpp::R_PPC64_PCREL28:
11476 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11477 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11478 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11479 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11480 case elfcpp::R_PPC64_REL16_HIGHER34:
11481 case elfcpp::R_PPC64_REL16_HIGHERA34:
11482 case elfcpp::R_PPC64_REL16_HIGHEST34:
11483 case elfcpp::R_PPC64_REL16_HIGHESTA34:
11484 value -= address;
11485 break;
11487 case elfcpp::R_PPC64_TOC16:
11488 case elfcpp::R_PPC64_TOC16_LO:
11489 case elfcpp::R_PPC64_TOC16_HI:
11490 case elfcpp::R_PPC64_TOC16_HA:
11491 case elfcpp::R_PPC64_TOC16_DS:
11492 case elfcpp::R_PPC64_TOC16_LO_DS:
11493 // Subtract the TOC base address.
11494 value -= target->toc_pointer();
11495 break;
11497 case elfcpp::R_POWERPC_SECTOFF:
11498 case elfcpp::R_POWERPC_SECTOFF_LO:
11499 case elfcpp::R_POWERPC_SECTOFF_HI:
11500 case elfcpp::R_POWERPC_SECTOFF_HA:
11501 case elfcpp::R_PPC64_SECTOFF_DS:
11502 case elfcpp::R_PPC64_SECTOFF_LO_DS:
11503 if (os != NULL)
11504 value -= os->address();
11505 break;
11507 case elfcpp::R_PPC64_TPREL16_DS:
11508 case elfcpp::R_PPC64_TPREL16_LO_DS:
11509 case elfcpp::R_PPC64_TPREL16_HIGH:
11510 case elfcpp::R_PPC64_TPREL16_HIGHA:
11511 if (size != 64)
11512 // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11513 break;
11514 // Fall through.
11515 case elfcpp::R_POWERPC_TPREL16:
11516 case elfcpp::R_POWERPC_TPREL16_LO:
11517 case elfcpp::R_POWERPC_TPREL16_HI:
11518 case elfcpp::R_POWERPC_TPREL16_HA:
11519 case elfcpp::R_POWERPC_TPREL:
11520 case elfcpp::R_PPC64_TPREL16_HIGHER:
11521 case elfcpp::R_PPC64_TPREL16_HIGHERA:
11522 case elfcpp::R_PPC64_TPREL16_HIGHEST:
11523 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11524 case elfcpp::R_PPC64_TPREL34:
11525 // tls symbol values are relative to tls_segment()->vaddr()
11526 value -= tp_offset;
11527 break;
11529 case elfcpp::R_PPC64_DTPREL16_DS:
11530 case elfcpp::R_PPC64_DTPREL16_LO_DS:
11531 case elfcpp::R_PPC64_DTPREL16_HIGHER:
11532 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11533 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11534 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11535 if (size != 64)
11536 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11537 // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11538 break;
11539 // Fall through.
11540 case elfcpp::R_POWERPC_DTPREL16:
11541 case elfcpp::R_POWERPC_DTPREL16_LO:
11542 case elfcpp::R_POWERPC_DTPREL16_HI:
11543 case elfcpp::R_POWERPC_DTPREL16_HA:
11544 case elfcpp::R_POWERPC_DTPREL:
11545 case elfcpp::R_PPC64_DTPREL16_HIGH:
11546 case elfcpp::R_PPC64_DTPREL16_HIGHA:
11547 case elfcpp::R_PPC64_DTPREL34:
11548 // tls symbol values are relative to tls_segment()->vaddr()
11549 value -= dtp_offset;
11550 break;
11552 case elfcpp::R_PPC64_ADDR64_LOCAL:
11553 if (gsym != NULL)
11554 value += object->ppc64_local_entry_offset(gsym);
11555 else
11556 value += object->ppc64_local_entry_offset(r_sym);
11557 break;
11559 default:
11560 break;
11563 Insn branch_bit = 0;
11564 switch (r_type)
11566 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11567 case elfcpp::R_POWERPC_REL14_BRTAKEN:
11568 branch_bit = 1 << 21;
11569 // Fall through.
11570 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11571 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11573 Insn* iview = reinterpret_cast<Insn*>(view);
11574 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11575 insn &= ~(1 << 21);
11576 insn |= branch_bit;
11577 if (this->is_isa_v2)
11579 // Set 'a' bit. This is 0b00010 in BO field for branch
11580 // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11581 // for branch on CTR insns (BO == 1a00t or 1a01t).
11582 if ((insn & (0x14 << 21)) == (0x04 << 21))
11583 insn |= 0x02 << 21;
11584 else if ((insn & (0x14 << 21)) == (0x10 << 21))
11585 insn |= 0x08 << 21;
11586 else
11587 break;
11589 else
11591 // Invert 'y' bit if not the default.
11592 if (static_cast<Signed_address>(value) < 0)
11593 insn ^= 1 << 21;
11595 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11597 break;
11599 case elfcpp::R_POWERPC_PLT16_HA:
11600 if (size == 32
11601 && !parameters->options().output_is_position_independent())
11603 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11604 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11606 // Convert addis to lis.
11607 if ((insn & (0x3f << 26)) == 15u << 26
11608 && (insn & (0x1f << 16)) != 0)
11610 insn &= ~(0x1f << 16);
11611 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11614 break;
11616 default:
11617 break;
11620 if (gsym
11621 ? relative_value_is_known(gsym)
11622 : relative_value_is_known(psymval))
11624 Insn* iview;
11625 Insn* iview2;
11626 Insn insn;
11627 uint64_t pinsn, pinsn2;
11629 switch (r_type)
11631 default:
11632 break;
11634 // Multi-instruction sequences that access the GOT/TOC can
11635 // be optimized, eg.
11636 // addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11637 // to addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11638 // and
11639 // addis ra,r2,0; addi rb,ra,x@toc@l;
11640 // to nop; addi rb,r2,x@toc;
11641 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11642 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11643 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11644 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11645 case elfcpp::R_POWERPC_GOT16_HA:
11646 case elfcpp::R_PPC64_TOC16_HA:
11647 if (size == 64 && parameters->options().toc_optimize())
11649 iview = reinterpret_cast<Insn*>(view - d_offset);
11650 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11651 if ((r_type == elfcpp::R_PPC64_TOC16_HA
11652 && object->make_toc_relative(target, &value))
11653 || (r_type == elfcpp::R_POWERPC_GOT16_HA
11654 && object->make_got_relative(target, psymval,
11655 rela.get_r_addend(),
11656 &value)))
11658 gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11659 == ((15u << 26) | (2 << 16)));
11661 if (((insn & ((0x3f << 26) | 0x1f << 16))
11662 == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11663 && value + 0x8000 < 0x10000)
11665 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11666 return true;
11669 break;
11671 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11672 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11673 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11674 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11675 case elfcpp::R_POWERPC_GOT16_LO:
11676 case elfcpp::R_PPC64_GOT16_LO_DS:
11677 case elfcpp::R_PPC64_TOC16_LO:
11678 case elfcpp::R_PPC64_TOC16_LO_DS:
11679 if (size == 64 && parameters->options().toc_optimize())
11681 iview = reinterpret_cast<Insn*>(view - d_offset);
11682 insn = elfcpp::Swap<32, big_endian>::readval(iview);
11683 bool changed = false;
11684 if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11685 && object->make_toc_relative(target, &value))
11686 || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11687 && object->make_got_relative(target, psymval,
11688 rela.get_r_addend(),
11689 &value)))
11691 gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11692 insn ^= (14u << 26) ^ (58u << 26);
11693 r_type = elfcpp::R_PPC64_TOC16_LO;
11694 changed = true;
11696 if (ok_lo_toc_insn(insn, r_type)
11697 && value + 0x8000 < 0x10000)
11699 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11701 // Transform addic to addi when we change reg.
11702 insn &= ~((0x3f << 26) | (0x1f << 16));
11703 insn |= (14u << 26) | (2 << 16);
11705 else
11707 insn &= ~(0x1f << 16);
11708 insn |= 2 << 16;
11710 changed = true;
11712 if (changed)
11713 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11715 break;
11717 case elfcpp::R_PPC64_GOT_PCREL34:
11718 if (size == 64 && parameters->options().toc_optimize())
11720 iview = reinterpret_cast<Insn*>(view);
11721 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11722 pinsn <<= 32;
11723 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11724 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11725 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11726 break;
11728 Address relval = psymval->value(object, rela.get_r_addend());
11729 relval -= address;
11730 if (relval + (1ULL << 33) < 1ULL << 34)
11732 value = relval;
11733 // Replace with paddi
11734 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11735 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11736 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11737 pinsn & 0xffffffff);
11738 goto pcrelopt;
11741 break;
11743 case elfcpp::R_PPC64_PCREL34:
11744 if (size == 64)
11746 iview = reinterpret_cast<Insn*>(view);
11747 pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11748 pinsn <<= 32;
11749 pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11750 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11751 != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11752 | (14ULL << 26) /* paddi */))
11753 break;
11755 pcrelopt:
11756 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11757 elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11758 size_t reloc_count = shdr.get_sh_size() / reloc_size;
11759 if (relnum >= reloc_count - 1)
11760 break;
11762 Reltype next_rela(preloc + reloc_size);
11763 if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11764 != elfcpp::R_PPC64_PCREL_OPT)
11765 || next_rela.get_r_offset() != rela.get_r_offset())
11766 break;
11768 Address off = next_rela.get_r_addend();
11769 if (off == 0)
11770 off = 8; // zero means next insn.
11771 if (off + rela.get_r_offset() + 4 > view_size)
11772 break;
11774 iview2 = reinterpret_cast<Insn*>(view + off);
11775 pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11776 pinsn2 <<= 32;
11777 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11778 break;
11779 if (xlate_pcrel_opt(&pinsn, &pinsn2))
11781 elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11782 elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11783 pinsn & 0xffffffff);
11784 elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11787 break;
11789 case elfcpp::R_POWERPC_TPREL16_HA:
11790 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11792 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11793 elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11794 return true;
11796 break;
11798 case elfcpp::R_PPC64_TPREL16_LO_DS:
11799 if (size == 32)
11800 // R_PPC_TLSGD, R_PPC_TLSLD
11801 break;
11802 // Fall through.
11803 case elfcpp::R_POWERPC_TPREL16_LO:
11804 if (target->tprel_opt() && value + 0x8000 < 0x10000)
11806 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11807 Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11808 insn &= ~(0x1f << 16);
11809 insn |= (size == 32 ? 2 : 13) << 16;
11810 elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11812 break;
11814 case elfcpp::R_PPC64_ENTRY:
11815 if (size == 64)
11817 value = target->toc_pointer();
11818 if (value + 0x80008000 <= 0xffffffff
11819 && !parameters->options().output_is_position_independent())
11821 Insn* iview = reinterpret_cast<Insn*>(view);
11822 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11823 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11825 if ((insn1 & ~0xfffc) == ld_2_12
11826 && insn2 == add_2_2_12)
11828 insn1 = lis_2 + ha(value);
11829 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11830 insn2 = addi_2_2 + l(value);
11831 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11832 return true;
11835 else
11837 value -= address;
11838 if (value + 0x80008000 <= 0xffffffff)
11840 Insn* iview = reinterpret_cast<Insn*>(view);
11841 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11842 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11844 if ((insn1 & ~0xfffc) == ld_2_12
11845 && insn2 == add_2_2_12)
11847 insn1 = addis_2_12 + ha(value);
11848 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11849 insn2 = addi_2_2 + l(value);
11850 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11851 return true;
11856 break;
11858 case elfcpp::R_POWERPC_REL16_LO:
11859 // If we are generating a non-PIC executable, edit
11860 // 0: addis 2,12,.TOC.-0b@ha
11861 // addi 2,2,.TOC.-0b@l
11862 // used by ELFv2 global entry points to set up r2, to
11863 // lis 2,.TOC.@ha
11864 // addi 2,2,.TOC.@l
11865 // if .TOC. is in range. */
11866 if (size == 64
11867 && value + address - 4 + 0x80008000 <= 0xffffffff
11868 && relnum + 1 > 1
11869 && preloc != NULL
11870 && target->abiversion() >= 2
11871 && !parameters->options().output_is_position_independent()
11872 && rela.get_r_addend() == d_offset + 4
11873 && gsym != NULL
11874 && strcmp(gsym->name(), ".TOC.") == 0)
11876 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11877 Reltype prev_rela(preloc - reloc_size);
11878 if ((prev_rela.get_r_info()
11879 == elfcpp::elf_r_info<size>(r_sym,
11880 elfcpp::R_POWERPC_REL16_HA))
11881 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11882 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11884 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11885 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11886 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11888 if ((insn1 & 0xffff0000) == addis_2_12
11889 && (insn2 & 0xffff0000) == addi_2_2)
11891 insn1 = lis_2 + ha(value + address - 4);
11892 elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11893 insn2 = addi_2_2 + l(value + address - 4);
11894 elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11895 if (relinfo->rr)
11897 relinfo->rr->set_strategy(relnum - 1,
11898 Relocatable_relocs::RELOC_SPECIAL);
11899 relinfo->rr->set_strategy(relnum,
11900 Relocatable_relocs::RELOC_SPECIAL);
11902 return true;
11906 break;
11910 typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11911 elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11912 switch (r_type)
11914 case elfcpp::R_POWERPC_ADDR32:
11915 case elfcpp::R_POWERPC_UADDR32:
11916 if (size == 64)
11917 overflow = Reloc::CHECK_BITFIELD;
11918 break;
11920 case elfcpp::R_POWERPC_REL32:
11921 case elfcpp::R_POWERPC_REL16DX_HA:
11922 if (size == 64)
11923 overflow = Reloc::CHECK_SIGNED;
11924 break;
11926 case elfcpp::R_POWERPC_UADDR16:
11927 overflow = Reloc::CHECK_BITFIELD;
11928 break;
11930 case elfcpp::R_POWERPC_ADDR16:
11931 // We really should have three separate relocations,
11932 // one for 16-bit data, one for insns with 16-bit signed fields,
11933 // and one for insns with 16-bit unsigned fields.
11934 overflow = Reloc::CHECK_BITFIELD;
11935 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11936 overflow = Reloc::CHECK_LOW_INSN;
11937 break;
11939 case elfcpp::R_POWERPC_ADDR16_HI:
11940 case elfcpp::R_POWERPC_ADDR16_HA:
11941 case elfcpp::R_POWERPC_GOT16_HI:
11942 case elfcpp::R_POWERPC_GOT16_HA:
11943 case elfcpp::R_POWERPC_PLT16_HI:
11944 case elfcpp::R_POWERPC_PLT16_HA:
11945 case elfcpp::R_POWERPC_SECTOFF_HI:
11946 case elfcpp::R_POWERPC_SECTOFF_HA:
11947 case elfcpp::R_PPC64_TOC16_HI:
11948 case elfcpp::R_PPC64_TOC16_HA:
11949 case elfcpp::R_PPC64_PLTGOT16_HI:
11950 case elfcpp::R_PPC64_PLTGOT16_HA:
11951 case elfcpp::R_POWERPC_TPREL16_HI:
11952 case elfcpp::R_POWERPC_TPREL16_HA:
11953 case elfcpp::R_POWERPC_DTPREL16_HI:
11954 case elfcpp::R_POWERPC_DTPREL16_HA:
11955 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11956 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11957 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11958 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11959 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11960 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11961 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11962 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11963 case elfcpp::R_POWERPC_REL16_HI:
11964 case elfcpp::R_POWERPC_REL16_HA:
11965 if (size != 32)
11966 overflow = Reloc::CHECK_HIGH_INSN;
11967 break;
11969 case elfcpp::R_POWERPC_REL16:
11970 case elfcpp::R_PPC64_TOC16:
11971 case elfcpp::R_POWERPC_GOT16:
11972 case elfcpp::R_POWERPC_SECTOFF:
11973 case elfcpp::R_POWERPC_TPREL16:
11974 case elfcpp::R_POWERPC_DTPREL16:
11975 case elfcpp::R_POWERPC_GOT_TLSGD16:
11976 case elfcpp::R_POWERPC_GOT_TLSLD16:
11977 case elfcpp::R_POWERPC_GOT_TPREL16:
11978 case elfcpp::R_POWERPC_GOT_DTPREL16:
11979 overflow = Reloc::CHECK_LOW_INSN;
11980 break;
11982 case elfcpp::R_PPC64_REL24_NOTOC:
11983 if (size == 32)
11984 break;
11985 // Fall through.
11986 case elfcpp::R_PPC64_REL24_P9NOTOC:
11987 case elfcpp::R_POWERPC_ADDR24:
11988 case elfcpp::R_POWERPC_ADDR14:
11989 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11990 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11991 case elfcpp::R_PPC64_ADDR16_DS:
11992 case elfcpp::R_POWERPC_REL24:
11993 case elfcpp::R_PPC_PLTREL24:
11994 case elfcpp::R_PPC_LOCAL24PC:
11995 case elfcpp::R_PPC64_TPREL16_DS:
11996 case elfcpp::R_PPC64_DTPREL16_DS:
11997 case elfcpp::R_PPC64_TOC16_DS:
11998 case elfcpp::R_PPC64_GOT16_DS:
11999 case elfcpp::R_PPC64_SECTOFF_DS:
12000 case elfcpp::R_POWERPC_REL14:
12001 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12002 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12003 case elfcpp::R_PPC64_D34:
12004 case elfcpp::R_PPC64_PCREL34:
12005 case elfcpp::R_PPC64_GOT_PCREL34:
12006 case elfcpp::R_PPC64_PLT_PCREL34:
12007 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12008 case elfcpp::R_PPC64_D28:
12009 case elfcpp::R_PPC64_PCREL28:
12010 case elfcpp::R_PPC64_TPREL34:
12011 case elfcpp::R_PPC64_DTPREL34:
12012 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12013 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12014 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12015 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12016 overflow = Reloc::CHECK_SIGNED;
12017 break;
12020 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
12021 Insn insn = 0;
12023 if (overflow == Reloc::CHECK_LOW_INSN
12024 || overflow == Reloc::CHECK_HIGH_INSN)
12026 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12028 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
12029 overflow = Reloc::CHECK_BITFIELD;
12030 else if (overflow == Reloc::CHECK_LOW_INSN
12031 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
12032 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
12033 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
12034 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
12035 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
12036 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
12037 overflow = Reloc::CHECK_UNSIGNED;
12038 else
12039 overflow = Reloc::CHECK_SIGNED;
12042 bool maybe_dq_reloc = false;
12043 typename Powerpc_relocate_functions<size, big_endian>::Status status
12044 = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
12045 switch (r_type)
12047 case elfcpp::R_POWERPC_NONE:
12048 case elfcpp::R_POWERPC_TLS:
12049 case elfcpp::R_POWERPC_GNU_VTINHERIT:
12050 case elfcpp::R_POWERPC_GNU_VTENTRY:
12051 case elfcpp::R_POWERPC_PLTSEQ:
12052 case elfcpp::R_POWERPC_PLTCALL:
12053 case elfcpp::R_PPC64_PLTSEQ_NOTOC:
12054 case elfcpp::R_PPC64_PLTCALL_NOTOC:
12055 case elfcpp::R_PPC64_PCREL_OPT:
12056 break;
12058 case elfcpp::R_PPC64_ADDR64:
12059 case elfcpp::R_PPC64_REL64:
12060 case elfcpp::R_PPC64_TOC:
12061 case elfcpp::R_PPC64_ADDR64_LOCAL:
12062 Reloc::addr64(view, value);
12063 break;
12065 case elfcpp::R_POWERPC_TPREL:
12066 case elfcpp::R_POWERPC_DTPREL:
12067 if (size == 64)
12068 Reloc::addr64(view, value);
12069 else
12070 status = Reloc::addr32(view, value, overflow);
12071 break;
12073 case elfcpp::R_PPC64_UADDR64:
12074 Reloc::addr64_u(view, value);
12075 break;
12077 case elfcpp::R_POWERPC_ADDR32:
12078 status = Reloc::addr32(view, value, overflow);
12079 break;
12081 case elfcpp::R_POWERPC_REL32:
12082 case elfcpp::R_POWERPC_UADDR32:
12083 status = Reloc::addr32_u(view, value, overflow);
12084 break;
12086 case elfcpp::R_PPC64_REL24_NOTOC:
12087 if (size == 32)
12088 goto unsupp; // R_PPC_EMB_RELSDA
12089 // Fall through.
12090 case elfcpp::R_PPC64_REL24_P9NOTOC:
12091 case elfcpp::R_POWERPC_ADDR24:
12092 case elfcpp::R_POWERPC_REL24:
12093 case elfcpp::R_PPC_PLTREL24:
12094 case elfcpp::R_PPC_LOCAL24PC:
12095 status = Reloc::addr24(view, value, overflow);
12096 break;
12098 case elfcpp::R_POWERPC_GOT_DTPREL16:
12099 case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
12100 case elfcpp::R_POWERPC_GOT_TPREL16:
12101 case elfcpp::R_POWERPC_GOT_TPREL16_LO:
12102 if (size == 64)
12104 // On ppc64 these are all ds form
12105 maybe_dq_reloc = true;
12106 break;
12108 // Fall through.
12109 case elfcpp::R_POWERPC_ADDR16:
12110 case elfcpp::R_POWERPC_REL16:
12111 case elfcpp::R_PPC64_TOC16:
12112 case elfcpp::R_POWERPC_GOT16:
12113 case elfcpp::R_POWERPC_SECTOFF:
12114 case elfcpp::R_POWERPC_TPREL16:
12115 case elfcpp::R_POWERPC_DTPREL16:
12116 case elfcpp::R_POWERPC_GOT_TLSGD16:
12117 case elfcpp::R_POWERPC_GOT_TLSLD16:
12118 case elfcpp::R_POWERPC_ADDR16_LO:
12119 case elfcpp::R_POWERPC_REL16_LO:
12120 case elfcpp::R_PPC64_TOC16_LO:
12121 case elfcpp::R_POWERPC_GOT16_LO:
12122 case elfcpp::R_POWERPC_PLT16_LO:
12123 case elfcpp::R_POWERPC_SECTOFF_LO:
12124 case elfcpp::R_POWERPC_TPREL16_LO:
12125 case elfcpp::R_POWERPC_DTPREL16_LO:
12126 case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
12127 case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
12128 if (size == 64)
12129 status = Reloc::addr16(view, value, overflow);
12130 else
12131 maybe_dq_reloc = true;
12132 break;
12134 case elfcpp::R_POWERPC_UADDR16:
12135 status = Reloc::addr16_u(view, value, overflow);
12136 break;
12138 case elfcpp::R_PPC64_ADDR16_HIGH:
12139 case elfcpp::R_PPC64_TPREL16_HIGH:
12140 case elfcpp::R_PPC64_DTPREL16_HIGH:
12141 if (size == 32)
12142 // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
12143 goto unsupp;
12144 // Fall through.
12145 case elfcpp::R_POWERPC_ADDR16_HI:
12146 case elfcpp::R_POWERPC_REL16_HI:
12147 case elfcpp::R_PPC64_REL16_HIGH:
12148 case elfcpp::R_PPC64_TOC16_HI:
12149 case elfcpp::R_POWERPC_GOT16_HI:
12150 case elfcpp::R_POWERPC_PLT16_HI:
12151 case elfcpp::R_POWERPC_SECTOFF_HI:
12152 case elfcpp::R_POWERPC_TPREL16_HI:
12153 case elfcpp::R_POWERPC_DTPREL16_HI:
12154 case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
12155 case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
12156 case elfcpp::R_POWERPC_GOT_TPREL16_HI:
12157 case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
12158 Reloc::addr16_hi(view, value);
12159 break;
12161 case elfcpp::R_PPC64_ADDR16_HIGHA:
12162 case elfcpp::R_PPC64_TPREL16_HIGHA:
12163 case elfcpp::R_PPC64_DTPREL16_HIGHA:
12164 if (size == 32)
12165 // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
12166 goto unsupp;
12167 // Fall through.
12168 case elfcpp::R_POWERPC_ADDR16_HA:
12169 case elfcpp::R_POWERPC_REL16_HA:
12170 case elfcpp::R_PPC64_REL16_HIGHA:
12171 case elfcpp::R_PPC64_TOC16_HA:
12172 case elfcpp::R_POWERPC_GOT16_HA:
12173 case elfcpp::R_POWERPC_PLT16_HA:
12174 case elfcpp::R_POWERPC_SECTOFF_HA:
12175 case elfcpp::R_POWERPC_TPREL16_HA:
12176 case elfcpp::R_POWERPC_DTPREL16_HA:
12177 case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
12178 case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
12179 case elfcpp::R_POWERPC_GOT_TPREL16_HA:
12180 case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
12181 Reloc::addr16_ha(view, value);
12182 break;
12184 case elfcpp::R_POWERPC_REL16DX_HA:
12185 status = Reloc::addr16dx_ha(view, value, overflow);
12186 break;
12188 case elfcpp::R_PPC64_DTPREL16_HIGHER:
12189 if (size == 32)
12190 // R_PPC_EMB_NADDR16_LO
12191 goto unsupp;
12192 // Fall through.
12193 case elfcpp::R_PPC64_ADDR16_HIGHER:
12194 case elfcpp::R_PPC64_REL16_HIGHER:
12195 case elfcpp::R_PPC64_TPREL16_HIGHER:
12196 Reloc::addr16_hi2(view, value);
12197 break;
12199 case elfcpp::R_PPC64_DTPREL16_HIGHERA:
12200 if (size == 32)
12201 // R_PPC_EMB_NADDR16_HI
12202 goto unsupp;
12203 // Fall through.
12204 case elfcpp::R_PPC64_ADDR16_HIGHERA:
12205 case elfcpp::R_PPC64_REL16_HIGHERA:
12206 case elfcpp::R_PPC64_TPREL16_HIGHERA:
12207 Reloc::addr16_ha2(view, value);
12208 break;
12210 case elfcpp::R_PPC64_DTPREL16_HIGHEST:
12211 if (size == 32)
12212 // R_PPC_EMB_NADDR16_HA
12213 goto unsupp;
12214 // Fall through.
12215 case elfcpp::R_PPC64_ADDR16_HIGHEST:
12216 case elfcpp::R_PPC64_REL16_HIGHEST:
12217 case elfcpp::R_PPC64_TPREL16_HIGHEST:
12218 Reloc::addr16_hi3(view, value);
12219 break;
12221 case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
12222 if (size == 32)
12223 // R_PPC_EMB_SDAI16
12224 goto unsupp;
12225 // Fall through.
12226 case elfcpp::R_PPC64_ADDR16_HIGHESTA:
12227 case elfcpp::R_PPC64_REL16_HIGHESTA:
12228 case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12229 Reloc::addr16_ha3(view, value);
12230 break;
12232 case elfcpp::R_PPC64_DTPREL16_DS:
12233 case elfcpp::R_PPC64_DTPREL16_LO_DS:
12234 if (size == 32)
12235 // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12236 goto unsupp;
12237 // Fall through.
12238 case elfcpp::R_PPC64_TPREL16_DS:
12239 case elfcpp::R_PPC64_TPREL16_LO_DS:
12240 if (size == 32)
12241 // R_PPC_TLSGD, R_PPC_TLSLD
12242 break;
12243 // Fall through.
12244 case elfcpp::R_PPC64_ADDR16_DS:
12245 case elfcpp::R_PPC64_ADDR16_LO_DS:
12246 case elfcpp::R_PPC64_TOC16_DS:
12247 case elfcpp::R_PPC64_TOC16_LO_DS:
12248 case elfcpp::R_PPC64_GOT16_DS:
12249 case elfcpp::R_PPC64_GOT16_LO_DS:
12250 case elfcpp::R_PPC64_PLT16_LO_DS:
12251 case elfcpp::R_PPC64_SECTOFF_DS:
12252 case elfcpp::R_PPC64_SECTOFF_LO_DS:
12253 maybe_dq_reloc = true;
12254 break;
12256 case elfcpp::R_POWERPC_ADDR14:
12257 case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12258 case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12259 case elfcpp::R_POWERPC_REL14:
12260 case elfcpp::R_POWERPC_REL14_BRTAKEN:
12261 case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12262 status = Reloc::addr14(view, value, overflow);
12263 break;
12265 case elfcpp::R_POWERPC_COPY:
12266 case elfcpp::R_POWERPC_GLOB_DAT:
12267 case elfcpp::R_POWERPC_JMP_SLOT:
12268 case elfcpp::R_POWERPC_RELATIVE:
12269 case elfcpp::R_POWERPC_DTPMOD:
12270 case elfcpp::R_PPC64_JMP_IREL:
12271 case elfcpp::R_POWERPC_IRELATIVE:
12272 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12273 _("unexpected reloc %u in object file"),
12274 r_type);
12275 break;
12277 case elfcpp::R_PPC64_TOCSAVE:
12278 if (size == 32)
12279 // R_PPC_EMB_SDA21
12280 goto unsupp;
12281 else
12283 Symbol_location loc;
12284 loc.object = relinfo->object;
12285 loc.shndx = relinfo->data_shndx;
12286 loc.offset = rela.get_r_offset();
12287 const Tocsave_loc *tocsave = target->tocsave_loc();
12288 if (tocsave->find(loc) != tocsave->end())
12290 // If we've generated plt calls using this tocsave, then
12291 // the nop needs to be changed to save r2.
12292 Insn* iview = reinterpret_cast<Insn*>(view);
12293 if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12294 elfcpp::Swap<32, big_endian>::
12295 writeval(iview, std_2_1 + target->stk_toc());
12298 break;
12300 case elfcpp::R_PPC_EMB_SDA2I16:
12301 case elfcpp::R_PPC_EMB_SDA2REL:
12302 if (size == 32)
12303 goto unsupp;
12304 // R_PPC64_TLSGD, R_PPC64_TLSLD
12305 break;
12307 case elfcpp::R_PPC64_D34:
12308 case elfcpp::R_PPC64_D34_LO:
12309 case elfcpp::R_PPC64_PCREL34:
12310 case elfcpp::R_PPC64_GOT_PCREL34:
12311 case elfcpp::R_PPC64_PLT_PCREL34:
12312 case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12313 case elfcpp::R_PPC64_TPREL34:
12314 case elfcpp::R_PPC64_DTPREL34:
12315 case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12316 case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12317 case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12318 case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12319 if (size == 32)
12320 goto unsupp;
12321 status = Reloc::addr34(view, value, overflow);
12322 break;
12324 case elfcpp::R_PPC64_D34_HI30:
12325 if (size == 32)
12326 goto unsupp;
12327 Reloc::addr34_hi(view, value);
12328 break;
12330 case elfcpp::R_PPC64_D34_HA30:
12331 if (size == 32)
12332 goto unsupp;
12333 Reloc::addr34_ha(view, value);
12334 break;
12336 case elfcpp::R_PPC64_D28:
12337 case elfcpp::R_PPC64_PCREL28:
12338 if (size == 32)
12339 goto unsupp;
12340 status = Reloc::addr28(view, value, overflow);
12341 break;
12343 case elfcpp::R_PPC64_ADDR16_HIGHER34:
12344 case elfcpp::R_PPC64_REL16_HIGHER34:
12345 if (size == 32)
12346 goto unsupp;
12347 Reloc::addr16_higher34(view, value);
12348 break;
12350 case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12351 case elfcpp::R_PPC64_REL16_HIGHERA34:
12352 if (size == 32)
12353 goto unsupp;
12354 Reloc::addr16_highera34(view, value);
12355 break;
12357 case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12358 case elfcpp::R_PPC64_REL16_HIGHEST34:
12359 if (size == 32)
12360 goto unsupp;
12361 Reloc::addr16_highest34(view, value);
12362 break;
12364 case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12365 case elfcpp::R_PPC64_REL16_HIGHESTA34:
12366 if (size == 32)
12367 goto unsupp;
12368 Reloc::addr16_highesta34(view, value);
12369 break;
12371 case elfcpp::R_POWERPC_PLT32:
12372 case elfcpp::R_POWERPC_PLTREL32:
12373 case elfcpp::R_PPC_SDAREL16:
12374 case elfcpp::R_POWERPC_ADDR30:
12375 case elfcpp::R_PPC64_PLT64:
12376 case elfcpp::R_PPC64_PLTREL64:
12377 case elfcpp::R_PPC64_PLTGOT16:
12378 case elfcpp::R_PPC64_PLTGOT16_LO:
12379 case elfcpp::R_PPC64_PLTGOT16_HI:
12380 case elfcpp::R_PPC64_PLTGOT16_HA:
12381 case elfcpp::R_PPC64_PLTGOT16_DS:
12382 case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12383 case elfcpp::R_PPC_TOC16:
12384 default:
12385 unsupp:
12386 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12387 _("unsupported reloc %u"),
12388 r_type);
12389 break;
12392 if (maybe_dq_reloc)
12394 if (insn == 0)
12395 insn = elfcpp::Swap<32, big_endian>::readval(iview);
12397 if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12398 || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12399 && (insn & 3) == 1))
12400 status = Reloc::addr16_dq(view, value, overflow);
12401 else if (size == 64
12402 || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12403 || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12404 || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12405 || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12406 status = Reloc::addr16_ds(view, value, overflow);
12407 else
12408 status = Reloc::addr16(view, value, overflow);
12411 if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12412 && (has_stub_value
12413 || !(gsym != NULL
12414 && gsym->is_undefined()
12415 && is_branch_reloc<size>(r_type))))
12417 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12418 _("relocation overflow"));
12419 if (has_stub_value)
12420 gold_info(_("try relinking with a smaller --stub-group-size"));
12423 return true;
12426 // Relocate section data.
12428 template<int size, bool big_endian>
12429 void
12430 Target_powerpc<size, big_endian>::relocate_section(
12431 const Relocate_info<size, big_endian>* relinfo,
12432 unsigned int sh_type,
12433 const unsigned char* prelocs,
12434 size_t reloc_count,
12435 Output_section* output_section,
12436 bool needs_special_offset_handling,
12437 unsigned char* view,
12438 Address address,
12439 section_size_type view_size,
12440 const Reloc_symbol_changes* reloc_symbol_changes)
12442 typedef Target_powerpc<size, big_endian> Powerpc;
12443 typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12444 typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12445 Powerpc_comdat_behavior;
12446 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12447 Classify_reloc;
12449 gold_assert(sh_type == elfcpp::SHT_RELA);
12451 gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12452 Powerpc_comdat_behavior, Classify_reloc>(
12453 relinfo,
12454 this,
12455 prelocs,
12456 reloc_count,
12457 output_section,
12458 needs_special_offset_handling,
12459 view,
12460 address,
12461 view_size,
12462 reloc_symbol_changes);
12465 template<int size, bool big_endian>
12466 class Powerpc_scan_relocatable_reloc
12468 public:
12469 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12470 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12471 static const int sh_type = elfcpp::SHT_RELA;
12473 // Return the symbol referred to by the relocation.
12474 static inline unsigned int
12475 get_r_sym(const Reltype* reloc)
12476 { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12478 // Return the type of the relocation.
12479 static inline unsigned int
12480 get_r_type(const Reltype* reloc)
12481 { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12483 // Return the strategy to use for a local symbol which is not a
12484 // section symbol, given the relocation type.
12485 inline Relocatable_relocs::Reloc_strategy
12486 local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12488 if (r_type == 0 && r_sym == 0)
12489 return Relocatable_relocs::RELOC_DISCARD;
12490 return Relocatable_relocs::RELOC_COPY;
12493 // Return the strategy to use for a local symbol which is a section
12494 // symbol, given the relocation type.
12495 inline Relocatable_relocs::Reloc_strategy
12496 local_section_strategy(unsigned int, Relobj*)
12498 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12501 // Return the strategy to use for a global symbol, given the
12502 // relocation type, the object, and the symbol index.
12503 inline Relocatable_relocs::Reloc_strategy
12504 global_strategy(unsigned int r_type, Relobj*, unsigned int)
12506 if (size == 32
12507 && (r_type == elfcpp::R_PPC_PLTREL24
12508 || r_type == elfcpp::R_POWERPC_PLT16_LO
12509 || r_type == elfcpp::R_POWERPC_PLT16_HI
12510 || r_type == elfcpp::R_POWERPC_PLT16_HA))
12511 return Relocatable_relocs::RELOC_SPECIAL;
12512 return Relocatable_relocs::RELOC_COPY;
12516 // Scan the relocs during a relocatable link.
12518 template<int size, bool big_endian>
12519 void
12520 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12521 Symbol_table* symtab,
12522 Layout* layout,
12523 Sized_relobj_file<size, big_endian>* object,
12524 unsigned int data_shndx,
12525 unsigned int sh_type,
12526 const unsigned char* prelocs,
12527 size_t reloc_count,
12528 Output_section* output_section,
12529 bool needs_special_offset_handling,
12530 size_t local_symbol_count,
12531 const unsigned char* plocal_symbols,
12532 Relocatable_relocs* rr)
12534 typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12536 gold_assert(sh_type == elfcpp::SHT_RELA);
12538 gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12539 symtab,
12540 layout,
12541 object,
12542 data_shndx,
12543 prelocs,
12544 reloc_count,
12545 output_section,
12546 needs_special_offset_handling,
12547 local_symbol_count,
12548 plocal_symbols,
12549 rr);
12552 // Scan the relocs for --emit-relocs.
12554 template<int size, bool big_endian>
12555 void
12556 Target_powerpc<size, big_endian>::emit_relocs_scan(
12557 Symbol_table* symtab,
12558 Layout* layout,
12559 Sized_relobj_file<size, big_endian>* object,
12560 unsigned int data_shndx,
12561 unsigned int sh_type,
12562 const unsigned char* prelocs,
12563 size_t reloc_count,
12564 Output_section* output_section,
12565 bool needs_special_offset_handling,
12566 size_t local_symbol_count,
12567 const unsigned char* plocal_syms,
12568 Relocatable_relocs* rr)
12570 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12571 Classify_reloc;
12572 typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12573 Emit_relocs_strategy;
12575 gold_assert(sh_type == elfcpp::SHT_RELA);
12577 gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12578 symtab,
12579 layout,
12580 object,
12581 data_shndx,
12582 prelocs,
12583 reloc_count,
12584 output_section,
12585 needs_special_offset_handling,
12586 local_symbol_count,
12587 plocal_syms,
12588 rr);
12591 // Emit relocations for a section.
12592 // This is a modified version of the function by the same name in
12593 // target-reloc.h. Using relocate_special_relocatable for
12594 // R_PPC_PLTREL24 would require duplication of the entire body of the
12595 // loop, so we may as well duplicate the whole thing.
12597 template<int size, bool big_endian>
12598 void
12599 Target_powerpc<size, big_endian>::relocate_relocs(
12600 const Relocate_info<size, big_endian>* relinfo,
12601 unsigned int sh_type,
12602 const unsigned char* prelocs,
12603 size_t reloc_count,
12604 Output_section* output_section,
12605 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12606 unsigned char*,
12607 Address view_address,
12608 section_size_type,
12609 unsigned char* reloc_view,
12610 section_size_type reloc_view_size)
12612 gold_assert(sh_type == elfcpp::SHT_RELA);
12614 typedef typename elfcpp::Rela<size, big_endian> Reltype;
12615 typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12616 const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12617 // Offset from start of insn to d-field reloc.
12618 const int d_offset = big_endian ? 2 : 0;
12620 Powerpc_relobj<size, big_endian>* const object
12621 = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12622 const unsigned int local_count = object->local_symbol_count();
12623 unsigned int got2_shndx = object->got2_shndx();
12624 Address got2_addend = 0;
12625 if (got2_shndx != 0)
12627 got2_addend = object->get_output_section_offset(got2_shndx);
12628 gold_assert(got2_addend != invalid_address);
12631 const bool relocatable = parameters->options().relocatable();
12633 unsigned char* pwrite = reloc_view;
12634 bool zap_next = false;
12635 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12637 Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12638 if (strategy == Relocatable_relocs::RELOC_DISCARD)
12639 continue;
12641 Reltype reloc(prelocs);
12642 Reltype_write reloc_write(pwrite);
12644 Address offset = reloc.get_r_offset();
12645 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12646 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12647 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12648 const unsigned int orig_r_sym = r_sym;
12649 typename elfcpp::Elf_types<size>::Elf_Swxword addend
12650 = reloc.get_r_addend();
12651 const Symbol* gsym = NULL;
12653 if (zap_next)
12655 // We could arrange to discard these and other relocs for
12656 // tls optimised sequences in the strategy methods, but for
12657 // now do as BFD ld does.
12658 r_type = elfcpp::R_POWERPC_NONE;
12659 zap_next = false;
12662 // Get the new symbol index.
12663 Output_section* os = NULL;
12664 if (r_sym < local_count)
12666 switch (strategy)
12668 case Relocatable_relocs::RELOC_COPY:
12669 case Relocatable_relocs::RELOC_SPECIAL:
12670 if (r_sym != 0)
12672 r_sym = object->symtab_index(r_sym);
12673 gold_assert(r_sym != -1U);
12675 break;
12677 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12679 // We are adjusting a section symbol. We need to find
12680 // the symbol table index of the section symbol for
12681 // the output section corresponding to input section
12682 // in which this symbol is defined.
12683 gold_assert(r_sym < local_count);
12684 bool is_ordinary;
12685 unsigned int shndx =
12686 object->local_symbol_input_shndx(r_sym, &is_ordinary);
12687 gold_assert(is_ordinary);
12688 os = object->output_section(shndx);
12689 gold_assert(os != NULL);
12690 gold_assert(os->needs_symtab_index());
12691 r_sym = os->symtab_index();
12693 break;
12695 default:
12696 gold_unreachable();
12699 else
12701 gsym = object->global_symbol(r_sym);
12702 gold_assert(gsym != NULL);
12703 if (gsym->is_forwarder())
12704 gsym = relinfo->symtab->resolve_forwards(gsym);
12706 gold_assert(gsym->has_symtab_index());
12707 r_sym = gsym->symtab_index();
12710 // Get the new offset--the location in the output section where
12711 // this relocation should be applied.
12712 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12713 offset += offset_in_output_section;
12714 else
12716 section_offset_type sot_offset =
12717 convert_types<section_offset_type, Address>(offset);
12718 section_offset_type new_sot_offset =
12719 output_section->output_offset(object, relinfo->data_shndx,
12720 sot_offset);
12721 gold_assert(new_sot_offset != -1);
12722 offset = new_sot_offset;
12725 // In an object file, r_offset is an offset within the section.
12726 // In an executable or dynamic object, generated by
12727 // --emit-relocs, r_offset is an absolute address.
12728 if (!relocatable)
12730 offset += view_address;
12731 if (static_cast<Address>(offset_in_output_section) != invalid_address)
12732 offset -= offset_in_output_section;
12735 // Handle the reloc addend based on the strategy.
12736 if (strategy == Relocatable_relocs::RELOC_COPY)
12738 else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12740 const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12741 addend = psymval->value(object, addend);
12742 // In a relocatable link, the symbol value is relative to
12743 // the start of the output section. For a non-relocatable
12744 // link, we need to adjust the addend.
12745 if (!relocatable)
12747 gold_assert(os != NULL);
12748 addend -= os->address();
12751 else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12753 if (size == 32)
12755 if (addend >= 32768)
12756 addend += got2_addend;
12758 else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12760 r_type = elfcpp::R_POWERPC_ADDR16_HA;
12761 addend -= d_offset;
12763 else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12765 r_type = elfcpp::R_POWERPC_ADDR16_LO;
12766 addend -= d_offset + 4;
12769 else
12770 gold_unreachable();
12772 if (!relocatable)
12774 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12775 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12776 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12777 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12779 // First instruction of a global dynamic sequence,
12780 // arg setup insn.
12781 bool final = gsym == NULL || gsym->final_value_is_known();
12782 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12783 switch (tls_type)
12785 case tls::TLSOPT_TO_IE:
12786 r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12787 - elfcpp::R_POWERPC_GOT_TLSGD16);
12788 break;
12789 case tls::TLSOPT_TO_LE:
12790 if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12791 || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12792 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12793 else
12795 r_type = elfcpp::R_POWERPC_NONE;
12796 offset -= d_offset;
12798 break;
12799 default:
12800 break;
12803 else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12804 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12805 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12806 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12808 // First instruction of a local dynamic sequence,
12809 // arg setup insn.
12810 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12811 if (tls_type == tls::TLSOPT_TO_LE)
12813 if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12814 || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12816 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12817 const Output_section* os = relinfo->layout->tls_segment()
12818 ->first_section();
12819 gold_assert(os != NULL);
12820 gold_assert(os->needs_symtab_index());
12821 r_sym = os->symtab_index();
12822 addend = dtp_offset;
12824 else
12826 r_type = elfcpp::R_POWERPC_NONE;
12827 offset -= d_offset;
12831 else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12832 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12833 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12834 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12836 // First instruction of initial exec sequence.
12837 bool final = gsym == NULL || gsym->final_value_is_known();
12838 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12840 if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12841 || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12842 r_type = elfcpp::R_POWERPC_TPREL16_HA;
12843 else
12845 r_type = elfcpp::R_POWERPC_NONE;
12846 offset -= d_offset;
12850 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12851 || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12853 // Second instruction of a global dynamic sequence,
12854 // the __tls_get_addr call
12855 bool final = gsym == NULL || gsym->final_value_is_known();
12856 tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12857 switch (tls_type)
12859 case tls::TLSOPT_TO_IE:
12860 r_type = elfcpp::R_POWERPC_NONE;
12861 zap_next = true;
12862 break;
12863 case tls::TLSOPT_TO_LE:
12864 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12865 offset += d_offset;
12866 zap_next = true;
12867 break;
12868 default:
12869 break;
12872 else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12873 || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12875 // Second instruction of a local dynamic sequence,
12876 // the __tls_get_addr call
12877 tls::Tls_optimization tls_type = this->optimize_tls_ld();
12878 if (tls_type == tls::TLSOPT_TO_LE)
12880 const Output_section* os = relinfo->layout->tls_segment()
12881 ->first_section();
12882 gold_assert(os != NULL);
12883 gold_assert(os->needs_symtab_index());
12884 r_sym = os->symtab_index();
12885 addend = dtp_offset;
12886 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12887 offset += d_offset;
12888 zap_next = true;
12891 else if (r_type == elfcpp::R_POWERPC_TLS)
12893 // Second instruction of an initial exec sequence
12894 bool final = gsym == NULL || gsym->final_value_is_known();
12895 if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12897 r_type = elfcpp::R_POWERPC_TPREL16_LO;
12898 offset += d_offset;
12903 reloc_write.put_r_offset(offset);
12904 reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12905 reloc_write.put_r_addend(addend);
12907 pwrite += reloc_size;
12910 gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12911 == reloc_view_size);
12914 // Return the value to use for a dynamic symbol which requires special
12915 // treatment. This is how we support equality comparisons of function
12916 // pointers across shared library boundaries, as described in the
12917 // processor specific ABI supplement.
12919 template<int size, bool big_endian>
12920 uint64_t
12921 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12923 if (size == 32)
12925 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12926 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12927 p != this->stub_tables_.end();
12928 ++p)
12930 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12931 = (*p)->find_plt_call_entry(gsym);
12932 if (ent != NULL)
12933 return (*p)->stub_address() + ent->off_;
12936 else if (this->abiversion() >= 2)
12938 Address off = this->glink_section()->find_global_entry(gsym);
12939 if (off != invalid_address)
12940 return this->glink_section()->global_entry_address() + off;
12942 gold_unreachable();
12945 // Return the PLT address to use for a local symbol.
12946 template<int size, bool big_endian>
12947 uint64_t
12948 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12949 const Relobj* object,
12950 unsigned int symndx) const
12952 if (size == 32)
12954 const Sized_relobj<size, big_endian>* relobj
12955 = static_cast<const Sized_relobj<size, big_endian>*>(object);
12956 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12957 p != this->stub_tables_.end();
12958 ++p)
12960 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12961 = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12962 if (ent != NULL)
12963 return (*p)->stub_address() + ent->off_;
12966 gold_unreachable();
12969 // Return the PLT address to use for a global symbol.
12970 template<int size, bool big_endian>
12971 uint64_t
12972 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12973 const Symbol* gsym) const
12975 if (size == 32)
12977 for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12978 p != this->stub_tables_.end();
12979 ++p)
12981 const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12982 = (*p)->find_plt_call_entry(gsym);
12983 if (ent != NULL)
12984 return (*p)->stub_address() + ent->off_;
12987 else if (this->abiversion() >= 2)
12989 Address off = this->glink_section()->find_global_entry(gsym);
12990 if (off != invalid_address)
12991 return this->glink_section()->global_entry_address() + off;
12993 gold_unreachable();
12996 // Return the offset to use for the GOT_INDX'th got entry which is
12997 // for a local tls symbol specified by OBJECT, SYMNDX.
12998 template<int size, bool big_endian>
12999 int64_t
13000 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
13001 const Relobj* object,
13002 unsigned int symndx,
13003 Output_data_got_base* got,
13004 unsigned int got_indx,
13005 uint64_t addend) const
13007 const Powerpc_relobj<size, big_endian>* ppc_object
13008 = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
13009 if (ppc_object->local_symbol(symndx)->is_tls_symbol())
13011 for (Got_type got_type = (size == 32
13012 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13013 got_type <= GOT_TYPE_SMALL_TPREL;
13014 got_type = Got_type(got_type + 1))
13015 if (got_type != GOT_TYPE_SMALL
13016 && ppc_object->local_has_got_offset(symndx, got_type, addend))
13018 unsigned int off
13019 = ppc_object->local_got_offset(symndx, got_type, addend);
13020 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13021 off += size / 8;
13022 if (off == got_indx * (size / 8)
13023 && (size == 32 || got == this->got_section(got_type)))
13025 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13026 return -tp_offset;
13027 else
13028 return -dtp_offset;
13032 gold_unreachable();
13035 // Return the offset to use for the GOT_INDX'th got entry which is
13036 // for global tls symbol GSYM.
13037 template<int size, bool big_endian>
13038 int64_t
13039 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
13040 Symbol* gsym,
13041 Output_data_got_base* got,
13042 unsigned int got_indx,
13043 uint64_t addend) const
13045 if (gsym->type() == elfcpp::STT_TLS)
13047 for (Got_type got_type = (size == 32
13048 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13049 got_type <= GOT_TYPE_SMALL_TPREL;
13050 got_type = Got_type(got_type + 1))
13051 if (got_type != GOT_TYPE_SMALL
13052 && gsym->has_got_offset(got_type, addend))
13054 unsigned int off = gsym->got_offset(got_type, addend);
13055 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13056 off += size / 8;
13057 if (off == got_indx * (size / 8)
13058 && (size == 32 || got == this->got_section(got_type)))
13060 if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13061 return -tp_offset;
13062 else
13063 return -dtp_offset;
13067 gold_unreachable();
13070 // The selector for powerpc object files.
13072 template<int size, bool big_endian>
13073 class Target_selector_powerpc : public Target_selector
13075 public:
13076 Target_selector_powerpc()
13077 : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
13078 size, big_endian,
13079 (size == 64
13080 ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
13081 : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
13082 (size == 64
13083 ? (big_endian ? "elf64ppc" : "elf64lppc")
13084 : (big_endian ? "elf32ppc" : "elf32lppc")))
13087 virtual Target*
13088 do_instantiate_target()
13089 { return new Target_powerpc<size, big_endian>(); }
13092 Target_selector_powerpc<32, true> target_selector_ppc32;
13093 Target_selector_powerpc<32, false> target_selector_ppc32le;
13094 Target_selector_powerpc<64, true> target_selector_ppc64;
13095 Target_selector_powerpc<64, false> target_selector_ppc64le;
13097 // Instantiate these constants for -O0
13098 template<int size, bool big_endian>
13099 const typename Output_data_glink<size, big_endian>::Address
13100 Output_data_glink<size, big_endian>::invalid_address;
13101 template<int size, bool big_endian>
13102 const typename Stub_table<size, big_endian>::Address
13103 Stub_table<size, big_endian>::invalid_address;
13104 template<int size, bool big_endian>
13105 const typename Target_powerpc<size, big_endian>::Address
13106 Target_powerpc<size, big_endian>::invalid_address;
13108 } // End anonymous namespace.