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