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