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