* output.h (Output_data_got::add_constant): Tidy.
[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_HIGHER:
4881         case elfcpp::R_PPC64_ADDR16_HIGHEST:
4882         case elfcpp::R_PPC64_ADDR16_HIGHERA:
4883         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4884         case elfcpp::R_PPC64_REL64:
4885         case elfcpp::R_POWERPC_ADDR30:
4886         case elfcpp::R_PPC64_TPREL16_DS:
4887         case elfcpp::R_PPC64_TPREL16_LO_DS:
4888         case elfcpp::R_PPC64_TPREL16_HIGHER:
4889         case elfcpp::R_PPC64_TPREL16_HIGHEST:
4890         case elfcpp::R_PPC64_TPREL16_HIGHERA:
4891         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4892           return;
4893
4894         default:
4895           break;
4896         }
4897     }
4898   else
4899     {
4900       switch (r_type)
4901         {
4902           // These are the relocation types supported only on 32-bit.
4903           // ??? glibc ld.so doesn't need to support these.
4904         case elfcpp::R_POWERPC_DTPREL16:
4905         case elfcpp::R_POWERPC_DTPREL16_LO:
4906         case elfcpp::R_POWERPC_DTPREL16_HI:
4907         case elfcpp::R_POWERPC_DTPREL16_HA:
4908           return;
4909
4910         default:
4911           break;
4912         }
4913     }
4914
4915   // This prevents us from issuing more than one error per reloc
4916   // section.  But we can still wind up issuing more than one
4917   // error per object file.
4918   if (this->issued_non_pic_error_)
4919     return;
4920   gold_assert(parameters->options().output_is_position_independent());
4921   object->error(_("requires unsupported dynamic reloc; "
4922                   "recompile with -fPIC"));
4923   this->issued_non_pic_error_ = true;
4924   return;
4925 }
4926
4927 // Return whether we need to make a PLT entry for a relocation of the
4928 // given type against a STT_GNU_IFUNC symbol.
4929
4930 template<int size, bool big_endian>
4931 bool
4932 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4933      Sized_relobj_file<size, big_endian>* object,
4934      unsigned int r_type,
4935      bool report_err)
4936 {
4937   // In non-pic code any reference will resolve to the plt call stub
4938   // for the ifunc symbol.
4939   if (size == 32 && !parameters->options().output_is_position_independent())
4940     return true;
4941
4942   switch (r_type)
4943     {
4944     // Word size refs from data sections are OK, but don't need a PLT entry.
4945     case elfcpp::R_POWERPC_ADDR32:
4946     case elfcpp::R_POWERPC_UADDR32:
4947       if (size == 32)
4948         return false;
4949       break;
4950
4951     case elfcpp::R_PPC64_ADDR64:
4952     case elfcpp::R_PPC64_UADDR64:
4953       if (size == 64)
4954         return false;
4955       break;
4956
4957     // GOT refs are good, but also don't need a PLT entry.
4958     case elfcpp::R_POWERPC_GOT16:
4959     case elfcpp::R_POWERPC_GOT16_LO:
4960     case elfcpp::R_POWERPC_GOT16_HI:
4961     case elfcpp::R_POWERPC_GOT16_HA:
4962     case elfcpp::R_PPC64_GOT16_DS:
4963     case elfcpp::R_PPC64_GOT16_LO_DS:
4964       return false;
4965
4966     // Function calls are good, and these do need a PLT entry.
4967     case elfcpp::R_POWERPC_ADDR24:
4968     case elfcpp::R_POWERPC_ADDR14:
4969     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4970     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4971     case elfcpp::R_POWERPC_REL24:
4972     case elfcpp::R_PPC_PLTREL24:
4973     case elfcpp::R_POWERPC_REL14:
4974     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4975     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4976       return true;
4977
4978     default:
4979       break;
4980     }
4981
4982   // Anything else is a problem.
4983   // If we are building a static executable, the libc startup function
4984   // responsible for applying indirect function relocations is going
4985   // to complain about the reloc type.
4986   // If we are building a dynamic executable, we will have a text
4987   // relocation.  The dynamic loader will set the text segment
4988   // writable and non-executable to apply text relocations.  So we'll
4989   // segfault when trying to run the indirection function to resolve
4990   // the reloc.
4991   if (report_err)
4992     gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4993                object->name().c_str(), r_type);
4994   return false;
4995 }
4996
4997 // Scan a relocation for a local symbol.
4998
4999 template<int size, bool big_endian>
5000 inline void
5001 Target_powerpc<size, big_endian>::Scan::local(
5002     Symbol_table* symtab,
5003     Layout* layout,
5004     Target_powerpc<size, big_endian>* target,
5005     Sized_relobj_file<size, big_endian>* object,
5006     unsigned int data_shndx,
5007     Output_section* output_section,
5008     const elfcpp::Rela<size, big_endian>& reloc,
5009     unsigned int r_type,
5010     const elfcpp::Sym<size, big_endian>& lsym,
5011     bool is_discarded)
5012 {
5013   this->maybe_skip_tls_get_addr_call(r_type, NULL);
5014
5015   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5016       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5017     {
5018       this->expect_tls_get_addr_call();
5019       const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5020       if (tls_type != tls::TLSOPT_NONE)
5021         this->skip_next_tls_get_addr_call();
5022     }
5023   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5024            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5025     {
5026       this->expect_tls_get_addr_call();
5027       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5028       if (tls_type != tls::TLSOPT_NONE)
5029         this->skip_next_tls_get_addr_call();
5030     }
5031
5032   Powerpc_relobj<size, big_endian>* ppc_object
5033     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5034
5035   if (is_discarded)
5036     {
5037       if (size == 64
5038           && data_shndx == ppc_object->opd_shndx()
5039           && r_type == elfcpp::R_PPC64_ADDR64)
5040         ppc_object->set_opd_discard(reloc.get_r_offset());
5041       return;
5042     }
5043
5044   // A local STT_GNU_IFUNC symbol may require a PLT entry.
5045   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5046   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
5047     {
5048       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5049       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5050                           r_type, r_sym, reloc.get_r_addend());
5051       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5052     }
5053
5054   switch (r_type)
5055     {
5056     case elfcpp::R_POWERPC_NONE:
5057     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5058     case elfcpp::R_POWERPC_GNU_VTENTRY:
5059     case elfcpp::R_PPC64_TOCSAVE:
5060     case elfcpp::R_PPC_EMB_MRKREF:
5061     case elfcpp::R_POWERPC_TLS:
5062       break;
5063
5064     case elfcpp::R_PPC64_TOC:
5065       {
5066         Output_data_got_powerpc<size, big_endian>* got
5067           = target->got_section(symtab, layout);
5068         if (parameters->options().output_is_position_independent())
5069           {
5070             Address off = reloc.get_r_offset();
5071             if (size == 64
5072                 && data_shndx == ppc_object->opd_shndx()
5073                 && ppc_object->get_opd_discard(off - 8))
5074               break;
5075
5076             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5077             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5078             rela_dyn->add_output_section_relative(got->output_section(),
5079                                                   elfcpp::R_POWERPC_RELATIVE,
5080                                                   output_section,
5081                                                   object, data_shndx, off,
5082                                                   symobj->toc_base_offset());
5083           }
5084       }
5085       break;
5086
5087     case elfcpp::R_PPC64_ADDR64:
5088     case elfcpp::R_PPC64_UADDR64:
5089     case elfcpp::R_POWERPC_ADDR32:
5090     case elfcpp::R_POWERPC_UADDR32:
5091     case elfcpp::R_POWERPC_ADDR24:
5092     case elfcpp::R_POWERPC_ADDR16:
5093     case elfcpp::R_POWERPC_ADDR16_LO:
5094     case elfcpp::R_POWERPC_ADDR16_HI:
5095     case elfcpp::R_POWERPC_ADDR16_HA:
5096     case elfcpp::R_POWERPC_UADDR16:
5097     case elfcpp::R_PPC64_ADDR16_HIGHER:
5098     case elfcpp::R_PPC64_ADDR16_HIGHERA:
5099     case elfcpp::R_PPC64_ADDR16_HIGHEST:
5100     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5101     case elfcpp::R_PPC64_ADDR16_DS:
5102     case elfcpp::R_PPC64_ADDR16_LO_DS:
5103     case elfcpp::R_POWERPC_ADDR14:
5104     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5105     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5106       // If building a shared library (or a position-independent
5107       // executable), we need to create a dynamic relocation for
5108       // this location.
5109       if (parameters->options().output_is_position_independent()
5110           || (size == 64 && is_ifunc))
5111         {
5112           Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5113                                                              is_ifunc);
5114           if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5115               || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5116             {
5117               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5118               unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5119                                      : elfcpp::R_POWERPC_RELATIVE);
5120               rela_dyn->add_local_relative(object, r_sym, dynrel,
5121                                            output_section, data_shndx,
5122                                            reloc.get_r_offset(),
5123                                            reloc.get_r_addend(), false);
5124             }
5125           else
5126             {
5127               check_non_pic(object, r_type);
5128               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5129               rela_dyn->add_local(object, r_sym, r_type, output_section,
5130                                   data_shndx, reloc.get_r_offset(),
5131                                   reloc.get_r_addend());
5132             }
5133         }
5134       break;
5135
5136     case elfcpp::R_POWERPC_REL24:
5137     case elfcpp::R_PPC_PLTREL24:
5138     case elfcpp::R_PPC_LOCAL24PC:
5139     case elfcpp::R_POWERPC_REL14:
5140     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5141     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5142       if (!is_ifunc)
5143         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5144                             r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5145                             reloc.get_r_addend());
5146       break;
5147
5148     case elfcpp::R_PPC64_REL64:
5149     case elfcpp::R_POWERPC_REL32:
5150     case elfcpp::R_POWERPC_REL16:
5151     case elfcpp::R_POWERPC_REL16_LO:
5152     case elfcpp::R_POWERPC_REL16_HI:
5153     case elfcpp::R_POWERPC_REL16_HA:
5154     case elfcpp::R_POWERPC_SECTOFF:
5155     case elfcpp::R_POWERPC_TPREL16:
5156     case elfcpp::R_POWERPC_DTPREL16:
5157     case elfcpp::R_POWERPC_SECTOFF_LO:
5158     case elfcpp::R_POWERPC_TPREL16_LO:
5159     case elfcpp::R_POWERPC_DTPREL16_LO:
5160     case elfcpp::R_POWERPC_SECTOFF_HI:
5161     case elfcpp::R_POWERPC_TPREL16_HI:
5162     case elfcpp::R_POWERPC_DTPREL16_HI:
5163     case elfcpp::R_POWERPC_SECTOFF_HA:
5164     case elfcpp::R_POWERPC_TPREL16_HA:
5165     case elfcpp::R_POWERPC_DTPREL16_HA:
5166     case elfcpp::R_PPC64_DTPREL16_HIGHER:
5167     case elfcpp::R_PPC64_TPREL16_HIGHER:
5168     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5169     case elfcpp::R_PPC64_TPREL16_HIGHERA:
5170     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5171     case elfcpp::R_PPC64_TPREL16_HIGHEST:
5172     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5173     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5174     case elfcpp::R_PPC64_TPREL16_DS:
5175     case elfcpp::R_PPC64_TPREL16_LO_DS:
5176     case elfcpp::R_PPC64_DTPREL16_DS:
5177     case elfcpp::R_PPC64_DTPREL16_LO_DS:
5178     case elfcpp::R_PPC64_SECTOFF_DS:
5179     case elfcpp::R_PPC64_SECTOFF_LO_DS:
5180     case elfcpp::R_PPC64_TLSGD:
5181     case elfcpp::R_PPC64_TLSLD:
5182       break;
5183
5184     case elfcpp::R_POWERPC_GOT16:
5185     case elfcpp::R_POWERPC_GOT16_LO:
5186     case elfcpp::R_POWERPC_GOT16_HI:
5187     case elfcpp::R_POWERPC_GOT16_HA:
5188     case elfcpp::R_PPC64_GOT16_DS:
5189     case elfcpp::R_PPC64_GOT16_LO_DS:
5190       {
5191         // The symbol requires a GOT entry.
5192         Output_data_got_powerpc<size, big_endian>* got
5193           = target->got_section(symtab, layout);
5194         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5195
5196         if (!parameters->options().output_is_position_independent())
5197           {
5198             if (size == 32 && is_ifunc)
5199               got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5200             else
5201               got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5202           }
5203         else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5204           {
5205             // If we are generating a shared object or a pie, this
5206             // symbol's GOT entry will be set by a dynamic relocation.
5207             unsigned int off;
5208             off = got->add_constant(0);
5209             object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5210
5211             Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5212                                                                is_ifunc);
5213             unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5214                                    : elfcpp::R_POWERPC_RELATIVE);
5215             rela_dyn->add_local_relative(object, r_sym, dynrel,
5216                                          got, off, 0, false);
5217           }
5218       }
5219       break;
5220
5221     case elfcpp::R_PPC64_TOC16:
5222     case elfcpp::R_PPC64_TOC16_LO:
5223     case elfcpp::R_PPC64_TOC16_HI:
5224     case elfcpp::R_PPC64_TOC16_HA:
5225     case elfcpp::R_PPC64_TOC16_DS:
5226     case elfcpp::R_PPC64_TOC16_LO_DS:
5227       // We need a GOT section.
5228       target->got_section(symtab, layout);
5229       break;
5230
5231     case elfcpp::R_POWERPC_GOT_TLSGD16:
5232     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5233     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5234     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5235       {
5236         const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5237         if (tls_type == tls::TLSOPT_NONE)
5238           {
5239             Output_data_got_powerpc<size, big_endian>* got
5240               = target->got_section(symtab, layout);
5241             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5242             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5243             got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5244                                     rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5245           }
5246         else if (tls_type == tls::TLSOPT_TO_LE)
5247           {
5248             // no GOT relocs needed for Local Exec.
5249           }
5250         else
5251           gold_unreachable();
5252       }
5253       break;
5254
5255     case elfcpp::R_POWERPC_GOT_TLSLD16:
5256     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5257     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5258     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5259       {
5260         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5261         if (tls_type == tls::TLSOPT_NONE)
5262           target->tlsld_got_offset(symtab, layout, object);
5263         else if (tls_type == tls::TLSOPT_TO_LE)
5264           {
5265             // no GOT relocs needed for Local Exec.
5266             if (parameters->options().emit_relocs())
5267               {
5268                 Output_section* os = layout->tls_segment()->first_section();
5269                 gold_assert(os != NULL);
5270                 os->set_needs_symtab_index();
5271               }
5272           }
5273         else
5274           gold_unreachable();
5275       }
5276       break;
5277
5278     case elfcpp::R_POWERPC_GOT_DTPREL16:
5279     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5280     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5281     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5282       {
5283         Output_data_got_powerpc<size, big_endian>* got
5284           = target->got_section(symtab, layout);
5285         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5286         got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5287       }
5288       break;
5289
5290     case elfcpp::R_POWERPC_GOT_TPREL16:
5291     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5292     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5293     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5294       {
5295         const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5296         if (tls_type == tls::TLSOPT_NONE)
5297           {
5298             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5299             if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5300               {
5301                 Output_data_got_powerpc<size, big_endian>* got
5302                   = target->got_section(symtab, layout);
5303                 unsigned int off = got->add_constant(0);
5304                 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5305
5306                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5307                 rela_dyn->add_symbolless_local_addend(object, r_sym,
5308                                                       elfcpp::R_POWERPC_TPREL,
5309                                                       got, off, 0);
5310               }
5311           }
5312         else if (tls_type == tls::TLSOPT_TO_LE)
5313           {
5314             // no GOT relocs needed for Local Exec.
5315           }
5316         else
5317           gold_unreachable();
5318       }
5319       break;
5320
5321     default:
5322       unsupported_reloc_local(object, r_type);
5323       break;
5324     }
5325
5326   switch (r_type)
5327     {
5328     case elfcpp::R_POWERPC_GOT_TLSLD16:
5329     case elfcpp::R_POWERPC_GOT_TLSGD16:
5330     case elfcpp::R_POWERPC_GOT_TPREL16:
5331     case elfcpp::R_POWERPC_GOT_DTPREL16:
5332     case elfcpp::R_POWERPC_GOT16:
5333     case elfcpp::R_PPC64_GOT16_DS:
5334     case elfcpp::R_PPC64_TOC16:
5335     case elfcpp::R_PPC64_TOC16_DS:
5336       ppc_object->set_has_small_toc_reloc();
5337     default:
5338       break;
5339     }
5340 }
5341
5342 // Report an unsupported relocation against a global symbol.
5343
5344 template<int size, bool big_endian>
5345 void
5346 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5347     Sized_relobj_file<size, big_endian>* object,
5348     unsigned int r_type,
5349     Symbol* gsym)
5350 {
5351   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5352              object->name().c_str(), r_type, gsym->demangled_name().c_str());
5353 }
5354
5355 // Scan a relocation for a global symbol.
5356
5357 template<int size, bool big_endian>
5358 inline void
5359 Target_powerpc<size, big_endian>::Scan::global(
5360     Symbol_table* symtab,
5361     Layout* layout,
5362     Target_powerpc<size, big_endian>* target,
5363     Sized_relobj_file<size, big_endian>* object,
5364     unsigned int data_shndx,
5365     Output_section* output_section,
5366     const elfcpp::Rela<size, big_endian>& reloc,
5367     unsigned int r_type,
5368     Symbol* gsym)
5369 {
5370   if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5371     return;
5372
5373   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5374       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5375     {
5376       this->expect_tls_get_addr_call();
5377       const bool final = gsym->final_value_is_known();
5378       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5379       if (tls_type != tls::TLSOPT_NONE)
5380         this->skip_next_tls_get_addr_call();
5381     }
5382   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5383            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5384     {
5385       this->expect_tls_get_addr_call();
5386       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5387       if (tls_type != tls::TLSOPT_NONE)
5388         this->skip_next_tls_get_addr_call();
5389     }
5390
5391   Powerpc_relobj<size, big_endian>* ppc_object
5392     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5393
5394   // A STT_GNU_IFUNC symbol may require a PLT entry.
5395   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5396   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
5397     {
5398       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5399                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5400                           reloc.get_r_addend());
5401       target->make_plt_entry(symtab, layout, gsym);
5402     }
5403
5404   switch (r_type)
5405     {
5406     case elfcpp::R_POWERPC_NONE:
5407     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5408     case elfcpp::R_POWERPC_GNU_VTENTRY:
5409     case elfcpp::R_PPC_LOCAL24PC:
5410     case elfcpp::R_PPC_EMB_MRKREF:
5411     case elfcpp::R_POWERPC_TLS:
5412       break;
5413
5414     case elfcpp::R_PPC64_TOC:
5415       {
5416         Output_data_got_powerpc<size, big_endian>* got
5417           = target->got_section(symtab, layout);
5418         if (parameters->options().output_is_position_independent())
5419           {
5420             Address off = reloc.get_r_offset();
5421             if (size == 64
5422                 && data_shndx == ppc_object->opd_shndx()
5423                 && ppc_object->get_opd_discard(off - 8))
5424               break;
5425
5426             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5427             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5428             if (data_shndx != ppc_object->opd_shndx())
5429               symobj = static_cast
5430                 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5431             rela_dyn->add_output_section_relative(got->output_section(),
5432                                                   elfcpp::R_POWERPC_RELATIVE,
5433                                                   output_section,
5434                                                   object, data_shndx, off,
5435                                                   symobj->toc_base_offset());
5436           }
5437       }
5438       break;
5439
5440     case elfcpp::R_PPC64_ADDR64:
5441       if (size == 64
5442           && data_shndx == ppc_object->opd_shndx()
5443           && (gsym->is_defined_in_discarded_section()
5444               || gsym->object() != object))
5445         {
5446           ppc_object->set_opd_discard(reloc.get_r_offset());
5447           break;
5448         }
5449       // Fall thru
5450     case elfcpp::R_PPC64_UADDR64:
5451     case elfcpp::R_POWERPC_ADDR32:
5452     case elfcpp::R_POWERPC_UADDR32:
5453     case elfcpp::R_POWERPC_ADDR24:
5454     case elfcpp::R_POWERPC_ADDR16:
5455     case elfcpp::R_POWERPC_ADDR16_LO:
5456     case elfcpp::R_POWERPC_ADDR16_HI:
5457     case elfcpp::R_POWERPC_ADDR16_HA:
5458     case elfcpp::R_POWERPC_UADDR16:
5459     case elfcpp::R_PPC64_ADDR16_HIGHER:
5460     case elfcpp::R_PPC64_ADDR16_HIGHERA:
5461     case elfcpp::R_PPC64_ADDR16_HIGHEST:
5462     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5463     case elfcpp::R_PPC64_ADDR16_DS:
5464     case elfcpp::R_PPC64_ADDR16_LO_DS:
5465     case elfcpp::R_POWERPC_ADDR14:
5466     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5467     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5468       {
5469         // Make a PLT entry if necessary.
5470         if (gsym->needs_plt_entry())
5471           {
5472             if (!is_ifunc)
5473               {
5474                 target->push_branch(ppc_object, data_shndx,
5475                                     reloc.get_r_offset(), r_type,
5476                                     elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5477                                     reloc.get_r_addend());
5478                 target->make_plt_entry(symtab, layout, gsym);
5479               }
5480             // Since this is not a PC-relative relocation, we may be
5481             // taking the address of a function. In that case we need to
5482             // set the entry in the dynamic symbol table to the address of
5483             // the PLT call stub.
5484             if (size == 32
5485                 && gsym->is_from_dynobj()
5486                 && !parameters->options().output_is_position_independent())
5487               gsym->set_needs_dynsym_value();
5488           }
5489         // Make a dynamic relocation if necessary.
5490         if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
5491             || (size == 64 && is_ifunc))
5492           {
5493             if (gsym->may_need_copy_reloc())
5494               {
5495                 target->copy_reloc(symtab, layout, object,
5496                                    data_shndx, output_section, gsym, reloc);
5497               }
5498             else if ((size == 32
5499                       && r_type == elfcpp::R_POWERPC_ADDR32
5500                       && gsym->can_use_relative_reloc(false)
5501                       && !(gsym->visibility() == elfcpp::STV_PROTECTED
5502                            && parameters->options().shared()))
5503                      || (size == 64
5504                          && r_type == elfcpp::R_PPC64_ADDR64
5505                          && (gsym->can_use_relative_reloc(false)
5506                              || data_shndx == ppc_object->opd_shndx())))
5507               {
5508                 Reloc_section* rela_dyn
5509                   = target->rela_dyn_section(symtab, layout, is_ifunc);
5510                 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5511                                        : elfcpp::R_POWERPC_RELATIVE);
5512                 rela_dyn->add_symbolless_global_addend(
5513                     gsym, dynrel, output_section, object, data_shndx,
5514                     reloc.get_r_offset(), reloc.get_r_addend());
5515               }
5516             else
5517               {
5518                 Reloc_section* rela_dyn
5519                   = target->rela_dyn_section(symtab, layout, is_ifunc);
5520                 check_non_pic(object, r_type);
5521                 rela_dyn->add_global(gsym, r_type, output_section,
5522                                      object, data_shndx,
5523                                      reloc.get_r_offset(),
5524                                      reloc.get_r_addend());
5525               }
5526           }
5527       }
5528       break;
5529
5530     case elfcpp::R_PPC_PLTREL24:
5531     case elfcpp::R_POWERPC_REL24:
5532       if (!is_ifunc)
5533         {
5534           target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5535                               r_type,
5536                               elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5537                               reloc.get_r_addend());
5538           if (gsym->needs_plt_entry()
5539               || (!gsym->final_value_is_known()
5540                   && (gsym->is_undefined()
5541                       || gsym->is_from_dynobj()
5542                       || gsym->is_preemptible())))
5543             target->make_plt_entry(symtab, layout, gsym);
5544         }
5545       // Fall thru
5546
5547     case elfcpp::R_PPC64_REL64:
5548     case elfcpp::R_POWERPC_REL32:
5549       // Make a dynamic relocation if necessary.
5550       if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
5551         {
5552           if (gsym->may_need_copy_reloc())
5553             {
5554               target->copy_reloc(symtab, layout, object,
5555                                  data_shndx, output_section, gsym,
5556                                  reloc);
5557             }
5558           else
5559             {
5560               Reloc_section* rela_dyn
5561                 = target->rela_dyn_section(symtab, layout, is_ifunc);
5562               check_non_pic(object, r_type);
5563               rela_dyn->add_global(gsym, r_type, output_section, object,
5564                                    data_shndx, reloc.get_r_offset(),
5565                                    reloc.get_r_addend());
5566             }
5567         }
5568       break;
5569
5570     case elfcpp::R_POWERPC_REL14:
5571     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5572     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5573       if (!is_ifunc)
5574         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5575                             r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5576                             reloc.get_r_addend());
5577       break;
5578
5579     case elfcpp::R_POWERPC_REL16:
5580     case elfcpp::R_POWERPC_REL16_LO:
5581     case elfcpp::R_POWERPC_REL16_HI:
5582     case elfcpp::R_POWERPC_REL16_HA:
5583     case elfcpp::R_POWERPC_SECTOFF:
5584     case elfcpp::R_POWERPC_TPREL16:
5585     case elfcpp::R_POWERPC_DTPREL16:
5586     case elfcpp::R_POWERPC_SECTOFF_LO:
5587     case elfcpp::R_POWERPC_TPREL16_LO:
5588     case elfcpp::R_POWERPC_DTPREL16_LO:
5589     case elfcpp::R_POWERPC_SECTOFF_HI:
5590     case elfcpp::R_POWERPC_TPREL16_HI:
5591     case elfcpp::R_POWERPC_DTPREL16_HI:
5592     case elfcpp::R_POWERPC_SECTOFF_HA:
5593     case elfcpp::R_POWERPC_TPREL16_HA:
5594     case elfcpp::R_POWERPC_DTPREL16_HA:
5595     case elfcpp::R_PPC64_DTPREL16_HIGHER:
5596     case elfcpp::R_PPC64_TPREL16_HIGHER:
5597     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5598     case elfcpp::R_PPC64_TPREL16_HIGHERA:
5599     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5600     case elfcpp::R_PPC64_TPREL16_HIGHEST:
5601     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5602     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5603     case elfcpp::R_PPC64_TPREL16_DS:
5604     case elfcpp::R_PPC64_TPREL16_LO_DS:
5605     case elfcpp::R_PPC64_DTPREL16_DS:
5606     case elfcpp::R_PPC64_DTPREL16_LO_DS:
5607     case elfcpp::R_PPC64_SECTOFF_DS:
5608     case elfcpp::R_PPC64_SECTOFF_LO_DS:
5609     case elfcpp::R_PPC64_TLSGD:
5610     case elfcpp::R_PPC64_TLSLD:
5611       break;
5612
5613     case elfcpp::R_POWERPC_GOT16:
5614     case elfcpp::R_POWERPC_GOT16_LO:
5615     case elfcpp::R_POWERPC_GOT16_HI:
5616     case elfcpp::R_POWERPC_GOT16_HA:
5617     case elfcpp::R_PPC64_GOT16_DS:
5618     case elfcpp::R_PPC64_GOT16_LO_DS:
5619       {
5620         // The symbol requires a GOT entry.
5621         Output_data_got_powerpc<size, big_endian>* got;
5622
5623         got = target->got_section(symtab, layout);
5624         if (gsym->final_value_is_known())
5625           {
5626             if (size == 32 && is_ifunc)
5627               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5628             else
5629               got->add_global(gsym, GOT_TYPE_STANDARD);
5630           }
5631         else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
5632           {
5633             // If we are generating a shared object or a pie, this
5634             // symbol's GOT entry will be set by a dynamic relocation.
5635             unsigned int off = got->add_constant(0);
5636             gsym->set_got_offset(GOT_TYPE_STANDARD, off);
5637
5638             Reloc_section* rela_dyn
5639               = target->rela_dyn_section(symtab, layout, is_ifunc);
5640
5641             if (gsym->can_use_relative_reloc(false)
5642                 && !(size == 32
5643                      && gsym->visibility() == elfcpp::STV_PROTECTED
5644                      && parameters->options().shared()))
5645               {
5646                 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5647                                        : elfcpp::R_POWERPC_RELATIVE);
5648                 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
5649               }
5650             else
5651               {
5652                 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5653                 rela_dyn->add_global(gsym, dynrel, got, off, 0);
5654               }
5655           }
5656       }
5657       break;
5658
5659     case elfcpp::R_PPC64_TOC16:
5660     case elfcpp::R_PPC64_TOC16_LO:
5661     case elfcpp::R_PPC64_TOC16_HI:
5662     case elfcpp::R_PPC64_TOC16_HA:
5663     case elfcpp::R_PPC64_TOC16_DS:
5664     case elfcpp::R_PPC64_TOC16_LO_DS:
5665       // We need a GOT section.
5666       target->got_section(symtab, layout);
5667       break;
5668
5669     case elfcpp::R_POWERPC_GOT_TLSGD16:
5670     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5671     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5672     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5673       {
5674         const bool final = gsym->final_value_is_known();
5675         const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5676         if (tls_type == tls::TLSOPT_NONE)
5677           {
5678             Output_data_got_powerpc<size, big_endian>* got
5679               = target->got_section(symtab, layout);
5680             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5681             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
5682                                           elfcpp::R_POWERPC_DTPMOD,
5683                                           elfcpp::R_POWERPC_DTPREL);
5684           }
5685         else if (tls_type == tls::TLSOPT_TO_IE)
5686           {
5687             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5688               {
5689                 Output_data_got_powerpc<size, big_endian>* got
5690                   = target->got_section(symtab, layout);
5691                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5692                 if (gsym->is_undefined()
5693                     || gsym->is_from_dynobj())
5694                   {
5695                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5696                                              elfcpp::R_POWERPC_TPREL);
5697                   }
5698                 else
5699                   {
5700                     unsigned int off = got->add_constant(0);
5701                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
5702                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5703                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5704                                                            got, off, 0);
5705                   }
5706               }
5707           }
5708         else if (tls_type == tls::TLSOPT_TO_LE)
5709           {
5710             // no GOT relocs needed for Local Exec.
5711           }
5712         else
5713           gold_unreachable();
5714       }
5715       break;
5716
5717     case elfcpp::R_POWERPC_GOT_TLSLD16:
5718     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5719     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5720     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5721       {
5722         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5723         if (tls_type == tls::TLSOPT_NONE)
5724           target->tlsld_got_offset(symtab, layout, object);
5725         else if (tls_type == tls::TLSOPT_TO_LE)
5726           {
5727             // no GOT relocs needed for Local Exec.
5728             if (parameters->options().emit_relocs())
5729               {
5730                 Output_section* os = layout->tls_segment()->first_section();
5731                 gold_assert(os != NULL);
5732                 os->set_needs_symtab_index();
5733               }
5734           }
5735         else
5736           gold_unreachable();
5737       }
5738       break;
5739
5740     case elfcpp::R_POWERPC_GOT_DTPREL16:
5741     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5742     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5743     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5744       {
5745         Output_data_got_powerpc<size, big_endian>* got
5746           = target->got_section(symtab, layout);
5747         if (!gsym->final_value_is_known()
5748             && (gsym->is_from_dynobj()
5749                 || gsym->is_undefined()
5750                 || gsym->is_preemptible()))
5751           got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
5752                                    target->rela_dyn_section(layout),
5753                                    elfcpp::R_POWERPC_DTPREL);
5754         else
5755           got->add_global_tls(gsym, GOT_TYPE_DTPREL);
5756       }
5757       break;
5758
5759     case elfcpp::R_POWERPC_GOT_TPREL16:
5760     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5761     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5762     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5763       {
5764         const bool final = gsym->final_value_is_known();
5765         const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5766         if (tls_type == tls::TLSOPT_NONE)
5767           {
5768             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5769               {
5770                 Output_data_got_powerpc<size, big_endian>* got
5771                   = target->got_section(symtab, layout);
5772                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5773                 if (gsym->is_undefined()
5774                     || gsym->is_from_dynobj())
5775                   {
5776                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5777                                              elfcpp::R_POWERPC_TPREL);
5778                   }
5779                 else
5780                   {
5781                     unsigned int off = got->add_constant(0);
5782                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
5783                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5784                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5785                                                            got, off, 0);
5786                   }
5787               }
5788           }
5789         else if (tls_type == tls::TLSOPT_TO_LE)
5790           {
5791             // no GOT relocs needed for Local Exec.
5792           }
5793         else
5794           gold_unreachable();
5795       }
5796       break;
5797
5798     default:
5799       unsupported_reloc_global(object, r_type, gsym);
5800       break;
5801     }
5802
5803   switch (r_type)
5804     {
5805     case elfcpp::R_POWERPC_GOT_TLSLD16:
5806     case elfcpp::R_POWERPC_GOT_TLSGD16:
5807     case elfcpp::R_POWERPC_GOT_TPREL16:
5808     case elfcpp::R_POWERPC_GOT_DTPREL16:
5809     case elfcpp::R_POWERPC_GOT16:
5810     case elfcpp::R_PPC64_GOT16_DS:
5811     case elfcpp::R_PPC64_TOC16:
5812     case elfcpp::R_PPC64_TOC16_DS:
5813       ppc_object->set_has_small_toc_reloc();
5814     default:
5815       break;
5816     }
5817 }
5818
5819 // Process relocations for gc.
5820
5821 template<int size, bool big_endian>
5822 void
5823 Target_powerpc<size, big_endian>::gc_process_relocs(
5824     Symbol_table* symtab,
5825     Layout* layout,
5826     Sized_relobj_file<size, big_endian>* object,
5827     unsigned int data_shndx,
5828     unsigned int,
5829     const unsigned char* prelocs,
5830     size_t reloc_count,
5831     Output_section* output_section,
5832     bool needs_special_offset_handling,
5833     size_t local_symbol_count,
5834     const unsigned char* plocal_symbols)
5835 {
5836   typedef Target_powerpc<size, big_endian> Powerpc;
5837   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5838   Powerpc_relobj<size, big_endian>* ppc_object
5839     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5840   if (size == 64)
5841     ppc_object->set_opd_valid();
5842   if (size == 64 && data_shndx == ppc_object->opd_shndx())
5843     {
5844       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
5845       for (p = ppc_object->access_from_map()->begin();
5846            p != ppc_object->access_from_map()->end();
5847            ++p)
5848         {
5849           Address dst_off = p->first;
5850           unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5851           typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
5852           for (s = p->second.begin(); s != p->second.end(); ++s)
5853             {
5854               Object* src_obj = s->first;
5855               unsigned int src_indx = s->second;
5856               symtab->gc()->add_reference(src_obj, src_indx,
5857                                           ppc_object, dst_indx);
5858             }
5859           p->second.clear();
5860         }
5861       ppc_object->access_from_map()->clear();
5862       ppc_object->process_gc_mark(symtab);
5863       // Don't look at .opd relocs as .opd will reference everything.
5864       return;
5865     }
5866
5867   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
5868                           typename Target_powerpc::Relocatable_size_for_reloc>(
5869     symtab,
5870     layout,
5871     this,
5872     object,
5873     data_shndx,
5874     prelocs,
5875     reloc_count,
5876     output_section,
5877     needs_special_offset_handling,
5878     local_symbol_count,
5879     plocal_symbols);
5880 }
5881
5882 // Handle target specific gc actions when adding a gc reference from
5883 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5884 // and DST_OFF.  For powerpc64, this adds a referenc to the code
5885 // section of a function descriptor.
5886
5887 template<int size, bool big_endian>
5888 void
5889 Target_powerpc<size, big_endian>::do_gc_add_reference(
5890     Symbol_table* symtab,
5891     Object* src_obj,
5892     unsigned int src_shndx,
5893     Object* dst_obj,
5894     unsigned int dst_shndx,
5895     Address dst_off) const
5896 {
5897   if (size != 64 || dst_obj->is_dynamic())
5898     return;
5899
5900   Powerpc_relobj<size, big_endian>* ppc_object
5901     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
5902   if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
5903     {
5904       if (ppc_object->opd_valid())
5905         {
5906           dst_shndx = ppc_object->get_opd_ent(dst_off);
5907           symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
5908         }
5909       else
5910         {
5911           // If we haven't run scan_opd_relocs, we must delay
5912           // processing this function descriptor reference.
5913           ppc_object->add_reference(src_obj, src_shndx, dst_off);
5914         }
5915     }
5916 }
5917
5918 // Add any special sections for this symbol to the gc work list.
5919 // For powerpc64, this adds the code section of a function
5920 // descriptor.
5921
5922 template<int size, bool big_endian>
5923 void
5924 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
5925     Symbol_table* symtab,
5926     Symbol* sym) const
5927 {
5928   if (size == 64)
5929     {
5930       Powerpc_relobj<size, big_endian>* ppc_object
5931         = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
5932       bool is_ordinary;
5933       unsigned int shndx = sym->shndx(&is_ordinary);
5934       if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
5935         {
5936           Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
5937           Address dst_off = gsym->value();
5938           if (ppc_object->opd_valid())
5939             {
5940               unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5941               symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
5942             }
5943           else
5944             ppc_object->add_gc_mark(dst_off);
5945         }
5946     }
5947 }
5948
5949 // For a symbol location in .opd, set LOC to the location of the
5950 // function entry.
5951
5952 template<int size, bool big_endian>
5953 void
5954 Target_powerpc<size, big_endian>::do_function_location(
5955     Symbol_location* loc) const
5956 {
5957   if (size == 64 && loc->shndx != 0)
5958     {
5959       if (loc->object->is_dynamic())
5960         {
5961           Powerpc_dynobj<size, big_endian>* ppc_object
5962             = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
5963           if (loc->shndx == ppc_object->opd_shndx())
5964             {
5965               Address dest_off;
5966               Address off = loc->offset - ppc_object->opd_address();
5967               loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
5968               loc->offset = dest_off;
5969             }
5970         }
5971       else
5972         {
5973           const Powerpc_relobj<size, big_endian>* ppc_object
5974             = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
5975           if (loc->shndx == ppc_object->opd_shndx())
5976             {
5977               Address dest_off;
5978               loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
5979               loc->offset = dest_off;
5980             }
5981         }
5982     }
5983 }
5984
5985 // Scan relocations for a section.
5986
5987 template<int size, bool big_endian>
5988 void
5989 Target_powerpc<size, big_endian>::scan_relocs(
5990     Symbol_table* symtab,
5991     Layout* layout,
5992     Sized_relobj_file<size, big_endian>* object,
5993     unsigned int data_shndx,
5994     unsigned int sh_type,
5995     const unsigned char* prelocs,
5996     size_t reloc_count,
5997     Output_section* output_section,
5998     bool needs_special_offset_handling,
5999     size_t local_symbol_count,
6000     const unsigned char* plocal_symbols)
6001 {
6002   typedef Target_powerpc<size, big_endian> Powerpc;
6003   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6004
6005   if (sh_type == elfcpp::SHT_REL)
6006     {
6007       gold_error(_("%s: unsupported REL reloc section"),
6008                  object->name().c_str());
6009       return;
6010     }
6011
6012   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
6013     symtab,
6014     layout,
6015     this,
6016     object,
6017     data_shndx,
6018     prelocs,
6019     reloc_count,
6020     output_section,
6021     needs_special_offset_handling,
6022     local_symbol_count,
6023     plocal_symbols);
6024 }
6025
6026 // Functor class for processing the global symbol table.
6027 // Removes symbols defined on discarded opd entries.
6028
6029 template<bool big_endian>
6030 class Global_symbol_visitor_opd
6031 {
6032  public:
6033   Global_symbol_visitor_opd()
6034   { }
6035
6036   void
6037   operator()(Sized_symbol<64>* sym)
6038   {
6039     if (sym->has_symtab_index()
6040         || sym->source() != Symbol::FROM_OBJECT
6041         || !sym->in_real_elf())
6042       return;
6043
6044     if (sym->object()->is_dynamic())
6045       return;
6046
6047     Powerpc_relobj<64, big_endian>* symobj
6048       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6049     if (symobj->opd_shndx() == 0)
6050       return;
6051
6052     bool is_ordinary;
6053     unsigned int shndx = sym->shndx(&is_ordinary);
6054     if (shndx == symobj->opd_shndx()
6055         && symobj->get_opd_discard(sym->value()))
6056       sym->set_symtab_index(-1U);
6057   }
6058 };
6059
6060 template<int size, bool big_endian>
6061 void
6062 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6063     Layout* layout,
6064     Symbol_table* symtab)
6065 {
6066   if (size == 64)
6067     {
6068       Output_data_save_res<64, big_endian>* savres
6069         = new Output_data_save_res<64, big_endian>(symtab);
6070       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6071                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6072                                       savres, ORDER_TEXT, false);
6073     }
6074 }
6075
6076 // Sort linker created .got section first (for the header), then input
6077 // sections belonging to files using small model code.
6078
6079 template<bool big_endian>
6080 class Sort_toc_sections
6081 {
6082  public:
6083   bool
6084   operator()(const Output_section::Input_section& is1,
6085              const Output_section::Input_section& is2) const
6086   {
6087     if (!is1.is_input_section() && is2.is_input_section())
6088       return true;
6089     bool small1
6090       = (is1.is_input_section()
6091          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6092              ->has_small_toc_reloc()));
6093     bool small2
6094       = (is2.is_input_section()
6095          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6096              ->has_small_toc_reloc()));
6097     return small1 && !small2;
6098   }
6099 };
6100
6101 // Finalize the sections.
6102
6103 template<int size, bool big_endian>
6104 void
6105 Target_powerpc<size, big_endian>::do_finalize_sections(
6106     Layout* layout,
6107     const Input_objects*,
6108     Symbol_table* symtab)
6109 {
6110   if (parameters->doing_static_link())
6111     {
6112       // At least some versions of glibc elf-init.o have a strong
6113       // reference to __rela_iplt marker syms.  A weak ref would be
6114       // better..
6115       if (this->iplt_ != NULL)
6116         {
6117           Reloc_section* rel = this->iplt_->rel_plt();
6118           symtab->define_in_output_data("__rela_iplt_start", NULL,
6119                                         Symbol_table::PREDEFINED, rel, 0, 0,
6120                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6121                                         elfcpp::STV_HIDDEN, 0, false, true);
6122           symtab->define_in_output_data("__rela_iplt_end", NULL,
6123                                         Symbol_table::PREDEFINED, rel, 0, 0,
6124                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6125                                         elfcpp::STV_HIDDEN, 0, true, true);
6126         }
6127       else
6128         {
6129           symtab->define_as_constant("__rela_iplt_start", NULL,
6130                                      Symbol_table::PREDEFINED, 0, 0,
6131                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6132                                      elfcpp::STV_HIDDEN, 0, true, false);
6133           symtab->define_as_constant("__rela_iplt_end", NULL,
6134                                      Symbol_table::PREDEFINED, 0, 0,
6135                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6136                                      elfcpp::STV_HIDDEN, 0, true, false);
6137         }
6138     }
6139
6140   if (size == 64)
6141     {
6142       typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6143       symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6144
6145       if (!parameters->options().relocatable())
6146         {
6147           this->define_save_restore_funcs(layout, symtab);
6148
6149           // Annoyingly, we need to make these sections now whether or
6150           // not we need them.  If we delay until do_relax then we
6151           // need to mess with the relaxation machinery checkpointing.
6152           this->got_section(symtab, layout);
6153           this->make_brlt_section(layout);
6154
6155           if (parameters->options().toc_sort())
6156             {
6157               Output_section* os = this->got_->output_section();
6158               if (os != NULL && os->input_sections().size() > 1)
6159                 std::stable_sort(os->input_sections().begin(),
6160                                  os->input_sections().end(),
6161                                  Sort_toc_sections<big_endian>());
6162             }
6163         }
6164     }
6165
6166   // Fill in some more dynamic tags.
6167   Output_data_dynamic* odyn = layout->dynamic_data();
6168   if (odyn != NULL)
6169     {
6170       const Reloc_section* rel_plt = (this->plt_ == NULL
6171                                       ? NULL
6172                                       : this->plt_->rel_plt());
6173       layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6174                                       this->rela_dyn_, true, size == 32);
6175
6176       if (size == 32)
6177         {
6178           if (this->got_ != NULL)
6179             {
6180               this->got_->finalize_data_size();
6181               odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6182                                             this->got_, this->got_->g_o_t());
6183             }
6184         }
6185       else
6186         {
6187           if (this->glink_ != NULL)
6188             {
6189               this->glink_->finalize_data_size();
6190               odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6191                                             this->glink_,
6192                                             (this->glink_->pltresolve_size
6193                                              - 32));
6194             }
6195         }
6196     }
6197
6198   // Emit any relocs we saved in an attempt to avoid generating COPY
6199   // relocs.
6200   if (this->copy_relocs_.any_saved_relocs())
6201     this->copy_relocs_.emit(this->rela_dyn_section(layout));
6202 }
6203
6204 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6205 // reloc.
6206
6207 static bool
6208 ok_lo_toc_insn(uint32_t insn)
6209 {
6210   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6211           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6212           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6213           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6214           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6215           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6216           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6217           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6218           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6219           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6220           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6221           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6222           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6223           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6224           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6225               && (insn & 3) != 1)
6226           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6227               && ((insn & 3) == 0 || (insn & 3) == 3))
6228           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6229 }
6230
6231 // Return the value to use for a branch relocation.
6232
6233 template<int size, bool big_endian>
6234 typename Target_powerpc<size, big_endian>::Address
6235 Target_powerpc<size, big_endian>::symval_for_branch(
6236     const Symbol_table* symtab,
6237     Address value,
6238     const Sized_symbol<size>* gsym,
6239     Powerpc_relobj<size, big_endian>* object,
6240     unsigned int *dest_shndx)
6241 {
6242   *dest_shndx = 0;
6243   if (size == 32)
6244     return value;
6245
6246   // If the symbol is defined in an opd section, ie. is a function
6247   // descriptor, use the function descriptor code entry address
6248   Powerpc_relobj<size, big_endian>* symobj = object;
6249   if (gsym != NULL
6250       && gsym->source() != Symbol::FROM_OBJECT)
6251     return value;
6252   if (gsym != NULL)
6253     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6254   unsigned int shndx = symobj->opd_shndx();
6255   if (shndx == 0)
6256     return value;
6257   Address opd_addr = symobj->get_output_section_offset(shndx);
6258   if (opd_addr == invalid_address)
6259     return value;
6260   opd_addr += symobj->output_section_address(shndx);
6261   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
6262     {
6263       Address sec_off;
6264       *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
6265       if (symtab->is_section_folded(symobj, *dest_shndx))
6266         {
6267           Section_id folded
6268             = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6269           symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6270           *dest_shndx = folded.second;
6271         }
6272       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6273       gold_assert(sec_addr != invalid_address);
6274       sec_addr += symobj->output_section(*dest_shndx)->address();
6275       value = sec_addr + sec_off;
6276     }
6277   return value;
6278 }
6279
6280 // Perform a relocation.
6281
6282 template<int size, bool big_endian>
6283 inline bool
6284 Target_powerpc<size, big_endian>::Relocate::relocate(
6285     const Relocate_info<size, big_endian>* relinfo,
6286     Target_powerpc* target,
6287     Output_section* os,
6288     size_t relnum,
6289     const elfcpp::Rela<size, big_endian>& rela,
6290     unsigned int r_type,
6291     const Sized_symbol<size>* gsym,
6292     const Symbol_value<size>* psymval,
6293     unsigned char* view,
6294     Address address,
6295     section_size_type view_size)
6296 {
6297   if (view == NULL)
6298     return true;
6299
6300   switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6301     {
6302     case Track_tls::NOT_EXPECTED:
6303       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6304                              _("__tls_get_addr call lacks marker reloc"));
6305       break;
6306     case Track_tls::EXPECTED:
6307       // We have already complained.
6308       break;
6309     case Track_tls::SKIP:
6310       return true;
6311     case Track_tls::NORMAL:
6312       break;
6313     }
6314
6315   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6316   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6317   Powerpc_relobj<size, big_endian>* const object
6318     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6319   Address value = 0;
6320   bool has_plt_value = false;
6321   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6322   if ((gsym != NULL
6323        ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
6324        : object->local_has_plt_offset(r_sym))
6325       && (!psymval->is_ifunc_symbol()
6326           || Scan::reloc_needs_plt_for_ifunc(object, r_type, false)))
6327     {
6328       Stub_table<size, big_endian>* stub_table
6329         = object->stub_table(relinfo->data_shndx);
6330       if (stub_table == NULL)
6331         {
6332           // This is a ref from a data section to an ifunc symbol.
6333           if (target->stub_tables().size() != 0)
6334             stub_table = target->stub_tables()[0];
6335         }
6336       gold_assert(stub_table != NULL);
6337       Address off;
6338       if (gsym != NULL)
6339         off = stub_table->find_plt_call_entry(object, gsym, r_type,
6340                                               rela.get_r_addend());
6341       else
6342         off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6343                                               rela.get_r_addend());
6344       gold_assert(off != invalid_address);
6345       value = stub_table->stub_address() + off;
6346       has_plt_value = true;
6347     }
6348
6349   if (r_type == elfcpp::R_POWERPC_GOT16
6350       || r_type == elfcpp::R_POWERPC_GOT16_LO
6351       || r_type == elfcpp::R_POWERPC_GOT16_HI
6352       || r_type == elfcpp::R_POWERPC_GOT16_HA
6353       || r_type == elfcpp::R_PPC64_GOT16_DS
6354       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6355     {
6356       if (gsym != NULL)
6357         {
6358           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6359           value = gsym->got_offset(GOT_TYPE_STANDARD);
6360         }
6361       else
6362         {
6363           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6364           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6365           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6366         }
6367       value -= target->got_section()->got_base_offset(object);
6368     }
6369   else if (r_type == elfcpp::R_PPC64_TOC)
6370     {
6371       value = (target->got_section()->output_section()->address()
6372                + object->toc_base_offset());
6373     }
6374   else if (gsym != NULL
6375            && (r_type == elfcpp::R_POWERPC_REL24
6376                || r_type == elfcpp::R_PPC_PLTREL24)
6377            && has_plt_value)
6378     {
6379       if (size == 64)
6380         {
6381           typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6382           Valtype* wv = reinterpret_cast<Valtype*>(view);
6383           bool can_plt_call = false;
6384           if (rela.get_r_offset() + 8 <= view_size)
6385             {
6386               Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6387               Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6388               if ((insn & 1) != 0
6389                   && (insn2 == nop
6390                       || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6391                 {
6392                   elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
6393                   can_plt_call = true;
6394                 }
6395             }
6396           if (!can_plt_call)
6397             {
6398               // If we don't have a branch and link followed by a nop,
6399               // we can't go via the plt because there is no place to
6400               // put a toc restoring instruction.
6401               // Unless we know we won't be returning.
6402               if (strcmp(gsym->name(), "__libc_start_main") == 0)
6403                 can_plt_call = true;
6404             }
6405           if (!can_plt_call)
6406             {
6407               // g++ as of 20130507 emits self-calls without a
6408               // following nop.  This is arguably wrong since we have
6409               // conflicting information.  On the one hand a global
6410               // symbol and on the other a local call sequence, but
6411               // don't error for this special case.
6412               // It isn't possible to cheaply verify we have exactly
6413               // such a call.  Allow all calls to the same section.
6414               bool ok = false;
6415               Address code = value;
6416               if (gsym->source() == Symbol::FROM_OBJECT
6417                   && gsym->object() == object)
6418                 {
6419                   Address addend = rela.get_r_addend();
6420                   unsigned int dest_shndx;
6421                   Address opdent = psymval->value(object, addend);
6422                   code = target->symval_for_branch(relinfo->symtab, opdent,
6423                                                    gsym, object, &dest_shndx);
6424                   bool is_ordinary;
6425                   if (dest_shndx == 0)
6426                     dest_shndx = gsym->shndx(&is_ordinary);
6427                   ok = dest_shndx == relinfo->data_shndx;
6428                 }
6429               if (!ok)
6430                 {
6431                   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6432                                          _("call lacks nop, can't restore toc; "
6433                                            "recompile with -fPIC"));
6434                   value = code;
6435                 }
6436             }
6437         }
6438     }
6439   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6440            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6441            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6442            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6443     {
6444       // First instruction of a global dynamic sequence, arg setup insn.
6445       const bool final = gsym == NULL || gsym->final_value_is_known();
6446       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6447       enum Got_type got_type = GOT_TYPE_STANDARD;
6448       if (tls_type == tls::TLSOPT_NONE)
6449         got_type = GOT_TYPE_TLSGD;
6450       else if (tls_type == tls::TLSOPT_TO_IE)
6451         got_type = GOT_TYPE_TPREL;
6452       if (got_type != GOT_TYPE_STANDARD)
6453         {
6454           if (gsym != NULL)
6455             {
6456               gold_assert(gsym->has_got_offset(got_type));
6457               value = gsym->got_offset(got_type);
6458             }
6459           else
6460             {
6461               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6462               gold_assert(object->local_has_got_offset(r_sym, got_type));
6463               value = object->local_got_offset(r_sym, got_type);
6464             }
6465           value -= target->got_section()->got_base_offset(object);
6466         }
6467       if (tls_type == tls::TLSOPT_TO_IE)
6468         {
6469           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6470               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6471             {
6472               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6473               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6474               insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6475               if (size == 32)
6476                 insn |= 32 << 26; // lwz
6477               else
6478                 insn |= 58 << 26; // ld
6479               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6480             }
6481           r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6482                      - elfcpp::R_POWERPC_GOT_TLSGD16);
6483         }
6484       else if (tls_type == tls::TLSOPT_TO_LE)
6485         {
6486           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6487               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6488             {
6489               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6490               Insn insn = addis_3_13;
6491               if (size == 32)
6492                 insn = addis_3_2;
6493               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6494               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6495               value = psymval->value(object, rela.get_r_addend());
6496             }
6497           else
6498             {
6499               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6500               Insn insn = nop;
6501               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6502               r_type = elfcpp::R_POWERPC_NONE;
6503             }
6504         }
6505     }
6506   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6507            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6508            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6509            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6510     {
6511       // First instruction of a local dynamic sequence, arg setup insn.
6512       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6513       if (tls_type == tls::TLSOPT_NONE)
6514         {
6515           value = target->tlsld_got_offset();
6516           value -= target->got_section()->got_base_offset(object);
6517         }
6518       else
6519         {
6520           gold_assert(tls_type == tls::TLSOPT_TO_LE);
6521           if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6522               || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6523             {
6524               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6525               Insn insn = addis_3_13;
6526               if (size == 32)
6527                 insn = addis_3_2;
6528               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6529               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6530               value = dtp_offset;
6531             }
6532           else
6533             {
6534               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6535               Insn insn = nop;
6536               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6537               r_type = elfcpp::R_POWERPC_NONE;
6538             }
6539         }
6540     }
6541   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6542            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6543            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6544            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6545     {
6546       // Accesses relative to a local dynamic sequence address,
6547       // no optimisation here.
6548       if (gsym != NULL)
6549         {
6550           gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6551           value = gsym->got_offset(GOT_TYPE_DTPREL);
6552         }
6553       else
6554         {
6555           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6556           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6557           value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6558         }
6559       value -= target->got_section()->got_base_offset(object);
6560     }
6561   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6562            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6563            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6564            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6565     {
6566       // First instruction of initial exec sequence.
6567       const bool final = gsym == NULL || gsym->final_value_is_known();
6568       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6569       if (tls_type == tls::TLSOPT_NONE)
6570         {
6571           if (gsym != NULL)
6572             {
6573               gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
6574               value = gsym->got_offset(GOT_TYPE_TPREL);
6575             }
6576           else
6577             {
6578               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6579               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
6580               value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
6581             }
6582           value -= target->got_section()->got_base_offset(object);
6583         }
6584       else
6585         {
6586           gold_assert(tls_type == tls::TLSOPT_TO_LE);
6587           if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6588               || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6589             {
6590               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6591               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6592               insn &= (1 << 26) - (1 << 21); // extract rt from ld
6593               if (size == 32)
6594                 insn |= addis_0_2;
6595               else
6596                 insn |= addis_0_13;
6597               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6598               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6599               value = psymval->value(object, rela.get_r_addend());
6600             }
6601           else
6602             {
6603               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6604               Insn insn = nop;
6605               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6606               r_type = elfcpp::R_POWERPC_NONE;
6607             }
6608         }
6609     }
6610   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6611            || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6612     {
6613       // Second instruction of a global dynamic sequence,
6614       // the __tls_get_addr call
6615       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6616       const bool final = gsym == NULL || gsym->final_value_is_known();
6617       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6618       if (tls_type != tls::TLSOPT_NONE)
6619         {
6620           if (tls_type == tls::TLSOPT_TO_IE)
6621             {
6622               Insn* iview = reinterpret_cast<Insn*>(view);
6623               Insn insn = add_3_3_13;
6624               if (size == 32)
6625                 insn = add_3_3_2;
6626               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6627               r_type = elfcpp::R_POWERPC_NONE;
6628             }
6629           else
6630             {
6631               Insn* iview = reinterpret_cast<Insn*>(view);
6632               Insn insn = addi_3_3;
6633               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6634               r_type = elfcpp::R_POWERPC_TPREL16_LO;
6635               view += 2 * big_endian;
6636               value = psymval->value(object, rela.get_r_addend());
6637             }
6638           this->skip_next_tls_get_addr_call();
6639         }
6640     }
6641   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6642            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6643     {
6644       // Second instruction of a local dynamic sequence,
6645       // the __tls_get_addr call
6646       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6647       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6648       if (tls_type == tls::TLSOPT_TO_LE)
6649         {
6650           Insn* iview = reinterpret_cast<Insn*>(view);
6651           Insn insn = addi_3_3;
6652           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6653           this->skip_next_tls_get_addr_call();
6654           r_type = elfcpp::R_POWERPC_TPREL16_LO;
6655           view += 2 * big_endian;
6656           value = dtp_offset;
6657         }
6658     }
6659   else if (r_type == elfcpp::R_POWERPC_TLS)
6660     {
6661       // Second instruction of an initial exec sequence
6662       const bool final = gsym == NULL || gsym->final_value_is_known();
6663       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6664       if (tls_type == tls::TLSOPT_TO_LE)
6665         {
6666           Insn* iview = reinterpret_cast<Insn*>(view);
6667           Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6668           unsigned int reg = size == 32 ? 2 : 13;
6669           insn = at_tls_transform(insn, reg);
6670           gold_assert(insn != 0);
6671           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6672           r_type = elfcpp::R_POWERPC_TPREL16_LO;
6673           view += 2 * big_endian;
6674           value = psymval->value(object, rela.get_r_addend());
6675         }
6676     }
6677   else if (!has_plt_value)
6678     {
6679       Address addend = 0;
6680       unsigned int dest_shndx;
6681       if (r_type != elfcpp::R_PPC_PLTREL24)
6682         addend = rela.get_r_addend();
6683       value = psymval->value(object, addend);
6684       if (size == 64 && is_branch_reloc(r_type))
6685         value = target->symval_for_branch(relinfo->symtab, value,
6686                                           gsym, object, &dest_shndx);
6687       unsigned int max_branch_offset = 0;
6688       if (r_type == elfcpp::R_POWERPC_REL24
6689           || r_type == elfcpp::R_PPC_PLTREL24
6690           || r_type == elfcpp::R_PPC_LOCAL24PC)
6691         max_branch_offset = 1 << 25;
6692       else if (r_type == elfcpp::R_POWERPC_REL14
6693                || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
6694                || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
6695         max_branch_offset = 1 << 15;
6696       if (max_branch_offset != 0
6697           && value - address + max_branch_offset >= 2 * max_branch_offset)
6698         {
6699           Stub_table<size, big_endian>* stub_table
6700             = object->stub_table(relinfo->data_shndx);
6701           if (stub_table != NULL)
6702             {
6703               Address off = stub_table->find_long_branch_entry(object, value);
6704               if (off != invalid_address)
6705                 value = (stub_table->stub_address() + stub_table->plt_size()
6706                          + off);
6707             }
6708         }
6709     }
6710
6711   switch (r_type)
6712     {
6713     case elfcpp::R_PPC64_REL64:
6714     case elfcpp::R_POWERPC_REL32:
6715     case elfcpp::R_POWERPC_REL24:
6716     case elfcpp::R_PPC_PLTREL24:
6717     case elfcpp::R_PPC_LOCAL24PC:
6718     case elfcpp::R_POWERPC_REL16:
6719     case elfcpp::R_POWERPC_REL16_LO:
6720     case elfcpp::R_POWERPC_REL16_HI:
6721     case elfcpp::R_POWERPC_REL16_HA:
6722     case elfcpp::R_POWERPC_REL14:
6723     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6724     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6725       value -= address;
6726       break;
6727
6728     case elfcpp::R_PPC64_TOC16:
6729     case elfcpp::R_PPC64_TOC16_LO:
6730     case elfcpp::R_PPC64_TOC16_HI:
6731     case elfcpp::R_PPC64_TOC16_HA:
6732     case elfcpp::R_PPC64_TOC16_DS:
6733     case elfcpp::R_PPC64_TOC16_LO_DS:
6734       // Subtract the TOC base address.
6735       value -= (target->got_section()->output_section()->address()
6736                 + object->toc_base_offset());
6737       break;
6738
6739     case elfcpp::R_POWERPC_SECTOFF:
6740     case elfcpp::R_POWERPC_SECTOFF_LO:
6741     case elfcpp::R_POWERPC_SECTOFF_HI:
6742     case elfcpp::R_POWERPC_SECTOFF_HA:
6743     case elfcpp::R_PPC64_SECTOFF_DS:
6744     case elfcpp::R_PPC64_SECTOFF_LO_DS:
6745       if (os != NULL)
6746         value -= os->address();
6747       break;
6748
6749     case elfcpp::R_PPC64_TPREL16_DS:
6750     case elfcpp::R_PPC64_TPREL16_LO_DS:
6751       if (size != 64)
6752         // R_PPC_TLSGD and R_PPC_TLSLD
6753         break;
6754     case elfcpp::R_POWERPC_TPREL16:
6755     case elfcpp::R_POWERPC_TPREL16_LO:
6756     case elfcpp::R_POWERPC_TPREL16_HI:
6757     case elfcpp::R_POWERPC_TPREL16_HA:
6758     case elfcpp::R_POWERPC_TPREL:
6759     case elfcpp::R_PPC64_TPREL16_HIGHER:
6760     case elfcpp::R_PPC64_TPREL16_HIGHERA:
6761     case elfcpp::R_PPC64_TPREL16_HIGHEST:
6762     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6763       // tls symbol values are relative to tls_segment()->vaddr()
6764       value -= tp_offset;
6765       break;
6766
6767     case elfcpp::R_PPC64_DTPREL16_DS:
6768     case elfcpp::R_PPC64_DTPREL16_LO_DS:
6769     case elfcpp::R_PPC64_DTPREL16_HIGHER:
6770     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6771     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6772     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6773       if (size != 64)
6774         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6775         // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6776         break;
6777     case elfcpp::R_POWERPC_DTPREL16:
6778     case elfcpp::R_POWERPC_DTPREL16_LO:
6779     case elfcpp::R_POWERPC_DTPREL16_HI:
6780     case elfcpp::R_POWERPC_DTPREL16_HA:
6781     case elfcpp::R_POWERPC_DTPREL:
6782       // tls symbol values are relative to tls_segment()->vaddr()
6783       value -= dtp_offset;
6784       break;
6785
6786     default:
6787       break;
6788     }
6789
6790   Insn branch_bit = 0;
6791   switch (r_type)
6792     {
6793     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6794     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6795       branch_bit = 1 << 21;
6796     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6797     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6798       {
6799         Insn* iview = reinterpret_cast<Insn*>(view);
6800         Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6801         insn &= ~(1 << 21);
6802         insn |= branch_bit;
6803         if (this->is_isa_v2)
6804           {
6805             // Set 'a' bit.  This is 0b00010 in BO field for branch
6806             // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6807             // for branch on CTR insns (BO == 1a00t or 1a01t).
6808             if ((insn & (0x14 << 21)) == (0x04 << 21))
6809               insn |= 0x02 << 21;
6810             else if ((insn & (0x14 << 21)) == (0x10 << 21))
6811               insn |= 0x08 << 21;
6812             else
6813               break;
6814           }
6815         else
6816           {
6817             // Invert 'y' bit if not the default.
6818             if (static_cast<Signed_address>(value) < 0)
6819               insn ^= 1 << 21;
6820           }
6821         elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6822       }
6823       break;
6824
6825     default:
6826       break;
6827     }
6828
6829   if (size == 64)
6830     {
6831       // Multi-instruction sequences that access the TOC can be
6832       // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6833       // to             nop;           addi rb,r2,x;
6834       switch (r_type)
6835         {
6836         default:
6837           break;
6838
6839         case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6840         case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6841         case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6842         case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6843         case elfcpp::R_POWERPC_GOT16_HA:
6844         case elfcpp::R_PPC64_TOC16_HA:
6845           if (parameters->options().toc_optimize())
6846             {
6847               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6848               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6849               if ((insn & ((0x3f << 26) | 0x1f << 16))
6850                   != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6851                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6852                                        _("toc optimization is not supported "
6853                                          "for %#08x instruction"), insn);
6854               else if (value + 0x8000 < 0x10000)
6855                 {
6856                   elfcpp::Swap<32, big_endian>::writeval(iview, nop);
6857                   return true;
6858                 }
6859             }
6860           break;
6861
6862         case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6863         case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6864         case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6865         case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6866         case elfcpp::R_POWERPC_GOT16_LO:
6867         case elfcpp::R_PPC64_GOT16_LO_DS:
6868         case elfcpp::R_PPC64_TOC16_LO:
6869         case elfcpp::R_PPC64_TOC16_LO_DS:
6870           if (parameters->options().toc_optimize())
6871             {
6872               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6873               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6874               if (!ok_lo_toc_insn(insn))
6875                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6876                                        _("toc optimization is not supported "
6877                                          "for %#08x instruction"), insn);
6878               else if (value + 0x8000 < 0x10000)
6879                 {
6880                   if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
6881                     {
6882                       // Transform addic to addi when we change reg.
6883                       insn &= ~((0x3f << 26) | (0x1f << 16));
6884                       insn |= (14u << 26) | (2 << 16);
6885                     }
6886                   else
6887                     {
6888                       insn &= ~(0x1f << 16);
6889                       insn |= 2 << 16;
6890                     }
6891                   elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6892                 }
6893             }
6894           break;
6895         }
6896     }
6897
6898   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
6899   switch (r_type)
6900     {
6901     case elfcpp::R_POWERPC_ADDR32:
6902     case elfcpp::R_POWERPC_UADDR32:
6903       if (size == 64)
6904         overflow = Reloc::CHECK_BITFIELD;
6905       break;
6906
6907     case elfcpp::R_POWERPC_REL32:
6908       if (size == 64)
6909         overflow = Reloc::CHECK_SIGNED;
6910       break;
6911
6912     case elfcpp::R_POWERPC_ADDR24:
6913     case elfcpp::R_POWERPC_ADDR16:
6914     case elfcpp::R_POWERPC_UADDR16:
6915     case elfcpp::R_PPC64_ADDR16_DS:
6916     case elfcpp::R_POWERPC_ADDR14:
6917     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6918     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6919       overflow = Reloc::CHECK_BITFIELD;
6920       break;
6921
6922     case elfcpp::R_POWERPC_REL24:
6923     case elfcpp::R_PPC_PLTREL24:
6924     case elfcpp::R_PPC_LOCAL24PC:
6925     case elfcpp::R_POWERPC_REL16:
6926     case elfcpp::R_PPC64_TOC16:
6927     case elfcpp::R_POWERPC_GOT16:
6928     case elfcpp::R_POWERPC_SECTOFF:
6929     case elfcpp::R_POWERPC_TPREL16:
6930     case elfcpp::R_POWERPC_DTPREL16:
6931     case elfcpp::R_PPC64_TPREL16_DS:
6932     case elfcpp::R_PPC64_DTPREL16_DS:
6933     case elfcpp::R_PPC64_TOC16_DS:
6934     case elfcpp::R_PPC64_GOT16_DS:
6935     case elfcpp::R_PPC64_SECTOFF_DS:
6936     case elfcpp::R_POWERPC_REL14:
6937     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6938     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6939     case elfcpp::R_POWERPC_GOT_TLSGD16:
6940     case elfcpp::R_POWERPC_GOT_TLSLD16:
6941     case elfcpp::R_POWERPC_GOT_TPREL16:
6942     case elfcpp::R_POWERPC_GOT_DTPREL16:
6943       overflow = Reloc::CHECK_SIGNED;
6944       break;
6945     }
6946
6947   typename Powerpc_relocate_functions<size, big_endian>::Status status
6948     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
6949   switch (r_type)
6950     {
6951     case elfcpp::R_POWERPC_NONE:
6952     case elfcpp::R_POWERPC_TLS:
6953     case elfcpp::R_POWERPC_GNU_VTINHERIT:
6954     case elfcpp::R_POWERPC_GNU_VTENTRY:
6955     case elfcpp::R_PPC_EMB_MRKREF:
6956       break;
6957
6958     case elfcpp::R_PPC64_ADDR64:
6959     case elfcpp::R_PPC64_REL64:
6960     case elfcpp::R_PPC64_TOC:
6961       Reloc::addr64(view, value);
6962       break;
6963
6964     case elfcpp::R_POWERPC_TPREL:
6965     case elfcpp::R_POWERPC_DTPREL:
6966       if (size == 64)
6967         Reloc::addr64(view, value);
6968       else
6969         status = Reloc::addr32(view, value, overflow);
6970       break;
6971
6972     case elfcpp::R_PPC64_UADDR64:
6973       Reloc::addr64_u(view, value);
6974       break;
6975
6976     case elfcpp::R_POWERPC_ADDR32:
6977       status = Reloc::addr32(view, value, overflow);
6978       break;
6979
6980     case elfcpp::R_POWERPC_REL32:
6981     case elfcpp::R_POWERPC_UADDR32:
6982       status = Reloc::addr32_u(view, value, overflow);
6983       break;
6984
6985     case elfcpp::R_POWERPC_ADDR24:
6986     case elfcpp::R_POWERPC_REL24:
6987     case elfcpp::R_PPC_PLTREL24:
6988     case elfcpp::R_PPC_LOCAL24PC:
6989       status = Reloc::addr24(view, value, overflow);
6990       break;
6991
6992     case elfcpp::R_POWERPC_GOT_DTPREL16:
6993     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6994       if (size == 64)
6995         {
6996           status = Reloc::addr16_ds(view, value, overflow);
6997           break;
6998         }
6999     case elfcpp::R_POWERPC_ADDR16:
7000     case elfcpp::R_POWERPC_REL16:
7001     case elfcpp::R_PPC64_TOC16:
7002     case elfcpp::R_POWERPC_GOT16:
7003     case elfcpp::R_POWERPC_SECTOFF:
7004     case elfcpp::R_POWERPC_TPREL16:
7005     case elfcpp::R_POWERPC_DTPREL16:
7006     case elfcpp::R_POWERPC_GOT_TLSGD16:
7007     case elfcpp::R_POWERPC_GOT_TLSLD16:
7008     case elfcpp::R_POWERPC_GOT_TPREL16:
7009     case elfcpp::R_POWERPC_ADDR16_LO:
7010     case elfcpp::R_POWERPC_REL16_LO:
7011     case elfcpp::R_PPC64_TOC16_LO:
7012     case elfcpp::R_POWERPC_GOT16_LO:
7013     case elfcpp::R_POWERPC_SECTOFF_LO:
7014     case elfcpp::R_POWERPC_TPREL16_LO:
7015     case elfcpp::R_POWERPC_DTPREL16_LO:
7016     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7017     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7018     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7019       status = Reloc::addr16(view, value, overflow);
7020       break;
7021
7022     case elfcpp::R_POWERPC_UADDR16:
7023       status = Reloc::addr16_u(view, value, overflow);
7024       break;
7025
7026     case elfcpp::R_POWERPC_ADDR16_HI:
7027     case elfcpp::R_POWERPC_REL16_HI:
7028     case elfcpp::R_PPC64_TOC16_HI:
7029     case elfcpp::R_POWERPC_GOT16_HI:
7030     case elfcpp::R_POWERPC_SECTOFF_HI:
7031     case elfcpp::R_POWERPC_TPREL16_HI:
7032     case elfcpp::R_POWERPC_DTPREL16_HI:
7033     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7034     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7035     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7036     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7037       Reloc::addr16_hi(view, value);
7038       break;
7039
7040     case elfcpp::R_POWERPC_ADDR16_HA:
7041     case elfcpp::R_POWERPC_REL16_HA:
7042     case elfcpp::R_PPC64_TOC16_HA:
7043     case elfcpp::R_POWERPC_GOT16_HA:
7044     case elfcpp::R_POWERPC_SECTOFF_HA:
7045     case elfcpp::R_POWERPC_TPREL16_HA:
7046     case elfcpp::R_POWERPC_DTPREL16_HA:
7047     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7048     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7049     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7050     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7051       Reloc::addr16_ha(view, value);
7052       break;
7053
7054     case elfcpp::R_PPC64_DTPREL16_HIGHER:
7055       if (size == 32)
7056         // R_PPC_EMB_NADDR16_LO
7057         goto unsupp;
7058     case elfcpp::R_PPC64_ADDR16_HIGHER:
7059     case elfcpp::R_PPC64_TPREL16_HIGHER:
7060       Reloc::addr16_hi2(view, value);
7061       break;
7062
7063     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7064       if (size == 32)
7065         // R_PPC_EMB_NADDR16_HI
7066         goto unsupp;
7067     case elfcpp::R_PPC64_ADDR16_HIGHERA:
7068     case elfcpp::R_PPC64_TPREL16_HIGHERA:
7069       Reloc::addr16_ha2(view, value);
7070       break;
7071
7072     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7073       if (size == 32)
7074         // R_PPC_EMB_NADDR16_HA
7075         goto unsupp;
7076     case elfcpp::R_PPC64_ADDR16_HIGHEST:
7077     case elfcpp::R_PPC64_TPREL16_HIGHEST:
7078       Reloc::addr16_hi3(view, value);
7079       break;
7080
7081     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7082       if (size == 32)
7083         // R_PPC_EMB_SDAI16
7084         goto unsupp;
7085     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7086     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7087       Reloc::addr16_ha3(view, value);
7088       break;
7089
7090     case elfcpp::R_PPC64_DTPREL16_DS:
7091     case elfcpp::R_PPC64_DTPREL16_LO_DS:
7092       if (size == 32)
7093         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7094         goto unsupp;
7095     case elfcpp::R_PPC64_TPREL16_DS:
7096     case elfcpp::R_PPC64_TPREL16_LO_DS:
7097       if (size == 32)
7098         // R_PPC_TLSGD, R_PPC_TLSLD
7099         break;
7100     case elfcpp::R_PPC64_ADDR16_DS:
7101     case elfcpp::R_PPC64_ADDR16_LO_DS:
7102     case elfcpp::R_PPC64_TOC16_DS:
7103     case elfcpp::R_PPC64_TOC16_LO_DS:
7104     case elfcpp::R_PPC64_GOT16_DS:
7105     case elfcpp::R_PPC64_GOT16_LO_DS:
7106     case elfcpp::R_PPC64_SECTOFF_DS:
7107     case elfcpp::R_PPC64_SECTOFF_LO_DS:
7108       status = Reloc::addr16_ds(view, value, overflow);
7109       break;
7110
7111     case elfcpp::R_POWERPC_ADDR14:
7112     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7113     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7114     case elfcpp::R_POWERPC_REL14:
7115     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7116     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7117       status = Reloc::addr14(view, value, overflow);
7118       break;
7119
7120     case elfcpp::R_POWERPC_COPY:
7121     case elfcpp::R_POWERPC_GLOB_DAT:
7122     case elfcpp::R_POWERPC_JMP_SLOT:
7123     case elfcpp::R_POWERPC_RELATIVE:
7124     case elfcpp::R_POWERPC_DTPMOD:
7125     case elfcpp::R_PPC64_JMP_IREL:
7126     case elfcpp::R_POWERPC_IRELATIVE:
7127       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7128                              _("unexpected reloc %u in object file"),
7129                              r_type);
7130       break;
7131
7132     case elfcpp::R_PPC_EMB_SDA21:
7133       if (size == 32)
7134         goto unsupp;
7135       else
7136         {
7137           // R_PPC64_TOCSAVE.  For the time being this can be ignored.
7138         }
7139       break;
7140
7141     case elfcpp::R_PPC_EMB_SDA2I16:
7142     case elfcpp::R_PPC_EMB_SDA2REL:
7143       if (size == 32)
7144         goto unsupp;
7145       // R_PPC64_TLSGD, R_PPC64_TLSLD
7146       break;
7147
7148     case elfcpp::R_POWERPC_PLT32:
7149     case elfcpp::R_POWERPC_PLTREL32:
7150     case elfcpp::R_POWERPC_PLT16_LO:
7151     case elfcpp::R_POWERPC_PLT16_HI:
7152     case elfcpp::R_POWERPC_PLT16_HA:
7153     case elfcpp::R_PPC_SDAREL16:
7154     case elfcpp::R_POWERPC_ADDR30:
7155     case elfcpp::R_PPC64_PLT64:
7156     case elfcpp::R_PPC64_PLTREL64:
7157     case elfcpp::R_PPC64_PLTGOT16:
7158     case elfcpp::R_PPC64_PLTGOT16_LO:
7159     case elfcpp::R_PPC64_PLTGOT16_HI:
7160     case elfcpp::R_PPC64_PLTGOT16_HA:
7161     case elfcpp::R_PPC64_PLT16_LO_DS:
7162     case elfcpp::R_PPC64_PLTGOT16_DS:
7163     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7164     case elfcpp::R_PPC_EMB_RELSEC16:
7165     case elfcpp::R_PPC_EMB_RELST_LO:
7166     case elfcpp::R_PPC_EMB_RELST_HI:
7167     case elfcpp::R_PPC_EMB_RELST_HA:
7168     case elfcpp::R_PPC_EMB_BIT_FLD:
7169     case elfcpp::R_PPC_EMB_RELSDA:
7170     case elfcpp::R_PPC_TOC16:
7171     default:
7172     unsupp:
7173       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7174                              _("unsupported reloc %u"),
7175                              r_type);
7176       break;
7177     }
7178   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
7179     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7180                            _("relocation overflow"));
7181
7182   return true;
7183 }
7184
7185 // Relocate section data.
7186
7187 template<int size, bool big_endian>
7188 void
7189 Target_powerpc<size, big_endian>::relocate_section(
7190     const Relocate_info<size, big_endian>* relinfo,
7191     unsigned int sh_type,
7192     const unsigned char* prelocs,
7193     size_t reloc_count,
7194     Output_section* output_section,
7195     bool needs_special_offset_handling,
7196     unsigned char* view,
7197     Address address,
7198     section_size_type view_size,
7199     const Reloc_symbol_changes* reloc_symbol_changes)
7200 {
7201   typedef Target_powerpc<size, big_endian> Powerpc;
7202   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7203   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7204     Powerpc_comdat_behavior;
7205
7206   gold_assert(sh_type == elfcpp::SHT_RELA);
7207
7208   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7209                          Powerpc_relocate, Powerpc_comdat_behavior>(
7210     relinfo,
7211     this,
7212     prelocs,
7213     reloc_count,
7214     output_section,
7215     needs_special_offset_handling,
7216     view,
7217     address,
7218     view_size,
7219     reloc_symbol_changes);
7220 }
7221
7222 class Powerpc_scan_relocatable_reloc
7223 {
7224 public:
7225   // Return the strategy to use for a local symbol which is not a
7226   // section symbol, given the relocation type.
7227   inline Relocatable_relocs::Reloc_strategy
7228   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7229   {
7230     if (r_type == 0 && r_sym == 0)
7231       return Relocatable_relocs::RELOC_DISCARD;
7232     return Relocatable_relocs::RELOC_COPY;
7233   }
7234
7235   // Return the strategy to use for a local symbol which is a section
7236   // symbol, given the relocation type.
7237   inline Relocatable_relocs::Reloc_strategy
7238   local_section_strategy(unsigned int, Relobj*)
7239   {
7240     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7241   }
7242
7243   // Return the strategy to use for a global symbol, given the
7244   // relocation type, the object, and the symbol index.
7245   inline Relocatable_relocs::Reloc_strategy
7246   global_strategy(unsigned int r_type, Relobj*, unsigned int)
7247   {
7248     if (r_type == elfcpp::R_PPC_PLTREL24)
7249       return Relocatable_relocs::RELOC_SPECIAL;
7250     return Relocatable_relocs::RELOC_COPY;
7251   }
7252 };
7253
7254 // Scan the relocs during a relocatable link.
7255
7256 template<int size, bool big_endian>
7257 void
7258 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7259     Symbol_table* symtab,
7260     Layout* layout,
7261     Sized_relobj_file<size, big_endian>* object,
7262     unsigned int data_shndx,
7263     unsigned int sh_type,
7264     const unsigned char* prelocs,
7265     size_t reloc_count,
7266     Output_section* output_section,
7267     bool needs_special_offset_handling,
7268     size_t local_symbol_count,
7269     const unsigned char* plocal_symbols,
7270     Relocatable_relocs* rr)
7271 {
7272   gold_assert(sh_type == elfcpp::SHT_RELA);
7273
7274   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7275                                 Powerpc_scan_relocatable_reloc>(
7276     symtab,
7277     layout,
7278     object,
7279     data_shndx,
7280     prelocs,
7281     reloc_count,
7282     output_section,
7283     needs_special_offset_handling,
7284     local_symbol_count,
7285     plocal_symbols,
7286     rr);
7287 }
7288
7289 // Emit relocations for a section.
7290 // This is a modified version of the function by the same name in
7291 // target-reloc.h.  Using relocate_special_relocatable for
7292 // R_PPC_PLTREL24 would require duplication of the entire body of the
7293 // loop, so we may as well duplicate the whole thing.
7294
7295 template<int size, bool big_endian>
7296 void
7297 Target_powerpc<size, big_endian>::relocate_relocs(
7298     const Relocate_info<size, big_endian>* relinfo,
7299     unsigned int sh_type,
7300     const unsigned char* prelocs,
7301     size_t reloc_count,
7302     Output_section* output_section,
7303     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7304     const Relocatable_relocs* rr,
7305     unsigned char*,
7306     Address view_address,
7307     section_size_type,
7308     unsigned char* reloc_view,
7309     section_size_type reloc_view_size)
7310 {
7311   gold_assert(sh_type == elfcpp::SHT_RELA);
7312
7313   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7314     Reltype;
7315   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7316     Reltype_write;
7317   const int reloc_size
7318     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7319
7320   Powerpc_relobj<size, big_endian>* const object
7321     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7322   const unsigned int local_count = object->local_symbol_count();
7323   unsigned int got2_shndx = object->got2_shndx();
7324   Address got2_addend = 0;
7325   if (got2_shndx != 0)
7326     {
7327       got2_addend = object->get_output_section_offset(got2_shndx);
7328       gold_assert(got2_addend != invalid_address);
7329     }
7330
7331   unsigned char* pwrite = reloc_view;
7332   bool zap_next = false;
7333   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7334     {
7335       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7336       if (strategy == Relocatable_relocs::RELOC_DISCARD)
7337         continue;
7338
7339       Reltype reloc(prelocs);
7340       Reltype_write reloc_write(pwrite);
7341
7342       Address offset = reloc.get_r_offset();
7343       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7344       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7345       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7346       const unsigned int orig_r_sym = r_sym;
7347       typename elfcpp::Elf_types<size>::Elf_Swxword addend
7348         = reloc.get_r_addend();
7349       const Symbol* gsym = NULL;
7350
7351       if (zap_next)
7352         {
7353           // We could arrange to discard these and other relocs for
7354           // tls optimised sequences in the strategy methods, but for
7355           // now do as BFD ld does.
7356           r_type = elfcpp::R_POWERPC_NONE;
7357           zap_next = false;
7358         }
7359
7360       // Get the new symbol index.
7361       if (r_sym < local_count)
7362         {
7363           switch (strategy)
7364             {
7365             case Relocatable_relocs::RELOC_COPY:
7366             case Relocatable_relocs::RELOC_SPECIAL:
7367               if (r_sym != 0)
7368                 {
7369                   r_sym = object->symtab_index(r_sym);
7370                   gold_assert(r_sym != -1U);
7371                 }
7372               break;
7373
7374             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7375               {
7376                 // We are adjusting a section symbol.  We need to find
7377                 // the symbol table index of the section symbol for
7378                 // the output section corresponding to input section
7379                 // in which this symbol is defined.
7380                 gold_assert(r_sym < local_count);
7381                 bool is_ordinary;
7382                 unsigned int shndx =
7383                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
7384                 gold_assert(is_ordinary);
7385                 Output_section* os = object->output_section(shndx);
7386                 gold_assert(os != NULL);
7387                 gold_assert(os->needs_symtab_index());
7388                 r_sym = os->symtab_index();
7389               }
7390               break;
7391
7392             default:
7393               gold_unreachable();
7394             }
7395         }
7396       else
7397         {
7398           gsym = object->global_symbol(r_sym);
7399           gold_assert(gsym != NULL);
7400           if (gsym->is_forwarder())
7401             gsym = relinfo->symtab->resolve_forwards(gsym);
7402
7403           gold_assert(gsym->has_symtab_index());
7404           r_sym = gsym->symtab_index();
7405         }
7406
7407       // Get the new offset--the location in the output section where
7408       // this relocation should be applied.
7409       if (static_cast<Address>(offset_in_output_section) != invalid_address)
7410         offset += offset_in_output_section;
7411       else
7412         {
7413           section_offset_type sot_offset =
7414             convert_types<section_offset_type, Address>(offset);
7415           section_offset_type new_sot_offset =
7416             output_section->output_offset(object, relinfo->data_shndx,
7417                                           sot_offset);
7418           gold_assert(new_sot_offset != -1);
7419           offset = new_sot_offset;
7420         }
7421
7422       // In an object file, r_offset is an offset within the section.
7423       // In an executable or dynamic object, generated by
7424       // --emit-relocs, r_offset is an absolute address.
7425       if (!parameters->options().relocatable())
7426         {
7427           offset += view_address;
7428           if (static_cast<Address>(offset_in_output_section) != invalid_address)
7429             offset -= offset_in_output_section;
7430         }
7431
7432       // Handle the reloc addend based on the strategy.
7433       if (strategy == Relocatable_relocs::RELOC_COPY)
7434         ;
7435       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7436         {
7437           const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7438           addend = psymval->value(object, addend);
7439         }
7440       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7441         {
7442           if (addend >= 32768)
7443             addend += got2_addend;
7444         }
7445       else
7446         gold_unreachable();
7447
7448       if (!parameters->options().relocatable())
7449         {
7450           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7451               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7452               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7453               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7454             {
7455               // First instruction of a global dynamic sequence,
7456               // arg setup insn.
7457               const bool final = gsym == NULL || gsym->final_value_is_known();
7458               switch (this->optimize_tls_gd(final))
7459                 {
7460                 case tls::TLSOPT_TO_IE:
7461                   r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7462                              - elfcpp::R_POWERPC_GOT_TLSGD16);
7463                   break;
7464                 case tls::TLSOPT_TO_LE:
7465                   if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7466                       || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7467                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
7468                   else
7469                     {
7470                       r_type = elfcpp::R_POWERPC_NONE;
7471                       offset -= 2 * big_endian;
7472                     }
7473                   break;
7474                 default:
7475                   break;
7476                 }
7477             }
7478           else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7479                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7480                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7481                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7482             {
7483               // First instruction of a local dynamic sequence,
7484               // arg setup insn.
7485               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7486                 {
7487                   if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7488                       || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7489                     {
7490                       r_type = elfcpp::R_POWERPC_TPREL16_HA;
7491                       const Output_section* os = relinfo->layout->tls_segment()
7492                         ->first_section();
7493                       gold_assert(os != NULL);
7494                       gold_assert(os->needs_symtab_index());
7495                       r_sym = os->symtab_index();
7496                       addend = dtp_offset;
7497                     }
7498                   else
7499                     {
7500                       r_type = elfcpp::R_POWERPC_NONE;
7501                       offset -= 2 * big_endian;
7502                     }
7503                 }
7504             }
7505           else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7506                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7507                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7508                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7509             {
7510               // First instruction of initial exec sequence.
7511               const bool final = gsym == NULL || gsym->final_value_is_known();
7512               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7513                 {
7514                   if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7515                       || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7516                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
7517                   else
7518                     {
7519                       r_type = elfcpp::R_POWERPC_NONE;
7520                       offset -= 2 * big_endian;
7521                     }
7522                 }
7523             }
7524           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7525                    || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7526             {
7527               // Second instruction of a global dynamic sequence,
7528               // the __tls_get_addr call
7529               const bool final = gsym == NULL || gsym->final_value_is_known();
7530               switch (this->optimize_tls_gd(final))
7531                 {
7532                 case tls::TLSOPT_TO_IE:
7533                   r_type = elfcpp::R_POWERPC_NONE;
7534                   zap_next = true;
7535                   break;
7536                 case tls::TLSOPT_TO_LE:
7537                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
7538                   offset += 2 * big_endian;
7539                   zap_next = true;
7540                   break;
7541                 default:
7542                   break;
7543                 }
7544             }
7545           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7546                    || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7547             {
7548               // Second instruction of a local dynamic sequence,
7549               // the __tls_get_addr call
7550               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7551                 {
7552                   const Output_section* os = relinfo->layout->tls_segment()
7553                     ->first_section();
7554                   gold_assert(os != NULL);
7555                   gold_assert(os->needs_symtab_index());
7556                   r_sym = os->symtab_index();
7557                   addend = dtp_offset;
7558                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
7559                   offset += 2 * big_endian;
7560                   zap_next = true;
7561                 }
7562             }
7563           else if (r_type == elfcpp::R_POWERPC_TLS)
7564             {
7565               // Second instruction of an initial exec sequence
7566               const bool final = gsym == NULL || gsym->final_value_is_known();
7567               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7568                 {
7569                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
7570                   offset += 2 * big_endian;
7571                 }
7572             }
7573         }
7574
7575       reloc_write.put_r_offset(offset);
7576       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
7577       reloc_write.put_r_addend(addend);
7578
7579       pwrite += reloc_size;
7580     }
7581
7582   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
7583               == reloc_view_size);
7584 }
7585
7586 // Return the value to use for a dynamic symbol which requires special
7587 // treatment.  This is how we support equality comparisons of function
7588 // pointers across shared library boundaries, as described in the
7589 // processor specific ABI supplement.
7590
7591 template<int size, bool big_endian>
7592 uint64_t
7593 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7594 {
7595   if (size == 32)
7596     {
7597       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7598       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7599            p != this->stub_tables_.end();
7600            ++p)
7601         {
7602           Address off = (*p)->find_plt_call_entry(gsym);
7603           if (off != invalid_address)
7604             return (*p)->stub_address() + off;
7605         }
7606     }
7607   gold_unreachable();
7608 }
7609
7610 // Return the PLT address to use for a local symbol.
7611 template<int size, bool big_endian>
7612 uint64_t
7613 Target_powerpc<size, big_endian>::do_plt_address_for_local(
7614     const Relobj* object,
7615     unsigned int symndx) const
7616 {
7617   if (size == 32)
7618     {
7619       const Sized_relobj<size, big_endian>* relobj
7620         = static_cast<const Sized_relobj<size, big_endian>*>(object);
7621       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7622            p != this->stub_tables_.end();
7623            ++p)
7624         {
7625           Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
7626                                                   symndx);
7627           if (off != invalid_address)
7628             return (*p)->stub_address() + off;
7629         }
7630     }
7631   gold_unreachable();
7632 }
7633
7634 // Return the PLT address to use for a global symbol.
7635 template<int size, bool big_endian>
7636 uint64_t
7637 Target_powerpc<size, big_endian>::do_plt_address_for_global(
7638     const Symbol* gsym) const
7639 {
7640   if (size == 32)
7641     {
7642       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7643            p != this->stub_tables_.end();
7644            ++p)
7645         {
7646           Address off = (*p)->find_plt_call_entry(gsym);
7647           if (off != invalid_address)
7648             return (*p)->stub_address() + off;
7649         }
7650     }
7651   gold_unreachable();
7652 }
7653
7654 // Return the offset to use for the GOT_INDX'th got entry which is
7655 // for a local tls symbol specified by OBJECT, SYMNDX.
7656 template<int size, bool big_endian>
7657 int64_t
7658 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
7659     const Relobj* object,
7660     unsigned int symndx,
7661     unsigned int got_indx) const
7662 {
7663   const Powerpc_relobj<size, big_endian>* ppc_object
7664     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
7665   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
7666     {
7667       for (Got_type got_type = GOT_TYPE_TLSGD;
7668            got_type <= GOT_TYPE_TPREL;
7669            got_type = Got_type(got_type + 1))
7670         if (ppc_object->local_has_got_offset(symndx, got_type))
7671           {
7672             unsigned int off = ppc_object->local_got_offset(symndx, got_type);
7673             if (got_type == GOT_TYPE_TLSGD)
7674               off += size / 8;
7675             if (off == got_indx * (size / 8))
7676               {
7677                 if (got_type == GOT_TYPE_TPREL)
7678                   return -tp_offset;
7679                 else
7680                   return -dtp_offset;
7681               }
7682           }
7683     }
7684   gold_unreachable();
7685 }
7686
7687 // Return the offset to use for the GOT_INDX'th got entry which is
7688 // for global tls symbol GSYM.
7689 template<int size, bool big_endian>
7690 int64_t
7691 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
7692     Symbol* gsym,
7693     unsigned int got_indx) const
7694 {
7695   if (gsym->type() == elfcpp::STT_TLS)
7696     {
7697       for (Got_type got_type = GOT_TYPE_TLSGD;
7698            got_type <= GOT_TYPE_TPREL;
7699            got_type = Got_type(got_type + 1))
7700         if (gsym->has_got_offset(got_type))
7701           {
7702             unsigned int off = gsym->got_offset(got_type);
7703             if (got_type == GOT_TYPE_TLSGD)
7704               off += size / 8;
7705             if (off == got_indx * (size / 8))
7706               {
7707                 if (got_type == GOT_TYPE_TPREL)
7708                   return -tp_offset;
7709                 else
7710                   return -dtp_offset;
7711               }
7712           }
7713     }
7714   gold_unreachable();
7715 }
7716
7717 // The selector for powerpc object files.
7718
7719 template<int size, bool big_endian>
7720 class Target_selector_powerpc : public Target_selector
7721 {
7722 public:
7723   Target_selector_powerpc()
7724     : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
7725                       size, big_endian,
7726                       (size == 64
7727                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
7728                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
7729                       (size == 64
7730                        ? (big_endian ? "elf64ppc" : "elf64lppc")
7731                        : (big_endian ? "elf32ppc" : "elf32lppc")))
7732   { }
7733
7734   virtual Target*
7735   do_instantiate_target()
7736   { return new Target_powerpc<size, big_endian>(); }
7737 };
7738
7739 Target_selector_powerpc<32, true> target_selector_ppc32;
7740 Target_selector_powerpc<32, false> target_selector_ppc32le;
7741 Target_selector_powerpc<64, true> target_selector_ppc64;
7742 Target_selector_powerpc<64, false> target_selector_ppc64le;
7743
7744 // Instantiate these constants for -O0
7745 template<int size, bool big_endian>
7746 const int Output_data_glink<size, big_endian>::pltresolve_size;
7747 template<int size, bool big_endian>
7748 const typename Stub_table<size, big_endian>::Address
7749   Stub_table<size, big_endian>::invalid_address;
7750 template<int size, bool big_endian>
7751 const typename Target_powerpc<size, big_endian>::Address
7752   Target_powerpc<size, big_endian>::invalid_address;
7753
7754 } // End anonymous namespace.