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