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