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