xref: /netbsd-src/external/gpl3/binutils.old/dist/gold/powerpc.cc (revision 06dfa8449cb5e76c0044ec0f3badf7d5180af0f5)
1 // powerpc.cc -- powerpc target support for gold.
2 
3 // Copyright (C) 2008-2020 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>
5 //        and David Edelsohn <edelsohn@gnu.org>
6 
7 // This file is part of gold.
8 
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.
13 
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.
18 
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.
23 
24 #include "gold.h"
25 
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"
45 
46 namespace
47 {
48 
49 using namespace gold;
50 
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
53 
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
56 
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
59 
60 template<int size, bool big_endian>
61 class Output_data_glink;
62 
63 template<int size, bool big_endian>
64 class Stub_table;
65 
66 template<int size, bool big_endian>
67 class Output_data_save_res;
68 
69 template<int size, bool big_endian>
70 class Target_powerpc;
71 
72 struct Stub_table_owner
73 {
74   Stub_table_owner()
75     : output_section(NULL), owner(NULL)
76   { }
77 
78   Output_section* output_section;
79   const Output_section::Input_section* owner;
80 };
81 
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
84 
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
87 
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
90 
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93 {
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;
98 
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)
107   {
108     this->set_abiversion(0);
109   }
110 
111   ~Powerpc_relobj()
112   { delete this->attributes_section_data_; }
113 
114   // Read the symbols then set up st_other vector.
115   void
116   do_read_symbols(Read_symbols_data*);
117 
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);
124 
125   // The .toc section index.
126   unsigned int
127   toc_shndx() const
128   {
129     return this->toc_;
130   }
131 
132   // Mark .toc entry at OFF as not optimizable.
133   void
134   set_no_toc_opt(Address off)
135   {
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;
142   }
143 
144   // Mark the entire .toc as not optimizable.
145   void
146   set_no_toc_opt()
147   {
148     this->no_toc_opt_.resize(1);
149     this->no_toc_opt_[0] = true;
150   }
151 
152   // Return true if code using the .toc entry at OFF should not be edited.
153   bool
154   no_toc_opt(Address off) const
155   {
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];
162   }
163 
164   // The .got2 section shndx.
165   unsigned int
166   got2_shndx() const
167   {
168     if (size == 32)
169       return this->special_;
170     else
171       return 0;
172   }
173 
174   // The .opd section shndx.
175   unsigned int
176   opd_shndx() const
177   {
178     if (size == 32)
179       return 0;
180     else
181       return this->special_;
182   }
183 
184   // Init OPD entry arrays.
185   void
186   init_opd(size_t opd_size)
187   {
188     size_t count = this->opd_ent_ndx(opd_size);
189     this->opd_ent_.resize(count);
190   }
191 
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
195   {
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;
202   }
203 
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)
207   {
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;
212   }
213 
214   // Return discard flag for .opd + R_OFF.
215   bool
216   get_opd_discard(Address r_off) const
217   {
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;
221   }
222 
223   // Set discard flag for .opd + R_OFF.
224   void
225   set_opd_discard(Address r_off)
226   {
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;
230   }
231 
232   bool
233   opd_valid() const
234   { return this->opd_valid_; }
235 
236   void
237   set_opd_valid()
238   { this->opd_valid_ = true; }
239 
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);
245 
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);
251 
252   bool
253   make_got_relative(Target_powerpc<size, big_endian>* target,
254 		    const Symbol_value<size>* psymval,
255 		    Address addend,
256 		    Address* value);
257 
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*);
262 
263   bool
264   do_find_special_sections(Read_symbols_data* sd);
265 
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
270   {
271     if (size == 64 && this->opd_shndx() != 0)
272       {
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;
278       }
279     return true;
280   }
281 
282   Access_from*
283   access_from_map()
284   { return &this->access_from_map_; }
285 
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)
292   {
293     Section_id src_id(src_obj, src_indx);
294     this->access_from_map_[dst_off].insert(src_id);
295   }
296 
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)
301   {
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;
306   }
307 
308   void
309   process_gc_mark(Symbol_table* symtab)
310   {
311     for (size_t i = 0; i < this->opd_ent_.size(); i++)
312       if (this->opd_ent_[i].gc_mark)
313 	{
314 	  unsigned int shndx = this->opd_ent_[i].shndx;
315 	  symtab->gc()->worklist().push_back(Section_id(this, shndx));
316 	}
317   }
318 
319   // Return offset in output GOT section that this object will use
320   // as a TOC pointer.  Won't be just a constant with multi-toc support.
321   Address
322   toc_base_offset() const
323   { return 0x8000; }
324 
325   void
326   set_has_small_toc_reloc()
327   { has_small_toc_reloc_ = true; }
328 
329   bool
330   has_small_toc_reloc() const
331   { return has_small_toc_reloc_; }
332 
333   void
334   set_has_14bit_branch(unsigned int shndx)
335   {
336     if (shndx >= this->has14_.size())
337       this->has14_.resize(shndx + 1);
338     this->has14_[shndx] = true;
339   }
340 
341   bool
342   has_14bit_branch(unsigned int shndx) const
343   { return shndx < this->has14_.size() && this->has14_[shndx];  }
344 
345   void
346   set_stub_table(unsigned int shndx, unsigned int stub_index)
347   {
348     if (shndx >= this->stub_table_index_.size())
349       this->stub_table_index_.resize(shndx + 1, -1);
350     this->stub_table_index_[shndx] = stub_index;
351   }
352 
353   Stub_table<size, big_endian>*
354   stub_table(unsigned int shndx)
355   {
356     if (shndx < this->stub_table_index_.size())
357       {
358 	Target_powerpc<size, big_endian>* target
359 	  = static_cast<Target_powerpc<size, big_endian>*>(
360 	      parameters->sized_target<size, big_endian>());
361 	unsigned int indx = this->stub_table_index_[shndx];
362 	if (indx < target->stub_tables().size())
363 	  return target->stub_tables()[indx];
364       }
365     return NULL;
366   }
367 
368   void
369   clear_stub_table()
370   {
371     this->stub_table_index_.clear();
372   }
373 
374   uint32_t
375   uniq() const
376   { return this->uniq_; }
377 
378   int
379   abiversion() const
380   { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
381 
382   // Set ABI version for input and output
383   void
384   set_abiversion(int ver);
385 
386   unsigned int
387   st_other (unsigned int symndx) const
388   {
389     return this->st_other_[symndx];
390   }
391 
392   unsigned int
393   ppc64_local_entry_offset(const Symbol* sym) const
394   { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
395 
396   unsigned int
397   ppc64_local_entry_offset(unsigned int symndx) const
398   { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
399 
400   bool
401   ppc64_needs_toc(const Symbol* sym) const
402   { return sym->nonvis() > 1 << 3; }
403 
404   bool
405   ppc64_needs_toc(unsigned int symndx) const
406   { return this->st_other_[symndx] > 1 << 5; }
407 
408   // The contents of the .gnu.attributes section if there is one.
409   const Attributes_section_data*
410   attributes_section_data() const
411   { return this->attributes_section_data_; }
412 
413 private:
414   struct Opd_ent
415   {
416     unsigned int shndx;
417     bool discard : 1;
418     bool gc_mark : 1;
419     Address off;
420   };
421 
422   // Return index into opd_ent_ array for .opd entry at OFF.
423   // .opd entries are 24 bytes long, but they can be spaced 16 bytes
424   // apart when the language doesn't use the last 8-byte word, the
425   // environment pointer.  Thus dividing the entry section offset by
426   // 16 will give an index into opd_ent_ that works for either layout
427   // of .opd.  (It leaves some elements of the vector unused when .opd
428   // entries are spaced 24 bytes apart, but we don't know the spacing
429   // until relocations are processed, and in any case it is possible
430   // for an object to have some entries spaced 16 bytes apart and
431   // others 24 bytes apart.)
432   size_t
433   opd_ent_ndx(size_t off) const
434   { return off >> 4;}
435 
436   // Per object unique identifier
437   uint32_t uniq_;
438 
439   // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
440   unsigned int special_;
441 
442   // For 64-bit the .rela.toc and .toc section shdnx.
443   unsigned int relatoc_;
444   unsigned int toc_;
445 
446   // For 64-bit, whether this object uses small model relocs to access
447   // the toc.
448   bool has_small_toc_reloc_;
449 
450   // Set at the start of gc_process_relocs, when we know opd_ent_
451   // vector is valid.  The flag could be made atomic and set in
452   // do_read_relocs with memory_order_release and then tested with
453   // memory_order_acquire, potentially resulting in fewer entries in
454   // access_from_map_.
455   bool opd_valid_;
456 
457   // Header e_flags
458   elfcpp::Elf_Word e_flags_;
459 
460   // For 64-bit, an array with one entry per 64-bit word in the .toc
461   // section, set if accesses using that word cannot be optimised.
462   std::vector<bool> no_toc_opt_;
463 
464   // The first 8-byte word of an OPD entry gives the address of the
465   // entry point of the function.  Relocatable object files have a
466   // relocation on this word.  The following vector records the
467   // section and offset specified by these relocations.
468   std::vector<Opd_ent> opd_ent_;
469 
470   // References made to this object's .opd section when running
471   // gc_process_relocs for another object, before the opd_ent_ vector
472   // is valid for this object.
473   Access_from access_from_map_;
474 
475   // Whether input section has a 14-bit branch reloc.
476   std::vector<bool> has14_;
477 
478   // The stub table to use for a given input section.
479   std::vector<unsigned int> stub_table_index_;
480 
481   // ELF st_other field for local symbols.
482   std::vector<unsigned char> st_other_;
483 
484   // Object attributes if there is a .gnu.attributes section.
485   Attributes_section_data* attributes_section_data_;
486 };
487 
488 template<int size, bool big_endian>
489 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
490 {
491 public:
492   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
493 
494   Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
495 		 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
496     : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
497       opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
498       attributes_section_data_(NULL)
499   {
500     this->set_abiversion(0);
501   }
502 
503   ~Powerpc_dynobj()
504   { delete this->attributes_section_data_; }
505 
506   // Call Sized_dynobj::do_read_symbols to read the symbols then
507   // read .opd from a dynamic object, filling in opd_ent_ vector,
508   void
509   do_read_symbols(Read_symbols_data*);
510 
511   // The .opd section shndx.
512   unsigned int
513   opd_shndx() const
514   {
515     return this->opd_shndx_;
516   }
517 
518   // The .opd section address.
519   Address
520   opd_address() const
521   {
522     return this->opd_address_;
523   }
524 
525   // Init OPD entry arrays.
526   void
527   init_opd(size_t opd_size)
528   {
529     size_t count = this->opd_ent_ndx(opd_size);
530     this->opd_ent_.resize(count);
531   }
532 
533   // Return section and offset of function entry for .opd + R_OFF.
534   unsigned int
535   get_opd_ent(Address r_off, Address* value = NULL) const
536   {
537     size_t ndx = this->opd_ent_ndx(r_off);
538     gold_assert(ndx < this->opd_ent_.size());
539     gold_assert(this->opd_ent_[ndx].shndx != 0);
540     if (value != NULL)
541       *value = this->opd_ent_[ndx].off;
542     return this->opd_ent_[ndx].shndx;
543   }
544 
545   // Set section and offset of function entry for .opd + R_OFF.
546   void
547   set_opd_ent(Address r_off, unsigned int shndx, Address value)
548   {
549     size_t ndx = this->opd_ent_ndx(r_off);
550     gold_assert(ndx < this->opd_ent_.size());
551     this->opd_ent_[ndx].shndx = shndx;
552     this->opd_ent_[ndx].off = value;
553   }
554 
555   int
556   abiversion() const
557   { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
558 
559   // Set ABI version for input and output.
560   void
561   set_abiversion(int ver);
562 
563   // The contents of the .gnu.attributes section if there is one.
564   const Attributes_section_data*
565   attributes_section_data() const
566   { return this->attributes_section_data_; }
567 
568 private:
569   // Used to specify extent of executable sections.
570   struct Sec_info
571   {
572     Sec_info(Address start_, Address len_, unsigned int shndx_)
573       : start(start_), len(len_), shndx(shndx_)
574     { }
575 
576     bool
577     operator<(const Sec_info& that) const
578     { return this->start < that.start; }
579 
580     Address start;
581     Address len;
582     unsigned int shndx;
583   };
584 
585   struct Opd_ent
586   {
587     unsigned int shndx;
588     Address off;
589   };
590 
591   // Return index into opd_ent_ array for .opd entry at OFF.
592   size_t
593   opd_ent_ndx(size_t off) const
594   { return off >> 4;}
595 
596   // For 64-bit the .opd section shndx and address.
597   unsigned int opd_shndx_;
598   Address opd_address_;
599 
600   // Header e_flags
601   elfcpp::Elf_Word e_flags_;
602 
603   // The first 8-byte word of an OPD entry gives the address of the
604   // entry point of the function.  Records the section and offset
605   // corresponding to the address.  Note that in dynamic objects,
606   // offset is *not* relative to the section.
607   std::vector<Opd_ent> opd_ent_;
608 
609   // Object attributes if there is a .gnu.attributes section.
610   Attributes_section_data* attributes_section_data_;
611 };
612 
613 // Powerpc_copy_relocs class.  Needed to peek at dynamic relocs the
614 // base class will emit.
615 
616 template<int sh_type, int size, bool big_endian>
617 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
618 {
619  public:
620   Powerpc_copy_relocs()
621     : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
622   { }
623 
624   // Emit any saved relocations which turn out to be needed.  This is
625   // called after all the relocs have been scanned.
626   void
627   emit(Output_data_reloc<sh_type, true, size, big_endian>*);
628 };
629 
630 template<int size, bool big_endian>
631 class Target_powerpc : public Sized_target<size, big_endian>
632 {
633  public:
634   typedef
635     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
636   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
637   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
638   typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
639   static const Address invalid_address = static_cast<Address>(0) - 1;
640   // Offset of tp and dtp pointers from start of TLS block.
641   static const Address tp_offset = 0x7000;
642   static const Address dtp_offset = 0x8000;
643 
644   Target_powerpc()
645     : Sized_target<size, big_endian>(&powerpc_info),
646       got_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL), brlt_section_(NULL),
647       glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
648       tlsld_got_offset_(-1U),
649       stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
650       powerxx_stubs_(false), plt_thread_safe_(false), plt_localentry0_(false),
651       plt_localentry0_init_(false), has_localentry0_(false),
652       has_tls_get_addr_opt_(false),
653       relax_failed_(false), relax_fail_count_(0),
654       stub_group_size_(0), savres_section_(0),
655       tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
656       attributes_section_data_(NULL),
657       last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
658   {
659   }
660 
661   // Process the relocations to determine unreferenced sections for
662   // garbage collection.
663   void
664   gc_process_relocs(Symbol_table* symtab,
665 		    Layout* layout,
666 		    Sized_relobj_file<size, big_endian>* object,
667 		    unsigned int data_shndx,
668 		    unsigned int sh_type,
669 		    const unsigned char* prelocs,
670 		    size_t reloc_count,
671 		    Output_section* output_section,
672 		    bool needs_special_offset_handling,
673 		    size_t local_symbol_count,
674 		    const unsigned char* plocal_symbols);
675 
676   // Scan the relocations to look for symbol adjustments.
677   void
678   scan_relocs(Symbol_table* symtab,
679 	      Layout* layout,
680 	      Sized_relobj_file<size, big_endian>* object,
681 	      unsigned int data_shndx,
682 	      unsigned int sh_type,
683 	      const unsigned char* prelocs,
684 	      size_t reloc_count,
685 	      Output_section* output_section,
686 	      bool needs_special_offset_handling,
687 	      size_t local_symbol_count,
688 	      const unsigned char* plocal_symbols);
689 
690   // Map input .toc section to output .got section.
691   const char*
692   do_output_section_name(const Relobj*, const char* name, size_t* plen) const
693   {
694     if (size == 64 && strcmp(name, ".toc") == 0)
695       {
696 	*plen = 4;
697 	return ".got";
698       }
699     return NULL;
700   }
701 
702   // Provide linker defined save/restore functions.
703   void
704   define_save_restore_funcs(Layout*, Symbol_table*);
705 
706   // No stubs unless a final link.
707   bool
708   do_may_relax() const
709   { return !parameters->options().relocatable(); }
710 
711   bool
712   do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
713 
714   void
715   do_plt_fde_location(const Output_data*, unsigned char*,
716 		      uint64_t*, off_t*) const;
717 
718   // Stash info about branches, for stub generation.
719   void
720   push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
721 	      unsigned int data_shndx, Address r_offset,
722 	      unsigned int r_type, unsigned int r_sym, Address addend)
723   {
724     Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
725     this->branch_info_.push_back(info);
726     if (r_type == elfcpp::R_POWERPC_REL14
727 	|| r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
728 	|| r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
729       ppc_object->set_has_14bit_branch(data_shndx);
730   }
731 
732   // Return whether the last branch is a plt call, and if so, mark the
733   // branch as having an R_PPC64_TOCSAVE.
734   bool
735   mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
736 	       unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
737   {
738     return (size == 64
739 	    && !this->branch_info_.empty()
740 	    && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
741 						      r_offset, this, symtab));
742   }
743 
744   // Say the given location, that of a nop in a function prologue with
745   // an R_PPC64_TOCSAVE reloc, will be used to save r2.
746   // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
747   void
748   add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
749 	      unsigned int shndx, Address offset)
750   {
751     Symbol_location loc;
752     loc.object = ppc_object;
753     loc.shndx = shndx;
754     loc.offset = offset;
755     this->tocsave_loc_.insert(loc);
756   }
757 
758   // Accessor
759   const Tocsave_loc
760   tocsave_loc() const
761   {
762     return this->tocsave_loc_;
763   }
764 
765   void
766   do_define_standard_symbols(Symbol_table*, Layout*);
767 
768   // Finalize the sections.
769   void
770   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
771 
772   // Return the value to use for a dynamic which requires special
773   // treatment.
774   uint64_t
775   do_dynsym_value(const Symbol*) const;
776 
777   // Return the PLT address to use for a local symbol.
778   uint64_t
779   do_plt_address_for_local(const Relobj*, unsigned int) const;
780 
781   // Return the PLT address to use for a global symbol.
782   uint64_t
783   do_plt_address_for_global(const Symbol*) const;
784 
785   // Return the offset to use for the GOT_INDX'th got entry which is
786   // for a local tls symbol specified by OBJECT, SYMNDX.
787   int64_t
788   do_tls_offset_for_local(const Relobj* object,
789 			  unsigned int symndx,
790 			  unsigned int got_indx) const;
791 
792   // Return the offset to use for the GOT_INDX'th got entry which is
793   // for global tls symbol GSYM.
794   int64_t
795   do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
796 
797   void
798   do_function_location(Symbol_location*) const;
799 
800   bool
801   do_can_check_for_function_pointers() const
802   { return true; }
803 
804   // Adjust -fsplit-stack code which calls non-split-stack code.
805   void
806   do_calls_non_split(Relobj* object, unsigned int shndx,
807 		     section_offset_type fnoffset, section_size_type fnsize,
808 		     const unsigned char* prelocs, size_t reloc_count,
809 		     unsigned char* view, section_size_type view_size,
810 		     std::string* from, std::string* to) const;
811 
812   // Relocate a section.
813   void
814   relocate_section(const Relocate_info<size, big_endian>*,
815 		   unsigned int sh_type,
816 		   const unsigned char* prelocs,
817 		   size_t reloc_count,
818 		   Output_section* output_section,
819 		   bool needs_special_offset_handling,
820 		   unsigned char* view,
821 		   Address view_address,
822 		   section_size_type view_size,
823 		   const Reloc_symbol_changes*);
824 
825   // Scan the relocs during a relocatable link.
826   void
827   scan_relocatable_relocs(Symbol_table* symtab,
828 			  Layout* layout,
829 			  Sized_relobj_file<size, big_endian>* object,
830 			  unsigned int data_shndx,
831 			  unsigned int sh_type,
832 			  const unsigned char* prelocs,
833 			  size_t reloc_count,
834 			  Output_section* output_section,
835 			  bool needs_special_offset_handling,
836 			  size_t local_symbol_count,
837 			  const unsigned char* plocal_symbols,
838 			  Relocatable_relocs*);
839 
840   // Scan the relocs for --emit-relocs.
841   void
842   emit_relocs_scan(Symbol_table* symtab,
843 		   Layout* layout,
844 		   Sized_relobj_file<size, big_endian>* object,
845 		   unsigned int data_shndx,
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 		   size_t local_symbol_count,
852 		   const unsigned char* plocal_syms,
853 		   Relocatable_relocs* rr);
854 
855   // Emit relocations for a section.
856   void
857   relocate_relocs(const Relocate_info<size, big_endian>*,
858 		  unsigned int sh_type,
859 		  const unsigned char* prelocs,
860 		  size_t reloc_count,
861 		  Output_section* output_section,
862 		  typename elfcpp::Elf_types<size>::Elf_Off
863                     offset_in_output_section,
864 		  unsigned char*,
865 		  Address view_address,
866 		  section_size_type,
867 		  unsigned char* reloc_view,
868 		  section_size_type reloc_view_size);
869 
870   // Return whether SYM is defined by the ABI.
871   bool
872   do_is_defined_by_abi(const Symbol* sym) const
873   {
874     return strcmp(sym->name(), "__tls_get_addr") == 0;
875   }
876 
877   // Return the size of the GOT section.
878   section_size_type
879   got_size() const
880   {
881     gold_assert(this->got_ != NULL);
882     return this->got_->data_size();
883   }
884 
885   // Get the PLT section.
886   const Output_data_plt_powerpc<size, big_endian>*
887   plt_section() const
888   {
889     gold_assert(this->plt_ != NULL);
890     return this->plt_;
891   }
892 
893   // Get the IPLT section.
894   const Output_data_plt_powerpc<size, big_endian>*
895   iplt_section() const
896   {
897     gold_assert(this->iplt_ != NULL);
898     return this->iplt_;
899   }
900 
901   // Get the LPLT section.
902   const Output_data_plt_powerpc<size, big_endian>*
903   lplt_section() const
904   {
905     return this->lplt_;
906   }
907 
908   // Return the plt offset and section for the given global sym.
909   Address
910   plt_off(const Symbol* gsym,
911 	  const Output_data_plt_powerpc<size, big_endian>** sec) const
912   {
913     if (gsym->type() == elfcpp::STT_GNU_IFUNC
914 	&& gsym->can_use_relative_reloc(false))
915       *sec = this->iplt_section();
916     else
917       *sec = this->plt_section();
918     return gsym->plt_offset();
919   }
920 
921   // Return the plt offset and section for the given local sym.
922   Address
923   plt_off(const Sized_relobj_file<size, big_endian>* relobj,
924 	  unsigned int local_sym_index,
925 	  const Output_data_plt_powerpc<size, big_endian>** sec) const
926   {
927     const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
928     if (lsym->is_ifunc_symbol())
929       *sec = this->iplt_section();
930     else
931       *sec = this->lplt_section();
932     return relobj->local_plt_offset(local_sym_index);
933   }
934 
935   // Get the .glink section.
936   const Output_data_glink<size, big_endian>*
937   glink_section() const
938   {
939     gold_assert(this->glink_ != NULL);
940     return this->glink_;
941   }
942 
943   Output_data_glink<size, big_endian>*
944   glink_section()
945   {
946     gold_assert(this->glink_ != NULL);
947     return this->glink_;
948   }
949 
950   bool has_glink() const
951   { return this->glink_ != NULL; }
952 
953   // Get the GOT section.
954   const Output_data_got_powerpc<size, big_endian>*
955   got_section() const
956   {
957     gold_assert(this->got_ != NULL);
958     return this->got_;
959   }
960 
961   // Get the GOT section, creating it if necessary.
962   Output_data_got_powerpc<size, big_endian>*
963   got_section(Symbol_table*, Layout*);
964 
965   Object*
966   do_make_elf_object(const std::string&, Input_file*, off_t,
967 		     const elfcpp::Ehdr<size, big_endian>&);
968 
969   // Return the number of entries in the GOT.
970   unsigned int
971   got_entry_count() const
972   {
973     if (this->got_ == NULL)
974       return 0;
975     return this->got_size() / (size / 8);
976   }
977 
978   // Return the number of entries in the PLT.
979   unsigned int
980   plt_entry_count() const;
981 
982   // Return the offset of the first non-reserved PLT entry.
983   unsigned int
984   first_plt_entry_offset() const
985   {
986     if (size == 32)
987       return 0;
988     if (this->abiversion() >= 2)
989       return 16;
990     return 24;
991   }
992 
993   // Return the size of each PLT entry.
994   unsigned int
995   plt_entry_size() const
996   {
997     if (size == 32)
998       return 4;
999     if (this->abiversion() >= 2)
1000       return 8;
1001     return 24;
1002   }
1003 
1004   Output_data_save_res<size, big_endian>*
1005   savres_section() const
1006   {
1007     return this->savres_section_;
1008   }
1009 
1010   // Add any special sections for this symbol to the gc work list.
1011   // For powerpc64, this adds the code section of a function
1012   // descriptor.
1013   void
1014   do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1015 
1016   // Handle target specific gc actions when adding a gc reference from
1017   // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1018   // and DST_OFF.  For powerpc64, this adds a referenc to the code
1019   // section of a function descriptor.
1020   void
1021   do_gc_add_reference(Symbol_table* symtab,
1022 		      Relobj* src_obj,
1023 		      unsigned int src_shndx,
1024 		      Relobj* dst_obj,
1025 		      unsigned int dst_shndx,
1026 		      Address dst_off) const;
1027 
1028   typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1029   const Stub_tables&
1030   stub_tables() const
1031   { return this->stub_tables_; }
1032 
1033   const Output_data_brlt_powerpc<size, big_endian>*
1034   brlt_section() const
1035   { return this->brlt_section_; }
1036 
1037   void
1038   add_branch_lookup_table(Address to)
1039   {
1040     unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1041     this->branch_lookup_table_.insert(std::make_pair(to, off));
1042   }
1043 
1044   Address
1045   find_branch_lookup_table(Address to)
1046   {
1047     typename Branch_lookup_table::const_iterator p
1048       = this->branch_lookup_table_.find(to);
1049     return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1050   }
1051 
1052   void
1053   write_branch_lookup_table(unsigned char *oview)
1054   {
1055     for (typename Branch_lookup_table::const_iterator p
1056 	   = this->branch_lookup_table_.begin();
1057 	 p != this->branch_lookup_table_.end();
1058 	 ++p)
1059       {
1060 	elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1061       }
1062   }
1063 
1064   // Wrapper used after relax to define a local symbol in output data,
1065   // from the end if value < 0.
1066   void
1067   define_local(Symbol_table* symtab, const char* name,
1068 	       Output_data* od, Address value, unsigned int symsize)
1069   {
1070     Symbol* sym
1071       = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1072 				      od, value, symsize, elfcpp::STT_NOTYPE,
1073 				      elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1074 				      static_cast<Signed_address>(value) < 0,
1075 				      false);
1076     // We are creating this symbol late, so need to fix up things
1077     // done early in Layout::finalize.
1078     sym->set_dynsym_index(-1U);
1079   }
1080 
1081   bool
1082   powerxx_stubs() const
1083   { return this->powerxx_stubs_; }
1084 
1085   void
1086   set_powerxx_stubs()
1087   {
1088     this->powerxx_stubs_ = true;
1089   }
1090 
1091   bool
1092   plt_thread_safe() const
1093   { return this->plt_thread_safe_; }
1094 
1095   bool
1096   plt_localentry0() const
1097   { return this->plt_localentry0_; }
1098 
1099   void
1100   set_has_localentry0()
1101   {
1102     this->has_localentry0_ = true;
1103   }
1104 
1105   bool
1106   is_elfv2_localentry0(const Symbol* gsym) const
1107   {
1108     return (size == 64
1109 	    && this->abiversion() >= 2
1110 	    && this->plt_localentry0()
1111 	    && gsym->type() == elfcpp::STT_FUNC
1112 	    && gsym->is_defined()
1113 	    && gsym->nonvis() >> 3 == 0
1114 	    && !gsym->non_zero_localentry());
1115   }
1116 
1117   bool
1118   is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1119 		       unsigned int r_sym) const
1120   {
1121     const Powerpc_relobj<size, big_endian>* ppc_object
1122       = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1123 
1124     if (size == 64
1125 	&& this->abiversion() >= 2
1126 	&& this->plt_localentry0()
1127 	&& ppc_object->st_other(r_sym) >> 5 == 0)
1128       {
1129 	const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1130 	bool is_ordinary;
1131 	if (!psymval->is_ifunc_symbol()
1132 	    && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1133 	    && is_ordinary)
1134 	  return true;
1135       }
1136     return false;
1137   }
1138 
1139   // Remember any symbols seen with non-zero localentry, even those
1140   // not providing a definition
1141   bool
1142   resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1143 	  const char*)
1144   {
1145     if (size == 64)
1146       {
1147 	unsigned char st_other = sym.get_st_other();
1148 	if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1149 	  to->set_non_zero_localentry();
1150       }
1151     // We haven't resolved anything, continue normal processing.
1152     return false;
1153   }
1154 
1155   int
1156   abiversion() const
1157   { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1158 
1159   void
1160   set_abiversion(int ver)
1161   {
1162     elfcpp::Elf_Word flags = this->processor_specific_flags();
1163     flags &= ~elfcpp::EF_PPC64_ABI;
1164     flags |= ver & elfcpp::EF_PPC64_ABI;
1165     this->set_processor_specific_flags(flags);
1166   }
1167 
1168   Symbol*
1169   tls_get_addr_opt() const
1170   { return this->tls_get_addr_opt_; }
1171 
1172   Symbol*
1173   tls_get_addr() const
1174   { return this->tls_get_addr_; }
1175 
1176   // If optimizing __tls_get_addr calls, whether this is the
1177   // "__tls_get_addr" symbol.
1178   bool
1179   is_tls_get_addr_opt(const Symbol* gsym) const
1180   {
1181     return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1182 				       || gsym == this->tls_get_addr_opt_);
1183   }
1184 
1185   bool
1186   replace_tls_get_addr(const Symbol* gsym) const
1187   { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1188 
1189   void
1190   set_has_tls_get_addr_opt()
1191   { this->has_tls_get_addr_opt_ = true; }
1192 
1193   // Offset to toc save stack slot
1194   int
1195   stk_toc() const
1196   { return this->abiversion() < 2 ? 40 : 24; }
1197 
1198   // Offset to linker save stack slot.  ELFv2 doesn't have a linker word,
1199   // so use the CR save slot.  Used only by __tls_get_addr call stub,
1200   // relying on __tls_get_addr not saving CR itself.
1201   int
1202   stk_linker() const
1203   { return this->abiversion() < 2 ? 32 : 8; }
1204 
1205   // Merge object attributes from input object with those in the output.
1206   void
1207   merge_object_attributes(const char*, const Attributes_section_data*);
1208 
1209  private:
1210 
1211   class Track_tls
1212   {
1213   public:
1214     enum Tls_get_addr
1215     {
1216       NOT_EXPECTED = 0,
1217       EXPECTED = 1,
1218       SKIP = 2,
1219       NORMAL = 3
1220     };
1221 
1222     Track_tls()
1223       : tls_get_addr_state_(NOT_EXPECTED),
1224 	relinfo_(NULL), relnum_(0), r_offset_(0)
1225     { }
1226 
1227     ~Track_tls()
1228     {
1229       if (this->tls_get_addr_state_ != NOT_EXPECTED)
1230 	this->missing();
1231     }
1232 
1233     void
1234     missing(void)
1235     {
1236       if (this->relinfo_ != NULL)
1237 	gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1238 			       _("missing expected __tls_get_addr call"));
1239     }
1240 
1241     void
1242     expect_tls_get_addr_call(
1243 	const Relocate_info<size, big_endian>* relinfo,
1244 	size_t relnum,
1245 	Address r_offset)
1246     {
1247       this->tls_get_addr_state_ = EXPECTED;
1248       this->relinfo_ = relinfo;
1249       this->relnum_ = relnum;
1250       this->r_offset_ = r_offset;
1251     }
1252 
1253     void
1254     expect_tls_get_addr_call()
1255     { this->tls_get_addr_state_ = EXPECTED; }
1256 
1257     void
1258     skip_next_tls_get_addr_call()
1259     {this->tls_get_addr_state_ = SKIP; }
1260 
1261     Tls_get_addr
1262     maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1263 				 unsigned int r_type, const Symbol* gsym)
1264     {
1265       bool is_tls_call
1266 	= ((r_type == elfcpp::R_POWERPC_REL24
1267 	    || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1268 	    || r_type == elfcpp::R_PPC_PLTREL24
1269 	    || is_plt16_reloc<size>(r_type)
1270 	    || r_type == elfcpp::R_PPC64_PLT_PCREL34
1271 	    || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1272 	    || r_type == elfcpp::R_POWERPC_PLTSEQ
1273 	    || r_type == elfcpp::R_POWERPC_PLTCALL
1274 	    || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1275 	    || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1276 	   && gsym != NULL
1277 	   && (gsym == target->tls_get_addr()
1278 	       || gsym == target->tls_get_addr_opt()));
1279       Tls_get_addr last_tls = this->tls_get_addr_state_;
1280       this->tls_get_addr_state_ = NOT_EXPECTED;
1281       if (is_tls_call && last_tls != EXPECTED)
1282 	return last_tls;
1283       else if (!is_tls_call && last_tls != NOT_EXPECTED)
1284 	{
1285 	  this->missing();
1286 	  return EXPECTED;
1287 	}
1288       return NORMAL;
1289     }
1290 
1291   private:
1292     // What we're up to regarding calls to __tls_get_addr.
1293     // On powerpc, the branch and link insn making a call to
1294     // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1295     // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1296     // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1297     // The marker relocation always comes first, and has the same
1298     // symbol as the reloc on the insn setting up the __tls_get_addr
1299     // argument.  This ties the arg setup insn with the call insn,
1300     // allowing ld to safely optimize away the call.  We check that
1301     // every call to __tls_get_addr has a marker relocation, and that
1302     // every marker relocation is on a call to __tls_get_addr.
1303     Tls_get_addr tls_get_addr_state_;
1304     // Info about the last reloc for error message.
1305     const Relocate_info<size, big_endian>* relinfo_;
1306     size_t relnum_;
1307     Address r_offset_;
1308   };
1309 
1310   // The class which scans relocations.
1311   class Scan : protected Track_tls
1312   {
1313   public:
1314     typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1315 
1316     Scan()
1317       : Track_tls(), issued_non_pic_error_(false)
1318     { }
1319 
1320     static inline int
1321     get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1322 
1323     inline void
1324     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1325 	  Sized_relobj_file<size, big_endian>* object,
1326 	  unsigned int data_shndx,
1327 	  Output_section* output_section,
1328 	  const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1329 	  const elfcpp::Sym<size, big_endian>& lsym,
1330 	  bool is_discarded);
1331 
1332     inline void
1333     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1334 	   Sized_relobj_file<size, big_endian>* object,
1335 	   unsigned int data_shndx,
1336 	   Output_section* output_section,
1337 	   const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1338 	   Symbol* gsym);
1339 
1340     inline bool
1341     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1342 					Target_powerpc* ,
1343 					Sized_relobj_file<size, big_endian>* relobj,
1344 					unsigned int ,
1345 					Output_section* ,
1346 					const elfcpp::Rela<size, big_endian>& ,
1347 					unsigned int r_type,
1348 					const elfcpp::Sym<size, big_endian>&)
1349     {
1350       // PowerPC64 .opd is not folded, so any identical function text
1351       // may be folded and we'll still keep function addresses distinct.
1352       // That means no reloc is of concern here.
1353       if (size == 64)
1354 	{
1355 	  Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1356 	    <Powerpc_relobj<size, big_endian>*>(relobj);
1357 	  if (ppcobj->abiversion() == 1)
1358 	    return false;
1359 	}
1360       // For 32-bit and ELFv2, conservatively assume anything but calls to
1361       // function code might be taking the address of the function.
1362       return !is_branch_reloc<size>(r_type);
1363     }
1364 
1365     inline bool
1366     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1367 					 Target_powerpc* ,
1368 					 Sized_relobj_file<size, big_endian>* relobj,
1369 					 unsigned int ,
1370 					 Output_section* ,
1371 					 const elfcpp::Rela<size, big_endian>& ,
1372 					 unsigned int r_type,
1373 					 Symbol*)
1374     {
1375       // As above.
1376       if (size == 64)
1377 	{
1378 	  Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1379 	    <Powerpc_relobj<size, big_endian>*>(relobj);
1380 	  if (ppcobj->abiversion() == 1)
1381 	    return false;
1382 	}
1383       return !is_branch_reloc<size>(r_type);
1384     }
1385 
1386     static bool
1387     reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1388 			      Sized_relobj_file<size, big_endian>* object,
1389 			      unsigned int r_type, bool report_err);
1390 
1391   private:
1392     static void
1393     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1394 			    unsigned int r_type);
1395 
1396     static void
1397     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1398 			     unsigned int r_type, Symbol*);
1399 
1400     static void
1401     generate_tls_call(Symbol_table* symtab, Layout* layout,
1402 		      Target_powerpc* target);
1403 
1404     void
1405     check_non_pic(Relobj*, unsigned int r_type);
1406 
1407     // Whether we have issued an error about a non-PIC compilation.
1408     bool issued_non_pic_error_;
1409   };
1410 
1411   bool
1412   symval_for_branch(const Symbol_table* symtab,
1413 		    const Sized_symbol<size>* gsym,
1414 		    Powerpc_relobj<size, big_endian>* object,
1415 		    Address *value, unsigned int *dest_shndx);
1416 
1417   // The class which implements relocation.
1418   class Relocate : protected Track_tls
1419   {
1420    public:
1421     // Use 'at' branch hints when true, 'y' when false.
1422     // FIXME maybe: set this with an option.
1423     static const bool is_isa_v2 = true;
1424 
1425     Relocate()
1426       : Track_tls()
1427     { }
1428 
1429     // Do a relocation.  Return false if the caller should not issue
1430     // any warnings about this relocation.
1431     inline bool
1432     relocate(const Relocate_info<size, big_endian>*, unsigned int,
1433 	     Target_powerpc*, Output_section*, size_t, const unsigned char*,
1434 	     const Sized_symbol<size>*, const Symbol_value<size>*,
1435 	     unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1436 	     section_size_type);
1437   };
1438 
1439   class Relocate_comdat_behavior
1440   {
1441    public:
1442     // Decide what the linker should do for relocations that refer to
1443     // discarded comdat sections.
1444     inline Comdat_behavior
1445     get(const char* name)
1446     {
1447       gold::Default_comdat_behavior default_behavior;
1448       Comdat_behavior ret = default_behavior.get(name);
1449       if (ret == CB_ERROR)
1450 	{
1451 	  if (size == 32
1452 	      && (strcmp(name, ".fixup") == 0
1453 		  || strcmp(name, ".got2") == 0))
1454 	    ret = CB_IGNORE;
1455 	  if (size == 64
1456 	      && (strcmp(name, ".opd") == 0
1457 		  || strcmp(name, ".toc") == 0
1458 		  || strcmp(name, ".toc1") == 0))
1459 	    ret = CB_IGNORE;
1460 	}
1461       return ret;
1462     }
1463   };
1464 
1465   // Optimize the TLS relocation type based on what we know about the
1466   // symbol.  IS_FINAL is true if the final address of this symbol is
1467   // known at link time.
1468 
1469   tls::Tls_optimization
1470   optimize_tls_gd(bool is_final)
1471   {
1472     // If we are generating a shared library, then we can't do anything
1473     // in the linker.
1474     if (parameters->options().shared()
1475 	|| !parameters->options().tls_optimize())
1476       return tls::TLSOPT_NONE;
1477 
1478     if (!is_final)
1479       return tls::TLSOPT_TO_IE;
1480     return tls::TLSOPT_TO_LE;
1481   }
1482 
1483   tls::Tls_optimization
1484   optimize_tls_ld()
1485   {
1486     if (parameters->options().shared()
1487 	|| !parameters->options().tls_optimize())
1488       return tls::TLSOPT_NONE;
1489 
1490     return tls::TLSOPT_TO_LE;
1491   }
1492 
1493   tls::Tls_optimization
1494   optimize_tls_ie(bool is_final)
1495   {
1496     if (!is_final
1497 	|| parameters->options().shared()
1498 	|| !parameters->options().tls_optimize())
1499       return tls::TLSOPT_NONE;
1500 
1501     return tls::TLSOPT_TO_LE;
1502   }
1503 
1504   // Create glink.
1505   void
1506   make_glink_section(Layout*);
1507 
1508   // Create the PLT section.
1509   void
1510   make_plt_section(Symbol_table*, Layout*);
1511 
1512   void
1513   make_iplt_section(Symbol_table*, Layout*);
1514 
1515   void
1516   make_lplt_section(Layout*);
1517 
1518   void
1519   make_brlt_section(Layout*);
1520 
1521   // Create a PLT entry for a global symbol.
1522   void
1523   make_plt_entry(Symbol_table*, Layout*, Symbol*);
1524 
1525   // Create a PLT entry for a local IFUNC symbol.
1526   void
1527   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1528 			     Sized_relobj_file<size, big_endian>*,
1529 			     unsigned int);
1530 
1531   // Create a PLT entry for a local non-IFUNC symbol.
1532   void
1533   make_local_plt_entry(Layout*,
1534 		       Sized_relobj_file<size, big_endian>*,
1535 		       unsigned int);
1536 
1537 
1538   // Create a GOT entry for local dynamic __tls_get_addr.
1539   unsigned int
1540   tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1541 		   Sized_relobj_file<size, big_endian>* object);
1542 
1543   unsigned int
1544   tlsld_got_offset() const
1545   {
1546     return this->tlsld_got_offset_;
1547   }
1548 
1549   // Get the dynamic reloc section, creating it if necessary.
1550   Reloc_section*
1551   rela_dyn_section(Layout*);
1552 
1553   // Similarly, but for ifunc symbols get the one for ifunc.
1554   Reloc_section*
1555   rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1556 
1557   // Copy a relocation against a global symbol.
1558   void
1559   copy_reloc(Symbol_table* symtab, Layout* layout,
1560 	     Sized_relobj_file<size, big_endian>* object,
1561 	     unsigned int shndx, Output_section* output_section,
1562 	     Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1563   {
1564     unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1565     this->copy_relocs_.copy_reloc(symtab, layout,
1566 				  symtab->get_sized_symbol<size>(sym),
1567 				  object, shndx, output_section,
1568 				  r_type, reloc.get_r_offset(),
1569 				  reloc.get_r_addend(),
1570 				  this->rela_dyn_section(layout));
1571   }
1572 
1573   // Look over all the input sections, deciding where to place stubs.
1574   void
1575   group_sections(Layout*, const Task*, bool);
1576 
1577   // Sort output sections by address.
1578   struct Sort_sections
1579   {
1580     bool
1581     operator()(const Output_section* sec1, const Output_section* sec2)
1582     { return sec1->address() < sec2->address(); }
1583   };
1584 
1585   class Branch_info
1586   {
1587    public:
1588     Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1589 		unsigned int data_shndx,
1590 		Address r_offset,
1591 		unsigned int r_type,
1592 		unsigned int r_sym,
1593 		Address addend)
1594       : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1595 	r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1596     { }
1597 
1598     ~Branch_info()
1599     { }
1600 
1601     // Return whether this branch is going via a plt call stub, and if
1602     // so, mark it as having an R_PPC64_TOCSAVE.
1603     bool
1604     mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1605 		 unsigned int shndx, Address offset,
1606 		 Target_powerpc* target, Symbol_table* symtab);
1607 
1608     // If this branch needs a plt call stub, or a long branch stub, make one.
1609     bool
1610     make_stub(Stub_table<size, big_endian>*,
1611 	      Stub_table<size, big_endian>*,
1612 	      Symbol_table*) const;
1613 
1614    private:
1615     // The branch location..
1616     Powerpc_relobj<size, big_endian>* object_;
1617     unsigned int shndx_;
1618     Address offset_;
1619     // ..and the branch type and destination.
1620     unsigned int r_type_ : 31;
1621     unsigned int tocsave_ : 1;
1622     unsigned int r_sym_;
1623     Address addend_;
1624   };
1625 
1626   // Information about this specific target which we pass to the
1627   // general Target structure.
1628   static Target::Target_info powerpc_info;
1629 
1630   // The types of GOT entries needed for this platform.
1631   // These values are exposed to the ABI in an incremental link.
1632   // Do not renumber existing values without changing the version
1633   // number of the .gnu_incremental_inputs section.
1634   enum Got_type
1635   {
1636     GOT_TYPE_STANDARD,
1637     GOT_TYPE_TLSGD,	// double entry for @got@tlsgd
1638     GOT_TYPE_DTPREL,	// entry for @got@dtprel
1639     GOT_TYPE_TPREL	// entry for @got@tprel
1640   };
1641 
1642   // The GOT section.
1643   Output_data_got_powerpc<size, big_endian>* got_;
1644   // The PLT section.  This is a container for a table of addresses,
1645   // and their relocations.  Each address in the PLT has a dynamic
1646   // relocation (R_*_JMP_SLOT) and each address will have a
1647   // corresponding entry in .glink for lazy resolution of the PLT.
1648   // ppc32 initialises the PLT to point at the .glink entry, while
1649   // ppc64 leaves this to ld.so.  To make a call via the PLT, the
1650   // linker adds a stub that loads the PLT entry into ctr then
1651   // branches to ctr.  There may be more than one stub for each PLT
1652   // entry.  DT_JMPREL points at the first PLT dynamic relocation and
1653   // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1654   Output_data_plt_powerpc<size, big_endian>* plt_;
1655   // The IPLT section.  Like plt_, this is a container for a table of
1656   // addresses and their relocations, specifically for STT_GNU_IFUNC
1657   // functions that resolve locally (STT_GNU_IFUNC functions that
1658   // don't resolve locally go in PLT).  Unlike plt_, these have no
1659   // entry in .glink for lazy resolution, and the relocation section
1660   // does not have a 1-1 correspondence with IPLT addresses.  In fact,
1661   // the relocation section may contain relocations against
1662   // STT_GNU_IFUNC symbols at locations outside of IPLT.  The
1663   // relocation section will appear at the end of other dynamic
1664   // relocations, so that ld.so applies these relocations after other
1665   // dynamic relocations.  In a static executable, the relocation
1666   // section is emitted and marked with __rela_iplt_start and
1667   // __rela_iplt_end symbols.
1668   Output_data_plt_powerpc<size, big_endian>* iplt_;
1669   // A PLT style section for local, non-ifunc symbols
1670   Output_data_plt_powerpc<size, big_endian>* lplt_;
1671   // Section holding long branch destinations.
1672   Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1673   // The .glink section.
1674   Output_data_glink<size, big_endian>* glink_;
1675   // The dynamic reloc section.
1676   Reloc_section* rela_dyn_;
1677   // Relocs saved to avoid a COPY reloc.
1678   Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1679   // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1680   unsigned int tlsld_got_offset_;
1681 
1682   Stub_tables stub_tables_;
1683   typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1684   Branch_lookup_table branch_lookup_table_;
1685 
1686   typedef std::vector<Branch_info> Branches;
1687   Branches branch_info_;
1688   Tocsave_loc tocsave_loc_;
1689 
1690   bool powerxx_stubs_;
1691   bool plt_thread_safe_;
1692   bool plt_localentry0_;
1693   bool plt_localentry0_init_;
1694   bool has_localentry0_;
1695   bool has_tls_get_addr_opt_;
1696 
1697   bool relax_failed_;
1698   int relax_fail_count_;
1699   int32_t stub_group_size_;
1700 
1701   Output_data_save_res<size, big_endian> *savres_section_;
1702 
1703   // The "__tls_get_addr" symbol, if present
1704   Symbol* tls_get_addr_;
1705   // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1706   Symbol* tls_get_addr_opt_;
1707 
1708   // Attributes in output.
1709   Attributes_section_data* attributes_section_data_;
1710 
1711   // Last input file to change various attribute tags
1712   const char* last_fp_;
1713   const char* last_ld_;
1714   const char* last_vec_;
1715   const char* last_struct_;
1716 };
1717 
1718 template<>
1719 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1720 {
1721   32,			// size
1722   true,			// is_big_endian
1723   elfcpp::EM_PPC,	// machine_code
1724   false,		// has_make_symbol
1725   false,		// has_resolve
1726   false,		// has_code_fill
1727   true,			// is_default_stack_executable
1728   false,		// can_icf_inline_merge_sections
1729   '\0',			// wrap_char
1730   "/usr/lib/ld.so.1",	// dynamic_linker
1731   0x10000000,		// default_text_segment_address
1732   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1733   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1734   false,		// isolate_execinstr
1735   0,			// rosegment_gap
1736   elfcpp::SHN_UNDEF,	// small_common_shndx
1737   elfcpp::SHN_UNDEF,	// large_common_shndx
1738   0,			// small_common_section_flags
1739   0,			// large_common_section_flags
1740   NULL,			// attributes_section
1741   NULL,			// attributes_vendor
1742   "_start",		// entry_symbol_name
1743   32,			// hash_entry_size
1744   elfcpp::SHT_PROGBITS,	// unwind_section_type
1745 };
1746 
1747 template<>
1748 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1749 {
1750   32,			// size
1751   false,		// is_big_endian
1752   elfcpp::EM_PPC,	// machine_code
1753   false,		// has_make_symbol
1754   false,		// has_resolve
1755   false,		// has_code_fill
1756   true,			// is_default_stack_executable
1757   false,		// can_icf_inline_merge_sections
1758   '\0',			// wrap_char
1759   "/usr/lib/ld.so.1",	// dynamic_linker
1760   0x10000000,		// default_text_segment_address
1761   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1762   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1763   false,		// isolate_execinstr
1764   0,			// rosegment_gap
1765   elfcpp::SHN_UNDEF,	// small_common_shndx
1766   elfcpp::SHN_UNDEF,	// large_common_shndx
1767   0,			// small_common_section_flags
1768   0,			// large_common_section_flags
1769   NULL,			// attributes_section
1770   NULL,			// attributes_vendor
1771   "_start",		// entry_symbol_name
1772   32,			// hash_entry_size
1773   elfcpp::SHT_PROGBITS,	// unwind_section_type
1774 };
1775 
1776 template<>
1777 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1778 {
1779   64,			// size
1780   true,			// is_big_endian
1781   elfcpp::EM_PPC64,	// machine_code
1782   false,		// has_make_symbol
1783   true,			// has_resolve
1784   false,		// has_code_fill
1785   false,		// is_default_stack_executable
1786   false,		// can_icf_inline_merge_sections
1787   '\0',			// wrap_char
1788   "/usr/lib/ld.so.1",	// dynamic_linker
1789   0x10000000,		// default_text_segment_address
1790   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1791   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1792   false,		// isolate_execinstr
1793   0,			// rosegment_gap
1794   elfcpp::SHN_UNDEF,	// small_common_shndx
1795   elfcpp::SHN_UNDEF,	// large_common_shndx
1796   0,			// small_common_section_flags
1797   0,			// large_common_section_flags
1798   NULL,			// attributes_section
1799   NULL,			// attributes_vendor
1800   "_start",		// entry_symbol_name
1801   32,			// hash_entry_size
1802   elfcpp::SHT_PROGBITS,	// unwind_section_type
1803 };
1804 
1805 template<>
1806 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1807 {
1808   64,			// size
1809   false,		// is_big_endian
1810   elfcpp::EM_PPC64,	// machine_code
1811   false,		// has_make_symbol
1812   true,			// has_resolve
1813   false,		// has_code_fill
1814   false,		// is_default_stack_executable
1815   false,		// can_icf_inline_merge_sections
1816   '\0',			// wrap_char
1817   "/usr/lib/ld.so.1",	// dynamic_linker
1818   0x10000000,		// default_text_segment_address
1819   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1820   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1821   false,		// isolate_execinstr
1822   0,			// rosegment_gap
1823   elfcpp::SHN_UNDEF,	// small_common_shndx
1824   elfcpp::SHN_UNDEF,	// large_common_shndx
1825   0,			// small_common_section_flags
1826   0,			// large_common_section_flags
1827   NULL,			// attributes_section
1828   NULL,			// attributes_vendor
1829   "_start",		// entry_symbol_name
1830   32,			// hash_entry_size
1831   elfcpp::SHT_PROGBITS,	// unwind_section_type
1832 };
1833 
1834 template<int size>
1835 inline bool
1836 is_branch_reloc(unsigned int r_type)
1837 {
1838   return (r_type == elfcpp::R_POWERPC_REL24
1839 	  || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1840 	  || r_type == elfcpp::R_PPC_PLTREL24
1841 	  || r_type == elfcpp::R_PPC_LOCAL24PC
1842 	  || r_type == elfcpp::R_POWERPC_REL14
1843 	  || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1844 	  || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1845 	  || r_type == elfcpp::R_POWERPC_ADDR24
1846 	  || r_type == elfcpp::R_POWERPC_ADDR14
1847 	  || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1848 	  || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1849 }
1850 
1851 // Reloc resolves to plt entry.
1852 template<int size>
1853 inline bool
1854 is_plt16_reloc(unsigned int r_type)
1855 {
1856   return (r_type == elfcpp::R_POWERPC_PLT16_LO
1857 	  || r_type == elfcpp::R_POWERPC_PLT16_HI
1858 	  || r_type == elfcpp::R_POWERPC_PLT16_HA
1859 	  || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1860 }
1861 
1862 // If INSN is an opcode that may be used with an @tls operand, return
1863 // the transformed insn for TLS optimisation, otherwise return 0.  If
1864 // REG is non-zero only match an insn with RB or RA equal to REG.
1865 uint32_t
1866 at_tls_transform(uint32_t insn, unsigned int reg)
1867 {
1868   if ((insn & (0x3f << 26)) != 31 << 26)
1869     return 0;
1870 
1871   unsigned int rtra;
1872   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1873     rtra = insn & ((1 << 26) - (1 << 16));
1874   else if (((insn >> 16) & 0x1f) == reg)
1875     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1876   else
1877     return 0;
1878 
1879   if ((insn & (0x3ff << 1)) == 266 << 1)
1880     // add -> addi
1881     insn = 14 << 26;
1882   else if ((insn & (0x1f << 1)) == 23 << 1
1883 	   && ((insn & (0x1f << 6)) < 14 << 6
1884 	       || ((insn & (0x1f << 6)) >= 16 << 6
1885 		   && (insn & (0x1f << 6)) < 24 << 6)))
1886     // load and store indexed -> dform
1887     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1888   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1889     // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1890     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1891   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1892     // lwax -> lwa
1893     insn = (58 << 26) | 2;
1894   else
1895     return 0;
1896   insn |= rtra;
1897   return insn;
1898 }
1899 
1900 
1901 template<int size, bool big_endian>
1902 class Powerpc_relocate_functions
1903 {
1904 public:
1905   enum Overflow_check
1906   {
1907     CHECK_NONE,
1908     CHECK_SIGNED,
1909     CHECK_UNSIGNED,
1910     CHECK_BITFIELD,
1911     CHECK_LOW_INSN,
1912     CHECK_HIGH_INSN
1913   };
1914 
1915   enum Status
1916   {
1917     STATUS_OK,
1918     STATUS_OVERFLOW
1919   };
1920 
1921 private:
1922   typedef Powerpc_relocate_functions<size, big_endian> This;
1923   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1924   typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1925 
1926   template<int valsize>
1927   static inline bool
1928   has_overflow_signed(Address value)
1929   {
1930     // limit = 1 << (valsize - 1) without shift count exceeding size of type
1931     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1932     limit <<= ((valsize - 1) >> 1);
1933     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1934     return value + limit > (limit << 1) - 1;
1935   }
1936 
1937   template<int valsize>
1938   static inline bool
1939   has_overflow_unsigned(Address value)
1940   {
1941     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1942     limit <<= ((valsize - 1) >> 1);
1943     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1944     return value > (limit << 1) - 1;
1945   }
1946 
1947   template<int valsize>
1948   static inline bool
1949   has_overflow_bitfield(Address value)
1950   {
1951     return (has_overflow_unsigned<valsize>(value)
1952 	    && has_overflow_signed<valsize>(value));
1953   }
1954 
1955   template<int valsize>
1956   static inline Status
1957   overflowed(Address value, Overflow_check overflow)
1958   {
1959     if (overflow == CHECK_SIGNED)
1960       {
1961 	if (has_overflow_signed<valsize>(value))
1962 	  return STATUS_OVERFLOW;
1963       }
1964     else if (overflow == CHECK_UNSIGNED)
1965       {
1966 	if (has_overflow_unsigned<valsize>(value))
1967 	  return STATUS_OVERFLOW;
1968       }
1969     else if (overflow == CHECK_BITFIELD)
1970       {
1971 	if (has_overflow_bitfield<valsize>(value))
1972 	  return STATUS_OVERFLOW;
1973       }
1974     return STATUS_OK;
1975   }
1976 
1977   // Do a simple RELA relocation
1978   template<int fieldsize, int valsize>
1979   static inline Status
1980   rela(unsigned char* view, Address value, Overflow_check overflow)
1981   {
1982     typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1983     Valtype* wv = reinterpret_cast<Valtype*>(view);
1984     elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
1985     return overflowed<valsize>(value, overflow);
1986   }
1987 
1988   template<int fieldsize, int valsize>
1989   static inline Status
1990   rela(unsigned char* view,
1991        unsigned int right_shift,
1992        typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
1993        Address value,
1994        Overflow_check overflow)
1995   {
1996     typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
1997     Valtype* wv = reinterpret_cast<Valtype*>(view);
1998     Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
1999     if (overflow == CHECK_SIGNED)
2000       value = static_cast<SignedAddress>(value) >> right_shift;
2001     else
2002       value = value >> right_shift;
2003     Valtype reloc = value;
2004     val &= ~dst_mask;
2005     reloc &= dst_mask;
2006     elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2007     return overflowed<valsize>(value, overflow);
2008   }
2009 
2010   // Do a simple RELA relocation, unaligned.
2011   template<int fieldsize, int valsize>
2012   static inline Status
2013   rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2014   {
2015     elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2016     return overflowed<valsize>(value, overflow);
2017   }
2018 
2019   template<int fieldsize, int valsize>
2020   static inline Status
2021   rela_ua(unsigned char* view,
2022 	  unsigned int right_shift,
2023 	  typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2024 	  Address value,
2025 	  Overflow_check overflow)
2026   {
2027     typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2028       Valtype;
2029     Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2030     if (overflow == CHECK_SIGNED)
2031       value = static_cast<SignedAddress>(value) >> right_shift;
2032     else
2033       value = value >> right_shift;
2034     Valtype reloc = value;
2035     val &= ~dst_mask;
2036     reloc &= dst_mask;
2037     elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2038     return overflowed<valsize>(value, overflow);
2039   }
2040 
2041 public:
2042   // R_PPC64_ADDR64: (Symbol + Addend)
2043   static inline void
2044   addr64(unsigned char* view, Address value)
2045   { This::template rela<64,64>(view, value, CHECK_NONE); }
2046 
2047   // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2048   static inline void
2049   addr64_u(unsigned char* view, Address value)
2050   { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2051 
2052   // R_POWERPC_ADDR32: (Symbol + Addend)
2053   static inline Status
2054   addr32(unsigned char* view, Address value, Overflow_check overflow)
2055   { return This::template rela<32,32>(view, value, overflow); }
2056 
2057   // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2058   static inline Status
2059   addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2060   { return This::template rela_ua<32,32>(view, value, overflow); }
2061 
2062   // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2063   static inline Status
2064   addr24(unsigned char* view, Address value, Overflow_check overflow)
2065   {
2066     Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2067 					     value, overflow);
2068     if (overflow != CHECK_NONE && (value & 3) != 0)
2069       stat = STATUS_OVERFLOW;
2070     return stat;
2071   }
2072 
2073   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2074   static inline Status
2075   addr16(unsigned char* view, Address value, Overflow_check overflow)
2076   { return This::template rela<16,16>(view, value, overflow); }
2077 
2078   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2079   static inline Status
2080   addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2081   { return This::template rela_ua<16,16>(view, value, overflow); }
2082 
2083   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2084   static inline Status
2085   addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2086   {
2087     Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2088     if ((value & 3) != 0)
2089       stat = STATUS_OVERFLOW;
2090     return stat;
2091   }
2092 
2093   // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2094   static inline Status
2095   addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2096   {
2097     Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2098     if ((value & 15) != 0)
2099       stat = STATUS_OVERFLOW;
2100     return stat;
2101   }
2102 
2103   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2104   static inline void
2105   addr16_hi(unsigned char* view, Address value)
2106   { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2107 
2108   // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2109   static inline void
2110   addr16_ha(unsigned char* view, Address value)
2111   { This::addr16_hi(view, value + 0x8000); }
2112 
2113   // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2114   static inline void
2115   addr16_hi2(unsigned char* view, Address value)
2116   { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2117 
2118   // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2119   static inline void
2120   addr16_ha2(unsigned char* view, Address value)
2121   { This::addr16_hi2(view, value + 0x8000); }
2122 
2123   // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2124   static inline void
2125   addr16_hi3(unsigned char* view, Address value)
2126   { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2127 
2128   // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2129   static inline void
2130   addr16_ha3(unsigned char* view, Address value)
2131   { This::addr16_hi3(view, value + 0x8000); }
2132 
2133   // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2134   static inline Status
2135   addr14(unsigned char* view, Address value, Overflow_check overflow)
2136   {
2137     Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2138     if (overflow != CHECK_NONE && (value & 3) != 0)
2139       stat = STATUS_OVERFLOW;
2140     return stat;
2141   }
2142 
2143   // R_POWERPC_REL16DX_HA
2144   static inline Status
2145   addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2146   {
2147     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2148     Valtype* wv = reinterpret_cast<Valtype*>(view);
2149     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2150     value += 0x8000;
2151     value = static_cast<SignedAddress>(value) >> 16;
2152     val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2153     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2154     return overflowed<16>(value, overflow);
2155   }
2156 
2157   // R_PPC64_D34
2158   static inline Status
2159   addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2160   {
2161     Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2162 					     value, overflow);
2163     This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2164     return stat;
2165   }
2166 
2167   // R_PPC64_D34_HI30
2168   static inline void
2169   addr34_hi(unsigned char *view, uint64_t value)
2170   { This::addr34(view, value >> 34, CHECK_NONE);}
2171 
2172   // R_PPC64_D34_HA30
2173   static inline void
2174   addr34_ha(unsigned char *view, uint64_t value)
2175   { This::addr34_hi(view, value + (1ULL << 33));}
2176 
2177   // R_PPC64_D28
2178   static inline Status
2179   addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2180   {
2181     Status stat = This::template rela<32,12>(view, 16, 0xfff,
2182 					     value, overflow);
2183     This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2184     return stat;
2185   }
2186 
2187   // R_PPC64_ADDR16_HIGHER34
2188   static inline void
2189   addr16_higher34(unsigned char* view, uint64_t value)
2190   { This::addr16(view, value >> 34, CHECK_NONE); }
2191 
2192   // R_PPC64_ADDR16_HIGHERA34
2193   static inline void
2194   addr16_highera34(unsigned char* view, uint64_t value)
2195   { This::addr16_higher34(view, value + (1ULL << 33)); }
2196 
2197   // R_PPC64_ADDR16_HIGHEST34
2198   static inline void
2199   addr16_highest34(unsigned char* view, uint64_t value)
2200   { This::addr16(view, value >> 50, CHECK_NONE); }
2201 
2202   // R_PPC64_ADDR16_HIGHESTA34
2203   static inline void
2204   addr16_highesta34(unsigned char* view, uint64_t value)
2205   { This::addr16_highest34(view, value + (1ULL << 33)); }
2206 };
2207 
2208 // Set ABI version for input and output.
2209 
2210 template<int size, bool big_endian>
2211 void
2212 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2213 {
2214   this->e_flags_ |= ver;
2215   if (this->abiversion() != 0)
2216     {
2217       Target_powerpc<size, big_endian>* target =
2218 	static_cast<Target_powerpc<size, big_endian>*>(
2219 	   parameters->sized_target<size, big_endian>());
2220       if (target->abiversion() == 0)
2221 	target->set_abiversion(this->abiversion());
2222       else if (target->abiversion() != this->abiversion())
2223 	gold_error(_("%s: ABI version %d is not compatible "
2224 		     "with ABI version %d output"),
2225 		   this->name().c_str(),
2226 		   this->abiversion(), target->abiversion());
2227 
2228     }
2229 }
2230 
2231 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2232 // relocatable object, if such sections exists.
2233 
2234 template<int size, bool big_endian>
2235 bool
2236 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2237     Read_symbols_data* sd)
2238 {
2239   const unsigned char* const pshdrs = sd->section_headers->data();
2240   const unsigned char* namesu = sd->section_names->data();
2241   const char* names = reinterpret_cast<const char*>(namesu);
2242   section_size_type names_size = sd->section_names_size;
2243   const unsigned char* s;
2244 
2245   s = this->template find_shdr<size, big_endian>(pshdrs,
2246 						 size == 32 ? ".got2" : ".opd",
2247 						 names, names_size, NULL);
2248   if (s != NULL)
2249     {
2250       unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2251       this->special_ = ndx;
2252       if (size == 64)
2253 	{
2254 	  if (this->abiversion() == 0)
2255 	    this->set_abiversion(1);
2256 	  else if (this->abiversion() > 1)
2257 	    gold_error(_("%s: .opd invalid in abiv%d"),
2258 		       this->name().c_str(), this->abiversion());
2259 	}
2260     }
2261   if (size == 64)
2262     {
2263       s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2264 						     names, names_size, NULL);
2265       if (s != NULL)
2266 	{
2267 	  unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2268 	  this->relatoc_ = ndx;
2269 	  typename elfcpp::Shdr<size, big_endian> shdr(s);
2270 	  this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2271 	}
2272     }
2273   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2274 }
2275 
2276 // Examine .rela.opd to build info about function entry points.
2277 
2278 template<int size, bool big_endian>
2279 void
2280 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2281     size_t reloc_count,
2282     const unsigned char* prelocs,
2283     const unsigned char* plocal_syms)
2284 {
2285   if (size == 64)
2286     {
2287       typedef typename elfcpp::Rela<size, big_endian> Reltype;
2288       const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2289       const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2290       Address expected_off = 0;
2291       bool regular = true;
2292       unsigned int opd_ent_size = 0;
2293 
2294       for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2295 	{
2296 	  Reltype reloc(prelocs);
2297 	  typename elfcpp::Elf_types<size>::Elf_WXword r_info
2298 	    = reloc.get_r_info();
2299 	  unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2300 	  if (r_type == elfcpp::R_PPC64_ADDR64)
2301 	    {
2302 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2303 	      typename elfcpp::Elf_types<size>::Elf_Addr value;
2304 	      bool is_ordinary;
2305 	      unsigned int shndx;
2306 	      if (r_sym < this->local_symbol_count())
2307 		{
2308 		  typename elfcpp::Sym<size, big_endian>
2309 		    lsym(plocal_syms + r_sym * sym_size);
2310 		  shndx = lsym.get_st_shndx();
2311 		  shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2312 		  value = lsym.get_st_value();
2313 		}
2314 	      else
2315 		shndx = this->symbol_section_and_value(r_sym, &value,
2316 						       &is_ordinary);
2317 	      this->set_opd_ent(reloc.get_r_offset(), shndx,
2318 				value + reloc.get_r_addend());
2319 	      if (i == 2)
2320 		{
2321 		  expected_off = reloc.get_r_offset();
2322 		  opd_ent_size = expected_off;
2323 		}
2324 	      else if (expected_off != reloc.get_r_offset())
2325 		regular = false;
2326 	      expected_off += opd_ent_size;
2327 	    }
2328 	  else if (r_type == elfcpp::R_PPC64_TOC)
2329 	    {
2330 	      if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2331 		regular = false;
2332 	    }
2333 	  else
2334 	    {
2335 	      gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2336 			   this->name().c_str(), r_type);
2337 	      regular = false;
2338 	    }
2339 	}
2340       if (reloc_count <= 2)
2341 	opd_ent_size = this->section_size(this->opd_shndx());
2342       if (opd_ent_size != 24 && opd_ent_size != 16)
2343 	regular = false;
2344       if (!regular)
2345 	{
2346 	  gold_warning(_("%s: .opd is not a regular array of opd entries"),
2347 		       this->name().c_str());
2348 	  opd_ent_size = 0;
2349 	}
2350     }
2351 }
2352 
2353 // Returns true if a code sequence loading the TOC entry at VALUE
2354 // relative to the TOC pointer can be converted into code calculating
2355 // a TOC pointer relative offset.
2356 // If so, the TOC pointer relative offset is stored to VALUE.
2357 
2358 template<int size, bool big_endian>
2359 bool
2360 Powerpc_relobj<size, big_endian>::make_toc_relative(
2361     Target_powerpc<size, big_endian>* target,
2362     Address* value)
2363 {
2364   if (size != 64)
2365     return false;
2366 
2367   // With -mcmodel=medium code it is quite possible to have
2368   // toc-relative relocs referring to objects outside the TOC.
2369   // Don't try to look at a non-existent TOC.
2370   if (this->toc_shndx() == 0)
2371     return false;
2372 
2373   // Convert VALUE back to an address by adding got_base (see below),
2374   // then to an offset in the TOC by subtracting the TOC output
2375   // section address and the TOC output offset.  Since this TOC output
2376   // section and the got output section are one and the same, we can
2377   // omit adding and subtracting the output section address.
2378   Address off = (*value + this->toc_base_offset()
2379 		 - this->output_section_offset(this->toc_shndx()));
2380   // Is this offset in the TOC?  -mcmodel=medium code may be using
2381   // TOC relative access to variables outside the TOC.  Those of
2382   // course can't be optimized.  We also don't try to optimize code
2383   // that is using a different object's TOC.
2384   if (off >= this->section_size(this->toc_shndx()))
2385     return false;
2386 
2387   if (this->no_toc_opt(off))
2388     return false;
2389 
2390   section_size_type vlen;
2391   unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2392   Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2393   // The TOC pointer
2394   Address got_base = (target->got_section()->output_section()->address()
2395 		      + this->toc_base_offset());
2396   addr -= got_base;
2397   if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2398     return false;
2399 
2400   *value = addr;
2401   return true;
2402 }
2403 
2404 template<int size, bool big_endian>
2405 bool
2406 Powerpc_relobj<size, big_endian>::make_got_relative(
2407     Target_powerpc<size, big_endian>* target,
2408     const Symbol_value<size>* psymval,
2409     Address addend,
2410     Address* value)
2411 {
2412   Address addr = psymval->value(this, addend);
2413   Address got_base = (target->got_section()->output_section()->address()
2414 		      + this->toc_base_offset());
2415   addr -= got_base;
2416   if (addr + 0x80008000 > 0xffffffff)
2417     return false;
2418 
2419   *value = addr;
2420   return true;
2421 }
2422 
2423 // Perform the Sized_relobj_file method, then set up opd info from
2424 // .opd relocs.
2425 
2426 template<int size, bool big_endian>
2427 void
2428 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2429 {
2430   Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2431   if (size == 64)
2432     {
2433       for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2434 	   p != rd->relocs.end();
2435 	   ++p)
2436 	{
2437 	  if (p->data_shndx == this->opd_shndx())
2438 	    {
2439 	      uint64_t opd_size = this->section_size(this->opd_shndx());
2440 	      gold_assert(opd_size == static_cast<size_t>(opd_size));
2441 	      if (opd_size != 0)
2442 		{
2443 		  this->init_opd(opd_size);
2444 		  this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2445 					rd->local_symbols->data());
2446 		}
2447 	      break;
2448 	    }
2449 	}
2450     }
2451 }
2452 
2453 // Read the symbols then set up st_other vector.
2454 
2455 template<int size, bool big_endian>
2456 void
2457 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2458 {
2459   this->base_read_symbols(sd);
2460   if (this->input_file()->format() != Input_file::FORMAT_ELF)
2461     return;
2462   if (size == 64)
2463     {
2464       const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2465       const unsigned char* const pshdrs = sd->section_headers->data();
2466       const unsigned int loccount = this->do_local_symbol_count();
2467       if (loccount != 0)
2468 	{
2469 	  this->st_other_.resize(loccount);
2470 	  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2471 	  off_t locsize = loccount * sym_size;
2472 	  const unsigned int symtab_shndx = this->symtab_shndx();
2473 	  const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2474 	  typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2475 	  const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2476 						      locsize, true, false);
2477 	  psyms += sym_size;
2478 	  for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2479 	    {
2480 	      elfcpp::Sym<size, big_endian> sym(psyms);
2481 	      unsigned char st_other = sym.get_st_other();
2482 	      this->st_other_[i] = st_other;
2483 	      if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2484 		{
2485 		  if (this->abiversion() == 0)
2486 		    this->set_abiversion(2);
2487 		  else if (this->abiversion() < 2)
2488 		    gold_error(_("%s: local symbol %d has invalid st_other"
2489 				 " for ABI version 1"),
2490 			       this->name().c_str(), i);
2491 		}
2492 	    }
2493 	}
2494     }
2495 
2496   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2497   const unsigned char* ps = sd->section_headers->data() + shdr_size;
2498   bool merge_attributes = false;
2499   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2500     {
2501       elfcpp::Shdr<size, big_endian> shdr(ps);
2502       switch (shdr.get_sh_type())
2503 	{
2504 	case elfcpp::SHT_GNU_ATTRIBUTES:
2505 	  {
2506 	    gold_assert(this->attributes_section_data_ == NULL);
2507 	    section_offset_type section_offset = shdr.get_sh_offset();
2508 	    section_size_type section_size =
2509 	      convert_to_section_size_type(shdr.get_sh_size());
2510 	    const unsigned char* view =
2511 	      this->get_view(section_offset, section_size, true, false);
2512 	    this->attributes_section_data_ =
2513 	      new Attributes_section_data(view, section_size);
2514 	  }
2515 	  break;
2516 
2517 	case elfcpp::SHT_SYMTAB:
2518 	  {
2519 	    // Sometimes an object has no contents except the section
2520 	    // name string table and an empty symbol table with the
2521 	    // undefined symbol.  We don't want to merge
2522 	    // processor-specific flags from such an object.
2523 	    const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2524 	      elfcpp::Elf_sizes<size>::sym_size;
2525 	    if (shdr.get_sh_size() > sym_size)
2526 	      merge_attributes = true;
2527 	  }
2528 	  break;
2529 
2530 	case elfcpp::SHT_STRTAB:
2531 	  break;
2532 
2533 	default:
2534 	  merge_attributes = true;
2535 	  break;
2536 	}
2537     }
2538 
2539   if (!merge_attributes)
2540     {
2541       // Should rarely happen.
2542       delete this->attributes_section_data_;
2543       this->attributes_section_data_ = NULL;
2544     }
2545 }
2546 
2547 template<int size, bool big_endian>
2548 void
2549 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2550 {
2551   this->e_flags_ |= ver;
2552   if (this->abiversion() != 0)
2553     {
2554       Target_powerpc<size, big_endian>* target =
2555 	static_cast<Target_powerpc<size, big_endian>*>(
2556 	  parameters->sized_target<size, big_endian>());
2557       if (target->abiversion() == 0)
2558 	target->set_abiversion(this->abiversion());
2559       else if (target->abiversion() != this->abiversion())
2560 	gold_error(_("%s: ABI version %d is not compatible "
2561 		     "with ABI version %d output"),
2562 		   this->name().c_str(),
2563 		   this->abiversion(), target->abiversion());
2564 
2565     }
2566 }
2567 
2568 // Call Sized_dynobj::base_read_symbols to read the symbols then
2569 // read .opd from a dynamic object, filling in opd_ent_ vector,
2570 
2571 template<int size, bool big_endian>
2572 void
2573 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2574 {
2575   this->base_read_symbols(sd);
2576   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2577   const unsigned char* ps =
2578     sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2579   for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2580     {
2581       elfcpp::Shdr<size, big_endian> shdr(ps);
2582       if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2583 	{
2584 	  section_offset_type section_offset = shdr.get_sh_offset();
2585 	  section_size_type section_size =
2586 	    convert_to_section_size_type(shdr.get_sh_size());
2587 	  const unsigned char* view =
2588 	    this->get_view(section_offset, section_size, true, false);
2589 	  this->attributes_section_data_ =
2590 	    new Attributes_section_data(view, section_size);
2591 	  break;
2592 	}
2593     }
2594   if (size == 64)
2595     {
2596       const unsigned char* const pshdrs = sd->section_headers->data();
2597       const unsigned char* namesu = sd->section_names->data();
2598       const char* names = reinterpret_cast<const char*>(namesu);
2599       const unsigned char* s = NULL;
2600       const unsigned char* opd;
2601       section_size_type opd_size;
2602 
2603       // Find and read .opd section.
2604       while (1)
2605 	{
2606 	  s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2607 							 sd->section_names_size,
2608 							 s);
2609 	  if (s == NULL)
2610 	    return;
2611 
2612 	  typename elfcpp::Shdr<size, big_endian> shdr(s);
2613 	  if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2614 	      && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2615 	    {
2616 	      if (this->abiversion() == 0)
2617 		this->set_abiversion(1);
2618 	      else if (this->abiversion() > 1)
2619 		gold_error(_("%s: .opd invalid in abiv%d"),
2620 			   this->name().c_str(), this->abiversion());
2621 
2622 	      this->opd_shndx_ = (s - pshdrs) / shdr_size;
2623 	      this->opd_address_ = shdr.get_sh_addr();
2624 	      opd_size = convert_to_section_size_type(shdr.get_sh_size());
2625 	      opd = this->get_view(shdr.get_sh_offset(), opd_size,
2626 				   true, false);
2627 	      break;
2628 	    }
2629 	}
2630 
2631       // Build set of executable sections.
2632       // Using a set is probably overkill.  There is likely to be only
2633       // a few executable sections, typically .init, .text and .fini,
2634       // and they are generally grouped together.
2635       typedef std::set<Sec_info> Exec_sections;
2636       Exec_sections exec_sections;
2637       s = pshdrs;
2638       for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2639 	{
2640 	  typename elfcpp::Shdr<size, big_endian> shdr(s);
2641 	  if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2642 	      && ((shdr.get_sh_flags()
2643 		   & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2644 		  == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2645 	      && shdr.get_sh_size() != 0)
2646 	    {
2647 	      exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2648 					    shdr.get_sh_size(), i));
2649 	    }
2650 	}
2651       if (exec_sections.empty())
2652 	return;
2653 
2654       // Look over the OPD entries.  This is complicated by the fact
2655       // that some binaries will use two-word entries while others
2656       // will use the standard three-word entries.  In most cases
2657       // the third word (the environment pointer for languages like
2658       // Pascal) is unused and will be zero.  If the third word is
2659       // used it should not be pointing into executable sections,
2660       // I think.
2661       this->init_opd(opd_size);
2662       for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2663 	{
2664 	  typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2665 	  const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2666 	  Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2667 	  if (val == 0)
2668 	    // Chances are that this is the third word of an OPD entry.
2669 	    continue;
2670 	  typename Exec_sections::const_iterator e
2671 	    = exec_sections.upper_bound(Sec_info(val, 0, 0));
2672 	  if (e != exec_sections.begin())
2673 	    {
2674 	      --e;
2675 	      if (e->start <= val && val < e->start + e->len)
2676 		{
2677 		  // We have an address in an executable section.
2678 		  // VAL ought to be the function entry, set it up.
2679 		  this->set_opd_ent(p - opd, e->shndx, val);
2680 		  // Skip second word of OPD entry, the TOC pointer.
2681 		  p += 8;
2682 		}
2683 	    }
2684 	  // If we didn't match any executable sections, we likely
2685 	  // have a non-zero third word in the OPD entry.
2686 	}
2687     }
2688 }
2689 
2690 // Relocate sections.
2691 
2692 template<int size, bool big_endian>
2693 void
2694 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2695     const Symbol_table* symtab, const Layout* layout,
2696     const unsigned char* pshdrs, Output_file* of,
2697     typename Sized_relobj_file<size, big_endian>::Views* pviews)
2698 {
2699   unsigned int start = 1;
2700   if (size == 64
2701       && this->relatoc_ != 0
2702       && !parameters->options().relocatable())
2703     {
2704       // Relocate .toc first.
2705       this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2706 				   this->relatoc_, this->relatoc_);
2707       this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2708 				   1, this->relatoc_ - 1);
2709       start = this->relatoc_ + 1;
2710     }
2711   this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2712 			       start, this->shnum() - 1);
2713 
2714   if (!parameters->options().output_is_position_independent())
2715     {
2716       Target_powerpc<size, big_endian>* target
2717 	= static_cast<Target_powerpc<size, big_endian>*>(
2718 	    parameters->sized_target<size, big_endian>());
2719       if (target->lplt_section() && target->lplt_section()->data_size() != 0)
2720 	{
2721 	  const section_size_type offset = target->lplt_section()->offset();
2722 	  const section_size_type oview_size
2723 	    = convert_to_section_size_type(target->lplt_section()->data_size());
2724 	  unsigned char* const oview = of->get_output_view(offset, oview_size);
2725 
2726 	  bool modified = false;
2727 	  unsigned int nsyms = this->local_symbol_count();
2728 	  for (unsigned int i = 0; i < nsyms; i++)
2729 	    if (this->local_has_plt_offset(i))
2730 	      {
2731 		Address value = this->local_symbol_value(i, 0);
2732 		if (size == 64)
2733 		  value += ppc64_local_entry_offset(i);
2734 		size_t off = this->local_plt_offset(i);
2735 		elfcpp::Swap<size, big_endian>::writeval(oview + off, value);
2736 		modified = true;
2737 	      }
2738 	  if (modified)
2739 	    of->write_output_view(offset, oview_size, oview);
2740 	}
2741     }
2742 }
2743 
2744 // Set up some symbols.
2745 
2746 template<int size, bool big_endian>
2747 void
2748 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2749     Symbol_table* symtab,
2750     Layout* layout)
2751 {
2752   if (size == 32)
2753     {
2754       // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2755       // undefined when scanning relocs (and thus requires
2756       // non-relative dynamic relocs).  The proper value will be
2757       // updated later.
2758       Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2759       if (gotsym != NULL && gotsym->is_undefined())
2760 	{
2761 	  Target_powerpc<size, big_endian>* target =
2762 	    static_cast<Target_powerpc<size, big_endian>*>(
2763 		parameters->sized_target<size, big_endian>());
2764 	  Output_data_got_powerpc<size, big_endian>* got
2765 	    = target->got_section(symtab, layout);
2766 	  symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2767 					Symbol_table::PREDEFINED,
2768 					got, 0, 0,
2769 					elfcpp::STT_OBJECT,
2770 					elfcpp::STB_LOCAL,
2771 					elfcpp::STV_HIDDEN, 0,
2772 					false, false);
2773 	}
2774 
2775       // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2776       Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2777       if (sdasym != NULL && sdasym->is_undefined())
2778 	{
2779 	  Output_data_space* sdata = new Output_data_space(4, "** sdata");
2780 	  Output_section* os
2781 	    = layout->add_output_section_data(".sdata", 0,
2782 					      elfcpp::SHF_ALLOC
2783 					      | elfcpp::SHF_WRITE,
2784 					      sdata, ORDER_SMALL_DATA, false);
2785 	  symtab->define_in_output_data("_SDA_BASE_", NULL,
2786 					Symbol_table::PREDEFINED,
2787 					os, 32768, 0, elfcpp::STT_OBJECT,
2788 					elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2789 					0, false, false);
2790 	}
2791     }
2792   else
2793     {
2794       // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2795       Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2796       if (gotsym != NULL && gotsym->is_undefined())
2797 	{
2798 	  Target_powerpc<size, big_endian>* target =
2799 	    static_cast<Target_powerpc<size, big_endian>*>(
2800 		parameters->sized_target<size, big_endian>());
2801 	  Output_data_got_powerpc<size, big_endian>* got
2802 	    = target->got_section(symtab, layout);
2803 	  symtab->define_in_output_data(".TOC.", NULL,
2804 					Symbol_table::PREDEFINED,
2805 					got, 0x8000, 0,
2806 					elfcpp::STT_OBJECT,
2807 					elfcpp::STB_LOCAL,
2808 					elfcpp::STV_HIDDEN, 0,
2809 					false, false);
2810 	}
2811     }
2812 
2813   this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2814   if (parameters->options().tls_get_addr_optimize()
2815       && this->tls_get_addr_ != NULL
2816       && this->tls_get_addr_->in_reg())
2817     this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2818   if (this->tls_get_addr_opt_ != NULL)
2819     {
2820       if (this->tls_get_addr_->is_undefined()
2821 	  || this->tls_get_addr_->is_from_dynobj())
2822 	{
2823 	  // Make it seem as if references to __tls_get_addr are
2824 	  // really to __tls_get_addr_opt, so the latter symbol is
2825 	  // made dynamic, not the former.
2826 	  this->tls_get_addr_->clear_in_reg();
2827 	  this->tls_get_addr_opt_->set_in_reg();
2828 	}
2829       // We have a non-dynamic definition for __tls_get_addr.
2830       // Make __tls_get_addr_opt the same, if it does not already have
2831       // a non-dynamic definition.
2832       else if (this->tls_get_addr_opt_->is_undefined()
2833 	       || this->tls_get_addr_opt_->is_from_dynobj())
2834 	{
2835 	  Sized_symbol<size>* from
2836 	    = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2837 	  Sized_symbol<size>* to
2838 	    = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2839 	  symtab->clone<size>(to, from);
2840 	}
2841     }
2842 }
2843 
2844 // Set up PowerPC target specific relobj.
2845 
2846 template<int size, bool big_endian>
2847 Object*
2848 Target_powerpc<size, big_endian>::do_make_elf_object(
2849     const std::string& name,
2850     Input_file* input_file,
2851     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2852 {
2853   int et = ehdr.get_e_type();
2854   // ET_EXEC files are valid input for --just-symbols/-R,
2855   // and we treat them as relocatable objects.
2856   if (et == elfcpp::ET_REL
2857       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2858     {
2859       Powerpc_relobj<size, big_endian>* obj =
2860 	new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2861       obj->setup();
2862       return obj;
2863     }
2864   else if (et == elfcpp::ET_DYN)
2865     {
2866       Powerpc_dynobj<size, big_endian>* obj =
2867 	new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2868       obj->setup();
2869       return obj;
2870     }
2871   else
2872     {
2873       gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2874       return NULL;
2875     }
2876 }
2877 
2878 template<int size, bool big_endian>
2879 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2880 {
2881 public:
2882   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2883   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2884 
2885   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
2886     : Output_data_got<size, big_endian>(),
2887       symtab_(symtab), layout_(layout),
2888       header_ent_cnt_(size == 32 ? 3 : 1),
2889       header_index_(size == 32 ? 0x2000 : 0)
2890   {
2891     if (size == 64)
2892       this->set_addralign(256);
2893   }
2894 
2895   // Override all the Output_data_got methods we use so as to first call
2896   // reserve_ent().
2897   bool
2898   add_global(Symbol* gsym, unsigned int got_type)
2899   {
2900     this->reserve_ent();
2901     return Output_data_got<size, big_endian>::add_global(gsym, got_type);
2902   }
2903 
2904   bool
2905   add_global_plt(Symbol* gsym, unsigned int got_type)
2906   {
2907     this->reserve_ent();
2908     return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type);
2909   }
2910 
2911   bool
2912   add_global_tls(Symbol* gsym, unsigned int got_type)
2913   { return this->add_global_plt(gsym, got_type); }
2914 
2915   void
2916   add_global_with_rel(Symbol* gsym, unsigned int got_type,
2917 		      Output_data_reloc_generic* rel_dyn, unsigned int r_type)
2918   {
2919     this->reserve_ent();
2920     Output_data_got<size, big_endian>::
2921       add_global_with_rel(gsym, got_type, rel_dyn, r_type);
2922   }
2923 
2924   void
2925   add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2926 			   Output_data_reloc_generic* rel_dyn,
2927 			   unsigned int r_type_1, unsigned int r_type_2)
2928   {
2929     if (gsym->has_got_offset(got_type))
2930       return;
2931 
2932     this->reserve_ent(2);
2933     Output_data_got<size, big_endian>::
2934       add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2);
2935   }
2936 
2937   bool
2938   add_local(Relobj* object, unsigned int sym_index, unsigned int got_type)
2939   {
2940     this->reserve_ent();
2941     return Output_data_got<size, big_endian>::add_local(object, sym_index,
2942 							got_type);
2943   }
2944 
2945   bool
2946   add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type)
2947   {
2948     this->reserve_ent();
2949     return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2950 							    got_type);
2951   }
2952 
2953   bool
2954   add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type)
2955   { return this->add_local_plt(object, sym_index, got_type); }
2956 
2957   void
2958   add_local_tls_pair(Relobj* object, unsigned int sym_index,
2959 		     unsigned int got_type,
2960 		     Output_data_reloc_generic* rel_dyn,
2961 		     unsigned int r_type)
2962   {
2963     if (object->local_has_got_offset(sym_index, got_type))
2964       return;
2965 
2966     this->reserve_ent(2);
2967     Output_data_got<size, big_endian>::
2968       add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type);
2969   }
2970 
2971   unsigned int
2972   add_constant(Valtype constant)
2973   {
2974     this->reserve_ent();
2975     return Output_data_got<size, big_endian>::add_constant(constant);
2976   }
2977 
2978   unsigned int
2979   add_constant_pair(Valtype c1, Valtype c2)
2980   {
2981     this->reserve_ent(2);
2982     return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
2983   }
2984 
2985   // Offset of _GLOBAL_OFFSET_TABLE_.
2986   unsigned int
2987   g_o_t() const
2988   {
2989     return this->got_offset(this->header_index_);
2990   }
2991 
2992   // Offset of base used to access the GOT/TOC.
2993   // The got/toc pointer reg will be set to this value.
2994   Valtype
2995   got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2996   {
2997     if (size == 32)
2998       return this->g_o_t();
2999     else
3000       return (this->output_section()->address()
3001 	      + object->toc_base_offset()
3002 	      - this->address());
3003   }
3004 
3005   // Ensure our GOT has a header.
3006   void
3007   set_final_data_size()
3008   {
3009     if (this->header_ent_cnt_ != 0)
3010       this->make_header();
3011     Output_data_got<size, big_endian>::set_final_data_size();
3012   }
3013 
3014   // First word of GOT header needs some values that are not
3015   // handled by Output_data_got so poke them in here.
3016   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3017   void
3018   do_write(Output_file* of)
3019   {
3020     Valtype val = 0;
3021     if (size == 32 && this->layout_->dynamic_data() != NULL)
3022       val = this->layout_->dynamic_section()->address();
3023     if (size == 64)
3024       val = this->output_section()->address() + 0x8000;
3025     this->replace_constant(this->header_index_, val);
3026     Output_data_got<size, big_endian>::do_write(of);
3027   }
3028 
3029 private:
3030   void
3031   reserve_ent(unsigned int cnt = 1)
3032   {
3033     if (this->header_ent_cnt_ == 0)
3034       return;
3035     if (this->num_entries() + cnt > this->header_index_)
3036       this->make_header();
3037   }
3038 
3039   void
3040   make_header()
3041   {
3042     this->header_ent_cnt_ = 0;
3043     this->header_index_ = this->num_entries();
3044     if (size == 32)
3045       {
3046 	Output_data_got<size, big_endian>::add_constant(0);
3047 	Output_data_got<size, big_endian>::add_constant(0);
3048 	Output_data_got<size, big_endian>::add_constant(0);
3049 
3050 	// Define _GLOBAL_OFFSET_TABLE_ at the header
3051 	Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3052 	if (gotsym != NULL)
3053 	  {
3054 	    Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3055 	    sym->set_value(this->g_o_t());
3056 	  }
3057 	else
3058 	  this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3059 					       Symbol_table::PREDEFINED,
3060 					       this, this->g_o_t(), 0,
3061 					       elfcpp::STT_OBJECT,
3062 					       elfcpp::STB_LOCAL,
3063 					       elfcpp::STV_HIDDEN, 0,
3064 					       false, false);
3065       }
3066     else
3067       Output_data_got<size, big_endian>::add_constant(0);
3068   }
3069 
3070   // Stashed pointers.
3071   Symbol_table* symtab_;
3072   Layout* layout_;
3073 
3074   // GOT header size.
3075   unsigned int header_ent_cnt_;
3076   // GOT header index.
3077   unsigned int header_index_;
3078 };
3079 
3080 // Get the GOT section, creating it if necessary.
3081 
3082 template<int size, bool big_endian>
3083 Output_data_got_powerpc<size, big_endian>*
3084 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3085 					      Layout* layout)
3086 {
3087   if (this->got_ == NULL)
3088     {
3089       gold_assert(symtab != NULL && layout != NULL);
3090 
3091       this->got_
3092 	= new Output_data_got_powerpc<size, big_endian>(symtab, layout);
3093 
3094       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3095 				      elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3096 				      this->got_, ORDER_DATA, false);
3097     }
3098 
3099   return this->got_;
3100 }
3101 
3102 // Get the dynamic reloc section, creating it if necessary.
3103 
3104 template<int size, bool big_endian>
3105 typename Target_powerpc<size, big_endian>::Reloc_section*
3106 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3107 {
3108   if (this->rela_dyn_ == NULL)
3109     {
3110       gold_assert(layout != NULL);
3111       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3112       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3113 				      elfcpp::SHF_ALLOC, this->rela_dyn_,
3114 				      ORDER_DYNAMIC_RELOCS, false);
3115     }
3116   return this->rela_dyn_;
3117 }
3118 
3119 // Similarly, but for ifunc symbols get the one for ifunc.
3120 
3121 template<int size, bool big_endian>
3122 typename Target_powerpc<size, big_endian>::Reloc_section*
3123 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3124 						   Layout* layout,
3125 						   bool for_ifunc)
3126 {
3127   if (!for_ifunc)
3128     return this->rela_dyn_section(layout);
3129 
3130   if (this->iplt_ == NULL)
3131     this->make_iplt_section(symtab, layout);
3132   return this->iplt_->rel_plt();
3133 }
3134 
3135 class Stub_control
3136 {
3137  public:
3138   // Determine the stub group size.  The group size is the absolute
3139   // value of the parameter --stub-group-size.  If --stub-group-size
3140   // is passed a negative value, we restrict stubs to be always after
3141   // the stubbed branches.
3142   Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3143     : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3144       suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3145       state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3146       owner_(NULL), output_section_(NULL)
3147   {
3148   }
3149 
3150   // Return true iff input section can be handled by current stub
3151   // group.
3152   bool
3153   can_add_to_stub_group(Output_section* o,
3154 			const Output_section::Input_section* i,
3155 			bool has14);
3156 
3157   const Output_section::Input_section*
3158   owner()
3159   { return owner_; }
3160 
3161   Output_section*
3162   output_section()
3163   { return output_section_; }
3164 
3165   void
3166   set_output_and_owner(Output_section* o,
3167 		       const Output_section::Input_section* i)
3168   {
3169     this->output_section_ = o;
3170     this->owner_ = i;
3171   }
3172 
3173  private:
3174   typedef enum
3175   {
3176     // Initial state.
3177     NO_GROUP,
3178     // Adding group sections before the stubs.
3179     FINDING_STUB_SECTION,
3180     // Adding group sections after the stubs.
3181     HAS_STUB_SECTION
3182   } State;
3183 
3184   uint32_t stub_group_size_;
3185   bool stubs_always_after_branch_;
3186   bool suppress_size_errors_;
3187   // True if a stub group can serve multiple output sections.
3188   bool multi_os_;
3189   State state_;
3190   // Current max size of group.  Starts at stub_group_size_ but is
3191   // reduced to stub_group_size_/1024 on seeing a section with
3192   // external conditional branches.
3193   uint32_t group_size_;
3194   uint64_t group_start_addr_;
3195   // owner_ and output_section_ specify the section to which stubs are
3196   // attached.  The stubs are placed at the end of this section.
3197   const Output_section::Input_section* owner_;
3198   Output_section* output_section_;
3199 };
3200 
3201 // Return true iff input section can be handled by current stub
3202 // group.  Sections are presented to this function in order,
3203 // so the first section is the head of the group.
3204 
3205 bool
3206 Stub_control::can_add_to_stub_group(Output_section* o,
3207 				    const Output_section::Input_section* i,
3208 				    bool has14)
3209 {
3210   bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3211   uint64_t this_size;
3212   uint64_t start_addr = o->address();
3213 
3214   if (whole_sec)
3215     // .init and .fini sections are pasted together to form a single
3216     // function.  We can't be adding stubs in the middle of the function.
3217     this_size = o->data_size();
3218   else
3219     {
3220       start_addr += i->relobj()->output_section_offset(i->shndx());
3221       this_size = i->data_size();
3222     }
3223 
3224   uint64_t end_addr = start_addr + this_size;
3225   uint32_t group_size = this->stub_group_size_;
3226   if (has14)
3227     this->group_size_ = group_size = group_size >> 10;
3228 
3229   if (this_size > group_size && !this->suppress_size_errors_)
3230     gold_warning(_("%s:%s exceeds group size"),
3231 		 i->relobj()->name().c_str(),
3232 		 i->relobj()->section_name(i->shndx()).c_str());
3233 
3234   gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3235 	     has14 ? " 14bit" : "",
3236 	     i->relobj()->name().c_str(),
3237 	     i->relobj()->section_name(i->shndx()).c_str(),
3238 	     (long long) this_size,
3239 	     (this->state_ == NO_GROUP
3240 	      ? this_size
3241 	      : (long long) end_addr - this->group_start_addr_));
3242 
3243   if (this->state_ == NO_GROUP)
3244     {
3245       // Only here on very first use of Stub_control
3246       this->owner_ = i;
3247       this->output_section_ = o;
3248       this->state_ = FINDING_STUB_SECTION;
3249       this->group_size_ = group_size;
3250       this->group_start_addr_ = start_addr;
3251       return true;
3252     }
3253   else if (!this->multi_os_ && this->output_section_ != o)
3254     ;
3255   else if (this->state_ == HAS_STUB_SECTION)
3256     {
3257       // Can we add this section, which is after the stubs, to the
3258       // group?
3259       if (end_addr - this->group_start_addr_ <= this->group_size_)
3260 	return true;
3261     }
3262   else if (this->state_ == FINDING_STUB_SECTION)
3263     {
3264       if ((whole_sec && this->output_section_ == o)
3265 	  || end_addr - this->group_start_addr_ <= this->group_size_)
3266 	{
3267 	  // Stubs are added at the end of "owner_".
3268 	  this->owner_ = i;
3269 	  this->output_section_ = o;
3270 	  return true;
3271 	}
3272       // The group before the stubs has reached maximum size.
3273       // Now see about adding sections after the stubs to the
3274       // group.  If the current section has a 14-bit branch and
3275       // the group before the stubs exceeds group_size_ (because
3276       // they didn't have 14-bit branches), don't add sections
3277       // after the stubs:  The size of stubs for such a large
3278       // group may exceed the reach of a 14-bit branch.
3279       if (!this->stubs_always_after_branch_
3280 	  && this_size <= this->group_size_
3281 	  && start_addr - this->group_start_addr_ <= this->group_size_)
3282 	{
3283 	  gold_debug(DEBUG_TARGET, "adding after stubs");
3284 	  this->state_ = HAS_STUB_SECTION;
3285 	  this->group_start_addr_ = start_addr;
3286 	  return true;
3287 	}
3288     }
3289   else
3290     gold_unreachable();
3291 
3292   gold_debug(DEBUG_TARGET,
3293 	     !this->multi_os_ && this->output_section_ != o
3294 	     ? "nope, new output section\n"
3295 	     : "nope, didn't fit\n");
3296 
3297   // The section fails to fit in the current group.  Set up a few
3298   // things for the next group.  owner_ and output_section_ will be
3299   // set later after we've retrieved those values for the current
3300   // group.
3301   this->state_ = FINDING_STUB_SECTION;
3302   this->group_size_ = group_size;
3303   this->group_start_addr_ = start_addr;
3304   return false;
3305 }
3306 
3307 // Look over all the input sections, deciding where to place stubs.
3308 
3309 template<int size, bool big_endian>
3310 void
3311 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3312 						 const Task*,
3313 						 bool no_size_errors)
3314 {
3315   Stub_control stub_control(this->stub_group_size_, no_size_errors,
3316 			    parameters->options().stub_group_multi());
3317 
3318   // Group input sections and insert stub table
3319   Stub_table_owner* table_owner = NULL;
3320   std::vector<Stub_table_owner*> tables;
3321   Layout::Section_list section_list;
3322   layout->get_executable_sections(&section_list);
3323   std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3324   for (Layout::Section_list::iterator o = section_list.begin();
3325        o != section_list.end();
3326        ++o)
3327     {
3328       typedef Output_section::Input_section_list Input_section_list;
3329       for (Input_section_list::const_iterator i
3330 	     = (*o)->input_sections().begin();
3331 	   i != (*o)->input_sections().end();
3332 	   ++i)
3333 	{
3334 	  if (i->is_input_section()
3335 	      || i->is_relaxed_input_section())
3336 	    {
3337 	      Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3338 		<Powerpc_relobj<size, big_endian>*>(i->relobj());
3339 	      bool has14 = ppcobj->has_14bit_branch(i->shndx());
3340 	      if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3341 		{
3342 		  table_owner->output_section = stub_control.output_section();
3343 		  table_owner->owner = stub_control.owner();
3344 		  stub_control.set_output_and_owner(*o, &*i);
3345 		  table_owner = NULL;
3346 		}
3347 	      if (table_owner == NULL)
3348 		{
3349 		  table_owner = new Stub_table_owner;
3350 		  tables.push_back(table_owner);
3351 		}
3352 	      ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3353 	    }
3354 	}
3355     }
3356   if (table_owner != NULL)
3357     {
3358       table_owner->output_section = stub_control.output_section();
3359       table_owner->owner = stub_control.owner();;
3360     }
3361   for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3362        t != tables.end();
3363        ++t)
3364     {
3365       Stub_table<size, big_endian>* stub_table;
3366 
3367       if ((*t)->owner->is_input_section())
3368 	stub_table = new Stub_table<size, big_endian>(this,
3369 						      (*t)->output_section,
3370 						      (*t)->owner,
3371 						      this->stub_tables_.size());
3372       else if ((*t)->owner->is_relaxed_input_section())
3373 	stub_table = static_cast<Stub_table<size, big_endian>*>(
3374 			(*t)->owner->relaxed_input_section());
3375       else
3376 	gold_unreachable();
3377       this->stub_tables_.push_back(stub_table);
3378       delete *t;
3379     }
3380 }
3381 
3382 template<int size>
3383 static unsigned long
3384 max_branch_delta (unsigned int r_type)
3385 {
3386   if (r_type == elfcpp::R_POWERPC_REL14
3387       || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3388       || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3389     return 1L << 15;
3390   if (r_type == elfcpp::R_POWERPC_REL24
3391       || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3392       || r_type == elfcpp::R_PPC_PLTREL24
3393       || r_type == elfcpp::R_PPC_LOCAL24PC)
3394     return 1L << 25;
3395   return 0;
3396 }
3397 
3398 // Return whether this branch is going via a plt call stub.
3399 
3400 template<int size, bool big_endian>
3401 bool
3402 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3403     Powerpc_relobj<size, big_endian>* ppc_object,
3404     unsigned int shndx,
3405     Address offset,
3406     Target_powerpc* target,
3407     Symbol_table* symtab)
3408 {
3409   if (this->object_ != ppc_object
3410       || this->shndx_ != shndx
3411       || this->offset_ != offset)
3412     return false;
3413 
3414   Symbol* sym = this->object_->global_symbol(this->r_sym_);
3415   if (sym != NULL && sym->is_forwarder())
3416     sym = symtab->resolve_forwards(sym);
3417   if (target->replace_tls_get_addr(sym))
3418     sym = target->tls_get_addr_opt();
3419   const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3420   if (gsym != NULL
3421       ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3422 	 && !target->is_elfv2_localentry0(gsym))
3423       : (this->object_->local_has_plt_offset(this->r_sym_)
3424 	 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3425     {
3426       this->tocsave_ = 1;
3427       return true;
3428     }
3429   return false;
3430 }
3431 
3432 // If this branch needs a plt call stub, or a long branch stub, make one.
3433 
3434 template<int size, bool big_endian>
3435 bool
3436 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3437     Stub_table<size, big_endian>* stub_table,
3438     Stub_table<size, big_endian>* ifunc_stub_table,
3439     Symbol_table* symtab) const
3440 {
3441   Symbol* sym = this->object_->global_symbol(this->r_sym_);
3442   Target_powerpc<size, big_endian>* target =
3443     static_cast<Target_powerpc<size, big_endian>*>(
3444       parameters->sized_target<size, big_endian>());
3445   if (sym != NULL && sym->is_forwarder())
3446     sym = symtab->resolve_forwards(sym);
3447   if (target->replace_tls_get_addr(sym))
3448     sym = target->tls_get_addr_opt();
3449   const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3450   bool ok = true;
3451 
3452   if (gsym != NULL
3453       ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3454       : this->object_->local_has_plt_offset(this->r_sym_))
3455     {
3456       if (size == 64
3457 	  && gsym != NULL
3458 	  && target->abiversion() >= 2
3459 	  && !parameters->options().output_is_position_independent()
3460 	  && !is_branch_reloc<size>(this->r_type_))
3461 	target->glink_section()->add_global_entry(gsym);
3462       else
3463 	{
3464 	  if (stub_table == NULL
3465 	      && !(size == 32
3466 		   && gsym != NULL
3467 		   && !parameters->options().output_is_position_independent()
3468 		   && !is_branch_reloc<size>(this->r_type_)))
3469 	    stub_table = this->object_->stub_table(this->shndx_);
3470 	  if (stub_table == NULL)
3471 	    {
3472 	      // This is a ref from a data section to an ifunc symbol,
3473 	      // or a non-branch reloc for which we always want to use
3474 	      // one set of stubs for resolving function addresses.
3475 	      stub_table = ifunc_stub_table;
3476 	    }
3477 	  gold_assert(stub_table != NULL);
3478 	  Address from = this->object_->get_output_section_offset(this->shndx_);
3479 	  if (from != invalid_address)
3480 	    from += (this->object_->output_section(this->shndx_)->address()
3481 		     + this->offset_);
3482 	  if (gsym != NULL)
3483 	    ok = stub_table->add_plt_call_entry(from,
3484 						this->object_, gsym,
3485 						this->r_type_, this->addend_,
3486 						this->tocsave_);
3487 	  else
3488 	    ok = stub_table->add_plt_call_entry(from,
3489 						this->object_, this->r_sym_,
3490 						this->r_type_, this->addend_,
3491 						this->tocsave_);
3492 	}
3493     }
3494   else
3495     {
3496       Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3497       if (max_branch_offset == 0)
3498 	return true;
3499       Address from = this->object_->get_output_section_offset(this->shndx_);
3500       gold_assert(from != invalid_address);
3501       from += (this->object_->output_section(this->shndx_)->address()
3502 	       + this->offset_);
3503       Address to;
3504       if (gsym != NULL)
3505 	{
3506 	  switch (gsym->source())
3507 	    {
3508 	    case Symbol::FROM_OBJECT:
3509 	      {
3510 		Object* symobj = gsym->object();
3511 		if (symobj->is_dynamic()
3512 		    || symobj->pluginobj() != NULL)
3513 		  return true;
3514 		bool is_ordinary;
3515 		unsigned int shndx = gsym->shndx(&is_ordinary);
3516 		if (shndx == elfcpp::SHN_UNDEF)
3517 		  return true;
3518 	      }
3519 	      break;
3520 
3521 	    case Symbol::IS_UNDEFINED:
3522 	      return true;
3523 
3524 	    default:
3525 	      break;
3526 	    }
3527 	  Symbol_table::Compute_final_value_status status;
3528 	  to = symtab->compute_final_value<size>(gsym, &status);
3529 	  if (status != Symbol_table::CFVS_OK)
3530 	    return true;
3531 	  if (size == 64)
3532 	    to += this->object_->ppc64_local_entry_offset(gsym);
3533 	}
3534       else
3535 	{
3536 	  const Symbol_value<size>* psymval
3537 	    = this->object_->local_symbol(this->r_sym_);
3538 	  Symbol_value<size> symval;
3539 	  if (psymval->is_section_symbol())
3540 	    symval.set_is_section_symbol();
3541 	  typedef Sized_relobj_file<size, big_endian> ObjType;
3542 	  typename ObjType::Compute_final_local_value_status status
3543 	    = this->object_->compute_final_local_value(this->r_sym_, psymval,
3544 						       &symval, symtab);
3545 	  if (status != ObjType::CFLV_OK
3546 	      || !symval.has_output_value())
3547 	    return true;
3548 	  to = symval.value(this->object_, 0);
3549 	  if (size == 64)
3550 	    to += this->object_->ppc64_local_entry_offset(this->r_sym_);
3551 	}
3552       if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3553 	to += this->addend_;
3554       if (stub_table == NULL)
3555 	stub_table = this->object_->stub_table(this->shndx_);
3556       if (size == 64 && target->abiversion() < 2)
3557 	{
3558 	  unsigned int dest_shndx;
3559 	  if (!target->symval_for_branch(symtab, gsym, this->object_,
3560 					 &to, &dest_shndx))
3561 	    return true;
3562 	}
3563       Address delta = to - from;
3564       if (delta + max_branch_offset >= 2 * max_branch_offset
3565 	  || (size == 64
3566 	      && this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3567 	      && (gsym != NULL
3568 		  ? this->object_->ppc64_needs_toc(gsym)
3569 		  : this->object_->ppc64_needs_toc(this->r_sym_))))
3570 	{
3571 	  if (stub_table == NULL)
3572 	    {
3573 	      gold_warning(_("%s:%s: branch in non-executable section,"
3574 			     " no long branch stub for you"),
3575 			   this->object_->name().c_str(),
3576 			   this->object_->section_name(this->shndx_).c_str());
3577 	      return true;
3578 	    }
3579 	  bool save_res = (size == 64
3580 			   && gsym != NULL
3581 			   && gsym->source() == Symbol::IN_OUTPUT_DATA
3582 			   && gsym->output_data() == target->savres_section());
3583 	  ok = stub_table->add_long_branch_entry(this->object_,
3584 						 this->r_type_,
3585 						 from, to, save_res);
3586 	}
3587     }
3588   if (!ok)
3589     gold_debug(DEBUG_TARGET,
3590 	       "branch at %s:%s+%#lx\n"
3591 	       "can't reach stub attached to %s:%s",
3592 	       this->object_->name().c_str(),
3593 	       this->object_->section_name(this->shndx_).c_str(),
3594 	       (unsigned long) this->offset_,
3595 	       stub_table->relobj()->name().c_str(),
3596 	       stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3597 
3598   return ok;
3599 }
3600 
3601 // Relaxation hook.  This is where we do stub generation.
3602 
3603 template<int size, bool big_endian>
3604 bool
3605 Target_powerpc<size, big_endian>::do_relax(int pass,
3606 					   const Input_objects*,
3607 					   Symbol_table* symtab,
3608 					   Layout* layout,
3609 					   const Task* task)
3610 {
3611   unsigned int prev_brlt_size = 0;
3612   if (pass == 1)
3613     {
3614       bool thread_safe
3615 	= this->abiversion() < 2 && parameters->options().plt_thread_safe();
3616       if (size == 64
3617 	  && this->abiversion() < 2
3618 	  && !thread_safe
3619 	  && !parameters->options().user_set_plt_thread_safe())
3620 	{
3621 	  static const char* const thread_starter[] =
3622 	    {
3623 	      "pthread_create",
3624 	      /* libstdc++ */
3625 	      "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3626 	      /* librt */
3627 	      "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3628 	      "mq_notify", "create_timer",
3629 	      /* libanl */
3630 	      "getaddrinfo_a",
3631 	      /* libgomp */
3632 	      "GOMP_parallel",
3633 	      "GOMP_parallel_start",
3634 	      "GOMP_parallel_loop_static",
3635 	      "GOMP_parallel_loop_static_start",
3636 	      "GOMP_parallel_loop_dynamic",
3637 	      "GOMP_parallel_loop_dynamic_start",
3638 	      "GOMP_parallel_loop_guided",
3639 	      "GOMP_parallel_loop_guided_start",
3640 	      "GOMP_parallel_loop_runtime",
3641 	      "GOMP_parallel_loop_runtime_start",
3642 	      "GOMP_parallel_sections",
3643 	      "GOMP_parallel_sections_start",
3644 	      /* libgo */
3645 	      "__go_go",
3646 	    };
3647 
3648 	  if (parameters->options().shared())
3649 	    thread_safe = true;
3650 	  else
3651 	    {
3652 	      for (unsigned int i = 0;
3653 		   i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3654 		   i++)
3655 		{
3656 		  Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3657 		  thread_safe = (sym != NULL
3658 				 && sym->in_reg()
3659 				 && sym->in_real_elf());
3660 		  if (thread_safe)
3661 		    break;
3662 		}
3663 	    }
3664 	}
3665       this->plt_thread_safe_ = thread_safe;
3666     }
3667 
3668   if (pass == 1)
3669     {
3670       this->stub_group_size_ = parameters->options().stub_group_size();
3671       bool no_size_errors = true;
3672       if (this->stub_group_size_ == 1)
3673 	this->stub_group_size_ = 0x1c00000;
3674       else if (this->stub_group_size_ == -1)
3675 	this->stub_group_size_ = -0x1e00000;
3676       else
3677 	no_size_errors = false;
3678       this->group_sections(layout, task, no_size_errors);
3679     }
3680   else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3681     {
3682       this->branch_lookup_table_.clear();
3683       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3684 	   p != this->stub_tables_.end();
3685 	   ++p)
3686 	{
3687 	  (*p)->clear_stubs(true);
3688 	}
3689       this->stub_tables_.clear();
3690       this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3691       gold_info(_("%s: stub group size is too large; retrying with %#x"),
3692 		program_name, this->stub_group_size_);
3693       this->group_sections(layout, task, true);
3694     }
3695 
3696   // We need address of stub tables valid for make_stub.
3697   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3698        p != this->stub_tables_.end();
3699        ++p)
3700     {
3701       const Powerpc_relobj<size, big_endian>* object
3702 	= static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3703       Address off = object->get_output_section_offset((*p)->shndx());
3704       gold_assert(off != invalid_address);
3705       Output_section* os = (*p)->output_section();
3706       (*p)->set_address_and_size(os, off);
3707     }
3708 
3709   if (pass != 1)
3710     {
3711       // Clear plt call stubs, long branch stubs and branch lookup table.
3712       prev_brlt_size = this->branch_lookup_table_.size();
3713       this->branch_lookup_table_.clear();
3714       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3715 	   p != this->stub_tables_.end();
3716 	   ++p)
3717 	{
3718 	  (*p)->clear_stubs(false);
3719 	}
3720     }
3721 
3722   // Build all the stubs.
3723   this->relax_failed_ = false;
3724   Stub_table<size, big_endian>* ifunc_stub_table
3725     = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3726   Stub_table<size, big_endian>* one_stub_table
3727     = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3728   for (typename Branches::const_iterator b = this->branch_info_.begin();
3729        b != this->branch_info_.end();
3730        b++)
3731     {
3732       if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3733 	  && !this->relax_failed_)
3734 	{
3735 	  this->relax_failed_ = true;
3736 	  this->relax_fail_count_++;
3737 	  if (this->relax_fail_count_ < 3)
3738 	    return true;
3739 	}
3740     }
3741   bool do_resize = false;
3742   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3743        p != this->stub_tables_.end();
3744        ++p)
3745     if ((*p)->need_resize())
3746       {
3747 	do_resize = true;
3748 	break;
3749       }
3750   if (do_resize)
3751     {
3752       this->branch_lookup_table_.clear();
3753       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3754 	   p != this->stub_tables_.end();
3755 	   ++p)
3756 	(*p)->set_resizing(true);
3757       for (typename Branches::const_iterator b = this->branch_info_.begin();
3758 	   b != this->branch_info_.end();
3759 	   b++)
3760 	{
3761 	  if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3762 	      && !this->relax_failed_)
3763 	    {
3764 	      this->relax_failed_ = true;
3765 	      this->relax_fail_count_++;
3766 	      if (this->relax_fail_count_ < 3)
3767 		return true;
3768 	    }
3769 	}
3770       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3771 	   p != this->stub_tables_.end();
3772 	   ++p)
3773 	(*p)->set_resizing(false);
3774     }
3775 
3776   // Did anything change size?
3777   unsigned int num_huge_branches = this->branch_lookup_table_.size();
3778   bool again = num_huge_branches != prev_brlt_size;
3779   if (size == 64 && num_huge_branches != 0)
3780     this->make_brlt_section(layout);
3781   if (size == 64 && again)
3782     this->brlt_section_->set_current_size(num_huge_branches);
3783 
3784   for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3785        p != this->stub_tables_.rend();
3786        ++p)
3787     (*p)->remove_eh_frame(layout);
3788 
3789   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3790        p != this->stub_tables_.end();
3791        ++p)
3792     (*p)->add_eh_frame(layout);
3793 
3794   typedef Unordered_set<Output_section*> Output_sections;
3795   Output_sections os_need_update;
3796   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3797        p != this->stub_tables_.end();
3798        ++p)
3799     {
3800       if ((*p)->size_update())
3801 	{
3802 	  again = true;
3803 	  os_need_update.insert((*p)->output_section());
3804 	}
3805     }
3806 
3807   // Set output section offsets for all input sections in an output
3808   // section that just changed size.  Anything past the stubs will
3809   // need updating.
3810   for (typename Output_sections::iterator p = os_need_update.begin();
3811        p != os_need_update.end();
3812        p++)
3813     {
3814       Output_section* os = *p;
3815       Address off = 0;
3816       typedef Output_section::Input_section_list Input_section_list;
3817       for (Input_section_list::const_iterator i = os->input_sections().begin();
3818 	   i != os->input_sections().end();
3819 	   ++i)
3820 	{
3821 	  off = align_address(off, i->addralign());
3822 	  if (i->is_input_section() || i->is_relaxed_input_section())
3823 	    i->relobj()->set_section_offset(i->shndx(), off);
3824 	  if (i->is_relaxed_input_section())
3825 	    {
3826 	      Stub_table<size, big_endian>* stub_table
3827 		= static_cast<Stub_table<size, big_endian>*>(
3828 		    i->relaxed_input_section());
3829 	      Address stub_table_size = stub_table->set_address_and_size(os, off);
3830 	      off += stub_table_size;
3831 	      // After a few iterations, set current stub table size
3832 	      // as min size threshold, so later stub tables can only
3833 	      // grow in size.
3834 	      if (pass >= 4)
3835 		stub_table->set_min_size_threshold(stub_table_size);
3836 	    }
3837 	  else
3838 	    off += i->data_size();
3839 	}
3840       // If .branch_lt is part of this output section, then we have
3841       // just done the offset adjustment.
3842       os->clear_section_offsets_need_adjustment();
3843     }
3844 
3845   if (size == 64
3846       && !again
3847       && num_huge_branches != 0
3848       && parameters->options().output_is_position_independent())
3849     {
3850       // Fill in the BRLT relocs.
3851       this->brlt_section_->reset_brlt_sizes();
3852       for (typename Branch_lookup_table::const_iterator p
3853 	     = this->branch_lookup_table_.begin();
3854 	   p != this->branch_lookup_table_.end();
3855 	   ++p)
3856 	{
3857 	  this->brlt_section_->add_reloc(p->first, p->second);
3858 	}
3859       this->brlt_section_->finalize_brlt_sizes();
3860     }
3861 
3862   if (!again
3863       && (parameters->options().user_set_emit_stub_syms()
3864 	  ? parameters->options().emit_stub_syms()
3865 	  : (size == 64
3866 	     || parameters->options().output_is_position_independent()
3867 	     || parameters->options().emit_relocs())))
3868     {
3869       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3870 	   p != this->stub_tables_.end();
3871 	   ++p)
3872 	(*p)->define_stub_syms(symtab);
3873 
3874       if (this->glink_ != NULL)
3875 	{
3876 	  int stub_size = this->glink_->pltresolve_size();
3877 	  Address value = -stub_size;
3878 	  if (size == 64)
3879 	    {
3880 	      value = 8;
3881 	      stub_size -= 8;
3882 	    }
3883 	  this->define_local(symtab, "__glink_PLTresolve",
3884 			     this->glink_, value, stub_size);
3885 
3886 	  if (size != 64)
3887 	    this->define_local(symtab, "__glink", this->glink_, 0, 0);
3888 	}
3889     }
3890 
3891   return again;
3892 }
3893 
3894 template<int size, bool big_endian>
3895 void
3896 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3897 						      unsigned char* oview,
3898 						      uint64_t* paddress,
3899 						      off_t* plen) const
3900 {
3901   uint64_t address = plt->address();
3902   off_t len = plt->data_size();
3903 
3904   if (plt == this->glink_)
3905     {
3906       // See Output_data_glink::do_write() for glink contents.
3907       if (len == 0)
3908 	{
3909 	  gold_assert(parameters->doing_static_link());
3910 	  // Static linking may need stubs, to support ifunc and long
3911 	  // branches.  We need to create an output section for
3912 	  // .eh_frame early in the link process, to have a place to
3913 	  // attach stub .eh_frame info.  We also need to have
3914 	  // registered a CIE that matches the stub CIE.  Both of
3915 	  // these requirements are satisfied by creating an FDE and
3916 	  // CIE for .glink, even though static linking will leave
3917 	  // .glink zero length.
3918 	  // ??? Hopefully generating an FDE with a zero address range
3919 	  // won't confuse anything that consumes .eh_frame info.
3920 	}
3921       else if (size == 64)
3922 	{
3923 	  // There is one word before __glink_PLTresolve
3924 	  address += 8;
3925 	  len -= 8;
3926 	}
3927       else if (parameters->options().output_is_position_independent())
3928 	{
3929 	  // There are two FDEs for a position independent glink.
3930 	  // The first covers the branch table, the second
3931 	  // __glink_PLTresolve at the end of glink.
3932 	  off_t resolve_size = this->glink_->pltresolve_size();
3933 	  if (oview[9] == elfcpp::DW_CFA_nop)
3934 	    len -= resolve_size;
3935 	  else
3936 	    {
3937 	      address += len - resolve_size;
3938 	      len = resolve_size;
3939 	    }
3940 	}
3941     }
3942   else
3943     {
3944       // Must be a stub table.
3945       const Stub_table<size, big_endian>* stub_table
3946 	= static_cast<const Stub_table<size, big_endian>*>(plt);
3947       uint64_t stub_address = stub_table->stub_address();
3948       len -= stub_address - address;
3949       address = stub_address;
3950     }
3951 
3952   *paddress = address;
3953   *plen = len;
3954 }
3955 
3956 // A class to handle the PLT data.
3957 
3958 template<int size, bool big_endian>
3959 class Output_data_plt_powerpc : public Output_section_data_build
3960 {
3961  public:
3962   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3963 			    size, big_endian> Reloc_section;
3964 
3965   Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
3966 			  Reloc_section* plt_rel,
3967 			  const char* name)
3968     : Output_section_data_build(size == 32 ? 4 : 8),
3969       rel_(plt_rel),
3970       targ_(targ),
3971       name_(name)
3972   { }
3973 
3974   // Add an entry to the PLT.
3975   void
3976   add_entry(Symbol*);
3977 
3978   void
3979   add_ifunc_entry(Symbol*);
3980 
3981   void
3982   add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3983 
3984   void
3985   add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
3986 
3987   // Return the .rela.plt section data.
3988   Reloc_section*
3989   rel_plt() const
3990   {
3991     return this->rel_;
3992   }
3993 
3994   // Return the number of PLT entries.
3995   unsigned int
3996   entry_count() const
3997   {
3998     if (this->current_data_size() == 0)
3999       return 0;
4000     return ((this->current_data_size() - this->first_plt_entry_offset())
4001 	    / this->plt_entry_size());
4002   }
4003 
4004  protected:
4005   void
4006   do_adjust_output_section(Output_section* os)
4007   {
4008     os->set_entsize(0);
4009   }
4010 
4011   // Write to a map file.
4012   void
4013   do_print_to_mapfile(Mapfile* mapfile) const
4014   { mapfile->print_output_data(this, this->name_); }
4015 
4016  private:
4017   // Return the offset of the first non-reserved PLT entry.
4018   unsigned int
4019   first_plt_entry_offset() const
4020   {
4021     // IPLT and LPLT have no reserved entry.
4022     if (this->name_[3] == 'I' || this->name_[3] == 'L')
4023       return 0;
4024     return this->targ_->first_plt_entry_offset();
4025   }
4026 
4027   // Return the size of each PLT entry.
4028   unsigned int
4029   plt_entry_size() const
4030   {
4031     return this->targ_->plt_entry_size();
4032   }
4033 
4034   // Write out the PLT data.
4035   void
4036   do_write(Output_file*);
4037 
4038   // The reloc section.
4039   Reloc_section* rel_;
4040   // Allows access to .glink for do_write.
4041   Target_powerpc<size, big_endian>* targ_;
4042   // What to report in map file.
4043   const char *name_;
4044 };
4045 
4046 // Add an entry to the PLT.
4047 
4048 template<int size, bool big_endian>
4049 void
4050 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
4051 {
4052   if (!gsym->has_plt_offset())
4053     {
4054       section_size_type off = this->current_data_size();
4055       if (off == 0)
4056 	off += this->first_plt_entry_offset();
4057       gsym->set_plt_offset(off);
4058       gsym->set_needs_dynsym_entry();
4059       unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4060       this->rel_->add_global(gsym, dynrel, this, off, 0);
4061       off += this->plt_entry_size();
4062       this->set_current_data_size(off);
4063     }
4064 }
4065 
4066 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4067 
4068 template<int size, bool big_endian>
4069 void
4070 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4071 {
4072   if (!gsym->has_plt_offset())
4073     {
4074       section_size_type off = this->current_data_size();
4075       gsym->set_plt_offset(off);
4076       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4077       if (size == 64 && this->targ_->abiversion() < 2)
4078 	dynrel = elfcpp::R_PPC64_JMP_IREL;
4079       this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4080       off += this->plt_entry_size();
4081       this->set_current_data_size(off);
4082     }
4083 }
4084 
4085 // Add an entry for a local symbol to the PLT.
4086 
4087 template<int size, bool big_endian>
4088 void
4089 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4090     Sized_relobj_file<size, big_endian>* relobj,
4091     unsigned int local_sym_index)
4092 {
4093   if (!relobj->local_has_plt_offset(local_sym_index))
4094     {
4095       section_size_type off = this->current_data_size();
4096       relobj->set_local_plt_offset(local_sym_index, off);
4097       if (this->rel_)
4098 	{
4099 	  unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4100 	  if (size == 64 && this->targ_->abiversion() < 2)
4101 	    dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4102 	  this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4103 						  dynrel, this, off, 0);
4104 	}
4105       off += this->plt_entry_size();
4106       this->set_current_data_size(off);
4107     }
4108 }
4109 
4110 // Add an entry for a local ifunc symbol to the IPLT.
4111 
4112 template<int size, bool big_endian>
4113 void
4114 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4115     Sized_relobj_file<size, big_endian>* relobj,
4116     unsigned int local_sym_index)
4117 {
4118   if (!relobj->local_has_plt_offset(local_sym_index))
4119     {
4120       section_size_type off = this->current_data_size();
4121       relobj->set_local_plt_offset(local_sym_index, off);
4122       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4123       if (size == 64 && this->targ_->abiversion() < 2)
4124 	dynrel = elfcpp::R_PPC64_JMP_IREL;
4125       this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4126 					      this, off, 0);
4127       off += this->plt_entry_size();
4128       this->set_current_data_size(off);
4129     }
4130 }
4131 
4132 static const uint32_t add_0_11_11	= 0x7c0b5a14;
4133 static const uint32_t add_2_2_11	= 0x7c425a14;
4134 static const uint32_t add_2_2_12	= 0x7c426214;
4135 static const uint32_t add_3_3_2		= 0x7c631214;
4136 static const uint32_t add_3_3_13	= 0x7c636a14;
4137 static const uint32_t add_3_12_2	= 0x7c6c1214;
4138 static const uint32_t add_3_12_13	= 0x7c6c6a14;
4139 static const uint32_t add_11_0_11	= 0x7d605a14;
4140 static const uint32_t add_11_2_11	= 0x7d625a14;
4141 static const uint32_t add_11_11_2	= 0x7d6b1214;
4142 static const uint32_t add_12_11_12	= 0x7d8b6214;
4143 static const uint32_t addi_0_12		= 0x380c0000;
4144 static const uint32_t addi_2_2		= 0x38420000;
4145 static const uint32_t addi_3_3		= 0x38630000;
4146 static const uint32_t addi_11_11	= 0x396b0000;
4147 static const uint32_t addi_12_1		= 0x39810000;
4148 static const uint32_t addi_12_11	= 0x398b0000;
4149 static const uint32_t addi_12_12	= 0x398c0000;
4150 static const uint32_t addis_0_2		= 0x3c020000;
4151 static const uint32_t addis_0_13	= 0x3c0d0000;
4152 static const uint32_t addis_2_12	= 0x3c4c0000;
4153 static const uint32_t addis_11_2	= 0x3d620000;
4154 static const uint32_t addis_11_11	= 0x3d6b0000;
4155 static const uint32_t addis_11_30	= 0x3d7e0000;
4156 static const uint32_t addis_12_1	= 0x3d810000;
4157 static const uint32_t addis_12_2	= 0x3d820000;
4158 static const uint32_t addis_12_11	= 0x3d8b0000;
4159 static const uint32_t addis_12_12	= 0x3d8c0000;
4160 static const uint32_t b			= 0x48000000;
4161 static const uint32_t bcl_20_31		= 0x429f0005;
4162 static const uint32_t bctr		= 0x4e800420;
4163 static const uint32_t bctrl		= 0x4e800421;
4164 static const uint32_t beqlr		= 0x4d820020;
4165 static const uint32_t blr		= 0x4e800020;
4166 static const uint32_t bnectr_p4		= 0x4ce20420;
4167 static const uint32_t cmpld_7_12_0	= 0x7fac0040;
4168 static const uint32_t cmpldi_2_0	= 0x28220000;
4169 static const uint32_t cmpdi_11_0	= 0x2c2b0000;
4170 static const uint32_t cmpwi_11_0	= 0x2c0b0000;
4171 static const uint32_t cror_15_15_15	= 0x4def7b82;
4172 static const uint32_t cror_31_31_31	= 0x4ffffb82;
4173 static const uint32_t ld_0_1		= 0xe8010000;
4174 static const uint32_t ld_0_12		= 0xe80c0000;
4175 static const uint32_t ld_2_1		= 0xe8410000;
4176 static const uint32_t ld_2_2		= 0xe8420000;
4177 static const uint32_t ld_2_11		= 0xe84b0000;
4178 static const uint32_t ld_2_12		= 0xe84c0000;
4179 static const uint32_t ld_11_1		= 0xe9610000;
4180 static const uint32_t ld_11_2		= 0xe9620000;
4181 static const uint32_t ld_11_3		= 0xe9630000;
4182 static const uint32_t ld_11_11		= 0xe96b0000;
4183 static const uint32_t ld_12_2		= 0xe9820000;
4184 static const uint32_t ld_12_3		= 0xe9830000;
4185 static const uint32_t ld_12_11		= 0xe98b0000;
4186 static const uint32_t ld_12_12		= 0xe98c0000;
4187 static const uint32_t ldx_12_11_12	= 0x7d8b602a;
4188 static const uint32_t lfd_0_1		= 0xc8010000;
4189 static const uint32_t li_0_0		= 0x38000000;
4190 static const uint32_t li_11_0		= 0x39600000;
4191 static const uint32_t li_12_0		= 0x39800000;
4192 static const uint32_t lis_0		= 0x3c000000;
4193 static const uint32_t lis_2		= 0x3c400000;
4194 static const uint32_t lis_11		= 0x3d600000;
4195 static const uint32_t lis_12		= 0x3d800000;
4196 static const uint32_t lvx_0_12_0	= 0x7c0c00ce;
4197 static const uint32_t lwz_0_12		= 0x800c0000;
4198 static const uint32_t lwz_11_3		= 0x81630000;
4199 static const uint32_t lwz_11_11		= 0x816b0000;
4200 static const uint32_t lwz_11_30		= 0x817e0000;
4201 static const uint32_t lwz_12_3		= 0x81830000;
4202 static const uint32_t lwz_12_12		= 0x818c0000;
4203 static const uint32_t lwzu_0_12		= 0x840c0000;
4204 static const uint32_t mflr_0		= 0x7c0802a6;
4205 static const uint32_t mflr_11		= 0x7d6802a6;
4206 static const uint32_t mflr_12		= 0x7d8802a6;
4207 static const uint32_t mr_0_3		= 0x7c601b78;
4208 static const uint32_t mr_3_0		= 0x7c030378;
4209 static const uint32_t mtctr_0		= 0x7c0903a6;
4210 static const uint32_t mtctr_11		= 0x7d6903a6;
4211 static const uint32_t mtctr_12		= 0x7d8903a6;
4212 static const uint32_t mtlr_0		= 0x7c0803a6;
4213 static const uint32_t mtlr_11		= 0x7d6803a6;
4214 static const uint32_t mtlr_12		= 0x7d8803a6;
4215 static const uint32_t nop		= 0x60000000;
4216 static const uint32_t ori_0_0_0		= 0x60000000;
4217 static const uint32_t ori_11_11_0	= 0x616b0000;
4218 static const uint32_t ori_12_12_0	= 0x618c0000;
4219 static const uint32_t oris_12_12_0	= 0x658c0000;
4220 static const uint32_t sldi_11_11_34	= 0x796b1746;
4221 static const uint32_t sldi_12_12_32	= 0x799c07c6;
4222 static const uint32_t srdi_0_0_2	= 0x7800f082;
4223 static const uint32_t std_0_1		= 0xf8010000;
4224 static const uint32_t std_0_12		= 0xf80c0000;
4225 static const uint32_t std_2_1		= 0xf8410000;
4226 static const uint32_t std_11_1		= 0xf9610000;
4227 static const uint32_t stfd_0_1		= 0xd8010000;
4228 static const uint32_t stvx_0_12_0	= 0x7c0c01ce;
4229 static const uint32_t sub_11_11_12	= 0x7d6c5850;
4230 static const uint32_t sub_12_12_11	= 0x7d8b6050;
4231 static const uint32_t xor_2_12_12	= 0x7d826278;
4232 static const uint32_t xor_11_12_12	= 0x7d8b6278;
4233 
4234 static const uint64_t paddi_12_pc	= 0x0610000039800000ULL;
4235 static const uint64_t pld_12_pc		= 0x04100000e5800000ULL;
4236 static const uint64_t pnop		= 0x0700000000000000ULL;
4237 
4238 // Write out the PLT.
4239 
4240 template<int size, bool big_endian>
4241 void
4242 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4243 {
4244   if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4245     {
4246       const section_size_type offset = this->offset();
4247       const section_size_type oview_size
4248 	= convert_to_section_size_type(this->data_size());
4249       unsigned char* const oview = of->get_output_view(offset, oview_size);
4250       unsigned char* pov = oview;
4251       unsigned char* endpov = oview + oview_size;
4252 
4253       // The address of the .glink branch table
4254       const Output_data_glink<size, big_endian>* glink
4255 	= this->targ_->glink_section();
4256       elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4257 
4258       while (pov < endpov)
4259 	{
4260 	  elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4261 	  pov += 4;
4262 	  branch_tab += 4;
4263 	}
4264 
4265       of->write_output_view(offset, oview_size, oview);
4266     }
4267 }
4268 
4269 // Create the PLT section.
4270 
4271 template<int size, bool big_endian>
4272 void
4273 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4274 						   Layout* layout)
4275 {
4276   if (this->plt_ == NULL)
4277     {
4278       if (this->got_ == NULL)
4279 	this->got_section(symtab, layout);
4280 
4281       if (this->glink_ == NULL)
4282 	make_glink_section(layout);
4283 
4284       // Ensure that .rela.dyn always appears before .rela.plt  This is
4285       // necessary due to how, on PowerPC and some other targets, .rela.dyn
4286       // needs to include .rela.plt in its range.
4287       this->rela_dyn_section(layout);
4288 
4289       Reloc_section* plt_rel = new Reloc_section(false);
4290       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4291 				      elfcpp::SHF_ALLOC, plt_rel,
4292 				      ORDER_DYNAMIC_PLT_RELOCS, false);
4293       this->plt_
4294 	= new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
4295 							"** PLT");
4296       layout->add_output_section_data(".plt",
4297 				      (size == 32
4298 				       ? elfcpp::SHT_PROGBITS
4299 				       : elfcpp::SHT_NOBITS),
4300 				      elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4301 				      this->plt_,
4302 				      (size == 32
4303 				       ? ORDER_SMALL_DATA
4304 				       : ORDER_SMALL_BSS),
4305 				      false);
4306 
4307       Output_section* rela_plt_os = plt_rel->output_section();
4308       rela_plt_os->set_info_section(this->plt_->output_section());
4309     }
4310 }
4311 
4312 // Create the IPLT section.
4313 
4314 template<int size, bool big_endian>
4315 void
4316 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4317 						    Layout* layout)
4318 {
4319   if (this->iplt_ == NULL)
4320     {
4321       this->make_plt_section(symtab, layout);
4322       this->make_lplt_section(layout);
4323 
4324       Reloc_section* iplt_rel = new Reloc_section(false);
4325       if (this->rela_dyn_->output_section())
4326 	this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4327       this->iplt_
4328 	= new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
4329 							"** IPLT");
4330       if (this->plt_->output_section())
4331 	this->plt_->output_section()->add_output_section_data(this->iplt_);
4332     }
4333 }
4334 
4335 // Create the LPLT section.
4336 
4337 template<int size, bool big_endian>
4338 void
4339 Target_powerpc<size, big_endian>::make_lplt_section(Layout* layout)
4340 {
4341   if (this->lplt_ == NULL)
4342     {
4343       Reloc_section* lplt_rel = NULL;
4344       if (parameters->options().output_is_position_independent())
4345 	{
4346 	  lplt_rel = new Reloc_section(false);
4347 	  this->rela_dyn_section(layout);
4348 	  if (this->rela_dyn_->output_section())
4349 	    this->rela_dyn_->output_section()
4350 	      ->add_output_section_data(lplt_rel);
4351 	}
4352       this->lplt_
4353 	= new Output_data_plt_powerpc<size, big_endian>(this, lplt_rel,
4354 							"** LPLT");
4355       this->make_brlt_section(layout);
4356       if (this->brlt_section_ && this->brlt_section_->output_section())
4357 	this->brlt_section_->output_section()
4358 	  ->add_output_section_data(this->lplt_);
4359       else
4360 	layout->add_output_section_data(".branch_lt",
4361 					elfcpp::SHT_PROGBITS,
4362 					elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4363 					this->lplt_,
4364 					ORDER_RELRO,
4365 					true);
4366     }
4367 }
4368 
4369 // A section for huge long branch addresses, similar to plt section.
4370 
4371 template<int size, bool big_endian>
4372 class Output_data_brlt_powerpc : public Output_section_data_build
4373 {
4374  public:
4375   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4376   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4377 			    size, big_endian> Reloc_section;
4378 
4379   Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4380 			   Reloc_section* brlt_rel)
4381     : Output_section_data_build(size == 32 ? 4 : 8),
4382       rel_(brlt_rel),
4383       targ_(targ)
4384   { }
4385 
4386   void
4387   reset_brlt_sizes()
4388   {
4389     this->reset_data_size();
4390     this->rel_->reset_data_size();
4391   }
4392 
4393   void
4394   finalize_brlt_sizes()
4395   {
4396     this->finalize_data_size();
4397     this->rel_->finalize_data_size();
4398   }
4399 
4400   // Add a reloc for an entry in the BRLT.
4401   void
4402   add_reloc(Address to, unsigned int off)
4403   { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4404 
4405   // Update section and reloc section size.
4406   void
4407   set_current_size(unsigned int num_branches)
4408   {
4409     this->reset_address_and_file_offset();
4410     this->set_current_data_size(num_branches * 16);
4411     this->finalize_data_size();
4412     Output_section* os = this->output_section();
4413     os->set_section_offsets_need_adjustment();
4414     if (this->rel_ != NULL)
4415       {
4416 	const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4417 	this->rel_->reset_address_and_file_offset();
4418 	this->rel_->set_current_data_size(num_branches * reloc_size);
4419 	this->rel_->finalize_data_size();
4420 	Output_section* os = this->rel_->output_section();
4421 	os->set_section_offsets_need_adjustment();
4422       }
4423   }
4424 
4425  protected:
4426   void
4427   do_adjust_output_section(Output_section* os)
4428   {
4429     os->set_entsize(0);
4430   }
4431 
4432   // Write to a map file.
4433   void
4434   do_print_to_mapfile(Mapfile* mapfile) const
4435   { mapfile->print_output_data(this, "** BRLT"); }
4436 
4437  private:
4438   // Write out the BRLT data.
4439   void
4440   do_write(Output_file*);
4441 
4442   // The reloc section.
4443   Reloc_section* rel_;
4444   Target_powerpc<size, big_endian>* targ_;
4445 };
4446 
4447 // Make the branch lookup table section.
4448 
4449 template<int size, bool big_endian>
4450 void
4451 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4452 {
4453   if (size == 64 && this->brlt_section_ == NULL)
4454     {
4455       Reloc_section* brlt_rel = NULL;
4456       bool is_pic = parameters->options().output_is_position_independent();
4457       if (is_pic)
4458 	{
4459 	  // When PIC we can't fill in .branch_lt but must initialise at
4460 	  // runtime via dynamic relocations.
4461 	  this->rela_dyn_section(layout);
4462 	  brlt_rel = new Reloc_section(false);
4463 	  if (this->rela_dyn_->output_section())
4464 	    this->rela_dyn_->output_section()
4465 	      ->add_output_section_data(brlt_rel);
4466 	}
4467       this->brlt_section_
4468 	= new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4469       if (this->plt_ && is_pic && this->plt_->output_section())
4470 	this->plt_->output_section()
4471 	  ->add_output_section_data(this->brlt_section_);
4472       else
4473 	layout->add_output_section_data(".branch_lt",
4474 					elfcpp::SHT_PROGBITS,
4475 					elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4476 					this->brlt_section_,
4477 					ORDER_RELRO,
4478 					true);
4479     }
4480 }
4481 
4482 // Write out .branch_lt when non-PIC.
4483 
4484 template<int size, bool big_endian>
4485 void
4486 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4487 {
4488   if (size == 64 && !parameters->options().output_is_position_independent())
4489     {
4490       const section_size_type offset = this->offset();
4491       const section_size_type oview_size
4492 	= convert_to_section_size_type(this->data_size());
4493       unsigned char* const oview = of->get_output_view(offset, oview_size);
4494 
4495       this->targ_->write_branch_lookup_table(oview);
4496       of->write_output_view(offset, oview_size, oview);
4497     }
4498 }
4499 
4500 static inline uint32_t
4501 l(uint32_t a)
4502 {
4503   return a & 0xffff;
4504 }
4505 
4506 static inline uint32_t
4507 hi(uint32_t a)
4508 {
4509   return l(a >> 16);
4510 }
4511 
4512 static inline uint32_t
4513 ha(uint32_t a)
4514 {
4515   return hi(a + 0x8000);
4516 }
4517 
4518 static inline uint64_t
4519 d34(uint64_t v)
4520 {
4521   return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4522 }
4523 
4524 static inline uint64_t
4525 ha34(uint64_t v)
4526 {
4527   return (v + (1ULL << 33)) >> 34;
4528 }
4529 
4530 template<int size>
4531 struct Eh_cie
4532 {
4533   static const unsigned char eh_frame_cie[12];
4534 };
4535 
4536 template<int size>
4537 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4538 {
4539   1,					// CIE version.
4540   'z', 'R', 0,				// Augmentation string.
4541   4,					// Code alignment.
4542   0x80 - size / 8 ,			// Data alignment.
4543   65,					// RA reg.
4544   1,					// Augmentation size.
4545   (elfcpp::DW_EH_PE_pcrel
4546    | elfcpp::DW_EH_PE_sdata4),		// FDE encoding.
4547   elfcpp::DW_CFA_def_cfa, 1, 0		// def_cfa: r1 offset 0.
4548 };
4549 
4550 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4551 static const unsigned char glink_eh_frame_fde_64v1[] =
4552 {
4553   0, 0, 0, 0,				// Replaced with offset to .glink.
4554   0, 0, 0, 0,				// Replaced with size of .glink.
4555   0,					// Augmentation size.
4556   elfcpp::DW_CFA_advance_loc + 1,
4557   elfcpp::DW_CFA_register, 65, 12,
4558   elfcpp::DW_CFA_advance_loc + 5,
4559   elfcpp::DW_CFA_restore_extended, 65
4560 };
4561 
4562 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4563 static const unsigned char glink_eh_frame_fde_64v2[] =
4564 {
4565   0, 0, 0, 0,				// Replaced with offset to .glink.
4566   0, 0, 0, 0,				// Replaced with size of .glink.
4567   0,					// Augmentation size.
4568   elfcpp::DW_CFA_advance_loc + 1,
4569   elfcpp::DW_CFA_register, 65, 0,
4570   elfcpp::DW_CFA_advance_loc + 7,
4571   elfcpp::DW_CFA_restore_extended, 65
4572 };
4573 
4574 // Describe __glink_PLTresolve use of LR, 32-bit version.
4575 static const unsigned char glink_eh_frame_fde_32[] =
4576 {
4577   0, 0, 0, 0,				// Replaced with offset to .glink.
4578   0, 0, 0, 0,				// Replaced with size of .glink.
4579   0,					// Augmentation size.
4580   elfcpp::DW_CFA_advance_loc + 2,
4581   elfcpp::DW_CFA_register, 65, 0,
4582   elfcpp::DW_CFA_advance_loc + 4,
4583   elfcpp::DW_CFA_restore_extended, 65
4584 };
4585 
4586 static const unsigned char default_fde[] =
4587 {
4588   0, 0, 0, 0,				// Replaced with offset to stubs.
4589   0, 0, 0, 0,				// Replaced with size of stubs.
4590   0,					// Augmentation size.
4591   elfcpp::DW_CFA_nop,			// Pad.
4592   elfcpp::DW_CFA_nop,
4593   elfcpp::DW_CFA_nop
4594 };
4595 
4596 template<bool big_endian>
4597 static inline void
4598 write_insn(unsigned char* p, uint32_t v)
4599 {
4600   elfcpp::Swap<32, big_endian>::writeval(p, v);
4601 }
4602 
4603 template<int size>
4604 static inline unsigned int
4605 param_plt_align()
4606 {
4607   if (!parameters->options().user_set_plt_align())
4608     return size == 64 ? 32 : 8;
4609   return 1 << parameters->options().plt_align();
4610 }
4611 
4612 // Stub_table holds information about plt and long branch stubs.
4613 // Stubs are built in an area following some input section determined
4614 // by group_sections().  This input section is converted to a relaxed
4615 // input section allowing it to be resized to accommodate the stubs
4616 
4617 template<int size, bool big_endian>
4618 class Stub_table : public Output_relaxed_input_section
4619 {
4620  public:
4621   struct Plt_stub_ent
4622   {
4623     Plt_stub_ent(unsigned int off, unsigned int indx)
4624       : off_(off), indx_(indx), iter_(0), notoc_(0), r2save_(0), localentry0_(0)
4625     { }
4626 
4627     unsigned int off_;
4628     unsigned int indx_ : 28;
4629     unsigned int iter_ : 1;
4630     unsigned int notoc_ : 1;
4631     unsigned int r2save_ : 1;
4632     unsigned int localentry0_ : 1;
4633   };
4634   struct Branch_stub_ent
4635   {
4636     Branch_stub_ent(unsigned int off, bool notoc, bool save_res)
4637       : off_(off), iter_(false), notoc_(notoc), save_res_(save_res)
4638     { }
4639 
4640     unsigned int off_;
4641     bool iter_;
4642     bool notoc_;
4643     bool save_res_;
4644   };
4645   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4646   static const Address invalid_address = static_cast<Address>(0) - 1;
4647 
4648   Stub_table(Target_powerpc<size, big_endian>* targ,
4649 	     Output_section* output_section,
4650 	     const Output_section::Input_section* owner,
4651 	     uint32_t id)
4652     : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4653 				   owner->relobj()
4654 				   ->section_addralign(owner->shndx())),
4655       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4656       orig_data_size_(owner->current_data_size()),
4657       plt_size_(0), last_plt_size_(0),
4658       branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4659       need_save_res_(false), need_resize_(false), resizing_(false),
4660       uniq_(id)
4661   {
4662     this->set_output_section(output_section);
4663 
4664     std::vector<Output_relaxed_input_section*> new_relaxed;
4665     new_relaxed.push_back(this);
4666     output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4667   }
4668 
4669   // Add a plt call stub.
4670   bool
4671   add_plt_call_entry(Address,
4672 		     const Sized_relobj_file<size, big_endian>*,
4673 		     const Symbol*,
4674 		     unsigned int,
4675 		     Address,
4676 		     bool);
4677 
4678   bool
4679   add_plt_call_entry(Address,
4680 		     const Sized_relobj_file<size, big_endian>*,
4681 		     unsigned int,
4682 		     unsigned int,
4683 		     Address,
4684 		     bool);
4685 
4686   // Find a given plt call stub.
4687   const Plt_stub_ent*
4688   find_plt_call_entry(const Symbol*) const;
4689 
4690   const Plt_stub_ent*
4691   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4692 		      unsigned int) const;
4693 
4694   const Plt_stub_ent*
4695   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4696 		      const Symbol*,
4697 		      unsigned int,
4698 		      Address) const;
4699 
4700   const Plt_stub_ent*
4701   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4702 		      unsigned int,
4703 		      unsigned int,
4704 		      Address) const;
4705 
4706   // Add a long branch stub.
4707   bool
4708   add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4709 			unsigned int, Address, Address, bool);
4710 
4711   const Branch_stub_ent*
4712   find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
4713 			 Address) const;
4714 
4715   bool
4716   can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4717   {
4718     Address max_branch_offset = max_branch_delta<size>(r_type);
4719     if (max_branch_offset == 0)
4720       return true;
4721     gold_assert(from != invalid_address);
4722     Address loc = off + this->stub_address();
4723     return loc - from + max_branch_offset < 2 * max_branch_offset;
4724   }
4725 
4726   void
4727   clear_stubs(bool all)
4728   {
4729     this->plt_call_stubs_.clear();
4730     this->plt_size_ = 0;
4731     this->long_branch_stubs_.clear();
4732     this->branch_size_ = 0;
4733     this->need_save_res_ = false;
4734     if (all)
4735       {
4736 	this->last_plt_size_ = 0;
4737 	this->last_branch_size_ = 0;
4738       }
4739   }
4740 
4741   bool
4742   need_resize() const
4743   { return need_resize_; }
4744 
4745   void
4746   set_resizing(bool val)
4747   {
4748     this->resizing_ = val;
4749     if (val)
4750       {
4751 	this->need_resize_ = false;
4752 	this->plt_size_ = 0;
4753 	this->branch_size_ = 0;
4754 	this->need_save_res_ = false;
4755       }
4756   }
4757 
4758   Address
4759   set_address_and_size(const Output_section* os, Address off)
4760   {
4761     Address start_off = off;
4762     off += this->orig_data_size_;
4763     Address my_size = this->plt_size_ + this->branch_size_;
4764     if (this->need_save_res_)
4765       my_size += this->targ_->savres_section()->data_size();
4766     if (my_size != 0)
4767       off = align_address(off, this->stub_align());
4768     // Include original section size and alignment padding in size
4769     my_size += off - start_off;
4770     // Ensure new size is always larger than min size
4771     // threshold. Alignment requirement is included in "my_size", so
4772     // increase "my_size" does not invalidate alignment.
4773     if (my_size < this->min_size_threshold_)
4774       my_size = this->min_size_threshold_;
4775     this->reset_address_and_file_offset();
4776     this->set_current_data_size(my_size);
4777     this->set_address_and_file_offset(os->address() + start_off,
4778 				      os->offset() + start_off);
4779     return my_size;
4780   }
4781 
4782   Address
4783   stub_address() const
4784   {
4785     return align_address(this->address() + this->orig_data_size_,
4786 			 this->stub_align());
4787   }
4788 
4789   Address
4790   stub_offset() const
4791   {
4792     return align_address(this->offset() + this->orig_data_size_,
4793 			 this->stub_align());
4794   }
4795 
4796   section_size_type
4797   plt_size() const
4798   { return this->plt_size_; }
4799 
4800   section_size_type
4801   branch_size() const
4802   { return this->branch_size_; }
4803 
4804   void
4805   set_min_size_threshold(Address min_size)
4806   { this->min_size_threshold_ = min_size; }
4807 
4808   void
4809   define_stub_syms(Symbol_table*);
4810 
4811   bool
4812   size_update()
4813   {
4814     Output_section* os = this->output_section();
4815     if (os->addralign() < this->stub_align())
4816       {
4817 	os->set_addralign(this->stub_align());
4818 	// FIXME: get rid of the insane checkpointing.
4819 	// We can't increase alignment of the input section to which
4820 	// stubs are attached;  The input section may be .init which
4821 	// is pasted together with other .init sections to form a
4822 	// function.  Aligning might insert zero padding resulting in
4823 	// sigill.  However we do need to increase alignment of the
4824 	// output section so that the align_address() on offset in
4825 	// set_address_and_size() adds the same padding as the
4826 	// align_address() on address in stub_address().
4827 	// What's more, we need this alignment for the layout done in
4828 	// relaxation_loop_body() so that the output section starts at
4829 	// a suitably aligned address.
4830 	os->checkpoint_set_addralign(this->stub_align());
4831       }
4832     if (this->last_plt_size_ != this->plt_size_
4833 	|| this->last_branch_size_ != this->branch_size_)
4834       {
4835 	this->last_plt_size_ = this->plt_size_;
4836 	this->last_branch_size_ = this->branch_size_;
4837 	return true;
4838       }
4839     return false;
4840   }
4841 
4842   // Add .eh_frame info for this stub section.
4843   void
4844   add_eh_frame(Layout* layout);
4845 
4846   // Remove .eh_frame info for this stub section.
4847   void
4848   remove_eh_frame(Layout* layout);
4849 
4850   Target_powerpc<size, big_endian>*
4851   targ() const
4852   { return targ_; }
4853 
4854  private:
4855   class Plt_stub_key;
4856   class Plt_stub_key_hash;
4857   typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
4858 			Plt_stub_key_hash> Plt_stub_entries;
4859   class Branch_stub_key;
4860   class Branch_stub_key_hash;
4861   typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
4862 			Branch_stub_key_hash> Branch_stub_entries;
4863 
4864   // Alignment of stub section.
4865   unsigned int
4866   stub_align() const
4867   {
4868     unsigned int min_align = size == 64 ? 32 : 16;
4869     unsigned int user_align = 1 << parameters->options().plt_align();
4870     return std::max(user_align, min_align);
4871   }
4872 
4873   // Return the plt offset for the given call stub.
4874   Address
4875   plt_off(typename Plt_stub_entries::const_iterator p,
4876 	  const Output_data_plt_powerpc<size, big_endian>** sec) const
4877   {
4878     const Symbol* gsym = p->first.sym_;
4879     if (gsym != NULL)
4880       return this->targ_->plt_off(gsym, sec);
4881     else
4882       {
4883 	const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
4884 	unsigned int local_sym_index = p->first.locsym_;
4885 	return this->targ_->plt_off(relobj, local_sym_index, sec);
4886       }
4887   }
4888 
4889   // Size of a given plt call stub.
4890   unsigned int
4891   plt_call_size(typename Plt_stub_entries::const_iterator p) const;
4892 
4893   unsigned int
4894   plt_call_align(unsigned int bytes) const
4895   {
4896     unsigned int align = param_plt_align<size>();
4897     return (bytes + align - 1) & -align;
4898   }
4899 
4900   // Return long branch stub size.
4901   unsigned int
4902   branch_stub_size(typename Branch_stub_entries::const_iterator p,
4903 		   bool* need_lt);
4904 
4905   bool
4906   build_tls_opt_head(unsigned char** pp,
4907 		     typename Plt_stub_entries::const_iterator cs);
4908 
4909   bool
4910   build_tls_opt_tail(unsigned char* p,
4911 		     typename Plt_stub_entries::const_iterator cs);
4912 
4913   void
4914   plt_error(const Plt_stub_key& p);
4915 
4916   // Write out stubs.
4917   void
4918   do_write(Output_file*);
4919 
4920   // Plt call stub keys.
4921   class Plt_stub_key
4922   {
4923   public:
4924     Plt_stub_key(const Symbol* sym)
4925       : sym_(sym), object_(0), addend_(0), locsym_(0)
4926     { }
4927 
4928     Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4929 		 unsigned int locsym_index)
4930       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4931     { }
4932 
4933     Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4934 		 const Symbol* sym,
4935 		 unsigned int r_type,
4936 		 Address addend)
4937       : sym_(sym), object_(0), addend_(0), locsym_(0)
4938     {
4939       if (size != 32)
4940 	this->addend_ = addend;
4941       else if (parameters->options().output_is_position_independent()
4942 	       && (r_type == elfcpp::R_PPC_PLTREL24
4943 		   || r_type == elfcpp::R_POWERPC_PLTCALL))
4944 	{
4945 	  this->addend_ = addend;
4946 	  if (this->addend_ >= 32768)
4947 	    this->object_ = object;
4948 	}
4949     }
4950 
4951     Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
4952 		 unsigned int locsym_index,
4953 		 unsigned int r_type,
4954 		 Address addend)
4955       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
4956     {
4957       if (size != 32)
4958 	this->addend_ = addend;
4959       else if (parameters->options().output_is_position_independent()
4960 	       && (r_type == elfcpp::R_PPC_PLTREL24
4961 		   || r_type == elfcpp::R_POWERPC_PLTCALL))
4962 	this->addend_ = addend;
4963     }
4964 
4965     bool operator==(const Plt_stub_key& that) const
4966     {
4967       return (this->sym_ == that.sym_
4968 	      && this->object_ == that.object_
4969 	      && this->addend_ == that.addend_
4970 	      && this->locsym_ == that.locsym_);
4971     }
4972 
4973     const Symbol* sym_;
4974     const Sized_relobj_file<size, big_endian>* object_;
4975     typename elfcpp::Elf_types<size>::Elf_Addr addend_;
4976     unsigned int locsym_;
4977   };
4978 
4979   class Plt_stub_key_hash
4980   {
4981   public:
4982     size_t operator()(const Plt_stub_key& ent) const
4983     {
4984       return (reinterpret_cast<uintptr_t>(ent.sym_)
4985 	      ^ reinterpret_cast<uintptr_t>(ent.object_)
4986 	      ^ ent.addend_
4987 	      ^ ent.locsym_);
4988     }
4989   };
4990 
4991   // Long branch stub keys.
4992   class Branch_stub_key
4993   {
4994   public:
4995     Branch_stub_key(const Powerpc_relobj<size, big_endian>* obj, Address to)
4996       : dest_(to), toc_base_off_(0)
4997     {
4998       if (size == 64)
4999 	toc_base_off_ = obj->toc_base_offset();
5000     }
5001 
5002     bool operator==(const Branch_stub_key& that) const
5003     {
5004       return (this->dest_ == that.dest_
5005 	      && (size == 32
5006 		  || this->toc_base_off_ == that.toc_base_off_));
5007     }
5008 
5009     Address dest_;
5010     unsigned int toc_base_off_;
5011   };
5012 
5013   class Branch_stub_key_hash
5014   {
5015   public:
5016     size_t operator()(const Branch_stub_key& key) const
5017     { return key.dest_ ^ key.toc_base_off_; }
5018   };
5019 
5020   // In a sane world this would be a global.
5021   Target_powerpc<size, big_endian>* targ_;
5022   // Map sym/object/addend to stub offset.
5023   Plt_stub_entries plt_call_stubs_;
5024   // Map destination address to stub offset.
5025   Branch_stub_entries long_branch_stubs_;
5026   // size of input section
5027   section_size_type orig_data_size_;
5028   // size of stubs
5029   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5030   // Some rare cases cause (PR/20529) fluctuation in stub table
5031   // size, which leads to an endless relax loop. This is to be fixed
5032   // by, after the first few iterations, allowing only increase of
5033   // stub table size. This variable sets the minimal possible size of
5034   // a stub table, it is zero for the first few iterations, then
5035   // increases monotonically.
5036   Address min_size_threshold_;
5037   // Set if this stub group needs a copy of out-of-line register
5038   // save/restore functions.
5039   bool need_save_res_;
5040   // Set when notoc_/r2save_ changes after sizing a stub
5041   bool need_resize_;
5042   // Set when resizing stubs
5043   bool resizing_;
5044   // Per stub table unique identifier.
5045   uint32_t uniq_;
5046 };
5047 
5048 // Add a plt call stub, if we do not already have one for this
5049 // sym/object/addend combo.
5050 
5051 template<int size, bool big_endian>
5052 bool
5053 Stub_table<size, big_endian>::add_plt_call_entry(
5054     Address from,
5055     const Sized_relobj_file<size, big_endian>* object,
5056     const Symbol* gsym,
5057     unsigned int r_type,
5058     Address addend,
5059     bool tocsave)
5060 {
5061   Plt_stub_key key(object, gsym, r_type, addend);
5062   Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5063   std::pair<typename Plt_stub_entries::iterator, bool> p
5064     = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5065   if (size == 64)
5066     {
5067       if (p.second
5068 	  && this->targ_->is_elfv2_localentry0(gsym))
5069 	{
5070 	  p.first->second.localentry0_ = 1;
5071 	  this->targ_->set_has_localentry0();
5072 	}
5073       if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5074 	{
5075 	  if (!p.second && !p.first->second.notoc_
5076 	      && !this->targ_->powerxx_stubs())
5077 	    this->need_resize_ = true;
5078 	  p.first->second.notoc_ = 1;
5079 	}
5080       else if (!tocsave && !p.first->second.localentry0_)
5081 	{
5082 	  if (!p.second && !p.first->second.r2save_)
5083 	    this->need_resize_ = true;
5084 	  p.first->second.r2save_ = 1;
5085 	}
5086     }
5087   if (p.second || (this->resizing_ && !p.first->second.iter_))
5088     {
5089       if (this->resizing_)
5090 	{
5091 	  p.first->second.iter_ = 1;
5092 	  p.first->second.off_ = this->plt_size_;
5093 	}
5094       this->plt_size_ += this->plt_call_size(p.first);
5095       if (this->targ_->is_tls_get_addr_opt(gsym))
5096 	this->targ_->set_has_tls_get_addr_opt();
5097       this->plt_size_ = this->plt_call_align(this->plt_size_);
5098     }
5099   return this->can_reach_stub(from, p.first->second.off_, r_type);
5100 }
5101 
5102 template<int size, bool big_endian>
5103 bool
5104 Stub_table<size, big_endian>::add_plt_call_entry(
5105     Address from,
5106     const Sized_relobj_file<size, big_endian>* object,
5107     unsigned int locsym_index,
5108     unsigned int r_type,
5109     Address addend,
5110     bool tocsave)
5111 {
5112   Plt_stub_key key(object, locsym_index, r_type, addend);
5113   Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5114   std::pair<typename Plt_stub_entries::iterator, bool> p
5115     = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5116   if (size == 64)
5117     {
5118       if (p.second
5119 	  && this->targ_->is_elfv2_localentry0(object, locsym_index))
5120 	{
5121 	  p.first->second.localentry0_ = 1;
5122 	  this->targ_->set_has_localentry0();
5123 	}
5124       if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
5125 	{
5126 	  if (!p.second && !p.first->second.notoc_
5127 	      && !this->targ_->powerxx_stubs())
5128 	    this->need_resize_ = true;
5129 	  p.first->second.notoc_ = 1;
5130 	}
5131       else if (!tocsave && !p.first->second.localentry0_)
5132 	{
5133 	  if (!p.second && !p.first->second.r2save_)
5134 	    this->need_resize_ = true;
5135 	  p.first->second.r2save_ = 1;
5136 	}
5137     }
5138   if (p.second || (this->resizing_ && !p.first->second.iter_))
5139     {
5140       if (this->resizing_)
5141 	{
5142 	  p.first->second.iter_ = 1;
5143 	  p.first->second.off_ = this->plt_size_;
5144 	}
5145       this->plt_size_ += this->plt_call_size(p.first);
5146       this->plt_size_ = this->plt_call_align(this->plt_size_);
5147     }
5148   return this->can_reach_stub(from, p.first->second.off_, r_type);
5149 }
5150 
5151 // Find a plt call stub.
5152 
5153 template<int size, bool big_endian>
5154 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5155 Stub_table<size, big_endian>::find_plt_call_entry(
5156     const Sized_relobj_file<size, big_endian>* object,
5157     const Symbol* gsym,
5158     unsigned int r_type,
5159     Address addend) const
5160 {
5161   Plt_stub_key key(object, gsym, r_type, addend);
5162   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5163   if (p == this->plt_call_stubs_.end())
5164     return NULL;
5165   return &p->second;
5166 }
5167 
5168 template<int size, bool big_endian>
5169 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5170 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5171 {
5172   Plt_stub_key key(gsym);
5173   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5174   if (p == this->plt_call_stubs_.end())
5175     return NULL;
5176   return &p->second;
5177 }
5178 
5179 template<int size, bool big_endian>
5180 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5181 Stub_table<size, big_endian>::find_plt_call_entry(
5182     const Sized_relobj_file<size, big_endian>* object,
5183     unsigned int locsym_index,
5184     unsigned int r_type,
5185     Address addend) const
5186 {
5187   Plt_stub_key key(object, locsym_index, r_type, addend);
5188   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5189   if (p == this->plt_call_stubs_.end())
5190     return NULL;
5191   return &p->second;
5192 }
5193 
5194 template<int size, bool big_endian>
5195 const typename Stub_table<size, big_endian>::Plt_stub_ent*
5196 Stub_table<size, big_endian>::find_plt_call_entry(
5197     const Sized_relobj_file<size, big_endian>* object,
5198     unsigned int locsym_index) const
5199 {
5200   Plt_stub_key key(object, locsym_index);
5201   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5202   if (p == this->plt_call_stubs_.end())
5203     return NULL;
5204   return &p->second;
5205 }
5206 
5207 // Add a long branch stub if we don't already have one to given
5208 // destination.
5209 
5210 template<int size, bool big_endian>
5211 bool
5212 Stub_table<size, big_endian>::add_long_branch_entry(
5213     const Powerpc_relobj<size, big_endian>* object,
5214     unsigned int r_type,
5215     Address from,
5216     Address to,
5217     bool save_res)
5218 {
5219   Branch_stub_key key(object, to);
5220   bool notoc = (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC);
5221   Branch_stub_ent ent(this->branch_size_, notoc, save_res);
5222   std::pair<typename Branch_stub_entries::iterator, bool> p
5223     = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5224   if (notoc && !p.first->second.notoc_)
5225     {
5226       this->need_resize_ = true;
5227       p.first->second.notoc_ = true;
5228     }
5229   gold_assert(save_res == p.first->second.save_res_);
5230   if (p.second || (this->resizing_ && !p.first->second.iter_))
5231     {
5232       if (this->resizing_)
5233 	{
5234 	  p.first->second.iter_ = 1;
5235 	  p.first->second.off_ = this->branch_size_;
5236 	}
5237       if (save_res)
5238 	this->need_save_res_ = true;
5239       else
5240 	{
5241 	  bool need_lt = false;
5242 	  unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5243 	  this->branch_size_ += stub_size;
5244 	  if (size == 64 && need_lt)
5245 	    this->targ_->add_branch_lookup_table(to);
5246 	}
5247     }
5248   return this->can_reach_stub(from, p.first->second.off_, r_type);
5249 }
5250 
5251 // Find long branch stub offset.
5252 
5253 template<int size, bool big_endian>
5254 const typename Stub_table<size, big_endian>::Branch_stub_ent*
5255 Stub_table<size, big_endian>::find_long_branch_entry(
5256     const Powerpc_relobj<size, big_endian>* object,
5257     Address to) const
5258 {
5259   Branch_stub_key key(object, to);
5260   typename Branch_stub_entries::const_iterator p
5261     = this->long_branch_stubs_.find(key);
5262   if (p == this->long_branch_stubs_.end())
5263     return NULL;
5264   return &p->second;
5265 }
5266 
5267 template<bool big_endian>
5268 static void
5269 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5270 {
5271   delta /= 4;
5272   if (delta < 64)
5273     fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5274   else if (delta < 256)
5275     {
5276       fde.push_back(elfcpp::DW_CFA_advance_loc1);
5277       fde.push_back(delta);
5278     }
5279   else if (delta < 65536)
5280     {
5281       fde.resize(fde.size() + 3);
5282       unsigned char *p = &*fde.end() - 3;
5283       *p++ = elfcpp::DW_CFA_advance_loc2;
5284       elfcpp::Swap<16, big_endian>::writeval(p, delta);
5285     }
5286   else
5287     {
5288       fde.resize(fde.size() + 5);
5289       unsigned char *p = &*fde.end() - 5;
5290       *p++ = elfcpp::DW_CFA_advance_loc4;
5291       elfcpp::Swap<32, big_endian>::writeval(p, delta);
5292     }
5293 }
5294 
5295 template<typename T>
5296 static bool
5297 stub_sort(T s1, T s2)
5298 {
5299   return s1->second.off_ < s2->second.off_;
5300 }
5301 
5302 // Add .eh_frame info for this stub section.  Unlike other linker
5303 // generated .eh_frame this is added late in the link, because we
5304 // only want the .eh_frame info if this particular stub section is
5305 // non-empty.
5306 
5307 template<int size, bool big_endian>
5308 void
5309 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5310 {
5311   if (size != 64
5312       || !parameters->options().ld_generated_unwind_info())
5313     return;
5314 
5315   // Since we add stub .eh_frame info late, it must be placed
5316   // after all other linker generated .eh_frame info so that
5317   // merge mapping need not be updated for input sections.
5318   // There is no provision to use a different CIE to that used
5319   // by .glink.
5320   if (!this->targ_->has_glink())
5321     return;
5322 
5323   typedef typename Plt_stub_entries::const_iterator plt_iter;
5324   std::vector<plt_iter> calls;
5325   if (!this->plt_call_stubs_.empty())
5326     for (plt_iter cs = this->plt_call_stubs_.begin();
5327 	 cs != this->plt_call_stubs_.end();
5328 	 ++cs)
5329       if ((this->targ_->is_tls_get_addr_opt(cs->first.sym_)
5330 	   && cs->second.r2save_
5331 	   && !cs->second.localentry0_)
5332 	  || (cs->second.notoc_
5333 	      && !this->targ_->powerxx_stubs()))
5334 	calls.push_back(cs);
5335   if (calls.size() > 1)
5336     std::stable_sort(calls.begin(), calls.end(),
5337 		     stub_sort<plt_iter>);
5338 
5339   typedef typename Branch_stub_entries::const_iterator branch_iter;
5340   std::vector<branch_iter> branches;
5341   if (!this->long_branch_stubs_.empty()
5342       && !this->targ_->powerxx_stubs())
5343     for (branch_iter bs = this->long_branch_stubs_.begin();
5344 	 bs != this->long_branch_stubs_.end();
5345 	 ++bs)
5346       if (bs->second.notoc_)
5347 	branches.push_back(bs);
5348   if (branches.size() > 1)
5349     std::stable_sort(branches.begin(), branches.end(),
5350 		     stub_sort<branch_iter>);
5351 
5352   if (calls.empty() && branches.empty())
5353     return;
5354 
5355   unsigned int last_eh_loc = 0;
5356   // offset pcrel sdata4, size udata4, and augmentation size byte.
5357   std::vector<unsigned char> fde(9, 0);
5358 
5359   for (unsigned int i = 0; i < calls.size(); i++)
5360     {
5361       plt_iter cs = calls[i];
5362       unsigned int off = cs->second.off_;
5363       // The __tls_get_addr_opt call stub needs to describe where
5364       // it saves LR, to support exceptions that might be thrown
5365       // from __tls_get_addr, and to support asynchronous exceptions.
5366       if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5367 	{
5368 	  off += 7 * 4;
5369 	  if (cs->second.r2save_
5370 	      && !cs->second.localentry0_)
5371 	    {
5372 	      off += 2 * 4;
5373 	      eh_advance<big_endian>(fde, off - last_eh_loc);
5374 	      fde.resize(fde.size() + 6);
5375 	      unsigned char* p = &*fde.end() - 6;
5376 	      *p++ = elfcpp::DW_CFA_offset_extended_sf;
5377 	      *p++ = 65;
5378 	      *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5379 	      unsigned int delta = this->plt_call_size(cs) - 4 - 9 * 4;
5380 	      *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5381 	      *p++ = elfcpp::DW_CFA_restore_extended;
5382 	      *p++ = 65;
5383 	      last_eh_loc = off + delta;
5384 	      continue;
5385 	    }
5386 	}
5387       // notoc stubs also should describe LR changes, to support
5388       // asynchronous exceptions.
5389       off += (cs->second.r2save_ ? 4 : 0) + 8;
5390       eh_advance<big_endian>(fde, off - last_eh_loc);
5391       fde.resize(fde.size() + 6);
5392       unsigned char* p = &*fde.end() - 6;
5393       *p++ = elfcpp::DW_CFA_register;
5394       *p++ = 65;
5395       *p++ = 12;
5396       *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5397       *p++ = elfcpp::DW_CFA_restore_extended;
5398       *p++ = 65;
5399       last_eh_loc = off + 8;
5400     }
5401 
5402   for (unsigned int i = 0; i < branches.size(); i++)
5403     {
5404       branch_iter bs = branches[i];
5405       unsigned int off = bs->second.off_ + 8;
5406       eh_advance<big_endian>(fde, off - last_eh_loc);
5407       fde.resize(fde.size() + 6);
5408       unsigned char* p = &*fde.end() - 6;
5409       *p++ = elfcpp::DW_CFA_register;
5410       *p++ = 65;
5411       *p++ = 12;
5412       *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5413       *p++ = elfcpp::DW_CFA_restore_extended;
5414       *p++ = 65;
5415       last_eh_loc = off + 8;
5416     }
5417 
5418   layout->add_eh_frame_for_plt(this,
5419 			       Eh_cie<size>::eh_frame_cie,
5420 			       sizeof (Eh_cie<size>::eh_frame_cie),
5421 			       &*fde.begin(), fde.size());
5422 }
5423 
5424 template<int size, bool big_endian>
5425 void
5426 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5427 {
5428   if (size == 64
5429       && parameters->options().ld_generated_unwind_info()
5430       && this->targ_->has_glink())
5431     layout->remove_eh_frame_for_plt(this,
5432 				    Eh_cie<size>::eh_frame_cie,
5433 				    sizeof (Eh_cie<size>::eh_frame_cie));
5434 }
5435 
5436 // A class to handle .glink.
5437 
5438 template<int size, bool big_endian>
5439 class Output_data_glink : public Output_section_data
5440 {
5441  public:
5442   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5443   static const Address invalid_address = static_cast<Address>(0) - 1;
5444 
5445   Output_data_glink(Target_powerpc<size, big_endian>* targ)
5446     : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5447       end_branch_table_(), ge_size_(0)
5448   { }
5449 
5450   void
5451   add_eh_frame(Layout* layout);
5452 
5453   void
5454   add_global_entry(const Symbol*);
5455 
5456   Address
5457   find_global_entry(const Symbol*) const;
5458 
5459   unsigned int
5460   global_entry_align(unsigned int off) const
5461   {
5462     unsigned int align = param_plt_align<size>();
5463     return (off + align - 1) & -align;
5464   }
5465 
5466   unsigned int
5467   global_entry_off() const
5468   {
5469     return this->global_entry_align(this->end_branch_table_);
5470   }
5471 
5472   Address
5473   global_entry_address() const
5474   {
5475     gold_assert(this->is_data_size_valid());
5476     return this->address() + this->global_entry_off();
5477   }
5478 
5479   int
5480   pltresolve_size() const
5481   {
5482     if (size == 64)
5483       return (8
5484 	      + (this->targ_->abiversion() < 2 ? 11 * 4 : 14 * 4));
5485     return 16 * 4;
5486   }
5487 
5488  protected:
5489   // Write to a map file.
5490   void
5491   do_print_to_mapfile(Mapfile* mapfile) const
5492   { mapfile->print_output_data(this, _("** glink")); }
5493 
5494  private:
5495   void
5496   set_final_data_size();
5497 
5498   // Write out .glink
5499   void
5500   do_write(Output_file*);
5501 
5502   // Allows access to .got and .plt for do_write.
5503   Target_powerpc<size, big_endian>* targ_;
5504 
5505   // Map sym to stub offset.
5506   typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5507   Global_entry_stub_entries global_entry_stubs_;
5508 
5509   unsigned int end_branch_table_, ge_size_;
5510 };
5511 
5512 template<int size, bool big_endian>
5513 void
5514 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5515 {
5516   if (!parameters->options().ld_generated_unwind_info())
5517     return;
5518 
5519   if (size == 64)
5520     {
5521       if (this->targ_->abiversion() < 2)
5522 	layout->add_eh_frame_for_plt(this,
5523 				     Eh_cie<64>::eh_frame_cie,
5524 				     sizeof (Eh_cie<64>::eh_frame_cie),
5525 				     glink_eh_frame_fde_64v1,
5526 				     sizeof (glink_eh_frame_fde_64v1));
5527       else
5528 	layout->add_eh_frame_for_plt(this,
5529 				     Eh_cie<64>::eh_frame_cie,
5530 				     sizeof (Eh_cie<64>::eh_frame_cie),
5531 				     glink_eh_frame_fde_64v2,
5532 				     sizeof (glink_eh_frame_fde_64v2));
5533     }
5534   else
5535     {
5536       // 32-bit .glink can use the default since the CIE return
5537       // address reg, LR, is valid.
5538       layout->add_eh_frame_for_plt(this,
5539 				   Eh_cie<32>::eh_frame_cie,
5540 				   sizeof (Eh_cie<32>::eh_frame_cie),
5541 				   default_fde,
5542 				   sizeof (default_fde));
5543       // Except where LR is used in a PIC __glink_PLTresolve.
5544       if (parameters->options().output_is_position_independent())
5545 	layout->add_eh_frame_for_plt(this,
5546 				     Eh_cie<32>::eh_frame_cie,
5547 				     sizeof (Eh_cie<32>::eh_frame_cie),
5548 				     glink_eh_frame_fde_32,
5549 				     sizeof (glink_eh_frame_fde_32));
5550     }
5551 }
5552 
5553 template<int size, bool big_endian>
5554 void
5555 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5556 {
5557   unsigned int off = this->global_entry_align(this->ge_size_);
5558   std::pair<typename Global_entry_stub_entries::iterator, bool> p
5559     = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5560   if (p.second)
5561     this->ge_size_ = off + 16;
5562 }
5563 
5564 template<int size, bool big_endian>
5565 typename Output_data_glink<size, big_endian>::Address
5566 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5567 {
5568   typename Global_entry_stub_entries::const_iterator p
5569     = this->global_entry_stubs_.find(gsym);
5570   return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5571 }
5572 
5573 template<int size, bool big_endian>
5574 void
5575 Output_data_glink<size, big_endian>::set_final_data_size()
5576 {
5577   unsigned int count = this->targ_->plt_entry_count();
5578   section_size_type total = 0;
5579 
5580   if (count != 0)
5581     {
5582       if (size == 32)
5583 	{
5584 	  // space for branch table
5585 	  total += 4 * (count - 1);
5586 
5587 	  total += -total & 15;
5588 	  total += this->pltresolve_size();
5589 	}
5590       else
5591 	{
5592 	  total += this->pltresolve_size();
5593 
5594 	  // space for branch table
5595 	  total += 4 * count;
5596 	  if (this->targ_->abiversion() < 2)
5597 	    {
5598 	      total += 4 * count;
5599 	      if (count > 0x8000)
5600 		total += 4 * (count - 0x8000);
5601 	    }
5602 	}
5603     }
5604   this->end_branch_table_ = total;
5605   total = this->global_entry_align(total);
5606   total += this->ge_size_;
5607 
5608   this->set_data_size(total);
5609 }
5610 
5611 // Define symbols on stubs, identifying the stub.
5612 
5613 template<int size, bool big_endian>
5614 void
5615 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5616 {
5617   if (!this->plt_call_stubs_.empty())
5618     {
5619       // The key for the plt call stub hash table includes addresses,
5620       // therefore traversal order depends on those addresses, which
5621       // can change between runs if gold is a PIE.  Unfortunately the
5622       // output .symtab ordering depends on the order in which symbols
5623       // are added to the linker symtab.  We want reproducible output
5624       // so must sort the call stub symbols.
5625       typedef typename Plt_stub_entries::const_iterator plt_iter;
5626       std::vector<plt_iter> sorted;
5627       sorted.resize(this->plt_call_stubs_.size());
5628 
5629       for (plt_iter cs = this->plt_call_stubs_.begin();
5630 	   cs != this->plt_call_stubs_.end();
5631 	   ++cs)
5632 	sorted[cs->second.indx_] = cs;
5633 
5634       for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5635 	{
5636 	  plt_iter cs = sorted[i];
5637 	  char add[10];
5638 	  add[0] = 0;
5639 	  if (cs->first.addend_ != 0)
5640 	    sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5641 	  char obj[10];
5642 	  obj[0] = 0;
5643 	  if (cs->first.object_)
5644 	    {
5645 	      const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5646 		<const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5647 	      sprintf(obj, "%x:", ppcobj->uniq());
5648 	    }
5649 	  char localname[9];
5650 	  const char *symname;
5651 	  if (cs->first.sym_ == NULL)
5652 	    {
5653 	      sprintf(localname, "%x", cs->first.locsym_);
5654 	      symname = localname;
5655 	    }
5656 	  else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5657 	    symname = this->targ_->tls_get_addr_opt()->name();
5658 	  else
5659 	    symname = cs->first.sym_->name();
5660 	  char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5661 	  sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5662 	  Address value
5663 	    = this->stub_address() - this->address() + cs->second.off_;
5664 	  unsigned int stub_size = this->plt_call_align(this->plt_call_size(cs));
5665 	  this->targ_->define_local(symtab, name, this, value, stub_size);
5666 	}
5667     }
5668 
5669   typedef typename Branch_stub_entries::const_iterator branch_iter;
5670   for (branch_iter bs = this->long_branch_stubs_.begin();
5671        bs != this->long_branch_stubs_.end();
5672        ++bs)
5673     {
5674       if (bs->second.save_res_)
5675 	continue;
5676 
5677       char* name = new char[8 + 13 + 16 + 1];
5678       sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5679 	      static_cast<unsigned long long>(bs->first.dest_));
5680       Address value = (this->stub_address() - this->address()
5681 		       + this->plt_size_ + bs->second.off_);
5682       bool need_lt = false;
5683       unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5684       this->targ_->define_local(symtab, name, this, value, stub_size);
5685     }
5686 }
5687 
5688 // Emit the start of a __tls_get_addr_opt plt call stub.
5689 
5690 template<int size, bool big_endian>
5691 bool
5692 Stub_table<size, big_endian>::build_tls_opt_head(
5693      unsigned char** pp,
5694      typename Plt_stub_entries::const_iterator cs)
5695 {
5696   if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5697     {
5698       unsigned char* p = *pp;
5699       if (size == 64)
5700 	{
5701 	  write_insn<big_endian>(p, ld_11_3 + 0);
5702 	  p += 4;
5703 	  write_insn<big_endian>(p, ld_12_3 + 8);
5704 	  p += 4;
5705 	  write_insn<big_endian>(p, mr_0_3);
5706 	  p += 4;
5707 	  write_insn<big_endian>(p, cmpdi_11_0);
5708 	  p += 4;
5709 	  write_insn<big_endian>(p, add_3_12_13);
5710 	  p += 4;
5711 	  write_insn<big_endian>(p, beqlr);
5712 	  p += 4;
5713 	  write_insn<big_endian>(p, mr_3_0);
5714 	  p += 4;
5715 	  if (cs->second.r2save_ && !cs->second.localentry0_)
5716 	    {
5717 	      write_insn<big_endian>(p, mflr_11);
5718 	      p += 4;
5719 	      write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5720 	      p += 4;
5721 	    }
5722 	}
5723       else
5724 	{
5725 	  write_insn<big_endian>(p, lwz_11_3 + 0);
5726 	  p += 4;
5727 	  write_insn<big_endian>(p, lwz_12_3 + 4);
5728 	  p += 4;
5729 	  write_insn<big_endian>(p, mr_0_3);
5730 	  p += 4;
5731 	  write_insn<big_endian>(p, cmpwi_11_0);
5732 	  p += 4;
5733 	  write_insn<big_endian>(p, add_3_12_2);
5734 	  p += 4;
5735 	  write_insn<big_endian>(p, beqlr);
5736 	  p += 4;
5737 	  write_insn<big_endian>(p, mr_3_0);
5738 	  p += 4;
5739 	  write_insn<big_endian>(p, nop);
5740 	  p += 4;
5741 	}
5742       *pp = p;
5743       return true;
5744     }
5745   return false;
5746 }
5747 
5748 // Emit the tail of a __tls_get_addr_opt plt call stub.
5749 
5750 template<int size, bool big_endian>
5751 bool
5752 Stub_table<size, big_endian>::build_tls_opt_tail(
5753      unsigned char* p,
5754      typename Plt_stub_entries::const_iterator cs)
5755 {
5756   if (size == 64
5757       && cs->second.r2save_
5758       && !cs->second.localentry0_
5759       && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5760     {
5761       write_insn<big_endian>(p, bctrl);
5762       p += 4;
5763       write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5764       p += 4;
5765       write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5766       p += 4;
5767       write_insn<big_endian>(p, mtlr_11);
5768       p += 4;
5769       write_insn<big_endian>(p, blr);
5770       return true;
5771     }
5772   return false;
5773 }
5774 
5775 // Emit pc-relative plt call stub code.
5776 
5777 template<bool big_endian>
5778 static unsigned char*
5779 build_powerxx_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
5780 {
5781   uint64_t insn;
5782   if (off - odd + (1ULL << 33) < 1ULL << 34)
5783     {
5784       off -= odd;
5785       if (odd)
5786 	{
5787 	  write_insn<big_endian>(p, nop);
5788 	  p += 4;
5789 	}
5790       if (load)
5791 	insn = pld_12_pc;
5792       else
5793 	insn = paddi_12_pc;
5794       insn |= d34(off);
5795       write_insn<big_endian>(p, insn >> 32);
5796       p += 4;
5797       write_insn<big_endian>(p, insn & 0xffffffff);
5798     }
5799   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5800     {
5801       off -= 8 - odd;
5802       write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
5803       p += 4;
5804       if (!odd)
5805 	{
5806 	  write_insn<big_endian>(p, sldi_11_11_34);
5807 	  p += 4;
5808 	}
5809       insn = paddi_12_pc | d34(off);
5810       write_insn<big_endian>(p, insn >> 32);
5811       p += 4;
5812       write_insn<big_endian>(p, insn & 0xffffffff);
5813       p += 4;
5814       if (odd)
5815 	{
5816 	  write_insn<big_endian>(p, sldi_11_11_34);
5817 	  p += 4;
5818 	}
5819       if (load)
5820 	write_insn<big_endian>(p, ldx_12_11_12);
5821       else
5822 	write_insn<big_endian>(p, add_12_11_12);
5823     }
5824   else
5825     {
5826       off -= odd + 8;
5827       write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
5828       p += 4;
5829       write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
5830       p += 4;
5831       if (odd)
5832 	{
5833 	  write_insn<big_endian>(p, sldi_11_11_34);
5834 	  p += 4;
5835 	}
5836       insn = paddi_12_pc | d34(off);
5837       write_insn<big_endian>(p, insn >> 32);
5838       p += 4;
5839       write_insn<big_endian>(p, insn & 0xffffffff);
5840       p += 4;
5841       if (!odd)
5842 	{
5843 	  write_insn<big_endian>(p, sldi_11_11_34);
5844 	  p += 4;
5845 	}
5846       if (load)
5847 	write_insn<big_endian>(p, ldx_12_11_12);
5848       else
5849 	write_insn<big_endian>(p, add_12_11_12);
5850     }
5851   p += 4;
5852   return p;
5853 }
5854 
5855 // Gets the address of a label (1:) in r11 and builds an offset in r12,
5856 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
5857 //	mflr	%r12
5858 //	bcl	20,31,1f
5859 // 1:	mflr	%r11
5860 //	mtlr	%r12
5861 //	lis	%r12,xxx-1b@highest
5862 //	ori	%r12,%r12,xxx-1b@higher
5863 //	sldi	%r12,%r12,32
5864 //	oris	%r12,%r12,xxx-1b@high
5865 //	ori	%r12,%r12,xxx-1b@l
5866 //	add/ldx	%r12,%r11,%r12
5867 
5868 template<bool big_endian>
5869 static unsigned char*
5870 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
5871 {
5872   write_insn<big_endian>(p, mflr_12);
5873   p += 4;
5874   write_insn<big_endian>(p, bcl_20_31);
5875   p += 4;
5876   write_insn<big_endian>(p, mflr_11);
5877   p += 4;
5878   write_insn<big_endian>(p, mtlr_12);
5879   p += 4;
5880   if (off + 0x8000 < 0x10000)
5881     {
5882       if (load)
5883 	write_insn<big_endian>(p, ld_12_11 + l(off));
5884       else
5885 	write_insn<big_endian>(p, addi_12_11 + l(off));
5886     }
5887   else if (off + 0x80008000ULL < 0x100000000ULL)
5888     {
5889       write_insn<big_endian>(p, addis_12_11 + ha(off));
5890       p += 4;
5891       if (load)
5892 	write_insn<big_endian>(p, ld_12_12 + l(off));
5893       else
5894 	write_insn<big_endian>(p, addi_12_12 + l(off));
5895     }
5896   else
5897     {
5898       if (off + 0x800000000000ULL < 0x1000000000000ULL)
5899 	{
5900 	  write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
5901 	  p += 4;
5902 	}
5903       else
5904 	{
5905 	  write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
5906 	  p += 4;
5907 	  if (((off >> 32) & 0xffff) != 0)
5908 	    {
5909 	      write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
5910 	      p += 4;
5911 	    }
5912 	}
5913       if (((off >> 32) & 0xffffffffULL) != 0)
5914 	{
5915 	  write_insn<big_endian>(p, sldi_12_12_32);
5916 	  p += 4;
5917 	}
5918       if (hi(off) != 0)
5919 	{
5920 	  write_insn<big_endian>(p, oris_12_12_0 + hi(off));
5921 	  p += 4;
5922 	}
5923       if (l(off) != 0)
5924 	{
5925 	  write_insn<big_endian>(p, ori_12_12_0 + l(off));
5926 	  p += 4;
5927 	}
5928       if (load)
5929 	write_insn<big_endian>(p, ldx_12_11_12);
5930       else
5931 	write_insn<big_endian>(p, add_12_11_12);
5932     }
5933   p += 4;
5934   return p;
5935 }
5936 
5937 // Size of a given plt call stub.
5938 
5939 template<int size, bool big_endian>
5940 unsigned int
5941 Stub_table<size, big_endian>::plt_call_size(
5942     typename Plt_stub_entries::const_iterator p) const
5943 {
5944   if (size == 32)
5945     {
5946       const Symbol* gsym = p->first.sym_;
5947       return (4 * 4
5948 	      + (this->targ_->is_tls_get_addr_opt(gsym) ? 8 * 4 : 0));
5949     }
5950 
5951   const Output_data_plt_powerpc<size, big_endian>* plt;
5952   uint64_t plt_addr = this->plt_off(p, &plt);
5953   plt_addr += plt->address();
5954   unsigned int bytes = 0;
5955   const Symbol* gsym = p->first.sym_;
5956   if (this->targ_->is_tls_get_addr_opt(gsym))
5957     {
5958       if (p->second.r2save_ && !p->second.localentry0_)
5959 	bytes = 13 * 4;
5960       else
5961 	bytes = 7 * 4;
5962     }
5963 
5964   if (p->second.r2save_)
5965     bytes += 4;
5966 
5967   if (this->targ_->powerxx_stubs())
5968     {
5969       uint64_t from = this->stub_address() + p->second.off_ + bytes;
5970       if (bytes > 8 * 4)
5971 	from -= 4 * 4;
5972       uint64_t odd = from & 4;
5973       uint64_t off = plt_addr - from;
5974       if (off - odd + (1ULL << 33) < 1ULL << 34)
5975 	bytes += odd + 4 * 4;
5976       else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
5977 	bytes += 7 * 4;
5978       else
5979 	bytes += 8 * 4;
5980       return bytes;
5981     }
5982 
5983   if (p->second.notoc_)
5984     {
5985       uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
5986       if (bytes > 32)
5987 	from -= 4 * 4;
5988       uint64_t off = plt_addr - from;
5989       if (off + 0x8000 < 0x10000)
5990 	bytes += 7 * 4;
5991       else if (off + 0x80008000ULL < 0x100000000ULL)
5992 	bytes += 8 * 4;
5993       else
5994 	{
5995 	  bytes += 8 * 4;
5996 	  if (off + 0x800000000000ULL >= 0x1000000000000ULL
5997 	      && ((off >> 32) & 0xffff) != 0)
5998 	    bytes += 4;
5999 	  if (((off >> 32) & 0xffffffffULL) != 0)
6000 	    bytes += 4;
6001 	  if (hi(off) != 0)
6002 	    bytes += 4;
6003 	  if (l(off) != 0)
6004 	    bytes += 4;
6005 	}
6006       return bytes;
6007     }
6008 
6009   uint64_t got_addr = this->targ_->got_section()->output_section()->address();
6010   const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6011     <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
6012   got_addr += ppcobj->toc_base_offset();
6013   uint64_t off = plt_addr - got_addr;
6014   bytes += 3 * 4 + 4 * (ha(off) != 0);
6015   if (this->targ_->abiversion() < 2)
6016     {
6017       bool static_chain = parameters->options().plt_static_chain();
6018       bool thread_safe = this->targ_->plt_thread_safe();
6019       bytes += (4
6020 		+ 4 * static_chain
6021 		+ 8 * thread_safe
6022 		+ 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6023     }
6024   return bytes;
6025 }
6026 
6027 // Return long branch stub size.
6028 
6029 template<int size, bool big_endian>
6030 unsigned int
6031 Stub_table<size, big_endian>::branch_stub_size(
6032      typename Branch_stub_entries::const_iterator p,
6033      bool* need_lt)
6034 {
6035   Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6036   if (size == 32)
6037     {
6038       if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6039 	return 4;
6040       if (parameters->options().output_is_position_independent())
6041 	return 32;
6042       return 16;
6043     }
6044 
6045   uint64_t off = p->first.dest_ - loc;
6046   if (p->second.notoc_)
6047     {
6048       if (this->targ_->powerxx_stubs())
6049 	{
6050 	  Address odd = loc & 4;
6051 	  if (off + (1 << 25) < 2 << 25)
6052 	    return odd + 12;
6053 	  if (off - odd + (1ULL << 33) < 1ULL << 34)
6054 	    return odd + 16;
6055 	  if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6056 	    return 28;
6057 	  return 32;
6058 	}
6059       off -= 8;
6060       if (off + 0x8000 < 0x10000)
6061 	return 24;
6062       if (off + 0x80008000ULL < 0x100000000ULL)
6063 	{
6064 	  if (off + 24 + (1 << 25) < 2 << 25)
6065 	    return 28;
6066 	  return 32;
6067 	}
6068       unsigned int bytes = 32;
6069       if (off + 0x800000000000ULL >= 0x1000000000000ULL
6070 	  && ((off >> 32) & 0xffff) != 0)
6071 	bytes += 4;
6072       if (((off >> 32) & 0xffffffffULL) != 0)
6073 	bytes += 4;
6074       if (hi(off) != 0)
6075 	bytes += 4;
6076       if (l(off) != 0)
6077 	bytes += 4;
6078       return bytes;
6079     }
6080 
6081   if (off + (1 << 25) < 2 << 25)
6082     return 4;
6083   if (!this->targ_->powerxx_stubs())
6084     *need_lt = true;
6085   return 16;
6086 }
6087 
6088 template<int size, bool big_endian>
6089 void
6090 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6091 {
6092   if (p.sym_)
6093     gold_error(_("linkage table error against `%s'"),
6094 	       p.sym_->demangled_name().c_str());
6095   else
6096     gold_error(_("linkage table error against `%s:[local %u]'"),
6097 	       p.object_->name().c_str(),
6098 	       p.locsym_);
6099 }
6100 
6101 // Write out plt and long branch stub code.
6102 
6103 template<int size, bool big_endian>
6104 void
6105 Stub_table<size, big_endian>::do_write(Output_file* of)
6106 {
6107   if (this->plt_call_stubs_.empty()
6108       && this->long_branch_stubs_.empty())
6109     return;
6110 
6111   const section_size_type start_off = this->offset();
6112   const section_size_type off = this->stub_offset();
6113   const section_size_type oview_size =
6114     convert_to_section_size_type(this->data_size() - (off - start_off));
6115   unsigned char* const oview = of->get_output_view(off, oview_size);
6116   unsigned char* p;
6117 
6118   if (size == 64
6119       && this->targ_->powerxx_stubs())
6120     {
6121       if (!this->plt_call_stubs_.empty())
6122 	{
6123 	  // Write out plt call stubs.
6124 	  typename Plt_stub_entries::const_iterator cs;
6125 	  for (cs = this->plt_call_stubs_.begin();
6126 	       cs != this->plt_call_stubs_.end();
6127 	       ++cs)
6128 	    {
6129 	      p = oview + cs->second.off_;
6130 	      this->build_tls_opt_head(&p, cs);
6131 	      if (cs->second.r2save_)
6132 		{
6133 		  write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6134 		  p += 4;
6135 		}
6136 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6137 	      Address pltoff = this->plt_off(cs, &plt);
6138 	      Address plt_addr = pltoff + plt->address();
6139 	      Address from = this->stub_address() + (p - oview);
6140 	      Address delta = plt_addr - from;
6141 	      p = build_powerxx_offset<big_endian>(p, delta, from & 4, true);
6142 	      write_insn<big_endian>(p, mtctr_12);
6143 	      p += 4;
6144 	      if (!this->build_tls_opt_tail(p, cs))
6145 		write_insn<big_endian>(p, bctr);
6146 	    }
6147 	}
6148 
6149       // Write out long branch stubs.
6150       typename Branch_stub_entries::const_iterator bs;
6151       for (bs = this->long_branch_stubs_.begin();
6152 	   bs != this->long_branch_stubs_.end();
6153 	   ++bs)
6154 	{
6155 	  if (bs->second.save_res_)
6156 	    continue;
6157 	  Address off = this->plt_size_ + bs->second.off_;
6158 	  p = oview + off;
6159 	  Address loc = this->stub_address() + off;
6160 	  Address delta = bs->first.dest_ - loc;
6161 	  if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6162 	    {
6163 	      unsigned char* startp = p;
6164 	      p = build_powerxx_offset<big_endian>(p, delta, loc & 4, false);
6165 	      delta -= p - startp;
6166 	    }
6167 	  if (delta + (1 << 25) < 2 << 25)
6168 	    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6169 	  else
6170 	    {
6171 	      write_insn<big_endian>(p, mtctr_12);
6172 	      p += 4;
6173 	      write_insn<big_endian>(p, bctr);
6174 	    }
6175 	}
6176     }
6177   else if (size == 64)
6178     {
6179       const Output_data_got_powerpc<size, big_endian>* got
6180 	= this->targ_->got_section();
6181       Address got_os_addr = got->output_section()->address();
6182 
6183       if (!this->plt_call_stubs_.empty()
6184 	  && this->targ_->abiversion() >= 2)
6185 	{
6186 	  // Write out plt call stubs for ELFv2.
6187 	  typename Plt_stub_entries::const_iterator cs;
6188 	  for (cs = this->plt_call_stubs_.begin();
6189 	       cs != this->plt_call_stubs_.end();
6190 	       ++cs)
6191 	    {
6192 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6193 	      Address pltoff = this->plt_off(cs, &plt);
6194 	      Address plt_addr = pltoff + plt->address();
6195 
6196 	      p = oview + cs->second.off_;
6197 	      this->build_tls_opt_head(&p, cs);
6198 	      if (cs->second.r2save_)
6199 		{
6200 		  write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6201 		  p += 4;
6202 		}
6203 	      if (cs->second.notoc_)
6204 		{
6205 		  Address from = this->stub_address() + (p - oview) + 8;
6206 		  Address off = plt_addr - from;
6207 		  p = build_notoc_offset<big_endian>(p, off, true);
6208 		}
6209 	      else
6210 		{
6211 		  const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6212 		    <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6213 		  Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6214 		  Address off = plt_addr - got_addr;
6215 
6216 		  if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6217 		    this->plt_error(cs->first);
6218 
6219 		  if (ha(off) != 0)
6220 		    {
6221 		      write_insn<big_endian>(p, addis_12_2 + ha(off));
6222 		      p += 4;
6223 		      write_insn<big_endian>(p, ld_12_12 + l(off));
6224 		      p += 4;
6225 		    }
6226 		  else
6227 		    {
6228 		      write_insn<big_endian>(p, ld_12_2 + l(off));
6229 		      p += 4;
6230 		    }
6231 		}
6232 	      write_insn<big_endian>(p, mtctr_12);
6233 	      p += 4;
6234 	      if (!this->build_tls_opt_tail(p, cs))
6235 		write_insn<big_endian>(p, bctr);
6236 	    }
6237 	}
6238       else if (!this->plt_call_stubs_.empty())
6239 	{
6240 	  // Write out plt call stubs for ELFv1.
6241 	  typename Plt_stub_entries::const_iterator cs;
6242 	  for (cs = this->plt_call_stubs_.begin();
6243 	       cs != this->plt_call_stubs_.end();
6244 	       ++cs)
6245 	    {
6246 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6247 	      Address pltoff = this->plt_off(cs, &plt);
6248 	      Address plt_addr = pltoff + plt->address();
6249 	      const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
6250 		<const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
6251 	      Address got_addr = got_os_addr + ppcobj->toc_base_offset();
6252 	      Address off = plt_addr - got_addr;
6253 
6254 	      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6255 		  || cs->second.notoc_)
6256 		this->plt_error(cs->first);
6257 
6258 	      bool static_chain = parameters->options().plt_static_chain();
6259 	      bool thread_safe = this->targ_->plt_thread_safe();
6260 	      bool use_fake_dep = false;
6261 	      Address cmp_branch_off = 0;
6262 	      if (thread_safe)
6263 		{
6264 		  unsigned int pltindex
6265 		    = ((pltoff - this->targ_->first_plt_entry_offset())
6266 		       / this->targ_->plt_entry_size());
6267 		  Address glinkoff
6268 		    = (this->targ_->glink_section()->pltresolve_size()
6269 		       + pltindex * 8);
6270 		  if (pltindex > 32768)
6271 		    glinkoff += (pltindex - 32768) * 4;
6272 		  Address to
6273 		    = this->targ_->glink_section()->address() + glinkoff;
6274 		  Address from
6275 		    = (this->stub_address() + cs->second.off_ + 20
6276 		       + 4 * cs->second.r2save_
6277 		       + 4 * (ha(off) != 0)
6278 		       + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6279 		       + 4 * static_chain);
6280 		  cmp_branch_off = to - from;
6281 		  use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6282 		}
6283 
6284 	      p = oview + cs->second.off_;
6285 	      if (this->build_tls_opt_head(&p, cs))
6286 		use_fake_dep = thread_safe;
6287 	      if (cs->second.r2save_)
6288 		{
6289 		  write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6290 		  p += 4;
6291 		}
6292 	      if (ha(off) != 0)
6293 		{
6294 		  write_insn<big_endian>(p, addis_11_2 + ha(off));
6295 		  p += 4;
6296 		  write_insn<big_endian>(p, ld_12_11 + l(off));
6297 		  p += 4;
6298 		  if (ha(off + 8 + 8 * static_chain) != ha(off))
6299 		    {
6300 		      write_insn<big_endian>(p, addi_11_11 + l(off));
6301 		      p += 4;
6302 		      off = 0;
6303 		    }
6304 		  write_insn<big_endian>(p, mtctr_12);
6305 		  p += 4;
6306 		  if (use_fake_dep)
6307 		    {
6308 		      write_insn<big_endian>(p, xor_2_12_12);
6309 		      p += 4;
6310 		      write_insn<big_endian>(p, add_11_11_2);
6311 		      p += 4;
6312 		    }
6313 		  write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6314 		  p += 4;
6315 		  if (static_chain)
6316 		    {
6317 		      write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6318 		      p += 4;
6319 		    }
6320 		}
6321 	      else
6322 		{
6323 		  write_insn<big_endian>(p, ld_12_2 + l(off));
6324 		  p += 4;
6325 		  if (ha(off + 8 + 8 * static_chain) != ha(off))
6326 		    {
6327 		      write_insn<big_endian>(p, addi_2_2 + l(off));
6328 		      p += 4;
6329 		      off = 0;
6330 		    }
6331 		  write_insn<big_endian>(p, mtctr_12);
6332 		  p += 4;
6333 		  if (use_fake_dep)
6334 		    {
6335 		      write_insn<big_endian>(p, xor_11_12_12);
6336 		      p += 4;
6337 		      write_insn<big_endian>(p, add_2_2_11);
6338 		      p += 4;
6339 		    }
6340 		  if (static_chain)
6341 		    {
6342 		      write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6343 		      p += 4;
6344 		    }
6345 		  write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6346 		  p += 4;
6347 		}
6348 	      if (this->build_tls_opt_tail(p, cs))
6349 		;
6350 	      else if (thread_safe && !use_fake_dep)
6351 		{
6352 		  write_insn<big_endian>(p, cmpldi_2_0);
6353 		  p += 4;
6354 		  write_insn<big_endian>(p, bnectr_p4);
6355 		  p += 4;
6356 		  write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6357 		}
6358 	      else
6359 		write_insn<big_endian>(p, bctr);
6360 	    }
6361 	}
6362 
6363       // Write out long branch stubs.
6364       typename Branch_stub_entries::const_iterator bs;
6365       for (bs = this->long_branch_stubs_.begin();
6366 	   bs != this->long_branch_stubs_.end();
6367 	   ++bs)
6368 	{
6369 	  if (bs->second.save_res_)
6370 	    continue;
6371 	  Address off = this->plt_size_ + bs->second.off_;
6372 	  p = oview + off;
6373 	  Address loc = this->stub_address() + off;
6374 	  Address delta = bs->first.dest_ - loc;
6375 	  if (bs->second.notoc_)
6376 	    {
6377 	      unsigned char* startp = p;
6378 	      p = build_notoc_offset<big_endian>(p, off, false);
6379 	      delta -= p - startp;
6380 	    }
6381 	  else if (delta + (1 << 25) >= 2 << 25)
6382 	    {
6383 	      Address brlt_addr
6384 		= this->targ_->find_branch_lookup_table(bs->first.dest_);
6385 	      gold_assert(brlt_addr != invalid_address);
6386 	      brlt_addr += this->targ_->brlt_section()->address();
6387 	      Address got_addr = got_os_addr + bs->first.toc_base_off_;
6388 	      Address brltoff = brlt_addr - got_addr;
6389 	      if (ha(brltoff) == 0)
6390 		{
6391 		  write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6392 		  p += 4;
6393 		}
6394 	      else
6395 		{
6396 		  write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6397 		  p += 4;
6398 		  write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6399 		  p += 4;
6400 		}
6401 	    }
6402 	  if (delta + (1 << 25) < 2 << 25)
6403 	    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6404 	  else
6405 	    {
6406 	      write_insn<big_endian>(p, mtctr_12);
6407 	      p += 4;
6408 	      write_insn<big_endian>(p, bctr);
6409 	    }
6410 	}
6411     }
6412   else // size == 32
6413     {
6414       if (!this->plt_call_stubs_.empty())
6415 	{
6416 	  // The address of _GLOBAL_OFFSET_TABLE_.
6417 	  Address g_o_t = invalid_address;
6418 
6419 	  // Write out plt call stubs.
6420 	  typename Plt_stub_entries::const_iterator cs;
6421 	  for (cs = this->plt_call_stubs_.begin();
6422 	       cs != this->plt_call_stubs_.end();
6423 	       ++cs)
6424 	    {
6425 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6426 	      Address plt_addr = this->plt_off(cs, &plt);
6427 	      plt_addr += plt->address();
6428 
6429 	      p = oview + cs->second.off_;
6430 	      this->build_tls_opt_head(&p, cs);
6431 	      if (parameters->options().output_is_position_independent())
6432 		{
6433 		  Address got_addr;
6434 		  const Powerpc_relobj<size, big_endian>* ppcobj
6435 		    = (static_cast<const Powerpc_relobj<size, big_endian>*>
6436 		       (cs->first.object_));
6437 		  if (ppcobj != NULL && cs->first.addend_ >= 32768)
6438 		    {
6439 		      unsigned int got2 = ppcobj->got2_shndx();
6440 		      got_addr = ppcobj->get_output_section_offset(got2);
6441 		      gold_assert(got_addr != invalid_address);
6442 		      got_addr += (ppcobj->output_section(got2)->address()
6443 				   + cs->first.addend_);
6444 		    }
6445 		  else
6446 		    {
6447 		      if (g_o_t == invalid_address)
6448 			{
6449 			  const Output_data_got_powerpc<size, big_endian>* got
6450 			    = this->targ_->got_section();
6451 			  g_o_t = got->address() + got->g_o_t();
6452 			}
6453 		      got_addr = g_o_t;
6454 		    }
6455 
6456 		  Address off = plt_addr - got_addr;
6457 		  if (ha(off) == 0)
6458 		    write_insn<big_endian>(p, lwz_11_30 + l(off));
6459 		  else
6460 		    {
6461 		      write_insn<big_endian>(p, addis_11_30 + ha(off));
6462 		      p += 4;
6463 		      write_insn<big_endian>(p, lwz_11_11 + l(off));
6464 		    }
6465 		}
6466 	      else
6467 		{
6468 		  write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6469 		  p += 4;
6470 		  write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6471 		}
6472 	      p += 4;
6473 	      write_insn<big_endian>(p, mtctr_11);
6474 	      p += 4;
6475 	      write_insn<big_endian>(p, bctr);
6476 	    }
6477 	}
6478 
6479       // Write out long branch stubs.
6480       typename Branch_stub_entries::const_iterator bs;
6481       for (bs = this->long_branch_stubs_.begin();
6482 	   bs != this->long_branch_stubs_.end();
6483 	   ++bs)
6484 	{
6485 	  if (bs->second.save_res_)
6486 	    continue;
6487 	  Address off = this->plt_size_ + bs->second.off_;
6488 	  p = oview + off;
6489 	  Address loc = this->stub_address() + off;
6490 	  Address delta = bs->first.dest_ - loc;
6491 	  if (delta + (1 << 25) < 2 << 25)
6492 	    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6493 	  else if (!parameters->options().output_is_position_independent())
6494 	    {
6495 	      write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6496 	      p += 4;
6497 	      write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6498 	    }
6499 	  else
6500 	    {
6501 	      delta -= 8;
6502 	      write_insn<big_endian>(p, mflr_0);
6503 	      p += 4;
6504 	      write_insn<big_endian>(p, bcl_20_31);
6505 	      p += 4;
6506 	      write_insn<big_endian>(p, mflr_12);
6507 	      p += 4;
6508 	      write_insn<big_endian>(p, addis_12_12 + ha(delta));
6509 	      p += 4;
6510 	      write_insn<big_endian>(p, addi_12_12 + l(delta));
6511 	      p += 4;
6512 	      write_insn<big_endian>(p, mtlr_0);
6513 	    }
6514 	  p += 4;
6515 	  write_insn<big_endian>(p, mtctr_12);
6516 	  p += 4;
6517 	  write_insn<big_endian>(p, bctr);
6518 	}
6519     }
6520   if (this->need_save_res_)
6521     {
6522       p = oview + this->plt_size_ + this->branch_size_;
6523       memcpy (p, this->targ_->savres_section()->contents(),
6524 	      this->targ_->savres_section()->data_size());
6525     }
6526 }
6527 
6528 // Write out .glink.
6529 
6530 template<int size, bool big_endian>
6531 void
6532 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6533 {
6534   const section_size_type off = this->offset();
6535   const section_size_type oview_size =
6536     convert_to_section_size_type(this->data_size());
6537   unsigned char* const oview = of->get_output_view(off, oview_size);
6538   unsigned char* p;
6539 
6540   // The base address of the .plt section.
6541   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
6542   Address plt_base = this->targ_->plt_section()->address();
6543 
6544   if (size == 64)
6545     {
6546       if (this->end_branch_table_ != 0)
6547 	{
6548 	  // Write pltresolve stub.
6549 	  p = oview;
6550 	  Address after_bcl = this->address() + 16;
6551 	  Address pltoff = plt_base - after_bcl;
6552 
6553 	  elfcpp::Swap<64, big_endian>::writeval(p, pltoff),	p += 8;
6554 
6555 	  if (this->targ_->abiversion() < 2)
6556 	    {
6557 	      write_insn<big_endian>(p, mflr_12),		p += 4;
6558 	      write_insn<big_endian>(p, bcl_20_31),		p += 4;
6559 	      write_insn<big_endian>(p, mflr_11),		p += 4;
6560 	      write_insn<big_endian>(p, ld_2_11 + l(-16)),	p += 4;
6561 	      write_insn<big_endian>(p, mtlr_12),		p += 4;
6562 	      write_insn<big_endian>(p, add_11_2_11),		p += 4;
6563 	      write_insn<big_endian>(p, ld_12_11 + 0),		p += 4;
6564 	      write_insn<big_endian>(p, ld_2_11 + 8),		p += 4;
6565 	      write_insn<big_endian>(p, mtctr_12),		p += 4;
6566 	      write_insn<big_endian>(p, ld_11_11 + 16),		p += 4;
6567 	    }
6568 	  else
6569 	    {
6570 	      write_insn<big_endian>(p, mflr_0),		p += 4;
6571 	      write_insn<big_endian>(p, bcl_20_31),		p += 4;
6572 	      write_insn<big_endian>(p, mflr_11),		p += 4;
6573 	      write_insn<big_endian>(p, std_2_1 + 24),		p += 4;
6574 	      write_insn<big_endian>(p, ld_2_11 + l(-16)),	p += 4;
6575 	      write_insn<big_endian>(p, mtlr_0),		p += 4;
6576 	      write_insn<big_endian>(p, sub_12_12_11),		p += 4;
6577 	      write_insn<big_endian>(p, add_11_2_11),		p += 4;
6578 	      write_insn<big_endian>(p, addi_0_12 + l(-48)),	p += 4;
6579 	      write_insn<big_endian>(p, ld_12_11 + 0),		p += 4;
6580 	      write_insn<big_endian>(p, srdi_0_0_2),		p += 4;
6581 	      write_insn<big_endian>(p, mtctr_12),		p += 4;
6582 	      write_insn<big_endian>(p, ld_11_11 + 8),		p += 4;
6583 	    }
6584 	  write_insn<big_endian>(p, bctr),			p += 4;
6585 	  gold_assert(p == oview + this->pltresolve_size());
6586 
6587 	  // Write lazy link call stubs.
6588 	  uint32_t indx = 0;
6589 	  while (p < oview + this->end_branch_table_)
6590 	    {
6591 	      if (this->targ_->abiversion() < 2)
6592 		{
6593 		  if (indx < 0x8000)
6594 		    {
6595 		      write_insn<big_endian>(p, li_0_0 + indx),		p += 4;
6596 		    }
6597 		  else
6598 		    {
6599 		      write_insn<big_endian>(p, lis_0 + hi(indx)),	p += 4;
6600 		      write_insn<big_endian>(p, ori_0_0_0 + l(indx)),	p += 4;
6601 		    }
6602 		}
6603 	      uint32_t branch_off = 8 - (p - oview);
6604 	      write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),	p += 4;
6605 	      indx++;
6606 	    }
6607 	}
6608 
6609       Address plt_base = this->targ_->plt_section()->address();
6610       Address iplt_base = invalid_address;
6611       unsigned int global_entry_off = this->global_entry_off();
6612       Address global_entry_base = this->address() + global_entry_off;
6613       typename Global_entry_stub_entries::const_iterator ge;
6614       for (ge = this->global_entry_stubs_.begin();
6615 	   ge != this->global_entry_stubs_.end();
6616 	   ++ge)
6617 	{
6618 	  p = oview + global_entry_off + ge->second;
6619 	  Address plt_addr = ge->first->plt_offset();
6620 	  if (ge->first->type() == elfcpp::STT_GNU_IFUNC
6621 	      && ge->first->can_use_relative_reloc(false))
6622 	    {
6623 	      if (iplt_base == invalid_address)
6624 		iplt_base = this->targ_->iplt_section()->address();
6625 	      plt_addr += iplt_base;
6626 	    }
6627 	  else
6628 	    plt_addr += plt_base;
6629 	  Address my_addr = global_entry_base + ge->second;
6630 	  Address off = plt_addr - my_addr;
6631 
6632 	  if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
6633 	    gold_error(_("linkage table error against `%s'"),
6634 		       ge->first->demangled_name().c_str());
6635 
6636 	  write_insn<big_endian>(p, addis_12_12 + ha(off)),	p += 4;
6637 	  write_insn<big_endian>(p, ld_12_12 + l(off)),		p += 4;
6638 	  write_insn<big_endian>(p, mtctr_12),			p += 4;
6639 	  write_insn<big_endian>(p, bctr);
6640 	}
6641     }
6642   else
6643     {
6644       const Output_data_got_powerpc<size, big_endian>* got
6645 	= this->targ_->got_section();
6646       // The address of _GLOBAL_OFFSET_TABLE_.
6647       Address g_o_t = got->address() + got->g_o_t();
6648 
6649       // Write out pltresolve branch table.
6650       p = oview;
6651       unsigned int the_end = oview_size - this->pltresolve_size();
6652       unsigned char* end_p = oview + the_end;
6653       while (p < end_p - 8 * 4)
6654 	write_insn<big_endian>(p, b + end_p - p), p += 4;
6655       while (p < end_p)
6656 	write_insn<big_endian>(p, nop), p += 4;
6657 
6658       // Write out pltresolve call stub.
6659       end_p = oview + oview_size;
6660       if (parameters->options().output_is_position_independent())
6661 	{
6662 	  Address res0_off = 0;
6663 	  Address after_bcl_off = the_end + 12;
6664 	  Address bcl_res0 = after_bcl_off - res0_off;
6665 
6666 	  write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
6667 	  p += 4;
6668 	  write_insn<big_endian>(p, mflr_0);
6669 	  p += 4;
6670 	  write_insn<big_endian>(p, bcl_20_31);
6671 	  p += 4;
6672 	  write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
6673 	  p += 4;
6674 	  write_insn<big_endian>(p, mflr_12);
6675 	  p += 4;
6676 	  write_insn<big_endian>(p, mtlr_0);
6677 	  p += 4;
6678 	  write_insn<big_endian>(p, sub_11_11_12);
6679 	  p += 4;
6680 
6681 	  Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
6682 
6683 	  write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
6684 	  p += 4;
6685 	  if (ha(got_bcl) == ha(got_bcl + 4))
6686 	    {
6687 	      write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
6688 	      p += 4;
6689 	      write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
6690 	    }
6691 	  else
6692 	    {
6693 	      write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
6694 	      p += 4;
6695 	      write_insn<big_endian>(p, lwz_12_12 + 4);
6696 	    }
6697 	  p += 4;
6698 	  write_insn<big_endian>(p, mtctr_0);
6699 	  p += 4;
6700 	  write_insn<big_endian>(p, add_0_11_11);
6701 	  p += 4;
6702 	  write_insn<big_endian>(p, add_11_0_11);
6703 	}
6704       else
6705 	{
6706 	  Address res0 = this->address();
6707 
6708 	  write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
6709 	  p += 4;
6710 	  write_insn<big_endian>(p, addis_11_11 + ha(-res0));
6711 	  p += 4;
6712 	  if (ha(g_o_t + 4) == ha(g_o_t + 8))
6713 	    write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
6714 	  else
6715 	    write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
6716 	  p += 4;
6717 	  write_insn<big_endian>(p, addi_11_11 + l(-res0));
6718 	  p += 4;
6719 	  write_insn<big_endian>(p, mtctr_0);
6720 	  p += 4;
6721 	  write_insn<big_endian>(p, add_0_11_11);
6722 	  p += 4;
6723 	  if (ha(g_o_t + 4) == ha(g_o_t + 8))
6724 	    write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
6725 	  else
6726 	    write_insn<big_endian>(p, lwz_12_12 + 4);
6727 	  p += 4;
6728 	  write_insn<big_endian>(p, add_11_0_11);
6729 	}
6730       p += 4;
6731       write_insn<big_endian>(p, bctr);
6732       p += 4;
6733       while (p < end_p)
6734 	{
6735 	  write_insn<big_endian>(p, nop);
6736 	  p += 4;
6737 	}
6738     }
6739 
6740   of->write_output_view(off, oview_size, oview);
6741 }
6742 
6743 
6744 // A class to handle linker generated save/restore functions.
6745 
6746 template<int size, bool big_endian>
6747 class Output_data_save_res : public Output_section_data_build
6748 {
6749  public:
6750   Output_data_save_res(Symbol_table* symtab);
6751 
6752   const unsigned char*
6753   contents() const
6754   {
6755     return contents_;
6756   }
6757 
6758  protected:
6759   // Write to a map file.
6760   void
6761   do_print_to_mapfile(Mapfile* mapfile) const
6762   { mapfile->print_output_data(this, _("** save/restore")); }
6763 
6764   void
6765   do_write(Output_file*);
6766 
6767  private:
6768   // The maximum size of save/restore contents.
6769   static const unsigned int savres_max = 218*4;
6770 
6771   void
6772   savres_define(Symbol_table* symtab,
6773 		const char *name,
6774 		unsigned int lo, unsigned int hi,
6775 		unsigned char* write_ent(unsigned char*, int),
6776 		unsigned char* write_tail(unsigned char*, int));
6777 
6778   unsigned char *contents_;
6779 };
6780 
6781 template<bool big_endian>
6782 static unsigned char*
6783 savegpr0(unsigned char* p, int r)
6784 {
6785   uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6786   write_insn<big_endian>(p, insn);
6787   return p + 4;
6788 }
6789 
6790 template<bool big_endian>
6791 static unsigned char*
6792 savegpr0_tail(unsigned char* p, int r)
6793 {
6794   p = savegpr0<big_endian>(p, r);
6795   uint32_t insn = std_0_1 + 16;
6796   write_insn<big_endian>(p, insn);
6797   p = p + 4;
6798   write_insn<big_endian>(p, blr);
6799   return p + 4;
6800 }
6801 
6802 template<bool big_endian>
6803 static unsigned char*
6804 restgpr0(unsigned char* p, int r)
6805 {
6806   uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6807   write_insn<big_endian>(p, insn);
6808   return p + 4;
6809 }
6810 
6811 template<bool big_endian>
6812 static unsigned char*
6813 restgpr0_tail(unsigned char* p, int r)
6814 {
6815   uint32_t insn = ld_0_1 + 16;
6816   write_insn<big_endian>(p, insn);
6817   p = p + 4;
6818   p = restgpr0<big_endian>(p, r);
6819   write_insn<big_endian>(p, mtlr_0);
6820   p = p + 4;
6821   if (r == 29)
6822     {
6823       p = restgpr0<big_endian>(p, 30);
6824       p = restgpr0<big_endian>(p, 31);
6825     }
6826   write_insn<big_endian>(p, blr);
6827   return p + 4;
6828 }
6829 
6830 template<bool big_endian>
6831 static unsigned char*
6832 savegpr1(unsigned char* p, int r)
6833 {
6834   uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6835   write_insn<big_endian>(p, insn);
6836   return p + 4;
6837 }
6838 
6839 template<bool big_endian>
6840 static unsigned char*
6841 savegpr1_tail(unsigned char* p, int r)
6842 {
6843   p = savegpr1<big_endian>(p, r);
6844   write_insn<big_endian>(p, blr);
6845   return p + 4;
6846 }
6847 
6848 template<bool big_endian>
6849 static unsigned char*
6850 restgpr1(unsigned char* p, int r)
6851 {
6852   uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
6853   write_insn<big_endian>(p, insn);
6854   return p + 4;
6855 }
6856 
6857 template<bool big_endian>
6858 static unsigned char*
6859 restgpr1_tail(unsigned char* p, int r)
6860 {
6861   p = restgpr1<big_endian>(p, r);
6862   write_insn<big_endian>(p, blr);
6863   return p + 4;
6864 }
6865 
6866 template<bool big_endian>
6867 static unsigned char*
6868 savefpr(unsigned char* p, int r)
6869 {
6870   uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6871   write_insn<big_endian>(p, insn);
6872   return p + 4;
6873 }
6874 
6875 template<bool big_endian>
6876 static unsigned char*
6877 savefpr0_tail(unsigned char* p, int r)
6878 {
6879   p = savefpr<big_endian>(p, r);
6880   write_insn<big_endian>(p, std_0_1 + 16);
6881   p = p + 4;
6882   write_insn<big_endian>(p, blr);
6883   return p + 4;
6884 }
6885 
6886 template<bool big_endian>
6887 static unsigned char*
6888 restfpr(unsigned char* p, int r)
6889 {
6890   uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
6891   write_insn<big_endian>(p, insn);
6892   return p + 4;
6893 }
6894 
6895 template<bool big_endian>
6896 static unsigned char*
6897 restfpr0_tail(unsigned char* p, int r)
6898 {
6899   write_insn<big_endian>(p, ld_0_1 + 16);
6900   p = p + 4;
6901   p = restfpr<big_endian>(p, r);
6902   write_insn<big_endian>(p, mtlr_0);
6903   p = p + 4;
6904   if (r == 29)
6905     {
6906       p = restfpr<big_endian>(p, 30);
6907       p = restfpr<big_endian>(p, 31);
6908     }
6909   write_insn<big_endian>(p, blr);
6910   return p + 4;
6911 }
6912 
6913 template<bool big_endian>
6914 static unsigned char*
6915 savefpr1_tail(unsigned char* p, int r)
6916 {
6917   p = savefpr<big_endian>(p, r);
6918   write_insn<big_endian>(p, blr);
6919   return p + 4;
6920 }
6921 
6922 template<bool big_endian>
6923 static unsigned char*
6924 restfpr1_tail(unsigned char* p, int r)
6925 {
6926   p = restfpr<big_endian>(p, r);
6927   write_insn<big_endian>(p, blr);
6928   return p + 4;
6929 }
6930 
6931 template<bool big_endian>
6932 static unsigned char*
6933 savevr(unsigned char* p, int r)
6934 {
6935   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6936   write_insn<big_endian>(p, insn);
6937   p = p + 4;
6938   insn = stvx_0_12_0 + (r << 21);
6939   write_insn<big_endian>(p, insn);
6940   return p + 4;
6941 }
6942 
6943 template<bool big_endian>
6944 static unsigned char*
6945 savevr_tail(unsigned char* p, int r)
6946 {
6947   p = savevr<big_endian>(p, r);
6948   write_insn<big_endian>(p, blr);
6949   return p + 4;
6950 }
6951 
6952 template<bool big_endian>
6953 static unsigned char*
6954 restvr(unsigned char* p, int r)
6955 {
6956   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
6957   write_insn<big_endian>(p, insn);
6958   p = p + 4;
6959   insn = lvx_0_12_0 + (r << 21);
6960   write_insn<big_endian>(p, insn);
6961   return p + 4;
6962 }
6963 
6964 template<bool big_endian>
6965 static unsigned char*
6966 restvr_tail(unsigned char* p, int r)
6967 {
6968   p = restvr<big_endian>(p, r);
6969   write_insn<big_endian>(p, blr);
6970   return p + 4;
6971 }
6972 
6973 
6974 template<int size, bool big_endian>
6975 Output_data_save_res<size, big_endian>::Output_data_save_res(
6976     Symbol_table* symtab)
6977   : Output_section_data_build(4),
6978     contents_(NULL)
6979 {
6980   this->savres_define(symtab,
6981 		      "_savegpr0_", 14, 31,
6982 		      savegpr0<big_endian>, savegpr0_tail<big_endian>);
6983   this->savres_define(symtab,
6984 		      "_restgpr0_", 14, 29,
6985 		      restgpr0<big_endian>, restgpr0_tail<big_endian>);
6986   this->savres_define(symtab,
6987 		      "_restgpr0_", 30, 31,
6988 		      restgpr0<big_endian>, restgpr0_tail<big_endian>);
6989   this->savres_define(symtab,
6990 		      "_savegpr1_", 14, 31,
6991 		      savegpr1<big_endian>, savegpr1_tail<big_endian>);
6992   this->savres_define(symtab,
6993 		      "_restgpr1_", 14, 31,
6994 		      restgpr1<big_endian>, restgpr1_tail<big_endian>);
6995   this->savres_define(symtab,
6996 		      "_savefpr_", 14, 31,
6997 		      savefpr<big_endian>, savefpr0_tail<big_endian>);
6998   this->savres_define(symtab,
6999 		      "_restfpr_", 14, 29,
7000 		      restfpr<big_endian>, restfpr0_tail<big_endian>);
7001   this->savres_define(symtab,
7002 		      "_restfpr_", 30, 31,
7003 		      restfpr<big_endian>, restfpr0_tail<big_endian>);
7004   this->savres_define(symtab,
7005 		      "._savef", 14, 31,
7006 		      savefpr<big_endian>, savefpr1_tail<big_endian>);
7007   this->savres_define(symtab,
7008 		      "._restf", 14, 31,
7009 		      restfpr<big_endian>, restfpr1_tail<big_endian>);
7010   this->savres_define(symtab,
7011 		      "_savevr_", 20, 31,
7012 		      savevr<big_endian>, savevr_tail<big_endian>);
7013   this->savres_define(symtab,
7014 		      "_restvr_", 20, 31,
7015 		      restvr<big_endian>, restvr_tail<big_endian>);
7016 }
7017 
7018 template<int size, bool big_endian>
7019 void
7020 Output_data_save_res<size, big_endian>::savres_define(
7021     Symbol_table* symtab,
7022     const char *name,
7023     unsigned int lo, unsigned int hi,
7024     unsigned char* write_ent(unsigned char*, int),
7025     unsigned char* write_tail(unsigned char*, int))
7026 {
7027   size_t len = strlen(name);
7028   bool writing = false;
7029   char sym[16];
7030 
7031   memcpy(sym, name, len);
7032   sym[len + 2] = 0;
7033 
7034   for (unsigned int i = lo; i <= hi; i++)
7035     {
7036       sym[len + 0] = i / 10 + '0';
7037       sym[len + 1] = i % 10 + '0';
7038       Symbol* gsym = symtab->lookup(sym);
7039       bool refd = gsym != NULL && gsym->is_undefined();
7040       writing = writing || refd;
7041       if (writing)
7042 	{
7043 	  if (this->contents_ == NULL)
7044 	    this->contents_ = new unsigned char[this->savres_max];
7045 
7046 	  section_size_type value = this->current_data_size();
7047 	  unsigned char* p = this->contents_ + value;
7048 	  if (i != hi)
7049 	    p = write_ent(p, i);
7050 	  else
7051 	    p = write_tail(p, i);
7052 	  section_size_type cur_size = p - this->contents_;
7053 	  this->set_current_data_size(cur_size);
7054 	  if (refd)
7055 	    symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7056 					  this, value, cur_size - value,
7057 					  elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7058 					  elfcpp::STV_HIDDEN, 0, false, false);
7059 	}
7060     }
7061 }
7062 
7063 // Write out save/restore.
7064 
7065 template<int size, bool big_endian>
7066 void
7067 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7068 {
7069   const section_size_type off = this->offset();
7070   const section_size_type oview_size =
7071     convert_to_section_size_type(this->data_size());
7072   unsigned char* const oview = of->get_output_view(off, oview_size);
7073   memcpy(oview, this->contents_, oview_size);
7074   of->write_output_view(off, oview_size, oview);
7075 }
7076 
7077 
7078 // Create the glink section.
7079 
7080 template<int size, bool big_endian>
7081 void
7082 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7083 {
7084   if (this->glink_ == NULL)
7085     {
7086       this->glink_ = new Output_data_glink<size, big_endian>(this);
7087       this->glink_->add_eh_frame(layout);
7088       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7089 				      elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7090 				      this->glink_, ORDER_TEXT, false);
7091     }
7092 }
7093 
7094 // Create a PLT entry for a global symbol.
7095 
7096 template<int size, bool big_endian>
7097 void
7098 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7099 						 Layout* layout,
7100 						 Symbol* gsym)
7101 {
7102   if (gsym->type() == elfcpp::STT_GNU_IFUNC
7103       && gsym->can_use_relative_reloc(false))
7104     {
7105       if (this->iplt_ == NULL)
7106 	this->make_iplt_section(symtab, layout);
7107       this->iplt_->add_ifunc_entry(gsym);
7108     }
7109   else
7110     {
7111       if (this->plt_ == NULL)
7112 	this->make_plt_section(symtab, layout);
7113       this->plt_->add_entry(gsym);
7114     }
7115 }
7116 
7117 // Make a PLT entry for a local symbol.
7118 
7119 template<int size, bool big_endian>
7120 void
7121 Target_powerpc<size, big_endian>::make_local_plt_entry(
7122     Layout* layout,
7123     Sized_relobj_file<size, big_endian>* relobj,
7124     unsigned int r_sym)
7125 {
7126   if (this->lplt_ == NULL)
7127     this->make_lplt_section(layout);
7128   this->lplt_->add_local_entry(relobj, r_sym);
7129 }
7130 
7131 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7132 
7133 template<int size, bool big_endian>
7134 void
7135 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7136     Symbol_table* symtab,
7137     Layout* layout,
7138     Sized_relobj_file<size, big_endian>* relobj,
7139     unsigned int r_sym)
7140 {
7141   if (this->iplt_ == NULL)
7142     this->make_iplt_section(symtab, layout);
7143   this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7144 }
7145 
7146 // Return the number of entries in the PLT.
7147 
7148 template<int size, bool big_endian>
7149 unsigned int
7150 Target_powerpc<size, big_endian>::plt_entry_count() const
7151 {
7152   if (this->plt_ == NULL)
7153     return 0;
7154   return this->plt_->entry_count();
7155 }
7156 
7157 // Create a GOT entry for local dynamic __tls_get_addr calls.
7158 
7159 template<int size, bool big_endian>
7160 unsigned int
7161 Target_powerpc<size, big_endian>::tlsld_got_offset(
7162     Symbol_table* symtab,
7163     Layout* layout,
7164     Sized_relobj_file<size, big_endian>* object)
7165 {
7166   if (this->tlsld_got_offset_ == -1U)
7167     {
7168       gold_assert(symtab != NULL && layout != NULL && object != NULL);
7169       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7170       Output_data_got_powerpc<size, big_endian>* got
7171 	= this->got_section(symtab, layout);
7172       unsigned int got_offset = got->add_constant_pair(0, 0);
7173       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7174 			  got_offset, 0);
7175       this->tlsld_got_offset_ = got_offset;
7176     }
7177   return this->tlsld_got_offset_;
7178 }
7179 
7180 // Get the Reference_flags for a particular relocation.
7181 
7182 template<int size, bool big_endian>
7183 int
7184 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7185     unsigned int r_type,
7186     const Target_powerpc* target)
7187 {
7188   int ref = 0;
7189 
7190   switch (r_type)
7191     {
7192     case elfcpp::R_POWERPC_NONE:
7193     case elfcpp::R_POWERPC_GNU_VTINHERIT:
7194     case elfcpp::R_POWERPC_GNU_VTENTRY:
7195     case elfcpp::R_PPC64_TOC:
7196       // No symbol reference.
7197       break;
7198 
7199     case elfcpp::R_PPC64_ADDR64:
7200     case elfcpp::R_PPC64_UADDR64:
7201     case elfcpp::R_POWERPC_ADDR32:
7202     case elfcpp::R_POWERPC_UADDR32:
7203     case elfcpp::R_POWERPC_ADDR16:
7204     case elfcpp::R_POWERPC_UADDR16:
7205     case elfcpp::R_POWERPC_ADDR16_LO:
7206     case elfcpp::R_POWERPC_ADDR16_HI:
7207     case elfcpp::R_POWERPC_ADDR16_HA:
7208     case elfcpp::R_PPC64_ADDR16_HIGHER34:
7209     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7210     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7211     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7212     case elfcpp::R_PPC64_D34:
7213     case elfcpp::R_PPC64_D34_LO:
7214     case elfcpp::R_PPC64_D34_HI30:
7215     case elfcpp::R_PPC64_D34_HA30:
7216     case elfcpp::R_PPC64_D28:
7217       ref = Symbol::ABSOLUTE_REF;
7218       break;
7219 
7220     case elfcpp::R_POWERPC_ADDR24:
7221     case elfcpp::R_POWERPC_ADDR14:
7222     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7223     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7224       ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7225       break;
7226 
7227     case elfcpp::R_PPC64_REL64:
7228     case elfcpp::R_POWERPC_REL32:
7229     case elfcpp::R_PPC_LOCAL24PC:
7230     case elfcpp::R_POWERPC_REL16:
7231     case elfcpp::R_POWERPC_REL16_LO:
7232     case elfcpp::R_POWERPC_REL16_HI:
7233     case elfcpp::R_POWERPC_REL16_HA:
7234     case elfcpp::R_PPC64_REL16_HIGH:
7235     case elfcpp::R_PPC64_REL16_HIGHA:
7236     case elfcpp::R_PPC64_REL16_HIGHER:
7237     case elfcpp::R_PPC64_REL16_HIGHERA:
7238     case elfcpp::R_PPC64_REL16_HIGHEST:
7239     case elfcpp::R_PPC64_REL16_HIGHESTA:
7240     case elfcpp::R_PPC64_PCREL34:
7241     case elfcpp::R_PPC64_REL16_HIGHER34:
7242     case elfcpp::R_PPC64_REL16_HIGHERA34:
7243     case elfcpp::R_PPC64_REL16_HIGHEST34:
7244     case elfcpp::R_PPC64_REL16_HIGHESTA34:
7245     case elfcpp::R_PPC64_PCREL28:
7246       ref = Symbol::RELATIVE_REF;
7247       break;
7248 
7249     case elfcpp::R_PPC64_REL24_NOTOC:
7250       if (size == 32)
7251 	break;
7252       // Fall through.
7253     case elfcpp::R_POWERPC_REL24:
7254     case elfcpp::R_PPC_PLTREL24:
7255     case elfcpp::R_POWERPC_REL14:
7256     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7257     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7258       ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7259       break;
7260 
7261     case elfcpp::R_POWERPC_GOT16:
7262     case elfcpp::R_POWERPC_GOT16_LO:
7263     case elfcpp::R_POWERPC_GOT16_HI:
7264     case elfcpp::R_POWERPC_GOT16_HA:
7265     case elfcpp::R_PPC64_GOT16_DS:
7266     case elfcpp::R_PPC64_GOT16_LO_DS:
7267     case elfcpp::R_PPC64_GOT_PCREL34:
7268     case elfcpp::R_PPC64_TOC16:
7269     case elfcpp::R_PPC64_TOC16_LO:
7270     case elfcpp::R_PPC64_TOC16_HI:
7271     case elfcpp::R_PPC64_TOC16_HA:
7272     case elfcpp::R_PPC64_TOC16_DS:
7273     case elfcpp::R_PPC64_TOC16_LO_DS:
7274     case elfcpp::R_POWERPC_PLT16_LO:
7275     case elfcpp::R_POWERPC_PLT16_HI:
7276     case elfcpp::R_POWERPC_PLT16_HA:
7277     case elfcpp::R_PPC64_PLT16_LO_DS:
7278     case elfcpp::R_PPC64_PLT_PCREL34:
7279     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7280       ref = Symbol::RELATIVE_REF;
7281       break;
7282 
7283     case elfcpp::R_POWERPC_GOT_TPREL16:
7284     case elfcpp::R_POWERPC_TLS:
7285     case elfcpp::R_PPC64_TLSGD:
7286     case elfcpp::R_PPC64_TLSLD:
7287     case elfcpp::R_PPC64_TPREL34:
7288     case elfcpp::R_PPC64_DTPREL34:
7289     case elfcpp::R_PPC64_GOT_TLSGD34:
7290     case elfcpp::R_PPC64_GOT_TLSLD34:
7291     case elfcpp::R_PPC64_GOT_TPREL34:
7292     case elfcpp::R_PPC64_GOT_DTPREL34:
7293       ref = Symbol::TLS_REF;
7294       break;
7295 
7296     case elfcpp::R_POWERPC_COPY:
7297     case elfcpp::R_POWERPC_GLOB_DAT:
7298     case elfcpp::R_POWERPC_JMP_SLOT:
7299     case elfcpp::R_POWERPC_RELATIVE:
7300     case elfcpp::R_POWERPC_DTPMOD:
7301     default:
7302       // Not expected.  We will give an error later.
7303       break;
7304     }
7305 
7306   if (size == 64 && target->abiversion() < 2)
7307     ref |= Symbol::FUNC_DESC_ABI;
7308   return ref;
7309 }
7310 
7311 // Report an unsupported relocation against a local symbol.
7312 
7313 template<int size, bool big_endian>
7314 void
7315 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7316     Sized_relobj_file<size, big_endian>* object,
7317     unsigned int r_type)
7318 {
7319   gold_error(_("%s: unsupported reloc %u against local symbol"),
7320 	     object->name().c_str(), r_type);
7321 }
7322 
7323 // We are about to emit a dynamic relocation of type R_TYPE.  If the
7324 // dynamic linker does not support it, issue an error.
7325 
7326 template<int size, bool big_endian>
7327 void
7328 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7329 						      unsigned int r_type)
7330 {
7331   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7332 
7333   // These are the relocation types supported by glibc for both 32-bit
7334   // and 64-bit powerpc.
7335   switch (r_type)
7336     {
7337     case elfcpp::R_POWERPC_NONE:
7338     case elfcpp::R_POWERPC_RELATIVE:
7339     case elfcpp::R_POWERPC_GLOB_DAT:
7340     case elfcpp::R_POWERPC_DTPMOD:
7341     case elfcpp::R_POWERPC_DTPREL:
7342     case elfcpp::R_POWERPC_TPREL:
7343     case elfcpp::R_POWERPC_JMP_SLOT:
7344     case elfcpp::R_POWERPC_COPY:
7345     case elfcpp::R_POWERPC_IRELATIVE:
7346     case elfcpp::R_POWERPC_ADDR32:
7347     case elfcpp::R_POWERPC_UADDR32:
7348     case elfcpp::R_POWERPC_ADDR24:
7349     case elfcpp::R_POWERPC_ADDR16:
7350     case elfcpp::R_POWERPC_UADDR16:
7351     case elfcpp::R_POWERPC_ADDR16_LO:
7352     case elfcpp::R_POWERPC_ADDR16_HI:
7353     case elfcpp::R_POWERPC_ADDR16_HA:
7354     case elfcpp::R_POWERPC_ADDR14:
7355     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7356     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7357     case elfcpp::R_POWERPC_REL32:
7358     case elfcpp::R_POWERPC_TPREL16:
7359     case elfcpp::R_POWERPC_TPREL16_LO:
7360     case elfcpp::R_POWERPC_TPREL16_HI:
7361     case elfcpp::R_POWERPC_TPREL16_HA:
7362       return;
7363 
7364     default:
7365       break;
7366     }
7367 
7368   if (size == 64)
7369     {
7370       switch (r_type)
7371 	{
7372 	  // These are the relocation types supported only on 64-bit.
7373 	case elfcpp::R_PPC64_ADDR64:
7374 	case elfcpp::R_PPC64_UADDR64:
7375 	case elfcpp::R_PPC64_JMP_IREL:
7376 	case elfcpp::R_PPC64_ADDR16_DS:
7377 	case elfcpp::R_PPC64_ADDR16_LO_DS:
7378 	case elfcpp::R_PPC64_ADDR16_HIGH:
7379 	case elfcpp::R_PPC64_ADDR16_HIGHA:
7380 	case elfcpp::R_PPC64_ADDR16_HIGHER:
7381 	case elfcpp::R_PPC64_ADDR16_HIGHEST:
7382 	case elfcpp::R_PPC64_ADDR16_HIGHERA:
7383 	case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7384 	case elfcpp::R_PPC64_REL64:
7385 	case elfcpp::R_POWERPC_ADDR30:
7386 	case elfcpp::R_PPC64_TPREL16_DS:
7387 	case elfcpp::R_PPC64_TPREL16_LO_DS:
7388 	case elfcpp::R_PPC64_TPREL16_HIGH:
7389 	case elfcpp::R_PPC64_TPREL16_HIGHA:
7390 	case elfcpp::R_PPC64_TPREL16_HIGHER:
7391 	case elfcpp::R_PPC64_TPREL16_HIGHEST:
7392 	case elfcpp::R_PPC64_TPREL16_HIGHERA:
7393 	case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7394 	  return;
7395 
7396 	default:
7397 	  break;
7398 	}
7399     }
7400   else
7401     {
7402       switch (r_type)
7403 	{
7404 	  // These are the relocation types supported only on 32-bit.
7405 	  // ??? glibc ld.so doesn't need to support these.
7406 	case elfcpp::R_POWERPC_REL24:
7407 	case elfcpp::R_POWERPC_DTPREL16:
7408 	case elfcpp::R_POWERPC_DTPREL16_LO:
7409 	case elfcpp::R_POWERPC_DTPREL16_HI:
7410 	case elfcpp::R_POWERPC_DTPREL16_HA:
7411 	  return;
7412 
7413 	default:
7414 	  break;
7415 	}
7416     }
7417 
7418   // This prevents us from issuing more than one error per reloc
7419   // section.  But we can still wind up issuing more than one
7420   // error per object file.
7421   if (this->issued_non_pic_error_)
7422     return;
7423   gold_assert(parameters->options().output_is_position_independent());
7424   object->error(_("requires unsupported dynamic reloc; "
7425 		  "recompile with -fPIC"));
7426   this->issued_non_pic_error_ = true;
7427   return;
7428 }
7429 
7430 // Return whether we need to make a PLT entry for a relocation of the
7431 // given type against a STT_GNU_IFUNC symbol.
7432 
7433 template<int size, bool big_endian>
7434 bool
7435 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7436      Target_powerpc<size, big_endian>* target,
7437      Sized_relobj_file<size, big_endian>* object,
7438      unsigned int r_type,
7439      bool report_err)
7440 {
7441   // In non-pic code any reference will resolve to the plt call stub
7442   // for the ifunc symbol.
7443   if ((size == 32 || target->abiversion() >= 2)
7444       && !parameters->options().output_is_position_independent())
7445     return true;
7446 
7447   switch (r_type)
7448     {
7449     // Word size refs from data sections are OK, but don't need a PLT entry.
7450     case elfcpp::R_POWERPC_ADDR32:
7451     case elfcpp::R_POWERPC_UADDR32:
7452       if (size == 32)
7453 	return false;
7454       break;
7455 
7456     case elfcpp::R_PPC64_ADDR64:
7457     case elfcpp::R_PPC64_UADDR64:
7458       if (size == 64)
7459 	return false;
7460       break;
7461 
7462     // GOT refs are good, but also don't need a PLT entry.
7463     case elfcpp::R_POWERPC_GOT16:
7464     case elfcpp::R_POWERPC_GOT16_LO:
7465     case elfcpp::R_POWERPC_GOT16_HI:
7466     case elfcpp::R_POWERPC_GOT16_HA:
7467     case elfcpp::R_PPC64_GOT16_DS:
7468     case elfcpp::R_PPC64_GOT16_LO_DS:
7469     case elfcpp::R_PPC64_GOT_PCREL34:
7470       return false;
7471 
7472     // PLT relocs are OK and need a PLT entry.
7473     case elfcpp::R_POWERPC_PLT16_LO:
7474     case elfcpp::R_POWERPC_PLT16_HI:
7475     case elfcpp::R_POWERPC_PLT16_HA:
7476     case elfcpp::R_PPC64_PLT16_LO_DS:
7477     case elfcpp::R_POWERPC_PLTSEQ:
7478     case elfcpp::R_POWERPC_PLTCALL:
7479     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7480     case elfcpp::R_PPC64_PLTCALL_NOTOC:
7481     case elfcpp::R_PPC64_PLT_PCREL34:
7482     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7483       return true;
7484       break;
7485 
7486     // Function calls are good, and these do need a PLT entry.
7487     case elfcpp::R_PPC64_REL24_NOTOC:
7488       if (size == 32)
7489 	break;
7490       // Fall through.
7491     case elfcpp::R_POWERPC_ADDR24:
7492     case elfcpp::R_POWERPC_ADDR14:
7493     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7494     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7495     case elfcpp::R_POWERPC_REL24:
7496     case elfcpp::R_PPC_PLTREL24:
7497     case elfcpp::R_POWERPC_REL14:
7498     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7499     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7500       return true;
7501 
7502     default:
7503       break;
7504     }
7505 
7506   // Anything else is a problem.
7507   // If we are building a static executable, the libc startup function
7508   // responsible for applying indirect function relocations is going
7509   // to complain about the reloc type.
7510   // If we are building a dynamic executable, we will have a text
7511   // relocation.  The dynamic loader will set the text segment
7512   // writable and non-executable to apply text relocations.  So we'll
7513   // segfault when trying to run the indirection function to resolve
7514   // the reloc.
7515   if (report_err)
7516     gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
7517 	       object->name().c_str(), r_type);
7518   return false;
7519 }
7520 
7521 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
7522 // reloc.
7523 
7524 static bool
7525 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
7526 {
7527   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
7528 	  || (insn & (0x3f << 26)) == 14u << 26 /* addi */
7529 	  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
7530 	  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
7531 	  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
7532 	  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
7533 	  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
7534 	  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
7535 	  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
7536 	  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
7537 	  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
7538 	  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
7539 	  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
7540 	  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
7541 	  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
7542 	  || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
7543 	  || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
7544 	      /* Exclude lfqu by testing reloc.  If relocs are ever
7545 		 defined for the reduced D field in psq_lu then those
7546 		 will need testing too.  */
7547 	      && r_type != elfcpp::R_PPC64_TOC16_LO
7548 	      && r_type != elfcpp::R_POWERPC_GOT16_LO)
7549 	  || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
7550 	      && (insn & 1) == 0)
7551 	  || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
7552 	  || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
7553 	      /* Exclude stfqu.  psq_stu as above for psq_lu.  */
7554 	      && r_type != elfcpp::R_PPC64_TOC16_LO
7555 	      && r_type != elfcpp::R_POWERPC_GOT16_LO)
7556 	  || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
7557 	      && (insn & 1) == 0));
7558 }
7559 
7560 // Scan a relocation for a local symbol.
7561 
7562 template<int size, bool big_endian>
7563 inline void
7564 Target_powerpc<size, big_endian>::Scan::local(
7565     Symbol_table* symtab,
7566     Layout* layout,
7567     Target_powerpc<size, big_endian>* target,
7568     Sized_relobj_file<size, big_endian>* object,
7569     unsigned int data_shndx,
7570     Output_section* output_section,
7571     const elfcpp::Rela<size, big_endian>& reloc,
7572     unsigned int r_type,
7573     const elfcpp::Sym<size, big_endian>& lsym,
7574     bool is_discarded)
7575 {
7576   this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
7577 
7578   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7579       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7580     {
7581       this->expect_tls_get_addr_call();
7582       const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7583       if (tls_type != tls::TLSOPT_NONE)
7584 	this->skip_next_tls_get_addr_call();
7585     }
7586   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7587 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7588     {
7589       this->expect_tls_get_addr_call();
7590       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7591       if (tls_type != tls::TLSOPT_NONE)
7592 	this->skip_next_tls_get_addr_call();
7593     }
7594 
7595   Powerpc_relobj<size, big_endian>* ppc_object
7596     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
7597 
7598   if (is_discarded)
7599     {
7600       if (size == 64
7601 	  && data_shndx == ppc_object->opd_shndx()
7602 	  && r_type == elfcpp::R_PPC64_ADDR64)
7603 	ppc_object->set_opd_discard(reloc.get_r_offset());
7604       return;
7605     }
7606 
7607   // A local STT_GNU_IFUNC symbol may require a PLT entry.
7608   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
7609   if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
7610     {
7611       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7612       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7613 			  r_type, r_sym, reloc.get_r_addend());
7614       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
7615     }
7616 
7617   switch (r_type)
7618     {
7619     case elfcpp::R_POWERPC_NONE:
7620     case elfcpp::R_POWERPC_GNU_VTINHERIT:
7621     case elfcpp::R_POWERPC_GNU_VTENTRY:
7622     case elfcpp::R_POWERPC_TLS:
7623     case elfcpp::R_PPC64_ENTRY:
7624     case elfcpp::R_POWERPC_PLTSEQ:
7625     case elfcpp::R_POWERPC_PLTCALL:
7626     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7627     case elfcpp::R_PPC64_PLTCALL_NOTOC:
7628     case elfcpp::R_PPC64_PCREL_OPT:
7629     case elfcpp::R_PPC64_ADDR16_HIGHER34:
7630     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7631     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7632     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7633     case elfcpp::R_PPC64_REL16_HIGHER34:
7634     case elfcpp::R_PPC64_REL16_HIGHERA34:
7635     case elfcpp::R_PPC64_REL16_HIGHEST34:
7636     case elfcpp::R_PPC64_REL16_HIGHESTA34:
7637     case elfcpp::R_PPC64_D34:
7638     case elfcpp::R_PPC64_D34_LO:
7639     case elfcpp::R_PPC64_D34_HI30:
7640     case elfcpp::R_PPC64_D34_HA30:
7641     case elfcpp::R_PPC64_D28:
7642     case elfcpp::R_PPC64_PCREL34:
7643     case elfcpp::R_PPC64_PCREL28:
7644     case elfcpp::R_PPC64_TPREL34:
7645     case elfcpp::R_PPC64_DTPREL34:
7646       break;
7647 
7648     case elfcpp::R_PPC64_TOC:
7649       {
7650 	Output_data_got_powerpc<size, big_endian>* got
7651 	  = target->got_section(symtab, layout);
7652 	if (parameters->options().output_is_position_independent())
7653 	  {
7654 	    Address off = reloc.get_r_offset();
7655 	    if (size == 64
7656 		&& target->abiversion() < 2
7657 		&& data_shndx == ppc_object->opd_shndx()
7658 		&& ppc_object->get_opd_discard(off - 8))
7659 	      break;
7660 
7661 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7662 	    Powerpc_relobj<size, big_endian>* symobj = ppc_object;
7663 	    rela_dyn->add_output_section_relative(got->output_section(),
7664 						  elfcpp::R_POWERPC_RELATIVE,
7665 						  output_section,
7666 						  object, data_shndx, off,
7667 						  symobj->toc_base_offset());
7668 	  }
7669       }
7670       break;
7671 
7672     case elfcpp::R_PPC64_ADDR64:
7673     case elfcpp::R_PPC64_UADDR64:
7674     case elfcpp::R_POWERPC_ADDR32:
7675     case elfcpp::R_POWERPC_UADDR32:
7676     case elfcpp::R_POWERPC_ADDR24:
7677     case elfcpp::R_POWERPC_ADDR16:
7678     case elfcpp::R_POWERPC_ADDR16_LO:
7679     case elfcpp::R_POWERPC_ADDR16_HI:
7680     case elfcpp::R_POWERPC_ADDR16_HA:
7681     case elfcpp::R_POWERPC_UADDR16:
7682     case elfcpp::R_PPC64_ADDR16_HIGH:
7683     case elfcpp::R_PPC64_ADDR16_HIGHA:
7684     case elfcpp::R_PPC64_ADDR16_HIGHER:
7685     case elfcpp::R_PPC64_ADDR16_HIGHERA:
7686     case elfcpp::R_PPC64_ADDR16_HIGHEST:
7687     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7688     case elfcpp::R_PPC64_ADDR16_DS:
7689     case elfcpp::R_PPC64_ADDR16_LO_DS:
7690     case elfcpp::R_POWERPC_ADDR14:
7691     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7692     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7693       // If building a shared library (or a position-independent
7694       // executable), we need to create a dynamic relocation for
7695       // this location.
7696       if (parameters->options().output_is_position_independent()
7697 	  || (size == 64 && is_ifunc && target->abiversion() < 2))
7698 	{
7699 	  Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7700 							     is_ifunc);
7701 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7702 	  if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
7703 	      || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
7704 	    {
7705 	      unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7706 				     : elfcpp::R_POWERPC_RELATIVE);
7707 	      rela_dyn->add_local_relative(object, r_sym, dynrel,
7708 					   output_section, data_shndx,
7709 					   reloc.get_r_offset(),
7710 					   reloc.get_r_addend(), false);
7711 	    }
7712 	  else if (lsym.get_st_type() != elfcpp::STT_SECTION)
7713 	    {
7714 	      check_non_pic(object, r_type);
7715 	      rela_dyn->add_local(object, r_sym, r_type, output_section,
7716 				  data_shndx, reloc.get_r_offset(),
7717 				  reloc.get_r_addend());
7718 	    }
7719 	  else
7720 	    {
7721 	      gold_assert(lsym.get_st_value() == 0);
7722 	      unsigned int shndx = lsym.get_st_shndx();
7723 	      bool is_ordinary;
7724 	      shndx = object->adjust_sym_shndx(r_sym, shndx,
7725 					       &is_ordinary);
7726 	      if (!is_ordinary)
7727 		object->error(_("section symbol %u has bad shndx %u"),
7728 			      r_sym, shndx);
7729 	      else
7730 		rela_dyn->add_local_section(object, shndx, r_type,
7731 					    output_section, data_shndx,
7732 					    reloc.get_r_offset());
7733 	    }
7734 	}
7735       break;
7736 
7737     case elfcpp::R_PPC64_PLT_PCREL34:
7738     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7739     case elfcpp::R_POWERPC_PLT16_LO:
7740     case elfcpp::R_POWERPC_PLT16_HI:
7741     case elfcpp::R_POWERPC_PLT16_HA:
7742     case elfcpp::R_PPC64_PLT16_LO_DS:
7743       if (!is_ifunc)
7744 	{
7745 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7746 	  target->make_local_plt_entry(layout, object, r_sym);
7747 	}
7748       break;
7749 
7750     case elfcpp::R_PPC64_REL24_NOTOC:
7751       if (size == 32)
7752 	break;
7753       // Fall through.
7754     case elfcpp::R_POWERPC_REL24:
7755     case elfcpp::R_PPC_PLTREL24:
7756     case elfcpp::R_PPC_LOCAL24PC:
7757     case elfcpp::R_POWERPC_REL14:
7758     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7759     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7760       if (!is_ifunc)
7761 	{
7762 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7763 	  target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
7764 			      r_type, r_sym, reloc.get_r_addend());
7765 	}
7766       break;
7767 
7768     case elfcpp::R_PPC64_TOCSAVE:
7769       // R_PPC64_TOCSAVE follows a call instruction to indicate the
7770       // caller has already saved r2 and thus a plt call stub need not
7771       // save r2.
7772       if (size == 64
7773 	  && target->mark_pltcall(ppc_object, data_shndx,
7774 				  reloc.get_r_offset() - 4, symtab))
7775 	{
7776 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7777 	  unsigned int shndx = lsym.get_st_shndx();
7778 	  bool is_ordinary;
7779 	  shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
7780 	  if (!is_ordinary)
7781 	    object->error(_("tocsave symbol %u has bad shndx %u"),
7782 			  r_sym, shndx);
7783 	  else
7784 	    target->add_tocsave(ppc_object, shndx,
7785 				lsym.get_st_value() + reloc.get_r_addend());
7786 	}
7787       break;
7788 
7789     case elfcpp::R_PPC64_REL64:
7790     case elfcpp::R_POWERPC_REL32:
7791     case elfcpp::R_POWERPC_REL16:
7792     case elfcpp::R_POWERPC_REL16_LO:
7793     case elfcpp::R_POWERPC_REL16_HI:
7794     case elfcpp::R_POWERPC_REL16_HA:
7795     case elfcpp::R_POWERPC_REL16DX_HA:
7796     case elfcpp::R_PPC64_REL16_HIGH:
7797     case elfcpp::R_PPC64_REL16_HIGHA:
7798     case elfcpp::R_PPC64_REL16_HIGHER:
7799     case elfcpp::R_PPC64_REL16_HIGHERA:
7800     case elfcpp::R_PPC64_REL16_HIGHEST:
7801     case elfcpp::R_PPC64_REL16_HIGHESTA:
7802     case elfcpp::R_POWERPC_SECTOFF:
7803     case elfcpp::R_POWERPC_SECTOFF_LO:
7804     case elfcpp::R_POWERPC_SECTOFF_HI:
7805     case elfcpp::R_POWERPC_SECTOFF_HA:
7806     case elfcpp::R_PPC64_SECTOFF_DS:
7807     case elfcpp::R_PPC64_SECTOFF_LO_DS:
7808     case elfcpp::R_POWERPC_TPREL16:
7809     case elfcpp::R_POWERPC_TPREL16_LO:
7810     case elfcpp::R_POWERPC_TPREL16_HI:
7811     case elfcpp::R_POWERPC_TPREL16_HA:
7812     case elfcpp::R_PPC64_TPREL16_DS:
7813     case elfcpp::R_PPC64_TPREL16_LO_DS:
7814     case elfcpp::R_PPC64_TPREL16_HIGH:
7815     case elfcpp::R_PPC64_TPREL16_HIGHA:
7816     case elfcpp::R_PPC64_TPREL16_HIGHER:
7817     case elfcpp::R_PPC64_TPREL16_HIGHERA:
7818     case elfcpp::R_PPC64_TPREL16_HIGHEST:
7819     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7820     case elfcpp::R_POWERPC_DTPREL16:
7821     case elfcpp::R_POWERPC_DTPREL16_LO:
7822     case elfcpp::R_POWERPC_DTPREL16_HI:
7823     case elfcpp::R_POWERPC_DTPREL16_HA:
7824     case elfcpp::R_PPC64_DTPREL16_DS:
7825     case elfcpp::R_PPC64_DTPREL16_LO_DS:
7826     case elfcpp::R_PPC64_DTPREL16_HIGH:
7827     case elfcpp::R_PPC64_DTPREL16_HIGHA:
7828     case elfcpp::R_PPC64_DTPREL16_HIGHER:
7829     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7830     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7831     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7832     case elfcpp::R_PPC64_TLSGD:
7833     case elfcpp::R_PPC64_TLSLD:
7834     case elfcpp::R_PPC64_ADDR64_LOCAL:
7835       break;
7836 
7837     case elfcpp::R_PPC64_GOT_PCREL34:
7838     case elfcpp::R_POWERPC_GOT16:
7839     case elfcpp::R_POWERPC_GOT16_LO:
7840     case elfcpp::R_POWERPC_GOT16_HI:
7841     case elfcpp::R_POWERPC_GOT16_HA:
7842     case elfcpp::R_PPC64_GOT16_DS:
7843     case elfcpp::R_PPC64_GOT16_LO_DS:
7844       {
7845 	// The symbol requires a GOT entry.
7846 	Output_data_got_powerpc<size, big_endian>* got
7847 	  = target->got_section(symtab, layout);
7848 	unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7849 
7850 	if (!parameters->options().output_is_position_independent())
7851 	  {
7852 	    if (is_ifunc
7853 		&& (size == 32 || target->abiversion() >= 2))
7854 	      got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
7855 	    else
7856 	      got->add_local(object, r_sym, GOT_TYPE_STANDARD);
7857 	  }
7858 	else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
7859 	  {
7860 	    // If we are generating a shared object or a pie, this
7861 	    // symbol's GOT entry will be set by a dynamic relocation.
7862 	    unsigned int off;
7863 	    off = got->add_constant(0);
7864 	    object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
7865 
7866 	    Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
7867 							       is_ifunc);
7868 	    unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
7869 				   : elfcpp::R_POWERPC_RELATIVE);
7870 	    rela_dyn->add_local_relative(object, r_sym, dynrel,
7871 					 got, off, 0, false);
7872 	  }
7873       }
7874       break;
7875 
7876     case elfcpp::R_PPC64_TOC16:
7877     case elfcpp::R_PPC64_TOC16_LO:
7878     case elfcpp::R_PPC64_TOC16_HI:
7879     case elfcpp::R_PPC64_TOC16_HA:
7880     case elfcpp::R_PPC64_TOC16_DS:
7881     case elfcpp::R_PPC64_TOC16_LO_DS:
7882       // We need a GOT section.
7883       target->got_section(symtab, layout);
7884       break;
7885 
7886     case elfcpp::R_PPC64_GOT_TLSGD34:
7887     case elfcpp::R_POWERPC_GOT_TLSGD16:
7888     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7889     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7890     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7891       {
7892 	const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
7893 	if (tls_type == tls::TLSOPT_NONE)
7894 	  {
7895 	    Output_data_got_powerpc<size, big_endian>* got
7896 	      = target->got_section(symtab, layout);
7897 	    unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7898 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7899 	    got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
7900 				    rela_dyn, elfcpp::R_POWERPC_DTPMOD);
7901 	  }
7902 	else if (tls_type == tls::TLSOPT_TO_LE)
7903 	  {
7904 	    // no GOT relocs needed for Local Exec.
7905 	  }
7906 	else
7907 	  gold_unreachable();
7908       }
7909       break;
7910 
7911     case elfcpp::R_PPC64_GOT_TLSLD34:
7912     case elfcpp::R_POWERPC_GOT_TLSLD16:
7913     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7914     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7915     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7916       {
7917 	const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7918 	if (tls_type == tls::TLSOPT_NONE)
7919 	  target->tlsld_got_offset(symtab, layout, object);
7920 	else if (tls_type == tls::TLSOPT_TO_LE)
7921 	  {
7922 	    // no GOT relocs needed for Local Exec.
7923 	    if (parameters->options().emit_relocs())
7924 	      {
7925 		Output_section* os = layout->tls_segment()->first_section();
7926 		gold_assert(os != NULL);
7927 		os->set_needs_symtab_index();
7928 	      }
7929 	  }
7930 	else
7931 	  gold_unreachable();
7932       }
7933       break;
7934 
7935     case elfcpp::R_PPC64_GOT_DTPREL34:
7936     case elfcpp::R_POWERPC_GOT_DTPREL16:
7937     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7938     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7939     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7940       {
7941 	Output_data_got_powerpc<size, big_endian>* got
7942 	  = target->got_section(symtab, layout);
7943 	unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7944 	got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
7945       }
7946       break;
7947 
7948     case elfcpp::R_PPC64_GOT_TPREL34:
7949     case elfcpp::R_POWERPC_GOT_TPREL16:
7950     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7951     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7952     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7953       {
7954 	const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
7955 	if (tls_type == tls::TLSOPT_NONE)
7956 	  {
7957 	    unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
7958 	    if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
7959 	      {
7960 		Output_data_got_powerpc<size, big_endian>* got
7961 		  = target->got_section(symtab, layout);
7962 		unsigned int off = got->add_constant(0);
7963 		object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
7964 
7965 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7966 		rela_dyn->add_symbolless_local_addend(object, r_sym,
7967 						      elfcpp::R_POWERPC_TPREL,
7968 						      got, off, 0);
7969 	      }
7970 	  }
7971 	else if (tls_type == tls::TLSOPT_TO_LE)
7972 	  {
7973 	    // no GOT relocs needed for Local Exec.
7974 	  }
7975 	else
7976 	  gold_unreachable();
7977       }
7978       break;
7979 
7980     default:
7981       unsupported_reloc_local(object, r_type);
7982       break;
7983     }
7984 
7985   if (size == 64
7986       && parameters->options().toc_optimize())
7987     {
7988       if (data_shndx == ppc_object->toc_shndx())
7989 	{
7990 	  bool ok = true;
7991 	  if (r_type != elfcpp::R_PPC64_ADDR64
7992 	      || (is_ifunc && target->abiversion() < 2))
7993 	    ok = false;
7994 	  else if (parameters->options().output_is_position_independent())
7995 	    {
7996 	      if (is_ifunc)
7997 		ok = false;
7998 	      else
7999 		{
8000 		  unsigned int shndx = lsym.get_st_shndx();
8001 		  if (shndx >= elfcpp::SHN_LORESERVE
8002 		      && shndx != elfcpp::SHN_XINDEX)
8003 		    ok = false;
8004 		}
8005 	    }
8006 	  if (!ok)
8007 	    ppc_object->set_no_toc_opt(reloc.get_r_offset());
8008 	}
8009 
8010       enum {no_check, check_lo, check_ha} insn_check;
8011       switch (r_type)
8012 	{
8013 	default:
8014 	  insn_check = no_check;
8015 	  break;
8016 
8017 	case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8018 	case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8019 	case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8020 	case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8021 	case elfcpp::R_POWERPC_GOT16_HA:
8022 	case elfcpp::R_PPC64_TOC16_HA:
8023 	  insn_check = check_ha;
8024 	  break;
8025 
8026 	case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8027 	case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8028 	case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8029 	case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8030 	case elfcpp::R_POWERPC_GOT16_LO:
8031 	case elfcpp::R_PPC64_GOT16_LO_DS:
8032 	case elfcpp::R_PPC64_TOC16_LO:
8033 	case elfcpp::R_PPC64_TOC16_LO_DS:
8034 	  insn_check = check_lo;
8035 	  break;
8036 	}
8037 
8038       section_size_type slen;
8039       const unsigned char* view = NULL;
8040       if (insn_check != no_check)
8041 	{
8042 	  view = ppc_object->section_contents(data_shndx, &slen, false);
8043 	  section_size_type off =
8044 	    convert_to_section_size_type(reloc.get_r_offset()) & -4;
8045 	  if (off < slen)
8046 	    {
8047 	      uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8048 	      if (insn_check == check_lo
8049 		  ? !ok_lo_toc_insn(insn, r_type)
8050 		  : ((insn & ((0x3f << 26) | 0x1f << 16))
8051 		     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8052 		{
8053 		  ppc_object->set_no_toc_opt();
8054 		  gold_warning(_("%s: toc optimization is not supported "
8055 				 "for %#08x instruction"),
8056 			       ppc_object->name().c_str(), insn);
8057 		}
8058 	    }
8059 	}
8060 
8061       switch (r_type)
8062 	{
8063 	default:
8064 	  break;
8065 	case elfcpp::R_PPC64_TOC16:
8066 	case elfcpp::R_PPC64_TOC16_LO:
8067 	case elfcpp::R_PPC64_TOC16_HI:
8068 	case elfcpp::R_PPC64_TOC16_HA:
8069 	case elfcpp::R_PPC64_TOC16_DS:
8070 	case elfcpp::R_PPC64_TOC16_LO_DS:
8071 	  unsigned int shndx = lsym.get_st_shndx();
8072 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8073 	  bool is_ordinary;
8074 	  shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8075 	  if (is_ordinary && shndx == ppc_object->toc_shndx())
8076 	    {
8077 	      Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8078 	      if (dst_off < ppc_object->section_size(shndx))
8079 		{
8080 		  bool ok = false;
8081 		  if (r_type == elfcpp::R_PPC64_TOC16_HA)
8082 		    ok = true;
8083 		  else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8084 		    {
8085 		      // Need to check that the insn is a ld
8086 		      if (!view)
8087 			view = ppc_object->section_contents(data_shndx,
8088 							    &slen,
8089 							    false);
8090 		      section_size_type off =
8091 			(convert_to_section_size_type(reloc.get_r_offset())
8092 			 + (big_endian ? -2 : 3));
8093 		      if (off < slen
8094 			  && (view[off] & (0x3f << 2)) == 58u << 2)
8095 			ok = true;
8096 		    }
8097 		  if (!ok)
8098 		    ppc_object->set_no_toc_opt(dst_off);
8099 		}
8100 	    }
8101 	  break;
8102 	}
8103     }
8104 
8105   if (size == 32)
8106     {
8107       switch (r_type)
8108 	{
8109 	case elfcpp::R_POWERPC_REL32:
8110 	  if (ppc_object->got2_shndx() != 0
8111 	      && parameters->options().output_is_position_independent())
8112 	    {
8113 	      unsigned int shndx = lsym.get_st_shndx();
8114 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8115 	      bool is_ordinary;
8116 	      shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8117 	      if (is_ordinary && shndx == ppc_object->got2_shndx()
8118 		  && (ppc_object->section_flags(data_shndx)
8119 		      & elfcpp::SHF_EXECINSTR) != 0)
8120 		gold_error(_("%s: unsupported -mbss-plt code"),
8121 			   ppc_object->name().c_str());
8122 	    }
8123 	  break;
8124 	default:
8125 	  break;
8126 	}
8127     }
8128 
8129   switch (r_type)
8130     {
8131     case elfcpp::R_POWERPC_GOT_TLSLD16:
8132     case elfcpp::R_POWERPC_GOT_TLSGD16:
8133     case elfcpp::R_POWERPC_GOT_TPREL16:
8134     case elfcpp::R_POWERPC_GOT_DTPREL16:
8135     case elfcpp::R_POWERPC_GOT16:
8136     case elfcpp::R_PPC64_GOT16_DS:
8137     case elfcpp::R_PPC64_TOC16:
8138     case elfcpp::R_PPC64_TOC16_DS:
8139       ppc_object->set_has_small_toc_reloc();
8140       break;
8141     default:
8142       break;
8143     }
8144 
8145   switch (r_type)
8146     {
8147     case elfcpp::R_POWERPC_TPREL16:
8148     case elfcpp::R_POWERPC_TPREL16_LO:
8149     case elfcpp::R_POWERPC_TPREL16_HI:
8150     case elfcpp::R_POWERPC_TPREL16_HA:
8151     case elfcpp::R_PPC64_TPREL16_DS:
8152     case elfcpp::R_PPC64_TPREL16_LO_DS:
8153     case elfcpp::R_PPC64_TPREL16_HIGH:
8154     case elfcpp::R_PPC64_TPREL16_HIGHA:
8155     case elfcpp::R_PPC64_TPREL16_HIGHER:
8156     case elfcpp::R_PPC64_TPREL16_HIGHERA:
8157     case elfcpp::R_PPC64_TPREL16_HIGHEST:
8158     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8159     case elfcpp::R_PPC64_TPREL34:
8160       layout->set_has_static_tls();
8161       break;
8162     default:
8163       break;
8164     }
8165 
8166   switch (r_type)
8167     {
8168     case elfcpp::R_PPC64_D34:
8169     case elfcpp::R_PPC64_D34_LO:
8170     case elfcpp::R_PPC64_D34_HI30:
8171     case elfcpp::R_PPC64_D34_HA30:
8172     case elfcpp::R_PPC64_D28:
8173     case elfcpp::R_PPC64_PCREL34:
8174     case elfcpp::R_PPC64_PCREL28:
8175     case elfcpp::R_PPC64_TPREL34:
8176     case elfcpp::R_PPC64_DTPREL34:
8177     case elfcpp::R_PPC64_PLT_PCREL34:
8178     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8179     case elfcpp::R_PPC64_GOT_PCREL34:
8180     case elfcpp::R_PPC64_GOT_TLSGD34:
8181     case elfcpp::R_PPC64_GOT_TLSLD34:
8182     case elfcpp::R_PPC64_GOT_DTPREL34:
8183     case elfcpp::R_PPC64_GOT_TPREL34:
8184       target->set_powerxx_stubs();
8185       break;
8186     default:
8187       break;
8188     }
8189 }
8190 
8191 // Report an unsupported relocation against a global symbol.
8192 
8193 template<int size, bool big_endian>
8194 void
8195 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8196     Sized_relobj_file<size, big_endian>* object,
8197     unsigned int r_type,
8198     Symbol* gsym)
8199 {
8200   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8201 	     object->name().c_str(), r_type, gsym->demangled_name().c_str());
8202 }
8203 
8204 // Scan a relocation for a global symbol.
8205 
8206 template<int size, bool big_endian>
8207 inline void
8208 Target_powerpc<size, big_endian>::Scan::global(
8209     Symbol_table* symtab,
8210     Layout* layout,
8211     Target_powerpc<size, big_endian>* target,
8212     Sized_relobj_file<size, big_endian>* object,
8213     unsigned int data_shndx,
8214     Output_section* output_section,
8215     const elfcpp::Rela<size, big_endian>& reloc,
8216     unsigned int r_type,
8217     Symbol* gsym)
8218 {
8219   if (this->maybe_skip_tls_get_addr_call(target, r_type, gsym)
8220       == Track_tls::SKIP)
8221     return;
8222 
8223   if (target->replace_tls_get_addr(gsym))
8224     // Change a __tls_get_addr reference to __tls_get_addr_opt
8225     // so dynamic relocs are emitted against the latter symbol.
8226     gsym = target->tls_get_addr_opt();
8227 
8228   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8229       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8230     {
8231       this->expect_tls_get_addr_call();
8232       const bool final = gsym->final_value_is_known();
8233       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8234       if (tls_type != tls::TLSOPT_NONE)
8235 	this->skip_next_tls_get_addr_call();
8236     }
8237   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8238 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8239     {
8240       this->expect_tls_get_addr_call();
8241       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8242       if (tls_type != tls::TLSOPT_NONE)
8243 	this->skip_next_tls_get_addr_call();
8244     }
8245 
8246   Powerpc_relobj<size, big_endian>* ppc_object
8247     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8248 
8249   // A STT_GNU_IFUNC symbol may require a PLT entry.
8250   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8251   bool pushed_ifunc = false;
8252   if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8253     {
8254       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8255       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8256 			  r_type, r_sym, reloc.get_r_addend());
8257       target->make_plt_entry(symtab, layout, gsym);
8258       pushed_ifunc = true;
8259     }
8260 
8261   switch (r_type)
8262     {
8263     case elfcpp::R_POWERPC_NONE:
8264     case elfcpp::R_POWERPC_GNU_VTINHERIT:
8265     case elfcpp::R_POWERPC_GNU_VTENTRY:
8266     case elfcpp::R_PPC_LOCAL24PC:
8267     case elfcpp::R_POWERPC_TLS:
8268     case elfcpp::R_PPC64_ENTRY:
8269     case elfcpp::R_POWERPC_PLTSEQ:
8270     case elfcpp::R_POWERPC_PLTCALL:
8271     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8272     case elfcpp::R_PPC64_PLTCALL_NOTOC:
8273     case elfcpp::R_PPC64_PCREL_OPT:
8274     case elfcpp::R_PPC64_ADDR16_HIGHER34:
8275     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8276     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8277     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8278     case elfcpp::R_PPC64_REL16_HIGHER34:
8279     case elfcpp::R_PPC64_REL16_HIGHERA34:
8280     case elfcpp::R_PPC64_REL16_HIGHEST34:
8281     case elfcpp::R_PPC64_REL16_HIGHESTA34:
8282     case elfcpp::R_PPC64_D34:
8283     case elfcpp::R_PPC64_D34_LO:
8284     case elfcpp::R_PPC64_D34_HI30:
8285     case elfcpp::R_PPC64_D34_HA30:
8286     case elfcpp::R_PPC64_D28:
8287     case elfcpp::R_PPC64_PCREL34:
8288     case elfcpp::R_PPC64_PCREL28:
8289     case elfcpp::R_PPC64_TPREL34:
8290     case elfcpp::R_PPC64_DTPREL34:
8291       break;
8292 
8293     case elfcpp::R_PPC64_TOC:
8294       {
8295 	Output_data_got_powerpc<size, big_endian>* got
8296 	  = target->got_section(symtab, layout);
8297 	if (parameters->options().output_is_position_independent())
8298 	  {
8299 	    Address off = reloc.get_r_offset();
8300 	    if (size == 64
8301 		&& data_shndx == ppc_object->opd_shndx()
8302 		&& ppc_object->get_opd_discard(off - 8))
8303 	      break;
8304 
8305 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8306 	    Powerpc_relobj<size, big_endian>* symobj = ppc_object;
8307 	    if (data_shndx != ppc_object->opd_shndx())
8308 	      symobj = static_cast
8309 		<Powerpc_relobj<size, big_endian>*>(gsym->object());
8310 	    rela_dyn->add_output_section_relative(got->output_section(),
8311 						  elfcpp::R_POWERPC_RELATIVE,
8312 						  output_section,
8313 						  object, data_shndx, off,
8314 						  symobj->toc_base_offset());
8315 	  }
8316       }
8317       break;
8318 
8319     case elfcpp::R_PPC64_ADDR64:
8320       if (size == 64
8321 	  && target->abiversion() < 2
8322 	  && data_shndx == ppc_object->opd_shndx()
8323 	  && (gsym->is_defined_in_discarded_section()
8324 	      || gsym->object() != object))
8325 	{
8326 	  ppc_object->set_opd_discard(reloc.get_r_offset());
8327 	  break;
8328 	}
8329       // Fall through.
8330     case elfcpp::R_PPC64_UADDR64:
8331     case elfcpp::R_POWERPC_ADDR32:
8332     case elfcpp::R_POWERPC_UADDR32:
8333     case elfcpp::R_POWERPC_ADDR24:
8334     case elfcpp::R_POWERPC_ADDR16:
8335     case elfcpp::R_POWERPC_ADDR16_LO:
8336     case elfcpp::R_POWERPC_ADDR16_HI:
8337     case elfcpp::R_POWERPC_ADDR16_HA:
8338     case elfcpp::R_POWERPC_UADDR16:
8339     case elfcpp::R_PPC64_ADDR16_HIGH:
8340     case elfcpp::R_PPC64_ADDR16_HIGHA:
8341     case elfcpp::R_PPC64_ADDR16_HIGHER:
8342     case elfcpp::R_PPC64_ADDR16_HIGHERA:
8343     case elfcpp::R_PPC64_ADDR16_HIGHEST:
8344     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8345     case elfcpp::R_PPC64_ADDR16_DS:
8346     case elfcpp::R_PPC64_ADDR16_LO_DS:
8347     case elfcpp::R_POWERPC_ADDR14:
8348     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8349     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8350       {
8351 	// Make a PLT entry if necessary.
8352 	if (gsym->needs_plt_entry())
8353 	  {
8354 	    // Since this is not a PC-relative relocation, we may be
8355 	    // taking the address of a function. In that case we need to
8356 	    // set the entry in the dynamic symbol table to the address of
8357 	    // the PLT call stub.
8358 	    bool need_ifunc_plt = false;
8359 	    if ((size == 32 || target->abiversion() >= 2)
8360 		&& gsym->is_from_dynobj()
8361 		&& !parameters->options().output_is_position_independent())
8362 	      {
8363 		gsym->set_needs_dynsym_value();
8364 		need_ifunc_plt = true;
8365 	      }
8366 	    if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8367 	      {
8368 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8369 		target->push_branch(ppc_object, data_shndx,
8370 				    reloc.get_r_offset(), r_type, r_sym,
8371 				    reloc.get_r_addend());
8372 		target->make_plt_entry(symtab, layout, gsym);
8373 	      }
8374 	  }
8375 	// Make a dynamic relocation if necessary.
8376 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8377 	    || (size == 64 && is_ifunc && target->abiversion() < 2))
8378 	  {
8379 	    if (!parameters->options().output_is_position_independent()
8380 		&& gsym->may_need_copy_reloc())
8381 	      {
8382 		target->copy_reloc(symtab, layout, object,
8383 				   data_shndx, output_section, gsym, reloc);
8384 	      }
8385 	    else if ((((size == 32
8386 			&& r_type == elfcpp::R_POWERPC_ADDR32)
8387 		       || (size == 64
8388 			   && r_type == elfcpp::R_PPC64_ADDR64
8389 			   && target->abiversion() >= 2))
8390 		      && gsym->can_use_relative_reloc(false)
8391 		      && !(gsym->visibility() == elfcpp::STV_PROTECTED
8392 			   && parameters->options().shared()))
8393 		     || (size == 64
8394 			 && r_type == elfcpp::R_PPC64_ADDR64
8395 			 && target->abiversion() < 2
8396 			 && (gsym->can_use_relative_reloc(false)
8397 			     || data_shndx == ppc_object->opd_shndx())))
8398 	      {
8399 		Reloc_section* rela_dyn
8400 		  = target->rela_dyn_section(symtab, layout, is_ifunc);
8401 		unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8402 				       : elfcpp::R_POWERPC_RELATIVE);
8403 		rela_dyn->add_symbolless_global_addend(
8404 		    gsym, dynrel, output_section, object, data_shndx,
8405 		    reloc.get_r_offset(), reloc.get_r_addend());
8406 	      }
8407 	    else
8408 	      {
8409 		Reloc_section* rela_dyn
8410 		  = target->rela_dyn_section(symtab, layout, is_ifunc);
8411 		check_non_pic(object, r_type);
8412 		rela_dyn->add_global(gsym, r_type, output_section,
8413 				     object, data_shndx,
8414 				     reloc.get_r_offset(),
8415 				     reloc.get_r_addend());
8416 
8417 		if (size == 64
8418 		    && parameters->options().toc_optimize()
8419 		    && data_shndx == ppc_object->toc_shndx())
8420 		  ppc_object->set_no_toc_opt(reloc.get_r_offset());
8421 	      }
8422 	  }
8423       }
8424       break;
8425 
8426     case elfcpp::R_PPC64_PLT_PCREL34:
8427     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8428     case elfcpp::R_POWERPC_PLT16_LO:
8429     case elfcpp::R_POWERPC_PLT16_HI:
8430     case elfcpp::R_POWERPC_PLT16_HA:
8431     case elfcpp::R_PPC64_PLT16_LO_DS:
8432       if (!pushed_ifunc)
8433 	target->make_plt_entry(symtab, layout, gsym);
8434       break;
8435 
8436     case elfcpp::R_PPC64_REL24_NOTOC:
8437       if (size == 32)
8438 	break;
8439       // Fall through.
8440     case elfcpp::R_PPC_PLTREL24:
8441     case elfcpp::R_POWERPC_REL24:
8442       if (!is_ifunc)
8443 	{
8444 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8445 	  target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8446 			      r_type, r_sym, reloc.get_r_addend());
8447 	  if (gsym->needs_plt_entry()
8448 	      || (!gsym->final_value_is_known()
8449 		  && (gsym->is_undefined()
8450 		      || gsym->is_from_dynobj()
8451 		      || gsym->is_preemptible())))
8452 	    target->make_plt_entry(symtab, layout, gsym);
8453 	}
8454       // Fall through.
8455 
8456     case elfcpp::R_PPC64_REL64:
8457     case elfcpp::R_POWERPC_REL32:
8458       // Make a dynamic relocation if necessary.
8459       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
8460 	{
8461 	  if (!parameters->options().output_is_position_independent()
8462 	      && gsym->may_need_copy_reloc())
8463 	    {
8464 	      target->copy_reloc(symtab, layout, object,
8465 				 data_shndx, output_section, gsym,
8466 				 reloc);
8467 	    }
8468 	  else
8469 	    {
8470 	      Reloc_section* rela_dyn
8471 		= target->rela_dyn_section(symtab, layout, is_ifunc);
8472 	      check_non_pic(object, r_type);
8473 	      rela_dyn->add_global(gsym, r_type, output_section, object,
8474 				   data_shndx, reloc.get_r_offset(),
8475 				   reloc.get_r_addend());
8476 	    }
8477 	}
8478       break;
8479 
8480     case elfcpp::R_POWERPC_REL14:
8481     case elfcpp::R_POWERPC_REL14_BRTAKEN:
8482     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8483       if (!is_ifunc)
8484 	{
8485 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8486 	  target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8487 			      r_type, r_sym, reloc.get_r_addend());
8488 	}
8489       break;
8490 
8491     case elfcpp::R_PPC64_TOCSAVE:
8492       // R_PPC64_TOCSAVE follows a call instruction to indicate the
8493       // caller has already saved r2 and thus a plt call stub need not
8494       // save r2.
8495       if (size == 64
8496 	  && target->mark_pltcall(ppc_object, data_shndx,
8497 				  reloc.get_r_offset() - 4, symtab))
8498 	{
8499 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8500 	  bool is_ordinary;
8501 	  unsigned int shndx = gsym->shndx(&is_ordinary);
8502 	  if (!is_ordinary)
8503 	    object->error(_("tocsave symbol %u has bad shndx %u"),
8504 			  r_sym, shndx);
8505 	  else
8506 	    {
8507 	      Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8508 	      target->add_tocsave(ppc_object, shndx,
8509 				  sym->value() + reloc.get_r_addend());
8510 	    }
8511 	}
8512       break;
8513 
8514     case elfcpp::R_POWERPC_REL16:
8515     case elfcpp::R_POWERPC_REL16_LO:
8516     case elfcpp::R_POWERPC_REL16_HI:
8517     case elfcpp::R_POWERPC_REL16_HA:
8518     case elfcpp::R_POWERPC_REL16DX_HA:
8519     case elfcpp::R_PPC64_REL16_HIGH:
8520     case elfcpp::R_PPC64_REL16_HIGHA:
8521     case elfcpp::R_PPC64_REL16_HIGHER:
8522     case elfcpp::R_PPC64_REL16_HIGHERA:
8523     case elfcpp::R_PPC64_REL16_HIGHEST:
8524     case elfcpp::R_PPC64_REL16_HIGHESTA:
8525     case elfcpp::R_POWERPC_SECTOFF:
8526     case elfcpp::R_POWERPC_SECTOFF_LO:
8527     case elfcpp::R_POWERPC_SECTOFF_HI:
8528     case elfcpp::R_POWERPC_SECTOFF_HA:
8529     case elfcpp::R_PPC64_SECTOFF_DS:
8530     case elfcpp::R_PPC64_SECTOFF_LO_DS:
8531     case elfcpp::R_POWERPC_TPREL16:
8532     case elfcpp::R_POWERPC_TPREL16_LO:
8533     case elfcpp::R_POWERPC_TPREL16_HI:
8534     case elfcpp::R_POWERPC_TPREL16_HA:
8535     case elfcpp::R_PPC64_TPREL16_DS:
8536     case elfcpp::R_PPC64_TPREL16_LO_DS:
8537     case elfcpp::R_PPC64_TPREL16_HIGH:
8538     case elfcpp::R_PPC64_TPREL16_HIGHA:
8539     case elfcpp::R_PPC64_TPREL16_HIGHER:
8540     case elfcpp::R_PPC64_TPREL16_HIGHERA:
8541     case elfcpp::R_PPC64_TPREL16_HIGHEST:
8542     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8543     case elfcpp::R_POWERPC_DTPREL16:
8544     case elfcpp::R_POWERPC_DTPREL16_LO:
8545     case elfcpp::R_POWERPC_DTPREL16_HI:
8546     case elfcpp::R_POWERPC_DTPREL16_HA:
8547     case elfcpp::R_PPC64_DTPREL16_DS:
8548     case elfcpp::R_PPC64_DTPREL16_LO_DS:
8549     case elfcpp::R_PPC64_DTPREL16_HIGH:
8550     case elfcpp::R_PPC64_DTPREL16_HIGHA:
8551     case elfcpp::R_PPC64_DTPREL16_HIGHER:
8552     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8553     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8554     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8555     case elfcpp::R_PPC64_TLSGD:
8556     case elfcpp::R_PPC64_TLSLD:
8557     case elfcpp::R_PPC64_ADDR64_LOCAL:
8558       break;
8559 
8560     case elfcpp::R_PPC64_GOT_PCREL34:
8561     case elfcpp::R_POWERPC_GOT16:
8562     case elfcpp::R_POWERPC_GOT16_LO:
8563     case elfcpp::R_POWERPC_GOT16_HI:
8564     case elfcpp::R_POWERPC_GOT16_HA:
8565     case elfcpp::R_PPC64_GOT16_DS:
8566     case elfcpp::R_PPC64_GOT16_LO_DS:
8567       {
8568 	// The symbol requires a GOT entry.
8569 	Output_data_got_powerpc<size, big_endian>* got;
8570 
8571 	got = target->got_section(symtab, layout);
8572 	if (gsym->final_value_is_known())
8573 	  {
8574 	    if (is_ifunc
8575 		&& (size == 32 || target->abiversion() >= 2))
8576 	      got->add_global_plt(gsym, GOT_TYPE_STANDARD);
8577 	    else
8578 	      got->add_global(gsym, GOT_TYPE_STANDARD);
8579 	  }
8580 	else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
8581 	  {
8582 	    // If we are generating a shared object or a pie, this
8583 	    // symbol's GOT entry will be set by a dynamic relocation.
8584 	    unsigned int off = got->add_constant(0);
8585 	    gsym->set_got_offset(GOT_TYPE_STANDARD, off);
8586 
8587 	    Reloc_section* rela_dyn
8588 	      = target->rela_dyn_section(symtab, layout, is_ifunc);
8589 
8590 	    if (gsym->can_use_relative_reloc(false)
8591 		&& !((size == 32
8592 		      || target->abiversion() >= 2)
8593 		     && gsym->visibility() == elfcpp::STV_PROTECTED
8594 		     && parameters->options().shared()))
8595 	      {
8596 		unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8597 				       : elfcpp::R_POWERPC_RELATIVE);
8598 		rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
8599 	      }
8600 	    else
8601 	      {
8602 		unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
8603 		rela_dyn->add_global(gsym, dynrel, got, off, 0);
8604 	      }
8605 	  }
8606       }
8607       break;
8608 
8609     case elfcpp::R_PPC64_TOC16:
8610     case elfcpp::R_PPC64_TOC16_LO:
8611     case elfcpp::R_PPC64_TOC16_HI:
8612     case elfcpp::R_PPC64_TOC16_HA:
8613     case elfcpp::R_PPC64_TOC16_DS:
8614     case elfcpp::R_PPC64_TOC16_LO_DS:
8615       // We need a GOT section.
8616       target->got_section(symtab, layout);
8617       break;
8618 
8619     case elfcpp::R_PPC64_GOT_TLSGD34:
8620     case elfcpp::R_POWERPC_GOT_TLSGD16:
8621     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8622     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8623     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8624       {
8625 	const bool final = gsym->final_value_is_known();
8626 	const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8627 	if (tls_type == tls::TLSOPT_NONE)
8628 	  {
8629 	    Output_data_got_powerpc<size, big_endian>* got
8630 	      = target->got_section(symtab, layout);
8631 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8632 	    got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
8633 					  elfcpp::R_POWERPC_DTPMOD,
8634 					  elfcpp::R_POWERPC_DTPREL);
8635 	  }
8636 	else if (tls_type == tls::TLSOPT_TO_IE)
8637 	  {
8638 	    if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8639 	      {
8640 		Output_data_got_powerpc<size, big_endian>* got
8641 		  = target->got_section(symtab, layout);
8642 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8643 		if (gsym->is_undefined()
8644 		    || gsym->is_from_dynobj())
8645 		  {
8646 		    got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8647 					     elfcpp::R_POWERPC_TPREL);
8648 		  }
8649 		else
8650 		  {
8651 		    unsigned int off = got->add_constant(0);
8652 		    gsym->set_got_offset(GOT_TYPE_TPREL, off);
8653 		    unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8654 		    rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8655 							   got, off, 0);
8656 		  }
8657 	      }
8658 	  }
8659 	else if (tls_type == tls::TLSOPT_TO_LE)
8660 	  {
8661 	    // no GOT relocs needed for Local Exec.
8662 	  }
8663 	else
8664 	  gold_unreachable();
8665       }
8666       break;
8667 
8668     case elfcpp::R_PPC64_GOT_TLSLD34:
8669     case elfcpp::R_POWERPC_GOT_TLSLD16:
8670     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8671     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8672     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8673       {
8674 	const tls::Tls_optimization tls_type = target->optimize_tls_ld();
8675 	if (tls_type == tls::TLSOPT_NONE)
8676 	  target->tlsld_got_offset(symtab, layout, object);
8677 	else if (tls_type == tls::TLSOPT_TO_LE)
8678 	  {
8679 	    // no GOT relocs needed for Local Exec.
8680 	    if (parameters->options().emit_relocs())
8681 	      {
8682 		Output_section* os = layout->tls_segment()->first_section();
8683 		gold_assert(os != NULL);
8684 		os->set_needs_symtab_index();
8685 	      }
8686 	  }
8687 	else
8688 	  gold_unreachable();
8689       }
8690       break;
8691 
8692     case elfcpp::R_PPC64_GOT_DTPREL34:
8693     case elfcpp::R_POWERPC_GOT_DTPREL16:
8694     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8695     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8696     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8697       {
8698 	Output_data_got_powerpc<size, big_endian>* got
8699 	  = target->got_section(symtab, layout);
8700 	if (!gsym->final_value_is_known()
8701 	    && (gsym->is_from_dynobj()
8702 		|| gsym->is_undefined()
8703 		|| gsym->is_preemptible()))
8704 	  got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
8705 				   target->rela_dyn_section(layout),
8706 				   elfcpp::R_POWERPC_DTPREL);
8707 	else
8708 	  got->add_global_tls(gsym, GOT_TYPE_DTPREL);
8709       }
8710       break;
8711 
8712     case elfcpp::R_PPC64_GOT_TPREL34:
8713     case elfcpp::R_POWERPC_GOT_TPREL16:
8714     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8715     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8716     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8717       {
8718 	const bool final = gsym->final_value_is_known();
8719 	const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
8720 	if (tls_type == tls::TLSOPT_NONE)
8721 	  {
8722 	    if (!gsym->has_got_offset(GOT_TYPE_TPREL))
8723 	      {
8724 		Output_data_got_powerpc<size, big_endian>* got
8725 		  = target->got_section(symtab, layout);
8726 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8727 		if (gsym->is_undefined()
8728 		    || gsym->is_from_dynobj())
8729 		  {
8730 		    got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
8731 					     elfcpp::R_POWERPC_TPREL);
8732 		  }
8733 		else
8734 		  {
8735 		    unsigned int off = got->add_constant(0);
8736 		    gsym->set_got_offset(GOT_TYPE_TPREL, off);
8737 		    unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
8738 		    rela_dyn->add_symbolless_global_addend(gsym, dynrel,
8739 							   got, off, 0);
8740 		  }
8741 	      }
8742 	  }
8743 	else if (tls_type == tls::TLSOPT_TO_LE)
8744 	  {
8745 	    // no GOT relocs needed for Local Exec.
8746 	  }
8747 	else
8748 	  gold_unreachable();
8749       }
8750       break;
8751 
8752     default:
8753       unsupported_reloc_global(object, r_type, gsym);
8754       break;
8755     }
8756 
8757   if (size == 64
8758       && parameters->options().toc_optimize())
8759     {
8760       if (data_shndx == ppc_object->toc_shndx())
8761 	{
8762 	  bool ok = true;
8763 	  if (r_type != elfcpp::R_PPC64_ADDR64
8764 	      || (is_ifunc && target->abiversion() < 2))
8765 	    ok = false;
8766 	  else if (parameters->options().output_is_position_independent()
8767 		   && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
8768 	    ok = false;
8769 	  if (!ok)
8770 	    ppc_object->set_no_toc_opt(reloc.get_r_offset());
8771 	}
8772 
8773       enum {no_check, check_lo, check_ha} insn_check;
8774       switch (r_type)
8775 	{
8776 	default:
8777 	  insn_check = no_check;
8778 	  break;
8779 
8780 	case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8781 	case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8782 	case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8783 	case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8784 	case elfcpp::R_POWERPC_GOT16_HA:
8785 	case elfcpp::R_PPC64_TOC16_HA:
8786 	  insn_check = check_ha;
8787 	  break;
8788 
8789 	case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8790 	case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8791 	case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8792 	case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8793 	case elfcpp::R_POWERPC_GOT16_LO:
8794 	case elfcpp::R_PPC64_GOT16_LO_DS:
8795 	case elfcpp::R_PPC64_TOC16_LO:
8796 	case elfcpp::R_PPC64_TOC16_LO_DS:
8797 	  insn_check = check_lo;
8798 	  break;
8799 	}
8800 
8801       section_size_type slen;
8802       const unsigned char* view = NULL;
8803       if (insn_check != no_check)
8804 	{
8805 	  view = ppc_object->section_contents(data_shndx, &slen, false);
8806 	  section_size_type off =
8807 	    convert_to_section_size_type(reloc.get_r_offset()) & -4;
8808 	  if (off < slen)
8809 	    {
8810 	      uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8811 	      if (insn_check == check_lo
8812 		  ? !ok_lo_toc_insn(insn, r_type)
8813 		  : ((insn & ((0x3f << 26) | 0x1f << 16))
8814 		     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8815 		{
8816 		  ppc_object->set_no_toc_opt();
8817 		  gold_warning(_("%s: toc optimization is not supported "
8818 				 "for %#08x instruction"),
8819 			       ppc_object->name().c_str(), insn);
8820 		}
8821 	    }
8822 	}
8823 
8824       switch (r_type)
8825 	{
8826 	default:
8827 	  break;
8828 	case elfcpp::R_PPC64_TOC16:
8829 	case elfcpp::R_PPC64_TOC16_LO:
8830 	case elfcpp::R_PPC64_TOC16_HI:
8831 	case elfcpp::R_PPC64_TOC16_HA:
8832 	case elfcpp::R_PPC64_TOC16_DS:
8833 	case elfcpp::R_PPC64_TOC16_LO_DS:
8834 	  if (gsym->source() == Symbol::FROM_OBJECT
8835 	      && !gsym->object()->is_dynamic())
8836 	    {
8837 	      Powerpc_relobj<size, big_endian>* sym_object
8838 		= static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
8839 	      bool is_ordinary;
8840 	      unsigned int shndx = gsym->shndx(&is_ordinary);
8841 	      if (shndx == sym_object->toc_shndx())
8842 		{
8843 		  Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
8844 		  Address dst_off = sym->value() + reloc.get_r_addend();
8845 		  if (dst_off < sym_object->section_size(shndx))
8846 		    {
8847 		      bool ok = false;
8848 		      if (r_type == elfcpp::R_PPC64_TOC16_HA)
8849 			ok = true;
8850 		      else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8851 			{
8852 			  // Need to check that the insn is a ld
8853 			  if (!view)
8854 			    view = ppc_object->section_contents(data_shndx,
8855 								&slen,
8856 								false);
8857 			  section_size_type off =
8858 			    (convert_to_section_size_type(reloc.get_r_offset())
8859 			     + (big_endian ? -2 : 3));
8860 			  if (off < slen
8861 			      && (view[off] & (0x3f << 2)) == (58u << 2))
8862 			    ok = true;
8863 			}
8864 		      if (!ok)
8865 			sym_object->set_no_toc_opt(dst_off);
8866 		    }
8867 		}
8868 	    }
8869 	  break;
8870 	}
8871     }
8872 
8873   if (size == 32)
8874     {
8875       switch (r_type)
8876 	{
8877 	case elfcpp::R_PPC_LOCAL24PC:
8878 	  if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
8879 	    gold_error(_("%s: unsupported -mbss-plt code"),
8880 		       ppc_object->name().c_str());
8881 	  break;
8882 	default:
8883 	  break;
8884 	}
8885     }
8886 
8887   switch (r_type)
8888     {
8889     case elfcpp::R_POWERPC_GOT_TLSLD16:
8890     case elfcpp::R_POWERPC_GOT_TLSGD16:
8891     case elfcpp::R_POWERPC_GOT_TPREL16:
8892     case elfcpp::R_POWERPC_GOT_DTPREL16:
8893     case elfcpp::R_POWERPC_GOT16:
8894     case elfcpp::R_PPC64_GOT16_DS:
8895     case elfcpp::R_PPC64_TOC16:
8896     case elfcpp::R_PPC64_TOC16_DS:
8897       ppc_object->set_has_small_toc_reloc();
8898       break;
8899     default:
8900       break;
8901     }
8902 
8903   switch (r_type)
8904     {
8905     case elfcpp::R_POWERPC_TPREL16:
8906     case elfcpp::R_POWERPC_TPREL16_LO:
8907     case elfcpp::R_POWERPC_TPREL16_HI:
8908     case elfcpp::R_POWERPC_TPREL16_HA:
8909     case elfcpp::R_PPC64_TPREL16_DS:
8910     case elfcpp::R_PPC64_TPREL16_LO_DS:
8911     case elfcpp::R_PPC64_TPREL16_HIGH:
8912     case elfcpp::R_PPC64_TPREL16_HIGHA:
8913     case elfcpp::R_PPC64_TPREL16_HIGHER:
8914     case elfcpp::R_PPC64_TPREL16_HIGHERA:
8915     case elfcpp::R_PPC64_TPREL16_HIGHEST:
8916     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8917     case elfcpp::R_PPC64_TPREL34:
8918       layout->set_has_static_tls();
8919       break;
8920     default:
8921       break;
8922     }
8923 
8924   switch (r_type)
8925     {
8926     case elfcpp::R_PPC64_D34:
8927     case elfcpp::R_PPC64_D34_LO:
8928     case elfcpp::R_PPC64_D34_HI30:
8929     case elfcpp::R_PPC64_D34_HA30:
8930     case elfcpp::R_PPC64_D28:
8931     case elfcpp::R_PPC64_PCREL34:
8932     case elfcpp::R_PPC64_PCREL28:
8933     case elfcpp::R_PPC64_TPREL34:
8934     case elfcpp::R_PPC64_DTPREL34:
8935     case elfcpp::R_PPC64_PLT_PCREL34:
8936     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8937     case elfcpp::R_PPC64_GOT_PCREL34:
8938     case elfcpp::R_PPC64_GOT_TLSGD34:
8939     case elfcpp::R_PPC64_GOT_TLSLD34:
8940     case elfcpp::R_PPC64_GOT_DTPREL34:
8941     case elfcpp::R_PPC64_GOT_TPREL34:
8942       target->set_powerxx_stubs();
8943       break;
8944     default:
8945       break;
8946     }
8947 }
8948 
8949 // Process relocations for gc.
8950 
8951 template<int size, bool big_endian>
8952 void
8953 Target_powerpc<size, big_endian>::gc_process_relocs(
8954     Symbol_table* symtab,
8955     Layout* layout,
8956     Sized_relobj_file<size, big_endian>* object,
8957     unsigned int data_shndx,
8958     unsigned int,
8959     const unsigned char* prelocs,
8960     size_t reloc_count,
8961     Output_section* output_section,
8962     bool needs_special_offset_handling,
8963     size_t local_symbol_count,
8964     const unsigned char* plocal_symbols)
8965 {
8966   typedef Target_powerpc<size, big_endian> Powerpc;
8967   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
8968       Classify_reloc;
8969 
8970   Powerpc_relobj<size, big_endian>* ppc_object
8971     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8972   if (size == 64)
8973     ppc_object->set_opd_valid();
8974   if (size == 64 && data_shndx == ppc_object->opd_shndx())
8975     {
8976       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
8977       for (p = ppc_object->access_from_map()->begin();
8978 	   p != ppc_object->access_from_map()->end();
8979 	   ++p)
8980 	{
8981 	  Address dst_off = p->first;
8982 	  unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
8983 	  typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
8984 	  for (s = p->second.begin(); s != p->second.end(); ++s)
8985 	    {
8986 	      Relobj* src_obj = s->first;
8987 	      unsigned int src_indx = s->second;
8988 	      symtab->gc()->add_reference(src_obj, src_indx,
8989 					  ppc_object, dst_indx);
8990 	    }
8991 	  p->second.clear();
8992 	}
8993       ppc_object->access_from_map()->clear();
8994       ppc_object->process_gc_mark(symtab);
8995       // Don't look at .opd relocs as .opd will reference everything.
8996       return;
8997     }
8998 
8999   gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9000     symtab,
9001     layout,
9002     this,
9003     object,
9004     data_shndx,
9005     prelocs,
9006     reloc_count,
9007     output_section,
9008     needs_special_offset_handling,
9009     local_symbol_count,
9010     plocal_symbols);
9011 }
9012 
9013 // Handle target specific gc actions when adding a gc reference from
9014 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9015 // and DST_OFF.  For powerpc64, this adds a referenc to the code
9016 // section of a function descriptor.
9017 
9018 template<int size, bool big_endian>
9019 void
9020 Target_powerpc<size, big_endian>::do_gc_add_reference(
9021     Symbol_table* symtab,
9022     Relobj* src_obj,
9023     unsigned int src_shndx,
9024     Relobj* dst_obj,
9025     unsigned int dst_shndx,
9026     Address dst_off) const
9027 {
9028   if (size != 64 || dst_obj->is_dynamic())
9029     return;
9030 
9031   Powerpc_relobj<size, big_endian>* ppc_object
9032     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9033   if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9034     {
9035       if (ppc_object->opd_valid())
9036 	{
9037 	  dst_shndx = ppc_object->get_opd_ent(dst_off);
9038 	  symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9039 	}
9040       else
9041 	{
9042 	  // If we haven't run scan_opd_relocs, we must delay
9043 	  // processing this function descriptor reference.
9044 	  ppc_object->add_reference(src_obj, src_shndx, dst_off);
9045 	}
9046     }
9047 }
9048 
9049 // Add any special sections for this symbol to the gc work list.
9050 // For powerpc64, this adds the code section of a function
9051 // descriptor.
9052 
9053 template<int size, bool big_endian>
9054 void
9055 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9056     Symbol_table* symtab,
9057     Symbol* sym) const
9058 {
9059   if (size == 64)
9060     {
9061       Powerpc_relobj<size, big_endian>* ppc_object
9062 	= static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9063       bool is_ordinary;
9064       unsigned int shndx = sym->shndx(&is_ordinary);
9065       if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9066 	{
9067 	  Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9068 	  Address dst_off = gsym->value();
9069 	  if (ppc_object->opd_valid())
9070 	    {
9071 	      unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9072 	      symtab->gc()->worklist().push_back(Section_id(ppc_object,
9073                                                             dst_indx));
9074 	    }
9075 	  else
9076 	    ppc_object->add_gc_mark(dst_off);
9077 	}
9078     }
9079 }
9080 
9081 // For a symbol location in .opd, set LOC to the location of the
9082 // function entry.
9083 
9084 template<int size, bool big_endian>
9085 void
9086 Target_powerpc<size, big_endian>::do_function_location(
9087     Symbol_location* loc) const
9088 {
9089   if (size == 64 && loc->shndx != 0)
9090     {
9091       if (loc->object->is_dynamic())
9092 	{
9093 	  Powerpc_dynobj<size, big_endian>* ppc_object
9094 	    = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9095 	  if (loc->shndx == ppc_object->opd_shndx())
9096 	    {
9097 	      Address dest_off;
9098 	      Address off = loc->offset - ppc_object->opd_address();
9099 	      loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9100 	      loc->offset = dest_off;
9101 	    }
9102 	}
9103       else
9104 	{
9105 	  const Powerpc_relobj<size, big_endian>* ppc_object
9106 	    = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9107 	  if (loc->shndx == ppc_object->opd_shndx())
9108 	    {
9109 	      Address dest_off;
9110 	      loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9111 	      loc->offset = dest_off;
9112 	    }
9113 	}
9114     }
9115 }
9116 
9117 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9118 // compiled with -fsplit-stack.  The function calls non-split-stack
9119 // code.  Change the function to ensure it has enough stack space to
9120 // call some random function.
9121 
9122 template<int size, bool big_endian>
9123 void
9124 Target_powerpc<size, big_endian>::do_calls_non_split(
9125     Relobj* object,
9126     unsigned int shndx,
9127     section_offset_type fnoffset,
9128     section_size_type fnsize,
9129     const unsigned char* prelocs,
9130     size_t reloc_count,
9131     unsigned char* view,
9132     section_size_type view_size,
9133     std::string* from,
9134     std::string* to) const
9135 {
9136   // 32-bit not supported.
9137   if (size == 32)
9138     {
9139       // warn
9140       Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9141 				 prelocs, reloc_count, view, view_size,
9142 				 from, to);
9143       return;
9144     }
9145 
9146   // The function always starts with
9147   //	ld %r0,-0x7000-64(%r13)  # tcbhead_t.__private_ss
9148   //	addis %r12,%r1,-allocate@ha
9149   //	addi %r12,%r12,-allocate@l
9150   //	cmpld %r12,%r0
9151   // but note that the addis or addi may be replaced with a nop
9152 
9153   unsigned char *entry = view + fnoffset;
9154   uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9155 
9156   if ((insn & 0xffff0000) == addis_2_12)
9157     {
9158       /* Skip ELFv2 global entry code.  */
9159       entry += 8;
9160       insn = elfcpp::Swap<32, big_endian>::readval(entry);
9161     }
9162 
9163   unsigned char *pinsn = entry;
9164   bool ok = false;
9165   const uint32_t ld_private_ss = 0xe80d8fc0;
9166   if (insn == ld_private_ss)
9167     {
9168       int32_t allocate = 0;
9169       while (1)
9170 	{
9171 	  pinsn += 4;
9172 	  insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9173 	  if ((insn & 0xffff0000) == addis_12_1)
9174 	    allocate += (insn & 0xffff) << 16;
9175 	  else if ((insn & 0xffff0000) == addi_12_1
9176 		   || (insn & 0xffff0000) == addi_12_12)
9177 	    allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9178 	  else if (insn != nop)
9179 	    break;
9180 	}
9181       if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9182 	{
9183 	  int extra = parameters->options().split_stack_adjust_size();
9184 	  allocate -= extra;
9185 	  if (allocate >= 0 || extra < 0)
9186 	    {
9187 	      object->error(_("split-stack stack size overflow at "
9188 			      "section %u offset %0zx"),
9189 			    shndx, static_cast<size_t>(fnoffset));
9190 	      return;
9191 	    }
9192 	  pinsn = entry + 4;
9193 	  insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9194 	  if (insn != addis_12_1)
9195 	    {
9196 	      elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9197 	      pinsn += 4;
9198 	      insn = addi_12_12 | (allocate & 0xffff);
9199 	      if (insn != addi_12_12)
9200 		{
9201 		  elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9202 		  pinsn += 4;
9203 		}
9204 	    }
9205 	  else
9206 	    {
9207 	      insn = addi_12_1 | (allocate & 0xffff);
9208 	      elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9209 	      pinsn += 4;
9210 	    }
9211 	  if (pinsn != entry + 12)
9212 	    elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9213 
9214 	  ok = true;
9215 	}
9216     }
9217 
9218   if (!ok)
9219     {
9220       if (!object->has_no_split_stack())
9221 	object->error(_("failed to match split-stack sequence at "
9222 			"section %u offset %0zx"),
9223 		      shndx, static_cast<size_t>(fnoffset));
9224     }
9225 }
9226 
9227 // Scan relocations for a section.
9228 
9229 template<int size, bool big_endian>
9230 void
9231 Target_powerpc<size, big_endian>::scan_relocs(
9232     Symbol_table* symtab,
9233     Layout* layout,
9234     Sized_relobj_file<size, big_endian>* object,
9235     unsigned int data_shndx,
9236     unsigned int sh_type,
9237     const unsigned char* prelocs,
9238     size_t reloc_count,
9239     Output_section* output_section,
9240     bool needs_special_offset_handling,
9241     size_t local_symbol_count,
9242     const unsigned char* plocal_symbols)
9243 {
9244   typedef Target_powerpc<size, big_endian> Powerpc;
9245   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9246       Classify_reloc;
9247 
9248   if (!this->plt_localentry0_init_)
9249     {
9250       bool plt_localentry0 = false;
9251       if (size == 64
9252 	  && this->abiversion() >= 2)
9253 	{
9254 	  if (parameters->options().user_set_plt_localentry())
9255 	    plt_localentry0 = parameters->options().plt_localentry();
9256 	  if (plt_localentry0
9257 	      && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9258 	    gold_warning(_("--plt-localentry is especially dangerous without "
9259 			   "ld.so support to detect ABI violations"));
9260 	}
9261       this->plt_localentry0_ = plt_localentry0;
9262       this->plt_localentry0_init_ = true;
9263     }
9264 
9265   if (sh_type == elfcpp::SHT_REL)
9266     {
9267       gold_error(_("%s: unsupported REL reloc section"),
9268 		 object->name().c_str());
9269       return;
9270     }
9271 
9272   gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9273     symtab,
9274     layout,
9275     this,
9276     object,
9277     data_shndx,
9278     prelocs,
9279     reloc_count,
9280     output_section,
9281     needs_special_offset_handling,
9282     local_symbol_count,
9283     plocal_symbols);
9284 }
9285 
9286 // Functor class for processing the global symbol table.
9287 // Removes symbols defined on discarded opd entries.
9288 
9289 template<bool big_endian>
9290 class Global_symbol_visitor_opd
9291 {
9292  public:
9293   Global_symbol_visitor_opd()
9294   { }
9295 
9296   void
9297   operator()(Sized_symbol<64>* sym)
9298   {
9299     if (sym->has_symtab_index()
9300 	|| sym->source() != Symbol::FROM_OBJECT
9301 	|| !sym->in_real_elf())
9302       return;
9303 
9304     if (sym->object()->is_dynamic())
9305       return;
9306 
9307     Powerpc_relobj<64, big_endian>* symobj
9308       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9309     if (symobj->opd_shndx() == 0)
9310       return;
9311 
9312     bool is_ordinary;
9313     unsigned int shndx = sym->shndx(&is_ordinary);
9314     if (shndx == symobj->opd_shndx()
9315 	&& symobj->get_opd_discard(sym->value()))
9316       {
9317 	sym->set_undefined();
9318 	sym->set_visibility(elfcpp::STV_DEFAULT);
9319 	sym->set_is_defined_in_discarded_section();
9320 	sym->set_symtab_index(-1U);
9321       }
9322   }
9323 };
9324 
9325 template<int size, bool big_endian>
9326 void
9327 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9328     Layout* layout,
9329     Symbol_table* symtab)
9330 {
9331   if (size == 64)
9332     {
9333       Output_data_save_res<size, big_endian>* savres
9334 	= new Output_data_save_res<size, big_endian>(symtab);
9335       this->savres_section_ = savres;
9336       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9337 				      elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9338 				      savres, ORDER_TEXT, false);
9339     }
9340 }
9341 
9342 // Sort linker created .got section first (for the header), then input
9343 // sections belonging to files using small model code.
9344 
9345 template<bool big_endian>
9346 class Sort_toc_sections
9347 {
9348  public:
9349   bool
9350   operator()(const Output_section::Input_section& is1,
9351 	     const Output_section::Input_section& is2) const
9352   {
9353     if (!is1.is_input_section() && is2.is_input_section())
9354       return true;
9355     bool small1
9356       = (is1.is_input_section()
9357 	 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
9358 	     ->has_small_toc_reloc()));
9359     bool small2
9360       = (is2.is_input_section()
9361 	 && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
9362 	     ->has_small_toc_reloc()));
9363     return small1 && !small2;
9364   }
9365 };
9366 
9367 // Finalize the sections.
9368 
9369 template<int size, bool big_endian>
9370 void
9371 Target_powerpc<size, big_endian>::do_finalize_sections(
9372     Layout* layout,
9373     const Input_objects* input_objects,
9374     Symbol_table* symtab)
9375 {
9376   if (parameters->doing_static_link())
9377     {
9378       // At least some versions of glibc elf-init.o have a strong
9379       // reference to __rela_iplt marker syms.  A weak ref would be
9380       // better..
9381       if (this->iplt_ != NULL)
9382 	{
9383 	  Reloc_section* rel = this->iplt_->rel_plt();
9384 	  symtab->define_in_output_data("__rela_iplt_start", NULL,
9385 					Symbol_table::PREDEFINED, rel, 0, 0,
9386 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9387 					elfcpp::STV_HIDDEN, 0, false, true);
9388 	  symtab->define_in_output_data("__rela_iplt_end", NULL,
9389 					Symbol_table::PREDEFINED, rel, 0, 0,
9390 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9391 					elfcpp::STV_HIDDEN, 0, true, true);
9392 	}
9393       else
9394 	{
9395 	  symtab->define_as_constant("__rela_iplt_start", NULL,
9396 				     Symbol_table::PREDEFINED, 0, 0,
9397 				     elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9398 				     elfcpp::STV_HIDDEN, 0, true, false);
9399 	  symtab->define_as_constant("__rela_iplt_end", NULL,
9400 				     Symbol_table::PREDEFINED, 0, 0,
9401 				     elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9402 				     elfcpp::STV_HIDDEN, 0, true, false);
9403 	}
9404     }
9405 
9406   if (size == 64)
9407     {
9408       typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
9409       symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
9410 
9411       if (!parameters->options().relocatable())
9412 	{
9413 	  this->define_save_restore_funcs(layout, symtab);
9414 
9415 	  // Annoyingly, we need to make these sections now whether or
9416 	  // not we need them.  If we delay until do_relax then we
9417 	  // need to mess with the relaxation machinery checkpointing.
9418 	  this->got_section(symtab, layout);
9419 	  this->make_brlt_section(layout);
9420 
9421 	  if (parameters->options().toc_sort())
9422 	    {
9423 	      Output_section* os = this->got_->output_section();
9424 	      if (os != NULL && os->input_sections().size() > 1)
9425 		std::stable_sort(os->input_sections().begin(),
9426 				 os->input_sections().end(),
9427 				 Sort_toc_sections<big_endian>());
9428 	    }
9429 	}
9430     }
9431 
9432   // Fill in some more dynamic tags.
9433   Output_data_dynamic* odyn = layout->dynamic_data();
9434   if (odyn != NULL)
9435     {
9436       const Reloc_section* rel_plt = (this->plt_ == NULL
9437 				      ? NULL
9438 				      : this->plt_->rel_plt());
9439       layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
9440 				      this->rela_dyn_, true, size == 32);
9441 
9442       if (size == 32)
9443 	{
9444 	  if (this->got_ != NULL)
9445 	    {
9446 	      this->got_->finalize_data_size();
9447 	      odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
9448 					    this->got_, this->got_->g_o_t());
9449 	    }
9450 	  if (this->has_tls_get_addr_opt_)
9451 	    odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
9452 	}
9453       else
9454 	{
9455 	  if (this->glink_ != NULL)
9456 	    {
9457 	      this->glink_->finalize_data_size();
9458 	      odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
9459 					    this->glink_,
9460 					    (this->glink_->pltresolve_size()
9461 					     - 32));
9462 	    }
9463 	  if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
9464 	    odyn->add_constant(elfcpp::DT_PPC64_OPT,
9465 			       ((this->has_localentry0_
9466 				 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
9467 				| (this->has_tls_get_addr_opt_
9468 				   ? elfcpp::PPC64_OPT_TLS : 0)));
9469 	}
9470     }
9471 
9472   // Emit any relocs we saved in an attempt to avoid generating COPY
9473   // relocs.
9474   if (this->copy_relocs_.any_saved_relocs())
9475     this->copy_relocs_.emit(this->rela_dyn_section(layout));
9476 
9477   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9478        p != input_objects->relobj_end();
9479        ++p)
9480     {
9481       Powerpc_relobj<size, big_endian>* ppc_relobj
9482 	= static_cast<Powerpc_relobj<size, big_endian>*>(*p);
9483       if (ppc_relobj->attributes_section_data())
9484 	this->merge_object_attributes(ppc_relobj->name().c_str(),
9485 				      ppc_relobj->attributes_section_data());
9486     }
9487   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9488        p != input_objects->dynobj_end();
9489        ++p)
9490     {
9491       Powerpc_dynobj<size, big_endian>* ppc_dynobj
9492 	= static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
9493       if (ppc_dynobj->attributes_section_data())
9494 	this->merge_object_attributes(ppc_dynobj->name().c_str(),
9495 				      ppc_dynobj->attributes_section_data());
9496     }
9497 
9498   // Create a .gnu.attributes section if we have merged any attributes
9499   // from inputs.
9500   if (this->attributes_section_data_ != NULL
9501       && this->attributes_section_data_->size() != 0)
9502     {
9503       Output_attributes_section_data* attributes_section
9504 	= new Output_attributes_section_data(*this->attributes_section_data_);
9505       layout->add_output_section_data(".gnu.attributes",
9506 				      elfcpp::SHT_GNU_ATTRIBUTES, 0,
9507 				      attributes_section, ORDER_INVALID, false);
9508     }
9509 }
9510 
9511 // Merge object attributes from input file called NAME with those of the
9512 // output.  The input object attributes are in the object pointed by PASD.
9513 
9514 template<int size, bool big_endian>
9515 void
9516 Target_powerpc<size, big_endian>::merge_object_attributes(
9517     const char* name,
9518     const Attributes_section_data* pasd)
9519 {
9520   // Return if there is no attributes section data.
9521   if (pasd == NULL)
9522     return;
9523 
9524   // Create output object attributes.
9525   if (this->attributes_section_data_ == NULL)
9526     this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
9527 
9528   const int vendor = Object_attribute::OBJ_ATTR_GNU;
9529   const Object_attribute* in_attr = pasd->known_attributes(vendor);
9530   Object_attribute* out_attr
9531     = this->attributes_section_data_->known_attributes(vendor);
9532 
9533   const char* err;
9534   const char* first;
9535   const char* second;
9536   int tag = elfcpp::Tag_GNU_Power_ABI_FP;
9537   int in_fp = in_attr[tag].int_value() & 0xf;
9538   int out_fp = out_attr[tag].int_value() & 0xf;
9539   if (in_fp != out_fp)
9540     {
9541       err = NULL;
9542       if ((in_fp & 3) == 0)
9543 	;
9544       else if ((out_fp & 3) == 0)
9545 	{
9546 	  out_fp |= in_fp & 3;
9547 	  out_attr[tag].set_int_value(out_fp);
9548 	  out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9549 	  this->last_fp_ = name;
9550 	}
9551       else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
9552 	{
9553 	  err = N_("%s uses hard float, %s uses soft float");
9554 	  first = this->last_fp_;
9555 	  second = name;
9556 	}
9557       else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
9558 	{
9559 	  err = N_("%s uses hard float, %s uses soft float");
9560 	  first = name;
9561 	  second = this->last_fp_;
9562 	}
9563       else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
9564 	{
9565 	  err = N_("%s uses double-precision hard float, "
9566 		   "%s uses single-precision hard float");
9567 	  first = this->last_fp_;
9568 	  second = name;
9569 	}
9570       else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
9571 	{
9572 	  err = N_("%s uses double-precision hard float, "
9573 		   "%s uses single-precision hard float");
9574 	  first = name;
9575 	  second = this->last_fp_;
9576 	}
9577 
9578       if (err || (in_fp & 0xc) == 0)
9579 	;
9580       else if ((out_fp & 0xc) == 0)
9581 	{
9582 	  out_fp |= in_fp & 0xc;
9583 	  out_attr[tag].set_int_value(out_fp);
9584 	  out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9585 	  this->last_ld_ = name;
9586 	}
9587       else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
9588 	{
9589 	  err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9590 	  first = name;
9591 	  second = this->last_ld_;
9592 	}
9593       else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
9594 	{
9595 	  err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
9596 	  first = this->last_ld_;
9597 	  second = name;
9598 	}
9599       else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
9600 	{
9601 	  err = N_("%s uses IBM long double, %s uses IEEE long double");
9602 	  first = this->last_ld_;
9603 	  second = name;
9604 	}
9605       else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
9606 	{
9607 	  err = N_("%s uses IBM long double, %s uses IEEE long double");
9608 	  first = name;
9609 	  second = this->last_ld_;
9610 	}
9611 
9612       if (err)
9613 	{
9614 	  if (parameters->options().warn_mismatch())
9615 	    gold_error(_(err), first, second);
9616 	  // Arrange for this attribute to be deleted.  It's better to
9617 	  // say "don't know" about a file than to wrongly claim compliance.
9618 	  out_attr[tag].set_type(0);
9619 	}
9620     }
9621 
9622   if (size == 32)
9623     {
9624       tag = elfcpp::Tag_GNU_Power_ABI_Vector;
9625       int in_vec = in_attr[tag].int_value() & 3;
9626       int out_vec = out_attr[tag].int_value() & 3;
9627       if (in_vec != out_vec)
9628 	{
9629 	  err = NULL;
9630 	  if (in_vec == 0)
9631 	    ;
9632 	  else if (out_vec == 0)
9633 	    {
9634 	      out_vec = in_vec;
9635 	      out_attr[tag].set_int_value(out_vec);
9636 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9637 	      this->last_vec_ = name;
9638 	    }
9639 	  // For now, allow generic to transition to AltiVec or SPE
9640 	  // without a warning.  If GCC marked files with their stack
9641 	  // alignment and used don't-care markings for files which are
9642 	  // not affected by the vector ABI, we could warn about this
9643 	  // case too.  */
9644 	  else if (in_vec == 1)
9645 	    ;
9646 	  else if (out_vec == 1)
9647 	    {
9648 	      out_vec = in_vec;
9649 	      out_attr[tag].set_int_value(out_vec);
9650 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9651 	      this->last_vec_ = name;
9652 	    }
9653 	  else if (out_vec < in_vec)
9654 	    {
9655 	      err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9656 	      first = this->last_vec_;
9657 	      second = name;
9658 	    }
9659 	  else if (out_vec > in_vec)
9660 	    {
9661 	      err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
9662 	      first = name;
9663 	      second = this->last_vec_;
9664 	    }
9665 	  if (err)
9666 	    {
9667 	      if (parameters->options().warn_mismatch())
9668 		gold_error(_(err), first, second);
9669 	      out_attr[tag].set_type(0);
9670 	    }
9671 	}
9672 
9673       tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
9674       int in_struct = in_attr[tag].int_value() & 3;
9675       int out_struct = out_attr[tag].int_value() & 3;
9676       if (in_struct != out_struct)
9677 	{
9678 	  err = NULL;
9679 	  if (in_struct == 0 || in_struct == 3)
9680 	    ;
9681 	  else if (out_struct == 0)
9682 	    {
9683 	      out_struct = in_struct;
9684 	      out_attr[tag].set_int_value(out_struct);
9685 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
9686 	      this->last_struct_ = name;
9687 	    }
9688 	  else if (out_struct < in_struct)
9689 	    {
9690 	      err = N_("%s uses r3/r4 for small structure returns, "
9691 		       "%s uses memory");
9692 	      first = this->last_struct_;
9693 	      second = name;
9694 	    }
9695 	  else if (out_struct > in_struct)
9696 	    {
9697 	      err = N_("%s uses r3/r4 for small structure returns, "
9698 		       "%s uses memory");
9699 	      first = name;
9700 	      second = this->last_struct_;
9701 	    }
9702 	  if (err)
9703 	    {
9704 	      if (parameters->options().warn_mismatch())
9705 		gold_error(_(err), first, second);
9706 	      out_attr[tag].set_type(0);
9707 	    }
9708 	}
9709     }
9710 
9711   // Merge Tag_compatibility attributes and any common GNU ones.
9712   this->attributes_section_data_->merge(name, pasd);
9713 }
9714 
9715 // Emit any saved relocs, and mark toc entries using any of these
9716 // relocs as not optimizable.
9717 
9718 template<int sh_type, int size, bool big_endian>
9719 void
9720 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
9721     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
9722 {
9723   if (size == 64
9724       && parameters->options().toc_optimize())
9725     {
9726       for (typename Copy_relocs<sh_type, size, big_endian>::
9727 	     Copy_reloc_entries::iterator p = this->entries_.begin();
9728 	   p != this->entries_.end();
9729 	   ++p)
9730 	{
9731 	  typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
9732 	    entry = *p;
9733 
9734 	  // If the symbol is no longer defined in a dynamic object,
9735 	  // then we emitted a COPY relocation.  If it is still
9736 	  // dynamic then we'll need dynamic relocations and thus
9737 	  // can't optimize toc entries.
9738 	  if (entry.sym_->is_from_dynobj())
9739 	    {
9740 	      Powerpc_relobj<size, big_endian>* ppc_object
9741 		= static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
9742 	      if (entry.shndx_ == ppc_object->toc_shndx())
9743 		ppc_object->set_no_toc_opt(entry.address_);
9744 	    }
9745 	}
9746     }
9747 
9748   Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
9749 }
9750 
9751 // Return the value to use for a branch relocation.
9752 
9753 template<int size, bool big_endian>
9754 bool
9755 Target_powerpc<size, big_endian>::symval_for_branch(
9756     const Symbol_table* symtab,
9757     const Sized_symbol<size>* gsym,
9758     Powerpc_relobj<size, big_endian>* object,
9759     Address *value,
9760     unsigned int *dest_shndx)
9761 {
9762   if (size == 32 || this->abiversion() >= 2)
9763     gold_unreachable();
9764   *dest_shndx = 0;
9765 
9766   // If the symbol is defined in an opd section, ie. is a function
9767   // descriptor, use the function descriptor code entry address
9768   Powerpc_relobj<size, big_endian>* symobj = object;
9769   if (gsym != NULL
9770       && (gsym->source() != Symbol::FROM_OBJECT
9771 	  || gsym->object()->is_dynamic()))
9772     return true;
9773   if (gsym != NULL)
9774     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9775   unsigned int shndx = symobj->opd_shndx();
9776   if (shndx == 0)
9777     return true;
9778   Address opd_addr = symobj->get_output_section_offset(shndx);
9779   if (opd_addr == invalid_address)
9780     return true;
9781   opd_addr += symobj->output_section_address(shndx);
9782   if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
9783     {
9784       Address sec_off;
9785       *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
9786       if (symtab->is_section_folded(symobj, *dest_shndx))
9787 	{
9788 	  Section_id folded
9789 	    = symtab->icf()->get_folded_section(symobj, *dest_shndx);
9790 	  symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
9791 	  *dest_shndx = folded.second;
9792 	}
9793       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
9794       if (sec_addr == invalid_address)
9795 	return false;
9796 
9797       sec_addr += symobj->output_section(*dest_shndx)->address();
9798       *value = sec_addr + sec_off;
9799     }
9800   return true;
9801 }
9802 
9803 template<int size>
9804 static bool
9805 relative_value_is_known(const Sized_symbol<size>* gsym)
9806 {
9807   if (gsym->type() == elfcpp::STT_GNU_IFUNC)
9808     return false;
9809 
9810   if (gsym->is_from_dynobj()
9811       || gsym->is_undefined()
9812       || gsym->is_preemptible())
9813     return false;
9814 
9815   if (gsym->is_absolute())
9816     return !parameters->options().output_is_position_independent();
9817 
9818   return true;
9819 }
9820 
9821 template<int size>
9822 static bool
9823 relative_value_is_known(const Symbol_value<size>* psymval)
9824 {
9825   if (psymval->is_ifunc_symbol())
9826     return false;
9827 
9828   bool is_ordinary;
9829   unsigned int shndx = psymval->input_shndx(&is_ordinary);
9830 
9831   return is_ordinary && shndx != elfcpp::SHN_UNDEF;
9832 }
9833 
9834 // PCREL_OPT in one instance flags to the linker that a pair of insns:
9835 //   pld ra,symbol@got@pcrel
9836 //   load/store rt,0(ra)
9837 // or
9838 //   pla ra,symbol@pcrel
9839 //   load/store rt,0(ra)
9840 // may be translated to
9841 //   pload/pstore rt,symbol@pcrel
9842 //   nop.
9843 // This function returns true if the optimization is possible, placing
9844 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
9845 //
9846 // On entry to this function, the linker has already determined that
9847 // the pld can be replaced with pla: *PINSN1 is that pla insn,
9848 // while *PINSN2 is the second instruction.
9849 
9850 inline bool
9851 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
9852 {
9853   uint32_t insn2 = *pinsn2 >> 32;
9854   uint64_t i1new;
9855 
9856   // Check that regs match.
9857   if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
9858     return false;
9859 
9860   switch ((insn2 >> 26) & 63)
9861     {
9862     default:
9863       return false;
9864 
9865     case 32: // lwz
9866     case 34: // lbz
9867     case 36: // stw
9868     case 38: // stb
9869     case 40: // lhz
9870     case 42: // lha
9871     case 44: // sth
9872     case 48: // lfs
9873     case 50: // lfd
9874     case 52: // stfs
9875     case 54: // stfd
9876       // These are the PMLS cases, where we just need to tack a prefix
9877       // on the insn.  Check that the D field is zero.
9878       if ((insn2 & 0xffff) != 0)
9879 	return false;
9880       i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
9881 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9882       break;
9883 
9884     case 58: // lwa, ld
9885       if ((insn2 & 0xfffd) != 0)
9886 	return false;
9887       i1new = ((1ULL << 58) | (1ULL << 52)
9888 	       | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
9889 	       | (insn2 & (31ULL << 21)));
9890       break;
9891 
9892     case 57: // lxsd, lxssp
9893       if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
9894 	return false;
9895       i1new = ((1ULL << 58) | (1ULL << 52)
9896 	       | ((40ULL | (insn2 & 3)) << 26)
9897 	       | (insn2 & (31ULL << 21)));
9898       break;
9899 
9900     case 61: // stxsd, stxssp, lxv, stxv
9901       if ((insn2 & 3) == 0)
9902 	return false;
9903       else if ((insn2 & 3) >= 2)
9904 	{
9905 	  if ((insn2 & 0xfffc) != 0)
9906 	    return false;
9907 	  i1new = ((1ULL << 58) | (1ULL << 52)
9908 		   | ((44ULL | (insn2 & 3)) << 26)
9909 		   | (insn2 & (31ULL << 21)));
9910 	}
9911       else
9912 	{
9913 	  if ((insn2 & 0xfff0) != 0)
9914 	    return false;
9915 	  i1new = ((1ULL << 58) | (1ULL << 52)
9916 		   | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
9917 		   | (insn2 & (31ULL << 21)));
9918 	}
9919       break;
9920 
9921     case 56: // lq
9922       if ((insn2 & 0xffff) != 0)
9923 	return false;
9924       i1new = ((1ULL << 58) | (1ULL << 52)
9925 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
9926       break;
9927 
9928     case 62: // std, stq
9929       if ((insn2 & 0xfffd) != 0)
9930 	return false;
9931       i1new = ((1ULL << 58) | (1ULL << 52)
9932 	       | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
9933 	       | (insn2 & (31ULL << 21)));
9934       break;
9935     }
9936 
9937   *pinsn1 = i1new;
9938   *pinsn2 = (uint64_t) nop << 32;
9939   return true;
9940 }
9941 
9942 // Perform a relocation.
9943 
9944 template<int size, bool big_endian>
9945 inline bool
9946 Target_powerpc<size, big_endian>::Relocate::relocate(
9947     const Relocate_info<size, big_endian>* relinfo,
9948     unsigned int,
9949     Target_powerpc* target,
9950     Output_section* os,
9951     size_t relnum,
9952     const unsigned char* preloc,
9953     const Sized_symbol<size>* gsym,
9954     const Symbol_value<size>* psymval,
9955     unsigned char* view,
9956     Address address,
9957     section_size_type view_size)
9958 {
9959   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
9960   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
9961   typedef typename elfcpp::Rela<size, big_endian> Reltype;
9962 
9963   if (view == NULL)
9964     return true;
9965 
9966   if (target->replace_tls_get_addr(gsym))
9967     gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
9968 
9969   const elfcpp::Rela<size, big_endian> rela(preloc);
9970   unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
9971   switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
9972     {
9973     case Track_tls::NOT_EXPECTED:
9974       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
9975 			     _("__tls_get_addr call lacks marker reloc"));
9976       break;
9977     case Track_tls::EXPECTED:
9978       // We have already complained.
9979       break;
9980     case Track_tls::SKIP:
9981       if (is_plt16_reloc<size>(r_type)
9982 	  || r_type == elfcpp::R_POWERPC_PLTSEQ
9983 	  || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
9984 	{
9985 	  Insn* iview = reinterpret_cast<Insn*>(view);
9986 	  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
9987 	}
9988       else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
9989 	{
9990 	  Insn* iview = reinterpret_cast<Insn*>(view);
9991 	  elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
9992 	}
9993       else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
9994 			      || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
9995 	{
9996 	  Insn* iview = reinterpret_cast<Insn*>(view);
9997 	  elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
9998 	  elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
9999 	}
10000       return true;
10001     case Track_tls::NORMAL:
10002       break;
10003     }
10004 
10005   // Offset from start of insn to d-field reloc.
10006   const int d_offset = big_endian ? 2 : 0;
10007 
10008   Powerpc_relobj<size, big_endian>* const object
10009     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10010   Address value = 0;
10011   bool has_stub_value = false;
10012   bool localentry0 = false;
10013   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10014   bool has_plt_offset
10015     = (gsym != NULL
10016        ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10017        : object->local_has_plt_offset(r_sym));
10018   if (has_plt_offset
10019       && !is_plt16_reloc<size>(r_type)
10020       && r_type != elfcpp::R_PPC64_PLT_PCREL34
10021       && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10022       && r_type != elfcpp::R_POWERPC_PLTSEQ
10023       && r_type != elfcpp::R_POWERPC_PLTCALL
10024       && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10025       && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
10026       && (!psymval->is_ifunc_symbol()
10027 	  || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
10028     {
10029       if (size == 64
10030 	  && gsym != NULL
10031 	  && target->abiversion() >= 2
10032 	  && !parameters->options().output_is_position_independent()
10033 	  && !is_branch_reloc<size>(r_type))
10034 	{
10035 	  Address off = target->glink_section()->find_global_entry(gsym);
10036 	  if (off != invalid_address)
10037 	    {
10038 	      value = target->glink_section()->global_entry_address() + off;
10039 	      has_stub_value = true;
10040 	    }
10041 	}
10042       else
10043 	{
10044 	  Stub_table<size, big_endian>* stub_table = NULL;
10045 	  if (target->stub_tables().size() == 1)
10046 	    stub_table = target->stub_tables()[0];
10047 	  if (stub_table == NULL
10048 	      && !(size == 32
10049 		   && gsym != NULL
10050 		   && !parameters->options().output_is_position_independent()
10051 		   && !is_branch_reloc<size>(r_type)))
10052 	    stub_table = object->stub_table(relinfo->data_shndx);
10053 	  if (stub_table == NULL)
10054 	    {
10055 	      // This is a ref from a data section to an ifunc symbol,
10056 	      // or a non-branch reloc for which we always want to use
10057 	      // one set of stubs for resolving function addresses.
10058 	      if (target->stub_tables().size() != 0)
10059 		stub_table = target->stub_tables()[0];
10060 	    }
10061 	  if (stub_table != NULL)
10062 	    {
10063 	      const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10064 	      if (gsym != NULL)
10065 		ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10066 						      rela.get_r_addend());
10067 	      else
10068 		ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10069 						      rela.get_r_addend());
10070 	      if (ent != NULL)
10071 		{
10072 		  value = stub_table->stub_address() + ent->off_;
10073 		  const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10074 		  elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10075 		  size_t reloc_count = shdr.get_sh_size() / reloc_size;
10076 		  if (size == 64
10077 		      && ent->r2save_
10078 		      && r_type == elfcpp::R_PPC64_REL24_NOTOC)
10079 		    value += 4;
10080 		  else if (size == 64
10081 			   && ent->r2save_
10082 			   && relnum < reloc_count - 1)
10083 		    {
10084 		      Reltype next_rela(preloc + reloc_size);
10085 		      if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10086 			  == elfcpp::R_PPC64_TOCSAVE
10087 			  && next_rela.get_r_offset() == rela.get_r_offset() + 4)
10088 			value += 4;
10089 		    }
10090 		  localentry0 = ent->localentry0_;
10091 		  has_stub_value = true;
10092 		}
10093 	    }
10094 	}
10095       // We don't care too much about bogus debug references to
10096       // non-local functions, but otherwise there had better be a plt
10097       // call stub or global entry stub as appropriate.
10098       gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10099     }
10100 
10101   if (has_plt_offset && (is_plt16_reloc<size>(r_type)
10102 			 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10103 			 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10104     {
10105       const Output_data_plt_powerpc<size, big_endian>* plt;
10106       if (gsym)
10107 	value = target->plt_off(gsym, &plt);
10108       else
10109 	value = target->plt_off(object, r_sym, &plt);
10110       value += plt->address();
10111 
10112       if (size == 64)
10113 	{
10114 	  if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10115 	      && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10116 	    value -= (target->got_section()->output_section()->address()
10117 		      + object->toc_base_offset());
10118 	}
10119       else if (parameters->options().output_is_position_independent())
10120 	{
10121 	  if (rela.get_r_addend() >= 32768)
10122 	    {
10123 	      unsigned int got2 = object->got2_shndx();
10124 	      value -= (object->get_output_section_offset(got2)
10125 			+ object->output_section(got2)->address()
10126 			+ rela.get_r_addend());
10127 	    }
10128 	  else
10129 	    value -= (target->got_section()->address()
10130 		      + target->got_section()->g_o_t());
10131 	}
10132     }
10133   else if (!has_plt_offset
10134 	   && (is_plt16_reloc<size>(r_type)
10135 	       || r_type == elfcpp::R_POWERPC_PLTSEQ
10136 	       || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10137     {
10138       Insn* iview = reinterpret_cast<Insn*>(view);
10139       elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10140       r_type = elfcpp::R_POWERPC_NONE;
10141     }
10142   else if (!has_plt_offset
10143 	   && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10144 	       || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10145     {
10146       Insn* iview = reinterpret_cast<Insn*>(view);
10147       elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10148       elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10149       r_type = elfcpp::R_POWERPC_NONE;
10150     }
10151   else if (r_type == elfcpp::R_POWERPC_GOT16
10152 	   || r_type == elfcpp::R_POWERPC_GOT16_LO
10153 	   || r_type == elfcpp::R_POWERPC_GOT16_HI
10154 	   || r_type == elfcpp::R_POWERPC_GOT16_HA
10155 	   || r_type == elfcpp::R_PPC64_GOT16_DS
10156 	   || r_type == elfcpp::R_PPC64_GOT16_LO_DS
10157 	   || r_type == elfcpp::R_PPC64_GOT_PCREL34)
10158     {
10159       if (gsym != NULL)
10160 	{
10161 	  gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
10162 	  value = gsym->got_offset(GOT_TYPE_STANDARD);
10163 	}
10164       else
10165 	{
10166 	  gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
10167 	  value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
10168 	}
10169       if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10170 	value += target->got_section()->address();
10171       else
10172 	value -= target->got_section()->got_base_offset(object);
10173     }
10174   else if (r_type == elfcpp::R_PPC64_TOC)
10175     {
10176       value = (target->got_section()->output_section()->address()
10177 	       + object->toc_base_offset());
10178     }
10179   else if (gsym != NULL
10180 	   && (r_type == elfcpp::R_POWERPC_REL24
10181 	       || r_type == elfcpp::R_PPC_PLTREL24)
10182 	   && has_stub_value)
10183     {
10184       if (size == 64)
10185 	{
10186 	  typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10187 	  Valtype* wv = reinterpret_cast<Valtype*>(view);
10188 	  bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10189 	  if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10190 	    {
10191 	      Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10192 	      Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10193 	      if ((insn & 1) != 0
10194 		  && (insn2 == nop
10195 		      || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10196 		{
10197 		  elfcpp::Swap<32, big_endian>::
10198 		    writeval(wv + 1, ld_2_1 + target->stk_toc());
10199 		  can_plt_call = true;
10200 		}
10201 	    }
10202 	  if (!can_plt_call)
10203 	    {
10204 	      // If we don't have a branch and link followed by a nop,
10205 	      // we can't go via the plt because there is no place to
10206 	      // put a toc restoring instruction.
10207 	      // Unless we know we won't be returning.
10208 	      if (strcmp(gsym->name(), "__libc_start_main") == 0)
10209 		can_plt_call = true;
10210 	    }
10211 	  if (!can_plt_call)
10212 	    {
10213 	      // g++ as of 20130507 emits self-calls without a
10214 	      // following nop.  This is arguably wrong since we have
10215 	      // conflicting information.  On the one hand a global
10216 	      // symbol and on the other a local call sequence, but
10217 	      // don't error for this special case.
10218 	      // It isn't possible to cheaply verify we have exactly
10219 	      // such a call.  Allow all calls to the same section.
10220 	      bool ok = false;
10221 	      Address code = value;
10222 	      if (gsym->source() == Symbol::FROM_OBJECT
10223 		  && gsym->object() == object)
10224 		{
10225 		  unsigned int dest_shndx = 0;
10226 		  if (target->abiversion() < 2)
10227 		    {
10228 		      Address addend = rela.get_r_addend();
10229 		      code = psymval->value(object, addend);
10230 		      target->symval_for_branch(relinfo->symtab, gsym, object,
10231 						&code, &dest_shndx);
10232 		    }
10233 		  bool is_ordinary;
10234 		  if (dest_shndx == 0)
10235 		    dest_shndx = gsym->shndx(&is_ordinary);
10236 		  ok = dest_shndx == relinfo->data_shndx;
10237 		}
10238 	      if (!ok)
10239 		{
10240 		  gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10241 					 _("call lacks nop, can't restore toc; "
10242 					   "recompile with -fPIC"));
10243 		  value = code;
10244 		}
10245 	    }
10246 	}
10247     }
10248   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10249 	   || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10250 	   || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10251 	   || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10252 	   || r_type == elfcpp::R_PPC64_GOT_TLSGD34)
10253     {
10254       // First instruction of a global dynamic sequence, arg setup insn.
10255       const bool final = gsym == NULL || gsym->final_value_is_known();
10256       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10257       enum Got_type got_type = GOT_TYPE_STANDARD;
10258       if (tls_type == tls::TLSOPT_NONE)
10259 	got_type = GOT_TYPE_TLSGD;
10260       else if (tls_type == tls::TLSOPT_TO_IE)
10261 	got_type = GOT_TYPE_TPREL;
10262       if (got_type != GOT_TYPE_STANDARD)
10263 	{
10264 	  if (gsym != NULL)
10265 	    {
10266 	      gold_assert(gsym->has_got_offset(got_type));
10267 	      value = gsym->got_offset(got_type);
10268 	    }
10269 	  else
10270 	    {
10271 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
10272 	      value = object->local_got_offset(r_sym, got_type);
10273 	    }
10274 	  if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
10275 	    value += target->got_section()->address();
10276 	  else
10277 	    value -= target->got_section()->got_base_offset(object);
10278 	}
10279       if (tls_type == tls::TLSOPT_TO_IE)
10280 	{
10281 	  if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
10282 	    {
10283 	      Insn* iview = reinterpret_cast<Insn*>(view);
10284 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10285 	      pinsn <<= 32;
10286 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10287 	      // pla -> pld
10288 	      pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10289 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10290 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10291 						     pinsn & 0xffffffff);
10292 	      r_type = elfcpp::R_PPC64_GOT_TPREL34;
10293 	    }
10294 	  else
10295 	    {
10296 	      if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10297 		  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10298 		{
10299 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10300 		  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10301 		  insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10302 		  if (size == 32)
10303 		    insn |= 32 << 26; // lwz
10304 		  else
10305 		    insn |= 58 << 26; // ld
10306 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10307 		}
10308 	      r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10309 			 - elfcpp::R_POWERPC_GOT_TLSGD16);
10310 	    }
10311 	}
10312       else if (tls_type == tls::TLSOPT_TO_LE)
10313 	{
10314 	  if (r_type == elfcpp::R_PPC64_GOT_TLSGD34)
10315 	    {
10316 	      Insn* iview = reinterpret_cast<Insn*>(view);
10317 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10318 	      pinsn <<= 32;
10319 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10320 	      // pla pcrel -> paddi r13
10321 	      pinsn += (-1ULL << 52) + (13ULL << 16);
10322 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10323 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10324 						     pinsn & 0xffffffff);
10325 	      r_type = elfcpp::R_PPC64_TPREL34;
10326 	      value = psymval->value(object, rela.get_r_addend());
10327 	    }
10328 	  else
10329 	    {
10330 	      if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10331 		  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10332 		{
10333 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10334 		  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10335 		  insn &= (1 << 26) - (1 << 21); // extract rt
10336 		  if (size == 32)
10337 		    insn |= addis_0_2;
10338 		  else
10339 		    insn |= addis_0_13;
10340 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10341 		  r_type = elfcpp::R_POWERPC_TPREL16_HA;
10342 		  value = psymval->value(object, rela.get_r_addend());
10343 		}
10344 	      else
10345 		{
10346 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10347 		  Insn insn = nop;
10348 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10349 		  r_type = elfcpp::R_POWERPC_NONE;
10350 		}
10351 	    }
10352 	}
10353     }
10354   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10355 	   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
10356 	   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
10357 	   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
10358 	   || r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10359     {
10360       // First instruction of a local dynamic sequence, arg setup insn.
10361       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10362       if (tls_type == tls::TLSOPT_NONE)
10363 	{
10364 	  value = target->tlsld_got_offset();
10365 	  if (r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10366 	    value += target->got_section()->address();
10367 	  else
10368 	    value -= target->got_section()->got_base_offset(object);
10369 	}
10370       else
10371 	{
10372 	  gold_assert(tls_type == tls::TLSOPT_TO_LE);
10373 	  if (r_type == elfcpp::R_PPC64_GOT_TLSLD34)
10374 	    {
10375 	      Insn* iview = reinterpret_cast<Insn*>(view);
10376 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10377 	      pinsn <<= 32;
10378 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10379 	      // pla pcrel -> paddi r13
10380 	      pinsn += (-1ULL << 52) + (13ULL << 16);
10381 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10382 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10383 						     pinsn & 0xffffffff);
10384 	      r_type = elfcpp::R_PPC64_TPREL34;
10385 	      value = dtp_offset;
10386 	    }
10387 	  else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
10388 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
10389 	    {
10390 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10391 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10392 	      insn &= (1 << 26) - (1 << 21); // extract rt
10393 	      if (size == 32)
10394 		insn |= addis_0_2;
10395 	      else
10396 		insn |= addis_0_13;
10397 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10398 	      r_type = elfcpp::R_POWERPC_TPREL16_HA;
10399 	      value = dtp_offset;
10400 	    }
10401 	  else
10402 	    {
10403 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10404 	      Insn insn = nop;
10405 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10406 	      r_type = elfcpp::R_POWERPC_NONE;
10407 	    }
10408 	}
10409     }
10410   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
10411 	   || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
10412 	   || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
10413 	   || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
10414 	   || r_type == elfcpp::R_PPC64_GOT_DTPREL34)
10415     {
10416       // Accesses relative to a local dynamic sequence address,
10417       // no optimisation here.
10418       if (gsym != NULL)
10419 	{
10420 	  gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
10421 	  value = gsym->got_offset(GOT_TYPE_DTPREL);
10422 	}
10423       else
10424 	{
10425 	  gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
10426 	  value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
10427 	}
10428       if (r_type == elfcpp::R_PPC64_GOT_DTPREL34)
10429 	value += target->got_section()->address();
10430       else
10431 	value -= target->got_section()->got_base_offset(object);
10432     }
10433   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10434 	   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
10435 	   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
10436 	   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
10437 	   || r_type == elfcpp::R_PPC64_GOT_TPREL34)
10438     {
10439       // First instruction of initial exec sequence.
10440       const bool final = gsym == NULL || gsym->final_value_is_known();
10441       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10442       if (tls_type == tls::TLSOPT_NONE)
10443 	{
10444 	  if (gsym != NULL)
10445 	    {
10446 	      gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
10447 	      value = gsym->got_offset(GOT_TYPE_TPREL);
10448 	    }
10449 	  else
10450 	    {
10451 	      gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
10452 	      value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
10453 	    }
10454 	  if (r_type == elfcpp::R_PPC64_GOT_TPREL34)
10455 	    value += target->got_section()->address();
10456 	  else
10457 	    value -= target->got_section()->got_base_offset(object);
10458 	}
10459       else
10460 	{
10461 	  gold_assert(tls_type == tls::TLSOPT_TO_LE);
10462 	  if (r_type == elfcpp::R_PPC64_GOT_TPREL34)
10463 	    {
10464 	      Insn* iview = reinterpret_cast<Insn*>(view);
10465 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10466 	      pinsn <<= 32;
10467 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10468 	      // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
10469 	      pinsn += ((2ULL << 56) + (-1ULL << 52)
10470 			+ (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
10471 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10472 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10473 						     pinsn & 0xffffffff);
10474 	      r_type = elfcpp::R_PPC64_TPREL34;
10475 	      value = psymval->value(object, rela.get_r_addend());
10476 	    }
10477 	  else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
10478 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
10479 	    {
10480 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10481 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10482 	      insn &= (1 << 26) - (1 << 21); // extract rt from ld
10483 	      if (size == 32)
10484 		insn |= addis_0_2;
10485 	      else
10486 		insn |= addis_0_13;
10487 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10488 	      r_type = elfcpp::R_POWERPC_TPREL16_HA;
10489 	      value = psymval->value(object, rela.get_r_addend());
10490 	    }
10491 	  else
10492 	    {
10493 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10494 	      Insn insn = nop;
10495 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10496 	      r_type = elfcpp::R_POWERPC_NONE;
10497 	    }
10498 	}
10499     }
10500   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
10501 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
10502     {
10503       // Second instruction of a global dynamic sequence,
10504       // the __tls_get_addr call
10505       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10506       const bool final = gsym == NULL || gsym->final_value_is_known();
10507       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10508       if (tls_type != tls::TLSOPT_NONE)
10509 	{
10510 	  if (tls_type == tls::TLSOPT_TO_IE)
10511 	    {
10512 	      Insn* iview = reinterpret_cast<Insn*>(view);
10513 	      Insn insn = add_3_3_13;
10514 	      if (size == 32)
10515 		insn = add_3_3_2;
10516 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10517 	      r_type = elfcpp::R_POWERPC_NONE;
10518 	    }
10519 	  else
10520 	    {
10521 	      bool is_pcrel = false;
10522 	      const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10523 	      elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10524 	      size_t reloc_count = shdr.get_sh_size() / reloc_size;
10525 	      if (relnum < reloc_count - 1)
10526 		{
10527 		  Reltype next_rela(preloc + reloc_size);
10528 		  unsigned int r_type2
10529 		    = elfcpp::elf_r_type<size>(next_rela.get_r_info());
10530 		  if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10531 		       || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10532 		      && next_rela.get_r_offset() == rela.get_r_offset())
10533 		    is_pcrel = true;
10534 		}
10535 	      Insn* iview = reinterpret_cast<Insn*>(view);
10536 	      if (is_pcrel)
10537 		{
10538 		  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10539 		  r_type = elfcpp::R_POWERPC_NONE;
10540 		}
10541 	      else
10542 		{
10543 		  elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10544 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
10545 		  view += d_offset;
10546 		  value = psymval->value(object, rela.get_r_addend());
10547 		}
10548 	    }
10549 	  this->skip_next_tls_get_addr_call();
10550 	}
10551     }
10552   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
10553 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
10554     {
10555       // Second instruction of a local dynamic sequence,
10556       // the __tls_get_addr call
10557       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
10558       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
10559       if (tls_type == tls::TLSOPT_TO_LE)
10560 	{
10561 	  bool is_pcrel = false;
10562 	  const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10563 	  elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10564 	  size_t reloc_count = shdr.get_sh_size() / reloc_size;
10565 	  if (relnum < reloc_count - 1)
10566 	    {
10567 	      Reltype next_rela(preloc + reloc_size);
10568 	      unsigned int r_type2
10569 		= elfcpp::elf_r_type<size>(next_rela.get_r_info());
10570 	      if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
10571 		   || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
10572 		  && next_rela.get_r_offset() == rela.get_r_offset())
10573 		is_pcrel = true;
10574 	    }
10575 	  Insn* iview = reinterpret_cast<Insn*>(view);
10576 	  if (is_pcrel)
10577 	    {
10578 	      elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10579 	      r_type = elfcpp::R_POWERPC_NONE;
10580 	    }
10581 	  else
10582 	    {
10583 	      elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
10584 	      r_type = elfcpp::R_POWERPC_TPREL16_LO;
10585 	      view += d_offset;
10586 	      value = dtp_offset;
10587 	    }
10588 	  this->skip_next_tls_get_addr_call();
10589 	}
10590     }
10591   else if (r_type == elfcpp::R_POWERPC_TLS)
10592     {
10593       // Second instruction of an initial exec sequence
10594       const bool final = gsym == NULL || gsym->final_value_is_known();
10595       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
10596       if (tls_type == tls::TLSOPT_TO_LE)
10597 	{
10598 	  Address roff = rela.get_r_offset() & 3;
10599 	  Insn* iview = reinterpret_cast<Insn*>(view - roff);
10600 	  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10601 	  unsigned int reg = size == 32 ? 2 : 13;
10602 	  insn = at_tls_transform(insn, reg);
10603 	  gold_assert(insn != 0);
10604 	  if (roff == 0)
10605 	    {
10606 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10607 	      r_type = elfcpp::R_POWERPC_TPREL16_LO;
10608 	      view += d_offset;
10609 	      value = psymval->value(object, rela.get_r_addend());
10610 	    }
10611 	  else if (roff == 1)
10612 	    {
10613 	      // For pcrel IE to LE we already have the full offset
10614 	      // and thus don't need an addi here.  A nop or mr will do.
10615 	      if ((insn & (0x3f << 26)) == 14 << 26)
10616 		{
10617 		  // Extract regs from addi rt,ra,si.
10618 		  unsigned int rt = (insn >> 21) & 0x1f;
10619 		  unsigned int ra = (insn >> 16) & 0x1f;
10620 		  if (rt == ra)
10621 		    insn = nop;
10622 		  else
10623 		    {
10624 		      // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
10625 		      insn = (rt << 16) | (ra << 21) | (ra << 11);
10626 		      insn |= (31u << 26) | (444u << 1);
10627 		    }
10628 		}
10629 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10630 	      r_type = elfcpp::R_POWERPC_NONE;
10631 	    }
10632 	}
10633     }
10634   else if (!has_stub_value)
10635     {
10636       if (!has_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
10637 			      || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
10638 	{
10639 	  // PLTCALL without plt entry => convert to direct call
10640 	  Insn* iview = reinterpret_cast<Insn*>(view);
10641 	  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10642 	  insn = (insn & 1) | b;
10643 	  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10644 	  if (size == 32)
10645 	    r_type = elfcpp::R_PPC_PLTREL24;
10646 	  else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10647 	    r_type = elfcpp::R_PPC64_REL24_NOTOC;
10648 	  else
10649 	    r_type = elfcpp::R_POWERPC_REL24;
10650 	}
10651       Address addend = 0;
10652       if (!(size == 32
10653 	    && (r_type == elfcpp::R_PPC_PLTREL24
10654 		|| r_type == elfcpp::R_POWERPC_PLT16_LO
10655 		|| r_type == elfcpp::R_POWERPC_PLT16_HI
10656 		|| r_type == elfcpp::R_POWERPC_PLT16_HA)))
10657 	addend = rela.get_r_addend();
10658       value = psymval->value(object, addend);
10659       if (size == 64 && is_branch_reloc<size>(r_type))
10660 	{
10661 	  if (target->abiversion() >= 2)
10662 	    {
10663 	      if (gsym != NULL)
10664 		value += object->ppc64_local_entry_offset(gsym);
10665 	      else
10666 		value += object->ppc64_local_entry_offset(r_sym);
10667 	    }
10668 	  else
10669 	    {
10670 	      unsigned int dest_shndx;
10671 	      target->symval_for_branch(relinfo->symtab, gsym, object,
10672 					&value, &dest_shndx);
10673 	    }
10674 	}
10675       Address max_branch_offset = max_branch_delta<size>(r_type);
10676       if (max_branch_offset != 0
10677 	  && (value - address + max_branch_offset >= 2 * max_branch_offset
10678 	      || (size == 64
10679 		  && r_type == elfcpp::R_PPC64_REL24_NOTOC
10680 		  && (gsym != NULL
10681 		      ? object->ppc64_needs_toc(gsym)
10682 		      : object->ppc64_needs_toc(r_sym)))))
10683 	{
10684 	  Stub_table<size, big_endian>* stub_table
10685 	    = object->stub_table(relinfo->data_shndx);
10686 	  if (stub_table != NULL)
10687 	    {
10688 	      const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
10689 		= stub_table->find_long_branch_entry(object, value);
10690 	      if (ent != NULL)
10691 		{
10692 		  if (ent->save_res_)
10693 		    value = (value - target->savres_section()->address()
10694 			     + stub_table->branch_size());
10695 		  else
10696 		    value = (stub_table->stub_address() + stub_table->plt_size()
10697 			     + ent->off_);
10698 		  has_stub_value = true;
10699 		}
10700 	    }
10701 	}
10702     }
10703 
10704   switch (r_type)
10705     {
10706     case elfcpp::R_PPC64_REL24_NOTOC:
10707       if (size == 32)
10708 	break;
10709       // Fall through.
10710     case elfcpp::R_PPC64_REL64:
10711     case elfcpp::R_POWERPC_REL32:
10712     case elfcpp::R_POWERPC_REL24:
10713     case elfcpp::R_PPC_PLTREL24:
10714     case elfcpp::R_PPC_LOCAL24PC:
10715     case elfcpp::R_POWERPC_REL16:
10716     case elfcpp::R_POWERPC_REL16_LO:
10717     case elfcpp::R_POWERPC_REL16_HI:
10718     case elfcpp::R_POWERPC_REL16_HA:
10719     case elfcpp::R_POWERPC_REL16DX_HA:
10720     case elfcpp::R_PPC64_REL16_HIGH:
10721     case elfcpp::R_PPC64_REL16_HIGHA:
10722     case elfcpp::R_PPC64_REL16_HIGHER:
10723     case elfcpp::R_PPC64_REL16_HIGHERA:
10724     case elfcpp::R_PPC64_REL16_HIGHEST:
10725     case elfcpp::R_PPC64_REL16_HIGHESTA:
10726     case elfcpp::R_POWERPC_REL14:
10727     case elfcpp::R_POWERPC_REL14_BRTAKEN:
10728     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10729     case elfcpp::R_PPC64_PCREL34:
10730     case elfcpp::R_PPC64_GOT_PCREL34:
10731     case elfcpp::R_PPC64_PLT_PCREL34:
10732     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
10733     case elfcpp::R_PPC64_PCREL28:
10734     case elfcpp::R_PPC64_GOT_TLSGD34:
10735     case elfcpp::R_PPC64_GOT_TLSLD34:
10736     case elfcpp::R_PPC64_GOT_TPREL34:
10737     case elfcpp::R_PPC64_GOT_DTPREL34:
10738     case elfcpp::R_PPC64_REL16_HIGHER34:
10739     case elfcpp::R_PPC64_REL16_HIGHERA34:
10740     case elfcpp::R_PPC64_REL16_HIGHEST34:
10741     case elfcpp::R_PPC64_REL16_HIGHESTA34:
10742       value -= address;
10743       break;
10744 
10745     case elfcpp::R_PPC64_TOC16:
10746     case elfcpp::R_PPC64_TOC16_LO:
10747     case elfcpp::R_PPC64_TOC16_HI:
10748     case elfcpp::R_PPC64_TOC16_HA:
10749     case elfcpp::R_PPC64_TOC16_DS:
10750     case elfcpp::R_PPC64_TOC16_LO_DS:
10751       // Subtract the TOC base address.
10752       value -= (target->got_section()->output_section()->address()
10753 		+ object->toc_base_offset());
10754       break;
10755 
10756     case elfcpp::R_POWERPC_SECTOFF:
10757     case elfcpp::R_POWERPC_SECTOFF_LO:
10758     case elfcpp::R_POWERPC_SECTOFF_HI:
10759     case elfcpp::R_POWERPC_SECTOFF_HA:
10760     case elfcpp::R_PPC64_SECTOFF_DS:
10761     case elfcpp::R_PPC64_SECTOFF_LO_DS:
10762       if (os != NULL)
10763 	value -= os->address();
10764       break;
10765 
10766     case elfcpp::R_PPC64_TPREL16_DS:
10767     case elfcpp::R_PPC64_TPREL16_LO_DS:
10768     case elfcpp::R_PPC64_TPREL16_HIGH:
10769     case elfcpp::R_PPC64_TPREL16_HIGHA:
10770       if (size != 64)
10771 	// R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
10772 	break;
10773       // Fall through.
10774     case elfcpp::R_POWERPC_TPREL16:
10775     case elfcpp::R_POWERPC_TPREL16_LO:
10776     case elfcpp::R_POWERPC_TPREL16_HI:
10777     case elfcpp::R_POWERPC_TPREL16_HA:
10778     case elfcpp::R_POWERPC_TPREL:
10779     case elfcpp::R_PPC64_TPREL16_HIGHER:
10780     case elfcpp::R_PPC64_TPREL16_HIGHERA:
10781     case elfcpp::R_PPC64_TPREL16_HIGHEST:
10782     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
10783     case elfcpp::R_PPC64_TPREL34:
10784       // tls symbol values are relative to tls_segment()->vaddr()
10785       value -= tp_offset;
10786       break;
10787 
10788     case elfcpp::R_PPC64_DTPREL16_DS:
10789     case elfcpp::R_PPC64_DTPREL16_LO_DS:
10790     case elfcpp::R_PPC64_DTPREL16_HIGHER:
10791     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
10792     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
10793     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
10794       if (size != 64)
10795 	// R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
10796 	// R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
10797 	break;
10798       // Fall through.
10799     case elfcpp::R_POWERPC_DTPREL16:
10800     case elfcpp::R_POWERPC_DTPREL16_LO:
10801     case elfcpp::R_POWERPC_DTPREL16_HI:
10802     case elfcpp::R_POWERPC_DTPREL16_HA:
10803     case elfcpp::R_POWERPC_DTPREL:
10804     case elfcpp::R_PPC64_DTPREL16_HIGH:
10805     case elfcpp::R_PPC64_DTPREL16_HIGHA:
10806     case elfcpp::R_PPC64_DTPREL34:
10807       // tls symbol values are relative to tls_segment()->vaddr()
10808       value -= dtp_offset;
10809       break;
10810 
10811     case elfcpp::R_PPC64_ADDR64_LOCAL:
10812       if (gsym != NULL)
10813 	value += object->ppc64_local_entry_offset(gsym);
10814       else
10815 	value += object->ppc64_local_entry_offset(r_sym);
10816       break;
10817 
10818     default:
10819       break;
10820     }
10821 
10822   Insn branch_bit = 0;
10823   switch (r_type)
10824     {
10825     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
10826     case elfcpp::R_POWERPC_REL14_BRTAKEN:
10827       branch_bit = 1 << 21;
10828       // Fall through.
10829     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
10830     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
10831       {
10832 	Insn* iview = reinterpret_cast<Insn*>(view);
10833 	Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10834 	insn &= ~(1 << 21);
10835 	insn |= branch_bit;
10836 	if (this->is_isa_v2)
10837 	  {
10838 	    // Set 'a' bit.  This is 0b00010 in BO field for branch
10839 	    // on CR(BI) insns (BO == 001at or 011at), and 0b01000
10840 	    // for branch on CTR insns (BO == 1a00t or 1a01t).
10841 	    if ((insn & (0x14 << 21)) == (0x04 << 21))
10842 	      insn |= 0x02 << 21;
10843 	    else if ((insn & (0x14 << 21)) == (0x10 << 21))
10844 	      insn |= 0x08 << 21;
10845 	    else
10846 	      break;
10847 	  }
10848 	else
10849 	  {
10850 	    // Invert 'y' bit if not the default.
10851 	    if (static_cast<Signed_address>(value) < 0)
10852 	      insn ^= 1 << 21;
10853 	  }
10854 	elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10855       }
10856       break;
10857 
10858     case elfcpp::R_POWERPC_PLT16_HA:
10859       if (size == 32
10860 	  && !parameters->options().output_is_position_independent())
10861 	{
10862 	  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10863 	  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10864 
10865 	  // Convert addis to lis.
10866 	  if ((insn & (0x3f << 26)) == 15u << 26
10867 	      && (insn & (0x1f << 16)) != 0)
10868 	    {
10869 	      insn &= ~(0x1f << 16);
10870 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10871 	    }
10872 	}
10873       break;
10874 
10875     default:
10876       break;
10877     }
10878 
10879   if (size == 64
10880       && (gsym
10881 	  ? relative_value_is_known(gsym)
10882 	  : relative_value_is_known(psymval)))
10883     {
10884       Insn* iview;
10885       Insn* iview2;
10886       Insn insn;
10887       uint64_t pinsn, pinsn2;
10888 
10889       switch (r_type)
10890 	{
10891 	default:
10892 	  break;
10893 
10894 	  // Multi-instruction sequences that access the GOT/TOC can
10895 	  // be optimized, eg.
10896 	  //     addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
10897 	  // to  addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
10898 	  // and
10899 	  //     addis ra,r2,0; addi rb,ra,x@toc@l;
10900 	  // to  nop;           addi rb,r2,x@toc;
10901 	case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
10902 	case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
10903 	case elfcpp::R_POWERPC_GOT_TPREL16_HA:
10904 	case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
10905 	case elfcpp::R_POWERPC_GOT16_HA:
10906 	case elfcpp::R_PPC64_TOC16_HA:
10907 	  if (parameters->options().toc_optimize())
10908 	    {
10909 	      iview = reinterpret_cast<Insn*>(view - d_offset);
10910 	      insn = elfcpp::Swap<32, big_endian>::readval(iview);
10911 	      if ((r_type == elfcpp::R_PPC64_TOC16_HA
10912 		   && object->make_toc_relative(target, &value))
10913 		  || (r_type == elfcpp::R_POWERPC_GOT16_HA
10914 		      && object->make_got_relative(target, psymval,
10915 						   rela.get_r_addend(),
10916 						   &value)))
10917 		{
10918 		  gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
10919 			      == ((15u << 26) | (2 << 16)));
10920 		}
10921 	      if (((insn & ((0x3f << 26) | 0x1f << 16))
10922 		   == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
10923 		  && value + 0x8000 < 0x10000)
10924 		{
10925 		  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10926 		  return true;
10927 		}
10928 	    }
10929 	  break;
10930 
10931 	case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
10932 	case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
10933 	case elfcpp::R_POWERPC_GOT_TPREL16_LO:
10934 	case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
10935 	case elfcpp::R_POWERPC_GOT16_LO:
10936 	case elfcpp::R_PPC64_GOT16_LO_DS:
10937 	case elfcpp::R_PPC64_TOC16_LO:
10938 	case elfcpp::R_PPC64_TOC16_LO_DS:
10939 	  if (parameters->options().toc_optimize())
10940 	    {
10941 	      iview = reinterpret_cast<Insn*>(view - d_offset);
10942 	      insn = elfcpp::Swap<32, big_endian>::readval(iview);
10943 	      bool changed = false;
10944 	      if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
10945 		   && object->make_toc_relative(target, &value))
10946 		  || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
10947 		      && object->make_got_relative(target, psymval,
10948 						   rela.get_r_addend(),
10949 						   &value)))
10950 		{
10951 		  gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
10952 		  insn ^= (14u << 26) ^ (58u << 26);
10953 		  r_type = elfcpp::R_PPC64_TOC16_LO;
10954 		  changed = true;
10955 		}
10956 	      if (ok_lo_toc_insn(insn, r_type)
10957 		  && value + 0x8000 < 0x10000)
10958 		{
10959 		  if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
10960 		    {
10961 		      // Transform addic to addi when we change reg.
10962 		      insn &= ~((0x3f << 26) | (0x1f << 16));
10963 		      insn |= (14u << 26) | (2 << 16);
10964 		    }
10965 		  else
10966 		    {
10967 		      insn &= ~(0x1f << 16);
10968 		      insn |= 2 << 16;
10969 		    }
10970 		  changed = true;
10971 		}
10972 	      if (changed)
10973 		elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10974 	    }
10975 	  break;
10976 
10977 	case elfcpp::R_PPC64_GOT_PCREL34:
10978 	  if (parameters->options().toc_optimize())
10979 	    {
10980 	      iview = reinterpret_cast<Insn*>(view);
10981 	      pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10982 	      pinsn <<= 32;
10983 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10984 	      if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
10985 		   != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
10986 		break;
10987 
10988 	      Address relval = psymval->value(object, rela.get_r_addend());
10989 	      relval -= address;
10990 	      if (relval + (1ULL << 33) < 1ULL << 34)
10991 		{
10992 		  value = relval;
10993 		  // Replace with paddi
10994 		  pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
10995 		  elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10996 		  elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10997 							 pinsn & 0xffffffff);
10998 		  goto pcrelopt;
10999 		}
11000 	    }
11001 	  break;
11002 
11003 	case elfcpp::R_PPC64_PCREL34:
11004 	  {
11005 	    iview = reinterpret_cast<Insn*>(view);
11006 	    pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11007 	    pinsn <<= 32;
11008 	    pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11009 	    if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11010 		!= ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11011 		    | (14ULL << 26) /* paddi */))
11012 	      break;
11013 
11014 	  pcrelopt:
11015 	    const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11016 	    elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11017 	    size_t reloc_count = shdr.get_sh_size() / reloc_size;
11018 	    if (relnum >= reloc_count - 1)
11019 	      break;
11020 
11021 	    Reltype next_rela(preloc + reloc_size);
11022 	    if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11023 		 != elfcpp::R_PPC64_PCREL_OPT)
11024 		|| next_rela.get_r_offset() != rela.get_r_offset())
11025 	      break;
11026 
11027 	    Address off = next_rela.get_r_addend();
11028 	    if (off == 0)
11029 	      off = 8; // zero means next insn.
11030 	    if (off + rela.get_r_offset() + 4 > view_size)
11031 	      break;
11032 
11033 	    iview2 = reinterpret_cast<Insn*>(view + off);
11034 	    pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11035 	    pinsn2 <<= 32;
11036 	    if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11037 	      break;
11038 	    if (xlate_pcrel_opt(&pinsn, &pinsn2))
11039 	      {
11040 		elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11041 		elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11042 						       pinsn & 0xffffffff);
11043 		elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11044 	      }
11045 	  }
11046 	  break;
11047 
11048 	case elfcpp::R_POWERPC_TPREL16_HA:
11049 	  if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
11050 	    {
11051 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11052 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11053 	      if ((insn & ((0x3f << 26) | 0x1f << 16))
11054 		  != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
11055 		;
11056 	      else
11057 		{
11058 		  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11059 		  return true;
11060 		}
11061 	    }
11062 	  break;
11063 
11064 	case elfcpp::R_PPC64_TPREL16_LO_DS:
11065 	  if (size == 32)
11066 	    // R_PPC_TLSGD, R_PPC_TLSLD
11067 	    break;
11068 	  // Fall through.
11069 	case elfcpp::R_POWERPC_TPREL16_LO:
11070 	  if (parameters->options().tls_optimize() && value + 0x8000 < 0x10000)
11071 	    {
11072 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11073 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11074 	      insn &= ~(0x1f << 16);
11075 	      insn |= (size == 32 ? 2 : 13) << 16;
11076 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11077 	    }
11078 	  break;
11079 
11080 	case elfcpp::R_PPC64_ENTRY:
11081 	  value = (target->got_section()->output_section()->address()
11082 		   + object->toc_base_offset());
11083 	  if (value + 0x80008000 <= 0xffffffff
11084 	      && !parameters->options().output_is_position_independent())
11085 	    {
11086 	      Insn* iview = reinterpret_cast<Insn*>(view);
11087 	      Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11088 	      Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11089 
11090 	      if ((insn1 & ~0xfffc) == ld_2_12
11091 		  && insn2 == add_2_2_12)
11092 		{
11093 		  insn1 = lis_2 + ha(value);
11094 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11095 		  insn2 = addi_2_2 + l(value);
11096 		  elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11097 		  return true;
11098 		}
11099 	    }
11100 	  else
11101 	    {
11102 	      value -= address;
11103 	      if (value + 0x80008000 <= 0xffffffff)
11104 		{
11105 		  Insn* iview = reinterpret_cast<Insn*>(view);
11106 		  Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11107 		  Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11108 
11109 		  if ((insn1 & ~0xfffc) == ld_2_12
11110 		      && insn2 == add_2_2_12)
11111 		    {
11112 		      insn1 = addis_2_12 + ha(value);
11113 		      elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11114 		      insn2 = addi_2_2 + l(value);
11115 		      elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11116 		      return true;
11117 		    }
11118 		}
11119 	    }
11120 	  break;
11121 
11122 	case elfcpp::R_POWERPC_REL16_LO:
11123 	  // If we are generating a non-PIC executable, edit
11124 	  // 	0:	addis 2,12,.TOC.-0b@ha
11125 	  //		addi 2,2,.TOC.-0b@l
11126 	  // used by ELFv2 global entry points to set up r2, to
11127 	  //		lis 2,.TOC.@ha
11128 	  //		addi 2,2,.TOC.@l
11129 	  // if .TOC. is in range.  */
11130 	  if (value + address - 4 + 0x80008000 <= 0xffffffff
11131 	      && relnum + 1 > 1
11132 	      && preloc != NULL
11133 	      && target->abiversion() >= 2
11134 	      && !parameters->options().output_is_position_independent()
11135 	      && rela.get_r_addend() == d_offset + 4
11136 	      && gsym != NULL
11137 	      && strcmp(gsym->name(), ".TOC.") == 0)
11138 	    {
11139 	      const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11140 	      Reltype prev_rela(preloc - reloc_size);
11141 	      if ((prev_rela.get_r_info()
11142 		   == elfcpp::elf_r_info<size>(r_sym,
11143 					       elfcpp::R_POWERPC_REL16_HA))
11144 		  && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11145 		  && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11146 		{
11147 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11148 		  Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11149 		  Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11150 
11151 		  if ((insn1 & 0xffff0000) == addis_2_12
11152 		      && (insn2 & 0xffff0000) == addi_2_2)
11153 		    {
11154 		      insn1 = lis_2 + ha(value + address - 4);
11155 		      elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11156 		      insn2 = addi_2_2 + l(value + address - 4);
11157 		      elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11158 		      if (relinfo->rr)
11159 			{
11160 			  relinfo->rr->set_strategy(relnum - 1,
11161 						    Relocatable_relocs::RELOC_SPECIAL);
11162 			  relinfo->rr->set_strategy(relnum,
11163 						    Relocatable_relocs::RELOC_SPECIAL);
11164 			}
11165 		      return true;
11166 		    }
11167 		}
11168 	    }
11169 	  break;
11170 	}
11171     }
11172 
11173   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11174   elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11175   switch (r_type)
11176     {
11177     case elfcpp::R_POWERPC_ADDR32:
11178     case elfcpp::R_POWERPC_UADDR32:
11179       if (size == 64)
11180 	overflow = Reloc::CHECK_BITFIELD;
11181       break;
11182 
11183     case elfcpp::R_POWERPC_REL32:
11184     case elfcpp::R_POWERPC_REL16DX_HA:
11185       if (size == 64)
11186 	overflow = Reloc::CHECK_SIGNED;
11187       break;
11188 
11189     case elfcpp::R_POWERPC_UADDR16:
11190       overflow = Reloc::CHECK_BITFIELD;
11191       break;
11192 
11193     case elfcpp::R_POWERPC_ADDR16:
11194       // We really should have three separate relocations,
11195       // one for 16-bit data, one for insns with 16-bit signed fields,
11196       // and one for insns with 16-bit unsigned fields.
11197       overflow = Reloc::CHECK_BITFIELD;
11198       if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11199 	overflow = Reloc::CHECK_LOW_INSN;
11200       break;
11201 
11202     case elfcpp::R_POWERPC_ADDR16_HI:
11203     case elfcpp::R_POWERPC_ADDR16_HA:
11204     case elfcpp::R_POWERPC_GOT16_HI:
11205     case elfcpp::R_POWERPC_GOT16_HA:
11206     case elfcpp::R_POWERPC_PLT16_HI:
11207     case elfcpp::R_POWERPC_PLT16_HA:
11208     case elfcpp::R_POWERPC_SECTOFF_HI:
11209     case elfcpp::R_POWERPC_SECTOFF_HA:
11210     case elfcpp::R_PPC64_TOC16_HI:
11211     case elfcpp::R_PPC64_TOC16_HA:
11212     case elfcpp::R_PPC64_PLTGOT16_HI:
11213     case elfcpp::R_PPC64_PLTGOT16_HA:
11214     case elfcpp::R_POWERPC_TPREL16_HI:
11215     case elfcpp::R_POWERPC_TPREL16_HA:
11216     case elfcpp::R_POWERPC_DTPREL16_HI:
11217     case elfcpp::R_POWERPC_DTPREL16_HA:
11218     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11219     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11220     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11221     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11222     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11223     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11224     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11225     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11226     case elfcpp::R_POWERPC_REL16_HI:
11227     case elfcpp::R_POWERPC_REL16_HA:
11228       if (size != 32)
11229 	overflow = Reloc::CHECK_HIGH_INSN;
11230       break;
11231 
11232     case elfcpp::R_POWERPC_REL16:
11233     case elfcpp::R_PPC64_TOC16:
11234     case elfcpp::R_POWERPC_GOT16:
11235     case elfcpp::R_POWERPC_SECTOFF:
11236     case elfcpp::R_POWERPC_TPREL16:
11237     case elfcpp::R_POWERPC_DTPREL16:
11238     case elfcpp::R_POWERPC_GOT_TLSGD16:
11239     case elfcpp::R_POWERPC_GOT_TLSLD16:
11240     case elfcpp::R_POWERPC_GOT_TPREL16:
11241     case elfcpp::R_POWERPC_GOT_DTPREL16:
11242       overflow = Reloc::CHECK_LOW_INSN;
11243       break;
11244 
11245     case elfcpp::R_PPC64_REL24_NOTOC:
11246       if (size == 32)
11247 	break;
11248       // Fall through.
11249     case elfcpp::R_POWERPC_ADDR24:
11250     case elfcpp::R_POWERPC_ADDR14:
11251     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11252     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11253     case elfcpp::R_PPC64_ADDR16_DS:
11254     case elfcpp::R_POWERPC_REL24:
11255     case elfcpp::R_PPC_PLTREL24:
11256     case elfcpp::R_PPC_LOCAL24PC:
11257     case elfcpp::R_PPC64_TPREL16_DS:
11258     case elfcpp::R_PPC64_DTPREL16_DS:
11259     case elfcpp::R_PPC64_TOC16_DS:
11260     case elfcpp::R_PPC64_GOT16_DS:
11261     case elfcpp::R_PPC64_SECTOFF_DS:
11262     case elfcpp::R_POWERPC_REL14:
11263     case elfcpp::R_POWERPC_REL14_BRTAKEN:
11264     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11265     case elfcpp::R_PPC64_D34:
11266     case elfcpp::R_PPC64_PCREL34:
11267     case elfcpp::R_PPC64_GOT_PCREL34:
11268     case elfcpp::R_PPC64_PLT_PCREL34:
11269     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11270     case elfcpp::R_PPC64_D28:
11271     case elfcpp::R_PPC64_PCREL28:
11272     case elfcpp::R_PPC64_TPREL34:
11273     case elfcpp::R_PPC64_DTPREL34:
11274     case elfcpp::R_PPC64_GOT_TLSGD34:
11275     case elfcpp::R_PPC64_GOT_TLSLD34:
11276     case elfcpp::R_PPC64_GOT_TPREL34:
11277     case elfcpp::R_PPC64_GOT_DTPREL34:
11278       overflow = Reloc::CHECK_SIGNED;
11279       break;
11280     }
11281 
11282   Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11283   Insn insn = 0;
11284 
11285   if (overflow == Reloc::CHECK_LOW_INSN
11286       || overflow == Reloc::CHECK_HIGH_INSN)
11287     {
11288       insn = elfcpp::Swap<32, big_endian>::readval(iview);
11289 
11290       if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11291 	overflow = Reloc::CHECK_BITFIELD;
11292       else if (overflow == Reloc::CHECK_LOW_INSN
11293 	       ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11294 		  || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11295 		  || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11296 	       : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11297 		  || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11298 		  || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
11299 	overflow = Reloc::CHECK_UNSIGNED;
11300       else
11301 	overflow = Reloc::CHECK_SIGNED;
11302     }
11303 
11304   bool maybe_dq_reloc = false;
11305   typename Powerpc_relocate_functions<size, big_endian>::Status status
11306     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
11307   switch (r_type)
11308     {
11309     case elfcpp::R_POWERPC_NONE:
11310     case elfcpp::R_POWERPC_TLS:
11311     case elfcpp::R_POWERPC_GNU_VTINHERIT:
11312     case elfcpp::R_POWERPC_GNU_VTENTRY:
11313     case elfcpp::R_POWERPC_PLTSEQ:
11314     case elfcpp::R_POWERPC_PLTCALL:
11315     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
11316     case elfcpp::R_PPC64_PLTCALL_NOTOC:
11317     case elfcpp::R_PPC64_PCREL_OPT:
11318       break;
11319 
11320     case elfcpp::R_PPC64_ADDR64:
11321     case elfcpp::R_PPC64_REL64:
11322     case elfcpp::R_PPC64_TOC:
11323     case elfcpp::R_PPC64_ADDR64_LOCAL:
11324       Reloc::addr64(view, value);
11325       break;
11326 
11327     case elfcpp::R_POWERPC_TPREL:
11328     case elfcpp::R_POWERPC_DTPREL:
11329       if (size == 64)
11330 	Reloc::addr64(view, value);
11331       else
11332 	status = Reloc::addr32(view, value, overflow);
11333       break;
11334 
11335     case elfcpp::R_PPC64_UADDR64:
11336       Reloc::addr64_u(view, value);
11337       break;
11338 
11339     case elfcpp::R_POWERPC_ADDR32:
11340       status = Reloc::addr32(view, value, overflow);
11341       break;
11342 
11343     case elfcpp::R_POWERPC_REL32:
11344     case elfcpp::R_POWERPC_UADDR32:
11345       status = Reloc::addr32_u(view, value, overflow);
11346       break;
11347 
11348     case elfcpp::R_PPC64_REL24_NOTOC:
11349       if (size == 32)
11350 	goto unsupp; // R_PPC_EMB_RELSDA
11351       // Fall through.
11352     case elfcpp::R_POWERPC_ADDR24:
11353     case elfcpp::R_POWERPC_REL24:
11354     case elfcpp::R_PPC_PLTREL24:
11355     case elfcpp::R_PPC_LOCAL24PC:
11356       status = Reloc::addr24(view, value, overflow);
11357       break;
11358 
11359     case elfcpp::R_POWERPC_GOT_DTPREL16:
11360     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11361     case elfcpp::R_POWERPC_GOT_TPREL16:
11362     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11363       if (size == 64)
11364 	{
11365 	  // On ppc64 these are all ds form
11366 	  maybe_dq_reloc = true;
11367 	  break;
11368 	}
11369       // Fall through.
11370     case elfcpp::R_POWERPC_ADDR16:
11371     case elfcpp::R_POWERPC_REL16:
11372     case elfcpp::R_PPC64_TOC16:
11373     case elfcpp::R_POWERPC_GOT16:
11374     case elfcpp::R_POWERPC_SECTOFF:
11375     case elfcpp::R_POWERPC_TPREL16:
11376     case elfcpp::R_POWERPC_DTPREL16:
11377     case elfcpp::R_POWERPC_GOT_TLSGD16:
11378     case elfcpp::R_POWERPC_GOT_TLSLD16:
11379     case elfcpp::R_POWERPC_ADDR16_LO:
11380     case elfcpp::R_POWERPC_REL16_LO:
11381     case elfcpp::R_PPC64_TOC16_LO:
11382     case elfcpp::R_POWERPC_GOT16_LO:
11383     case elfcpp::R_POWERPC_PLT16_LO:
11384     case elfcpp::R_POWERPC_SECTOFF_LO:
11385     case elfcpp::R_POWERPC_TPREL16_LO:
11386     case elfcpp::R_POWERPC_DTPREL16_LO:
11387     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11388     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11389       if (size == 64)
11390 	status = Reloc::addr16(view, value, overflow);
11391       else
11392 	maybe_dq_reloc = true;
11393       break;
11394 
11395     case elfcpp::R_POWERPC_UADDR16:
11396       status = Reloc::addr16_u(view, value, overflow);
11397       break;
11398 
11399     case elfcpp::R_PPC64_ADDR16_HIGH:
11400     case elfcpp::R_PPC64_TPREL16_HIGH:
11401     case elfcpp::R_PPC64_DTPREL16_HIGH:
11402       if (size == 32)
11403 	// R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
11404 	goto unsupp;
11405       // Fall through.
11406     case elfcpp::R_POWERPC_ADDR16_HI:
11407     case elfcpp::R_POWERPC_REL16_HI:
11408     case elfcpp::R_PPC64_REL16_HIGH:
11409     case elfcpp::R_PPC64_TOC16_HI:
11410     case elfcpp::R_POWERPC_GOT16_HI:
11411     case elfcpp::R_POWERPC_PLT16_HI:
11412     case elfcpp::R_POWERPC_SECTOFF_HI:
11413     case elfcpp::R_POWERPC_TPREL16_HI:
11414     case elfcpp::R_POWERPC_DTPREL16_HI:
11415     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11416     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11417     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11418     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11419       Reloc::addr16_hi(view, value);
11420       break;
11421 
11422     case elfcpp::R_PPC64_ADDR16_HIGHA:
11423     case elfcpp::R_PPC64_TPREL16_HIGHA:
11424     case elfcpp::R_PPC64_DTPREL16_HIGHA:
11425       if (size == 32)
11426 	// R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
11427 	goto unsupp;
11428       // Fall through.
11429     case elfcpp::R_POWERPC_ADDR16_HA:
11430     case elfcpp::R_POWERPC_REL16_HA:
11431     case elfcpp::R_PPC64_REL16_HIGHA:
11432     case elfcpp::R_PPC64_TOC16_HA:
11433     case elfcpp::R_POWERPC_GOT16_HA:
11434     case elfcpp::R_POWERPC_PLT16_HA:
11435     case elfcpp::R_POWERPC_SECTOFF_HA:
11436     case elfcpp::R_POWERPC_TPREL16_HA:
11437     case elfcpp::R_POWERPC_DTPREL16_HA:
11438     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11439     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11440     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11441     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11442       Reloc::addr16_ha(view, value);
11443       break;
11444 
11445     case elfcpp::R_POWERPC_REL16DX_HA:
11446       status = Reloc::addr16dx_ha(view, value, overflow);
11447       break;
11448 
11449     case elfcpp::R_PPC64_DTPREL16_HIGHER:
11450       if (size == 32)
11451 	// R_PPC_EMB_NADDR16_LO
11452 	goto unsupp;
11453       // Fall through.
11454     case elfcpp::R_PPC64_ADDR16_HIGHER:
11455     case elfcpp::R_PPC64_REL16_HIGHER:
11456     case elfcpp::R_PPC64_TPREL16_HIGHER:
11457       Reloc::addr16_hi2(view, value);
11458       break;
11459 
11460     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11461       if (size == 32)
11462 	// R_PPC_EMB_NADDR16_HI
11463 	goto unsupp;
11464       // Fall through.
11465     case elfcpp::R_PPC64_ADDR16_HIGHERA:
11466     case elfcpp::R_PPC64_REL16_HIGHERA:
11467     case elfcpp::R_PPC64_TPREL16_HIGHERA:
11468       Reloc::addr16_ha2(view, value);
11469       break;
11470 
11471     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11472       if (size == 32)
11473 	// R_PPC_EMB_NADDR16_HA
11474 	goto unsupp;
11475       // Fall through.
11476     case elfcpp::R_PPC64_ADDR16_HIGHEST:
11477     case elfcpp::R_PPC64_REL16_HIGHEST:
11478     case elfcpp::R_PPC64_TPREL16_HIGHEST:
11479       Reloc::addr16_hi3(view, value);
11480       break;
11481 
11482     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11483       if (size == 32)
11484 	// R_PPC_EMB_SDAI16
11485 	goto unsupp;
11486       // Fall through.
11487     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
11488     case elfcpp::R_PPC64_REL16_HIGHESTA:
11489     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11490       Reloc::addr16_ha3(view, value);
11491       break;
11492 
11493     case elfcpp::R_PPC64_DTPREL16_DS:
11494     case elfcpp::R_PPC64_DTPREL16_LO_DS:
11495       if (size == 32)
11496 	// R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
11497 	goto unsupp;
11498       // Fall through.
11499     case elfcpp::R_PPC64_TPREL16_DS:
11500     case elfcpp::R_PPC64_TPREL16_LO_DS:
11501       if (size == 32)
11502 	// R_PPC_TLSGD, R_PPC_TLSLD
11503 	break;
11504       // Fall through.
11505     case elfcpp::R_PPC64_ADDR16_DS:
11506     case elfcpp::R_PPC64_ADDR16_LO_DS:
11507     case elfcpp::R_PPC64_TOC16_DS:
11508     case elfcpp::R_PPC64_TOC16_LO_DS:
11509     case elfcpp::R_PPC64_GOT16_DS:
11510     case elfcpp::R_PPC64_GOT16_LO_DS:
11511     case elfcpp::R_PPC64_PLT16_LO_DS:
11512     case elfcpp::R_PPC64_SECTOFF_DS:
11513     case elfcpp::R_PPC64_SECTOFF_LO_DS:
11514       maybe_dq_reloc = true;
11515       break;
11516 
11517     case elfcpp::R_POWERPC_ADDR14:
11518     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11519     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11520     case elfcpp::R_POWERPC_REL14:
11521     case elfcpp::R_POWERPC_REL14_BRTAKEN:
11522     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11523       status = Reloc::addr14(view, value, overflow);
11524       break;
11525 
11526     case elfcpp::R_POWERPC_COPY:
11527     case elfcpp::R_POWERPC_GLOB_DAT:
11528     case elfcpp::R_POWERPC_JMP_SLOT:
11529     case elfcpp::R_POWERPC_RELATIVE:
11530     case elfcpp::R_POWERPC_DTPMOD:
11531     case elfcpp::R_PPC64_JMP_IREL:
11532     case elfcpp::R_POWERPC_IRELATIVE:
11533       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11534 			     _("unexpected reloc %u in object file"),
11535 			     r_type);
11536       break;
11537 
11538     case elfcpp::R_PPC64_TOCSAVE:
11539       if (size == 32)
11540 	// R_PPC_EMB_SDA21
11541 	goto unsupp;
11542       else
11543 	{
11544 	  Symbol_location loc;
11545 	  loc.object = relinfo->object;
11546 	  loc.shndx = relinfo->data_shndx;
11547 	  loc.offset = rela.get_r_offset();
11548 	  Tocsave_loc::const_iterator p = target->tocsave_loc().find(loc);
11549 	  if (p != target->tocsave_loc().end())
11550 	    {
11551 	      // If we've generated plt calls using this tocsave, then
11552 	      // the nop needs to be changed to save r2.
11553 	      Insn* iview = reinterpret_cast<Insn*>(view);
11554 	      if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
11555 		elfcpp::Swap<32, big_endian>::
11556 		  writeval(iview, std_2_1 + target->stk_toc());
11557 	    }
11558 	}
11559       break;
11560 
11561     case elfcpp::R_PPC_EMB_SDA2I16:
11562     case elfcpp::R_PPC_EMB_SDA2REL:
11563       if (size == 32)
11564 	goto unsupp;
11565       // R_PPC64_TLSGD, R_PPC64_TLSLD
11566       break;
11567 
11568     case elfcpp::R_PPC64_D34:
11569     case elfcpp::R_PPC64_D34_LO:
11570     case elfcpp::R_PPC64_PCREL34:
11571     case elfcpp::R_PPC64_GOT_PCREL34:
11572     case elfcpp::R_PPC64_PLT_PCREL34:
11573     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11574     case elfcpp::R_PPC64_TPREL34:
11575     case elfcpp::R_PPC64_DTPREL34:
11576     case elfcpp::R_PPC64_GOT_TLSGD34:
11577     case elfcpp::R_PPC64_GOT_TLSLD34:
11578     case elfcpp::R_PPC64_GOT_TPREL34:
11579     case elfcpp::R_PPC64_GOT_DTPREL34:
11580       if (size == 32)
11581 	goto unsupp;
11582       status = Reloc::addr34(view, value, overflow);
11583       break;
11584 
11585     case elfcpp::R_PPC64_D34_HI30:
11586       if (size == 32)
11587 	goto unsupp;
11588       Reloc::addr34_hi(view, value);
11589       break;
11590 
11591     case elfcpp::R_PPC64_D34_HA30:
11592       if (size == 32)
11593 	goto unsupp;
11594       Reloc::addr34_ha(view, value);
11595       break;
11596 
11597     case elfcpp::R_PPC64_D28:
11598     case elfcpp::R_PPC64_PCREL28:
11599       if (size == 32)
11600 	goto unsupp;
11601       status = Reloc::addr28(view, value, overflow);
11602       break;
11603 
11604     case elfcpp::R_PPC64_ADDR16_HIGHER34:
11605     case elfcpp::R_PPC64_REL16_HIGHER34:
11606       if (size == 32)
11607 	goto unsupp;
11608       Reloc::addr16_higher34(view, value);
11609       break;
11610 
11611     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
11612     case elfcpp::R_PPC64_REL16_HIGHERA34:
11613       if (size == 32)
11614 	goto unsupp;
11615       Reloc::addr16_highera34(view, value);
11616       break;
11617 
11618     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
11619     case elfcpp::R_PPC64_REL16_HIGHEST34:
11620       if (size == 32)
11621 	goto unsupp;
11622       Reloc::addr16_highest34(view, value);
11623       break;
11624 
11625     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
11626     case elfcpp::R_PPC64_REL16_HIGHESTA34:
11627       if (size == 32)
11628 	goto unsupp;
11629       Reloc::addr16_highesta34(view, value);
11630       break;
11631 
11632     case elfcpp::R_POWERPC_PLT32:
11633     case elfcpp::R_POWERPC_PLTREL32:
11634     case elfcpp::R_PPC_SDAREL16:
11635     case elfcpp::R_POWERPC_ADDR30:
11636     case elfcpp::R_PPC64_PLT64:
11637     case elfcpp::R_PPC64_PLTREL64:
11638     case elfcpp::R_PPC64_PLTGOT16:
11639     case elfcpp::R_PPC64_PLTGOT16_LO:
11640     case elfcpp::R_PPC64_PLTGOT16_HI:
11641     case elfcpp::R_PPC64_PLTGOT16_HA:
11642     case elfcpp::R_PPC64_PLTGOT16_DS:
11643     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
11644     case elfcpp::R_PPC_TOC16:
11645     default:
11646     unsupp:
11647       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11648 			     _("unsupported reloc %u"),
11649 			     r_type);
11650       break;
11651     }
11652 
11653   if (maybe_dq_reloc)
11654     {
11655       if (insn == 0)
11656 	insn = elfcpp::Swap<32, big_endian>::readval(iview);
11657 
11658       if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
11659 	  || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
11660 	      && (insn & 3) == 1))
11661 	status = Reloc::addr16_dq(view, value, overflow);
11662       else if (size == 64
11663 	       || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
11664 	       || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
11665 	       || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
11666 	       || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
11667 	status = Reloc::addr16_ds(view, value, overflow);
11668       else
11669 	status = Reloc::addr16(view, value, overflow);
11670     }
11671 
11672   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
11673       && (has_stub_value
11674 	  || !(gsym != NULL
11675 	       && gsym->is_undefined()
11676 	       && is_branch_reloc<size>(r_type))))
11677     {
11678       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
11679 			     _("relocation overflow"));
11680       if (has_stub_value)
11681 	gold_info(_("try relinking with a smaller --stub-group-size"));
11682     }
11683 
11684   return true;
11685 }
11686 
11687 // Relocate section data.
11688 
11689 template<int size, bool big_endian>
11690 void
11691 Target_powerpc<size, big_endian>::relocate_section(
11692     const Relocate_info<size, big_endian>* relinfo,
11693     unsigned int sh_type,
11694     const unsigned char* prelocs,
11695     size_t reloc_count,
11696     Output_section* output_section,
11697     bool needs_special_offset_handling,
11698     unsigned char* view,
11699     Address address,
11700     section_size_type view_size,
11701     const Reloc_symbol_changes* reloc_symbol_changes)
11702 {
11703   typedef Target_powerpc<size, big_endian> Powerpc;
11704   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
11705   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
11706     Powerpc_comdat_behavior;
11707   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11708       Classify_reloc;
11709 
11710   gold_assert(sh_type == elfcpp::SHT_RELA);
11711 
11712   gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
11713 			 Powerpc_comdat_behavior, Classify_reloc>(
11714     relinfo,
11715     this,
11716     prelocs,
11717     reloc_count,
11718     output_section,
11719     needs_special_offset_handling,
11720     view,
11721     address,
11722     view_size,
11723     reloc_symbol_changes);
11724 }
11725 
11726 template<int size, bool big_endian>
11727 class Powerpc_scan_relocatable_reloc
11728 {
11729 public:
11730   typedef typename elfcpp::Rela<size, big_endian> Reltype;
11731   static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11732   static const int sh_type = elfcpp::SHT_RELA;
11733 
11734   // Return the symbol referred to by the relocation.
11735   static inline unsigned int
11736   get_r_sym(const Reltype* reloc)
11737   { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
11738 
11739   // Return the type of the relocation.
11740   static inline unsigned int
11741   get_r_type(const Reltype* reloc)
11742   { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
11743 
11744   // Return the strategy to use for a local symbol which is not a
11745   // section symbol, given the relocation type.
11746   inline Relocatable_relocs::Reloc_strategy
11747   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
11748   {
11749     if (r_type == 0 && r_sym == 0)
11750       return Relocatable_relocs::RELOC_DISCARD;
11751     return Relocatable_relocs::RELOC_COPY;
11752   }
11753 
11754   // Return the strategy to use for a local symbol which is a section
11755   // symbol, given the relocation type.
11756   inline Relocatable_relocs::Reloc_strategy
11757   local_section_strategy(unsigned int, Relobj*)
11758   {
11759     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
11760   }
11761 
11762   // Return the strategy to use for a global symbol, given the
11763   // relocation type, the object, and the symbol index.
11764   inline Relocatable_relocs::Reloc_strategy
11765   global_strategy(unsigned int r_type, Relobj*, unsigned int)
11766   {
11767     if (size == 32
11768 	&& (r_type == elfcpp::R_PPC_PLTREL24
11769 	    || r_type == elfcpp::R_POWERPC_PLT16_LO
11770 	    || r_type == elfcpp::R_POWERPC_PLT16_HI
11771 	    || r_type == elfcpp::R_POWERPC_PLT16_HA))
11772       return Relocatable_relocs::RELOC_SPECIAL;
11773     return Relocatable_relocs::RELOC_COPY;
11774   }
11775 };
11776 
11777 // Scan the relocs during a relocatable link.
11778 
11779 template<int size, bool big_endian>
11780 void
11781 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
11782     Symbol_table* symtab,
11783     Layout* layout,
11784     Sized_relobj_file<size, big_endian>* object,
11785     unsigned int data_shndx,
11786     unsigned int sh_type,
11787     const unsigned char* prelocs,
11788     size_t reloc_count,
11789     Output_section* output_section,
11790     bool needs_special_offset_handling,
11791     size_t local_symbol_count,
11792     const unsigned char* plocal_symbols,
11793     Relocatable_relocs* rr)
11794 {
11795   typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
11796 
11797   gold_assert(sh_type == elfcpp::SHT_RELA);
11798 
11799   gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
11800     symtab,
11801     layout,
11802     object,
11803     data_shndx,
11804     prelocs,
11805     reloc_count,
11806     output_section,
11807     needs_special_offset_handling,
11808     local_symbol_count,
11809     plocal_symbols,
11810     rr);
11811 }
11812 
11813 // Scan the relocs for --emit-relocs.
11814 
11815 template<int size, bool big_endian>
11816 void
11817 Target_powerpc<size, big_endian>::emit_relocs_scan(
11818     Symbol_table* symtab,
11819     Layout* layout,
11820     Sized_relobj_file<size, big_endian>* object,
11821     unsigned int data_shndx,
11822     unsigned int sh_type,
11823     const unsigned char* prelocs,
11824     size_t reloc_count,
11825     Output_section* output_section,
11826     bool needs_special_offset_handling,
11827     size_t local_symbol_count,
11828     const unsigned char* plocal_syms,
11829     Relocatable_relocs* rr)
11830 {
11831   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
11832       Classify_reloc;
11833   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
11834       Emit_relocs_strategy;
11835 
11836   gold_assert(sh_type == elfcpp::SHT_RELA);
11837 
11838   gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
11839     symtab,
11840     layout,
11841     object,
11842     data_shndx,
11843     prelocs,
11844     reloc_count,
11845     output_section,
11846     needs_special_offset_handling,
11847     local_symbol_count,
11848     plocal_syms,
11849     rr);
11850 }
11851 
11852 // Emit relocations for a section.
11853 // This is a modified version of the function by the same name in
11854 // target-reloc.h.  Using relocate_special_relocatable for
11855 // R_PPC_PLTREL24 would require duplication of the entire body of the
11856 // loop, so we may as well duplicate the whole thing.
11857 
11858 template<int size, bool big_endian>
11859 void
11860 Target_powerpc<size, big_endian>::relocate_relocs(
11861     const Relocate_info<size, big_endian>* relinfo,
11862     unsigned int sh_type,
11863     const unsigned char* prelocs,
11864     size_t reloc_count,
11865     Output_section* output_section,
11866     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
11867     unsigned char*,
11868     Address view_address,
11869     section_size_type,
11870     unsigned char* reloc_view,
11871     section_size_type reloc_view_size)
11872 {
11873   gold_assert(sh_type == elfcpp::SHT_RELA);
11874 
11875   typedef typename elfcpp::Rela<size, big_endian> Reltype;
11876   typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
11877   const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11878   // Offset from start of insn to d-field reloc.
11879   const int d_offset = big_endian ? 2 : 0;
11880 
11881   Powerpc_relobj<size, big_endian>* const object
11882     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
11883   const unsigned int local_count = object->local_symbol_count();
11884   unsigned int got2_shndx = object->got2_shndx();
11885   Address got2_addend = 0;
11886   if (got2_shndx != 0)
11887     {
11888       got2_addend = object->get_output_section_offset(got2_shndx);
11889       gold_assert(got2_addend != invalid_address);
11890     }
11891 
11892   const bool relocatable = parameters->options().relocatable();
11893 
11894   unsigned char* pwrite = reloc_view;
11895   bool zap_next = false;
11896   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
11897     {
11898       Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
11899       if (strategy == Relocatable_relocs::RELOC_DISCARD)
11900 	continue;
11901 
11902       Reltype reloc(prelocs);
11903       Reltype_write reloc_write(pwrite);
11904 
11905       Address offset = reloc.get_r_offset();
11906       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
11907       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
11908       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
11909       const unsigned int orig_r_sym = r_sym;
11910       typename elfcpp::Elf_types<size>::Elf_Swxword addend
11911 	= reloc.get_r_addend();
11912       const Symbol* gsym = NULL;
11913 
11914       if (zap_next)
11915 	{
11916 	  // We could arrange to discard these and other relocs for
11917 	  // tls optimised sequences in the strategy methods, but for
11918 	  // now do as BFD ld does.
11919 	  r_type = elfcpp::R_POWERPC_NONE;
11920 	  zap_next = false;
11921 	}
11922 
11923       // Get the new symbol index.
11924       Output_section* os = NULL;
11925       if (r_sym < local_count)
11926 	{
11927 	  switch (strategy)
11928 	    {
11929 	    case Relocatable_relocs::RELOC_COPY:
11930 	    case Relocatable_relocs::RELOC_SPECIAL:
11931 	      if (r_sym != 0)
11932 		{
11933 		  r_sym = object->symtab_index(r_sym);
11934 		  gold_assert(r_sym != -1U);
11935 		}
11936 	      break;
11937 
11938 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
11939 	      {
11940 		// We are adjusting a section symbol.  We need to find
11941 		// the symbol table index of the section symbol for
11942 		// the output section corresponding to input section
11943 		// in which this symbol is defined.
11944 		gold_assert(r_sym < local_count);
11945 		bool is_ordinary;
11946 		unsigned int shndx =
11947 		  object->local_symbol_input_shndx(r_sym, &is_ordinary);
11948 		gold_assert(is_ordinary);
11949 		os = object->output_section(shndx);
11950 		gold_assert(os != NULL);
11951 		gold_assert(os->needs_symtab_index());
11952 		r_sym = os->symtab_index();
11953 	      }
11954 	      break;
11955 
11956 	    default:
11957 	      gold_unreachable();
11958 	    }
11959 	}
11960       else
11961 	{
11962 	  gsym = object->global_symbol(r_sym);
11963 	  gold_assert(gsym != NULL);
11964 	  if (gsym->is_forwarder())
11965 	    gsym = relinfo->symtab->resolve_forwards(gsym);
11966 
11967 	  gold_assert(gsym->has_symtab_index());
11968 	  r_sym = gsym->symtab_index();
11969 	}
11970 
11971       // Get the new offset--the location in the output section where
11972       // this relocation should be applied.
11973       if (static_cast<Address>(offset_in_output_section) != invalid_address)
11974 	offset += offset_in_output_section;
11975       else
11976 	{
11977 	  section_offset_type sot_offset =
11978 	    convert_types<section_offset_type, Address>(offset);
11979 	  section_offset_type new_sot_offset =
11980 	    output_section->output_offset(object, relinfo->data_shndx,
11981 					  sot_offset);
11982 	  gold_assert(new_sot_offset != -1);
11983 	  offset = new_sot_offset;
11984 	}
11985 
11986       // In an object file, r_offset is an offset within the section.
11987       // In an executable or dynamic object, generated by
11988       // --emit-relocs, r_offset is an absolute address.
11989       if (!relocatable)
11990 	{
11991 	  offset += view_address;
11992 	  if (static_cast<Address>(offset_in_output_section) != invalid_address)
11993 	    offset -= offset_in_output_section;
11994 	}
11995 
11996       // Handle the reloc addend based on the strategy.
11997       if (strategy == Relocatable_relocs::RELOC_COPY)
11998 	;
11999       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12000 	{
12001 	  const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12002 	  addend = psymval->value(object, addend);
12003 	  // In a relocatable link, the symbol value is relative to
12004 	  // the start of the output section. For a non-relocatable
12005 	  // link, we need to adjust the addend.
12006 	  if (!relocatable)
12007 	    {
12008 	      gold_assert(os != NULL);
12009 	      addend -= os->address();
12010 	    }
12011 	}
12012       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12013 	{
12014 	  if (size == 32)
12015 	    {
12016 	      if (addend >= 32768)
12017 		addend += got2_addend;
12018 	    }
12019 	  else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12020 	    {
12021 	      r_type = elfcpp::R_POWERPC_ADDR16_HA;
12022 	      addend -= d_offset;
12023 	    }
12024 	  else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12025 	    {
12026 	      r_type = elfcpp::R_POWERPC_ADDR16_LO;
12027 	      addend -= d_offset + 4;
12028 	    }
12029 	}
12030       else
12031 	gold_unreachable();
12032 
12033       if (!relocatable)
12034 	{
12035 	  if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12036 	      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12037 	      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12038 	      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12039 	    {
12040 	      // First instruction of a global dynamic sequence,
12041 	      // arg setup insn.
12042 	      const bool final = gsym == NULL || gsym->final_value_is_known();
12043 	      switch (this->optimize_tls_gd(final))
12044 		{
12045 		case tls::TLSOPT_TO_IE:
12046 		  r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12047 			     - elfcpp::R_POWERPC_GOT_TLSGD16);
12048 		  break;
12049 		case tls::TLSOPT_TO_LE:
12050 		  if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12051 		      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12052 		    r_type = elfcpp::R_POWERPC_TPREL16_HA;
12053 		  else
12054 		    {
12055 		      r_type = elfcpp::R_POWERPC_NONE;
12056 		      offset -= d_offset;
12057 		    }
12058 		  break;
12059 		default:
12060 		  break;
12061 		}
12062 	    }
12063 	  else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12064 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12065 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12066 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12067 	    {
12068 	      // First instruction of a local dynamic sequence,
12069 	      // arg setup insn.
12070 	      if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12071 		{
12072 		  if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12073 		      || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12074 		    {
12075 		      r_type = elfcpp::R_POWERPC_TPREL16_HA;
12076 		      const Output_section* os = relinfo->layout->tls_segment()
12077 			->first_section();
12078 		      gold_assert(os != NULL);
12079 		      gold_assert(os->needs_symtab_index());
12080 		      r_sym = os->symtab_index();
12081 		      addend = dtp_offset;
12082 		    }
12083 		  else
12084 		    {
12085 		      r_type = elfcpp::R_POWERPC_NONE;
12086 		      offset -= d_offset;
12087 		    }
12088 		}
12089 	    }
12090 	  else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12091 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12092 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12093 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12094 	    {
12095 	      // First instruction of initial exec sequence.
12096 	      const bool final = gsym == NULL || gsym->final_value_is_known();
12097 	      if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12098 		{
12099 		  if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12100 		      || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12101 		    r_type = elfcpp::R_POWERPC_TPREL16_HA;
12102 		  else
12103 		    {
12104 		      r_type = elfcpp::R_POWERPC_NONE;
12105 		      offset -= d_offset;
12106 		    }
12107 		}
12108 	    }
12109 	  else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12110 		   || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12111 	    {
12112 	      // Second instruction of a global dynamic sequence,
12113 	      // the __tls_get_addr call
12114 	      const bool final = gsym == NULL || gsym->final_value_is_known();
12115 	      switch (this->optimize_tls_gd(final))
12116 		{
12117 		case tls::TLSOPT_TO_IE:
12118 		  r_type = elfcpp::R_POWERPC_NONE;
12119 		  zap_next = true;
12120 		  break;
12121 		case tls::TLSOPT_TO_LE:
12122 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
12123 		  offset += d_offset;
12124 		  zap_next = true;
12125 		  break;
12126 		default:
12127 		  break;
12128 		}
12129 	    }
12130 	  else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12131 		   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12132 	    {
12133 	      // Second instruction of a local dynamic sequence,
12134 	      // the __tls_get_addr call
12135 	      if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
12136 		{
12137 		  const Output_section* os = relinfo->layout->tls_segment()
12138 		    ->first_section();
12139 		  gold_assert(os != NULL);
12140 		  gold_assert(os->needs_symtab_index());
12141 		  r_sym = os->symtab_index();
12142 		  addend = dtp_offset;
12143 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
12144 		  offset += d_offset;
12145 		  zap_next = true;
12146 		}
12147 	    }
12148 	  else if (r_type == elfcpp::R_POWERPC_TLS)
12149 	    {
12150 	      // Second instruction of an initial exec sequence
12151 	      const bool final = gsym == NULL || gsym->final_value_is_known();
12152 	      if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12153 		{
12154 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
12155 		  offset += d_offset;
12156 		}
12157 	    }
12158 	}
12159 
12160       reloc_write.put_r_offset(offset);
12161       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12162       reloc_write.put_r_addend(addend);
12163 
12164       pwrite += reloc_size;
12165     }
12166 
12167   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12168 	      == reloc_view_size);
12169 }
12170 
12171 // Return the value to use for a dynamic symbol which requires special
12172 // treatment.  This is how we support equality comparisons of function
12173 // pointers across shared library boundaries, as described in the
12174 // processor specific ABI supplement.
12175 
12176 template<int size, bool big_endian>
12177 uint64_t
12178 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12179 {
12180   if (size == 32)
12181     {
12182       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12183       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12184 	   p != this->stub_tables_.end();
12185 	   ++p)
12186 	{
12187 	  const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12188 	    = (*p)->find_plt_call_entry(gsym);
12189 	  if (ent != NULL)
12190 	    return (*p)->stub_address() + ent->off_;
12191 	}
12192     }
12193   else if (this->abiversion() >= 2)
12194     {
12195       Address off = this->glink_section()->find_global_entry(gsym);
12196       if (off != invalid_address)
12197 	return this->glink_section()->global_entry_address() + off;
12198     }
12199   gold_unreachable();
12200 }
12201 
12202 // Return the PLT address to use for a local symbol.
12203 template<int size, bool big_endian>
12204 uint64_t
12205 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12206     const Relobj* object,
12207     unsigned int symndx) const
12208 {
12209   if (size == 32)
12210     {
12211       const Sized_relobj<size, big_endian>* relobj
12212 	= static_cast<const Sized_relobj<size, big_endian>*>(object);
12213       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12214 	   p != this->stub_tables_.end();
12215 	   ++p)
12216 	{
12217 	  const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12218 	    = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12219 	  if (ent != NULL)
12220 	    return (*p)->stub_address() + ent->off_;
12221 	}
12222     }
12223   gold_unreachable();
12224 }
12225 
12226 // Return the PLT address to use for a global symbol.
12227 template<int size, bool big_endian>
12228 uint64_t
12229 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12230     const Symbol* gsym) const
12231 {
12232   if (size == 32)
12233     {
12234       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12235 	   p != this->stub_tables_.end();
12236 	   ++p)
12237 	{
12238 	  const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12239 	    = (*p)->find_plt_call_entry(gsym);
12240 	  if (ent != NULL)
12241 	    return (*p)->stub_address() + ent->off_;
12242 	}
12243     }
12244   else if (this->abiversion() >= 2)
12245     {
12246       Address off = this->glink_section()->find_global_entry(gsym);
12247       if (off != invalid_address)
12248 	return this->glink_section()->global_entry_address() + off;
12249     }
12250   gold_unreachable();
12251 }
12252 
12253 // Return the offset to use for the GOT_INDX'th got entry which is
12254 // for a local tls symbol specified by OBJECT, SYMNDX.
12255 template<int size, bool big_endian>
12256 int64_t
12257 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12258     const Relobj* object,
12259     unsigned int symndx,
12260     unsigned int got_indx) const
12261 {
12262   const Powerpc_relobj<size, big_endian>* ppc_object
12263     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12264   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12265     {
12266       for (Got_type got_type = GOT_TYPE_TLSGD;
12267 	   got_type <= GOT_TYPE_TPREL;
12268 	   got_type = Got_type(got_type + 1))
12269 	if (ppc_object->local_has_got_offset(symndx, got_type))
12270 	  {
12271 	    unsigned int off = ppc_object->local_got_offset(symndx, got_type);
12272 	    if (got_type == GOT_TYPE_TLSGD)
12273 	      off += size / 8;
12274 	    if (off == got_indx * (size / 8))
12275 	      {
12276 		if (got_type == GOT_TYPE_TPREL)
12277 		  return -tp_offset;
12278 		else
12279 		  return -dtp_offset;
12280 	      }
12281 	  }
12282     }
12283   gold_unreachable();
12284 }
12285 
12286 // Return the offset to use for the GOT_INDX'th got entry which is
12287 // for global tls symbol GSYM.
12288 template<int size, bool big_endian>
12289 int64_t
12290 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
12291     Symbol* gsym,
12292     unsigned int got_indx) const
12293 {
12294   if (gsym->type() == elfcpp::STT_TLS)
12295     {
12296       for (Got_type got_type = GOT_TYPE_TLSGD;
12297 	   got_type <= GOT_TYPE_TPREL;
12298 	   got_type = Got_type(got_type + 1))
12299 	if (gsym->has_got_offset(got_type))
12300 	  {
12301 	    unsigned int off = gsym->got_offset(got_type);
12302 	    if (got_type == GOT_TYPE_TLSGD)
12303 	      off += size / 8;
12304 	    if (off == got_indx * (size / 8))
12305 	      {
12306 		if (got_type == GOT_TYPE_TPREL)
12307 		  return -tp_offset;
12308 		else
12309 		  return -dtp_offset;
12310 	      }
12311 	  }
12312     }
12313   gold_unreachable();
12314 }
12315 
12316 // The selector for powerpc object files.
12317 
12318 template<int size, bool big_endian>
12319 class Target_selector_powerpc : public Target_selector
12320 {
12321 public:
12322   Target_selector_powerpc()
12323     : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
12324 		      size, big_endian,
12325 		      (size == 64
12326 		       ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
12327 		       : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
12328 		      (size == 64
12329 		       ? (big_endian ? "elf64ppc" : "elf64lppc")
12330 		       : (big_endian ? "elf32ppc" : "elf32lppc")))
12331   { }
12332 
12333   virtual Target*
12334   do_instantiate_target()
12335   { return new Target_powerpc<size, big_endian>(); }
12336 };
12337 
12338 Target_selector_powerpc<32, true> target_selector_ppc32;
12339 Target_selector_powerpc<32, false> target_selector_ppc32le;
12340 Target_selector_powerpc<64, true> target_selector_ppc64;
12341 Target_selector_powerpc<64, false> target_selector_ppc64le;
12342 
12343 // Instantiate these constants for -O0
12344 template<int size, bool big_endian>
12345 const typename Output_data_glink<size, big_endian>::Address
12346   Output_data_glink<size, big_endian>::invalid_address;
12347 template<int size, bool big_endian>
12348 const typename Stub_table<size, big_endian>::Address
12349   Stub_table<size, big_endian>::invalid_address;
12350 template<int size, bool big_endian>
12351 const typename Target_powerpc<size, big_endian>::Address
12352   Target_powerpc<size, big_endian>::invalid_address;
12353 
12354 } // End anonymous namespace.
12355