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