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