* options.h (General_options): Add --toc-sort/--no-toc-sort.
[platform/upstream/binutils.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright 2008, 2009, 2010, 2011, 2012 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 <algorithm>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "powerpc.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "errors.h"
41 #include "gc.h"
42
43 namespace
44 {
45
46 using namespace gold;
47
48 template<int size, bool big_endian>
49 class Output_data_plt_powerpc;
50
51 template<int size, bool big_endian>
52 class Output_data_brlt_powerpc;
53
54 template<int size, bool big_endian>
55 class Output_data_got_powerpc;
56
57 template<int size, bool big_endian>
58 class Output_data_glink;
59
60 template<int size, bool big_endian>
61 class Stub_table;
62
63 template<int size, bool big_endian>
64 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
65 {
66 public:
67   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
68   typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
69   typedef Unordered_map<Address, Section_refs> Access_from;
70
71   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
72                  const typename elfcpp::Ehdr<size, big_endian>& ehdr)
73     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
74       special_(0), has_small_toc_reloc_(false), opd_valid_(false),
75       opd_ent_(), access_from_map_(), has14_(), stub_table_()
76   { }
77
78   ~Powerpc_relobj()
79   { }
80
81   // The .got2 section shndx.
82   unsigned int
83   got2_shndx() const
84   {
85     if (size == 32)
86       return this->special_;
87     else
88       return 0;
89   }
90
91   // The .opd section shndx.
92   unsigned int
93   opd_shndx() const
94   {
95     if (size == 32)
96       return 0;
97     else
98       return this->special_;
99   }
100
101   // Init OPD entry arrays.
102   void
103   init_opd(size_t opd_size)
104   {
105     size_t count = this->opd_ent_ndx(opd_size);
106     this->opd_ent_.resize(count);
107   }
108
109   // Return section and offset of function entry for .opd + R_OFF.
110   unsigned int
111   get_opd_ent(Address r_off, Address* value = NULL) const
112   {
113     size_t ndx = this->opd_ent_ndx(r_off);
114     gold_assert(ndx < this->opd_ent_.size());
115     gold_assert(this->opd_ent_[ndx].shndx != 0);
116     if (value != NULL)
117       *value = this->opd_ent_[ndx].off;
118     return this->opd_ent_[ndx].shndx;
119   }
120
121   // Set section and offset of function entry for .opd + R_OFF.
122   void
123   set_opd_ent(Address r_off, unsigned int shndx, Address value)
124   {
125     size_t ndx = this->opd_ent_ndx(r_off);
126     gold_assert(ndx < this->opd_ent_.size());
127     this->opd_ent_[ndx].shndx = shndx;
128     this->opd_ent_[ndx].off = value;
129   }
130
131   // Return discard flag for .opd + R_OFF.
132   bool
133   get_opd_discard(Address r_off) const
134   {
135     size_t ndx = this->opd_ent_ndx(r_off);
136     gold_assert(ndx < this->opd_ent_.size());
137     return this->opd_ent_[ndx].discard;
138   }
139
140   // Set discard flag for .opd + R_OFF.
141   void
142   set_opd_discard(Address r_off)
143   {
144     size_t ndx = this->opd_ent_ndx(r_off);
145     gold_assert(ndx < this->opd_ent_.size());
146     this->opd_ent_[ndx].discard = true;
147   }
148
149   Access_from*
150   access_from_map()
151   { return &this->access_from_map_; }
152
153   // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
154   // section at DST_OFF.
155   void
156   add_reference(Object* src_obj,
157                 unsigned int src_indx,
158                 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
159   {
160     Section_id src_id(src_obj, src_indx);
161     this->access_from_map_[dst_off].insert(src_id);
162   }
163
164   // Add a reference to the code section specified by the .opd entry
165   // at DST_OFF
166   void
167   add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
168   {
169     size_t ndx = this->opd_ent_ndx(dst_off);
170     if (ndx >= this->opd_ent_.size())
171       this->opd_ent_.resize(ndx + 1);
172     this->opd_ent_[ndx].gc_mark = true;
173   }
174
175   void
176   process_gc_mark(Symbol_table* symtab)
177   {
178     for (size_t i = 0; i < this->opd_ent_.size(); i++)
179       if (this->opd_ent_[i].gc_mark)
180         {
181           unsigned int shndx = this->opd_ent_[i].shndx;
182           symtab->gc()->worklist().push(Section_id(this, shndx));
183         }
184   }
185
186   bool
187   opd_valid() const
188   { return this->opd_valid_; }
189
190   void
191   set_opd_valid()
192   { this->opd_valid_ = true; }
193
194   // Examine .rela.opd to build info about function entry points.
195   void
196   scan_opd_relocs(size_t reloc_count,
197                   const unsigned char* prelocs,
198                   const unsigned char* plocal_syms);
199
200   // Perform the Sized_relobj_file method, then set up opd info from
201   // .opd relocs.
202   void
203   do_read_relocs(Read_relocs_data*);
204
205   bool
206   do_find_special_sections(Read_symbols_data* sd);
207
208   // Adjust this local symbol value.  Return false if the symbol
209   // should be discarded from the output file.
210   bool
211   do_adjust_local_symbol(Symbol_value<size>* lv) const
212   {
213     if (size == 64 && this->opd_shndx() != 0)
214       {
215         bool is_ordinary;
216         if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
217           return true;
218         if (this->get_opd_discard(lv->input_value()))
219           return false;
220       }
221     return true;
222   }
223
224   // Return offset in output GOT section that this object will use
225   // as a TOC pointer.  Won't be just a constant with multi-toc support.
226   Address
227   toc_base_offset() const
228   { return 0x8000; }
229
230   void
231   set_has_small_toc_reloc()
232   { has_small_toc_reloc_ = true; }
233
234   bool
235   has_small_toc_reloc() const
236   { return has_small_toc_reloc_; }
237
238   void
239   set_has_14bit_branch(unsigned int shndx)
240   {
241     if (shndx >= this->has14_.size())
242       this->has14_.resize(shndx + 1);
243     this->has14_[shndx] = true;
244   }
245
246   bool
247   has_14bit_branch(unsigned int shndx) const
248   { return shndx < this->has14_.size() && this->has14_[shndx];  }
249
250   void
251   set_stub_table(unsigned int shndx, Stub_table<size, big_endian>* stub_table)
252   {
253     if (shndx >= this->stub_table_.size())
254       this->stub_table_.resize(shndx + 1);
255     this->stub_table_[shndx] = stub_table;
256   }
257
258   Stub_table<size, big_endian>*
259   stub_table(unsigned int shndx)
260   {
261     if (shndx < this->stub_table_.size())
262       return this->stub_table_[shndx];
263     return NULL;
264   }
265
266 private:
267   struct Opd_ent
268   {
269     unsigned int shndx;
270     bool discard : 1;
271     bool gc_mark : 1;
272     Address off;
273   };
274
275   // Return index into opd_ent_ array for .opd entry at OFF.
276   // .opd entries are 24 bytes long, but they can be spaced 16 bytes
277   // apart when the language doesn't use the last 8-byte word, the
278   // environment pointer.  Thus dividing the entry section offset by
279   // 16 will give an index into opd_ent_ that works for either layout
280   // of .opd.  (It leaves some elements of the vector unused when .opd
281   // entries are spaced 24 bytes apart, but we don't know the spacing
282   // until relocations are processed, and in any case it is possible
283   // for an object to have some entries spaced 16 bytes apart and
284   // others 24 bytes apart.)
285   size_t
286   opd_ent_ndx(size_t off) const
287   { return off >> 4;}
288
289   // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
290   unsigned int special_;
291
292   // For 64-bit, whether this object uses small model relocs to access
293   // the toc.
294   bool has_small_toc_reloc_;
295
296   // Set at the start of gc_process_relocs, when we know opd_ent_
297   // vector is valid.  The flag could be made atomic and set in
298   // do_read_relocs with memory_order_release and then tested with
299   // memory_order_acquire, potentially resulting in fewer entries in
300   // access_from_map_.
301   bool opd_valid_;
302
303   // The first 8-byte word of an OPD entry gives the address of the
304   // entry point of the function.  Relocatable object files have a
305   // relocation on this word.  The following vector records the
306   // section and offset specified by these relocations.
307   std::vector<Opd_ent> opd_ent_;
308
309   // References made to this object's .opd section when running
310   // gc_process_relocs for another object, before the opd_ent_ vector
311   // is valid for this object.
312   Access_from access_from_map_;
313
314   // Whether input section has a 14-bit branch reloc.
315   std::vector<bool> has14_;
316
317   // The stub table to use for a given input section.
318   std::vector<Stub_table<size, big_endian>*> stub_table_;
319 };
320
321 template<int size, bool big_endian>
322 class Target_powerpc : public Sized_target<size, big_endian>
323 {
324  public:
325   typedef
326     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
327   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
328   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
329   static const Address invalid_address = static_cast<Address>(0) - 1;
330   // Offset of tp and dtp pointers from start of TLS block.
331   static const Address tp_offset = 0x7000;
332   static const Address dtp_offset = 0x8000;
333
334   Target_powerpc()
335     : Sized_target<size, big_endian>(&powerpc_info),
336       got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL),
337       glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY),
338       dynbss_(NULL), tlsld_got_offset_(-1U),
339       stub_tables_(), branch_lookup_table_(), branch_info_(),
340       plt_thread_safe_(false)
341   {
342   }
343
344   // Process the relocations to determine unreferenced sections for
345   // garbage collection.
346   void
347   gc_process_relocs(Symbol_table* symtab,
348                     Layout* layout,
349                     Sized_relobj_file<size, big_endian>* object,
350                     unsigned int data_shndx,
351                     unsigned int sh_type,
352                     const unsigned char* prelocs,
353                     size_t reloc_count,
354                     Output_section* output_section,
355                     bool needs_special_offset_handling,
356                     size_t local_symbol_count,
357                     const unsigned char* plocal_symbols);
358
359   // Scan the relocations to look for symbol adjustments.
360   void
361   scan_relocs(Symbol_table* symtab,
362               Layout* layout,
363               Sized_relobj_file<size, big_endian>* object,
364               unsigned int data_shndx,
365               unsigned int sh_type,
366               const unsigned char* prelocs,
367               size_t reloc_count,
368               Output_section* output_section,
369               bool needs_special_offset_handling,
370               size_t local_symbol_count,
371               const unsigned char* plocal_symbols);
372
373   // Map input .toc section to output .got section.
374   const char*
375   do_output_section_name(const Relobj*, const char* name, size_t* plen) const
376   {
377     if (size == 64 && strcmp(name, ".toc") == 0)
378       {
379         *plen = 4;
380         return ".got";
381       }
382     return NULL;
383   }
384
385   // Provide linker defined save/restore functions.
386   void
387   define_save_restore_funcs(Layout*, Symbol_table*);
388
389   // No stubs unless a final link.
390   bool
391   do_may_relax() const
392   { return !parameters->options().relocatable(); }
393
394   bool
395   do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
396
397   // Stash info about branches, for stub generation.
398   void
399   push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
400               unsigned int data_shndx, Address r_offset,
401               unsigned int r_type, unsigned int r_sym, Address addend)
402   {
403     Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
404     this->branch_info_.push_back(info);
405     if (r_type == elfcpp::R_POWERPC_REL14
406         || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
407         || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
408       ppc_object->set_has_14bit_branch(data_shndx);
409   }
410
411   Stub_table<size, big_endian>*
412   new_stub_table();
413
414   void
415   do_define_standard_symbols(Symbol_table*, Layout*);
416
417   // Finalize the sections.
418   void
419   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
420
421   // Return the value to use for a dynamic which requires special
422   // treatment.
423   uint64_t
424   do_dynsym_value(const Symbol*) const;
425
426   // Return the PLT address to use for a local symbol.
427   uint64_t
428   do_plt_address_for_local(const Relobj*, unsigned int) const;
429
430   // Return the PLT address to use for a global symbol.
431   uint64_t
432   do_plt_address_for_global(const Symbol*) const;
433
434   // Return the offset to use for the GOT_INDX'th got entry which is
435   // for a local tls symbol specified by OBJECT, SYMNDX.
436   int64_t
437   do_tls_offset_for_local(const Relobj* object,
438                           unsigned int symndx,
439                           unsigned int got_indx) const;
440
441   // Return the offset to use for the GOT_INDX'th got entry which is
442   // for global tls symbol GSYM.
443   int64_t
444   do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
445
446   // Relocate a section.
447   void
448   relocate_section(const Relocate_info<size, big_endian>*,
449                    unsigned int sh_type,
450                    const unsigned char* prelocs,
451                    size_t reloc_count,
452                    Output_section* output_section,
453                    bool needs_special_offset_handling,
454                    unsigned char* view,
455                    Address view_address,
456                    section_size_type view_size,
457                    const Reloc_symbol_changes*);
458
459   // Scan the relocs during a relocatable link.
460   void
461   scan_relocatable_relocs(Symbol_table* symtab,
462                           Layout* layout,
463                           Sized_relobj_file<size, big_endian>* object,
464                           unsigned int data_shndx,
465                           unsigned int sh_type,
466                           const unsigned char* prelocs,
467                           size_t reloc_count,
468                           Output_section* output_section,
469                           bool needs_special_offset_handling,
470                           size_t local_symbol_count,
471                           const unsigned char* plocal_symbols,
472                           Relocatable_relocs*);
473
474   // Emit relocations for a section.
475   void
476   relocate_relocs(const Relocate_info<size, big_endian>*,
477                   unsigned int sh_type,
478                   const unsigned char* prelocs,
479                   size_t reloc_count,
480                   Output_section* output_section,
481                   typename elfcpp::Elf_types<size>::Elf_Off
482                     offset_in_output_section,
483                   const Relocatable_relocs*,
484                   unsigned char*,
485                   Address view_address,
486                   section_size_type,
487                   unsigned char* reloc_view,
488                   section_size_type reloc_view_size);
489
490   // Return whether SYM is defined by the ABI.
491   bool
492   do_is_defined_by_abi(const Symbol* sym) const
493   {
494     return strcmp(sym->name(), "__tls_get_addr") == 0;
495   }
496
497   // Return the size of the GOT section.
498   section_size_type
499   got_size() const
500   {
501     gold_assert(this->got_ != NULL);
502     return this->got_->data_size();
503   }
504
505   // Get the PLT section.
506   const Output_data_plt_powerpc<size, big_endian>*
507   plt_section() const
508   {
509     gold_assert(this->plt_ != NULL);
510     return this->plt_;
511   }
512
513   // Get the IPLT section.
514   const Output_data_plt_powerpc<size, big_endian>*
515   iplt_section() const
516   {
517     gold_assert(this->iplt_ != NULL);
518     return this->iplt_;
519   }
520
521   // Get the .glink section.
522   const Output_data_glink<size, big_endian>*
523   glink_section() const
524   {
525     gold_assert(this->glink_ != NULL);
526     return this->glink_;
527   }
528
529   // Get the GOT section.
530   const Output_data_got_powerpc<size, big_endian>*
531   got_section() const
532   {
533     gold_assert(this->got_ != NULL);
534     return this->got_;
535   }
536
537   // Get the GOT section, creating it if necessary.
538   Output_data_got_powerpc<size, big_endian>*
539   got_section(Symbol_table*, Layout*);
540
541   Object*
542   do_make_elf_object(const std::string&, Input_file*, off_t,
543                      const elfcpp::Ehdr<size, big_endian>&);
544
545   // Return the number of entries in the GOT.
546   unsigned int
547   got_entry_count() const
548   {
549     if (this->got_ == NULL)
550       return 0;
551     return this->got_size() / (size / 8);
552   }
553
554   // Return the number of entries in the PLT.
555   unsigned int
556   plt_entry_count() const;
557
558   // Return the offset of the first non-reserved PLT entry.
559   unsigned int
560   first_plt_entry_offset() const;
561
562   // Return the size of each PLT entry.
563   unsigned int
564   plt_entry_size() const;
565
566   // Add any special sections for this symbol to the gc work list.
567   // For powerpc64, this adds the code section of a function
568   // descriptor.
569   void
570   do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
571
572   // Handle target specific gc actions when adding a gc reference from
573   // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
574   // and DST_OFF.  For powerpc64, this adds a referenc to the code
575   // section of a function descriptor.
576   void
577   do_gc_add_reference(Symbol_table* symtab,
578                       Object* src_obj,
579                       unsigned int src_shndx,
580                       Object* dst_obj,
581                       unsigned int dst_shndx,
582                       Address dst_off) const;
583
584   typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
585   const Stub_tables&
586   stub_tables() const
587   { return this->stub_tables_; }
588
589   const Output_data_brlt_powerpc<size, big_endian>*
590   brlt_section() const
591   { return this->brlt_section_; }
592
593   void
594   add_branch_lookup_table(Address to)
595   {
596     unsigned int off = this->branch_lookup_table_.size() * (size / 8);
597     this->branch_lookup_table_.insert(std::make_pair(to, off));
598   }
599
600   Address
601   find_branch_lookup_table(Address to)
602   {
603     typename Branch_lookup_table::const_iterator p
604       = this->branch_lookup_table_.find(to);
605     return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
606   }
607
608   void
609   write_branch_lookup_table(unsigned char *oview)
610   {
611     for (typename Branch_lookup_table::const_iterator p
612            = this->branch_lookup_table_.begin();
613          p != this->branch_lookup_table_.end();
614          ++p)
615       {
616         elfcpp::Swap<32, big_endian>::writeval(oview + p->second, p->first);
617       }
618   }
619
620   bool
621   plt_thread_safe() const
622   { return this->plt_thread_safe_; }
623
624  private:
625
626   // The class which scans relocations.
627   class Scan
628   {
629   public:
630     typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
631
632     Scan()
633       : issued_non_pic_error_(false)
634     { }
635
636     static inline int
637     get_reference_flags(unsigned int r_type);
638
639     inline void
640     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
641           Sized_relobj_file<size, big_endian>* object,
642           unsigned int data_shndx,
643           Output_section* output_section,
644           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
645           const elfcpp::Sym<size, big_endian>& lsym,
646           bool is_discarded);
647
648     inline void
649     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
650            Sized_relobj_file<size, big_endian>* object,
651            unsigned int data_shndx,
652            Output_section* output_section,
653            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
654            Symbol* gsym);
655
656     inline bool
657     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
658                                         Target_powerpc* ,
659                                         Sized_relobj_file<size, big_endian>* ,
660                                         unsigned int ,
661                                         Output_section* ,
662                                         const elfcpp::Rela<size, big_endian>& ,
663                                         unsigned int ,
664                                         const elfcpp::Sym<size, big_endian>&)
665     { return false; }
666
667     inline bool
668     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
669                                          Target_powerpc* ,
670                                          Sized_relobj_file<size, big_endian>* ,
671                                          unsigned int ,
672                                          Output_section* ,
673                                          const elfcpp::Rela<size,
674                                                             big_endian>& ,
675                                          unsigned int , Symbol*)
676     { return false; }
677
678   private:
679     static void
680     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
681                             unsigned int r_type);
682
683     static void
684     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
685                              unsigned int r_type, Symbol*);
686
687     static void
688     generate_tls_call(Symbol_table* symtab, Layout* layout,
689                       Target_powerpc* target);
690
691     void
692     check_non_pic(Relobj*, unsigned int r_type);
693
694     bool
695     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
696                               unsigned int r_type);
697
698     // Whether we have issued an error about a non-PIC compilation.
699     bool issued_non_pic_error_;
700   };
701
702   Address
703   symval_for_branch(Address value, const Sized_symbol<size>* gsym,
704                     Powerpc_relobj<size, big_endian>* object,
705                     unsigned int *dest_shndx);
706
707   // The class which implements relocation.
708   class Relocate
709   {
710    public:
711     // Use 'at' branch hints when true, 'y' when false.
712     // FIXME maybe: set this with an option.
713     static const bool is_isa_v2 = true;
714
715     enum skip_tls
716     {
717       CALL_NOT_EXPECTED = 0,
718       CALL_EXPECTED = 1,
719       CALL_SKIP = 2
720     };
721
722     Relocate()
723       : call_tls_get_addr_(CALL_NOT_EXPECTED)
724     { }
725
726     ~Relocate()
727     {
728       if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
729         {
730           // FIXME: This needs to specify the location somehow.
731           gold_error(_("missing expected __tls_get_addr call"));
732         }
733     }
734
735     // Do a relocation.  Return false if the caller should not issue
736     // any warnings about this relocation.
737     inline bool
738     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
739              Output_section*, size_t relnum,
740              const elfcpp::Rela<size, big_endian>&,
741              unsigned int r_type, const Sized_symbol<size>*,
742              const Symbol_value<size>*,
743              unsigned char*,
744              typename elfcpp::Elf_types<size>::Elf_Addr,
745              section_size_type);
746
747     // This is set if we should skip the next reloc, which should be a
748     // call to __tls_get_addr.
749     enum skip_tls call_tls_get_addr_;
750   };
751
752   class Relocate_comdat_behavior
753   {
754    public:
755     // Decide what the linker should do for relocations that refer to
756     // discarded comdat sections.
757     inline Comdat_behavior
758     get(const char* name)
759     {
760       gold::Default_comdat_behavior default_behavior;
761       Comdat_behavior ret = default_behavior.get(name);
762       if (ret == CB_WARNING)
763         {
764           if (size == 32
765               && (strcmp(name, ".fixup") == 0
766                   || strcmp(name, ".got2") == 0))
767             ret = CB_IGNORE;
768           if (size == 64
769               && (strcmp(name, ".opd") == 0
770                   || strcmp(name, ".toc") == 0
771                   || strcmp(name, ".toc1") == 0))
772             ret = CB_IGNORE;
773         }
774       return ret;
775     }
776   };
777
778   // A class which returns the size required for a relocation type,
779   // used while scanning relocs during a relocatable link.
780   class Relocatable_size_for_reloc
781   {
782    public:
783     unsigned int
784     get_size_for_reloc(unsigned int, Relobj*)
785     {
786       gold_unreachable();
787       return 0;
788     }
789   };
790
791   // Optimize the TLS relocation type based on what we know about the
792   // symbol.  IS_FINAL is true if the final address of this symbol is
793   // known at link time.
794
795   tls::Tls_optimization
796   optimize_tls_gd(bool is_final)
797   {
798     // If we are generating a shared library, then we can't do anything
799     // in the linker.
800     if (parameters->options().shared())
801       return tls::TLSOPT_NONE;
802
803     if (!is_final)
804       return tls::TLSOPT_TO_IE;
805     return tls::TLSOPT_TO_LE;
806   }
807
808   tls::Tls_optimization
809   optimize_tls_ld()
810   {
811     if (parameters->options().shared())
812       return tls::TLSOPT_NONE;
813
814     return tls::TLSOPT_TO_LE;
815   }
816
817   tls::Tls_optimization
818   optimize_tls_ie(bool is_final)
819   {
820     if (!is_final || parameters->options().shared())
821       return tls::TLSOPT_NONE;
822
823     return tls::TLSOPT_TO_LE;
824   }
825
826   // Create glink.
827   void
828   make_glink_section(Layout*);
829
830   // Create the PLT section.
831   void
832   make_plt_section(Symbol_table*, Layout*);
833
834   void
835   make_iplt_section(Symbol_table*, Layout*);
836
837   void
838   make_brlt_section(Layout*);
839
840   // Create a PLT entry for a global symbol.
841   void
842   make_plt_entry(Symbol_table*, Layout*, Symbol*);
843
844   // Create a PLT entry for a local IFUNC symbol.
845   void
846   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
847                              Sized_relobj_file<size, big_endian>*,
848                              unsigned int);
849
850
851   // Create a GOT entry for local dynamic __tls_get_addr.
852   unsigned int
853   tlsld_got_offset(Symbol_table* symtab, Layout* layout,
854                    Sized_relobj_file<size, big_endian>* object);
855
856   unsigned int
857   tlsld_got_offset() const
858   {
859     return this->tlsld_got_offset_;
860   }
861
862   // Get the dynamic reloc section, creating it if necessary.
863   Reloc_section*
864   rela_dyn_section(Layout*);
865
866   // Copy a relocation against a global symbol.
867   void
868   copy_reloc(Symbol_table* symtab, Layout* layout,
869              Sized_relobj_file<size, big_endian>* object,
870              unsigned int shndx, Output_section* output_section,
871              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
872   {
873     this->copy_relocs_.copy_reloc(symtab, layout,
874                                   symtab->get_sized_symbol<size>(sym),
875                                   object, shndx, output_section,
876                                   reloc, this->rela_dyn_section(layout));
877   }
878
879   // Look over all the input sections, deciding where to place stub.
880   void
881   group_sections(Layout*, const Task*);
882
883   // Sort output sections by address.
884   struct Sort_sections
885   {
886     bool
887     operator()(const Output_section* sec1, const Output_section* sec2)
888     { return sec1->address() < sec2->address(); }
889   };
890
891   class Branch_info
892   {
893    public:
894     Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
895                 unsigned int data_shndx,
896                 Address r_offset,
897                 unsigned int r_type,
898                 unsigned int r_sym,
899                 Address addend)
900       : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
901         r_type_(r_type), r_sym_(r_sym), addend_(addend)
902     { }
903
904     ~Branch_info()
905     { }
906
907     // If this branch needs a plt call stub, or a long branch stub, make one.
908     void
909     make_stub(Stub_table<size, big_endian>*,
910               Stub_table<size, big_endian>*,
911               Symbol_table*) const;
912
913    private:
914     // The branch location..
915     Powerpc_relobj<size, big_endian>* object_;
916     unsigned int shndx_;
917     Address offset_;
918     // ..and the branch type and destination.
919     unsigned int r_type_;
920     unsigned int r_sym_;
921     Address addend_;
922   };
923
924   // Information about this specific target which we pass to the
925   // general Target structure.
926   static Target::Target_info powerpc_info;
927
928   // The types of GOT entries needed for this platform.
929   // These values are exposed to the ABI in an incremental link.
930   // Do not renumber existing values without changing the version
931   // number of the .gnu_incremental_inputs section.
932   enum Got_type
933   {
934     GOT_TYPE_STANDARD,
935     GOT_TYPE_TLSGD,     // double entry for @got@tlsgd
936     GOT_TYPE_DTPREL,    // entry for @got@dtprel
937     GOT_TYPE_TPREL      // entry for @got@tprel
938   };
939
940   // The GOT section.
941   Output_data_got_powerpc<size, big_endian>* got_;
942   // The PLT section.
943   Output_data_plt_powerpc<size, big_endian>* plt_;
944   // The IPLT section.
945   Output_data_plt_powerpc<size, big_endian>* iplt_;
946   // Section holding long branch destinations.
947   Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
948   // The .glink section.
949   Output_data_glink<size, big_endian>* glink_;
950   // The dynamic reloc section.
951   Reloc_section* rela_dyn_;
952   // Relocs saved to avoid a COPY reloc.
953   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
954   // Space for variables copied with a COPY reloc.
955   Output_data_space* dynbss_;
956   // Offset of the GOT entry for local dynamic __tls_get_addr calls.
957   unsigned int tlsld_got_offset_;
958
959   Stub_tables stub_tables_;
960   typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
961   Branch_lookup_table branch_lookup_table_;
962
963   typedef std::vector<Branch_info> Branches;
964   Branches branch_info_;
965
966   bool plt_thread_safe_;
967 };
968
969 template<>
970 Target::Target_info Target_powerpc<32, true>::powerpc_info =
971 {
972   32,                   // size
973   true,                 // is_big_endian
974   elfcpp::EM_PPC,       // machine_code
975   false,                // has_make_symbol
976   false,                // has_resolve
977   false,                // has_code_fill
978   true,                 // is_default_stack_executable
979   false,                // can_icf_inline_merge_sections
980   '\0',                 // wrap_char
981   "/usr/lib/ld.so.1",   // dynamic_linker
982   0x10000000,           // default_text_segment_address
983   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
984   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
985   false,                // isolate_execinstr
986   0,                    // rosegment_gap
987   elfcpp::SHN_UNDEF,    // small_common_shndx
988   elfcpp::SHN_UNDEF,    // large_common_shndx
989   0,                    // small_common_section_flags
990   0,                    // large_common_section_flags
991   NULL,                 // attributes_section
992   NULL                  // attributes_vendor
993 };
994
995 template<>
996 Target::Target_info Target_powerpc<32, false>::powerpc_info =
997 {
998   32,                   // size
999   false,                // is_big_endian
1000   elfcpp::EM_PPC,       // machine_code
1001   false,                // has_make_symbol
1002   false,                // has_resolve
1003   false,                // has_code_fill
1004   true,                 // is_default_stack_executable
1005   false,                // can_icf_inline_merge_sections
1006   '\0',                 // wrap_char
1007   "/usr/lib/ld.so.1",   // dynamic_linker
1008   0x10000000,           // default_text_segment_address
1009   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1010   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1011   false,                // isolate_execinstr
1012   0,                    // rosegment_gap
1013   elfcpp::SHN_UNDEF,    // small_common_shndx
1014   elfcpp::SHN_UNDEF,    // large_common_shndx
1015   0,                    // small_common_section_flags
1016   0,                    // large_common_section_flags
1017   NULL,                 // attributes_section
1018   NULL                  // attributes_vendor
1019 };
1020
1021 template<>
1022 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1023 {
1024   64,                   // size
1025   true,                 // is_big_endian
1026   elfcpp::EM_PPC64,     // machine_code
1027   false,                // has_make_symbol
1028   false,                // has_resolve
1029   false,                // has_code_fill
1030   true,                 // is_default_stack_executable
1031   false,                // can_icf_inline_merge_sections
1032   '\0',                 // wrap_char
1033   "/usr/lib/ld.so.1",   // dynamic_linker
1034   0x10000000,           // default_text_segment_address
1035   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1036   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1037   false,                // isolate_execinstr
1038   0,                    // rosegment_gap
1039   elfcpp::SHN_UNDEF,    // small_common_shndx
1040   elfcpp::SHN_UNDEF,    // large_common_shndx
1041   0,                    // small_common_section_flags
1042   0,                    // large_common_section_flags
1043   NULL,                 // attributes_section
1044   NULL                  // attributes_vendor
1045 };
1046
1047 template<>
1048 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1049 {
1050   64,                   // size
1051   false,                // is_big_endian
1052   elfcpp::EM_PPC64,     // machine_code
1053   false,                // has_make_symbol
1054   false,                // has_resolve
1055   false,                // has_code_fill
1056   true,                 // is_default_stack_executable
1057   false,                // can_icf_inline_merge_sections
1058   '\0',                 // wrap_char
1059   "/usr/lib/ld.so.1",   // dynamic_linker
1060   0x10000000,           // default_text_segment_address
1061   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1062   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1063   false,                // isolate_execinstr
1064   0,                    // rosegment_gap
1065   elfcpp::SHN_UNDEF,    // small_common_shndx
1066   elfcpp::SHN_UNDEF,    // large_common_shndx
1067   0,                    // small_common_section_flags
1068   0,                    // large_common_section_flags
1069   NULL,                 // attributes_section
1070   NULL                  // attributes_vendor
1071 };
1072
1073 inline bool
1074 is_branch_reloc(unsigned int r_type)
1075 {
1076   return (r_type == elfcpp::R_POWERPC_REL24
1077           || r_type == elfcpp::R_PPC_PLTREL24
1078           || r_type == elfcpp::R_PPC_LOCAL24PC
1079           || r_type == elfcpp::R_POWERPC_REL14
1080           || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1081           || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1082           || r_type == elfcpp::R_POWERPC_ADDR24
1083           || r_type == elfcpp::R_POWERPC_ADDR14
1084           || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1085           || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1086 }
1087
1088 // If INSN is an opcode that may be used with an @tls operand, return
1089 // the transformed insn for TLS optimisation, otherwise return 0.  If
1090 // REG is non-zero only match an insn with RB or RA equal to REG.
1091 uint32_t
1092 at_tls_transform(uint32_t insn, unsigned int reg)
1093 {
1094   if ((insn & (0x3f << 26)) != 31 << 26)
1095     return 0;
1096
1097   unsigned int rtra;
1098   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1099     rtra = insn & ((1 << 26) - (1 << 16));
1100   else if (((insn >> 16) & 0x1f) == reg)
1101     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1102   else
1103     return 0;
1104
1105   if ((insn & (0x3ff << 1)) == 266 << 1)
1106     // add -> addi
1107     insn = 14 << 26;
1108   else if ((insn & (0x1f << 1)) == 23 << 1
1109            && ((insn & (0x1f << 6)) < 14 << 6
1110                || ((insn & (0x1f << 6)) >= 16 << 6
1111                    && (insn & (0x1f << 6)) < 24 << 6)))
1112     // load and store indexed -> dform
1113     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1114   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1115     // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1116     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1117   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1118     // lwax -> lwa
1119     insn = (58 << 26) | 2;
1120   else
1121     return 0;
1122   insn |= rtra;
1123   return insn;
1124 }
1125
1126 // Modified version of symtab.h class Symbol member
1127 // Given a direct absolute or pc-relative static relocation against
1128 // the global symbol, this function returns whether a dynamic relocation
1129 // is needed.
1130
1131 template<int size>
1132 bool
1133 needs_dynamic_reloc(const Symbol* gsym, int flags)
1134 {
1135   // No dynamic relocations in a static link!
1136   if (parameters->doing_static_link())
1137     return false;
1138
1139   // A reference to an undefined symbol from an executable should be
1140   // statically resolved to 0, and does not need a dynamic relocation.
1141   // This matches gnu ld behavior.
1142   if (gsym->is_undefined() && !parameters->options().shared())
1143     return false;
1144
1145   // A reference to an absolute symbol does not need a dynamic relocation.
1146   if (gsym->is_absolute())
1147     return false;
1148
1149   // An absolute reference within a position-independent output file
1150   // will need a dynamic relocation.
1151   if ((flags & Symbol::ABSOLUTE_REF)
1152       && parameters->options().output_is_position_independent())
1153     return true;
1154
1155   // A function call that can branch to a local PLT entry does not need
1156   // a dynamic relocation.
1157   if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
1158     return false;
1159
1160   // A reference to any PLT entry in a non-position-independent executable
1161   // does not need a dynamic relocation.
1162   // Except due to having function descriptors on powerpc64 we don't define
1163   // functions to their plt code in an executable, so this doesn't apply.
1164   if (size == 32
1165       && !parameters->options().output_is_position_independent()
1166       && gsym->has_plt_offset())
1167     return false;
1168
1169   // A reference to a symbol defined in a dynamic object or to a
1170   // symbol that is preemptible will need a dynamic relocation.
1171   if (gsym->is_from_dynobj()
1172       || gsym->is_undefined()
1173       || gsym->is_preemptible())
1174     return true;
1175
1176   // For all other cases, return FALSE.
1177   return false;
1178 }
1179
1180 // Modified version of symtab.h class Symbol member
1181 // Whether we should use the PLT offset associated with a symbol for
1182 // a relocation.  FLAGS is a set of Reference_flags.
1183
1184 template<int size>
1185 bool
1186 use_plt_offset(const Symbol* gsym, int flags)
1187 {
1188   // If the symbol doesn't have a PLT offset, then naturally we
1189   // don't want to use it.
1190   if (!gsym->has_plt_offset())
1191     return false;
1192
1193   // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
1194   if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1195     return true;
1196
1197   // If we are going to generate a dynamic relocation, then we will
1198   // wind up using that, so no need to use the PLT entry.
1199   if (needs_dynamic_reloc<size>(gsym, flags))
1200     return false;
1201
1202   // If the symbol is from a dynamic object, we need to use the PLT
1203   // entry.
1204   if (gsym->is_from_dynobj())
1205     return true;
1206
1207   // If we are generating a shared object, and this symbol is
1208   // undefined or preemptible, we need to use the PLT entry.
1209   if (parameters->options().shared()
1210       && (gsym->is_undefined() || gsym->is_preemptible()))
1211     return true;
1212
1213   // If this is a call to a weak undefined symbol, we need to use
1214   // the PLT entry; the symbol may be defined by a library loaded
1215   // at runtime.
1216   if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
1217     return true;
1218
1219   // Otherwise we can use the regular definition.
1220   return false;
1221 }
1222
1223 template<int size, bool big_endian>
1224 class Powerpc_relocate_functions
1225 {
1226 public:
1227   enum Overflow_check
1228   {
1229     CHECK_NONE,
1230     CHECK_SIGNED,
1231     CHECK_BITFIELD
1232   };
1233
1234   enum Status
1235   {
1236     STATUS_OK,
1237     STATUS_OVERFLOW
1238   };
1239
1240 private:
1241   typedef Powerpc_relocate_functions<size, big_endian> This;
1242   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1243
1244   template<int valsize>
1245   static inline bool
1246   has_overflow_signed(Address value)
1247   {
1248     // limit = 1 << (valsize - 1) without shift count exceeding size of type
1249     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1250     limit <<= ((valsize - 1) >> 1);
1251     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1252     return value + limit > (limit << 1) - 1;
1253   }
1254
1255   template<int valsize>
1256   static inline bool
1257   has_overflow_bitfield(Address value)
1258   {
1259     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1260     limit <<= ((valsize - 1) >> 1);
1261     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1262     return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
1263   }
1264
1265   template<int valsize>
1266   static inline Status
1267   overflowed(Address value, Overflow_check overflow)
1268   {
1269     if (overflow == CHECK_SIGNED)
1270       {
1271         if (has_overflow_signed<valsize>(value))
1272           return STATUS_OVERFLOW;
1273       }
1274     else if (overflow == CHECK_BITFIELD)
1275       {
1276         if (has_overflow_bitfield<valsize>(value))
1277           return STATUS_OVERFLOW;
1278       }
1279     return STATUS_OK;
1280   }
1281
1282   // Do a simple RELA relocation
1283   template<int valsize>
1284   static inline Status
1285   rela(unsigned char* view, Address value, Overflow_check overflow)
1286   {
1287     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1288     Valtype* wv = reinterpret_cast<Valtype*>(view);
1289     elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
1290     return overflowed<valsize>(value, overflow);
1291   }
1292
1293   template<int valsize>
1294   static inline Status
1295   rela(unsigned char* view,
1296        unsigned int right_shift,
1297        typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1298        Address value,
1299        Overflow_check overflow)
1300   {
1301     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
1302     Valtype* wv = reinterpret_cast<Valtype*>(view);
1303     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
1304     Valtype reloc = value >> right_shift;
1305     val &= ~dst_mask;
1306     reloc &= dst_mask;
1307     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
1308     return overflowed<valsize>(value >> right_shift, overflow);
1309   }
1310
1311   // Do a simple RELA relocation, unaligned.
1312   template<int valsize>
1313   static inline Status
1314   rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1315   {
1316     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1317     return overflowed<valsize>(value, overflow);
1318   }
1319
1320   template<int valsize>
1321   static inline Status
1322   rela_ua(unsigned char* view,
1323           unsigned int right_shift,
1324           typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1325           Address value,
1326           Overflow_check overflow)
1327   {
1328     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1329       Valtype;
1330     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1331     Valtype reloc = value >> right_shift;
1332     val &= ~dst_mask;
1333     reloc &= dst_mask;
1334     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1335     return overflowed<valsize>(value >> right_shift, overflow);
1336   }
1337
1338 public:
1339   // R_PPC64_ADDR64: (Symbol + Addend)
1340   static inline void
1341   addr64(unsigned char* view, Address value)
1342   { This::template rela<64>(view, value, CHECK_NONE); }
1343
1344   // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1345   static inline void
1346   addr64_u(unsigned char* view, Address value)
1347   { This::template rela_ua<64>(view, value, CHECK_NONE); }
1348
1349   // R_POWERPC_ADDR32: (Symbol + Addend)
1350   static inline Status
1351   addr32(unsigned char* view, Address value, Overflow_check overflow)
1352   { return This::template rela<32>(view, value, overflow); }
1353
1354   // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1355   static inline Status
1356   addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1357   { return This::template rela_ua<32>(view, value, overflow); }
1358
1359   // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1360   static inline Status
1361   addr24(unsigned char* view, Address value, Overflow_check overflow)
1362   {
1363     Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1364     if (overflow != CHECK_NONE && (value & 3) != 0)
1365       stat = STATUS_OVERFLOW;
1366     return stat;
1367   }
1368
1369   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1370   static inline Status
1371   addr16(unsigned char* view, Address value, Overflow_check overflow)
1372   { return This::template rela<16>(view, value, overflow); }
1373
1374   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1375   static inline Status
1376   addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1377   { return This::template rela_ua<16>(view, value, overflow); }
1378
1379   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1380   static inline Status
1381   addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1382   {
1383     Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1384     if (overflow != CHECK_NONE && (value & 3) != 0)
1385       stat = STATUS_OVERFLOW;
1386     return stat;
1387   }
1388
1389   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1390   static inline void
1391   addr16_hi(unsigned char* view, Address value)
1392   { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1393
1394   // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1395   static inline void
1396   addr16_ha(unsigned char* view, Address value)
1397   { This::addr16_hi(view, value + 0x8000); }
1398
1399   // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1400   static inline void
1401   addr16_hi2(unsigned char* view, Address value)
1402   { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1403
1404   // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1405   static inline void
1406   addr16_ha2(unsigned char* view, Address value)
1407   { This::addr16_hi2(view, value + 0x8000); }
1408
1409   // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1410   static inline void
1411   addr16_hi3(unsigned char* view, Address value)
1412   { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1413
1414   // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1415   static inline void
1416   addr16_ha3(unsigned char* view, Address value)
1417   { This::addr16_hi3(view, value + 0x8000); }
1418
1419   // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1420   static inline Status
1421   addr14(unsigned char* view, Address value, Overflow_check overflow)
1422   {
1423     Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1424     if (overflow != CHECK_NONE && (value & 3) != 0)
1425       stat = STATUS_OVERFLOW;
1426     return stat;
1427   }
1428 };
1429
1430 // Stash away the index of .got2 or .opd in a relocatable object, if
1431 // such a section exists.
1432
1433 template<int size, bool big_endian>
1434 bool
1435 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1436     Read_symbols_data* sd)
1437 {
1438   const unsigned char* const pshdrs = sd->section_headers->data();
1439   const unsigned char* namesu = sd->section_names->data();
1440   const char* names = reinterpret_cast<const char*>(namesu);
1441   section_size_type names_size = sd->section_names_size;
1442   const unsigned char* s;
1443
1444   s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1445                       names, names_size, NULL);
1446   if (s != NULL)
1447     {
1448       unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1449       this->special_ = ndx;
1450     }
1451   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1452 }
1453
1454 // Examine .rela.opd to build info about function entry points.
1455
1456 template<int size, bool big_endian>
1457 void
1458 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1459     size_t reloc_count,
1460     const unsigned char* prelocs,
1461     const unsigned char* plocal_syms)
1462 {
1463   if (size == 64)
1464     {
1465       typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1466         Reltype;
1467       const int reloc_size
1468         = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1469       const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1470       Address expected_off = 0;
1471       bool regular = true;
1472       unsigned int opd_ent_size = 0;
1473
1474       for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1475         {
1476           Reltype reloc(prelocs);
1477           typename elfcpp::Elf_types<size>::Elf_WXword r_info
1478             = reloc.get_r_info();
1479           unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1480           if (r_type == elfcpp::R_PPC64_ADDR64)
1481             {
1482               unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1483               typename elfcpp::Elf_types<size>::Elf_Addr value;
1484               bool is_ordinary;
1485               unsigned int shndx;
1486               if (r_sym < this->local_symbol_count())
1487                 {
1488                   typename elfcpp::Sym<size, big_endian>
1489                     lsym(plocal_syms + r_sym * sym_size);
1490                   shndx = lsym.get_st_shndx();
1491                   shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1492                   value = lsym.get_st_value();
1493                 }
1494               else
1495                 shndx = this->symbol_section_and_value(r_sym, &value,
1496                                                        &is_ordinary);
1497               this->set_opd_ent(reloc.get_r_offset(), shndx,
1498                                 value + reloc.get_r_addend());
1499               if (i == 2)
1500                 {
1501                   expected_off = reloc.get_r_offset();
1502                   opd_ent_size = expected_off;
1503                 }
1504               else if (expected_off != reloc.get_r_offset())
1505                 regular = false;
1506               expected_off += opd_ent_size;
1507             }
1508           else if (r_type == elfcpp::R_PPC64_TOC)
1509             {
1510               if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
1511                 regular = false;
1512             }
1513           else
1514             {
1515               gold_warning(_("%s: unexpected reloc type %u in .opd section"),
1516                            this->name().c_str(), r_type);
1517               regular = false;
1518             }
1519         }
1520       if (reloc_count <= 2)
1521         opd_ent_size = this->section_size(this->opd_shndx());
1522       if (opd_ent_size != 24 && opd_ent_size != 16)
1523         regular = false;
1524       if (!regular)
1525         {
1526           gold_warning(_("%s: .opd is not a regular array of opd entries"),
1527                        this->name().c_str());
1528           opd_ent_size = 0;
1529         }
1530     }
1531 }
1532
1533 template<int size, bool big_endian>
1534 void
1535 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1536 {
1537   Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1538   if (size == 64)
1539     {
1540       for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1541            p != rd->relocs.end();
1542            ++p)
1543         {
1544           if (p->data_shndx == this->opd_shndx())
1545             {
1546               uint64_t opd_size = this->section_size(this->opd_shndx());
1547               gold_assert(opd_size == static_cast<size_t>(opd_size));
1548               if (opd_size != 0)
1549                 {
1550                   this->init_opd(opd_size);
1551                   this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1552                                         rd->local_symbols->data());
1553                 }
1554               break;
1555             }
1556         }
1557     }
1558 }
1559
1560 // Set up some symbols.
1561
1562 template<int size, bool big_endian>
1563 void
1564 Target_powerpc<size, big_endian>::do_define_standard_symbols(
1565     Symbol_table* symtab,
1566     Layout* layout)
1567 {
1568   if (size == 32)
1569     {
1570       // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
1571       // undefined when scanning relocs (and thus requires
1572       // non-relative dynamic relocs).  The proper value will be
1573       // updated later.
1574       Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1575       if (gotsym != NULL && gotsym->is_undefined())
1576         {
1577           Target_powerpc<size, big_endian>* target =
1578             static_cast<Target_powerpc<size, big_endian>*>(
1579                 parameters->sized_target<size, big_endian>());
1580           Output_data_got_powerpc<size, big_endian>* got
1581             = target->got_section(symtab, layout);
1582           symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1583                                         Symbol_table::PREDEFINED,
1584                                         got, 0, 0,
1585                                         elfcpp::STT_OBJECT,
1586                                         elfcpp::STB_LOCAL,
1587                                         elfcpp::STV_HIDDEN, 0,
1588                                         false, false);
1589         }
1590
1591       // Define _SDA_BASE_ at the start of the .sdata section + 32768.
1592       Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
1593       if (sdasym != NULL && sdasym->is_undefined())
1594         {
1595           Output_data_space* sdata = new Output_data_space(4, "** sdata");
1596           Output_section* os
1597             = layout->add_output_section_data(".sdata", 0,
1598                                               elfcpp::SHF_ALLOC
1599                                               | elfcpp::SHF_WRITE,
1600                                               sdata, ORDER_SMALL_DATA, false);
1601           symtab->define_in_output_data("_SDA_BASE_", NULL,
1602                                         Symbol_table::PREDEFINED,
1603                                         os, 32768, 0, elfcpp::STT_OBJECT,
1604                                         elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
1605                                         0, false, false);
1606         }
1607     }
1608 }
1609
1610 // Set up PowerPC target specific relobj.
1611
1612 template<int size, bool big_endian>
1613 Object*
1614 Target_powerpc<size, big_endian>::do_make_elf_object(
1615     const std::string& name,
1616     Input_file* input_file,
1617     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1618 {
1619   int et = ehdr.get_e_type();
1620   // ET_EXEC files are valid input for --just-symbols/-R,
1621   // and we treat them as relocatable objects.
1622   if (et == elfcpp::ET_REL
1623       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1624     {
1625       Powerpc_relobj<size, big_endian>* obj =
1626         new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1627       obj->setup();
1628       return obj;
1629     }
1630   else if (et == elfcpp::ET_DYN)
1631     {
1632       Sized_dynobj<size, big_endian>* obj =
1633         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1634       obj->setup();
1635       return obj;
1636     }
1637   else
1638     {
1639       gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1640       return NULL;
1641     }
1642 }
1643
1644 template<int size, bool big_endian>
1645 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1646 {
1647 public:
1648   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1649   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1650
1651   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1652     : Output_data_got<size, big_endian>(),
1653       symtab_(symtab), layout_(layout),
1654       header_ent_cnt_(size == 32 ? 3 : 1),
1655       header_index_(size == 32 ? 0x2000 : 0)
1656   { }
1657
1658   class Got_entry;
1659
1660   // Create a new GOT entry and return its offset.
1661   unsigned int
1662   add_got_entry(Got_entry got_entry)
1663   {
1664     this->reserve_ent();
1665     return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1666   }
1667
1668   // Create a pair of new GOT entries and return the offset of the first.
1669   unsigned int
1670   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1671   {
1672     this->reserve_ent(2);
1673     return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1674                                                                  got_entry_2);
1675   }
1676
1677   unsigned int
1678   add_constant_pair(Valtype c1, Valtype c2)
1679   {
1680     this->reserve_ent(2);
1681     unsigned int got_offset = this->add_constant(c1);
1682     this->add_constant(c2);
1683     return got_offset;
1684   }
1685
1686   // Offset of _GLOBAL_OFFSET_TABLE_.
1687   unsigned int
1688   g_o_t() const
1689   {
1690     return this->got_offset(this->header_index_);
1691   }
1692
1693   // Offset of base used to access the GOT/TOC.
1694   // The got/toc pointer reg will be set to this value.
1695   Valtype
1696   got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1697   {
1698     if (size == 32)
1699       return this->g_o_t();
1700     else
1701       return (this->output_section()->address()
1702               + object->toc_base_offset()
1703               - this->address());
1704   }
1705
1706   // Ensure our GOT has a header.
1707   void
1708   set_final_data_size()
1709   {
1710     if (this->header_ent_cnt_ != 0)
1711       this->make_header();
1712     Output_data_got<size, big_endian>::set_final_data_size();
1713   }
1714
1715   // First word of GOT header needs some values that are not
1716   // handled by Output_data_got so poke them in here.
1717   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1718   void
1719   do_write(Output_file* of)
1720   {
1721     Valtype val = 0;
1722     if (size == 32 && this->layout_->dynamic_data() != NULL)
1723       val = this->layout_->dynamic_section()->address();
1724     if (size == 64)
1725       val = this->output_section()->address() + 0x8000;
1726     this->replace_constant(this->header_index_, val);
1727     Output_data_got<size, big_endian>::do_write(of);
1728   }
1729
1730 private:
1731   void
1732   reserve_ent(unsigned int cnt = 1)
1733   {
1734     if (this->header_ent_cnt_ == 0)
1735       return;
1736     if (this->num_entries() + cnt > this->header_index_)
1737       this->make_header();
1738   }
1739
1740   void
1741   make_header()
1742   {
1743     this->header_ent_cnt_ = 0;
1744     this->header_index_ = this->num_entries();
1745     if (size == 32)
1746       {
1747         Output_data_got<size, big_endian>::add_constant(0);
1748         Output_data_got<size, big_endian>::add_constant(0);
1749         Output_data_got<size, big_endian>::add_constant(0);
1750
1751         // Define _GLOBAL_OFFSET_TABLE_ at the header
1752         Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
1753         if (gotsym != NULL)
1754           {
1755             Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
1756             sym->set_value(this->g_o_t());
1757           }
1758         else
1759           this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1760                                                Symbol_table::PREDEFINED,
1761                                                this, this->g_o_t(), 0,
1762                                                elfcpp::STT_OBJECT,
1763                                                elfcpp::STB_LOCAL,
1764                                                elfcpp::STV_HIDDEN, 0,
1765                                                false, false);
1766       }
1767     else
1768       Output_data_got<size, big_endian>::add_constant(0);
1769   }
1770
1771   // Stashed pointers.
1772   Symbol_table* symtab_;
1773   Layout* layout_;
1774
1775   // GOT header size.
1776   unsigned int header_ent_cnt_;
1777   // GOT header index.
1778   unsigned int header_index_;
1779 };
1780
1781 // Get the GOT section, creating it if necessary.
1782
1783 template<int size, bool big_endian>
1784 Output_data_got_powerpc<size, big_endian>*
1785 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1786                                               Layout* layout)
1787 {
1788   if (this->got_ == NULL)
1789     {
1790       gold_assert(symtab != NULL && layout != NULL);
1791
1792       this->got_
1793         = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1794
1795       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1796                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1797                                       this->got_, ORDER_DATA, false);
1798     }
1799
1800   return this->got_;
1801 }
1802
1803 // Get the dynamic reloc section, creating it if necessary.
1804
1805 template<int size, bool big_endian>
1806 typename Target_powerpc<size, big_endian>::Reloc_section*
1807 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1808 {
1809   if (this->rela_dyn_ == NULL)
1810     {
1811       gold_assert(layout != NULL);
1812       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1813       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1814                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
1815                                       ORDER_DYNAMIC_RELOCS, false);
1816     }
1817   return this->rela_dyn_;
1818 }
1819
1820 class Stub_control
1821 {
1822  public:
1823   // Determine the stub group size.  The group size is the absolute
1824   // value of the parameter --stub-group-size.  If --stub-group-size
1825   // is passed a negative value, we restrict stubs to be always before
1826   // the stubbed branches.
1827   Stub_control(int32_t size)
1828     : state_(NO_GROUP), stub_group_size_(abs(size)),
1829       stub14_group_size_(abs(size)),
1830       stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
1831       group_end_addr_(0), owner_(NULL), output_section_(NULL)
1832   {
1833     if (stub_group_size_ == 1)
1834       {
1835         // Default values.
1836         if (stubs_always_before_branch_)
1837           {
1838             stub_group_size_ = 0x1e00000;
1839             stub14_group_size_ = 0x7800;
1840           }
1841         else
1842           {
1843             stub_group_size_ = 0x1c00000;
1844             stub14_group_size_ = 0x7000;
1845           }
1846         suppress_size_errors_ = true;
1847       }
1848   }
1849
1850   // Return true iff input section can be handled by current stub
1851   // group.
1852   bool
1853   can_add_to_stub_group(Output_section* o,
1854                         const Output_section::Input_section* i,
1855                         bool has14);
1856
1857   const Output_section::Input_section*
1858   owner()
1859   { return owner_; }
1860
1861   Output_section*
1862   output_section()
1863   { return output_section_; }
1864
1865  private:
1866   typedef enum
1867   {
1868     NO_GROUP,
1869     FINDING_STUB_SECTION,
1870     HAS_STUB_SECTION
1871   } State;
1872
1873   State state_;
1874   uint32_t stub_group_size_;
1875   uint32_t stub14_group_size_;
1876   bool stubs_always_before_branch_;
1877   bool suppress_size_errors_;
1878   uint64_t group_end_addr_;
1879   const Output_section::Input_section* owner_;
1880   Output_section* output_section_;
1881 };
1882
1883 // Return true iff input section can be handled by current stub/
1884 // group.
1885
1886 bool
1887 Stub_control::can_add_to_stub_group(Output_section* o,
1888                                     const Output_section::Input_section* i,
1889                                     bool has14)
1890 {
1891   uint32_t group_size
1892     = has14 ? this->stub14_group_size_ : this->stub_group_size_;
1893   bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
1894   uint64_t this_size;
1895   uint64_t start_addr = o->address();
1896
1897   if (whole_sec)
1898     // .init and .fini sections are pasted together to form a single
1899     // function.  We can't be adding stubs in the middle of the function.
1900     this_size = o->data_size();
1901   else
1902     {
1903       start_addr += i->relobj()->output_section_offset(i->shndx());
1904       this_size = i->data_size();
1905     }
1906   uint64_t end_addr = start_addr + this_size;
1907   bool toobig = this_size > group_size;
1908
1909   if (toobig && !this->suppress_size_errors_)
1910     gold_warning(_("%s:%s exceeds group size"),
1911                  i->relobj()->name().c_str(),
1912                  i->relobj()->section_name(i->shndx()).c_str());
1913
1914   if (this->state_ != HAS_STUB_SECTION
1915       && (!whole_sec || this->output_section_ != o))
1916     {
1917       this->owner_ = i;
1918       this->output_section_ = o;
1919     }
1920
1921   if (this->state_ == NO_GROUP)
1922     {
1923       this->state_ = FINDING_STUB_SECTION;
1924       this->group_end_addr_ = end_addr;
1925     }
1926   else if (this->group_end_addr_ - start_addr < group_size)
1927     ;
1928   // Adding this section would make the group larger than GROUP_SIZE.
1929   else if (this->state_ == FINDING_STUB_SECTION
1930            && !this->stubs_always_before_branch_
1931            && !toobig)
1932     {
1933       // But wait, there's more!  Input sections up to GROUP_SIZE
1934       // bytes before the stub table can be handled by it too.
1935       this->state_ = HAS_STUB_SECTION;
1936       this->group_end_addr_ = end_addr;
1937     }
1938   else
1939     {
1940       this->state_ = NO_GROUP;
1941       return false;
1942     }
1943   return true;
1944 }
1945
1946 // Look over all the input sections, deciding where to place stubs.
1947
1948 template<int size, bool big_endian>
1949 void
1950 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
1951                                                  const Task*)
1952 {
1953   Stub_control stub_control(parameters->options().stub_group_size());
1954
1955   // Group input sections and insert stub table
1956   Stub_table<size, big_endian>* stub_table = NULL;
1957   Layout::Section_list section_list;
1958   layout->get_executable_sections(&section_list);
1959   std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
1960   for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
1961        o != section_list.rend();
1962        ++o)
1963     {
1964       typedef Output_section::Input_section_list Input_section_list;
1965       for (Input_section_list::const_reverse_iterator i
1966              = (*o)->input_sections().rbegin();
1967            i != (*o)->input_sections().rend();
1968            ++i)
1969         {
1970           if (i->is_input_section())
1971             {
1972               Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1973                 <Powerpc_relobj<size, big_endian>*>(i->relobj());
1974               bool has14 = ppcobj->has_14bit_branch(i->shndx());
1975               if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
1976                 {
1977                   stub_table->init(stub_control.owner(),
1978                                    stub_control.output_section());
1979                   stub_table = NULL;
1980                 }
1981               if (stub_table == NULL)
1982                 stub_table = this->new_stub_table();
1983               ppcobj->set_stub_table(i->shndx(), stub_table);
1984             }
1985         }
1986     }
1987   if (stub_table != NULL)
1988     stub_table->init(stub_control.owner(), stub_control.output_section());
1989 }
1990
1991 // If this branch needs a plt call stub, or a long branch stub, make one.
1992
1993 template<int size, bool big_endian>
1994 void
1995 Target_powerpc<size, big_endian>::Branch_info::make_stub(
1996     Stub_table<size, big_endian>* stub_table,
1997     Stub_table<size, big_endian>* ifunc_stub_table,
1998     Symbol_table* symtab) const
1999 {
2000   Symbol* sym = this->object_->global_symbol(this->r_sym_);
2001   if (sym != NULL && sym->is_forwarder())
2002     sym = symtab->resolve_forwards(sym);
2003   const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2004   if (gsym != NULL
2005       ? use_plt_offset<size>(gsym, Scan::get_reference_flags(this->r_type_))
2006       : this->object_->local_has_plt_offset(this->r_sym_))
2007     {
2008       if (stub_table == NULL)
2009         stub_table = this->object_->stub_table(this->shndx_);
2010       if (stub_table == NULL)
2011         {
2012           // This is a ref from a data section to an ifunc symbol.
2013           stub_table = ifunc_stub_table;
2014         }
2015       gold_assert(stub_table != NULL);
2016       if (gsym != NULL)
2017         stub_table->add_plt_call_entry(this->object_, gsym,
2018                                        this->r_type_, this->addend_);
2019       else
2020         stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2021                                        this->r_type_, this->addend_);
2022     }
2023   else
2024     {
2025       unsigned int max_branch_offset;
2026       if (this->r_type_ == elfcpp::R_POWERPC_REL14
2027           || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2028           || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2029         max_branch_offset = 1 << 15;
2030       else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2031                || this->r_type_ == elfcpp::R_PPC_PLTREL24
2032                || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2033         max_branch_offset = 1 << 25;
2034       else
2035         return;
2036       Address from = this->object_->get_output_section_offset(this->shndx_);
2037       gold_assert(from != invalid_address);
2038       from += (this->object_->output_section(this->shndx_)->address()
2039                + this->offset_);
2040       Address to;
2041       if (gsym != NULL)
2042         {
2043           switch (gsym->source())
2044             {
2045             case Symbol::FROM_OBJECT:
2046               {
2047                 Object* symobj = gsym->object();
2048                 if (symobj->is_dynamic()
2049                     || symobj->pluginobj() != NULL)
2050                   return;
2051                 bool is_ordinary;
2052                 unsigned int shndx = gsym->shndx(&is_ordinary);
2053                 if (shndx == elfcpp::SHN_UNDEF)
2054                   return;
2055               }
2056               break;
2057
2058             case Symbol::IS_UNDEFINED:
2059               return;
2060
2061             default:
2062               break;
2063             }
2064           Symbol_table::Compute_final_value_status status;
2065           to = symtab->compute_final_value<size>(gsym, &status);
2066           if (status != Symbol_table::CFVS_OK)
2067             return;
2068         }
2069       else
2070         {
2071           const Symbol_value<size>* psymval
2072             = this->object_->local_symbol(this->r_sym_);
2073           Symbol_value<size> symval;
2074           typedef Sized_relobj_file<size, big_endian> ObjType;
2075           typename ObjType::Compute_final_local_value_status status
2076             = this->object_->compute_final_local_value(this->r_sym_, psymval,
2077                                                        &symval, symtab);
2078           if (status != ObjType::CFLV_OK
2079               || !symval.has_output_value())
2080             return;
2081           to = symval.value(this->object_, 0);
2082         }
2083       to += this->addend_;
2084       if (stub_table == NULL)
2085         stub_table = this->object_->stub_table(this->shndx_);
2086       gold_assert(stub_table != NULL);
2087       if (size == 64 && is_branch_reloc(this->r_type_))
2088         {
2089           unsigned int dest_shndx;
2090           to = stub_table->targ()->symval_for_branch(to, gsym, this->object_,
2091                                                      &dest_shndx);
2092         }
2093       Address delta = to - from;
2094       if (delta + max_branch_offset >= 2 * max_branch_offset)
2095         {
2096           stub_table->add_long_branch_entry(this->object_, to);
2097         }
2098     }
2099 }
2100
2101 // Relaxation hook.  This is where we do stub generation.
2102
2103 template<int size, bool big_endian>
2104 bool
2105 Target_powerpc<size, big_endian>::do_relax(int pass,
2106                                            const Input_objects*,
2107                                            Symbol_table* symtab,
2108                                            Layout* layout,
2109                                            const Task* task)
2110 {
2111   unsigned int prev_brlt_size = 0;
2112   if (pass == 1)
2113     {
2114       bool thread_safe = parameters->options().plt_thread_safe();
2115       if (size == 64 && !parameters->options().user_set_plt_thread_safe())
2116         {
2117           const char* const thread_starter[] =
2118             {
2119               "pthread_create",
2120               /* libstdc++ */
2121               "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2122               /* librt */
2123               "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2124               "mq_notify", "create_timer",
2125               /* libanl */
2126               "getaddrinfo_a",
2127               /* libgomp */
2128               "GOMP_parallel_start",
2129               "GOMP_parallel_loop_static_start",
2130               "GOMP_parallel_loop_dynamic_start",
2131               "GOMP_parallel_loop_guided_start",
2132               "GOMP_parallel_loop_runtime_start",
2133               "GOMP_parallel_sections_start", 
2134             };
2135
2136           for (unsigned int i = 0;
2137                i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2138                i++)
2139             {
2140               Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2141               thread_safe = sym != NULL && sym->in_reg() && sym->in_real_elf();
2142               if (thread_safe)
2143                 break;
2144             }
2145         }
2146       this->plt_thread_safe_ = thread_safe;
2147       this->group_sections(layout, task);
2148     }
2149
2150   // We need address of stub tables valid for make_stub.
2151   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2152        p != this->stub_tables_.end();
2153        ++p)
2154     {
2155       const Powerpc_relobj<size, big_endian>* object
2156         = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2157       Address off = object->get_output_section_offset((*p)->shndx());
2158       gold_assert(off != invalid_address);
2159       Output_section* os = (*p)->output_section();
2160       (*p)->set_address_and_size(os, off);
2161     }
2162
2163   if (pass != 1)
2164     {
2165       // Clear plt call stubs, long branch stubs and branch lookup table.
2166       prev_brlt_size = this->branch_lookup_table_.size();
2167       this->branch_lookup_table_.clear();
2168       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2169            p != this->stub_tables_.end();
2170            ++p)
2171         {
2172           (*p)->clear_stubs();
2173         }
2174     }
2175
2176   // Build all the stubs.
2177   Stub_table<size, big_endian>* ifunc_stub_table
2178     = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2179   Stub_table<size, big_endian>* one_stub_table
2180     = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2181   for (typename Branches::const_iterator b = this->branch_info_.begin();
2182        b != this->branch_info_.end();
2183        b++)
2184     {
2185       b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2186     }
2187
2188   // Did anything change size?
2189   unsigned int num_huge_branches = this->branch_lookup_table_.size();
2190   bool again = num_huge_branches != prev_brlt_size;
2191   if (size == 64 && num_huge_branches != 0)
2192     this->make_brlt_section(layout);
2193   if (size == 64 && again)
2194     this->brlt_section_->set_current_size(num_huge_branches);
2195
2196   typedef Unordered_set<Output_section*> Output_sections;
2197   Output_sections os_need_update;
2198   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2199        p != this->stub_tables_.end();
2200        ++p)
2201     {
2202       if ((*p)->size_update())
2203         {
2204           again = true;
2205           os_need_update.insert((*p)->output_section());
2206         }
2207     }
2208
2209   // Set output section offsets for all input sections in an output
2210   // section that just changed size.  Anything past the stubs will
2211   // need updating.
2212   for (typename Output_sections::iterator p = os_need_update.begin();
2213        p != os_need_update.end();
2214        p++)
2215     {
2216       Output_section* os = *p;
2217       Address off = 0;
2218       typedef Output_section::Input_section_list Input_section_list;
2219       for (Input_section_list::const_iterator i = os->input_sections().begin();
2220            i != os->input_sections().end();
2221            ++i)
2222         {
2223           off = align_address(off, i->addralign());
2224           if (i->is_input_section() || i->is_relaxed_input_section())
2225             i->relobj()->set_section_offset(i->shndx(), off);
2226           if (i->is_relaxed_input_section())
2227             {
2228               Stub_table<size, big_endian>* stub_table
2229                 = static_cast<Stub_table<size, big_endian>*>(
2230                     i->relaxed_input_section());
2231               off += stub_table->set_address_and_size(os, off);
2232             }
2233           else
2234             off += i->data_size();
2235         }
2236       // If .brlt is part of this output section, then we have just
2237       // done the offset adjustment.
2238       os->clear_section_offsets_need_adjustment();
2239     }
2240
2241   if (size == 64
2242       && !again
2243       && num_huge_branches != 0
2244       && parameters->options().output_is_position_independent())
2245     {
2246       // Fill in the BRLT relocs.
2247       this->brlt_section_->reset_data_size();
2248       for (typename Branch_lookup_table::const_iterator p
2249              = this->branch_lookup_table_.begin();
2250            p != this->branch_lookup_table_.end();
2251            ++p)
2252         {
2253           this->brlt_section_->add_reloc(p->first, p->second);
2254         }
2255       this->brlt_section_->finalize_data_size();
2256     }
2257   return again;
2258 }
2259
2260 // A class to handle the PLT data.
2261
2262 template<int size, bool big_endian>
2263 class Output_data_plt_powerpc : public Output_section_data_build
2264 {
2265  public:
2266   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2267                             size, big_endian> Reloc_section;
2268
2269   Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2270                           Reloc_section* plt_rel,
2271                           unsigned int reserved_size,
2272                           const char* name)
2273     : Output_section_data_build(size == 32 ? 4 : 8),
2274       rel_(plt_rel),
2275       targ_(targ),
2276       initial_plt_entry_size_(reserved_size),
2277       name_(name)
2278   { }
2279
2280   // Add an entry to the PLT.
2281   void
2282   add_entry(Symbol*);
2283
2284   void
2285   add_ifunc_entry(Symbol*);
2286
2287   void
2288   add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2289
2290   // Return the .rela.plt section data.
2291   Reloc_section*
2292   rel_plt() const
2293   {
2294     return this->rel_;
2295   }
2296
2297   // Return the number of PLT entries.
2298   unsigned int
2299   entry_count() const
2300   {
2301     return ((this->current_data_size() - this->initial_plt_entry_size_)
2302             / plt_entry_size);
2303   }
2304
2305   // Return the offset of the first non-reserved PLT entry.
2306   unsigned int
2307   first_plt_entry_offset()
2308   { return this->initial_plt_entry_size_; }
2309
2310   // Return the size of a PLT entry.
2311   static unsigned int
2312   get_plt_entry_size()
2313   { return plt_entry_size; }
2314
2315  protected:
2316   void
2317   do_adjust_output_section(Output_section* os)
2318   {
2319     os->set_entsize(0);
2320   }
2321
2322   // Write to a map file.
2323   void
2324   do_print_to_mapfile(Mapfile* mapfile) const
2325   { mapfile->print_output_data(this, this->name_); }
2326
2327  private:
2328   // The size of an entry in the PLT.
2329   static const int plt_entry_size = size == 32 ? 4 : 24;
2330
2331   // Write out the PLT data.
2332   void
2333   do_write(Output_file*);
2334
2335   // The reloc section.
2336   Reloc_section* rel_;
2337   // Allows access to .glink for do_write.
2338   Target_powerpc<size, big_endian>* targ_;
2339   // The size of the first reserved entry.
2340   int initial_plt_entry_size_;
2341   // What to report in map file.
2342   const char *name_;
2343 };
2344
2345 // Add an entry to the PLT.
2346
2347 template<int size, bool big_endian>
2348 void
2349 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
2350 {
2351   if (!gsym->has_plt_offset())
2352     {
2353       section_size_type off = this->current_data_size();
2354       if (off == 0)
2355         off += this->first_plt_entry_offset();
2356       gsym->set_plt_offset(off);
2357       gsym->set_needs_dynsym_entry();
2358       unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
2359       this->rel_->add_global(gsym, dynrel, this, off, 0);
2360       off += plt_entry_size;
2361       this->set_current_data_size(off);
2362     }
2363 }
2364
2365 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2366
2367 template<int size, bool big_endian>
2368 void
2369 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
2370 {
2371   if (!gsym->has_plt_offset())
2372     {
2373       section_size_type off = this->current_data_size();
2374       gsym->set_plt_offset(off);
2375       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2376       if (size == 64)
2377         dynrel = elfcpp::R_PPC64_JMP_IREL;
2378       this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
2379       off += plt_entry_size;
2380       this->set_current_data_size(off);
2381     }
2382 }
2383
2384 // Add an entry for a local ifunc symbol to the IPLT.
2385
2386 template<int size, bool big_endian>
2387 void
2388 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
2389     Sized_relobj_file<size, big_endian>* relobj,
2390     unsigned int local_sym_index)
2391 {
2392   if (!relobj->local_has_plt_offset(local_sym_index))
2393     {
2394       section_size_type off = this->current_data_size();
2395       relobj->set_local_plt_offset(local_sym_index, off);
2396       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2397       if (size == 64)
2398         dynrel = elfcpp::R_PPC64_JMP_IREL;
2399       this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
2400                                               this, off, 0);
2401       off += plt_entry_size;
2402       this->set_current_data_size(off);
2403     }
2404 }
2405
2406 static const uint32_t add_0_11_11       = 0x7c0b5a14;
2407 static const uint32_t add_2_2_11        = 0x7c425a14;
2408 static const uint32_t add_3_3_2         = 0x7c631214;
2409 static const uint32_t add_3_3_13        = 0x7c636a14;
2410 static const uint32_t add_11_0_11       = 0x7d605a14;
2411 static const uint32_t add_12_2_11       = 0x7d825a14;
2412 static const uint32_t add_12_12_11      = 0x7d8c5a14;
2413 static const uint32_t addi_11_11        = 0x396b0000;
2414 static const uint32_t addi_12_12        = 0x398c0000;
2415 static const uint32_t addi_2_2          = 0x38420000;
2416 static const uint32_t addi_3_2          = 0x38620000;
2417 static const uint32_t addi_3_3          = 0x38630000;
2418 static const uint32_t addis_0_2         = 0x3c020000;
2419 static const uint32_t addis_0_13        = 0x3c0d0000;
2420 static const uint32_t addis_11_11       = 0x3d6b0000;
2421 static const uint32_t addis_11_30       = 0x3d7e0000;
2422 static const uint32_t addis_12_12       = 0x3d8c0000;
2423 static const uint32_t addis_12_2        = 0x3d820000;
2424 static const uint32_t addis_3_2         = 0x3c620000;
2425 static const uint32_t addis_3_13        = 0x3c6d0000;
2426 static const uint32_t b                 = 0x48000000;
2427 static const uint32_t bcl_20_31         = 0x429f0005;
2428 static const uint32_t bctr              = 0x4e800420;
2429 static const uint32_t blr               = 0x4e800020;
2430 static const uint32_t blrl              = 0x4e800021;
2431 static const uint32_t bnectr_p4         = 0x4ce20420;
2432 static const uint32_t cmpldi_2_0        = 0x28220000;
2433 static const uint32_t cror_15_15_15     = 0x4def7b82;
2434 static const uint32_t cror_31_31_31     = 0x4ffffb82;
2435 static const uint32_t ld_0_1            = 0xe8010000;
2436 static const uint32_t ld_0_12           = 0xe80c0000;
2437 static const uint32_t ld_11_12          = 0xe96c0000;
2438 static const uint32_t ld_11_2           = 0xe9620000;
2439 static const uint32_t ld_2_1            = 0xe8410000;
2440 static const uint32_t ld_2_11           = 0xe84b0000;
2441 static const uint32_t ld_2_12           = 0xe84c0000;
2442 static const uint32_t ld_2_2            = 0xe8420000;
2443 static const uint32_t lfd_0_1           = 0xc8010000;
2444 static const uint32_t li_0_0            = 0x38000000;
2445 static const uint32_t li_12_0           = 0x39800000;
2446 static const uint32_t lis_0_0           = 0x3c000000;
2447 static const uint32_t lis_11            = 0x3d600000;
2448 static const uint32_t lis_12            = 0x3d800000;
2449 static const uint32_t lwz_0_12          = 0x800c0000;
2450 static const uint32_t lwz_11_11         = 0x816b0000;
2451 static const uint32_t lwz_11_30         = 0x817e0000;
2452 static const uint32_t lwz_12_12         = 0x818c0000;
2453 static const uint32_t lwzu_0_12         = 0x840c0000;
2454 static const uint32_t lvx_0_12_0        = 0x7c0c00ce;
2455 static const uint32_t mflr_0            = 0x7c0802a6;
2456 static const uint32_t mflr_11           = 0x7d6802a6;
2457 static const uint32_t mflr_12           = 0x7d8802a6;
2458 static const uint32_t mtctr_0           = 0x7c0903a6;
2459 static const uint32_t mtctr_11          = 0x7d6903a6;
2460 static const uint32_t mtctr_12          = 0x7d8903a6;
2461 static const uint32_t mtlr_0            = 0x7c0803a6;
2462 static const uint32_t mtlr_12           = 0x7d8803a6;
2463 static const uint32_t nop               = 0x60000000;
2464 static const uint32_t ori_0_0_0         = 0x60000000;
2465 static const uint32_t std_0_1           = 0xf8010000;
2466 static const uint32_t std_0_12          = 0xf80c0000;
2467 static const uint32_t std_2_1           = 0xf8410000;
2468 static const uint32_t stfd_0_1          = 0xd8010000;
2469 static const uint32_t stvx_0_12_0       = 0x7c0c01ce;
2470 static const uint32_t sub_11_11_12      = 0x7d6c5850;
2471 static const uint32_t xor_11_11_11      = 0x7d6b5a78;
2472
2473 // Write out the PLT.
2474
2475 template<int size, bool big_endian>
2476 void
2477 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
2478 {
2479   if (size == 32)
2480     {
2481       const section_size_type offset = this->offset();
2482       const section_size_type oview_size
2483         = convert_to_section_size_type(this->data_size());
2484       unsigned char* const oview = of->get_output_view(offset, oview_size);
2485       unsigned char* pov = oview;
2486       unsigned char* endpov = oview + oview_size;
2487
2488       // The address of the .glink branch table
2489       const Output_data_glink<size, big_endian>* glink
2490         = this->targ_->glink_section();
2491       elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
2492
2493       while (pov < endpov)
2494         {
2495           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
2496           pov += 4;
2497           branch_tab += 4;
2498         }
2499
2500       of->write_output_view(offset, oview_size, oview);
2501     }
2502 }
2503
2504 // Create the PLT section.
2505
2506 template<int size, bool big_endian>
2507 void
2508 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
2509                                                    Layout* layout)
2510 {
2511   if (this->plt_ == NULL)
2512     {
2513       if (this->got_ == NULL)
2514         this->got_section(symtab, layout);
2515
2516       if (this->glink_ == NULL)
2517         make_glink_section(layout);
2518
2519       // Ensure that .rela.dyn always appears before .rela.plt  This is
2520       // necessary due to how, on PowerPC and some other targets, .rela.dyn
2521       // needs to include .rela.plt in it's range.
2522       this->rela_dyn_section(layout);
2523
2524       Reloc_section* plt_rel = new Reloc_section(false);
2525       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
2526                                       elfcpp::SHF_ALLOC, plt_rel,
2527                                       ORDER_DYNAMIC_PLT_RELOCS, false);
2528       this->plt_
2529         = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
2530                                                         size == 32 ? 0 : 24,
2531                                                         "** PLT");
2532       layout->add_output_section_data(".plt",
2533                                       (size == 32
2534                                        ? elfcpp::SHT_PROGBITS
2535                                        : elfcpp::SHT_NOBITS),
2536                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2537                                       this->plt_,
2538                                       (size == 32
2539                                        ? ORDER_SMALL_DATA
2540                                        : ORDER_SMALL_BSS),
2541                                       false);
2542     }
2543 }
2544
2545 // Create the IPLT section.
2546
2547 template<int size, bool big_endian>
2548 void
2549 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
2550                                                     Layout* layout)
2551 {
2552   if (this->iplt_ == NULL)
2553     {
2554       this->make_plt_section(symtab, layout);
2555
2556       Reloc_section* iplt_rel = new Reloc_section(false);
2557       this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
2558       this->iplt_
2559         = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
2560                                                         0, "** IPLT");
2561       this->plt_->output_section()->add_output_section_data(this->iplt_);
2562     }
2563 }
2564
2565 // A section for huge long branch addresses, similar to plt section.
2566
2567 template<int size, bool big_endian>
2568 class Output_data_brlt_powerpc : public Output_section_data_build
2569 {
2570  public:
2571   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2572   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2573                             size, big_endian> Reloc_section;
2574
2575   Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
2576                            Reloc_section* brlt_rel)
2577     : Output_section_data_build(size == 32 ? 4 : 8),
2578       rel_(brlt_rel),
2579       targ_(targ)
2580   { }
2581
2582   // Add a reloc for an entry in the BRLT.
2583   void
2584   add_reloc(Address to, unsigned int off)
2585   { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
2586
2587   // Update section and reloc section size.
2588   void
2589   set_current_size(unsigned int num_branches)
2590   {
2591     this->reset_address_and_file_offset();
2592     this->set_current_data_size(num_branches * 16);
2593     this->finalize_data_size();
2594     Output_section* os = this->output_section();
2595     os->set_section_offsets_need_adjustment();
2596     if (this->rel_ != NULL)
2597       {
2598         unsigned int reloc_size
2599           = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2600         this->rel_->reset_address_and_file_offset();
2601         this->rel_->set_current_data_size(num_branches * reloc_size);
2602         this->rel_->finalize_data_size();
2603         Output_section* os = this->rel_->output_section();
2604         os->set_section_offsets_need_adjustment();
2605       }
2606   }
2607
2608  protected:
2609   void
2610   do_adjust_output_section(Output_section* os)
2611   {
2612     os->set_entsize(0);
2613   }
2614
2615   // Write to a map file.
2616   void
2617   do_print_to_mapfile(Mapfile* mapfile) const
2618   { mapfile->print_output_data(this, "** BRLT"); }
2619
2620  private:
2621   // Write out the BRLT data.
2622   void
2623   do_write(Output_file*);
2624
2625   // The reloc section.
2626   Reloc_section* rel_;
2627   Target_powerpc<size, big_endian>* targ_;
2628 };
2629
2630 // Make the branch lookup table section.
2631
2632 template<int size, bool big_endian>
2633 void
2634 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
2635 {
2636   if (size == 64 && this->brlt_section_ == NULL)
2637     {
2638       Reloc_section* brlt_rel = NULL;
2639       bool is_pic = parameters->options().output_is_position_independent();
2640       if (is_pic)
2641         {
2642           // When PIC we can't fill in .brlt (like .plt it can be a
2643           // bss style section) but must initialise at runtime via
2644           // dynamic relocats.
2645           this->rela_dyn_section(layout);
2646           brlt_rel = new Reloc_section(false);
2647           this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
2648         }
2649       this->brlt_section_
2650         = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
2651       if (this->plt_ && is_pic)
2652         this->plt_->output_section()
2653           ->add_output_section_data(this->brlt_section_);
2654       else
2655         layout->add_output_section_data(".brlt",
2656                                         (is_pic ? elfcpp::SHT_NOBITS
2657                                          : elfcpp::SHT_PROGBITS),
2658                                         elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2659                                         this->brlt_section_,
2660                                         (is_pic ? ORDER_SMALL_BSS
2661                                          : ORDER_SMALL_DATA),
2662                                         false);
2663     }
2664 }
2665
2666 // Write out .brlt when non-PIC.
2667
2668 template<int size, bool big_endian>
2669 void
2670 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
2671 {
2672   if (size == 64 && !parameters->options().output_is_position_independent())
2673     {
2674       const section_size_type offset = this->offset();
2675       const section_size_type oview_size
2676         = convert_to_section_size_type(this->data_size());
2677       unsigned char* const oview = of->get_output_view(offset, oview_size);
2678
2679       this->targ_->write_branch_lookup_table(oview);
2680       of->write_output_view(offset, oview_size, oview);
2681     }
2682 }
2683
2684 static inline uint32_t
2685 l(uint32_t a)
2686 {
2687   return a & 0xffff;
2688 }
2689
2690 static inline uint32_t
2691 hi(uint32_t a)
2692 {
2693   return l(a >> 16);
2694 }
2695
2696 static inline uint32_t
2697 ha(uint32_t a)
2698 {
2699   return hi(a + 0x8000);
2700 }
2701
2702 template<bool big_endian>
2703 static inline void
2704 write_insn(unsigned char* p, uint32_t v)
2705 {
2706   elfcpp::Swap<32, big_endian>::writeval(p, v);
2707 }
2708
2709 // Stub_table holds information about plt and long branch stubs.
2710 // Stubs are built in an area following some input section determined
2711 // by group_sections().  This input section is converted to a relaxed
2712 // input section allowing it to be resized to accommodate the stubs
2713
2714 template<int size, bool big_endian>
2715 class Stub_table : public Output_relaxed_input_section
2716 {
2717  public:
2718   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2719   static const Address invalid_address = static_cast<Address>(0) - 1;
2720
2721   Stub_table(Target_powerpc<size, big_endian>* targ)
2722     : Output_relaxed_input_section(NULL, 0, 0),
2723       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
2724       orig_data_size_(0), plt_size_(0), last_plt_size_(0),
2725       branch_size_(0), last_branch_size_(0)
2726   { }
2727
2728   // Delayed Output_relaxed_input_section init.
2729   void
2730   init(const Output_section::Input_section*, Output_section*);
2731
2732   // Add a plt call stub.
2733   void
2734   add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2735                      const Symbol*,
2736                      unsigned int,
2737                      Address);
2738
2739   void
2740   add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2741                      unsigned int,
2742                      unsigned int,
2743                      Address);
2744
2745   // Find a given plt call stub.
2746   Address
2747   find_plt_call_entry(const Symbol*) const;
2748
2749   Address
2750   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2751                       unsigned int) const;
2752
2753   Address
2754   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2755                       const Symbol*,
2756                       unsigned int,
2757                       Address) const;
2758
2759   Address
2760   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
2761                       unsigned int,
2762                       unsigned int,
2763                       Address) const;
2764
2765   // Add a long branch stub.
2766   void
2767   add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
2768
2769   Address
2770   find_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
2771
2772   void
2773   clear_stubs()
2774   {
2775     this->plt_call_stubs_.clear();
2776     this->plt_size_ = 0;
2777     this->long_branch_stubs_.clear();
2778     this->branch_size_ = 0;
2779   }
2780
2781   Address
2782   set_address_and_size(const Output_section* os, Address off)
2783   {
2784     Address start_off = off;
2785     off += this->orig_data_size_;
2786     Address my_size = this->plt_size_ + this->branch_size_;
2787     if (my_size != 0)
2788       off = align_address(off, this->stub_align());
2789     // Include original section size and alignment padding in size
2790     my_size += off - start_off;
2791     this->reset_address_and_file_offset();
2792     this->set_current_data_size(my_size);
2793     this->set_address_and_file_offset(os->address() + start_off,
2794                                       os->offset() + start_off);
2795     return my_size;
2796   }
2797
2798   Address
2799   stub_address()
2800   {
2801     return align_address(this->address() + this->orig_data_size_,
2802                          this->stub_align());
2803   }
2804
2805   Address
2806   stub_offset()
2807   {
2808     return align_address(this->offset() + this->orig_data_size_,
2809                          this->stub_align());
2810   }
2811
2812   section_size_type
2813   plt_size() const
2814   { return this->plt_size_; }
2815
2816   bool
2817   size_update()
2818   {
2819     Output_section* os = this->output_section();
2820     if (os->addralign() < this->stub_align())
2821       {
2822         os->set_addralign(this->stub_align());
2823         // FIXME: get rid of the insane checkpointing.
2824         // We can't increase alignment of the input section to which
2825         // stubs are attached;  The input section may be .init which
2826         // is pasted together with other .init sections to form a
2827         // function.  Aligning might insert zero padding resulting in
2828         // sigill.  However we do need to increase alignment of the
2829         // output section so that the align_address() on offset in
2830         // set_address_and_size() adds the same padding as the
2831         // align_address() on address in stub_address().
2832         // What's more, we need this alignment for the layout done in
2833         // relaxation_loop_body() so that the output section starts at
2834         // a suitably aligned address.
2835         os->checkpoint_set_addralign(this->stub_align());
2836       }
2837     if (this->last_plt_size_ != this->plt_size_
2838         || this->last_branch_size_ != this->branch_size_)
2839       {
2840         this->last_plt_size_ = this->plt_size_;
2841         this->last_branch_size_ = this->branch_size_;
2842         return true;
2843       }
2844     return false;
2845   }
2846
2847   Target_powerpc<size, big_endian>*
2848   targ() const
2849   { return targ_; }
2850
2851  private:
2852   class Plt_stub_ent;
2853   class Plt_stub_ent_hash;
2854   typedef Unordered_map<Plt_stub_ent, unsigned int,
2855                         Plt_stub_ent_hash> Plt_stub_entries;
2856
2857   // Alignment of stub section.
2858   unsigned int
2859   stub_align() const
2860   {
2861     if (size == 32)
2862       return 16;
2863     unsigned int min_align = 32;
2864     unsigned int user_align = 1 << parameters->options().plt_align();
2865     return std::max(user_align, min_align);
2866   }
2867
2868   // Size of a given plt call stub.
2869   unsigned int
2870   plt_call_size(typename Plt_stub_entries::const_iterator p) const
2871   {
2872     if (size == 32)
2873       return 16;
2874
2875     Address pltaddr = p->second;
2876     if (p->first.sym_ == NULL 
2877         || (p->first.sym_->type() == elfcpp::STT_GNU_IFUNC
2878             && p->first.sym_->can_use_relative_reloc(false)))
2879       pltaddr += this->targ_->iplt_section()->address();
2880     else
2881       pltaddr += this->targ_->plt_section()->address();
2882     Address tocbase = this->targ_->got_section()->output_section()->address();
2883     const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2884       <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
2885     tocbase += ppcobj->toc_base_offset();
2886     Address off = pltaddr - tocbase;
2887     bool static_chain = parameters->options().plt_static_chain();
2888     bool thread_safe = this->targ_->plt_thread_safe();
2889     unsigned int bytes = (4 * 5
2890                           + 4 * static_chain
2891                           + 8 * thread_safe
2892                           + 4 * (ha(off) != 0)
2893                           + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
2894     unsigned int align = 1 << parameters->options().plt_align();
2895     if (align > 1)
2896       bytes = (bytes + align - 1) & -align;
2897     return bytes;
2898   }
2899
2900   // Return long branch stub size.
2901   unsigned int
2902   branch_stub_size(Address to)
2903   {
2904     Address loc
2905       = this->stub_address() + this->last_plt_size_ + this->branch_size_;
2906     if (to - loc + (1 << 25) < 2 << 25)
2907       return 4;
2908     if (size == 64 || !parameters->options().output_is_position_independent())
2909       return 16;
2910     return 32;
2911   }
2912
2913   // Write out stubs.
2914   void
2915   do_write(Output_file*);
2916
2917   // Plt call stub keys.
2918   class Plt_stub_ent
2919   {
2920   public:
2921     Plt_stub_ent(const Symbol* sym)
2922       : sym_(sym), object_(0), addend_(0), locsym_(0)
2923     { }
2924
2925     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
2926                  unsigned int locsym_index)
2927       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
2928     { }
2929
2930     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
2931                  const Symbol* sym,
2932                  unsigned int r_type,
2933                  Address addend)
2934       : sym_(sym), object_(0), addend_(0), locsym_(0)
2935     {
2936       if (size != 32)
2937         this->addend_ = addend;
2938       else if (parameters->options().output_is_position_independent()
2939                && r_type == elfcpp::R_PPC_PLTREL24)
2940         {
2941           this->addend_ = addend;
2942           if (this->addend_ >= 32768)
2943             this->object_ = object;
2944         }
2945     }
2946
2947     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
2948                  unsigned int locsym_index,
2949                  unsigned int r_type,
2950                  Address addend)
2951       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
2952     {
2953       if (size != 32)
2954         this->addend_ = addend;
2955       else if (parameters->options().output_is_position_independent()
2956                && r_type == elfcpp::R_PPC_PLTREL24)
2957         this->addend_ = addend;
2958     }
2959
2960     bool operator==(const Plt_stub_ent& that) const
2961     {
2962       return (this->sym_ == that.sym_
2963               && this->object_ == that.object_
2964               && this->addend_ == that.addend_
2965               && this->locsym_ == that.locsym_);
2966     }
2967
2968     const Symbol* sym_;
2969     const Sized_relobj_file<size, big_endian>* object_;
2970     typename elfcpp::Elf_types<size>::Elf_Addr addend_;
2971     unsigned int locsym_;
2972   };
2973
2974   class Plt_stub_ent_hash
2975   {
2976   public:
2977     size_t operator()(const Plt_stub_ent& ent) const
2978     {
2979       return (reinterpret_cast<uintptr_t>(ent.sym_)
2980               ^ reinterpret_cast<uintptr_t>(ent.object_)
2981               ^ ent.addend_
2982               ^ ent.locsym_);
2983     }
2984   };
2985
2986   // Long branch stub keys.
2987   class Branch_stub_ent
2988   {
2989   public:
2990     Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
2991       : dest_(to), toc_base_off_(0)
2992     {
2993       if (size == 64)
2994         toc_base_off_ = obj->toc_base_offset();
2995     }
2996
2997     bool operator==(const Branch_stub_ent& that) const
2998     {
2999       return (this->dest_ == that.dest_
3000               && (size == 32
3001                   || this->toc_base_off_ == that.toc_base_off_));
3002     }
3003
3004     Address dest_;
3005     unsigned int toc_base_off_;
3006   };
3007
3008   class Branch_stub_ent_hash
3009   {
3010   public:
3011     size_t operator()(const Branch_stub_ent& ent) const
3012     { return ent.dest_ ^ ent.toc_base_off_; }
3013   };
3014
3015   // In a sane world this would be a global.
3016   Target_powerpc<size, big_endian>* targ_;
3017   // Map sym/object/addend to stub offset.
3018   Plt_stub_entries plt_call_stubs_;
3019   // Map destination address to stub offset.
3020   typedef Unordered_map<Branch_stub_ent, unsigned int,
3021                         Branch_stub_ent_hash> Branch_stub_entries;
3022   Branch_stub_entries long_branch_stubs_;
3023   // size of input section
3024   section_size_type orig_data_size_;
3025   // size of stubs
3026   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3027 };
3028
3029 // Make a new stub table, and record.
3030
3031 template<int size, bool big_endian>
3032 Stub_table<size, big_endian>*
3033 Target_powerpc<size, big_endian>::new_stub_table()
3034 {
3035   Stub_table<size, big_endian>* stub_table
3036     = new Stub_table<size, big_endian>(this);
3037   this->stub_tables_.push_back(stub_table);
3038   return stub_table;
3039 }
3040
3041 // Delayed stub table initialisation, because we create the stub table
3042 // before we know to which section it will be attached.
3043
3044 template<int size, bool big_endian>
3045 void
3046 Stub_table<size, big_endian>::init(
3047     const Output_section::Input_section* owner,
3048     Output_section* output_section)
3049 {
3050   this->set_relobj(owner->relobj());
3051   this->set_shndx(owner->shndx());
3052   this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3053   this->set_output_section(output_section);
3054   this->orig_data_size_ = owner->current_data_size();
3055
3056   std::vector<Output_relaxed_input_section*> new_relaxed;
3057   new_relaxed.push_back(this);
3058   output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3059 }
3060
3061 // Add a plt call stub, if we do not already have one for this
3062 // sym/object/addend combo.
3063
3064 template<int size, bool big_endian>
3065 void
3066 Stub_table<size, big_endian>::add_plt_call_entry(
3067     const Sized_relobj_file<size, big_endian>* object,
3068     const Symbol* gsym,
3069     unsigned int r_type,
3070     Address addend)
3071 {
3072   Plt_stub_ent ent(object, gsym, r_type, addend);
3073   Address off = this->plt_size_;
3074   std::pair<typename Plt_stub_entries::iterator, bool> p
3075     = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3076   if (p.second)
3077     this->plt_size_ = off + this->plt_call_size(p.first);
3078 }
3079
3080 template<int size, bool big_endian>
3081 void
3082 Stub_table<size, big_endian>::add_plt_call_entry(
3083     const Sized_relobj_file<size, big_endian>* object,
3084     unsigned int locsym_index,
3085     unsigned int r_type,
3086     Address addend)
3087 {
3088   Plt_stub_ent ent(object, locsym_index, r_type, addend);
3089   Address off = this->plt_size_;
3090   std::pair<typename Plt_stub_entries::iterator, bool> p
3091     = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3092   if (p.second)
3093     this->plt_size_ = off + this->plt_call_size(p.first);
3094 }
3095
3096 // Find a plt call stub.
3097
3098 template<int size, bool big_endian>
3099 typename elfcpp::Elf_types<size>::Elf_Addr
3100 Stub_table<size, big_endian>::find_plt_call_entry(
3101     const Sized_relobj_file<size, big_endian>* object,
3102     const Symbol* gsym,
3103     unsigned int r_type,
3104     Address addend) const
3105 {
3106   Plt_stub_ent ent(object, gsym, r_type, addend);
3107   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3108   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3109 }
3110
3111 template<int size, bool big_endian>
3112 typename elfcpp::Elf_types<size>::Elf_Addr
3113 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3114 {
3115   Plt_stub_ent ent(gsym);
3116   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3117   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3118 }
3119
3120 template<int size, bool big_endian>
3121 typename elfcpp::Elf_types<size>::Elf_Addr
3122 Stub_table<size, big_endian>::find_plt_call_entry(
3123     const Sized_relobj_file<size, big_endian>* object,
3124     unsigned int locsym_index,
3125     unsigned int r_type,
3126     Address addend) const
3127 {
3128   Plt_stub_ent ent(object, locsym_index, r_type, addend);
3129   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3130   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3131 }
3132
3133 template<int size, bool big_endian>
3134 typename elfcpp::Elf_types<size>::Elf_Addr
3135 Stub_table<size, big_endian>::find_plt_call_entry(
3136     const Sized_relobj_file<size, big_endian>* object,
3137     unsigned int locsym_index) const
3138 {
3139   Plt_stub_ent ent(object, locsym_index);
3140   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3141   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3142 }
3143
3144 // Add a long branch stub if we don't already have one to given
3145 // destination.
3146
3147 template<int size, bool big_endian>
3148 void
3149 Stub_table<size, big_endian>::add_long_branch_entry(
3150     const Powerpc_relobj<size, big_endian>* object,
3151     Address to)
3152 {
3153   Branch_stub_ent ent(object, to);
3154   Address off = this->branch_size_;
3155   if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3156     {
3157       unsigned int stub_size = this->branch_stub_size(to);
3158       this->branch_size_ = off + stub_size;
3159       if (size == 64 && stub_size != 4)
3160         this->targ_->add_branch_lookup_table(to);
3161     }
3162 }
3163
3164 // Find long branch stub.
3165
3166 template<int size, bool big_endian>
3167 typename elfcpp::Elf_types<size>::Elf_Addr
3168 Stub_table<size, big_endian>::find_long_branch_entry(
3169     const Powerpc_relobj<size, big_endian>* object,
3170     Address to)
3171 {
3172   Branch_stub_ent ent(object, to);
3173   typename Branch_stub_entries::const_iterator p
3174     = this->long_branch_stubs_.find(ent);
3175   return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3176 }
3177
3178 // A class to handle .glink.
3179
3180 template<int size, bool big_endian>
3181 class Output_data_glink : public Output_section_data
3182 {
3183  public:
3184   static const int pltresolve_size = 16*4;
3185
3186   Output_data_glink(Target_powerpc<size, big_endian>* targ)
3187     : Output_section_data(16), targ_(targ)
3188   { }
3189
3190  protected:
3191   // Write to a map file.
3192   void
3193   do_print_to_mapfile(Mapfile* mapfile) const
3194   { mapfile->print_output_data(this, _("** glink")); }
3195
3196  private:
3197   void
3198   set_final_data_size();
3199
3200   // Write out .glink
3201   void
3202   do_write(Output_file*);
3203
3204   // Allows access to .got and .plt for do_write.
3205   Target_powerpc<size, big_endian>* targ_;
3206 };
3207
3208 template<int size, bool big_endian>
3209 void
3210 Output_data_glink<size, big_endian>::set_final_data_size()
3211 {
3212   unsigned int count = this->targ_->plt_entry_count();
3213   section_size_type total = 0;
3214
3215   if (count != 0)
3216     {
3217       if (size == 32)
3218         {
3219           // space for branch table
3220           total += 4 * (count - 1);
3221
3222           total += -total & 15;
3223           total += this->pltresolve_size;
3224         }
3225       else
3226         {
3227           total += this->pltresolve_size;
3228
3229           // space for branch table
3230           total += 8 * count;
3231           if (count > 0x8000)
3232             total += 4 * (count - 0x8000);
3233         }
3234     }
3235
3236   this->set_data_size(total);
3237 }
3238
3239 // Write out plt and long branch stub code.
3240
3241 template<int size, bool big_endian>
3242 void
3243 Stub_table<size, big_endian>::do_write(Output_file* of)
3244 {
3245   if (this->plt_call_stubs_.empty()
3246       && this->long_branch_stubs_.empty())
3247     return;
3248
3249   const section_size_type start_off = this->offset();
3250   const section_size_type off = this->stub_offset();
3251   const section_size_type oview_size =
3252     convert_to_section_size_type(this->data_size() - (off - start_off));
3253   unsigned char* const oview = of->get_output_view(off, oview_size);
3254   unsigned char* p;
3255
3256   if (size == 64)
3257     {
3258       const Output_data_got_powerpc<size, big_endian>* got
3259         = this->targ_->got_section();
3260       Address got_os_addr = got->output_section()->address();
3261
3262       if (!this->plt_call_stubs_.empty())
3263         {
3264           // The base address of the .plt section.
3265           Address plt_base = this->targ_->plt_section()->address();
3266           Address iplt_base = invalid_address;
3267
3268           // Write out plt call stubs.
3269           typename Plt_stub_entries::const_iterator cs;
3270           for (cs = this->plt_call_stubs_.begin();
3271                cs != this->plt_call_stubs_.end();
3272                ++cs)
3273             {
3274               Address pltoff;
3275               bool is_ifunc;
3276               const Symbol* gsym = cs->first.sym_;
3277               if (gsym != NULL)
3278                 {
3279                   is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
3280                               && gsym->can_use_relative_reloc(false));
3281                   pltoff = gsym->plt_offset();
3282                 }
3283               else
3284                 {
3285                   is_ifunc = true;
3286                   const Sized_relobj_file<size, big_endian>* relobj
3287                     = cs->first.object_;
3288                   unsigned int local_sym_index = cs->first.locsym_;
3289                   pltoff = relobj->local_plt_offset(local_sym_index);
3290                 }
3291               Address plt_addr = pltoff;
3292               if (is_ifunc)
3293                 {
3294                   if (iplt_base == invalid_address)
3295                     iplt_base = this->targ_->iplt_section()->address();
3296                   plt_addr += iplt_base;
3297                 }
3298               else
3299                 plt_addr += plt_base;
3300               const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3301                 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
3302               Address got_addr = got_os_addr + ppcobj->toc_base_offset();
3303               Address off = plt_addr - got_addr;
3304
3305               if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
3306                 gold_error(_("%s: linkage table error against `%s'"),
3307                            cs->first.object_->name().c_str(),
3308                            cs->first.sym_->demangled_name().c_str());
3309
3310               bool static_chain = parameters->options().plt_static_chain();
3311               bool thread_safe = this->targ_->plt_thread_safe();
3312               bool use_fake_dep = false;
3313               Address cmp_branch_off = 0;
3314               if (thread_safe)
3315                 {
3316                   unsigned int pltindex
3317                     = ((pltoff - this->targ_->first_plt_entry_offset())
3318                        / this->targ_->plt_entry_size());
3319                   Address glinkoff
3320                     = (this->targ_->glink_section()->pltresolve_size
3321                        + pltindex * 8);
3322                   if (pltindex > 32768)
3323                     glinkoff += (pltindex - 32768) * 4;
3324                   Address to
3325                     = this->targ_->glink_section()->address() + glinkoff;
3326                   Address from
3327                     = (this->stub_address() + cs->second + 24
3328                        + 4 * (ha(off) != 0)
3329                        + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
3330                        + 4 * static_chain);
3331                   cmp_branch_off = to - from;
3332                   use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
3333                 }
3334
3335               p = oview + cs->second;
3336               if (ha(off) != 0)
3337                 {
3338                   write_insn<big_endian>(p, std_2_1 + 40),              p += 4;
3339                   write_insn<big_endian>(p, addis_12_2 + ha(off)),      p += 4;
3340                   write_insn<big_endian>(p, ld_11_12 + l(off)),         p += 4;
3341                   if (ha(off + 8 + 8 * static_chain) != ha(off))
3342                     {
3343                       write_insn<big_endian>(p, addi_12_12 + l(off)),   p += 4;
3344                       off = 0;
3345                     }
3346                   write_insn<big_endian>(p, mtctr_11),                  p += 4;
3347                   if (use_fake_dep)
3348                     {
3349                       write_insn<big_endian>(p, xor_11_11_11),          p += 4;
3350                       write_insn<big_endian>(p, add_12_12_11),          p += 4;
3351                     }
3352                   write_insn<big_endian>(p, ld_2_12 + l(off + 8)),      p += 4;
3353                   if (static_chain)
3354                     write_insn<big_endian>(p, ld_11_12 + l(off + 16)),  p += 4;
3355                 }
3356               else
3357                 {
3358                   write_insn<big_endian>(p, std_2_1 + 40),              p += 4;
3359                   write_insn<big_endian>(p, ld_11_2 + l(off)),  p += 4;
3360                   if (ha(off + 8 + 8 * static_chain) != ha(off))
3361                     {
3362                       write_insn<big_endian>(p, addi_2_2 + l(off)),     p += 4;
3363                       off = 0;
3364                     }
3365                   write_insn<big_endian>(p, mtctr_11),                  p += 4;
3366                   if (use_fake_dep)
3367                     {
3368                       write_insn<big_endian>(p, xor_11_11_11),          p += 4;
3369                       write_insn<big_endian>(p, add_2_2_11),            p += 4;
3370                     }
3371                   if (static_chain)
3372                     write_insn<big_endian>(p, ld_11_2 + l(off + 16)),   p += 4;
3373                   write_insn<big_endian>(p, ld_2_2 + l(off + 8)),       p += 4;
3374                 }
3375               if (thread_safe && !use_fake_dep)
3376                 {
3377                   write_insn<big_endian>(p, cmpldi_2_0),                p += 4;
3378                   write_insn<big_endian>(p, bnectr_p4),                 p += 4;
3379                   write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
3380                 }
3381               else
3382                 write_insn<big_endian>(p, bctr);
3383             }
3384         }
3385
3386       // Write out long branch stubs.
3387       typename Branch_stub_entries::const_iterator bs;
3388       for (bs = this->long_branch_stubs_.begin();
3389            bs != this->long_branch_stubs_.end();
3390            ++bs)
3391         {
3392           p = oview + this->plt_size_ + bs->second;
3393           Address loc = this->stub_address() + this->plt_size_ + bs->second;
3394           Address delta = bs->first.dest_ - loc;
3395           if (delta + (1 << 25) < 2 << 25)
3396             write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3397           else
3398             {
3399               Address brlt_addr
3400                 = this->targ_->find_branch_lookup_table(bs->first.dest_);
3401               gold_assert(brlt_addr != invalid_address);
3402               brlt_addr += this->targ_->brlt_section()->address();
3403               Address got_addr = got_os_addr + bs->first.toc_base_off_;
3404               Address brltoff = brlt_addr - got_addr;
3405               if (ha(brltoff) == 0)
3406                 {
3407                   write_insn<big_endian>(p, ld_11_2 + l(brltoff)),      p += 4;
3408                 }
3409               else
3410                 {
3411                   write_insn<big_endian>(p, addis_12_2 + ha(brltoff)),  p += 4;
3412                   write_insn<big_endian>(p, ld_11_12 + l(brltoff)),     p += 4;
3413                 }
3414               write_insn<big_endian>(p, mtctr_11),                      p += 4;
3415               write_insn<big_endian>(p, bctr);
3416             }
3417         }
3418     }
3419   else
3420     {
3421       if (!this->plt_call_stubs_.empty())
3422         {
3423           // The base address of the .plt section.
3424           Address plt_base = this->targ_->plt_section()->address();
3425           Address iplt_base = invalid_address;
3426           // The address of _GLOBAL_OFFSET_TABLE_.
3427           Address g_o_t = invalid_address;
3428
3429           // Write out plt call stubs.
3430           typename Plt_stub_entries::const_iterator cs;
3431           for (cs = this->plt_call_stubs_.begin();
3432                cs != this->plt_call_stubs_.end();
3433                ++cs)
3434             {
3435               Address plt_addr;
3436               bool is_ifunc;
3437               const Symbol* gsym = cs->first.sym_;
3438               if (gsym != NULL)
3439                 {
3440                   is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
3441                               && gsym->can_use_relative_reloc(false));
3442                   plt_addr = gsym->plt_offset();
3443                 }
3444               else
3445                 {
3446                   is_ifunc = true;
3447                   const Sized_relobj_file<size, big_endian>* relobj
3448                     = cs->first.object_;
3449                   unsigned int local_sym_index = cs->first.locsym_;
3450                   plt_addr = relobj->local_plt_offset(local_sym_index);
3451                 }
3452               if (is_ifunc)
3453                 {
3454                   if (iplt_base == invalid_address)
3455                     iplt_base = this->targ_->iplt_section()->address();
3456                   plt_addr += iplt_base;
3457                 }
3458               else
3459                 plt_addr += plt_base;
3460
3461               p = oview + cs->second;
3462               if (parameters->options().output_is_position_independent())
3463                 {
3464                   Address got_addr;
3465                   const Powerpc_relobj<size, big_endian>* ppcobj
3466                     = (static_cast<const Powerpc_relobj<size, big_endian>*>
3467                        (cs->first.object_));
3468                   if (ppcobj != NULL && cs->first.addend_ >= 32768)
3469                     {
3470                       unsigned int got2 = ppcobj->got2_shndx();
3471                       got_addr = ppcobj->get_output_section_offset(got2);
3472                       gold_assert(got_addr != invalid_address);
3473                       got_addr += (ppcobj->output_section(got2)->address()
3474                                    + cs->first.addend_);
3475                     }
3476                   else
3477                     {
3478                       if (g_o_t == invalid_address)
3479                         {
3480                           const Output_data_got_powerpc<size, big_endian>* got
3481                             = this->targ_->got_section();
3482                           g_o_t = got->address() + got->g_o_t();
3483                         }
3484                       got_addr = g_o_t;
3485                     }
3486
3487                   Address off = plt_addr - got_addr;
3488                   if (ha(off) == 0)
3489                     {
3490                       write_insn<big_endian>(p +  0, lwz_11_30 + l(off));
3491                       write_insn<big_endian>(p +  4, mtctr_11);
3492                       write_insn<big_endian>(p +  8, bctr);
3493                     }
3494                   else
3495                     {
3496                       write_insn<big_endian>(p +  0, addis_11_30 + ha(off));
3497                       write_insn<big_endian>(p +  4, lwz_11_11 + l(off));
3498                       write_insn<big_endian>(p +  8, mtctr_11);
3499                       write_insn<big_endian>(p + 12, bctr);
3500                     }
3501                 }
3502               else
3503                 {
3504                   write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
3505                   write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
3506                   write_insn<big_endian>(p +  8, mtctr_11);
3507                   write_insn<big_endian>(p + 12, bctr);
3508                 }
3509             }
3510         }
3511
3512       // Write out long branch stubs.
3513       typename Branch_stub_entries::const_iterator bs;
3514       for (bs = this->long_branch_stubs_.begin();
3515            bs != this->long_branch_stubs_.end();
3516            ++bs)
3517         {
3518           p = oview + this->plt_size_ + bs->second;
3519           Address loc = this->stub_address() + this->plt_size_ + bs->second;
3520           Address delta = bs->first.dest_ - loc;
3521           if (delta + (1 << 25) < 2 << 25)
3522             write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3523           else if (!parameters->options().output_is_position_independent())
3524             {
3525               write_insn<big_endian>(p +  0, lis_12 + ha(bs->first.dest_));
3526               write_insn<big_endian>(p +  4, addi_12_12 + l(bs->first.dest_));
3527               write_insn<big_endian>(p +  8, mtctr_12);
3528               write_insn<big_endian>(p + 12, bctr);
3529             }
3530           else
3531             {
3532               delta -= 8;
3533               write_insn<big_endian>(p +  0, mflr_0);
3534               write_insn<big_endian>(p +  4, bcl_20_31);
3535               write_insn<big_endian>(p +  8, mflr_12);
3536               write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
3537               write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
3538               write_insn<big_endian>(p + 20, mtlr_0);
3539               write_insn<big_endian>(p + 24, mtctr_12);
3540               write_insn<big_endian>(p + 28, bctr);
3541             }
3542         }
3543     }
3544 }
3545
3546 // Write out .glink.
3547
3548 template<int size, bool big_endian>
3549 void
3550 Output_data_glink<size, big_endian>::do_write(Output_file* of)
3551 {
3552   const section_size_type off = this->offset();
3553   const section_size_type oview_size =
3554     convert_to_section_size_type(this->data_size());
3555   unsigned char* const oview = of->get_output_view(off, oview_size);
3556   unsigned char* p;
3557
3558   // The base address of the .plt section.
3559   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3560   Address plt_base = this->targ_->plt_section()->address();
3561
3562   if (size == 64)
3563     {
3564       // Write pltresolve stub.
3565       p = oview;
3566       Address after_bcl = this->address() + 16;
3567       Address pltoff = plt_base - after_bcl;
3568
3569       elfcpp::Swap<64, big_endian>::writeval(p, pltoff),        p += 8;
3570
3571       write_insn<big_endian>(p, mflr_12),                       p += 4;
3572       write_insn<big_endian>(p, bcl_20_31),                     p += 4;
3573       write_insn<big_endian>(p, mflr_11),                       p += 4;
3574       write_insn<big_endian>(p, ld_2_11 + l(-16)),              p += 4;
3575       write_insn<big_endian>(p, mtlr_12),                       p += 4;
3576       write_insn<big_endian>(p, add_12_2_11),                   p += 4;
3577       write_insn<big_endian>(p, ld_11_12 + 0),                  p += 4;
3578       write_insn<big_endian>(p, ld_2_12 + 8),                   p += 4;
3579       write_insn<big_endian>(p, mtctr_11),                      p += 4;
3580       write_insn<big_endian>(p, ld_11_12 + 16),                 p += 4;
3581       write_insn<big_endian>(p, bctr),                          p += 4;
3582       while (p < oview + this->pltresolve_size)
3583         write_insn<big_endian>(p, nop), p += 4;
3584
3585       // Write lazy link call stubs.
3586       uint32_t indx = 0;
3587       while (p < oview + oview_size)
3588         {
3589           if (indx < 0x8000)
3590             {
3591               write_insn<big_endian>(p, li_0_0 + indx),                 p += 4;
3592             }
3593           else
3594             {
3595               write_insn<big_endian>(p, lis_0_0 + hi(indx)),            p += 4;
3596               write_insn<big_endian>(p, ori_0_0_0 + l(indx)),           p += 4;
3597             }
3598           uint32_t branch_off = 8 - (p - oview);
3599           write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),      p += 4;
3600           indx++;
3601         }
3602     }
3603   else
3604     {
3605       const Output_data_got_powerpc<size, big_endian>* got
3606         = this->targ_->got_section();
3607       // The address of _GLOBAL_OFFSET_TABLE_.
3608       Address g_o_t = got->address() + got->g_o_t();
3609
3610       // Write out pltresolve branch table.
3611       p = oview;
3612       unsigned int the_end = oview_size - this->pltresolve_size;
3613       unsigned char* end_p = oview + the_end;
3614       while (p < end_p - 8 * 4)
3615         write_insn<big_endian>(p, b + end_p - p), p += 4;
3616       while (p < end_p)
3617         write_insn<big_endian>(p, nop), p += 4;
3618
3619       // Write out pltresolve call stub.
3620       if (parameters->options().output_is_position_independent())
3621         {
3622           Address res0_off = 0;
3623           Address after_bcl_off = the_end + 12;
3624           Address bcl_res0 = after_bcl_off - res0_off;
3625
3626           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
3627           write_insn<big_endian>(p +  4, mflr_0);
3628           write_insn<big_endian>(p +  8, bcl_20_31);
3629           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
3630           write_insn<big_endian>(p + 16, mflr_12);
3631           write_insn<big_endian>(p + 20, mtlr_0);
3632           write_insn<big_endian>(p + 24, sub_11_11_12);
3633
3634           Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
3635
3636           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
3637           if (ha(got_bcl) == ha(got_bcl + 4))
3638             {
3639               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
3640               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
3641             }
3642           else
3643             {
3644               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
3645               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
3646             }
3647           write_insn<big_endian>(p + 40, mtctr_0);
3648           write_insn<big_endian>(p + 44, add_0_11_11);
3649           write_insn<big_endian>(p + 48, add_11_0_11);
3650           write_insn<big_endian>(p + 52, bctr);
3651           write_insn<big_endian>(p + 56, nop);
3652           write_insn<big_endian>(p + 60, nop);
3653         }
3654       else
3655         {
3656           Address res0 = this->address();
3657
3658           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
3659           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
3660           if (ha(g_o_t + 4) == ha(g_o_t + 8))
3661             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
3662           else
3663             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
3664           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
3665           write_insn<big_endian>(p + 16, mtctr_0);
3666           write_insn<big_endian>(p + 20, add_0_11_11);
3667           if (ha(g_o_t + 4) == ha(g_o_t + 8))
3668             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
3669           else
3670             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
3671           write_insn<big_endian>(p + 28, add_11_0_11);
3672           write_insn<big_endian>(p + 32, bctr);
3673           write_insn<big_endian>(p + 36, nop);
3674           write_insn<big_endian>(p + 40, nop);
3675           write_insn<big_endian>(p + 44, nop);
3676           write_insn<big_endian>(p + 48, nop);
3677           write_insn<big_endian>(p + 52, nop);
3678           write_insn<big_endian>(p + 56, nop);
3679           write_insn<big_endian>(p + 60, nop);
3680         }
3681       p += 64;
3682     }
3683
3684   of->write_output_view(off, oview_size, oview);
3685 }
3686
3687
3688 // A class to handle linker generated save/restore functions.
3689
3690 template<int size, bool big_endian>
3691 class Output_data_save_res : public Output_section_data_build
3692 {
3693  public:
3694   Output_data_save_res(Symbol_table* symtab);
3695
3696  protected:
3697   // Write to a map file.
3698   void
3699   do_print_to_mapfile(Mapfile* mapfile) const
3700   { mapfile->print_output_data(this, _("** save/restore")); }
3701
3702   void
3703   do_write(Output_file*);
3704
3705  private:
3706   // The maximum size of save/restore contents.
3707   static const unsigned int savres_max = 218*4;
3708
3709   void
3710   savres_define(Symbol_table* symtab,
3711                 const char *name,
3712                 unsigned int lo, unsigned int hi,
3713                 unsigned char* write_ent(unsigned char*, int),
3714                 unsigned char* write_tail(unsigned char*, int));
3715
3716   unsigned char *contents_;
3717 };
3718
3719 template<bool big_endian>
3720 static unsigned char*
3721 savegpr0(unsigned char* p, int r)
3722 {
3723   uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3724   write_insn<big_endian>(p, insn);
3725   return p + 4;
3726 }
3727
3728 template<bool big_endian>
3729 static unsigned char*
3730 savegpr0_tail(unsigned char* p, int r)
3731 {
3732   p = savegpr0<big_endian>(p, r);
3733   uint32_t insn = std_0_1 + 16;
3734   write_insn<big_endian>(p, insn);
3735   p = p + 4;
3736   write_insn<big_endian>(p, blr);
3737   return p + 4;
3738 }
3739
3740 template<bool big_endian>
3741 static unsigned char*
3742 restgpr0(unsigned char* p, int r)
3743 {
3744   uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3745   write_insn<big_endian>(p, insn);
3746   return p + 4;
3747 }
3748
3749 template<bool big_endian>
3750 static unsigned char*
3751 restgpr0_tail(unsigned char* p, int r)
3752 {
3753   uint32_t insn = ld_0_1 + 16;
3754   write_insn<big_endian>(p, insn);
3755   p = p + 4;
3756   p = restgpr0<big_endian>(p, r);
3757   write_insn<big_endian>(p, mtlr_0);
3758   p = p + 4;
3759   if (r == 29)
3760     {
3761       p = restgpr0<big_endian>(p, 30);
3762       p = restgpr0<big_endian>(p, 31);
3763     }
3764   write_insn<big_endian>(p, blr);
3765   return p + 4;
3766 }
3767
3768 template<bool big_endian>
3769 static unsigned char*
3770 savegpr1(unsigned char* p, int r)
3771 {
3772   uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
3773   write_insn<big_endian>(p, insn);
3774   return p + 4;
3775 }
3776
3777 template<bool big_endian>
3778 static unsigned char*
3779 savegpr1_tail(unsigned char* p, int r)
3780 {
3781   p = savegpr1<big_endian>(p, r);
3782   write_insn<big_endian>(p, blr);
3783   return p + 4;
3784 }
3785
3786 template<bool big_endian>
3787 static unsigned char*
3788 restgpr1(unsigned char* p, int r)
3789 {
3790   uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
3791   write_insn<big_endian>(p, insn);
3792   return p + 4;
3793 }
3794
3795 template<bool big_endian>
3796 static unsigned char*
3797 restgpr1_tail(unsigned char* p, int r)
3798 {
3799   p = restgpr1<big_endian>(p, r);
3800   write_insn<big_endian>(p, blr);
3801   return p + 4;
3802 }
3803
3804 template<bool big_endian>
3805 static unsigned char*
3806 savefpr(unsigned char* p, int r)
3807 {
3808   uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3809   write_insn<big_endian>(p, insn);
3810   return p + 4;
3811 }
3812
3813 template<bool big_endian>
3814 static unsigned char*
3815 savefpr0_tail(unsigned char* p, int r)
3816 {
3817   p = savefpr<big_endian>(p, r);
3818   write_insn<big_endian>(p, std_0_1 + 16);
3819   p = p + 4;
3820   write_insn<big_endian>(p, blr);
3821   return p + 4;
3822 }
3823
3824 template<bool big_endian>
3825 static unsigned char*
3826 restfpr(unsigned char* p, int r)
3827 {
3828   uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
3829   write_insn<big_endian>(p, insn);
3830   return p + 4;
3831 }
3832
3833 template<bool big_endian>
3834 static unsigned char*
3835 restfpr0_tail(unsigned char* p, int r)
3836 {
3837   write_insn<big_endian>(p, ld_0_1 + 16);
3838   p = p + 4;
3839   p = restfpr<big_endian>(p, r);
3840   write_insn<big_endian>(p, mtlr_0);
3841   p = p + 4;
3842   if (r == 29)
3843     {
3844       p = restfpr<big_endian>(p, 30);
3845       p = restfpr<big_endian>(p, 31);
3846     }
3847   write_insn<big_endian>(p, blr);
3848   return p + 4;
3849 }
3850
3851 template<bool big_endian>
3852 static unsigned char*
3853 savefpr1_tail(unsigned char* p, int r)
3854 {
3855   p = savefpr<big_endian>(p, r);
3856   write_insn<big_endian>(p, blr);
3857   return p + 4;
3858 }
3859
3860 template<bool big_endian>
3861 static unsigned char*
3862 restfpr1_tail(unsigned char* p, int r)
3863 {
3864   p = restfpr<big_endian>(p, r);
3865   write_insn<big_endian>(p, blr);
3866   return p + 4;
3867 }
3868
3869 template<bool big_endian>
3870 static unsigned char*
3871 savevr(unsigned char* p, int r)
3872 {
3873   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
3874   write_insn<big_endian>(p, insn);
3875   p = p + 4;
3876   insn = stvx_0_12_0 + (r << 21);
3877   write_insn<big_endian>(p, insn);
3878   return p + 4;
3879 }
3880
3881 template<bool big_endian>
3882 static unsigned char*
3883 savevr_tail(unsigned char* p, int r)
3884 {
3885   p = savevr<big_endian>(p, r);
3886   write_insn<big_endian>(p, blr);
3887   return p + 4;
3888 }
3889
3890 template<bool big_endian>
3891 static unsigned char*
3892 restvr(unsigned char* p, int r)
3893 {
3894   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
3895   write_insn<big_endian>(p, insn);
3896   p = p + 4;
3897   insn = lvx_0_12_0 + (r << 21);
3898   write_insn<big_endian>(p, insn);
3899   return p + 4;
3900 }
3901
3902 template<bool big_endian>
3903 static unsigned char*
3904 restvr_tail(unsigned char* p, int r)
3905 {
3906   p = restvr<big_endian>(p, r);
3907   write_insn<big_endian>(p, blr);
3908   return p + 4;
3909 }
3910
3911
3912 template<int size, bool big_endian>
3913 Output_data_save_res<size, big_endian>::Output_data_save_res(
3914     Symbol_table* symtab)
3915   : Output_section_data_build(4),
3916     contents_(NULL)
3917 {
3918   this->savres_define(symtab,
3919                       "_savegpr0_", 14, 31,
3920                       savegpr0<big_endian>, savegpr0_tail<big_endian>);
3921   this->savres_define(symtab,
3922                       "_restgpr0_", 14, 29,
3923                       restgpr0<big_endian>, restgpr0_tail<big_endian>);
3924   this->savres_define(symtab,
3925                       "_restgpr0_", 30, 31,
3926                       restgpr0<big_endian>, restgpr0_tail<big_endian>);
3927   this->savres_define(symtab,
3928                       "_savegpr1_", 14, 31,
3929                       savegpr1<big_endian>, savegpr1_tail<big_endian>);
3930   this->savres_define(symtab,
3931                       "_restgpr1_", 14, 31,
3932                       restgpr1<big_endian>, restgpr1_tail<big_endian>);
3933   this->savres_define(symtab,
3934                       "_savefpr_", 14, 31,
3935                       savefpr<big_endian>, savefpr0_tail<big_endian>);
3936   this->savres_define(symtab,
3937                       "_restfpr_", 14, 29,
3938                       restfpr<big_endian>, restfpr0_tail<big_endian>);
3939   this->savres_define(symtab,
3940                       "_restfpr_", 30, 31,
3941                       restfpr<big_endian>, restfpr0_tail<big_endian>);
3942   this->savres_define(symtab,
3943                       "._savef", 14, 31,
3944                       savefpr<big_endian>, savefpr1_tail<big_endian>);
3945   this->savres_define(symtab,
3946                       "._restf", 14, 31,
3947                       restfpr<big_endian>, restfpr1_tail<big_endian>);
3948   this->savres_define(symtab,
3949                       "_savevr_", 20, 31,
3950                       savevr<big_endian>, savevr_tail<big_endian>);
3951   this->savres_define(symtab,
3952                       "_restvr_", 20, 31,
3953                       restvr<big_endian>, restvr_tail<big_endian>);
3954 }
3955
3956 template<int size, bool big_endian>
3957 void
3958 Output_data_save_res<size, big_endian>::savres_define(
3959     Symbol_table* symtab,
3960     const char *name,
3961     unsigned int lo, unsigned int hi,
3962     unsigned char* write_ent(unsigned char*, int),
3963     unsigned char* write_tail(unsigned char*, int))
3964 {
3965   size_t len = strlen(name);
3966   bool writing = false;
3967   char sym[16];
3968
3969   memcpy(sym, name, len);
3970   sym[len + 2] = 0;
3971
3972   for (unsigned int i = lo; i <= hi; i++)
3973     {
3974       sym[len + 0] = i / 10 + '0';
3975       sym[len + 1] = i % 10 + '0';
3976       Symbol* gsym = symtab->lookup(sym);
3977       bool refd = gsym != NULL && gsym->is_undefined();
3978       writing = writing || refd;
3979       if (writing)
3980         {
3981           if (this->contents_ == NULL)
3982             this->contents_ = new unsigned char[this->savres_max];
3983
3984           section_size_type value = this->current_data_size();
3985           unsigned char* p = this->contents_ + value;
3986           if (i != hi)
3987             p = write_ent(p, i);
3988           else
3989             p = write_tail(p, i);
3990           section_size_type cur_size = p - this->contents_;
3991           this->set_current_data_size(cur_size);
3992           if (refd)
3993             symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
3994                                           this, value, cur_size - value,
3995                                           elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
3996                                           elfcpp::STV_HIDDEN, 0, false, false);
3997         }
3998     }
3999 }
4000
4001 // Write out save/restore.
4002
4003 template<int size, bool big_endian>
4004 void
4005 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4006 {
4007   const section_size_type off = this->offset();
4008   const section_size_type oview_size =
4009     convert_to_section_size_type(this->data_size());
4010   unsigned char* const oview = of->get_output_view(off, oview_size);
4011   memcpy(oview, this->contents_, oview_size);
4012   of->write_output_view(off, oview_size, oview);
4013 }
4014
4015
4016 // Create the glink section.
4017
4018 template<int size, bool big_endian>
4019 void
4020 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4021 {
4022   if (this->glink_ == NULL)
4023     {
4024       this->glink_ = new Output_data_glink<size, big_endian>(this);
4025       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4026                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4027                                       this->glink_, ORDER_TEXT, false);
4028     }
4029 }
4030
4031 // Create a PLT entry for a global symbol.
4032
4033 template<int size, bool big_endian>
4034 void
4035 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
4036                                                  Layout* layout,
4037                                                  Symbol* gsym)
4038 {
4039   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4040       && gsym->can_use_relative_reloc(false))
4041     {
4042       if (this->iplt_ == NULL)
4043         this->make_iplt_section(symtab, layout);
4044       this->iplt_->add_ifunc_entry(gsym);
4045     }
4046   else
4047     {
4048       if (this->plt_ == NULL)
4049         this->make_plt_section(symtab, layout);
4050       this->plt_->add_entry(gsym);
4051     }
4052 }
4053
4054 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4055
4056 template<int size, bool big_endian>
4057 void
4058 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
4059     Symbol_table* symtab,
4060     Layout* layout,
4061     Sized_relobj_file<size, big_endian>* relobj,
4062     unsigned int r_sym)
4063 {
4064   if (this->iplt_ == NULL)
4065     this->make_iplt_section(symtab, layout);
4066   this->iplt_->add_local_ifunc_entry(relobj, r_sym);
4067 }
4068
4069 // Return the number of entries in the PLT.
4070
4071 template<int size, bool big_endian>
4072 unsigned int
4073 Target_powerpc<size, big_endian>::plt_entry_count() const
4074 {
4075   if (this->plt_ == NULL)
4076     return 0;
4077   unsigned int count = this->plt_->entry_count();
4078   if (this->iplt_ != NULL)
4079     count += this->iplt_->entry_count();
4080   return count;
4081 }
4082
4083 // Return the offset of the first non-reserved PLT entry.
4084
4085 template<int size, bool big_endian>
4086 unsigned int
4087 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
4088 {
4089   return this->plt_->first_plt_entry_offset();
4090 }
4091
4092 // Return the size of each PLT entry.
4093
4094 template<int size, bool big_endian>
4095 unsigned int
4096 Target_powerpc<size, big_endian>::plt_entry_size() const
4097 {
4098   return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
4099 }
4100
4101 // Create a GOT entry for local dynamic __tls_get_addr calls.
4102
4103 template<int size, bool big_endian>
4104 unsigned int
4105 Target_powerpc<size, big_endian>::tlsld_got_offset(
4106     Symbol_table* symtab,
4107     Layout* layout,
4108     Sized_relobj_file<size, big_endian>* object)
4109 {
4110   if (this->tlsld_got_offset_ == -1U)
4111     {
4112       gold_assert(symtab != NULL && layout != NULL && object != NULL);
4113       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4114       Output_data_got_powerpc<size, big_endian>* got
4115         = this->got_section(symtab, layout);
4116       unsigned int got_offset = got->add_constant_pair(0, 0);
4117       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
4118                           got_offset, 0);
4119       this->tlsld_got_offset_ = got_offset;
4120     }
4121   return this->tlsld_got_offset_;
4122 }
4123
4124 // Get the Reference_flags for a particular relocation.
4125
4126 template<int size, bool big_endian>
4127 int
4128 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
4129 {
4130   switch (r_type)
4131     {
4132     case elfcpp::R_POWERPC_NONE:
4133     case elfcpp::R_POWERPC_GNU_VTINHERIT:
4134     case elfcpp::R_POWERPC_GNU_VTENTRY:
4135     case elfcpp::R_PPC64_TOC:
4136       // No symbol reference.
4137       return 0;
4138
4139     case elfcpp::R_PPC64_ADDR64:
4140     case elfcpp::R_PPC64_UADDR64:
4141     case elfcpp::R_POWERPC_ADDR32:
4142     case elfcpp::R_POWERPC_UADDR32:
4143     case elfcpp::R_POWERPC_ADDR16:
4144     case elfcpp::R_POWERPC_UADDR16:
4145     case elfcpp::R_POWERPC_ADDR16_LO:
4146     case elfcpp::R_POWERPC_ADDR16_HI:
4147     case elfcpp::R_POWERPC_ADDR16_HA:
4148       return Symbol::ABSOLUTE_REF;
4149
4150     case elfcpp::R_POWERPC_ADDR24:
4151     case elfcpp::R_POWERPC_ADDR14:
4152     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4153     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4154       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
4155
4156     case elfcpp::R_PPC64_REL64:
4157     case elfcpp::R_POWERPC_REL32:
4158     case elfcpp::R_PPC_LOCAL24PC:
4159     case elfcpp::R_POWERPC_REL16:
4160     case elfcpp::R_POWERPC_REL16_LO:
4161     case elfcpp::R_POWERPC_REL16_HI:
4162     case elfcpp::R_POWERPC_REL16_HA:
4163       return Symbol::RELATIVE_REF;
4164
4165     case elfcpp::R_POWERPC_REL24:
4166     case elfcpp::R_PPC_PLTREL24:
4167     case elfcpp::R_POWERPC_REL14:
4168     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4169     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4170       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
4171
4172     case elfcpp::R_POWERPC_GOT16:
4173     case elfcpp::R_POWERPC_GOT16_LO:
4174     case elfcpp::R_POWERPC_GOT16_HI:
4175     case elfcpp::R_POWERPC_GOT16_HA:
4176     case elfcpp::R_PPC64_GOT16_DS:
4177     case elfcpp::R_PPC64_GOT16_LO_DS:
4178     case elfcpp::R_PPC64_TOC16:
4179     case elfcpp::R_PPC64_TOC16_LO:
4180     case elfcpp::R_PPC64_TOC16_HI:
4181     case elfcpp::R_PPC64_TOC16_HA:
4182     case elfcpp::R_PPC64_TOC16_DS:
4183     case elfcpp::R_PPC64_TOC16_LO_DS:
4184       // Absolute in GOT.
4185       return Symbol::ABSOLUTE_REF;
4186
4187     case elfcpp::R_POWERPC_GOT_TPREL16:
4188     case elfcpp::R_POWERPC_TLS:
4189       return Symbol::TLS_REF;
4190
4191     case elfcpp::R_POWERPC_COPY:
4192     case elfcpp::R_POWERPC_GLOB_DAT:
4193     case elfcpp::R_POWERPC_JMP_SLOT:
4194     case elfcpp::R_POWERPC_RELATIVE:
4195     case elfcpp::R_POWERPC_DTPMOD:
4196     default:
4197       // Not expected.  We will give an error later.
4198       return 0;
4199     }
4200 }
4201
4202 // Report an unsupported relocation against a local symbol.
4203
4204 template<int size, bool big_endian>
4205 void
4206 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
4207     Sized_relobj_file<size, big_endian>* object,
4208     unsigned int r_type)
4209 {
4210   gold_error(_("%s: unsupported reloc %u against local symbol"),
4211              object->name().c_str(), r_type);
4212 }
4213
4214 // We are about to emit a dynamic relocation of type R_TYPE.  If the
4215 // dynamic linker does not support it, issue an error.
4216
4217 template<int size, bool big_endian>
4218 void
4219 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
4220                                                       unsigned int r_type)
4221 {
4222   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
4223
4224   // These are the relocation types supported by glibc for both 32-bit
4225   // and 64-bit powerpc.
4226   switch (r_type)
4227     {
4228     case elfcpp::R_POWERPC_NONE:
4229     case elfcpp::R_POWERPC_RELATIVE:
4230     case elfcpp::R_POWERPC_GLOB_DAT:
4231     case elfcpp::R_POWERPC_DTPMOD:
4232     case elfcpp::R_POWERPC_DTPREL:
4233     case elfcpp::R_POWERPC_TPREL:
4234     case elfcpp::R_POWERPC_JMP_SLOT:
4235     case elfcpp::R_POWERPC_COPY:
4236     case elfcpp::R_POWERPC_IRELATIVE:
4237     case elfcpp::R_POWERPC_ADDR32:
4238     case elfcpp::R_POWERPC_UADDR32:
4239     case elfcpp::R_POWERPC_ADDR24:
4240     case elfcpp::R_POWERPC_ADDR16:
4241     case elfcpp::R_POWERPC_UADDR16:
4242     case elfcpp::R_POWERPC_ADDR16_LO:
4243     case elfcpp::R_POWERPC_ADDR16_HI:
4244     case elfcpp::R_POWERPC_ADDR16_HA:
4245     case elfcpp::R_POWERPC_ADDR14:
4246     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4247     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4248     case elfcpp::R_POWERPC_REL32:
4249     case elfcpp::R_POWERPC_REL24:
4250     case elfcpp::R_POWERPC_TPREL16:
4251     case elfcpp::R_POWERPC_TPREL16_LO:
4252     case elfcpp::R_POWERPC_TPREL16_HI:
4253     case elfcpp::R_POWERPC_TPREL16_HA:
4254       return;
4255
4256     default:
4257       break;
4258     }
4259
4260   if (size == 64)
4261     {
4262       switch (r_type)
4263         {
4264           // These are the relocation types supported only on 64-bit.
4265         case elfcpp::R_PPC64_ADDR64:
4266         case elfcpp::R_PPC64_UADDR64:
4267         case elfcpp::R_PPC64_JMP_IREL:
4268         case elfcpp::R_PPC64_ADDR16_DS:
4269         case elfcpp::R_PPC64_ADDR16_LO_DS:
4270         case elfcpp::R_PPC64_ADDR16_HIGHER:
4271         case elfcpp::R_PPC64_ADDR16_HIGHEST:
4272         case elfcpp::R_PPC64_ADDR16_HIGHERA:
4273         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4274         case elfcpp::R_PPC64_REL64:
4275         case elfcpp::R_POWERPC_ADDR30:
4276         case elfcpp::R_PPC64_TPREL16_DS:
4277         case elfcpp::R_PPC64_TPREL16_LO_DS:
4278         case elfcpp::R_PPC64_TPREL16_HIGHER:
4279         case elfcpp::R_PPC64_TPREL16_HIGHEST:
4280         case elfcpp::R_PPC64_TPREL16_HIGHERA:
4281         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4282           return;
4283
4284         default:
4285           break;
4286         }
4287     }
4288   else
4289     {
4290       switch (r_type)
4291         {
4292           // These are the relocation types supported only on 32-bit.
4293           // ??? glibc ld.so doesn't need to support these.
4294         case elfcpp::R_POWERPC_DTPREL16:
4295         case elfcpp::R_POWERPC_DTPREL16_LO:
4296         case elfcpp::R_POWERPC_DTPREL16_HI:
4297         case elfcpp::R_POWERPC_DTPREL16_HA:
4298           return;
4299
4300         default:
4301           break;
4302         }
4303     }
4304
4305   // This prevents us from issuing more than one error per reloc
4306   // section.  But we can still wind up issuing more than one
4307   // error per object file.
4308   if (this->issued_non_pic_error_)
4309     return;
4310   gold_assert(parameters->options().output_is_position_independent());
4311   object->error(_("requires unsupported dynamic reloc; "
4312                   "recompile with -fPIC"));
4313   this->issued_non_pic_error_ = true;
4314   return;
4315 }
4316
4317 // Return whether we need to make a PLT entry for a relocation of the
4318 // given type against a STT_GNU_IFUNC symbol.
4319
4320 template<int size, bool big_endian>
4321 bool
4322 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4323      Sized_relobj_file<size, big_endian>* object,
4324      unsigned int r_type)
4325 {
4326   // In non-pic code any reference will resolve to the plt call stub
4327   // for the ifunc symbol.
4328   if (size == 32 && !parameters->options().output_is_position_independent())
4329     return true;
4330
4331   switch (r_type)
4332     {
4333     // Word size refs from data sections are OK.
4334     case elfcpp::R_POWERPC_ADDR32:
4335     case elfcpp::R_POWERPC_UADDR32:
4336       if (size == 32)
4337         return true;
4338       break;
4339
4340     case elfcpp::R_PPC64_ADDR64:
4341     case elfcpp::R_PPC64_UADDR64:
4342       if (size == 64)
4343         return true;
4344       break;
4345
4346     // GOT refs are good.
4347     case elfcpp::R_POWERPC_GOT16:
4348     case elfcpp::R_POWERPC_GOT16_LO:
4349     case elfcpp::R_POWERPC_GOT16_HI:
4350     case elfcpp::R_POWERPC_GOT16_HA:
4351     case elfcpp::R_PPC64_GOT16_DS:
4352     case elfcpp::R_PPC64_GOT16_LO_DS:
4353       return true;
4354
4355     // So are function calls.
4356     case elfcpp::R_POWERPC_ADDR24:
4357     case elfcpp::R_POWERPC_ADDR14:
4358     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4359     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4360     case elfcpp::R_POWERPC_REL24:
4361     case elfcpp::R_PPC_PLTREL24:
4362     case elfcpp::R_POWERPC_REL14:
4363     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4364     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4365       return true;
4366
4367     default:
4368       break;
4369     }
4370
4371   // Anything else is a problem.
4372   // If we are building a static executable, the libc startup function
4373   // responsible for applying indirect function relocations is going
4374   // to complain about the reloc type.
4375   // If we are building a dynamic executable, we will have a text
4376   // relocation.  The dynamic loader will set the text segment
4377   // writable and non-executable to apply text relocations.  So we'll
4378   // segfault when trying to run the indirection function to resolve
4379   // the reloc.
4380   gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4381                object->name().c_str(), r_type);
4382   return false;
4383 }
4384
4385 // Scan a relocation for a local symbol.
4386
4387 template<int size, bool big_endian>
4388 inline void
4389 Target_powerpc<size, big_endian>::Scan::local(
4390     Symbol_table* symtab,
4391     Layout* layout,
4392     Target_powerpc<size, big_endian>* target,
4393     Sized_relobj_file<size, big_endian>* object,
4394     unsigned int data_shndx,
4395     Output_section* output_section,
4396     const elfcpp::Rela<size, big_endian>& reloc,
4397     unsigned int r_type,
4398     const elfcpp::Sym<size, big_endian>& lsym,
4399     bool is_discarded)
4400 {
4401   Powerpc_relobj<size, big_endian>* ppc_object
4402     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4403
4404   if (is_discarded)
4405     {
4406       if (size == 64
4407           && data_shndx == ppc_object->opd_shndx()
4408           && r_type == elfcpp::R_PPC64_ADDR64)
4409         ppc_object->set_opd_discard(reloc.get_r_offset());
4410       return;
4411     }
4412
4413   // A local STT_GNU_IFUNC symbol may require a PLT entry.
4414   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4415   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
4416     {
4417       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4418       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4419                           r_type, r_sym, reloc.get_r_addend());
4420       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4421     }
4422
4423   switch (r_type)
4424     {
4425     case elfcpp::R_POWERPC_NONE:
4426     case elfcpp::R_POWERPC_GNU_VTINHERIT:
4427     case elfcpp::R_POWERPC_GNU_VTENTRY:
4428     case elfcpp::R_PPC64_TOCSAVE:
4429     case elfcpp::R_PPC_EMB_MRKREF:
4430     case elfcpp::R_POWERPC_TLS:
4431       break;
4432
4433     case elfcpp::R_PPC64_TOC:
4434       {
4435         Output_data_got_powerpc<size, big_endian>* got
4436           = target->got_section(symtab, layout);
4437         if (parameters->options().output_is_position_independent())
4438           {
4439             Address off = reloc.get_r_offset();
4440             if (size == 64
4441                 && data_shndx == ppc_object->opd_shndx()
4442                 && ppc_object->get_opd_discard(off - 8))
4443               break;
4444
4445             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4446             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
4447             rela_dyn->add_output_section_relative(got->output_section(),
4448                                                   elfcpp::R_POWERPC_RELATIVE,
4449                                                   output_section,
4450                                                   object, data_shndx, off,
4451                                                   symobj->toc_base_offset());
4452           }
4453       }
4454       break;
4455
4456     case elfcpp::R_PPC64_ADDR64:
4457     case elfcpp::R_PPC64_UADDR64:
4458     case elfcpp::R_POWERPC_ADDR32:
4459     case elfcpp::R_POWERPC_UADDR32:
4460     case elfcpp::R_POWERPC_ADDR24:
4461     case elfcpp::R_POWERPC_ADDR16:
4462     case elfcpp::R_POWERPC_ADDR16_LO:
4463     case elfcpp::R_POWERPC_ADDR16_HI:
4464     case elfcpp::R_POWERPC_ADDR16_HA:
4465     case elfcpp::R_POWERPC_UADDR16:
4466     case elfcpp::R_PPC64_ADDR16_HIGHER:
4467     case elfcpp::R_PPC64_ADDR16_HIGHERA:
4468     case elfcpp::R_PPC64_ADDR16_HIGHEST:
4469     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4470     case elfcpp::R_PPC64_ADDR16_DS:
4471     case elfcpp::R_PPC64_ADDR16_LO_DS:
4472     case elfcpp::R_POWERPC_ADDR14:
4473     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4474     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4475       // If building a shared library (or a position-independent
4476       // executable), we need to create a dynamic relocation for
4477       // this location.
4478       if (parameters->options().output_is_position_independent()
4479           || (size == 64 && is_ifunc))
4480         {
4481           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4482
4483           if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
4484               || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
4485             {
4486               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4487               unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4488               if (is_ifunc)
4489                 {
4490                   rela_dyn = target->iplt_section()->rel_plt();
4491                   dynrel = elfcpp::R_POWERPC_IRELATIVE;
4492                 }
4493               rela_dyn->add_local_relative(object, r_sym, dynrel,
4494                                            output_section, data_shndx,
4495                                            reloc.get_r_offset(),
4496                                            reloc.get_r_addend(), false);
4497             }
4498           else
4499             {
4500               check_non_pic(object, r_type);
4501               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4502               rela_dyn->add_local(object, r_sym, r_type, output_section,
4503                                   data_shndx, reloc.get_r_offset(),
4504                                   reloc.get_r_addend());
4505             }
4506         }
4507       break;
4508
4509     case elfcpp::R_POWERPC_REL24:
4510     case elfcpp::R_PPC_PLTREL24:
4511     case elfcpp::R_PPC_LOCAL24PC:
4512       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4513                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4514                           reloc.get_r_addend());
4515       break;
4516
4517     case elfcpp::R_POWERPC_REL14:
4518     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4519     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4520       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4521                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4522                           reloc.get_r_addend());
4523       break;
4524
4525     case elfcpp::R_PPC64_REL64:
4526     case elfcpp::R_POWERPC_REL32:
4527     case elfcpp::R_POWERPC_REL16:
4528     case elfcpp::R_POWERPC_REL16_LO:
4529     case elfcpp::R_POWERPC_REL16_HI:
4530     case elfcpp::R_POWERPC_REL16_HA:
4531     case elfcpp::R_POWERPC_SECTOFF:
4532     case elfcpp::R_POWERPC_TPREL16:
4533     case elfcpp::R_POWERPC_DTPREL16:
4534     case elfcpp::R_POWERPC_SECTOFF_LO:
4535     case elfcpp::R_POWERPC_TPREL16_LO:
4536     case elfcpp::R_POWERPC_DTPREL16_LO:
4537     case elfcpp::R_POWERPC_SECTOFF_HI:
4538     case elfcpp::R_POWERPC_TPREL16_HI:
4539     case elfcpp::R_POWERPC_DTPREL16_HI:
4540     case elfcpp::R_POWERPC_SECTOFF_HA:
4541     case elfcpp::R_POWERPC_TPREL16_HA:
4542     case elfcpp::R_POWERPC_DTPREL16_HA:
4543     case elfcpp::R_PPC64_DTPREL16_HIGHER:
4544     case elfcpp::R_PPC64_TPREL16_HIGHER:
4545     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4546     case elfcpp::R_PPC64_TPREL16_HIGHERA:
4547     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4548     case elfcpp::R_PPC64_TPREL16_HIGHEST:
4549     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4550     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4551     case elfcpp::R_PPC64_TPREL16_DS:
4552     case elfcpp::R_PPC64_TPREL16_LO_DS:
4553     case elfcpp::R_PPC64_DTPREL16_DS:
4554     case elfcpp::R_PPC64_DTPREL16_LO_DS:
4555     case elfcpp::R_PPC64_SECTOFF_DS:
4556     case elfcpp::R_PPC64_SECTOFF_LO_DS:
4557     case elfcpp::R_PPC64_TLSGD:
4558     case elfcpp::R_PPC64_TLSLD:
4559       break;
4560
4561     case elfcpp::R_POWERPC_GOT16:
4562     case elfcpp::R_POWERPC_GOT16_LO:
4563     case elfcpp::R_POWERPC_GOT16_HI:
4564     case elfcpp::R_POWERPC_GOT16_HA:
4565     case elfcpp::R_PPC64_GOT16_DS:
4566     case elfcpp::R_PPC64_GOT16_LO_DS:
4567       {
4568         // The symbol requires a GOT entry.
4569         Output_data_got_powerpc<size, big_endian>* got
4570           = target->got_section(symtab, layout);
4571         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4572
4573         if (!parameters->options().output_is_position_independent())
4574           {
4575             if (size == 32 && is_ifunc)
4576               got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
4577             else
4578               got->add_local(object, r_sym, GOT_TYPE_STANDARD);
4579           }
4580         else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
4581           {
4582             // If we are generating a shared object or a pie, this
4583             // symbol's GOT entry will be set by a dynamic relocation.
4584             unsigned int off;
4585             off = got->add_constant(0);
4586             object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
4587
4588             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4589             unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4590             if (is_ifunc)
4591               {
4592                 rela_dyn = target->iplt_section()->rel_plt();
4593                 dynrel = elfcpp::R_POWERPC_IRELATIVE;
4594               }
4595             rela_dyn->add_local_relative(object, r_sym, dynrel,
4596                                          got, off, 0, false);
4597           }
4598       }
4599       break;
4600
4601     case elfcpp::R_PPC64_TOC16:
4602     case elfcpp::R_PPC64_TOC16_LO:
4603     case elfcpp::R_PPC64_TOC16_HI:
4604     case elfcpp::R_PPC64_TOC16_HA:
4605     case elfcpp::R_PPC64_TOC16_DS:
4606     case elfcpp::R_PPC64_TOC16_LO_DS:
4607       // We need a GOT section.
4608       target->got_section(symtab, layout);
4609       break;
4610
4611     case elfcpp::R_POWERPC_GOT_TLSGD16:
4612     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
4613     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
4614     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
4615       {
4616         const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
4617         if (tls_type == tls::TLSOPT_NONE)
4618           {
4619             Output_data_got_powerpc<size, big_endian>* got
4620               = target->got_section(symtab, layout);
4621             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4622             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4623             got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
4624                                     rela_dyn, elfcpp::R_POWERPC_DTPMOD);
4625           }
4626         else if (tls_type == tls::TLSOPT_TO_LE)
4627           {
4628             // no GOT relocs needed for Local Exec.
4629           }
4630         else
4631           gold_unreachable();
4632       }
4633       break;
4634
4635     case elfcpp::R_POWERPC_GOT_TLSLD16:
4636     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
4637     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
4638     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
4639       {
4640         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4641         if (tls_type == tls::TLSOPT_NONE)
4642           target->tlsld_got_offset(symtab, layout, object);
4643         else if (tls_type == tls::TLSOPT_TO_LE)
4644           {
4645             // no GOT relocs needed for Local Exec.
4646             if (parameters->options().emit_relocs())
4647               {
4648                 Output_section* os = layout->tls_segment()->first_section();
4649                 gold_assert(os != NULL);
4650                 os->set_needs_symtab_index();
4651               }
4652           }
4653         else
4654           gold_unreachable();
4655       }
4656       break;
4657
4658     case elfcpp::R_POWERPC_GOT_DTPREL16:
4659     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
4660     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
4661     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
4662       {
4663         Output_data_got_powerpc<size, big_endian>* got
4664           = target->got_section(symtab, layout);
4665         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4666         got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
4667       }
4668       break;
4669
4670     case elfcpp::R_POWERPC_GOT_TPREL16:
4671     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
4672     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
4673     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
4674       {
4675         const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
4676         if (tls_type == tls::TLSOPT_NONE)
4677           {
4678             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4679             if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
4680               {
4681                 Output_data_got_powerpc<size, big_endian>* got
4682                   = target->got_section(symtab, layout);
4683                 unsigned int off = got->add_constant(0);
4684                 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
4685
4686                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4687                 rela_dyn->add_symbolless_local_addend(object, r_sym,
4688                                                       elfcpp::R_POWERPC_TPREL,
4689                                                       got, off, 0);
4690               }
4691           }
4692         else if (tls_type == tls::TLSOPT_TO_LE)
4693           {
4694             // no GOT relocs needed for Local Exec.
4695           }
4696         else
4697           gold_unreachable();
4698       }
4699       break;
4700
4701     default:
4702       unsupported_reloc_local(object, r_type);
4703       break;
4704     }
4705
4706   switch (r_type)
4707     {
4708     case elfcpp::R_POWERPC_GOT_TLSLD16:
4709     case elfcpp::R_POWERPC_GOT_TLSGD16:
4710     case elfcpp::R_POWERPC_GOT_TPREL16:
4711     case elfcpp::R_POWERPC_GOT_DTPREL16:
4712     case elfcpp::R_POWERPC_GOT16:
4713     case elfcpp::R_PPC64_GOT16_DS:
4714     case elfcpp::R_PPC64_TOC16:
4715     case elfcpp::R_PPC64_TOC16_DS:
4716       ppc_object->set_has_small_toc_reloc();
4717     default:
4718       break;
4719     }
4720 }
4721
4722 // Report an unsupported relocation against a global symbol.
4723
4724 template<int size, bool big_endian>
4725 void
4726 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
4727     Sized_relobj_file<size, big_endian>* object,
4728     unsigned int r_type,
4729     Symbol* gsym)
4730 {
4731   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4732              object->name().c_str(), r_type, gsym->demangled_name().c_str());
4733 }
4734
4735 // Scan a relocation for a global symbol.
4736
4737 template<int size, bool big_endian>
4738 inline void
4739 Target_powerpc<size, big_endian>::Scan::global(
4740     Symbol_table* symtab,
4741     Layout* layout,
4742     Target_powerpc<size, big_endian>* target,
4743     Sized_relobj_file<size, big_endian>* object,
4744     unsigned int data_shndx,
4745     Output_section* output_section,
4746     const elfcpp::Rela<size, big_endian>& reloc,
4747     unsigned int r_type,
4748     Symbol* gsym)
4749 {
4750   Powerpc_relobj<size, big_endian>* ppc_object
4751     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4752
4753   // A STT_GNU_IFUNC symbol may require a PLT entry.
4754   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4755       && this->reloc_needs_plt_for_ifunc(object, r_type))
4756     {
4757       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4758                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4759                           reloc.get_r_addend());
4760       target->make_plt_entry(symtab, layout, gsym);
4761     }
4762
4763   switch (r_type)
4764     {
4765     case elfcpp::R_POWERPC_NONE:
4766     case elfcpp::R_POWERPC_GNU_VTINHERIT:
4767     case elfcpp::R_POWERPC_GNU_VTENTRY:
4768     case elfcpp::R_PPC_LOCAL24PC:
4769     case elfcpp::R_PPC_EMB_MRKREF:
4770     case elfcpp::R_POWERPC_TLS:
4771       break;
4772
4773     case elfcpp::R_PPC64_TOC:
4774       {
4775         Output_data_got_powerpc<size, big_endian>* got
4776           = target->got_section(symtab, layout);
4777         if (parameters->options().output_is_position_independent())
4778           {
4779             Address off = reloc.get_r_offset();
4780             if (size == 64
4781                 && data_shndx == ppc_object->opd_shndx()
4782                 && ppc_object->get_opd_discard(off - 8))
4783               break;
4784
4785             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4786             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
4787             if (data_shndx != ppc_object->opd_shndx())
4788               symobj = static_cast
4789                 <Powerpc_relobj<size, big_endian>*>(gsym->object());
4790             rela_dyn->add_output_section_relative(got->output_section(),
4791                                                   elfcpp::R_POWERPC_RELATIVE,
4792                                                   output_section,
4793                                                   object, data_shndx, off,
4794                                                   symobj->toc_base_offset());
4795           }
4796       }
4797       break;
4798
4799     case elfcpp::R_PPC64_ADDR64:
4800       if (size == 64
4801           && data_shndx == ppc_object->opd_shndx()
4802           && (gsym->is_defined_in_discarded_section()
4803               || gsym->object() != object))
4804         {
4805           ppc_object->set_opd_discard(reloc.get_r_offset());
4806           break;
4807         }
4808       // Fall thru
4809     case elfcpp::R_PPC64_UADDR64:
4810     case elfcpp::R_POWERPC_ADDR32:
4811     case elfcpp::R_POWERPC_UADDR32:
4812     case elfcpp::R_POWERPC_ADDR24:
4813     case elfcpp::R_POWERPC_ADDR16:
4814     case elfcpp::R_POWERPC_ADDR16_LO:
4815     case elfcpp::R_POWERPC_ADDR16_HI:
4816     case elfcpp::R_POWERPC_ADDR16_HA:
4817     case elfcpp::R_POWERPC_UADDR16:
4818     case elfcpp::R_PPC64_ADDR16_HIGHER:
4819     case elfcpp::R_PPC64_ADDR16_HIGHERA:
4820     case elfcpp::R_PPC64_ADDR16_HIGHEST:
4821     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4822     case elfcpp::R_PPC64_ADDR16_DS:
4823     case elfcpp::R_PPC64_ADDR16_LO_DS:
4824     case elfcpp::R_POWERPC_ADDR14:
4825     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4826     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4827       {
4828         // Make a PLT entry if necessary.
4829         if (gsym->needs_plt_entry())
4830           {
4831             target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4832                                 r_type,
4833                                 elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4834                                 reloc.get_r_addend());
4835             target->make_plt_entry(symtab, layout, gsym);
4836             // Since this is not a PC-relative relocation, we may be
4837             // taking the address of a function. In that case we need to
4838             // set the entry in the dynamic symbol table to the address of
4839             // the PLT call stub.
4840             if (size == 32
4841                 && gsym->is_from_dynobj()
4842                 && !parameters->options().output_is_position_independent())
4843               gsym->set_needs_dynsym_value();
4844           }
4845         // Make a dynamic relocation if necessary.
4846         if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
4847             || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
4848           {
4849             if (gsym->may_need_copy_reloc())
4850               {
4851                 target->copy_reloc(symtab, layout, object,
4852                                    data_shndx, output_section, gsym, reloc);
4853               }
4854             else if ((size == 32
4855                       && r_type == elfcpp::R_POWERPC_ADDR32
4856                       && gsym->can_use_relative_reloc(false)
4857                       && !(gsym->visibility() == elfcpp::STV_PROTECTED
4858                            && parameters->options().shared()))
4859                      || (size == 64
4860                          && r_type == elfcpp::R_PPC64_ADDR64
4861                          && (gsym->can_use_relative_reloc(false)
4862                              || data_shndx == ppc_object->opd_shndx())))
4863               {
4864                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4865                 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4866                 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4867                   {
4868                     rela_dyn = target->iplt_section()->rel_plt();
4869                     dynrel = elfcpp::R_POWERPC_IRELATIVE;
4870                   }
4871                 rela_dyn->add_symbolless_global_addend(
4872                     gsym, dynrel, output_section, object, data_shndx,
4873                     reloc.get_r_offset(), reloc.get_r_addend());
4874               }
4875             else
4876               {
4877                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4878                 check_non_pic(object, r_type);
4879                 rela_dyn->add_global(gsym, r_type, output_section,
4880                                      object, data_shndx,
4881                                      reloc.get_r_offset(),
4882                                      reloc.get_r_addend());
4883               }
4884           }
4885       }
4886       break;
4887
4888     case elfcpp::R_PPC_PLTREL24:
4889     case elfcpp::R_POWERPC_REL24:
4890       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4891                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4892                           reloc.get_r_addend());
4893       if (gsym->needs_plt_entry()
4894           || (!gsym->final_value_is_known()
4895               && (gsym->is_undefined()
4896                   || gsym->is_from_dynobj()
4897                   || gsym->is_preemptible())))
4898         target->make_plt_entry(symtab, layout, gsym);
4899       // Fall thru
4900
4901     case elfcpp::R_PPC64_REL64:
4902     case elfcpp::R_POWERPC_REL32:
4903       // Make a dynamic relocation if necessary.
4904       if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
4905         {
4906           if (gsym->may_need_copy_reloc())
4907             {
4908               target->copy_reloc(symtab, layout, object,
4909                                  data_shndx, output_section, gsym,
4910                                  reloc);
4911             }
4912           else
4913             {
4914               Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4915               check_non_pic(object, r_type);
4916               rela_dyn->add_global(gsym, r_type, output_section, object,
4917                                    data_shndx, reloc.get_r_offset(),
4918                                    reloc.get_r_addend());
4919             }
4920         }
4921       break;
4922
4923     case elfcpp::R_POWERPC_REL14:
4924     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4925     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4926       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4927                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
4928                           reloc.get_r_addend());
4929       break;
4930
4931     case elfcpp::R_POWERPC_REL16:
4932     case elfcpp::R_POWERPC_REL16_LO:
4933     case elfcpp::R_POWERPC_REL16_HI:
4934     case elfcpp::R_POWERPC_REL16_HA:
4935     case elfcpp::R_POWERPC_SECTOFF:
4936     case elfcpp::R_POWERPC_TPREL16:
4937     case elfcpp::R_POWERPC_DTPREL16:
4938     case elfcpp::R_POWERPC_SECTOFF_LO:
4939     case elfcpp::R_POWERPC_TPREL16_LO:
4940     case elfcpp::R_POWERPC_DTPREL16_LO:
4941     case elfcpp::R_POWERPC_SECTOFF_HI:
4942     case elfcpp::R_POWERPC_TPREL16_HI:
4943     case elfcpp::R_POWERPC_DTPREL16_HI:
4944     case elfcpp::R_POWERPC_SECTOFF_HA:
4945     case elfcpp::R_POWERPC_TPREL16_HA:
4946     case elfcpp::R_POWERPC_DTPREL16_HA:
4947     case elfcpp::R_PPC64_DTPREL16_HIGHER:
4948     case elfcpp::R_PPC64_TPREL16_HIGHER:
4949     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
4950     case elfcpp::R_PPC64_TPREL16_HIGHERA:
4951     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
4952     case elfcpp::R_PPC64_TPREL16_HIGHEST:
4953     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
4954     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4955     case elfcpp::R_PPC64_TPREL16_DS:
4956     case elfcpp::R_PPC64_TPREL16_LO_DS:
4957     case elfcpp::R_PPC64_DTPREL16_DS:
4958     case elfcpp::R_PPC64_DTPREL16_LO_DS:
4959     case elfcpp::R_PPC64_SECTOFF_DS:
4960     case elfcpp::R_PPC64_SECTOFF_LO_DS:
4961     case elfcpp::R_PPC64_TLSGD:
4962     case elfcpp::R_PPC64_TLSLD:
4963       break;
4964
4965     case elfcpp::R_POWERPC_GOT16:
4966     case elfcpp::R_POWERPC_GOT16_LO:
4967     case elfcpp::R_POWERPC_GOT16_HI:
4968     case elfcpp::R_POWERPC_GOT16_HA:
4969     case elfcpp::R_PPC64_GOT16_DS:
4970     case elfcpp::R_PPC64_GOT16_LO_DS:
4971       {
4972         // The symbol requires a GOT entry.
4973         Output_data_got_powerpc<size, big_endian>* got;
4974
4975         got = target->got_section(symtab, layout);
4976         if (gsym->final_value_is_known())
4977           {
4978             if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
4979               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
4980             else
4981               got->add_global(gsym, GOT_TYPE_STANDARD);
4982           }
4983         else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
4984           {
4985             // If we are generating a shared object or a pie, this
4986             // symbol's GOT entry will be set by a dynamic relocation.
4987             unsigned int off = got->add_constant(0);
4988             gsym->set_got_offset(GOT_TYPE_STANDARD, off);
4989
4990             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4991             if (gsym->can_use_relative_reloc(false)
4992                 && !(size == 32
4993                      && gsym->visibility() == elfcpp::STV_PROTECTED
4994                      && parameters->options().shared()))
4995               {
4996                 unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4997                 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
4998                   {
4999                     rela_dyn = target->iplt_section()->rel_plt();
5000                     dynrel = elfcpp::R_POWERPC_IRELATIVE;
5001                   }
5002                 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
5003               }
5004             else
5005               {
5006                 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5007                 rela_dyn->add_global(gsym, dynrel, got, off, 0);
5008               }
5009           }
5010       }
5011       break;
5012
5013     case elfcpp::R_PPC64_TOC16:
5014     case elfcpp::R_PPC64_TOC16_LO:
5015     case elfcpp::R_PPC64_TOC16_HI:
5016     case elfcpp::R_PPC64_TOC16_HA:
5017     case elfcpp::R_PPC64_TOC16_DS:
5018     case elfcpp::R_PPC64_TOC16_LO_DS:
5019       // We need a GOT section.
5020       target->got_section(symtab, layout);
5021       break;
5022
5023     case elfcpp::R_POWERPC_GOT_TLSGD16:
5024     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5025     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5026     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5027       {
5028         const bool final = gsym->final_value_is_known();
5029         const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5030         if (tls_type == tls::TLSOPT_NONE)
5031           {
5032             Output_data_got_powerpc<size, big_endian>* got
5033               = target->got_section(symtab, layout);
5034             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
5035                                           target->rela_dyn_section(layout),
5036                                           elfcpp::R_POWERPC_DTPMOD,
5037                                           elfcpp::R_POWERPC_DTPREL);
5038           }
5039         else if (tls_type == tls::TLSOPT_TO_IE)
5040           {
5041             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5042               {
5043                 Output_data_got_powerpc<size, big_endian>* got
5044                   = target->got_section(symtab, layout);
5045                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5046                 if (gsym->is_undefined()
5047                     || gsym->is_from_dynobj())
5048                   {
5049                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5050                                              elfcpp::R_POWERPC_TPREL);
5051                   }
5052                 else
5053                   {
5054                     unsigned int off = got->add_constant(0);
5055                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
5056                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5057                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5058                                                            got, off, 0);
5059                   }
5060               }
5061           }
5062         else if (tls_type == tls::TLSOPT_TO_LE)
5063           {
5064             // no GOT relocs needed for Local Exec.
5065           }
5066         else
5067           gold_unreachable();
5068       }
5069       break;
5070
5071     case elfcpp::R_POWERPC_GOT_TLSLD16:
5072     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5073     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5074     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5075       {
5076         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5077         if (tls_type == tls::TLSOPT_NONE)
5078           target->tlsld_got_offset(symtab, layout, object);
5079         else if (tls_type == tls::TLSOPT_TO_LE)
5080           {
5081             // no GOT relocs needed for Local Exec.
5082             if (parameters->options().emit_relocs())
5083               {
5084                 Output_section* os = layout->tls_segment()->first_section();
5085                 gold_assert(os != NULL);
5086                 os->set_needs_symtab_index();
5087               }
5088           }
5089         else
5090           gold_unreachable();
5091       }
5092       break;
5093
5094     case elfcpp::R_POWERPC_GOT_DTPREL16:
5095     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5096     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5097     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5098       {
5099         Output_data_got_powerpc<size, big_endian>* got
5100           = target->got_section(symtab, layout);
5101         if (!gsym->final_value_is_known()
5102             && (gsym->is_from_dynobj()
5103                 || gsym->is_undefined()
5104                 || gsym->is_preemptible()))
5105           got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
5106                                    target->rela_dyn_section(layout),
5107                                    elfcpp::R_POWERPC_DTPREL);
5108         else
5109           got->add_global_tls(gsym, GOT_TYPE_DTPREL);
5110       }
5111       break;
5112
5113     case elfcpp::R_POWERPC_GOT_TPREL16:
5114     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5115     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5116     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5117       {
5118         const bool final = gsym->final_value_is_known();
5119         const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5120         if (tls_type == tls::TLSOPT_NONE)
5121           {
5122             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5123               {
5124                 Output_data_got_powerpc<size, big_endian>* got
5125                   = target->got_section(symtab, layout);
5126                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5127                 if (gsym->is_undefined()
5128                     || gsym->is_from_dynobj())
5129                   {
5130                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5131                                              elfcpp::R_POWERPC_TPREL);
5132                   }
5133                 else
5134                   {
5135                     unsigned int off = got->add_constant(0);
5136                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
5137                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5138                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5139                                                            got, off, 0);
5140                   }
5141               }
5142           }
5143         else if (tls_type == tls::TLSOPT_TO_LE)
5144           {
5145             // no GOT relocs needed for Local Exec.
5146           }
5147         else
5148           gold_unreachable();
5149       }
5150       break;
5151
5152     default:
5153       unsupported_reloc_global(object, r_type, gsym);
5154       break;
5155     }
5156
5157   switch (r_type)
5158     {
5159     case elfcpp::R_POWERPC_GOT_TLSLD16:
5160     case elfcpp::R_POWERPC_GOT_TLSGD16:
5161     case elfcpp::R_POWERPC_GOT_TPREL16:
5162     case elfcpp::R_POWERPC_GOT_DTPREL16:
5163     case elfcpp::R_POWERPC_GOT16:
5164     case elfcpp::R_PPC64_GOT16_DS:
5165     case elfcpp::R_PPC64_TOC16:
5166     case elfcpp::R_PPC64_TOC16_DS:
5167       ppc_object->set_has_small_toc_reloc();
5168     default:
5169       break;
5170     }
5171 }
5172
5173 // Process relocations for gc.
5174
5175 template<int size, bool big_endian>
5176 void
5177 Target_powerpc<size, big_endian>::gc_process_relocs(
5178     Symbol_table* symtab,
5179     Layout* layout,
5180     Sized_relobj_file<size, big_endian>* object,
5181     unsigned int data_shndx,
5182     unsigned int,
5183     const unsigned char* prelocs,
5184     size_t reloc_count,
5185     Output_section* output_section,
5186     bool needs_special_offset_handling,
5187     size_t local_symbol_count,
5188     const unsigned char* plocal_symbols)
5189 {
5190   typedef Target_powerpc<size, big_endian> Powerpc;
5191   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5192   Powerpc_relobj<size, big_endian>* ppc_object
5193     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5194   if (size == 64)
5195     ppc_object->set_opd_valid();
5196   if (size == 64 && data_shndx == ppc_object->opd_shndx())
5197     {
5198       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
5199       for (p = ppc_object->access_from_map()->begin();
5200            p != ppc_object->access_from_map()->end();
5201            ++p)
5202         {
5203           Address dst_off = p->first;
5204           unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5205           typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
5206           for (s = p->second.begin(); s != p->second.end(); ++s)
5207             {
5208               Object* src_obj = s->first;
5209               unsigned int src_indx = s->second;
5210               symtab->gc()->add_reference(src_obj, src_indx,
5211                                           ppc_object, dst_indx);
5212             }
5213           p->second.clear();
5214         }
5215       ppc_object->access_from_map()->clear();
5216       ppc_object->process_gc_mark(symtab);
5217       // Don't look at .opd relocs as .opd will reference everything.
5218       return;
5219     }
5220
5221   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
5222                           typename Target_powerpc::Relocatable_size_for_reloc>(
5223     symtab,
5224     layout,
5225     this,
5226     object,
5227     data_shndx,
5228     prelocs,
5229     reloc_count,
5230     output_section,
5231     needs_special_offset_handling,
5232     local_symbol_count,
5233     plocal_symbols);
5234 }
5235
5236 // Handle target specific gc actions when adding a gc reference from
5237 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5238 // and DST_OFF.  For powerpc64, this adds a referenc to the code
5239 // section of a function descriptor.
5240
5241 template<int size, bool big_endian>
5242 void
5243 Target_powerpc<size, big_endian>::do_gc_add_reference(
5244     Symbol_table* symtab,
5245     Object* src_obj,
5246     unsigned int src_shndx,
5247     Object* dst_obj,
5248     unsigned int dst_shndx,
5249     Address dst_off) const
5250 {
5251   Powerpc_relobj<size, big_endian>* ppc_object
5252     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
5253   if (size == 64
5254       && !ppc_object->is_dynamic()
5255       && dst_shndx == ppc_object->opd_shndx())
5256     {
5257       if (ppc_object->opd_valid())
5258         {
5259           dst_shndx = ppc_object->get_opd_ent(dst_off);
5260           symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
5261         }
5262       else
5263         {
5264           // If we haven't run scan_opd_relocs, we must delay
5265           // processing this function descriptor reference.
5266           ppc_object->add_reference(src_obj, src_shndx, dst_off);
5267         }
5268     }
5269 }
5270
5271 // Add any special sections for this symbol to the gc work list.
5272 // For powerpc64, this adds the code section of a function
5273 // descriptor.
5274
5275 template<int size, bool big_endian>
5276 void
5277 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
5278     Symbol_table* symtab,
5279     Symbol* sym) const
5280 {
5281   if (size == 64)
5282     {
5283       Powerpc_relobj<size, big_endian>* ppc_object
5284         = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
5285       bool is_ordinary;
5286       unsigned int shndx = sym->shndx(&is_ordinary);
5287       if (is_ordinary && shndx == ppc_object->opd_shndx())
5288         {
5289           Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
5290           Address dst_off = gsym->value();
5291           if (ppc_object->opd_valid())
5292             {
5293               unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5294               symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
5295             }
5296           else
5297             ppc_object->add_gc_mark(dst_off);
5298         }
5299     }
5300 }
5301
5302 // Scan relocations for a section.
5303
5304 template<int size, bool big_endian>
5305 void
5306 Target_powerpc<size, big_endian>::scan_relocs(
5307     Symbol_table* symtab,
5308     Layout* layout,
5309     Sized_relobj_file<size, big_endian>* object,
5310     unsigned int data_shndx,
5311     unsigned int sh_type,
5312     const unsigned char* prelocs,
5313     size_t reloc_count,
5314     Output_section* output_section,
5315     bool needs_special_offset_handling,
5316     size_t local_symbol_count,
5317     const unsigned char* plocal_symbols)
5318 {
5319   typedef Target_powerpc<size, big_endian> Powerpc;
5320   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5321
5322   if (sh_type == elfcpp::SHT_REL)
5323     {
5324       gold_error(_("%s: unsupported REL reloc section"),
5325                  object->name().c_str());
5326       return;
5327     }
5328
5329   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
5330     symtab,
5331     layout,
5332     this,
5333     object,
5334     data_shndx,
5335     prelocs,
5336     reloc_count,
5337     output_section,
5338     needs_special_offset_handling,
5339     local_symbol_count,
5340     plocal_symbols);
5341 }
5342
5343 // Functor class for processing the global symbol table.
5344 // Removes symbols defined on discarded opd entries.
5345
5346 template<bool big_endian>
5347 class Global_symbol_visitor_opd
5348 {
5349  public:
5350   Global_symbol_visitor_opd()
5351   { }
5352
5353   void
5354   operator()(Sized_symbol<64>* sym)
5355   {
5356     if (sym->has_symtab_index()
5357         || sym->source() != Symbol::FROM_OBJECT
5358         || !sym->in_real_elf())
5359       return;
5360
5361     Powerpc_relobj<64, big_endian>* symobj
5362       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
5363     if (symobj->is_dynamic()
5364         || symobj->opd_shndx() == 0)
5365       return;
5366
5367     bool is_ordinary;
5368     unsigned int shndx = sym->shndx(&is_ordinary);
5369     if (shndx == symobj->opd_shndx()
5370         && symobj->get_opd_discard(sym->value()))
5371       sym->set_symtab_index(-1U);
5372   }
5373 };
5374
5375 template<int size, bool big_endian>
5376 void
5377 Target_powerpc<size, big_endian>::define_save_restore_funcs(
5378     Layout* layout,
5379     Symbol_table* symtab)
5380 {
5381   if (size == 64)
5382     {
5383       Output_data_save_res<64, big_endian>* savres
5384         = new Output_data_save_res<64, big_endian>(symtab);
5385       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5386                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5387                                       savres, ORDER_TEXT, false);
5388     }
5389 }
5390
5391 // Sort linker created .got section first (for the header), then input
5392 // sections belonging to files using small model code.
5393
5394 template<bool big_endian>
5395 class Sort_toc_sections
5396 {
5397  public:
5398   bool
5399   operator()(const Output_section::Input_section& is1,
5400              const Output_section::Input_section& is2) const
5401   {
5402     if (!is1.is_input_section() && is2.is_input_section())
5403       return true;
5404     bool small1
5405       = (is1.is_input_section()
5406          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
5407              ->has_small_toc_reloc()));
5408     bool small2
5409       = (is2.is_input_section()
5410          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
5411              ->has_small_toc_reloc()));
5412     return small1 && !small2;
5413   }
5414 };
5415
5416 // Finalize the sections.
5417
5418 template<int size, bool big_endian>
5419 void
5420 Target_powerpc<size, big_endian>::do_finalize_sections(
5421     Layout* layout,
5422     const Input_objects*,
5423     Symbol_table* symtab)
5424 {
5425   if (parameters->doing_static_link())
5426     {
5427       // At least some versions of glibc elf-init.o have a strong
5428       // reference to __rela_iplt marker syms.  A weak ref would be
5429       // better..
5430       if (this->iplt_ != NULL)
5431         {
5432           Reloc_section* rel = this->iplt_->rel_plt();
5433           symtab->define_in_output_data("__rela_iplt_start", NULL,
5434                                         Symbol_table::PREDEFINED, rel, 0, 0,
5435                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5436                                         elfcpp::STV_HIDDEN, 0, false, true);
5437           symtab->define_in_output_data("__rela_iplt_end", NULL,
5438                                         Symbol_table::PREDEFINED, rel, 0, 0,
5439                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5440                                         elfcpp::STV_HIDDEN, 0, true, true);
5441         }
5442       else
5443         {
5444           symtab->define_as_constant("__rela_iplt_start", NULL,
5445                                      Symbol_table::PREDEFINED, 0, 0,
5446                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5447                                      elfcpp::STV_HIDDEN, 0, true, false);
5448           symtab->define_as_constant("__rela_iplt_end", NULL,
5449                                      Symbol_table::PREDEFINED, 0, 0,
5450                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
5451                                      elfcpp::STV_HIDDEN, 0, true, false);
5452         }
5453     }
5454
5455   if (size == 64)
5456     {
5457       typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
5458       symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
5459
5460       if (!parameters->options().relocatable())
5461         {
5462           this->define_save_restore_funcs(layout, symtab);
5463
5464           // Annoyingly, we need to make these sections now whether or
5465           // not we need them.  If we delay until do_relax then we
5466           // need to mess with the relaxation machinery checkpointing.
5467           this->got_section(symtab, layout);
5468           this->make_brlt_section(layout);
5469
5470           if (parameters->options().toc_sort())
5471             {
5472               Output_section* os = this->got_->output_section();
5473               if (os != NULL && os->input_sections().size() > 1)
5474                 std::stable_sort(os->input_sections().begin(),
5475                                  os->input_sections().end(),
5476                                  Sort_toc_sections<big_endian>());
5477             }
5478         }
5479     }
5480
5481   // Fill in some more dynamic tags.
5482   Output_data_dynamic* odyn = layout->dynamic_data();
5483   if (odyn != NULL)
5484     {
5485       const Reloc_section* rel_plt = (this->plt_ == NULL
5486                                       ? NULL
5487                                       : this->plt_->rel_plt());
5488       layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
5489                                       this->rela_dyn_, true, size == 32);
5490
5491       if (size == 32)
5492         {
5493           if (this->got_ != NULL)
5494             {
5495               this->got_->finalize_data_size();
5496               odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
5497                                             this->got_, this->got_->g_o_t());
5498             }
5499         }
5500       else
5501         {
5502           if (this->glink_ != NULL)
5503             {
5504               this->glink_->finalize_data_size();
5505               odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
5506                                             this->glink_,
5507                                             (this->glink_->pltresolve_size
5508                                              - 32));
5509             }
5510         }
5511     }
5512
5513   // Emit any relocs we saved in an attempt to avoid generating COPY
5514   // relocs.
5515   if (this->copy_relocs_.any_saved_relocs())
5516     this->copy_relocs_.emit(this->rela_dyn_section(layout));
5517 }
5518
5519 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
5520 // reloc.
5521
5522 static bool
5523 ok_lo_toc_insn(uint32_t insn)
5524 {
5525   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
5526           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
5527           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
5528           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
5529           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
5530           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
5531           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
5532           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
5533           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
5534           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
5535           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
5536           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
5537           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
5538           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
5539           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
5540               && (insn & 3) != 1)
5541           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
5542               && ((insn & 3) == 0 || (insn & 3) == 3))
5543           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
5544 }
5545
5546 // Return the value to use for a branch relocation.
5547
5548 template<int size, bool big_endian>
5549 typename elfcpp::Elf_types<size>::Elf_Addr
5550 Target_powerpc<size, big_endian>::symval_for_branch(
5551     Address value,
5552     const Sized_symbol<size>* gsym,
5553     Powerpc_relobj<size, big_endian>* object,
5554     unsigned int *dest_shndx)
5555 {
5556   *dest_shndx = 0;
5557   if (size == 32)
5558     return value;
5559
5560   // If the symbol is defined in an opd section, ie. is a function
5561   // descriptor, use the function descriptor code entry address
5562   Powerpc_relobj<size, big_endian>* symobj = object;
5563   if (gsym != NULL
5564       && gsym->source() != Symbol::FROM_OBJECT)
5565     return value;
5566   if (gsym != NULL)
5567     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
5568   unsigned int shndx = symobj->opd_shndx();
5569   if (shndx == 0)
5570     return value;
5571   Address opd_addr = symobj->get_output_section_offset(shndx);
5572   gold_assert(opd_addr != invalid_address);
5573   opd_addr += symobj->output_section(shndx)->address();
5574   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
5575     {
5576       Address sec_off;
5577       *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
5578       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
5579       gold_assert(sec_addr != invalid_address);
5580       sec_addr += symobj->output_section(*dest_shndx)->address();
5581       value = sec_addr + sec_off;
5582     }
5583   return value;
5584 }
5585
5586 // Perform a relocation.
5587
5588 template<int size, bool big_endian>
5589 inline bool
5590 Target_powerpc<size, big_endian>::Relocate::relocate(
5591     const Relocate_info<size, big_endian>* relinfo,
5592     Target_powerpc* target,
5593     Output_section* os,
5594     size_t relnum,
5595     const elfcpp::Rela<size, big_endian>& rela,
5596     unsigned int r_type,
5597     const Sized_symbol<size>* gsym,
5598     const Symbol_value<size>* psymval,
5599     unsigned char* view,
5600     Address address,
5601     section_size_type view_size)
5602 {
5603   bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
5604                        || r_type == elfcpp::R_PPC_PLTREL24)
5605                       && gsym != NULL
5606                       && strcmp(gsym->name(), "__tls_get_addr") == 0);
5607   enum skip_tls last_tls = this->call_tls_get_addr_;
5608   this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
5609   if (is_tls_call)
5610     {
5611       if (last_tls == CALL_NOT_EXPECTED)
5612         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5613                                _("__tls_get_addr call lacks marker reloc"));
5614       else if (last_tls == CALL_SKIP)
5615         return false;
5616     }
5617   else if (last_tls != CALL_NOT_EXPECTED)
5618     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5619                            _("missing expected __tls_get_addr call"));
5620
5621   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
5622   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
5623   Powerpc_relobj<size, big_endian>* const object
5624     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
5625   Address value = 0;
5626   bool has_plt_value = false;
5627   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5628   if (gsym != NULL
5629       ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
5630       : object->local_has_plt_offset(r_sym))
5631     {
5632       Stub_table<size, big_endian>* stub_table
5633         = object->stub_table(relinfo->data_shndx);
5634       if (stub_table == NULL)
5635         {
5636           // This is a ref from a data section to an ifunc symbol.
5637           if (target->stub_tables().size() != 0)
5638             stub_table = target->stub_tables()[0];
5639         }
5640       gold_assert(stub_table != NULL);
5641       Address off;
5642       if (gsym != NULL)
5643         off = stub_table->find_plt_call_entry(object, gsym, r_type,
5644                                               rela.get_r_addend());
5645       else
5646         off = stub_table->find_plt_call_entry(object, r_sym, r_type,
5647                                               rela.get_r_addend());
5648       gold_assert(off != invalid_address);
5649       value = stub_table->stub_address() + off;
5650       has_plt_value = true;
5651     }
5652
5653   if (r_type == elfcpp::R_POWERPC_GOT16
5654       || r_type == elfcpp::R_POWERPC_GOT16_LO
5655       || r_type == elfcpp::R_POWERPC_GOT16_HI
5656       || r_type == elfcpp::R_POWERPC_GOT16_HA
5657       || r_type == elfcpp::R_PPC64_GOT16_DS
5658       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
5659     {
5660       if (gsym != NULL)
5661         {
5662           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
5663           value = gsym->got_offset(GOT_TYPE_STANDARD);
5664         }
5665       else
5666         {
5667           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5668           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
5669           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
5670         }
5671       value -= target->got_section()->got_base_offset(object);
5672     }
5673   else if (r_type == elfcpp::R_PPC64_TOC)
5674     {
5675       value = (target->got_section()->output_section()->address()
5676                + object->toc_base_offset());
5677     }
5678   else if (gsym != NULL
5679            && (r_type == elfcpp::R_POWERPC_REL24
5680                || r_type == elfcpp::R_PPC_PLTREL24)
5681            && has_plt_value)
5682     {
5683       if (size == 64)
5684         {
5685           typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
5686           Valtype* wv = reinterpret_cast<Valtype*>(view);
5687           bool can_plt_call = false;
5688           if (rela.get_r_offset() + 8 <= view_size)
5689             {
5690               Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
5691               Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
5692               if ((insn & 1) != 0
5693                   && (insn2 == nop
5694                       || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
5695                 {
5696                   elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
5697                   can_plt_call = true;
5698                 }
5699             }
5700           if (!can_plt_call)
5701             {
5702               // If we don't have a branch and link followed by a nop,
5703               // we can't go via the plt because there is no place to
5704               // put a toc restoring instruction.
5705               // Unless we know we won't be returning.
5706               if (strcmp(gsym->name(), "__libc_start_main") == 0)
5707                 can_plt_call = true;
5708             }
5709           if (!can_plt_call)
5710             {
5711               // This is not an error in one special case: A self
5712               // call.  It isn't possible to cheaply verify we have
5713               // such a call so just check for a call to the same
5714               // section.
5715               bool ok = false;
5716               Address code = value;
5717               if (gsym->source() == Symbol::FROM_OBJECT
5718                   && gsym->object() == object)
5719                 {
5720                   Address addend = rela.get_r_addend();
5721                   unsigned int dest_shndx;
5722                   Address opdent = psymval->value(object, addend);
5723                   code = target->symval_for_branch(opdent, gsym, object,
5724                                                    &dest_shndx);
5725                   bool is_ordinary;
5726                   if (dest_shndx == 0)
5727                     dest_shndx = gsym->shndx(&is_ordinary);
5728                   ok = dest_shndx == relinfo->data_shndx;
5729                 }
5730               if (!ok)
5731                 {
5732                   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
5733                                          _("call lacks nop, can't restore toc; "
5734                                            "recompile with -fPIC"));
5735                   value = code;
5736                 }
5737             }
5738         }
5739     }
5740   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5741            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
5742            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
5743            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
5744     {
5745       // First instruction of a global dynamic sequence, arg setup insn.
5746       const bool final = gsym == NULL || gsym->final_value_is_known();
5747       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5748       enum Got_type got_type = GOT_TYPE_STANDARD;
5749       if (tls_type == tls::TLSOPT_NONE)
5750         got_type = GOT_TYPE_TLSGD;
5751       else if (tls_type == tls::TLSOPT_TO_IE)
5752         got_type = GOT_TYPE_TPREL;
5753       if (got_type != GOT_TYPE_STANDARD)
5754         {
5755           if (gsym != NULL)
5756             {
5757               gold_assert(gsym->has_got_offset(got_type));
5758               value = gsym->got_offset(got_type);
5759             }
5760           else
5761             {
5762               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5763               gold_assert(object->local_has_got_offset(r_sym, got_type));
5764               value = object->local_got_offset(r_sym, got_type);
5765             }
5766           value -= target->got_section()->got_base_offset(object);
5767         }
5768       if (tls_type == tls::TLSOPT_TO_IE)
5769         {
5770           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5771               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5772             {
5773               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5774               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5775               insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
5776               if (size == 32)
5777                 insn |= 32 << 26; // lwz
5778               else
5779                 insn |= 58 << 26; // ld
5780               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5781             }
5782           r_type += (elfcpp::R_POWERPC_GOT_TPREL16
5783                      - elfcpp::R_POWERPC_GOT_TLSGD16);
5784         }
5785       else if (tls_type == tls::TLSOPT_TO_LE)
5786         {
5787           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
5788               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
5789             {
5790               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5791               Insn insn = addis_3_13;
5792               if (size == 32)
5793                 insn = addis_3_2;
5794               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5795               r_type = elfcpp::R_POWERPC_TPREL16_HA;
5796               value = psymval->value(object, rela.get_r_addend());
5797             }
5798           else
5799             {
5800               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5801               Insn insn = nop;
5802               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5803               r_type = elfcpp::R_POWERPC_NONE;
5804             }
5805         }
5806     }
5807   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5808            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
5809            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
5810            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
5811     {
5812       // First instruction of a local dynamic sequence, arg setup insn.
5813       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5814       if (tls_type == tls::TLSOPT_NONE)
5815         {
5816           value = target->tlsld_got_offset();
5817           value -= target->got_section()->got_base_offset(object);
5818         }
5819       else
5820         {
5821           gold_assert(tls_type == tls::TLSOPT_TO_LE);
5822           if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
5823               || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
5824             {
5825               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5826               Insn insn = addis_3_13;
5827               if (size == 32)
5828                 insn = addis_3_2;
5829               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5830               r_type = elfcpp::R_POWERPC_TPREL16_HA;
5831               value = dtp_offset;
5832             }
5833           else
5834             {
5835               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5836               Insn insn = nop;
5837               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5838               r_type = elfcpp::R_POWERPC_NONE;
5839             }
5840         }
5841     }
5842   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
5843            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
5844            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
5845            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
5846     {
5847       // Accesses relative to a local dynamic sequence address,
5848       // no optimisation here.
5849       if (gsym != NULL)
5850         {
5851           gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
5852           value = gsym->got_offset(GOT_TYPE_DTPREL);
5853         }
5854       else
5855         {
5856           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5857           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
5858           value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
5859         }
5860       value -= target->got_section()->got_base_offset(object);
5861     }
5862   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5863            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
5864            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
5865            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
5866     {
5867       // First instruction of initial exec sequence.
5868       const bool final = gsym == NULL || gsym->final_value_is_known();
5869       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5870       if (tls_type == tls::TLSOPT_NONE)
5871         {
5872           if (gsym != NULL)
5873             {
5874               gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
5875               value = gsym->got_offset(GOT_TYPE_TPREL);
5876             }
5877           else
5878             {
5879               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
5880               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
5881               value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
5882             }
5883           value -= target->got_section()->got_base_offset(object);
5884         }
5885       else
5886         {
5887           gold_assert(tls_type == tls::TLSOPT_TO_LE);
5888           if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
5889               || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
5890             {
5891               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5892               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5893               insn &= (1 << 26) - (1 << 21); // extract rt from ld
5894               if (size == 32)
5895                 insn |= addis_0_2;
5896               else
5897                 insn |= addis_0_13;
5898               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5899               r_type = elfcpp::R_POWERPC_TPREL16_HA;
5900               value = psymval->value(object, rela.get_r_addend());
5901             }
5902           else
5903             {
5904               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
5905               Insn insn = nop;
5906               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5907               r_type = elfcpp::R_POWERPC_NONE;
5908             }
5909         }
5910     }
5911   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5912            || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5913     {
5914       // Second instruction of a global dynamic sequence,
5915       // the __tls_get_addr call
5916       this->call_tls_get_addr_ = CALL_EXPECTED;
5917       const bool final = gsym == NULL || gsym->final_value_is_known();
5918       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5919       if (tls_type != tls::TLSOPT_NONE)
5920         {
5921           if (tls_type == tls::TLSOPT_TO_IE)
5922             {
5923               Insn* iview = reinterpret_cast<Insn*>(view);
5924               Insn insn = add_3_3_13;
5925               if (size == 32)
5926                 insn = add_3_3_2;
5927               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5928               r_type = elfcpp::R_POWERPC_NONE;
5929             }
5930           else
5931             {
5932               Insn* iview = reinterpret_cast<Insn*>(view);
5933               Insn insn = addi_3_3;
5934               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5935               r_type = elfcpp::R_POWERPC_TPREL16_LO;
5936               view += 2 * big_endian;
5937               value = psymval->value(object, rela.get_r_addend());
5938             }
5939           this->call_tls_get_addr_ = CALL_SKIP;
5940         }
5941     }
5942   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5943            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5944     {
5945       // Second instruction of a local dynamic sequence,
5946       // the __tls_get_addr call
5947       this->call_tls_get_addr_ = CALL_EXPECTED;
5948       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5949       if (tls_type == tls::TLSOPT_TO_LE)
5950         {
5951           Insn* iview = reinterpret_cast<Insn*>(view);
5952           Insn insn = addi_3_3;
5953           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5954           this->call_tls_get_addr_ = CALL_SKIP;
5955           r_type = elfcpp::R_POWERPC_TPREL16_LO;
5956           view += 2 * big_endian;
5957           value = dtp_offset;
5958         }
5959     }
5960   else if (r_type == elfcpp::R_POWERPC_TLS)
5961     {
5962       // Second instruction of an initial exec sequence
5963       const bool final = gsym == NULL || gsym->final_value_is_known();
5964       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5965       if (tls_type == tls::TLSOPT_TO_LE)
5966         {
5967           Insn* iview = reinterpret_cast<Insn*>(view);
5968           Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
5969           unsigned int reg = size == 32 ? 2 : 13;
5970           insn = at_tls_transform(insn, reg);
5971           gold_assert(insn != 0);
5972           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
5973           r_type = elfcpp::R_POWERPC_TPREL16_LO;
5974           view += 2 * big_endian;
5975           value = psymval->value(object, rela.get_r_addend());
5976         }
5977     }
5978   else if (!has_plt_value)
5979     {
5980       Address addend = 0;
5981       unsigned int dest_shndx;
5982       if (r_type != elfcpp::R_PPC_PLTREL24)
5983         addend = rela.get_r_addend();
5984       value = psymval->value(object, addend);
5985       if (size == 64 && is_branch_reloc(r_type))
5986         value = target->symval_for_branch(value, gsym, object, &dest_shndx);
5987       unsigned int max_branch_offset = 0;
5988       if (r_type == elfcpp::R_POWERPC_REL24
5989           || r_type == elfcpp::R_PPC_PLTREL24
5990           || r_type == elfcpp::R_PPC_LOCAL24PC)
5991         max_branch_offset = 1 << 25;
5992       else if (r_type == elfcpp::R_POWERPC_REL14
5993                || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
5994                || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
5995         max_branch_offset = 1 << 15;
5996       if (max_branch_offset != 0
5997           && value - address + max_branch_offset >= 2 * max_branch_offset)
5998         {
5999           Stub_table<size, big_endian>* stub_table
6000             = object->stub_table(relinfo->data_shndx);
6001           gold_assert(stub_table != NULL);
6002           Address off = stub_table->find_long_branch_entry(object, value);
6003           if (off != invalid_address)
6004             value = stub_table->stub_address() + stub_table->plt_size() + off;
6005         }
6006     }
6007
6008   switch (r_type)
6009     {
6010     case elfcpp::R_PPC64_REL64:
6011     case elfcpp::R_POWERPC_REL32:
6012     case elfcpp::R_POWERPC_REL24:
6013     case elfcpp::R_PPC_PLTREL24:
6014     case elfcpp::R_PPC_LOCAL24PC:
6015     case elfcpp::R_POWERPC_REL16:
6016     case elfcpp::R_POWERPC_REL16_LO:
6017     case elfcpp::R_POWERPC_REL16_HI:
6018     case elfcpp::R_POWERPC_REL16_HA:
6019     case elfcpp::R_POWERPC_REL14:
6020     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6021     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6022       value -= address;
6023       break;
6024
6025     case elfcpp::R_PPC64_TOC16:
6026     case elfcpp::R_PPC64_TOC16_LO:
6027     case elfcpp::R_PPC64_TOC16_HI:
6028     case elfcpp::R_PPC64_TOC16_HA:
6029     case elfcpp::R_PPC64_TOC16_DS:
6030     case elfcpp::R_PPC64_TOC16_LO_DS:
6031       // Subtract the TOC base address.
6032       value -= (target->got_section()->output_section()->address()
6033                 + object->toc_base_offset());
6034       break;
6035
6036     case elfcpp::R_POWERPC_SECTOFF:
6037     case elfcpp::R_POWERPC_SECTOFF_LO:
6038     case elfcpp::R_POWERPC_SECTOFF_HI:
6039     case elfcpp::R_POWERPC_SECTOFF_HA:
6040     case elfcpp::R_PPC64_SECTOFF_DS:
6041     case elfcpp::R_PPC64_SECTOFF_LO_DS:
6042       if (os != NULL)
6043         value -= os->address();
6044       break;
6045
6046     case elfcpp::R_PPC64_TPREL16_DS:
6047     case elfcpp::R_PPC64_TPREL16_LO_DS:
6048       if (size != 64)
6049         // R_PPC_TLSGD and R_PPC_TLSLD
6050         break;
6051     case elfcpp::R_POWERPC_TPREL16:
6052     case elfcpp::R_POWERPC_TPREL16_LO:
6053     case elfcpp::R_POWERPC_TPREL16_HI:
6054     case elfcpp::R_POWERPC_TPREL16_HA:
6055     case elfcpp::R_POWERPC_TPREL:
6056     case elfcpp::R_PPC64_TPREL16_HIGHER:
6057     case elfcpp::R_PPC64_TPREL16_HIGHERA:
6058     case elfcpp::R_PPC64_TPREL16_HIGHEST:
6059     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6060       // tls symbol values are relative to tls_segment()->vaddr()
6061       value -= tp_offset;
6062       break;
6063
6064     case elfcpp::R_PPC64_DTPREL16_DS:
6065     case elfcpp::R_PPC64_DTPREL16_LO_DS:
6066     case elfcpp::R_PPC64_DTPREL16_HIGHER:
6067     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6068     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6069     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6070       if (size != 64)
6071         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6072         // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6073         break;
6074     case elfcpp::R_POWERPC_DTPREL16:
6075     case elfcpp::R_POWERPC_DTPREL16_LO:
6076     case elfcpp::R_POWERPC_DTPREL16_HI:
6077     case elfcpp::R_POWERPC_DTPREL16_HA:
6078     case elfcpp::R_POWERPC_DTPREL:
6079       // tls symbol values are relative to tls_segment()->vaddr()
6080       value -= dtp_offset;
6081       break;
6082
6083     default:
6084       break;
6085     }
6086
6087   Insn branch_bit = 0;
6088   switch (r_type)
6089     {
6090     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6091     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6092       branch_bit = 1 << 21;
6093     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6094     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6095       {
6096         Insn* iview = reinterpret_cast<Insn*>(view);
6097         Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6098         insn &= ~(1 << 21);
6099         insn |= branch_bit;
6100         if (this->is_isa_v2)
6101           {
6102             // Set 'a' bit.  This is 0b00010 in BO field for branch
6103             // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6104             // for branch on CTR insns (BO == 1a00t or 1a01t).
6105             if ((insn & (0x14 << 21)) == (0x04 << 21))
6106               insn |= 0x02 << 21;
6107             else if ((insn & (0x14 << 21)) == (0x10 << 21))
6108               insn |= 0x08 << 21;
6109             else
6110               break;
6111           }
6112         else
6113           {
6114             // Invert 'y' bit if not the default.
6115             if (static_cast<Signed_address>(value) < 0)
6116               insn ^= 1 << 21;
6117           }
6118         elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6119       }
6120       break;
6121
6122     default:
6123       break;
6124     }
6125
6126   if (size == 64)
6127     {
6128       // Multi-instruction sequences that access the TOC can be
6129       // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6130       // to             nop;           addi rb,r2,x;
6131       switch (r_type)
6132         {
6133         default:
6134           break;
6135
6136         case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6137         case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6138         case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6139         case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6140         case elfcpp::R_POWERPC_GOT16_HA:
6141         case elfcpp::R_PPC64_TOC16_HA:
6142           if (parameters->options().toc_optimize())
6143             {
6144               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6145               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6146               if ((insn & ((0x3f << 26) | 0x1f << 16))
6147                   != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6148                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6149                                        _("toc optimization is not supported "
6150                                          "for %#08x instruction"), insn);
6151               else if (value + 0x8000 < 0x10000)
6152                 {
6153                   elfcpp::Swap<32, big_endian>::writeval(iview, nop);
6154                   return true;
6155                 }
6156             }
6157           break;
6158
6159         case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6160         case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6161         case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6162         case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6163         case elfcpp::R_POWERPC_GOT16_LO:
6164         case elfcpp::R_PPC64_GOT16_LO_DS:
6165         case elfcpp::R_PPC64_TOC16_LO:
6166         case elfcpp::R_PPC64_TOC16_LO_DS:
6167           if (parameters->options().toc_optimize())
6168             {
6169               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6170               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6171               if (!ok_lo_toc_insn(insn))
6172                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6173                                        _("toc optimization is not supported "
6174                                          "for %#08x instruction"), insn);
6175               else if (value + 0x8000 < 0x10000)
6176                 {
6177                   if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
6178                     {
6179                       // Transform addic to addi when we change reg.
6180                       insn &= ~((0x3f << 26) | (0x1f << 16));
6181                       insn |= (14u << 26) | (2 << 16);
6182                     }
6183                   else
6184                     {
6185                       insn &= ~(0x1f << 16);
6186                       insn |= 2 << 16;
6187                     }
6188                   elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6189                 }
6190             }
6191           break;
6192         }
6193     }
6194
6195   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
6196   switch (r_type)
6197     {
6198     case elfcpp::R_POWERPC_ADDR32:
6199     case elfcpp::R_POWERPC_UADDR32:
6200       if (size == 64)
6201         overflow = Reloc::CHECK_BITFIELD;
6202       break;
6203
6204     case elfcpp::R_POWERPC_REL32:
6205       if (size == 64)
6206         overflow = Reloc::CHECK_SIGNED;
6207       break;
6208
6209     case elfcpp::R_POWERPC_ADDR24:
6210     case elfcpp::R_POWERPC_ADDR16:
6211     case elfcpp::R_POWERPC_UADDR16:
6212     case elfcpp::R_PPC64_ADDR16_DS:
6213     case elfcpp::R_POWERPC_ADDR14:
6214     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6215     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6216       overflow = Reloc::CHECK_BITFIELD;
6217       break;
6218
6219     case elfcpp::R_POWERPC_REL24:
6220     case elfcpp::R_PPC_PLTREL24:
6221     case elfcpp::R_PPC_LOCAL24PC:
6222     case elfcpp::R_POWERPC_REL16:
6223     case elfcpp::R_PPC64_TOC16:
6224     case elfcpp::R_POWERPC_GOT16:
6225     case elfcpp::R_POWERPC_SECTOFF:
6226     case elfcpp::R_POWERPC_TPREL16:
6227     case elfcpp::R_POWERPC_DTPREL16:
6228     case elfcpp::R_PPC64_TPREL16_DS:
6229     case elfcpp::R_PPC64_DTPREL16_DS:
6230     case elfcpp::R_PPC64_TOC16_DS:
6231     case elfcpp::R_PPC64_GOT16_DS:
6232     case elfcpp::R_PPC64_SECTOFF_DS:
6233     case elfcpp::R_POWERPC_REL14:
6234     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6235     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6236     case elfcpp::R_POWERPC_GOT_TLSGD16:
6237     case elfcpp::R_POWERPC_GOT_TLSLD16:
6238     case elfcpp::R_POWERPC_GOT_TPREL16:
6239     case elfcpp::R_POWERPC_GOT_DTPREL16:
6240       overflow = Reloc::CHECK_SIGNED;
6241       break;
6242     }
6243
6244   typename Powerpc_relocate_functions<size, big_endian>::Status status
6245     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
6246   switch (r_type)
6247     {
6248     case elfcpp::R_POWERPC_NONE:
6249     case elfcpp::R_POWERPC_TLS:
6250     case elfcpp::R_POWERPC_GNU_VTINHERIT:
6251     case elfcpp::R_POWERPC_GNU_VTENTRY:
6252     case elfcpp::R_PPC_EMB_MRKREF:
6253       break;
6254
6255     case elfcpp::R_PPC64_ADDR64:
6256     case elfcpp::R_PPC64_REL64:
6257     case elfcpp::R_PPC64_TOC:
6258       Reloc::addr64(view, value);
6259       break;
6260
6261     case elfcpp::R_POWERPC_TPREL:
6262     case elfcpp::R_POWERPC_DTPREL:
6263       if (size == 64)
6264         Reloc::addr64(view, value);
6265       else
6266         status = Reloc::addr32(view, value, overflow);
6267       break;
6268
6269     case elfcpp::R_PPC64_UADDR64:
6270       Reloc::addr64_u(view, value);
6271       break;
6272
6273     case elfcpp::R_POWERPC_ADDR32:
6274       status = Reloc::addr32(view, value, overflow);
6275       break;
6276
6277     case elfcpp::R_POWERPC_REL32:
6278     case elfcpp::R_POWERPC_UADDR32:
6279       status = Reloc::addr32_u(view, value, overflow);
6280       break;
6281
6282     case elfcpp::R_POWERPC_ADDR24:
6283     case elfcpp::R_POWERPC_REL24:
6284     case elfcpp::R_PPC_PLTREL24:
6285     case elfcpp::R_PPC_LOCAL24PC:
6286       status = Reloc::addr24(view, value, overflow);
6287       break;
6288
6289     case elfcpp::R_POWERPC_GOT_DTPREL16:
6290     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6291       if (size == 64)
6292         {
6293           status = Reloc::addr16_ds(view, value, overflow);
6294           break;
6295         }
6296     case elfcpp::R_POWERPC_ADDR16:
6297     case elfcpp::R_POWERPC_REL16:
6298     case elfcpp::R_PPC64_TOC16:
6299     case elfcpp::R_POWERPC_GOT16:
6300     case elfcpp::R_POWERPC_SECTOFF:
6301     case elfcpp::R_POWERPC_TPREL16:
6302     case elfcpp::R_POWERPC_DTPREL16:
6303     case elfcpp::R_POWERPC_GOT_TLSGD16:
6304     case elfcpp::R_POWERPC_GOT_TLSLD16:
6305     case elfcpp::R_POWERPC_GOT_TPREL16:
6306     case elfcpp::R_POWERPC_ADDR16_LO:
6307     case elfcpp::R_POWERPC_REL16_LO:
6308     case elfcpp::R_PPC64_TOC16_LO:
6309     case elfcpp::R_POWERPC_GOT16_LO:
6310     case elfcpp::R_POWERPC_SECTOFF_LO:
6311     case elfcpp::R_POWERPC_TPREL16_LO:
6312     case elfcpp::R_POWERPC_DTPREL16_LO:
6313     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6314     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6315     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6316       status = Reloc::addr16(view, value, overflow);
6317       break;
6318
6319     case elfcpp::R_POWERPC_UADDR16:
6320       status = Reloc::addr16_u(view, value, overflow);
6321       break;
6322
6323     case elfcpp::R_POWERPC_ADDR16_HI:
6324     case elfcpp::R_POWERPC_REL16_HI:
6325     case elfcpp::R_PPC64_TOC16_HI:
6326     case elfcpp::R_POWERPC_GOT16_HI:
6327     case elfcpp::R_POWERPC_SECTOFF_HI:
6328     case elfcpp::R_POWERPC_TPREL16_HI:
6329     case elfcpp::R_POWERPC_DTPREL16_HI:
6330     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6331     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6332     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6333     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6334       Reloc::addr16_hi(view, value);
6335       break;
6336
6337     case elfcpp::R_POWERPC_ADDR16_HA:
6338     case elfcpp::R_POWERPC_REL16_HA:
6339     case elfcpp::R_PPC64_TOC16_HA:
6340     case elfcpp::R_POWERPC_GOT16_HA:
6341     case elfcpp::R_POWERPC_SECTOFF_HA:
6342     case elfcpp::R_POWERPC_TPREL16_HA:
6343     case elfcpp::R_POWERPC_DTPREL16_HA:
6344     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6345     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6346     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6347     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6348       Reloc::addr16_ha(view, value);
6349       break;
6350
6351     case elfcpp::R_PPC64_DTPREL16_HIGHER:
6352       if (size == 32)
6353         // R_PPC_EMB_NADDR16_LO
6354         goto unsupp;
6355     case elfcpp::R_PPC64_ADDR16_HIGHER:
6356     case elfcpp::R_PPC64_TPREL16_HIGHER:
6357       Reloc::addr16_hi2(view, value);
6358       break;
6359
6360     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6361       if (size == 32)
6362         // R_PPC_EMB_NADDR16_HI
6363         goto unsupp;
6364     case elfcpp::R_PPC64_ADDR16_HIGHERA:
6365     case elfcpp::R_PPC64_TPREL16_HIGHERA:
6366       Reloc::addr16_ha2(view, value);
6367       break;
6368
6369     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6370       if (size == 32)
6371         // R_PPC_EMB_NADDR16_HA
6372         goto unsupp;
6373     case elfcpp::R_PPC64_ADDR16_HIGHEST:
6374     case elfcpp::R_PPC64_TPREL16_HIGHEST:
6375       Reloc::addr16_hi3(view, value);
6376       break;
6377
6378     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6379       if (size == 32)
6380         // R_PPC_EMB_SDAI16
6381         goto unsupp;
6382     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
6383     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6384       Reloc::addr16_ha3(view, value);
6385       break;
6386
6387     case elfcpp::R_PPC64_DTPREL16_DS:
6388     case elfcpp::R_PPC64_DTPREL16_LO_DS:
6389       if (size == 32)
6390         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
6391         goto unsupp;
6392     case elfcpp::R_PPC64_TPREL16_DS:
6393     case elfcpp::R_PPC64_TPREL16_LO_DS:
6394       if (size == 32)
6395         // R_PPC_TLSGD, R_PPC_TLSLD
6396         break;
6397     case elfcpp::R_PPC64_ADDR16_DS:
6398     case elfcpp::R_PPC64_ADDR16_LO_DS:
6399     case elfcpp::R_PPC64_TOC16_DS:
6400     case elfcpp::R_PPC64_TOC16_LO_DS:
6401     case elfcpp::R_PPC64_GOT16_DS:
6402     case elfcpp::R_PPC64_GOT16_LO_DS:
6403     case elfcpp::R_PPC64_SECTOFF_DS:
6404     case elfcpp::R_PPC64_SECTOFF_LO_DS:
6405       status = Reloc::addr16_ds(view, value, overflow);
6406       break;
6407
6408     case elfcpp::R_POWERPC_ADDR14:
6409     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6410     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6411     case elfcpp::R_POWERPC_REL14:
6412     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6413     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6414       status = Reloc::addr14(view, value, overflow);
6415       break;
6416
6417     case elfcpp::R_POWERPC_COPY:
6418     case elfcpp::R_POWERPC_GLOB_DAT:
6419     case elfcpp::R_POWERPC_JMP_SLOT:
6420     case elfcpp::R_POWERPC_RELATIVE:
6421     case elfcpp::R_POWERPC_DTPMOD:
6422     case elfcpp::R_PPC64_JMP_IREL:
6423     case elfcpp::R_POWERPC_IRELATIVE:
6424       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6425                              _("unexpected reloc %u in object file"),
6426                              r_type);
6427       break;
6428
6429     case elfcpp::R_PPC_EMB_SDA21:
6430       if (size == 32)
6431         goto unsupp;
6432       else
6433         {
6434           // R_PPC64_TOCSAVE.  For the time being this can be ignored.
6435         }
6436       break;
6437
6438     case elfcpp::R_PPC_EMB_SDA2I16:
6439     case elfcpp::R_PPC_EMB_SDA2REL:
6440       if (size == 32)
6441         goto unsupp;
6442       // R_PPC64_TLSGD, R_PPC64_TLSLD
6443       break;
6444
6445     case elfcpp::R_POWERPC_PLT32:
6446     case elfcpp::R_POWERPC_PLTREL32:
6447     case elfcpp::R_POWERPC_PLT16_LO:
6448     case elfcpp::R_POWERPC_PLT16_HI:
6449     case elfcpp::R_POWERPC_PLT16_HA:
6450     case elfcpp::R_PPC_SDAREL16:
6451     case elfcpp::R_POWERPC_ADDR30:
6452     case elfcpp::R_PPC64_PLT64:
6453     case elfcpp::R_PPC64_PLTREL64:
6454     case elfcpp::R_PPC64_PLTGOT16:
6455     case elfcpp::R_PPC64_PLTGOT16_LO:
6456     case elfcpp::R_PPC64_PLTGOT16_HI:
6457     case elfcpp::R_PPC64_PLTGOT16_HA:
6458     case elfcpp::R_PPC64_PLT16_LO_DS:
6459     case elfcpp::R_PPC64_PLTGOT16_DS:
6460     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
6461     case elfcpp::R_PPC_EMB_RELSEC16:
6462     case elfcpp::R_PPC_EMB_RELST_LO:
6463     case elfcpp::R_PPC_EMB_RELST_HI:
6464     case elfcpp::R_PPC_EMB_RELST_HA:
6465     case elfcpp::R_PPC_EMB_BIT_FLD:
6466     case elfcpp::R_PPC_EMB_RELSDA:
6467     case elfcpp::R_PPC_TOC16:
6468     default:
6469     unsupp:
6470       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6471                              _("unsupported reloc %u"),
6472                              r_type);
6473       break;
6474     }
6475   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
6476     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6477                            _("relocation overflow"));
6478
6479   return true;
6480 }
6481
6482 // Relocate section data.
6483
6484 template<int size, bool big_endian>
6485 void
6486 Target_powerpc<size, big_endian>::relocate_section(
6487     const Relocate_info<size, big_endian>* relinfo,
6488     unsigned int sh_type,
6489     const unsigned char* prelocs,
6490     size_t reloc_count,
6491     Output_section* output_section,
6492     bool needs_special_offset_handling,
6493     unsigned char* view,
6494     Address address,
6495     section_size_type view_size,
6496     const Reloc_symbol_changes* reloc_symbol_changes)
6497 {
6498   typedef Target_powerpc<size, big_endian> Powerpc;
6499   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
6500   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
6501     Powerpc_comdat_behavior;
6502
6503   gold_assert(sh_type == elfcpp::SHT_RELA);
6504
6505   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
6506                          Powerpc_relocate, Powerpc_comdat_behavior>(
6507     relinfo,
6508     this,
6509     prelocs,
6510     reloc_count,
6511     output_section,
6512     needs_special_offset_handling,
6513     view,
6514     address,
6515     view_size,
6516     reloc_symbol_changes);
6517 }
6518
6519 class Powerpc_scan_relocatable_reloc
6520 {
6521 public:
6522   // Return the strategy to use for a local symbol which is not a
6523   // section symbol, given the relocation type.
6524   inline Relocatable_relocs::Reloc_strategy
6525   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
6526   {
6527     if (r_type == 0 && r_sym == 0)
6528       return Relocatable_relocs::RELOC_DISCARD;
6529     return Relocatable_relocs::RELOC_COPY;
6530   }
6531
6532   // Return the strategy to use for a local symbol which is a section
6533   // symbol, given the relocation type.
6534   inline Relocatable_relocs::Reloc_strategy
6535   local_section_strategy(unsigned int, Relobj*)
6536   {
6537     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
6538   }
6539
6540   // Return the strategy to use for a global symbol, given the
6541   // relocation type, the object, and the symbol index.
6542   inline Relocatable_relocs::Reloc_strategy
6543   global_strategy(unsigned int r_type, Relobj*, unsigned int)
6544   {
6545     if (r_type == elfcpp::R_PPC_PLTREL24)
6546       return Relocatable_relocs::RELOC_SPECIAL;
6547     return Relocatable_relocs::RELOC_COPY;
6548   }
6549 };
6550
6551 // Scan the relocs during a relocatable link.
6552
6553 template<int size, bool big_endian>
6554 void
6555 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
6556     Symbol_table* symtab,
6557     Layout* layout,
6558     Sized_relobj_file<size, big_endian>* object,
6559     unsigned int data_shndx,
6560     unsigned int sh_type,
6561     const unsigned char* prelocs,
6562     size_t reloc_count,
6563     Output_section* output_section,
6564     bool needs_special_offset_handling,
6565     size_t local_symbol_count,
6566     const unsigned char* plocal_symbols,
6567     Relocatable_relocs* rr)
6568 {
6569   gold_assert(sh_type == elfcpp::SHT_RELA);
6570
6571   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
6572                                 Powerpc_scan_relocatable_reloc>(
6573     symtab,
6574     layout,
6575     object,
6576     data_shndx,
6577     prelocs,
6578     reloc_count,
6579     output_section,
6580     needs_special_offset_handling,
6581     local_symbol_count,
6582     plocal_symbols,
6583     rr);
6584 }
6585
6586 // Emit relocations for a section.
6587 // This is a modified version of the function by the same name in
6588 // target-reloc.h.  Using relocate_special_relocatable for
6589 // R_PPC_PLTREL24 would require duplication of the entire body of the
6590 // loop, so we may as well duplicate the whole thing.
6591
6592 template<int size, bool big_endian>
6593 void
6594 Target_powerpc<size, big_endian>::relocate_relocs(
6595     const Relocate_info<size, big_endian>* relinfo,
6596     unsigned int sh_type,
6597     const unsigned char* prelocs,
6598     size_t reloc_count,
6599     Output_section* output_section,
6600     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
6601     const Relocatable_relocs* rr,
6602     unsigned char*,
6603     Address view_address,
6604     section_size_type,
6605     unsigned char* reloc_view,
6606     section_size_type reloc_view_size)
6607 {
6608   gold_assert(sh_type == elfcpp::SHT_RELA);
6609
6610   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
6611     Reltype;
6612   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
6613     Reltype_write;
6614   const int reloc_size
6615     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
6616
6617   Powerpc_relobj<size, big_endian>* const object
6618     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6619   const unsigned int local_count = object->local_symbol_count();
6620   unsigned int got2_shndx = object->got2_shndx();
6621   Address got2_addend = 0;
6622   if (got2_shndx != 0)
6623     {
6624       got2_addend = object->get_output_section_offset(got2_shndx);
6625       gold_assert(got2_addend != invalid_address);
6626     }
6627
6628   unsigned char* pwrite = reloc_view;
6629   bool zap_next = false;
6630   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
6631     {
6632       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
6633       if (strategy == Relocatable_relocs::RELOC_DISCARD)
6634         continue;
6635
6636       Reltype reloc(prelocs);
6637       Reltype_write reloc_write(pwrite);
6638
6639       Address offset = reloc.get_r_offset();
6640       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
6641       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
6642       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
6643       const unsigned int orig_r_sym = r_sym;
6644       typename elfcpp::Elf_types<size>::Elf_Swxword addend
6645         = reloc.get_r_addend();
6646       const Symbol* gsym = NULL;
6647
6648       if (zap_next)
6649         {
6650           // We could arrange to discard these and other relocs for
6651           // tls optimised sequences in the strategy methods, but for
6652           // now do as BFD ld does.
6653           r_type = elfcpp::R_POWERPC_NONE;
6654           zap_next = false;
6655         }
6656
6657       // Get the new symbol index.
6658       if (r_sym < local_count)
6659         {
6660           switch (strategy)
6661             {
6662             case Relocatable_relocs::RELOC_COPY:
6663             case Relocatable_relocs::RELOC_SPECIAL:
6664               if (r_sym != 0)
6665                 {
6666                   r_sym = object->symtab_index(r_sym);
6667                   gold_assert(r_sym != -1U);
6668                 }
6669               break;
6670
6671             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
6672               {
6673                 // We are adjusting a section symbol.  We need to find
6674                 // the symbol table index of the section symbol for
6675                 // the output section corresponding to input section
6676                 // in which this symbol is defined.
6677                 gold_assert(r_sym < local_count);
6678                 bool is_ordinary;
6679                 unsigned int shndx =
6680                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
6681                 gold_assert(is_ordinary);
6682                 Output_section* os = object->output_section(shndx);
6683                 gold_assert(os != NULL);
6684                 gold_assert(os->needs_symtab_index());
6685                 r_sym = os->symtab_index();
6686               }
6687               break;
6688
6689             default:
6690               gold_unreachable();
6691             }
6692         }
6693       else
6694         {
6695           gsym = object->global_symbol(r_sym);
6696           gold_assert(gsym != NULL);
6697           if (gsym->is_forwarder())
6698             gsym = relinfo->symtab->resolve_forwards(gsym);
6699
6700           gold_assert(gsym->has_symtab_index());
6701           r_sym = gsym->symtab_index();
6702         }
6703
6704       // Get the new offset--the location in the output section where
6705       // this relocation should be applied.
6706       if (static_cast<Address>(offset_in_output_section) != invalid_address)
6707         offset += offset_in_output_section;
6708       else
6709         {
6710           section_offset_type sot_offset =
6711             convert_types<section_offset_type, Address>(offset);
6712           section_offset_type new_sot_offset =
6713             output_section->output_offset(object, relinfo->data_shndx,
6714                                           sot_offset);
6715           gold_assert(new_sot_offset != -1);
6716           offset = new_sot_offset;
6717         }
6718
6719       // In an object file, r_offset is an offset within the section.
6720       // In an executable or dynamic object, generated by
6721       // --emit-relocs, r_offset is an absolute address.
6722       if (!parameters->options().relocatable())
6723         {
6724           offset += view_address;
6725           if (static_cast<Address>(offset_in_output_section) != invalid_address)
6726             offset -= offset_in_output_section;
6727         }
6728
6729       // Handle the reloc addend based on the strategy.
6730       if (strategy == Relocatable_relocs::RELOC_COPY)
6731         ;
6732       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
6733         {
6734           const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
6735           addend = psymval->value(object, addend);
6736         }
6737       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
6738         {
6739           if (addend >= 32768)
6740             addend += got2_addend;
6741         }
6742       else
6743         gold_unreachable();
6744
6745       if (!parameters->options().relocatable())
6746         {
6747           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6748               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6749               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6750               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6751             {
6752               // First instruction of a global dynamic sequence,
6753               // arg setup insn.
6754               const bool final = gsym == NULL || gsym->final_value_is_known();
6755               switch (this->optimize_tls_gd(final))
6756                 {
6757                 case tls::TLSOPT_TO_IE:
6758                   r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6759                              - elfcpp::R_POWERPC_GOT_TLSGD16);
6760                   break;
6761                 case tls::TLSOPT_TO_LE:
6762                   if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6763                       || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6764                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
6765                   else
6766                     {
6767                       r_type = elfcpp::R_POWERPC_NONE;
6768                       offset -= 2 * big_endian;
6769                     }
6770                   break;
6771                 default:
6772                   break;
6773                 }
6774             }
6775           else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6776                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6777                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6778                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6779             {
6780               // First instruction of a local dynamic sequence,
6781               // arg setup insn.
6782               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
6783                 {
6784                   if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6785                       || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6786                     {
6787                       r_type = elfcpp::R_POWERPC_TPREL16_HA;
6788                       const Output_section* os = relinfo->layout->tls_segment()
6789                         ->first_section();
6790                       gold_assert(os != NULL);
6791                       gold_assert(os->needs_symtab_index());
6792                       r_sym = os->symtab_index();
6793                       addend = dtp_offset;
6794                     }
6795                   else
6796                     {
6797                       r_type = elfcpp::R_POWERPC_NONE;
6798                       offset -= 2 * big_endian;
6799                     }
6800                 }
6801             }
6802           else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6803                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6804                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6805                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6806             {
6807               // First instruction of initial exec sequence.
6808               const bool final = gsym == NULL || gsym->final_value_is_known();
6809               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
6810                 {
6811                   if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6812                       || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6813                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
6814                   else
6815                     {
6816                       r_type = elfcpp::R_POWERPC_NONE;
6817                       offset -= 2 * big_endian;
6818                     }
6819                 }
6820             }
6821           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6822                    || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6823             {
6824               // Second instruction of a global dynamic sequence,
6825               // the __tls_get_addr call
6826               const bool final = gsym == NULL || gsym->final_value_is_known();
6827               switch (this->optimize_tls_gd(final))
6828                 {
6829                 case tls::TLSOPT_TO_IE:
6830                   r_type = elfcpp::R_POWERPC_NONE;
6831                   zap_next = true;
6832                   break;
6833                 case tls::TLSOPT_TO_LE:
6834                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
6835                   offset += 2 * big_endian;
6836                   zap_next = true;
6837                   break;
6838                 default:
6839                   break;
6840                 }
6841             }
6842           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6843                    || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6844             {
6845               // Second instruction of a local dynamic sequence,
6846               // the __tls_get_addr call
6847               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
6848                 {
6849                   const Output_section* os = relinfo->layout->tls_segment()
6850                     ->first_section();
6851                   gold_assert(os != NULL);
6852                   gold_assert(os->needs_symtab_index());
6853                   r_sym = os->symtab_index();
6854                   addend = dtp_offset;
6855                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
6856                   offset += 2 * big_endian;
6857                   zap_next = true;
6858                 }
6859             }
6860           else if (r_type == elfcpp::R_POWERPC_TLS)
6861             {
6862               // Second instruction of an initial exec sequence
6863               const bool final = gsym == NULL || gsym->final_value_is_known();
6864               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
6865                 {
6866                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
6867                   offset += 2 * big_endian;
6868                 }
6869             }
6870         }
6871
6872       reloc_write.put_r_offset(offset);
6873       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
6874       reloc_write.put_r_addend(addend);
6875
6876       pwrite += reloc_size;
6877     }
6878
6879   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
6880               == reloc_view_size);
6881 }
6882
6883 // Return the value to use for a dynamic symbol which requires special
6884 // treatment.  This is how we support equality comparisons of function
6885 // pointers across shared library boundaries, as described in the
6886 // processor specific ABI supplement.
6887
6888 template<int size, bool big_endian>
6889 uint64_t
6890 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
6891 {
6892   if (size == 32)
6893     {
6894       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
6895       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
6896            p != this->stub_tables_.end();
6897            ++p)
6898         {
6899           Address off = (*p)->find_plt_call_entry(gsym);
6900           if (off != invalid_address)
6901             return (*p)->stub_address() + off;
6902         }
6903     }
6904   gold_unreachable();
6905 }
6906
6907 // Return the PLT address to use for a local symbol.
6908 template<int size, bool big_endian>
6909 uint64_t
6910 Target_powerpc<size, big_endian>::do_plt_address_for_local(
6911     const Relobj* object,
6912     unsigned int symndx) const
6913 {
6914   if (size == 32)
6915     {
6916       const Sized_relobj<size, big_endian>* relobj
6917         = static_cast<const Sized_relobj<size, big_endian>*>(object);
6918       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
6919            p != this->stub_tables_.end();
6920            ++p)
6921         {
6922           Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
6923                                                   symndx);
6924           if (off != invalid_address)
6925             return (*p)->stub_address() + off;
6926         }
6927     }
6928   gold_unreachable();
6929 }
6930
6931 // Return the PLT address to use for a global symbol.
6932 template<int size, bool big_endian>
6933 uint64_t
6934 Target_powerpc<size, big_endian>::do_plt_address_for_global(
6935     const Symbol* gsym) const
6936 {
6937   if (size == 32)
6938     {
6939       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
6940            p != this->stub_tables_.end();
6941            ++p)
6942         {
6943           Address off = (*p)->find_plt_call_entry(gsym);
6944           if (off != invalid_address)
6945             return (*p)->stub_address() + off;
6946         }
6947     }
6948   gold_unreachable();
6949 }
6950
6951 // Return the offset to use for the GOT_INDX'th got entry which is
6952 // for a local tls symbol specified by OBJECT, SYMNDX.
6953 template<int size, bool big_endian>
6954 int64_t
6955 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
6956     const Relobj* object,
6957     unsigned int symndx,
6958     unsigned int got_indx) const
6959 {
6960   const Powerpc_relobj<size, big_endian>* ppc_object
6961     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
6962   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
6963     {
6964       for (Got_type got_type = GOT_TYPE_TLSGD;
6965            got_type <= GOT_TYPE_TPREL;
6966            got_type = Got_type(got_type + 1))
6967         if (ppc_object->local_has_got_offset(symndx, got_type))
6968           {
6969             unsigned int off = ppc_object->local_got_offset(symndx, got_type);
6970             if (got_type == GOT_TYPE_TLSGD)
6971               off += size / 8;
6972             if (off == got_indx * (size / 8))
6973               {
6974                 if (got_type == GOT_TYPE_TPREL)
6975                   return -tp_offset;
6976                 else
6977                   return -dtp_offset;
6978               }
6979           }
6980     }
6981   gold_unreachable();
6982 }
6983
6984 // Return the offset to use for the GOT_INDX'th got entry which is
6985 // for global tls symbol GSYM.
6986 template<int size, bool big_endian>
6987 int64_t
6988 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
6989     Symbol* gsym,
6990     unsigned int got_indx) const
6991 {
6992   if (gsym->type() == elfcpp::STT_TLS)
6993     {
6994       for (Got_type got_type = GOT_TYPE_TLSGD;
6995            got_type <= GOT_TYPE_TPREL;
6996            got_type = Got_type(got_type + 1))
6997         if (gsym->has_got_offset(got_type))
6998           {
6999             unsigned int off = gsym->got_offset(got_type);
7000             if (got_type == GOT_TYPE_TLSGD)
7001               off += size / 8;
7002             if (off == got_indx * (size / 8))
7003               {
7004                 if (got_type == GOT_TYPE_TPREL)
7005                   return -tp_offset;
7006                 else
7007                   return -dtp_offset;
7008               }
7009           }
7010     }
7011   gold_unreachable();
7012 }
7013
7014 // The selector for powerpc object files.
7015
7016 template<int size, bool big_endian>
7017 class Target_selector_powerpc : public Target_selector
7018 {
7019 public:
7020   Target_selector_powerpc()
7021     : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
7022                       size, big_endian,
7023                       (size == 64
7024                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
7025                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
7026                       (size == 64
7027                        ? (big_endian ? "elf64ppc" : "elf64lppc")
7028                        : (big_endian ? "elf32ppc" : "elf32lppc")))
7029   { }
7030
7031   virtual Target*
7032   do_instantiate_target()
7033   { return new Target_powerpc<size, big_endian>(); }
7034 };
7035
7036 Target_selector_powerpc<32, true> target_selector_ppc32;
7037 Target_selector_powerpc<32, false> target_selector_ppc32le;
7038 Target_selector_powerpc<64, true> target_selector_ppc64;
7039 Target_selector_powerpc<64, false> target_selector_ppc64le;
7040
7041 // Instantiate these constants for -O0
7042 template<int size, bool big_endian>
7043 const int Output_data_glink<size, big_endian>::pltresolve_size;
7044 template<int size, bool big_endian>
7045 const typename Stub_table<size, big_endian>::Address
7046   Stub_table<size, big_endian>::invalid_address;
7047 template<int size, bool big_endian>
7048 const typename Target_powerpc<size, big_endian>::Address
7049   Target_powerpc<size, big_endian>::invalid_address;
7050
7051 } // End anonymous namespace.