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