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