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