gold/
[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   class Got_entry;
1991
1992   // Create a new GOT entry and return its offset.
1993   unsigned int
1994   add_got_entry(Got_entry got_entry)
1995   {
1996     this->reserve_ent();
1997     return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1998   }
1999
2000   // Create a pair of new GOT entries and return the offset of the first.
2001   unsigned int
2002   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
2003   {
2004     this->reserve_ent(2);
2005     return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
2006                                                                  got_entry_2);
2007   }
2008
2009   unsigned int
2010   add_constant_pair(Valtype c1, Valtype c2)
2011   {
2012     this->reserve_ent(2);
2013     unsigned int got_offset = this->add_constant(c1);
2014     this->add_constant(c2);
2015     return got_offset;
2016   }
2017
2018   // Offset of _GLOBAL_OFFSET_TABLE_.
2019   unsigned int
2020   g_o_t() const
2021   {
2022     return this->got_offset(this->header_index_);
2023   }
2024
2025   // Offset of base used to access the GOT/TOC.
2026   // The got/toc pointer reg will be set to this value.
2027   Valtype
2028   got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
2029   {
2030     if (size == 32)
2031       return this->g_o_t();
2032     else
2033       return (this->output_section()->address()
2034               + object->toc_base_offset()
2035               - this->address());
2036   }
2037
2038   // Ensure our GOT has a header.
2039   void
2040   set_final_data_size()
2041   {
2042     if (this->header_ent_cnt_ != 0)
2043       this->make_header();
2044     Output_data_got<size, big_endian>::set_final_data_size();
2045   }
2046
2047   // First word of GOT header needs some values that are not
2048   // handled by Output_data_got so poke them in here.
2049   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
2050   void
2051   do_write(Output_file* of)
2052   {
2053     Valtype val = 0;
2054     if (size == 32 && this->layout_->dynamic_data() != NULL)
2055       val = this->layout_->dynamic_section()->address();
2056     if (size == 64)
2057       val = this->output_section()->address() + 0x8000;
2058     this->replace_constant(this->header_index_, val);
2059     Output_data_got<size, big_endian>::do_write(of);
2060   }
2061
2062 private:
2063   void
2064   reserve_ent(unsigned int cnt = 1)
2065   {
2066     if (this->header_ent_cnt_ == 0)
2067       return;
2068     if (this->num_entries() + cnt > this->header_index_)
2069       this->make_header();
2070   }
2071
2072   void
2073   make_header()
2074   {
2075     this->header_ent_cnt_ = 0;
2076     this->header_index_ = this->num_entries();
2077     if (size == 32)
2078       {
2079         Output_data_got<size, big_endian>::add_constant(0);
2080         Output_data_got<size, big_endian>::add_constant(0);
2081         Output_data_got<size, big_endian>::add_constant(0);
2082
2083         // Define _GLOBAL_OFFSET_TABLE_ at the header
2084         Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2085         if (gotsym != NULL)
2086           {
2087             Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
2088             sym->set_value(this->g_o_t());
2089           }
2090         else
2091           this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2092                                                Symbol_table::PREDEFINED,
2093                                                this, this->g_o_t(), 0,
2094                                                elfcpp::STT_OBJECT,
2095                                                elfcpp::STB_LOCAL,
2096                                                elfcpp::STV_HIDDEN, 0,
2097                                                false, false);
2098       }
2099     else
2100       Output_data_got<size, big_endian>::add_constant(0);
2101   }
2102
2103   // Stashed pointers.
2104   Symbol_table* symtab_;
2105   Layout* layout_;
2106
2107   // GOT header size.
2108   unsigned int header_ent_cnt_;
2109   // GOT header index.
2110   unsigned int header_index_;
2111 };
2112
2113 // Get the GOT section, creating it if necessary.
2114
2115 template<int size, bool big_endian>
2116 Output_data_got_powerpc<size, big_endian>*
2117 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
2118                                               Layout* layout)
2119 {
2120   if (this->got_ == NULL)
2121     {
2122       gold_assert(symtab != NULL && layout != NULL);
2123
2124       this->got_
2125         = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
2126
2127       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
2128                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2129                                       this->got_, ORDER_DATA, false);
2130     }
2131
2132   return this->got_;
2133 }
2134
2135 // Get the dynamic reloc section, creating it if necessary.
2136
2137 template<int size, bool big_endian>
2138 typename Target_powerpc<size, big_endian>::Reloc_section*
2139 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
2140 {
2141   if (this->rela_dyn_ == NULL)
2142     {
2143       gold_assert(layout != NULL);
2144       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
2145       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
2146                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
2147                                       ORDER_DYNAMIC_RELOCS, false);
2148     }
2149   return this->rela_dyn_;
2150 }
2151
2152 // Similarly, but for ifunc symbols get the one for ifunc.
2153
2154 template<int size, bool big_endian>
2155 typename Target_powerpc<size, big_endian>::Reloc_section*
2156 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
2157                                                    Layout* layout,
2158                                                    bool for_ifunc)
2159 {
2160   if (!for_ifunc)
2161     return this->rela_dyn_section(layout);
2162
2163   if (this->iplt_ == NULL)
2164     this->make_iplt_section(symtab, layout);
2165   return this->iplt_->rel_plt();
2166 }
2167
2168 class Stub_control
2169 {
2170  public:
2171   // Determine the stub group size.  The group size is the absolute
2172   // value of the parameter --stub-group-size.  If --stub-group-size
2173   // is passed a negative value, we restrict stubs to be always before
2174   // the stubbed branches.
2175   Stub_control(int32_t size)
2176     : state_(NO_GROUP), stub_group_size_(abs(size)),
2177       stub14_group_size_(abs(size)),
2178       stubs_always_before_branch_(size < 0), suppress_size_errors_(false),
2179       group_end_addr_(0), owner_(NULL), output_section_(NULL)
2180   {
2181     if (stub_group_size_ == 1)
2182       {
2183         // Default values.
2184         if (stubs_always_before_branch_)
2185           {
2186             stub_group_size_ = 0x1e00000;
2187             stub14_group_size_ = 0x7800;
2188           }
2189         else
2190           {
2191             stub_group_size_ = 0x1c00000;
2192             stub14_group_size_ = 0x7000;
2193           }
2194         suppress_size_errors_ = true;
2195       }
2196   }
2197
2198   // Return true iff input section can be handled by current stub
2199   // group.
2200   bool
2201   can_add_to_stub_group(Output_section* o,
2202                         const Output_section::Input_section* i,
2203                         bool has14);
2204
2205   const Output_section::Input_section*
2206   owner()
2207   { return owner_; }
2208
2209   Output_section*
2210   output_section()
2211   { return output_section_; }
2212
2213  private:
2214   typedef enum
2215   {
2216     NO_GROUP,
2217     FINDING_STUB_SECTION,
2218     HAS_STUB_SECTION
2219   } State;
2220
2221   State state_;
2222   uint32_t stub_group_size_;
2223   uint32_t stub14_group_size_;
2224   bool stubs_always_before_branch_;
2225   bool suppress_size_errors_;
2226   uint64_t group_end_addr_;
2227   const Output_section::Input_section* owner_;
2228   Output_section* output_section_;
2229 };
2230
2231 // Return true iff input section can be handled by current stub
2232 // group.
2233
2234 bool
2235 Stub_control::can_add_to_stub_group(Output_section* o,
2236                                     const Output_section::Input_section* i,
2237                                     bool has14)
2238 {
2239   uint32_t group_size
2240     = has14 ? this->stub14_group_size_ : this->stub_group_size_;
2241   bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
2242   uint64_t this_size;
2243   uint64_t start_addr = o->address();
2244
2245   if (whole_sec)
2246     // .init and .fini sections are pasted together to form a single
2247     // function.  We can't be adding stubs in the middle of the function.
2248     this_size = o->data_size();
2249   else
2250     {
2251       start_addr += i->relobj()->output_section_offset(i->shndx());
2252       this_size = i->data_size();
2253     }
2254   uint64_t end_addr = start_addr + this_size;
2255   bool toobig = this_size > group_size;
2256
2257   if (toobig && !this->suppress_size_errors_)
2258     gold_warning(_("%s:%s exceeds group size"),
2259                  i->relobj()->name().c_str(),
2260                  i->relobj()->section_name(i->shndx()).c_str());
2261
2262   if (this->state_ != HAS_STUB_SECTION
2263       && (!whole_sec || this->output_section_ != o)
2264       && (this->state_ == NO_GROUP
2265           || this->group_end_addr_ - end_addr < group_size))
2266     {
2267       this->owner_ = i;
2268       this->output_section_ = o;
2269     }
2270
2271   if (this->state_ == NO_GROUP)
2272     {
2273       this->state_ = FINDING_STUB_SECTION;
2274       this->group_end_addr_ = end_addr;
2275     }
2276   else if (this->group_end_addr_ - start_addr < group_size)
2277     ;
2278   // Adding this section would make the group larger than GROUP_SIZE.
2279   else if (this->state_ == FINDING_STUB_SECTION
2280            && !this->stubs_always_before_branch_
2281            && !toobig)
2282     {
2283       // But wait, there's more!  Input sections up to GROUP_SIZE
2284       // bytes before the stub table can be handled by it too.
2285       this->state_ = HAS_STUB_SECTION;
2286       this->group_end_addr_ = end_addr;
2287     }
2288   else
2289     {
2290       this->state_ = NO_GROUP;
2291       return false;
2292     }
2293   return true;
2294 }
2295
2296 // Look over all the input sections, deciding where to place stubs.
2297
2298 template<int size, bool big_endian>
2299 void
2300 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
2301                                                  const Task*)
2302 {
2303   Stub_control stub_control(parameters->options().stub_group_size());
2304
2305   // Group input sections and insert stub table
2306   Stub_table<size, big_endian>* stub_table = NULL;
2307   Layout::Section_list section_list;
2308   layout->get_executable_sections(&section_list);
2309   std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
2310   for (Layout::Section_list::reverse_iterator o = section_list.rbegin();
2311        o != section_list.rend();
2312        ++o)
2313     {
2314       typedef Output_section::Input_section_list Input_section_list;
2315       for (Input_section_list::const_reverse_iterator i
2316              = (*o)->input_sections().rbegin();
2317            i != (*o)->input_sections().rend();
2318            ++i)
2319         {
2320           if (i->is_input_section())
2321             {
2322               Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2323                 <Powerpc_relobj<size, big_endian>*>(i->relobj());
2324               bool has14 = ppcobj->has_14bit_branch(i->shndx());
2325               if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
2326                 {
2327                   stub_table->init(stub_control.owner(),
2328                                    stub_control.output_section());
2329                   stub_table = NULL;
2330                 }
2331               if (stub_table == NULL)
2332                 stub_table = this->new_stub_table();
2333               ppcobj->set_stub_table(i->shndx(), stub_table);
2334             }
2335         }
2336     }
2337   if (stub_table != NULL)
2338     {
2339       const Output_section::Input_section* i = stub_control.owner();
2340       if (!i->is_input_section())
2341         {
2342           // Corner case.  A new stub group was made for the first
2343           // section (last one looked at here) for some reason, but
2344           // the first section is already being used as the owner for
2345           // a stub table for following sections.  Force it into that
2346           // stub group.
2347           gold_assert(this->stub_tables_.size() >= 2);
2348           this->stub_tables_.pop_back();
2349           delete stub_table;
2350           Powerpc_relobj<size, big_endian>* ppcobj = static_cast
2351             <Powerpc_relobj<size, big_endian>*>(i->relobj());
2352           ppcobj->set_stub_table(i->shndx(), this->stub_tables_.back());
2353         }
2354       else
2355         stub_table->init(i, stub_control.output_section());
2356     }
2357 }
2358
2359 // If this branch needs a plt call stub, or a long branch stub, make one.
2360
2361 template<int size, bool big_endian>
2362 void
2363 Target_powerpc<size, big_endian>::Branch_info::make_stub(
2364     Stub_table<size, big_endian>* stub_table,
2365     Stub_table<size, big_endian>* ifunc_stub_table,
2366     Symbol_table* symtab) const
2367 {
2368   Symbol* sym = this->object_->global_symbol(this->r_sym_);
2369   if (sym != NULL && sym->is_forwarder())
2370     sym = symtab->resolve_forwards(sym);
2371   const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
2372   if (gsym != NULL
2373       ? use_plt_offset<size>(gsym, Scan::get_reference_flags(this->r_type_))
2374       : this->object_->local_has_plt_offset(this->r_sym_))
2375     {
2376       if (stub_table == NULL)
2377         stub_table = this->object_->stub_table(this->shndx_);
2378       if (stub_table == NULL)
2379         {
2380           // This is a ref from a data section to an ifunc symbol.
2381           stub_table = ifunc_stub_table;
2382         }
2383       gold_assert(stub_table != NULL);
2384       if (gsym != NULL)
2385         stub_table->add_plt_call_entry(this->object_, gsym,
2386                                        this->r_type_, this->addend_);
2387       else
2388         stub_table->add_plt_call_entry(this->object_, this->r_sym_,
2389                                        this->r_type_, this->addend_);
2390     }
2391   else
2392     {
2393       unsigned int max_branch_offset;
2394       if (this->r_type_ == elfcpp::R_POWERPC_REL14
2395           || this->r_type_ == elfcpp::R_POWERPC_REL14_BRTAKEN
2396           || this->r_type_ == elfcpp::R_POWERPC_REL14_BRNTAKEN)
2397         max_branch_offset = 1 << 15;
2398       else if (this->r_type_ == elfcpp::R_POWERPC_REL24
2399                || this->r_type_ == elfcpp::R_PPC_PLTREL24
2400                || this->r_type_ == elfcpp::R_PPC_LOCAL24PC)
2401         max_branch_offset = 1 << 25;
2402       else
2403         return;
2404       Address from = this->object_->get_output_section_offset(this->shndx_);
2405       gold_assert(from != invalid_address);
2406       from += (this->object_->output_section(this->shndx_)->address()
2407                + this->offset_);
2408       Address to;
2409       if (gsym != NULL)
2410         {
2411           switch (gsym->source())
2412             {
2413             case Symbol::FROM_OBJECT:
2414               {
2415                 Object* symobj = gsym->object();
2416                 if (symobj->is_dynamic()
2417                     || symobj->pluginobj() != NULL)
2418                   return;
2419                 bool is_ordinary;
2420                 unsigned int shndx = gsym->shndx(&is_ordinary);
2421                 if (shndx == elfcpp::SHN_UNDEF)
2422                   return;
2423               }
2424               break;
2425
2426             case Symbol::IS_UNDEFINED:
2427               return;
2428
2429             default:
2430               break;
2431             }
2432           Symbol_table::Compute_final_value_status status;
2433           to = symtab->compute_final_value<size>(gsym, &status);
2434           if (status != Symbol_table::CFVS_OK)
2435             return;
2436         }
2437       else
2438         {
2439           const Symbol_value<size>* psymval
2440             = this->object_->local_symbol(this->r_sym_);
2441           Symbol_value<size> symval;
2442           typedef Sized_relobj_file<size, big_endian> ObjType;
2443           typename ObjType::Compute_final_local_value_status status
2444             = this->object_->compute_final_local_value(this->r_sym_, psymval,
2445                                                        &symval, symtab);
2446           if (status != ObjType::CFLV_OK
2447               || !symval.has_output_value())
2448             return;
2449           to = symval.value(this->object_, 0);
2450         }
2451       to += this->addend_;
2452       if (stub_table == NULL)
2453         stub_table = this->object_->stub_table(this->shndx_);
2454       if (size == 64 && is_branch_reloc(this->r_type_))
2455         {
2456           unsigned int dest_shndx;
2457           Target_powerpc<size, big_endian>* target =
2458             static_cast<Target_powerpc<size, big_endian>*>(
2459                 parameters->sized_target<size, big_endian>());
2460           to = target->symval_for_branch(symtab, to, gsym,
2461                                          this->object_, &dest_shndx);
2462         }
2463       Address delta = to - from;
2464       if (delta + max_branch_offset >= 2 * max_branch_offset)
2465         {
2466           if (stub_table == NULL)
2467             {
2468               gold_warning(_("%s:%s: branch in non-executable section,"
2469                              " no long branch stub for you"),
2470                            this->object_->name().c_str(),
2471                            this->object_->section_name(this->shndx_).c_str());
2472               return;
2473             }
2474           stub_table->add_long_branch_entry(this->object_, to);
2475         }
2476     }
2477 }
2478
2479 // Relaxation hook.  This is where we do stub generation.
2480
2481 template<int size, bool big_endian>
2482 bool
2483 Target_powerpc<size, big_endian>::do_relax(int pass,
2484                                            const Input_objects*,
2485                                            Symbol_table* symtab,
2486                                            Layout* layout,
2487                                            const Task* task)
2488 {
2489   unsigned int prev_brlt_size = 0;
2490   if (pass == 1)
2491     {
2492       bool thread_safe = parameters->options().plt_thread_safe();
2493       if (size == 64 && !parameters->options().user_set_plt_thread_safe())
2494         {
2495           static const char* const thread_starter[] =
2496             {
2497               "pthread_create",
2498               /* libstdc++ */
2499               "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2500               /* librt */
2501               "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2502               "mq_notify", "create_timer",
2503               /* libanl */
2504               "getaddrinfo_a",
2505               /* libgomp */
2506               "GOMP_parallel_start",
2507               "GOMP_parallel_loop_static_start",
2508               "GOMP_parallel_loop_dynamic_start",
2509               "GOMP_parallel_loop_guided_start",
2510               "GOMP_parallel_loop_runtime_start",
2511               "GOMP_parallel_sections_start",
2512             };
2513
2514           if (parameters->options().shared())
2515             thread_safe = true;
2516           else
2517             {
2518               for (unsigned int i = 0;
2519                    i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2520                    i++)
2521                 {
2522                   Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2523                   thread_safe = (sym != NULL
2524                                  && sym->in_reg()
2525                                  && sym->in_real_elf());
2526                   if (thread_safe)
2527                     break;
2528                 }
2529             }
2530         }
2531       this->plt_thread_safe_ = thread_safe;
2532       this->group_sections(layout, task);
2533     }
2534
2535   // We need address of stub tables valid for make_stub.
2536   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2537        p != this->stub_tables_.end();
2538        ++p)
2539     {
2540       const Powerpc_relobj<size, big_endian>* object
2541         = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2542       Address off = object->get_output_section_offset((*p)->shndx());
2543       gold_assert(off != invalid_address);
2544       Output_section* os = (*p)->output_section();
2545       (*p)->set_address_and_size(os, off);
2546     }
2547
2548   if (pass != 1)
2549     {
2550       // Clear plt call stubs, long branch stubs and branch lookup table.
2551       prev_brlt_size = this->branch_lookup_table_.size();
2552       this->branch_lookup_table_.clear();
2553       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2554            p != this->stub_tables_.end();
2555            ++p)
2556         {
2557           (*p)->clear_stubs();
2558         }
2559     }
2560
2561   // Build all the stubs.
2562   Stub_table<size, big_endian>* ifunc_stub_table
2563     = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2564   Stub_table<size, big_endian>* one_stub_table
2565     = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2566   for (typename Branches::const_iterator b = this->branch_info_.begin();
2567        b != this->branch_info_.end();
2568        b++)
2569     {
2570       b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2571     }
2572
2573   // Did anything change size?
2574   unsigned int num_huge_branches = this->branch_lookup_table_.size();
2575   bool again = num_huge_branches != prev_brlt_size;
2576   if (size == 64 && num_huge_branches != 0)
2577     this->make_brlt_section(layout);
2578   if (size == 64 && again)
2579     this->brlt_section_->set_current_size(num_huge_branches);
2580
2581   typedef Unordered_set<Output_section*> Output_sections;
2582   Output_sections os_need_update;
2583   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2584        p != this->stub_tables_.end();
2585        ++p)
2586     {
2587       if ((*p)->size_update())
2588         {
2589           again = true;
2590           (*p)->add_eh_frame(layout);
2591           os_need_update.insert((*p)->output_section());
2592         }
2593     }
2594
2595   // Set output section offsets for all input sections in an output
2596   // section that just changed size.  Anything past the stubs will
2597   // need updating.
2598   for (typename Output_sections::iterator p = os_need_update.begin();
2599        p != os_need_update.end();
2600        p++)
2601     {
2602       Output_section* os = *p;
2603       Address off = 0;
2604       typedef Output_section::Input_section_list Input_section_list;
2605       for (Input_section_list::const_iterator i = os->input_sections().begin();
2606            i != os->input_sections().end();
2607            ++i)
2608         {
2609           off = align_address(off, i->addralign());
2610           if (i->is_input_section() || i->is_relaxed_input_section())
2611             i->relobj()->set_section_offset(i->shndx(), off);
2612           if (i->is_relaxed_input_section())
2613             {
2614               Stub_table<size, big_endian>* stub_table
2615                 = static_cast<Stub_table<size, big_endian>*>(
2616                     i->relaxed_input_section());
2617               off += stub_table->set_address_and_size(os, off);
2618             }
2619           else
2620             off += i->data_size();
2621         }
2622       // If .branch_lt is part of this output section, then we have
2623       // just done the offset adjustment.
2624       os->clear_section_offsets_need_adjustment();
2625     }
2626
2627   if (size == 64
2628       && !again
2629       && num_huge_branches != 0
2630       && parameters->options().output_is_position_independent())
2631     {
2632       // Fill in the BRLT relocs.
2633       this->brlt_section_->reset_brlt_sizes();
2634       for (typename Branch_lookup_table::const_iterator p
2635              = this->branch_lookup_table_.begin();
2636            p != this->branch_lookup_table_.end();
2637            ++p)
2638         {
2639           this->brlt_section_->add_reloc(p->first, p->second);
2640         }
2641       this->brlt_section_->finalize_brlt_sizes();
2642     }
2643   return again;
2644 }
2645
2646 template<int size, bool big_endian>
2647 void
2648 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2649                                                       unsigned char* oview,
2650                                                       uint64_t* paddress,
2651                                                       off_t* plen) const
2652 {
2653   uint64_t address = plt->address();
2654   off_t len = plt->data_size();
2655
2656   if (plt == this->glink_)
2657     {
2658       // See Output_data_glink::do_write() for glink contents.
2659       if (size == 64)
2660         {
2661           // There is one word before __glink_PLTresolve
2662           address += 8;
2663           len -= 8;
2664         }
2665       else if (parameters->options().output_is_position_independent())
2666         {
2667           // There are two FDEs for a position independent glink.
2668           // The first covers the branch table, the second
2669           // __glink_PLTresolve at the end of glink.
2670           off_t resolve_size = this->glink_->pltresolve_size;
2671           if (oview[9] == 0)
2672             len -= resolve_size;
2673           else
2674             {
2675               address += len - resolve_size;
2676               len = resolve_size;
2677             }
2678         }
2679     }
2680   else
2681     {
2682       // Must be a stub table.
2683       const Stub_table<size, big_endian>* stub_table
2684         = static_cast<const Stub_table<size, big_endian>*>(plt);
2685       uint64_t stub_address = stub_table->stub_address();
2686       len -= stub_address - address;
2687       address = stub_address;
2688     }
2689
2690   *paddress = address;
2691   *plen = len;
2692 }
2693
2694 // A class to handle the PLT data.
2695
2696 template<int size, bool big_endian>
2697 class Output_data_plt_powerpc : public Output_section_data_build
2698 {
2699  public:
2700   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2701                             size, big_endian> Reloc_section;
2702
2703   Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2704                           Reloc_section* plt_rel,
2705                           unsigned int reserved_size,
2706                           const char* name)
2707     : Output_section_data_build(size == 32 ? 4 : 8),
2708       rel_(plt_rel),
2709       targ_(targ),
2710       initial_plt_entry_size_(reserved_size),
2711       name_(name)
2712   { }
2713
2714   // Add an entry to the PLT.
2715   void
2716   add_entry(Symbol*);
2717
2718   void
2719   add_ifunc_entry(Symbol*);
2720
2721   void
2722   add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2723
2724   // Return the .rela.plt section data.
2725   Reloc_section*
2726   rel_plt() const
2727   {
2728     return this->rel_;
2729   }
2730
2731   // Return the number of PLT entries.
2732   unsigned int
2733   entry_count() const
2734   {
2735     if (this->current_data_size() == 0)
2736       return 0;
2737     return ((this->current_data_size() - this->initial_plt_entry_size_)
2738             / plt_entry_size);
2739   }
2740
2741   // Return the offset of the first non-reserved PLT entry.
2742   unsigned int
2743   first_plt_entry_offset()
2744   { return this->initial_plt_entry_size_; }
2745
2746   // Return the size of a PLT entry.
2747   static unsigned int
2748   get_plt_entry_size()
2749   { return plt_entry_size; }
2750
2751  protected:
2752   void
2753   do_adjust_output_section(Output_section* os)
2754   {
2755     os->set_entsize(0);
2756   }
2757
2758   // Write to a map file.
2759   void
2760   do_print_to_mapfile(Mapfile* mapfile) const
2761   { mapfile->print_output_data(this, this->name_); }
2762
2763  private:
2764   // The size of an entry in the PLT.
2765   static const int plt_entry_size = size == 32 ? 4 : 24;
2766
2767   // Write out the PLT data.
2768   void
2769   do_write(Output_file*);
2770
2771   // The reloc section.
2772   Reloc_section* rel_;
2773   // Allows access to .glink for do_write.
2774   Target_powerpc<size, big_endian>* targ_;
2775   // The size of the first reserved entry.
2776   int initial_plt_entry_size_;
2777   // What to report in map file.
2778   const char *name_;
2779 };
2780
2781 // Add an entry to the PLT.
2782
2783 template<int size, bool big_endian>
2784 void
2785 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
2786 {
2787   if (!gsym->has_plt_offset())
2788     {
2789       section_size_type off = this->current_data_size();
2790       if (off == 0)
2791         off += this->first_plt_entry_offset();
2792       gsym->set_plt_offset(off);
2793       gsym->set_needs_dynsym_entry();
2794       unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
2795       this->rel_->add_global(gsym, dynrel, this, off, 0);
2796       off += plt_entry_size;
2797       this->set_current_data_size(off);
2798     }
2799 }
2800
2801 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
2802
2803 template<int size, bool big_endian>
2804 void
2805 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
2806 {
2807   if (!gsym->has_plt_offset())
2808     {
2809       section_size_type off = this->current_data_size();
2810       gsym->set_plt_offset(off);
2811       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2812       if (size == 64)
2813         dynrel = elfcpp::R_PPC64_JMP_IREL;
2814       this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
2815       off += plt_entry_size;
2816       this->set_current_data_size(off);
2817     }
2818 }
2819
2820 // Add an entry for a local ifunc symbol to the IPLT.
2821
2822 template<int size, bool big_endian>
2823 void
2824 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
2825     Sized_relobj_file<size, big_endian>* relobj,
2826     unsigned int local_sym_index)
2827 {
2828   if (!relobj->local_has_plt_offset(local_sym_index))
2829     {
2830       section_size_type off = this->current_data_size();
2831       relobj->set_local_plt_offset(local_sym_index, off);
2832       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
2833       if (size == 64)
2834         dynrel = elfcpp::R_PPC64_JMP_IREL;
2835       this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
2836                                               this, off, 0);
2837       off += plt_entry_size;
2838       this->set_current_data_size(off);
2839     }
2840 }
2841
2842 static const uint32_t add_0_11_11       = 0x7c0b5a14;
2843 static const uint32_t add_2_2_11        = 0x7c425a14;
2844 static const uint32_t add_3_3_2         = 0x7c631214;
2845 static const uint32_t add_3_3_13        = 0x7c636a14;
2846 static const uint32_t add_11_0_11       = 0x7d605a14;
2847 static const uint32_t add_12_2_11       = 0x7d825a14;
2848 static const uint32_t add_12_12_11      = 0x7d8c5a14;
2849 static const uint32_t addi_11_11        = 0x396b0000;
2850 static const uint32_t addi_12_12        = 0x398c0000;
2851 static const uint32_t addi_2_2          = 0x38420000;
2852 static const uint32_t addi_3_2          = 0x38620000;
2853 static const uint32_t addi_3_3          = 0x38630000;
2854 static const uint32_t addis_0_2         = 0x3c020000;
2855 static const uint32_t addis_0_13        = 0x3c0d0000;
2856 static const uint32_t addis_11_11       = 0x3d6b0000;
2857 static const uint32_t addis_11_30       = 0x3d7e0000;
2858 static const uint32_t addis_12_12       = 0x3d8c0000;
2859 static const uint32_t addis_12_2        = 0x3d820000;
2860 static const uint32_t addis_3_2         = 0x3c620000;
2861 static const uint32_t addis_3_13        = 0x3c6d0000;
2862 static const uint32_t b                 = 0x48000000;
2863 static const uint32_t bcl_20_31         = 0x429f0005;
2864 static const uint32_t bctr              = 0x4e800420;
2865 static const uint32_t blr               = 0x4e800020;
2866 static const uint32_t blrl              = 0x4e800021;
2867 static const uint32_t bnectr_p4         = 0x4ce20420;
2868 static const uint32_t cmpldi_2_0        = 0x28220000;
2869 static const uint32_t cror_15_15_15     = 0x4def7b82;
2870 static const uint32_t cror_31_31_31     = 0x4ffffb82;
2871 static const uint32_t ld_0_1            = 0xe8010000;
2872 static const uint32_t ld_0_12           = 0xe80c0000;
2873 static const uint32_t ld_11_12          = 0xe96c0000;
2874 static const uint32_t ld_11_2           = 0xe9620000;
2875 static const uint32_t ld_2_1            = 0xe8410000;
2876 static const uint32_t ld_2_11           = 0xe84b0000;
2877 static const uint32_t ld_2_12           = 0xe84c0000;
2878 static const uint32_t ld_2_2            = 0xe8420000;
2879 static const uint32_t lfd_0_1           = 0xc8010000;
2880 static const uint32_t li_0_0            = 0x38000000;
2881 static const uint32_t li_12_0           = 0x39800000;
2882 static const uint32_t lis_0_0           = 0x3c000000;
2883 static const uint32_t lis_11            = 0x3d600000;
2884 static const uint32_t lis_12            = 0x3d800000;
2885 static const uint32_t lwz_0_12          = 0x800c0000;
2886 static const uint32_t lwz_11_11         = 0x816b0000;
2887 static const uint32_t lwz_11_30         = 0x817e0000;
2888 static const uint32_t lwz_12_12         = 0x818c0000;
2889 static const uint32_t lwzu_0_12         = 0x840c0000;
2890 static const uint32_t lvx_0_12_0        = 0x7c0c00ce;
2891 static const uint32_t mflr_0            = 0x7c0802a6;
2892 static const uint32_t mflr_11           = 0x7d6802a6;
2893 static const uint32_t mflr_12           = 0x7d8802a6;
2894 static const uint32_t mtctr_0           = 0x7c0903a6;
2895 static const uint32_t mtctr_11          = 0x7d6903a6;
2896 static const uint32_t mtctr_12          = 0x7d8903a6;
2897 static const uint32_t mtlr_0            = 0x7c0803a6;
2898 static const uint32_t mtlr_12           = 0x7d8803a6;
2899 static const uint32_t nop               = 0x60000000;
2900 static const uint32_t ori_0_0_0         = 0x60000000;
2901 static const uint32_t std_0_1           = 0xf8010000;
2902 static const uint32_t std_0_12          = 0xf80c0000;
2903 static const uint32_t std_2_1           = 0xf8410000;
2904 static const uint32_t stfd_0_1          = 0xd8010000;
2905 static const uint32_t stvx_0_12_0       = 0x7c0c01ce;
2906 static const uint32_t sub_11_11_12      = 0x7d6c5850;
2907 static const uint32_t xor_11_11_11      = 0x7d6b5a78;
2908
2909 // Write out the PLT.
2910
2911 template<int size, bool big_endian>
2912 void
2913 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
2914 {
2915   if (size == 32 && this->name_[3] != 'I')
2916     {
2917       const section_size_type offset = this->offset();
2918       const section_size_type oview_size
2919         = convert_to_section_size_type(this->data_size());
2920       unsigned char* const oview = of->get_output_view(offset, oview_size);
2921       unsigned char* pov = oview;
2922       unsigned char* endpov = oview + oview_size;
2923
2924       // The address of the .glink branch table
2925       const Output_data_glink<size, big_endian>* glink
2926         = this->targ_->glink_section();
2927       elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
2928
2929       while (pov < endpov)
2930         {
2931           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
2932           pov += 4;
2933           branch_tab += 4;
2934         }
2935
2936       of->write_output_view(offset, oview_size, oview);
2937     }
2938 }
2939
2940 // Create the PLT section.
2941
2942 template<int size, bool big_endian>
2943 void
2944 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
2945                                                    Layout* layout)
2946 {
2947   if (this->plt_ == NULL)
2948     {
2949       if (this->got_ == NULL)
2950         this->got_section(symtab, layout);
2951
2952       if (this->glink_ == NULL)
2953         make_glink_section(layout);
2954
2955       // Ensure that .rela.dyn always appears before .rela.plt  This is
2956       // necessary due to how, on PowerPC and some other targets, .rela.dyn
2957       // needs to include .rela.plt in its range.
2958       this->rela_dyn_section(layout);
2959
2960       Reloc_section* plt_rel = new Reloc_section(false);
2961       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
2962                                       elfcpp::SHF_ALLOC, plt_rel,
2963                                       ORDER_DYNAMIC_PLT_RELOCS, false);
2964       this->plt_
2965         = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
2966                                                         size == 32 ? 0 : 24,
2967                                                         "** PLT");
2968       layout->add_output_section_data(".plt",
2969                                       (size == 32
2970                                        ? elfcpp::SHT_PROGBITS
2971                                        : elfcpp::SHT_NOBITS),
2972                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
2973                                       this->plt_,
2974                                       (size == 32
2975                                        ? ORDER_SMALL_DATA
2976                                        : ORDER_SMALL_BSS),
2977                                       false);
2978     }
2979 }
2980
2981 // Create the IPLT section.
2982
2983 template<int size, bool big_endian>
2984 void
2985 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
2986                                                     Layout* layout)
2987 {
2988   if (this->iplt_ == NULL)
2989     {
2990       this->make_plt_section(symtab, layout);
2991
2992       Reloc_section* iplt_rel = new Reloc_section(false);
2993       this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
2994       this->iplt_
2995         = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
2996                                                         0, "** IPLT");
2997       this->plt_->output_section()->add_output_section_data(this->iplt_);
2998     }
2999 }
3000
3001 // A section for huge long branch addresses, similar to plt section.
3002
3003 template<int size, bool big_endian>
3004 class Output_data_brlt_powerpc : public Output_section_data_build
3005 {
3006  public:
3007   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3008   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3009                             size, big_endian> Reloc_section;
3010
3011   Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3012                            Reloc_section* brlt_rel)
3013     : Output_section_data_build(size == 32 ? 4 : 8),
3014       rel_(brlt_rel),
3015       targ_(targ)
3016   { }
3017
3018   void
3019   reset_brlt_sizes()
3020   {
3021     this->reset_data_size();
3022     this->rel_->reset_data_size();
3023   }
3024
3025   void
3026   finalize_brlt_sizes()
3027   {
3028     this->finalize_data_size();
3029     this->rel_->finalize_data_size();
3030   }
3031
3032   // Add a reloc for an entry in the BRLT.
3033   void
3034   add_reloc(Address to, unsigned int off)
3035   { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3036
3037   // Update section and reloc section size.
3038   void
3039   set_current_size(unsigned int num_branches)
3040   {
3041     this->reset_address_and_file_offset();
3042     this->set_current_data_size(num_branches * 16);
3043     this->finalize_data_size();
3044     Output_section* os = this->output_section();
3045     os->set_section_offsets_need_adjustment();
3046     if (this->rel_ != NULL)
3047       {
3048         unsigned int reloc_size
3049           = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3050         this->rel_->reset_address_and_file_offset();
3051         this->rel_->set_current_data_size(num_branches * reloc_size);
3052         this->rel_->finalize_data_size();
3053         Output_section* os = this->rel_->output_section();
3054         os->set_section_offsets_need_adjustment();
3055       }
3056   }
3057
3058  protected:
3059   void
3060   do_adjust_output_section(Output_section* os)
3061   {
3062     os->set_entsize(0);
3063   }
3064
3065   // Write to a map file.
3066   void
3067   do_print_to_mapfile(Mapfile* mapfile) const
3068   { mapfile->print_output_data(this, "** BRLT"); }
3069
3070  private:
3071   // Write out the BRLT data.
3072   void
3073   do_write(Output_file*);
3074
3075   // The reloc section.
3076   Reloc_section* rel_;
3077   Target_powerpc<size, big_endian>* targ_;
3078 };
3079
3080 // Make the branch lookup table section.
3081
3082 template<int size, bool big_endian>
3083 void
3084 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3085 {
3086   if (size == 64 && this->brlt_section_ == NULL)
3087     {
3088       Reloc_section* brlt_rel = NULL;
3089       bool is_pic = parameters->options().output_is_position_independent();
3090       if (is_pic)
3091         {
3092           // When PIC we can't fill in .branch_lt (like .plt it can be
3093           // a bss style section) but must initialise at runtime via
3094           // dynamic relocats.
3095           this->rela_dyn_section(layout);
3096           brlt_rel = new Reloc_section(false);
3097           this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3098         }
3099       this->brlt_section_
3100         = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3101       if (this->plt_ && is_pic)
3102         this->plt_->output_section()
3103           ->add_output_section_data(this->brlt_section_);
3104       else
3105         layout->add_output_section_data(".branch_lt",
3106                                         (is_pic ? elfcpp::SHT_NOBITS
3107                                          : elfcpp::SHT_PROGBITS),
3108                                         elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3109                                         this->brlt_section_,
3110                                         (is_pic ? ORDER_SMALL_BSS
3111                                          : ORDER_SMALL_DATA),
3112                                         false);
3113     }
3114 }
3115
3116 // Write out .branch_lt when non-PIC.
3117
3118 template<int size, bool big_endian>
3119 void
3120 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3121 {
3122   if (size == 64 && !parameters->options().output_is_position_independent())
3123     {
3124       const section_size_type offset = this->offset();
3125       const section_size_type oview_size
3126         = convert_to_section_size_type(this->data_size());
3127       unsigned char* const oview = of->get_output_view(offset, oview_size);
3128
3129       this->targ_->write_branch_lookup_table(oview);
3130       of->write_output_view(offset, oview_size, oview);
3131     }
3132 }
3133
3134 static inline uint32_t
3135 l(uint32_t a)
3136 {
3137   return a & 0xffff;
3138 }
3139
3140 static inline uint32_t
3141 hi(uint32_t a)
3142 {
3143   return l(a >> 16);
3144 }
3145
3146 static inline uint32_t
3147 ha(uint32_t a)
3148 {
3149   return hi(a + 0x8000);
3150 }
3151
3152 template<int size>
3153 struct Eh_cie
3154 {
3155   static const unsigned char eh_frame_cie[12];
3156 };
3157
3158 template<int size>
3159 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3160 {
3161   1,                                    // CIE version.
3162   'z', 'R', 0,                          // Augmentation string.
3163   4,                                    // Code alignment.
3164   0x80 - size / 8 ,                     // Data alignment.
3165   65,                                   // RA reg.
3166   1,                                    // Augmentation size.
3167   (elfcpp::DW_EH_PE_pcrel
3168    | elfcpp::DW_EH_PE_sdata4),          // FDE encoding.
3169   elfcpp::DW_CFA_def_cfa, 1, 0          // def_cfa: r1 offset 0.
3170 };
3171
3172 // Describe __glink_PLTresolve use of LR, 64-bit version.
3173 static const unsigned char glink_eh_frame_fde_64[] =
3174 {
3175   0, 0, 0, 0,                           // Replaced with offset to .glink.
3176   0, 0, 0, 0,                           // Replaced with size of .glink.
3177   0,                                    // Augmentation size.
3178   elfcpp::DW_CFA_advance_loc + 1,
3179   elfcpp::DW_CFA_register, 65, 12,
3180   elfcpp::DW_CFA_advance_loc + 4,
3181   elfcpp::DW_CFA_restore_extended, 65
3182 };
3183
3184 // Describe __glink_PLTresolve use of LR, 32-bit version.
3185 static const unsigned char glink_eh_frame_fde_32[] =
3186 {
3187   0, 0, 0, 0,                           // Replaced with offset to .glink.
3188   0, 0, 0, 0,                           // Replaced with size of .glink.
3189   0,                                    // Augmentation size.
3190   elfcpp::DW_CFA_advance_loc + 2,
3191   elfcpp::DW_CFA_register, 65, 0,
3192   elfcpp::DW_CFA_advance_loc + 4,
3193   elfcpp::DW_CFA_restore_extended, 65
3194 };
3195
3196 static const unsigned char default_fde[] =
3197 {
3198   0, 0, 0, 0,                           // Replaced with offset to stubs.
3199   0, 0, 0, 0,                           // Replaced with size of stubs.
3200   0,                                    // Augmentation size.
3201   elfcpp::DW_CFA_nop,                   // Pad.
3202   elfcpp::DW_CFA_nop,
3203   elfcpp::DW_CFA_nop
3204 };
3205
3206 template<bool big_endian>
3207 static inline void
3208 write_insn(unsigned char* p, uint32_t v)
3209 {
3210   elfcpp::Swap<32, big_endian>::writeval(p, v);
3211 }
3212
3213 // Stub_table holds information about plt and long branch stubs.
3214 // Stubs are built in an area following some input section determined
3215 // by group_sections().  This input section is converted to a relaxed
3216 // input section allowing it to be resized to accommodate the stubs
3217
3218 template<int size, bool big_endian>
3219 class Stub_table : public Output_relaxed_input_section
3220 {
3221  public:
3222   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3223   static const Address invalid_address = static_cast<Address>(0) - 1;
3224
3225   Stub_table(Target_powerpc<size, big_endian>* targ)
3226     : Output_relaxed_input_section(NULL, 0, 0),
3227       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3228       orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3229       branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3230   { }
3231
3232   // Delayed Output_relaxed_input_section init.
3233   void
3234   init(const Output_section::Input_section*, Output_section*);
3235
3236   // Add a plt call stub.
3237   void
3238   add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3239                      const Symbol*,
3240                      unsigned int,
3241                      Address);
3242
3243   void
3244   add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3245                      unsigned int,
3246                      unsigned int,
3247                      Address);
3248
3249   // Find a given plt call stub.
3250   Address
3251   find_plt_call_entry(const Symbol*) const;
3252
3253   Address
3254   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3255                       unsigned int) const;
3256
3257   Address
3258   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3259                       const Symbol*,
3260                       unsigned int,
3261                       Address) const;
3262
3263   Address
3264   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3265                       unsigned int,
3266                       unsigned int,
3267                       Address) const;
3268
3269   // Add a long branch stub.
3270   void
3271   add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3272
3273   Address
3274   find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3275                          Address) const;
3276
3277   void
3278   clear_stubs()
3279   {
3280     this->plt_call_stubs_.clear();
3281     this->plt_size_ = 0;
3282     this->long_branch_stubs_.clear();
3283     this->branch_size_ = 0;
3284   }
3285
3286   Address
3287   set_address_and_size(const Output_section* os, Address off)
3288   {
3289     Address start_off = off;
3290     off += this->orig_data_size_;
3291     Address my_size = this->plt_size_ + this->branch_size_;
3292     if (my_size != 0)
3293       off = align_address(off, this->stub_align());
3294     // Include original section size and alignment padding in size
3295     my_size += off - start_off;
3296     this->reset_address_and_file_offset();
3297     this->set_current_data_size(my_size);
3298     this->set_address_and_file_offset(os->address() + start_off,
3299                                       os->offset() + start_off);
3300     return my_size;
3301   }
3302
3303   Address
3304   stub_address() const
3305   {
3306     return align_address(this->address() + this->orig_data_size_,
3307                          this->stub_align());
3308   }
3309
3310   Address
3311   stub_offset() const
3312   {
3313     return align_address(this->offset() + this->orig_data_size_,
3314                          this->stub_align());
3315   }
3316
3317   section_size_type
3318   plt_size() const
3319   { return this->plt_size_; }
3320
3321   bool
3322   size_update()
3323   {
3324     Output_section* os = this->output_section();
3325     if (os->addralign() < this->stub_align())
3326       {
3327         os->set_addralign(this->stub_align());
3328         // FIXME: get rid of the insane checkpointing.
3329         // We can't increase alignment of the input section to which
3330         // stubs are attached;  The input section may be .init which
3331         // is pasted together with other .init sections to form a
3332         // function.  Aligning might insert zero padding resulting in
3333         // sigill.  However we do need to increase alignment of the
3334         // output section so that the align_address() on offset in
3335         // set_address_and_size() adds the same padding as the
3336         // align_address() on address in stub_address().
3337         // What's more, we need this alignment for the layout done in
3338         // relaxation_loop_body() so that the output section starts at
3339         // a suitably aligned address.
3340         os->checkpoint_set_addralign(this->stub_align());
3341       }
3342     if (this->last_plt_size_ != this->plt_size_
3343         || this->last_branch_size_ != this->branch_size_)
3344       {
3345         this->last_plt_size_ = this->plt_size_;
3346         this->last_branch_size_ = this->branch_size_;
3347         return true;
3348       }
3349     return false;
3350   }
3351
3352   // Add .eh_frame info for this stub section.  Unlike other linker
3353   // generated .eh_frame this is added late in the link, because we
3354   // only want the .eh_frame info if this particular stub section is
3355   // non-empty.
3356   void
3357   add_eh_frame(Layout* layout)
3358   {
3359     if (!this->eh_frame_added_)
3360       {
3361         if (!parameters->options().ld_generated_unwind_info())
3362           return;
3363
3364         // Since we add stub .eh_frame info late, it must be placed
3365         // after all other linker generated .eh_frame info so that
3366         // merge mapping need not be updated for input sections.
3367         // There is no provision to use a different CIE to that used
3368         // by .glink.
3369         if (!this->targ_->has_glink())
3370           return;
3371
3372         layout->add_eh_frame_for_plt(this,
3373                                      Eh_cie<size>::eh_frame_cie,
3374                                      sizeof (Eh_cie<size>::eh_frame_cie),
3375                                      default_fde,
3376                                      sizeof (default_fde));
3377         this->eh_frame_added_ = true;
3378       }
3379   }
3380
3381   Target_powerpc<size, big_endian>*
3382   targ() const
3383   { return targ_; }
3384
3385  private:
3386   class Plt_stub_ent;
3387   class Plt_stub_ent_hash;
3388   typedef Unordered_map<Plt_stub_ent, unsigned int,
3389                         Plt_stub_ent_hash> Plt_stub_entries;
3390
3391   // Alignment of stub section.
3392   unsigned int
3393   stub_align() const
3394   {
3395     if (size == 32)
3396       return 16;
3397     unsigned int min_align = 32;
3398     unsigned int user_align = 1 << parameters->options().plt_align();
3399     return std::max(user_align, min_align);
3400   }
3401
3402   // Return the plt offset for the given call stub.
3403   Address
3404   plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3405   {
3406     const Symbol* gsym = p->first.sym_;
3407     if (gsym != NULL)
3408       {
3409         *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3410                     && gsym->can_use_relative_reloc(false));
3411         return gsym->plt_offset();
3412       }
3413     else
3414       {
3415         *is_iplt = true;
3416         const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3417         unsigned int local_sym_index = p->first.locsym_;
3418         return relobj->local_plt_offset(local_sym_index);
3419       }
3420   }
3421
3422   // Size of a given plt call stub.
3423   unsigned int
3424   plt_call_size(typename Plt_stub_entries::const_iterator p) const
3425   {
3426     if (size == 32)
3427       return 16;
3428
3429     bool is_iplt;
3430     Address plt_addr = this->plt_off(p, &is_iplt);
3431     if (is_iplt)
3432       plt_addr += this->targ_->iplt_section()->address();
3433     else
3434       plt_addr += this->targ_->plt_section()->address();
3435     Address got_addr = this->targ_->got_section()->output_section()->address();
3436     const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3437       <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3438     got_addr += ppcobj->toc_base_offset();
3439     Address off = plt_addr - got_addr;
3440     bool static_chain = parameters->options().plt_static_chain();
3441     bool thread_safe = this->targ_->plt_thread_safe();
3442     unsigned int bytes = (4 * 5
3443                           + 4 * static_chain
3444                           + 8 * thread_safe
3445                           + 4 * (ha(off) != 0)
3446                           + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3447     unsigned int align = 1 << parameters->options().plt_align();
3448     if (align > 1)
3449       bytes = (bytes + align - 1) & -align;
3450     return bytes;
3451   }
3452
3453   // Return long branch stub size.
3454   unsigned int
3455   branch_stub_size(Address to)
3456   {
3457     Address loc
3458       = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3459     if (to - loc + (1 << 25) < 2 << 25)
3460       return 4;
3461     if (size == 64 || !parameters->options().output_is_position_independent())
3462       return 16;
3463     return 32;
3464   }
3465
3466   // Write out stubs.
3467   void
3468   do_write(Output_file*);
3469
3470   // Plt call stub keys.
3471   class Plt_stub_ent
3472   {
3473   public:
3474     Plt_stub_ent(const Symbol* sym)
3475       : sym_(sym), object_(0), addend_(0), locsym_(0)
3476     { }
3477
3478     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3479                  unsigned int locsym_index)
3480       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3481     { }
3482
3483     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3484                  const Symbol* sym,
3485                  unsigned int r_type,
3486                  Address addend)
3487       : sym_(sym), object_(0), addend_(0), locsym_(0)
3488     {
3489       if (size != 32)
3490         this->addend_ = addend;
3491       else if (parameters->options().output_is_position_independent()
3492                && r_type == elfcpp::R_PPC_PLTREL24)
3493         {
3494           this->addend_ = addend;
3495           if (this->addend_ >= 32768)
3496             this->object_ = object;
3497         }
3498     }
3499
3500     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3501                  unsigned int locsym_index,
3502                  unsigned int r_type,
3503                  Address addend)
3504       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3505     {
3506       if (size != 32)
3507         this->addend_ = addend;
3508       else if (parameters->options().output_is_position_independent()
3509                && r_type == elfcpp::R_PPC_PLTREL24)
3510         this->addend_ = addend;
3511     }
3512
3513     bool operator==(const Plt_stub_ent& that) const
3514     {
3515       return (this->sym_ == that.sym_
3516               && this->object_ == that.object_
3517               && this->addend_ == that.addend_
3518               && this->locsym_ == that.locsym_);
3519     }
3520
3521     const Symbol* sym_;
3522     const Sized_relobj_file<size, big_endian>* object_;
3523     typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3524     unsigned int locsym_;
3525   };
3526
3527   class Plt_stub_ent_hash
3528   {
3529   public:
3530     size_t operator()(const Plt_stub_ent& ent) const
3531     {
3532       return (reinterpret_cast<uintptr_t>(ent.sym_)
3533               ^ reinterpret_cast<uintptr_t>(ent.object_)
3534               ^ ent.addend_
3535               ^ ent.locsym_);
3536     }
3537   };
3538
3539   // Long branch stub keys.
3540   class Branch_stub_ent
3541   {
3542   public:
3543     Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3544       : dest_(to), toc_base_off_(0)
3545     {
3546       if (size == 64)
3547         toc_base_off_ = obj->toc_base_offset();
3548     }
3549
3550     bool operator==(const Branch_stub_ent& that) const
3551     {
3552       return (this->dest_ == that.dest_
3553               && (size == 32
3554                   || this->toc_base_off_ == that.toc_base_off_));
3555     }
3556
3557     Address dest_;
3558     unsigned int toc_base_off_;
3559   };
3560
3561   class Branch_stub_ent_hash
3562   {
3563   public:
3564     size_t operator()(const Branch_stub_ent& ent) const
3565     { return ent.dest_ ^ ent.toc_base_off_; }
3566   };
3567
3568   // In a sane world this would be a global.
3569   Target_powerpc<size, big_endian>* targ_;
3570   // Map sym/object/addend to stub offset.
3571   Plt_stub_entries plt_call_stubs_;
3572   // Map destination address to stub offset.
3573   typedef Unordered_map<Branch_stub_ent, unsigned int,
3574                         Branch_stub_ent_hash> Branch_stub_entries;
3575   Branch_stub_entries long_branch_stubs_;
3576   // size of input section
3577   section_size_type orig_data_size_;
3578   // size of stubs
3579   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3580   // Whether .eh_frame info has been created for this stub section.
3581   bool eh_frame_added_;
3582 };
3583
3584 // Make a new stub table, and record.
3585
3586 template<int size, bool big_endian>
3587 Stub_table<size, big_endian>*
3588 Target_powerpc<size, big_endian>::new_stub_table()
3589 {
3590   Stub_table<size, big_endian>* stub_table
3591     = new Stub_table<size, big_endian>(this);
3592   this->stub_tables_.push_back(stub_table);
3593   return stub_table;
3594 }
3595
3596 // Delayed stub table initialisation, because we create the stub table
3597 // before we know to which section it will be attached.
3598
3599 template<int size, bool big_endian>
3600 void
3601 Stub_table<size, big_endian>::init(
3602     const Output_section::Input_section* owner,
3603     Output_section* output_section)
3604 {
3605   this->set_relobj(owner->relobj());
3606   this->set_shndx(owner->shndx());
3607   this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3608   this->set_output_section(output_section);
3609   this->orig_data_size_ = owner->current_data_size();
3610
3611   std::vector<Output_relaxed_input_section*> new_relaxed;
3612   new_relaxed.push_back(this);
3613   output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3614 }
3615
3616 // Add a plt call stub, if we do not already have one for this
3617 // sym/object/addend combo.
3618
3619 template<int size, bool big_endian>
3620 void
3621 Stub_table<size, big_endian>::add_plt_call_entry(
3622     const Sized_relobj_file<size, big_endian>* object,
3623     const Symbol* gsym,
3624     unsigned int r_type,
3625     Address addend)
3626 {
3627   Plt_stub_ent ent(object, gsym, r_type, addend);
3628   Address off = this->plt_size_;
3629   std::pair<typename Plt_stub_entries::iterator, bool> p
3630     = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3631   if (p.second)
3632     this->plt_size_ = off + this->plt_call_size(p.first);
3633 }
3634
3635 template<int size, bool big_endian>
3636 void
3637 Stub_table<size, big_endian>::add_plt_call_entry(
3638     const Sized_relobj_file<size, big_endian>* object,
3639     unsigned int locsym_index,
3640     unsigned int r_type,
3641     Address addend)
3642 {
3643   Plt_stub_ent ent(object, locsym_index, r_type, addend);
3644   Address off = this->plt_size_;
3645   std::pair<typename Plt_stub_entries::iterator, bool> p
3646     = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3647   if (p.second)
3648     this->plt_size_ = off + this->plt_call_size(p.first);
3649 }
3650
3651 // Find a plt call stub.
3652
3653 template<int size, bool big_endian>
3654 typename Stub_table<size, big_endian>::Address
3655 Stub_table<size, big_endian>::find_plt_call_entry(
3656     const Sized_relobj_file<size, big_endian>* object,
3657     const Symbol* gsym,
3658     unsigned int r_type,
3659     Address addend) const
3660 {
3661   Plt_stub_ent ent(object, gsym, r_type, addend);
3662   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3663   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3664 }
3665
3666 template<int size, bool big_endian>
3667 typename Stub_table<size, big_endian>::Address
3668 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3669 {
3670   Plt_stub_ent ent(gsym);
3671   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3672   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3673 }
3674
3675 template<int size, bool big_endian>
3676 typename Stub_table<size, big_endian>::Address
3677 Stub_table<size, big_endian>::find_plt_call_entry(
3678     const Sized_relobj_file<size, big_endian>* object,
3679     unsigned int locsym_index,
3680     unsigned int r_type,
3681     Address addend) const
3682 {
3683   Plt_stub_ent ent(object, locsym_index, r_type, addend);
3684   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3685   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3686 }
3687
3688 template<int size, bool big_endian>
3689 typename Stub_table<size, big_endian>::Address
3690 Stub_table<size, big_endian>::find_plt_call_entry(
3691     const Sized_relobj_file<size, big_endian>* object,
3692     unsigned int locsym_index) const
3693 {
3694   Plt_stub_ent ent(object, locsym_index);
3695   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3696   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3697 }
3698
3699 // Add a long branch stub if we don't already have one to given
3700 // destination.
3701
3702 template<int size, bool big_endian>
3703 void
3704 Stub_table<size, big_endian>::add_long_branch_entry(
3705     const Powerpc_relobj<size, big_endian>* object,
3706     Address to)
3707 {
3708   Branch_stub_ent ent(object, to);
3709   Address off = this->branch_size_;
3710   if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3711     {
3712       unsigned int stub_size = this->branch_stub_size(to);
3713       this->branch_size_ = off + stub_size;
3714       if (size == 64 && stub_size != 4)
3715         this->targ_->add_branch_lookup_table(to);
3716     }
3717 }
3718
3719 // Find long branch stub.
3720
3721 template<int size, bool big_endian>
3722 typename Stub_table<size, big_endian>::Address
3723 Stub_table<size, big_endian>::find_long_branch_entry(
3724     const Powerpc_relobj<size, big_endian>* object,
3725     Address to) const
3726 {
3727   Branch_stub_ent ent(object, to);
3728   typename Branch_stub_entries::const_iterator p
3729     = this->long_branch_stubs_.find(ent);
3730   return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3731 }
3732
3733 // A class to handle .glink.
3734
3735 template<int size, bool big_endian>
3736 class Output_data_glink : public Output_section_data
3737 {
3738  public:
3739   static const int pltresolve_size = 16*4;
3740
3741   Output_data_glink(Target_powerpc<size, big_endian>* targ)
3742     : Output_section_data(16), targ_(targ)
3743   { }
3744
3745   void
3746   add_eh_frame(Layout* layout)
3747   {
3748     if (!parameters->options().ld_generated_unwind_info())
3749       return;
3750
3751     if (size == 64)
3752       layout->add_eh_frame_for_plt(this,
3753                                    Eh_cie<64>::eh_frame_cie,
3754                                    sizeof (Eh_cie<64>::eh_frame_cie),
3755                                    glink_eh_frame_fde_64,
3756                                    sizeof (glink_eh_frame_fde_64));
3757     else
3758       {
3759         // 32-bit .glink can use the default since the CIE return
3760         // address reg, LR, is valid.
3761         layout->add_eh_frame_for_plt(this,
3762                                      Eh_cie<32>::eh_frame_cie,
3763                                      sizeof (Eh_cie<32>::eh_frame_cie),
3764                                      default_fde,
3765                                      sizeof (default_fde));
3766         // Except where LR is used in a PIC __glink_PLTresolve.
3767         if (parameters->options().output_is_position_independent())
3768           layout->add_eh_frame_for_plt(this,
3769                                        Eh_cie<32>::eh_frame_cie,
3770                                        sizeof (Eh_cie<32>::eh_frame_cie),
3771                                        glink_eh_frame_fde_32,
3772                                        sizeof (glink_eh_frame_fde_32));
3773       }
3774   }
3775
3776  protected:
3777   // Write to a map file.
3778   void
3779   do_print_to_mapfile(Mapfile* mapfile) const
3780   { mapfile->print_output_data(this, _("** glink")); }
3781
3782  private:
3783   void
3784   set_final_data_size();
3785
3786   // Write out .glink
3787   void
3788   do_write(Output_file*);
3789
3790   // Allows access to .got and .plt for do_write.
3791   Target_powerpc<size, big_endian>* targ_;
3792 };
3793
3794 template<int size, bool big_endian>
3795 void
3796 Output_data_glink<size, big_endian>::set_final_data_size()
3797 {
3798   unsigned int count = this->targ_->plt_entry_count();
3799   section_size_type total = 0;
3800
3801   if (count != 0)
3802     {
3803       if (size == 32)
3804         {
3805           // space for branch table
3806           total += 4 * (count - 1);
3807
3808           total += -total & 15;
3809           total += this->pltresolve_size;
3810         }
3811       else
3812         {
3813           total += this->pltresolve_size;
3814
3815           // space for branch table
3816           total += 8 * count;
3817           if (count > 0x8000)
3818             total += 4 * (count - 0x8000);
3819         }
3820     }
3821
3822   this->set_data_size(total);
3823 }
3824
3825 // Write out plt and long branch stub code.
3826
3827 template<int size, bool big_endian>
3828 void
3829 Stub_table<size, big_endian>::do_write(Output_file* of)
3830 {
3831   if (this->plt_call_stubs_.empty()
3832       && this->long_branch_stubs_.empty())
3833     return;
3834
3835   const section_size_type start_off = this->offset();
3836   const section_size_type off = this->stub_offset();
3837   const section_size_type oview_size =
3838     convert_to_section_size_type(this->data_size() - (off - start_off));
3839   unsigned char* const oview = of->get_output_view(off, oview_size);
3840   unsigned char* p;
3841
3842   if (size == 64)
3843     {
3844       const Output_data_got_powerpc<size, big_endian>* got
3845         = this->targ_->got_section();
3846       Address got_os_addr = got->output_section()->address();
3847
3848       if (!this->plt_call_stubs_.empty())
3849         {
3850           // The base address of the .plt section.
3851           Address plt_base = this->targ_->plt_section()->address();
3852           Address iplt_base = invalid_address;
3853
3854           // Write out plt call stubs.
3855           typename Plt_stub_entries::const_iterator cs;
3856           for (cs = this->plt_call_stubs_.begin();
3857                cs != this->plt_call_stubs_.end();
3858                ++cs)
3859             {
3860               bool is_iplt;
3861               Address pltoff = this->plt_off(cs, &is_iplt);
3862               Address plt_addr = pltoff;
3863               if (is_iplt)
3864                 {
3865                   if (iplt_base == invalid_address)
3866                     iplt_base = this->targ_->iplt_section()->address();
3867                   plt_addr += iplt_base;
3868                 }
3869               else
3870                 plt_addr += plt_base;
3871               const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3872                 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
3873               Address got_addr = got_os_addr + ppcobj->toc_base_offset();
3874               Address off = plt_addr - got_addr;
3875
3876               if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
3877                 gold_error(_("%s: linkage table error against `%s'"),
3878                            cs->first.object_->name().c_str(),
3879                            cs->first.sym_->demangled_name().c_str());
3880
3881               bool static_chain = parameters->options().plt_static_chain();
3882               bool thread_safe = this->targ_->plt_thread_safe();
3883               bool use_fake_dep = false;
3884               Address cmp_branch_off = 0;
3885               if (thread_safe)
3886                 {
3887                   unsigned int pltindex
3888                     = ((pltoff - this->targ_->first_plt_entry_offset())
3889                        / this->targ_->plt_entry_size());
3890                   Address glinkoff
3891                     = (this->targ_->glink_section()->pltresolve_size
3892                        + pltindex * 8);
3893                   if (pltindex > 32768)
3894                     glinkoff += (pltindex - 32768) * 4;
3895                   Address to
3896                     = this->targ_->glink_section()->address() + glinkoff;
3897                   Address from
3898                     = (this->stub_address() + cs->second + 24
3899                        + 4 * (ha(off) != 0)
3900                        + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
3901                        + 4 * static_chain);
3902                   cmp_branch_off = to - from;
3903                   use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
3904                 }
3905
3906               p = oview + cs->second;
3907               if (ha(off) != 0)
3908                 {
3909                   write_insn<big_endian>(p, std_2_1 + 40),              p += 4;
3910                   write_insn<big_endian>(p, addis_12_2 + ha(off)),      p += 4;
3911                   write_insn<big_endian>(p, ld_11_12 + l(off)),         p += 4;
3912                   if (ha(off + 8 + 8 * static_chain) != ha(off))
3913                     {
3914                       write_insn<big_endian>(p, addi_12_12 + l(off)),   p += 4;
3915                       off = 0;
3916                     }
3917                   write_insn<big_endian>(p, mtctr_11),                  p += 4;
3918                   if (use_fake_dep)
3919                     {
3920                       write_insn<big_endian>(p, xor_11_11_11),          p += 4;
3921                       write_insn<big_endian>(p, add_12_12_11),          p += 4;
3922                     }
3923                   write_insn<big_endian>(p, ld_2_12 + l(off + 8)),      p += 4;
3924                   if (static_chain)
3925                     write_insn<big_endian>(p, ld_11_12 + l(off + 16)),  p += 4;
3926                 }
3927               else
3928                 {
3929                   write_insn<big_endian>(p, std_2_1 + 40),              p += 4;
3930                   write_insn<big_endian>(p, ld_11_2 + l(off)),          p += 4;
3931                   if (ha(off + 8 + 8 * static_chain) != ha(off))
3932                     {
3933                       write_insn<big_endian>(p, addi_2_2 + l(off)),     p += 4;
3934                       off = 0;
3935                     }
3936                   write_insn<big_endian>(p, mtctr_11),                  p += 4;
3937                   if (use_fake_dep)
3938                     {
3939                       write_insn<big_endian>(p, xor_11_11_11),          p += 4;
3940                       write_insn<big_endian>(p, add_2_2_11),            p += 4;
3941                     }
3942                   if (static_chain)
3943                     write_insn<big_endian>(p, ld_11_2 + l(off + 16)),   p += 4;
3944                   write_insn<big_endian>(p, ld_2_2 + l(off + 8)),       p += 4;
3945                 }
3946               if (thread_safe && !use_fake_dep)
3947                 {
3948                   write_insn<big_endian>(p, cmpldi_2_0),                p += 4;
3949                   write_insn<big_endian>(p, bnectr_p4),                 p += 4;
3950                   write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
3951                 }
3952               else
3953                 write_insn<big_endian>(p, bctr);
3954             }
3955         }
3956
3957       // Write out long branch stubs.
3958       typename Branch_stub_entries::const_iterator bs;
3959       for (bs = this->long_branch_stubs_.begin();
3960            bs != this->long_branch_stubs_.end();
3961            ++bs)
3962         {
3963           p = oview + this->plt_size_ + bs->second;
3964           Address loc = this->stub_address() + this->plt_size_ + bs->second;
3965           Address delta = bs->first.dest_ - loc;
3966           if (delta + (1 << 25) < 2 << 25)
3967             write_insn<big_endian>(p, b | (delta & 0x3fffffc));
3968           else
3969             {
3970               Address brlt_addr
3971                 = this->targ_->find_branch_lookup_table(bs->first.dest_);
3972               gold_assert(brlt_addr != invalid_address);
3973               brlt_addr += this->targ_->brlt_section()->address();
3974               Address got_addr = got_os_addr + bs->first.toc_base_off_;
3975               Address brltoff = brlt_addr - got_addr;
3976               if (ha(brltoff) == 0)
3977                 {
3978                   write_insn<big_endian>(p, ld_11_2 + l(brltoff)),      p += 4;
3979                 }
3980               else
3981                 {
3982                   write_insn<big_endian>(p, addis_12_2 + ha(brltoff)),  p += 4;
3983                   write_insn<big_endian>(p, ld_11_12 + l(brltoff)),     p += 4;
3984                 }
3985               write_insn<big_endian>(p, mtctr_11),                      p += 4;
3986               write_insn<big_endian>(p, bctr);
3987             }
3988         }
3989     }
3990   else
3991     {
3992       if (!this->plt_call_stubs_.empty())
3993         {
3994           // The base address of the .plt section.
3995           Address plt_base = this->targ_->plt_section()->address();
3996           Address iplt_base = invalid_address;
3997           // The address of _GLOBAL_OFFSET_TABLE_.
3998           Address g_o_t = invalid_address;
3999
4000           // Write out plt call stubs.
4001           typename Plt_stub_entries::const_iterator cs;
4002           for (cs = this->plt_call_stubs_.begin();
4003                cs != this->plt_call_stubs_.end();
4004                ++cs)
4005             {
4006               bool is_iplt;
4007               Address plt_addr = this->plt_off(cs, &is_iplt);
4008               if (is_iplt)
4009                 {
4010                   if (iplt_base == invalid_address)
4011                     iplt_base = this->targ_->iplt_section()->address();
4012                   plt_addr += iplt_base;
4013                 }
4014               else
4015                 plt_addr += plt_base;
4016
4017               p = oview + cs->second;
4018               if (parameters->options().output_is_position_independent())
4019                 {
4020                   Address got_addr;
4021                   const Powerpc_relobj<size, big_endian>* ppcobj
4022                     = (static_cast<const Powerpc_relobj<size, big_endian>*>
4023                        (cs->first.object_));
4024                   if (ppcobj != NULL && cs->first.addend_ >= 32768)
4025                     {
4026                       unsigned int got2 = ppcobj->got2_shndx();
4027                       got_addr = ppcobj->get_output_section_offset(got2);
4028                       gold_assert(got_addr != invalid_address);
4029                       got_addr += (ppcobj->output_section(got2)->address()
4030                                    + cs->first.addend_);
4031                     }
4032                   else
4033                     {
4034                       if (g_o_t == invalid_address)
4035                         {
4036                           const Output_data_got_powerpc<size, big_endian>* got
4037                             = this->targ_->got_section();
4038                           g_o_t = got->address() + got->g_o_t();
4039                         }
4040                       got_addr = g_o_t;
4041                     }
4042
4043                   Address off = plt_addr - got_addr;
4044                   if (ha(off) == 0)
4045                     {
4046                       write_insn<big_endian>(p +  0, lwz_11_30 + l(off));
4047                       write_insn<big_endian>(p +  4, mtctr_11);
4048                       write_insn<big_endian>(p +  8, bctr);
4049                     }
4050                   else
4051                     {
4052                       write_insn<big_endian>(p +  0, addis_11_30 + ha(off));
4053                       write_insn<big_endian>(p +  4, lwz_11_11 + l(off));
4054                       write_insn<big_endian>(p +  8, mtctr_11);
4055                       write_insn<big_endian>(p + 12, bctr);
4056                     }
4057                 }
4058               else
4059                 {
4060                   write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
4061                   write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
4062                   write_insn<big_endian>(p +  8, mtctr_11);
4063                   write_insn<big_endian>(p + 12, bctr);
4064                 }
4065             }
4066         }
4067
4068       // Write out long branch stubs.
4069       typename Branch_stub_entries::const_iterator bs;
4070       for (bs = this->long_branch_stubs_.begin();
4071            bs != this->long_branch_stubs_.end();
4072            ++bs)
4073         {
4074           p = oview + this->plt_size_ + bs->second;
4075           Address loc = this->stub_address() + this->plt_size_ + bs->second;
4076           Address delta = bs->first.dest_ - loc;
4077           if (delta + (1 << 25) < 2 << 25)
4078             write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4079           else if (!parameters->options().output_is_position_independent())
4080             {
4081               write_insn<big_endian>(p +  0, lis_12 + ha(bs->first.dest_));
4082               write_insn<big_endian>(p +  4, addi_12_12 + l(bs->first.dest_));
4083               write_insn<big_endian>(p +  8, mtctr_12);
4084               write_insn<big_endian>(p + 12, bctr);
4085             }
4086           else
4087             {
4088               delta -= 8;
4089               write_insn<big_endian>(p +  0, mflr_0);
4090               write_insn<big_endian>(p +  4, bcl_20_31);
4091               write_insn<big_endian>(p +  8, mflr_12);
4092               write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4093               write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4094               write_insn<big_endian>(p + 20, mtlr_0);
4095               write_insn<big_endian>(p + 24, mtctr_12);
4096               write_insn<big_endian>(p + 28, bctr);
4097             }
4098         }
4099     }
4100 }
4101
4102 // Write out .glink.
4103
4104 template<int size, bool big_endian>
4105 void
4106 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4107 {
4108   const section_size_type off = this->offset();
4109   const section_size_type oview_size =
4110     convert_to_section_size_type(this->data_size());
4111   unsigned char* const oview = of->get_output_view(off, oview_size);
4112   unsigned char* p;
4113
4114   // The base address of the .plt section.
4115   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4116   Address plt_base = this->targ_->plt_section()->address();
4117
4118   if (size == 64)
4119     {
4120       // Write pltresolve stub.
4121       p = oview;
4122       Address after_bcl = this->address() + 16;
4123       Address pltoff = plt_base - after_bcl;
4124
4125       elfcpp::Swap<64, big_endian>::writeval(p, pltoff),        p += 8;
4126
4127       write_insn<big_endian>(p, mflr_12),                       p += 4;
4128       write_insn<big_endian>(p, bcl_20_31),                     p += 4;
4129       write_insn<big_endian>(p, mflr_11),                       p += 4;
4130       write_insn<big_endian>(p, ld_2_11 + l(-16)),              p += 4;
4131       write_insn<big_endian>(p, mtlr_12),                       p += 4;
4132       write_insn<big_endian>(p, add_12_2_11),                   p += 4;
4133       write_insn<big_endian>(p, ld_11_12 + 0),                  p += 4;
4134       write_insn<big_endian>(p, ld_2_12 + 8),                   p += 4;
4135       write_insn<big_endian>(p, mtctr_11),                      p += 4;
4136       write_insn<big_endian>(p, ld_11_12 + 16),                 p += 4;
4137       write_insn<big_endian>(p, bctr),                          p += 4;
4138       while (p < oview + this->pltresolve_size)
4139         write_insn<big_endian>(p, nop), p += 4;
4140
4141       // Write lazy link call stubs.
4142       uint32_t indx = 0;
4143       while (p < oview + oview_size)
4144         {
4145           if (indx < 0x8000)
4146             {
4147               write_insn<big_endian>(p, li_0_0 + indx),                 p += 4;
4148             }
4149           else
4150             {
4151               write_insn<big_endian>(p, lis_0_0 + hi(indx)),            p += 4;
4152               write_insn<big_endian>(p, ori_0_0_0 + l(indx)),           p += 4;
4153             }
4154           uint32_t branch_off = 8 - (p - oview);
4155           write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),      p += 4;
4156           indx++;
4157         }
4158     }
4159   else
4160     {
4161       const Output_data_got_powerpc<size, big_endian>* got
4162         = this->targ_->got_section();
4163       // The address of _GLOBAL_OFFSET_TABLE_.
4164       Address g_o_t = got->address() + got->g_o_t();
4165
4166       // Write out pltresolve branch table.
4167       p = oview;
4168       unsigned int the_end = oview_size - this->pltresolve_size;
4169       unsigned char* end_p = oview + the_end;
4170       while (p < end_p - 8 * 4)
4171         write_insn<big_endian>(p, b + end_p - p), p += 4;
4172       while (p < end_p)
4173         write_insn<big_endian>(p, nop), p += 4;
4174
4175       // Write out pltresolve call stub.
4176       if (parameters->options().output_is_position_independent())
4177         {
4178           Address res0_off = 0;
4179           Address after_bcl_off = the_end + 12;
4180           Address bcl_res0 = after_bcl_off - res0_off;
4181
4182           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
4183           write_insn<big_endian>(p +  4, mflr_0);
4184           write_insn<big_endian>(p +  8, bcl_20_31);
4185           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4186           write_insn<big_endian>(p + 16, mflr_12);
4187           write_insn<big_endian>(p + 20, mtlr_0);
4188           write_insn<big_endian>(p + 24, sub_11_11_12);
4189
4190           Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4191
4192           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4193           if (ha(got_bcl) == ha(got_bcl + 4))
4194             {
4195               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4196               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4197             }
4198           else
4199             {
4200               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4201               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4202             }
4203           write_insn<big_endian>(p + 40, mtctr_0);
4204           write_insn<big_endian>(p + 44, add_0_11_11);
4205           write_insn<big_endian>(p + 48, add_11_0_11);
4206           write_insn<big_endian>(p + 52, bctr);
4207           write_insn<big_endian>(p + 56, nop);
4208           write_insn<big_endian>(p + 60, nop);
4209         }
4210       else
4211         {
4212           Address res0 = this->address();
4213
4214           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4215           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4216           if (ha(g_o_t + 4) == ha(g_o_t + 8))
4217             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4218           else
4219             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4220           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4221           write_insn<big_endian>(p + 16, mtctr_0);
4222           write_insn<big_endian>(p + 20, add_0_11_11);
4223           if (ha(g_o_t + 4) == ha(g_o_t + 8))
4224             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4225           else
4226             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4227           write_insn<big_endian>(p + 28, add_11_0_11);
4228           write_insn<big_endian>(p + 32, bctr);
4229           write_insn<big_endian>(p + 36, nop);
4230           write_insn<big_endian>(p + 40, nop);
4231           write_insn<big_endian>(p + 44, nop);
4232           write_insn<big_endian>(p + 48, nop);
4233           write_insn<big_endian>(p + 52, nop);
4234           write_insn<big_endian>(p + 56, nop);
4235           write_insn<big_endian>(p + 60, nop);
4236         }
4237       p += 64;
4238     }
4239
4240   of->write_output_view(off, oview_size, oview);
4241 }
4242
4243
4244 // A class to handle linker generated save/restore functions.
4245
4246 template<int size, bool big_endian>
4247 class Output_data_save_res : public Output_section_data_build
4248 {
4249  public:
4250   Output_data_save_res(Symbol_table* symtab);
4251
4252  protected:
4253   // Write to a map file.
4254   void
4255   do_print_to_mapfile(Mapfile* mapfile) const
4256   { mapfile->print_output_data(this, _("** save/restore")); }
4257
4258   void
4259   do_write(Output_file*);
4260
4261  private:
4262   // The maximum size of save/restore contents.
4263   static const unsigned int savres_max = 218*4;
4264
4265   void
4266   savres_define(Symbol_table* symtab,
4267                 const char *name,
4268                 unsigned int lo, unsigned int hi,
4269                 unsigned char* write_ent(unsigned char*, int),
4270                 unsigned char* write_tail(unsigned char*, int));
4271
4272   unsigned char *contents_;
4273 };
4274
4275 template<bool big_endian>
4276 static unsigned char*
4277 savegpr0(unsigned char* p, int r)
4278 {
4279   uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4280   write_insn<big_endian>(p, insn);
4281   return p + 4;
4282 }
4283
4284 template<bool big_endian>
4285 static unsigned char*
4286 savegpr0_tail(unsigned char* p, int r)
4287 {
4288   p = savegpr0<big_endian>(p, r);
4289   uint32_t insn = std_0_1 + 16;
4290   write_insn<big_endian>(p, insn);
4291   p = p + 4;
4292   write_insn<big_endian>(p, blr);
4293   return p + 4;
4294 }
4295
4296 template<bool big_endian>
4297 static unsigned char*
4298 restgpr0(unsigned char* p, int r)
4299 {
4300   uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4301   write_insn<big_endian>(p, insn);
4302   return p + 4;
4303 }
4304
4305 template<bool big_endian>
4306 static unsigned char*
4307 restgpr0_tail(unsigned char* p, int r)
4308 {
4309   uint32_t insn = ld_0_1 + 16;
4310   write_insn<big_endian>(p, insn);
4311   p = p + 4;
4312   p = restgpr0<big_endian>(p, r);
4313   write_insn<big_endian>(p, mtlr_0);
4314   p = p + 4;
4315   if (r == 29)
4316     {
4317       p = restgpr0<big_endian>(p, 30);
4318       p = restgpr0<big_endian>(p, 31);
4319     }
4320   write_insn<big_endian>(p, blr);
4321   return p + 4;
4322 }
4323
4324 template<bool big_endian>
4325 static unsigned char*
4326 savegpr1(unsigned char* p, int r)
4327 {
4328   uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4329   write_insn<big_endian>(p, insn);
4330   return p + 4;
4331 }
4332
4333 template<bool big_endian>
4334 static unsigned char*
4335 savegpr1_tail(unsigned char* p, int r)
4336 {
4337   p = savegpr1<big_endian>(p, r);
4338   write_insn<big_endian>(p, blr);
4339   return p + 4;
4340 }
4341
4342 template<bool big_endian>
4343 static unsigned char*
4344 restgpr1(unsigned char* p, int r)
4345 {
4346   uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4347   write_insn<big_endian>(p, insn);
4348   return p + 4;
4349 }
4350
4351 template<bool big_endian>
4352 static unsigned char*
4353 restgpr1_tail(unsigned char* p, int r)
4354 {
4355   p = restgpr1<big_endian>(p, r);
4356   write_insn<big_endian>(p, blr);
4357   return p + 4;
4358 }
4359
4360 template<bool big_endian>
4361 static unsigned char*
4362 savefpr(unsigned char* p, int r)
4363 {
4364   uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4365   write_insn<big_endian>(p, insn);
4366   return p + 4;
4367 }
4368
4369 template<bool big_endian>
4370 static unsigned char*
4371 savefpr0_tail(unsigned char* p, int r)
4372 {
4373   p = savefpr<big_endian>(p, r);
4374   write_insn<big_endian>(p, std_0_1 + 16);
4375   p = p + 4;
4376   write_insn<big_endian>(p, blr);
4377   return p + 4;
4378 }
4379
4380 template<bool big_endian>
4381 static unsigned char*
4382 restfpr(unsigned char* p, int r)
4383 {
4384   uint32_t insn = lfd_0_1 + (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 restfpr0_tail(unsigned char* p, int r)
4392 {
4393   write_insn<big_endian>(p, ld_0_1 + 16);
4394   p = p + 4;
4395   p = restfpr<big_endian>(p, r);
4396   write_insn<big_endian>(p, mtlr_0);
4397   p = p + 4;
4398   if (r == 29)
4399     {
4400       p = restfpr<big_endian>(p, 30);
4401       p = restfpr<big_endian>(p, 31);
4402     }
4403   write_insn<big_endian>(p, blr);
4404   return p + 4;
4405 }
4406
4407 template<bool big_endian>
4408 static unsigned char*
4409 savefpr1_tail(unsigned char* p, int r)
4410 {
4411   p = savefpr<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 restfpr1_tail(unsigned char* p, int r)
4419 {
4420   p = restfpr<big_endian>(p, r);
4421   write_insn<big_endian>(p, blr);
4422   return p + 4;
4423 }
4424
4425 template<bool big_endian>
4426 static unsigned char*
4427 savevr(unsigned char* p, int r)
4428 {
4429   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4430   write_insn<big_endian>(p, insn);
4431   p = p + 4;
4432   insn = stvx_0_12_0 + (r << 21);
4433   write_insn<big_endian>(p, insn);
4434   return p + 4;
4435 }
4436
4437 template<bool big_endian>
4438 static unsigned char*
4439 savevr_tail(unsigned char* p, int r)
4440 {
4441   p = savevr<big_endian>(p, r);
4442   write_insn<big_endian>(p, blr);
4443   return p + 4;
4444 }
4445
4446 template<bool big_endian>
4447 static unsigned char*
4448 restvr(unsigned char* p, int r)
4449 {
4450   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4451   write_insn<big_endian>(p, insn);
4452   p = p + 4;
4453   insn = lvx_0_12_0 + (r << 21);
4454   write_insn<big_endian>(p, insn);
4455   return p + 4;
4456 }
4457
4458 template<bool big_endian>
4459 static unsigned char*
4460 restvr_tail(unsigned char* p, int r)
4461 {
4462   p = restvr<big_endian>(p, r);
4463   write_insn<big_endian>(p, blr);
4464   return p + 4;
4465 }
4466
4467
4468 template<int size, bool big_endian>
4469 Output_data_save_res<size, big_endian>::Output_data_save_res(
4470     Symbol_table* symtab)
4471   : Output_section_data_build(4),
4472     contents_(NULL)
4473 {
4474   this->savres_define(symtab,
4475                       "_savegpr0_", 14, 31,
4476                       savegpr0<big_endian>, savegpr0_tail<big_endian>);
4477   this->savres_define(symtab,
4478                       "_restgpr0_", 14, 29,
4479                       restgpr0<big_endian>, restgpr0_tail<big_endian>);
4480   this->savres_define(symtab,
4481                       "_restgpr0_", 30, 31,
4482                       restgpr0<big_endian>, restgpr0_tail<big_endian>);
4483   this->savres_define(symtab,
4484                       "_savegpr1_", 14, 31,
4485                       savegpr1<big_endian>, savegpr1_tail<big_endian>);
4486   this->savres_define(symtab,
4487                       "_restgpr1_", 14, 31,
4488                       restgpr1<big_endian>, restgpr1_tail<big_endian>);
4489   this->savres_define(symtab,
4490                       "_savefpr_", 14, 31,
4491                       savefpr<big_endian>, savefpr0_tail<big_endian>);
4492   this->savres_define(symtab,
4493                       "_restfpr_", 14, 29,
4494                       restfpr<big_endian>, restfpr0_tail<big_endian>);
4495   this->savres_define(symtab,
4496                       "_restfpr_", 30, 31,
4497                       restfpr<big_endian>, restfpr0_tail<big_endian>);
4498   this->savres_define(symtab,
4499                       "._savef", 14, 31,
4500                       savefpr<big_endian>, savefpr1_tail<big_endian>);
4501   this->savres_define(symtab,
4502                       "._restf", 14, 31,
4503                       restfpr<big_endian>, restfpr1_tail<big_endian>);
4504   this->savres_define(symtab,
4505                       "_savevr_", 20, 31,
4506                       savevr<big_endian>, savevr_tail<big_endian>);
4507   this->savres_define(symtab,
4508                       "_restvr_", 20, 31,
4509                       restvr<big_endian>, restvr_tail<big_endian>);
4510 }
4511
4512 template<int size, bool big_endian>
4513 void
4514 Output_data_save_res<size, big_endian>::savres_define(
4515     Symbol_table* symtab,
4516     const char *name,
4517     unsigned int lo, unsigned int hi,
4518     unsigned char* write_ent(unsigned char*, int),
4519     unsigned char* write_tail(unsigned char*, int))
4520 {
4521   size_t len = strlen(name);
4522   bool writing = false;
4523   char sym[16];
4524
4525   memcpy(sym, name, len);
4526   sym[len + 2] = 0;
4527
4528   for (unsigned int i = lo; i <= hi; i++)
4529     {
4530       sym[len + 0] = i / 10 + '0';
4531       sym[len + 1] = i % 10 + '0';
4532       Symbol* gsym = symtab->lookup(sym);
4533       bool refd = gsym != NULL && gsym->is_undefined();
4534       writing = writing || refd;
4535       if (writing)
4536         {
4537           if (this->contents_ == NULL)
4538             this->contents_ = new unsigned char[this->savres_max];
4539
4540           section_size_type value = this->current_data_size();
4541           unsigned char* p = this->contents_ + value;
4542           if (i != hi)
4543             p = write_ent(p, i);
4544           else
4545             p = write_tail(p, i);
4546           section_size_type cur_size = p - this->contents_;
4547           this->set_current_data_size(cur_size);
4548           if (refd)
4549             symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4550                                           this, value, cur_size - value,
4551                                           elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4552                                           elfcpp::STV_HIDDEN, 0, false, false);
4553         }
4554     }
4555 }
4556
4557 // Write out save/restore.
4558
4559 template<int size, bool big_endian>
4560 void
4561 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4562 {
4563   const section_size_type off = this->offset();
4564   const section_size_type oview_size =
4565     convert_to_section_size_type(this->data_size());
4566   unsigned char* const oview = of->get_output_view(off, oview_size);
4567   memcpy(oview, this->contents_, oview_size);
4568   of->write_output_view(off, oview_size, oview);
4569 }
4570
4571
4572 // Create the glink section.
4573
4574 template<int size, bool big_endian>
4575 void
4576 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4577 {
4578   if (this->glink_ == NULL)
4579     {
4580       this->glink_ = new Output_data_glink<size, big_endian>(this);
4581       this->glink_->add_eh_frame(layout);
4582       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
4583                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
4584                                       this->glink_, ORDER_TEXT, false);
4585     }
4586 }
4587
4588 // Create a PLT entry for a global symbol.
4589
4590 template<int size, bool big_endian>
4591 void
4592 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
4593                                                  Layout* layout,
4594                                                  Symbol* gsym)
4595 {
4596   if (gsym->type() == elfcpp::STT_GNU_IFUNC
4597       && gsym->can_use_relative_reloc(false))
4598     {
4599       if (this->iplt_ == NULL)
4600         this->make_iplt_section(symtab, layout);
4601       this->iplt_->add_ifunc_entry(gsym);
4602     }
4603   else
4604     {
4605       if (this->plt_ == NULL)
4606         this->make_plt_section(symtab, layout);
4607       this->plt_->add_entry(gsym);
4608     }
4609 }
4610
4611 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
4612
4613 template<int size, bool big_endian>
4614 void
4615 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
4616     Symbol_table* symtab,
4617     Layout* layout,
4618     Sized_relobj_file<size, big_endian>* relobj,
4619     unsigned int r_sym)
4620 {
4621   if (this->iplt_ == NULL)
4622     this->make_iplt_section(symtab, layout);
4623   this->iplt_->add_local_ifunc_entry(relobj, r_sym);
4624 }
4625
4626 // Return the number of entries in the PLT.
4627
4628 template<int size, bool big_endian>
4629 unsigned int
4630 Target_powerpc<size, big_endian>::plt_entry_count() const
4631 {
4632   if (this->plt_ == NULL)
4633     return 0;
4634   return this->plt_->entry_count();
4635 }
4636
4637 // Return the offset of the first non-reserved PLT entry.
4638
4639 template<int size, bool big_endian>
4640 unsigned int
4641 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
4642 {
4643   return this->plt_->first_plt_entry_offset();
4644 }
4645
4646 // Return the size of each PLT entry.
4647
4648 template<int size, bool big_endian>
4649 unsigned int
4650 Target_powerpc<size, big_endian>::plt_entry_size() const
4651 {
4652   return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
4653 }
4654
4655 // Create a GOT entry for local dynamic __tls_get_addr calls.
4656
4657 template<int size, bool big_endian>
4658 unsigned int
4659 Target_powerpc<size, big_endian>::tlsld_got_offset(
4660     Symbol_table* symtab,
4661     Layout* layout,
4662     Sized_relobj_file<size, big_endian>* object)
4663 {
4664   if (this->tlsld_got_offset_ == -1U)
4665     {
4666       gold_assert(symtab != NULL && layout != NULL && object != NULL);
4667       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
4668       Output_data_got_powerpc<size, big_endian>* got
4669         = this->got_section(symtab, layout);
4670       unsigned int got_offset = got->add_constant_pair(0, 0);
4671       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
4672                           got_offset, 0);
4673       this->tlsld_got_offset_ = got_offset;
4674     }
4675   return this->tlsld_got_offset_;
4676 }
4677
4678 // Get the Reference_flags for a particular relocation.
4679
4680 template<int size, bool big_endian>
4681 int
4682 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
4683 {
4684   switch (r_type)
4685     {
4686     case elfcpp::R_POWERPC_NONE:
4687     case elfcpp::R_POWERPC_GNU_VTINHERIT:
4688     case elfcpp::R_POWERPC_GNU_VTENTRY:
4689     case elfcpp::R_PPC64_TOC:
4690       // No symbol reference.
4691       return 0;
4692
4693     case elfcpp::R_PPC64_ADDR64:
4694     case elfcpp::R_PPC64_UADDR64:
4695     case elfcpp::R_POWERPC_ADDR32:
4696     case elfcpp::R_POWERPC_UADDR32:
4697     case elfcpp::R_POWERPC_ADDR16:
4698     case elfcpp::R_POWERPC_UADDR16:
4699     case elfcpp::R_POWERPC_ADDR16_LO:
4700     case elfcpp::R_POWERPC_ADDR16_HI:
4701     case elfcpp::R_POWERPC_ADDR16_HA:
4702       return Symbol::ABSOLUTE_REF;
4703
4704     case elfcpp::R_POWERPC_ADDR24:
4705     case elfcpp::R_POWERPC_ADDR14:
4706     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4707     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4708       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
4709
4710     case elfcpp::R_PPC64_REL64:
4711     case elfcpp::R_POWERPC_REL32:
4712     case elfcpp::R_PPC_LOCAL24PC:
4713     case elfcpp::R_POWERPC_REL16:
4714     case elfcpp::R_POWERPC_REL16_LO:
4715     case elfcpp::R_POWERPC_REL16_HI:
4716     case elfcpp::R_POWERPC_REL16_HA:
4717       return Symbol::RELATIVE_REF;
4718
4719     case elfcpp::R_POWERPC_REL24:
4720     case elfcpp::R_PPC_PLTREL24:
4721     case elfcpp::R_POWERPC_REL14:
4722     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4723     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4724       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
4725
4726     case elfcpp::R_POWERPC_GOT16:
4727     case elfcpp::R_POWERPC_GOT16_LO:
4728     case elfcpp::R_POWERPC_GOT16_HI:
4729     case elfcpp::R_POWERPC_GOT16_HA:
4730     case elfcpp::R_PPC64_GOT16_DS:
4731     case elfcpp::R_PPC64_GOT16_LO_DS:
4732     case elfcpp::R_PPC64_TOC16:
4733     case elfcpp::R_PPC64_TOC16_LO:
4734     case elfcpp::R_PPC64_TOC16_HI:
4735     case elfcpp::R_PPC64_TOC16_HA:
4736     case elfcpp::R_PPC64_TOC16_DS:
4737     case elfcpp::R_PPC64_TOC16_LO_DS:
4738       // Absolute in GOT.
4739       return Symbol::ABSOLUTE_REF;
4740
4741     case elfcpp::R_POWERPC_GOT_TPREL16:
4742     case elfcpp::R_POWERPC_TLS:
4743       return Symbol::TLS_REF;
4744
4745     case elfcpp::R_POWERPC_COPY:
4746     case elfcpp::R_POWERPC_GLOB_DAT:
4747     case elfcpp::R_POWERPC_JMP_SLOT:
4748     case elfcpp::R_POWERPC_RELATIVE:
4749     case elfcpp::R_POWERPC_DTPMOD:
4750     default:
4751       // Not expected.  We will give an error later.
4752       return 0;
4753     }
4754 }
4755
4756 // Report an unsupported relocation against a local symbol.
4757
4758 template<int size, bool big_endian>
4759 void
4760 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
4761     Sized_relobj_file<size, big_endian>* object,
4762     unsigned int r_type)
4763 {
4764   gold_error(_("%s: unsupported reloc %u against local symbol"),
4765              object->name().c_str(), r_type);
4766 }
4767
4768 // We are about to emit a dynamic relocation of type R_TYPE.  If the
4769 // dynamic linker does not support it, issue an error.
4770
4771 template<int size, bool big_endian>
4772 void
4773 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
4774                                                       unsigned int r_type)
4775 {
4776   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
4777
4778   // These are the relocation types supported by glibc for both 32-bit
4779   // and 64-bit powerpc.
4780   switch (r_type)
4781     {
4782     case elfcpp::R_POWERPC_NONE:
4783     case elfcpp::R_POWERPC_RELATIVE:
4784     case elfcpp::R_POWERPC_GLOB_DAT:
4785     case elfcpp::R_POWERPC_DTPMOD:
4786     case elfcpp::R_POWERPC_DTPREL:
4787     case elfcpp::R_POWERPC_TPREL:
4788     case elfcpp::R_POWERPC_JMP_SLOT:
4789     case elfcpp::R_POWERPC_COPY:
4790     case elfcpp::R_POWERPC_IRELATIVE:
4791     case elfcpp::R_POWERPC_ADDR32:
4792     case elfcpp::R_POWERPC_UADDR32:
4793     case elfcpp::R_POWERPC_ADDR24:
4794     case elfcpp::R_POWERPC_ADDR16:
4795     case elfcpp::R_POWERPC_UADDR16:
4796     case elfcpp::R_POWERPC_ADDR16_LO:
4797     case elfcpp::R_POWERPC_ADDR16_HI:
4798     case elfcpp::R_POWERPC_ADDR16_HA:
4799     case elfcpp::R_POWERPC_ADDR14:
4800     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4801     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4802     case elfcpp::R_POWERPC_REL32:
4803     case elfcpp::R_POWERPC_REL24:
4804     case elfcpp::R_POWERPC_TPREL16:
4805     case elfcpp::R_POWERPC_TPREL16_LO:
4806     case elfcpp::R_POWERPC_TPREL16_HI:
4807     case elfcpp::R_POWERPC_TPREL16_HA:
4808       return;
4809
4810     default:
4811       break;
4812     }
4813
4814   if (size == 64)
4815     {
4816       switch (r_type)
4817         {
4818           // These are the relocation types supported only on 64-bit.
4819         case elfcpp::R_PPC64_ADDR64:
4820         case elfcpp::R_PPC64_UADDR64:
4821         case elfcpp::R_PPC64_JMP_IREL:
4822         case elfcpp::R_PPC64_ADDR16_DS:
4823         case elfcpp::R_PPC64_ADDR16_LO_DS:
4824         case elfcpp::R_PPC64_ADDR16_HIGHER:
4825         case elfcpp::R_PPC64_ADDR16_HIGHEST:
4826         case elfcpp::R_PPC64_ADDR16_HIGHERA:
4827         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
4828         case elfcpp::R_PPC64_REL64:
4829         case elfcpp::R_POWERPC_ADDR30:
4830         case elfcpp::R_PPC64_TPREL16_DS:
4831         case elfcpp::R_PPC64_TPREL16_LO_DS:
4832         case elfcpp::R_PPC64_TPREL16_HIGHER:
4833         case elfcpp::R_PPC64_TPREL16_HIGHEST:
4834         case elfcpp::R_PPC64_TPREL16_HIGHERA:
4835         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
4836           return;
4837
4838         default:
4839           break;
4840         }
4841     }
4842   else
4843     {
4844       switch (r_type)
4845         {
4846           // These are the relocation types supported only on 32-bit.
4847           // ??? glibc ld.so doesn't need to support these.
4848         case elfcpp::R_POWERPC_DTPREL16:
4849         case elfcpp::R_POWERPC_DTPREL16_LO:
4850         case elfcpp::R_POWERPC_DTPREL16_HI:
4851         case elfcpp::R_POWERPC_DTPREL16_HA:
4852           return;
4853
4854         default:
4855           break;
4856         }
4857     }
4858
4859   // This prevents us from issuing more than one error per reloc
4860   // section.  But we can still wind up issuing more than one
4861   // error per object file.
4862   if (this->issued_non_pic_error_)
4863     return;
4864   gold_assert(parameters->options().output_is_position_independent());
4865   object->error(_("requires unsupported dynamic reloc; "
4866                   "recompile with -fPIC"));
4867   this->issued_non_pic_error_ = true;
4868   return;
4869 }
4870
4871 // Return whether we need to make a PLT entry for a relocation of the
4872 // given type against a STT_GNU_IFUNC symbol.
4873
4874 template<int size, bool big_endian>
4875 bool
4876 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
4877      Sized_relobj_file<size, big_endian>* object,
4878      unsigned int r_type,
4879      bool report_err)
4880 {
4881   // In non-pic code any reference will resolve to the plt call stub
4882   // for the ifunc symbol.
4883   if (size == 32 && !parameters->options().output_is_position_independent())
4884     return true;
4885
4886   switch (r_type)
4887     {
4888     // Word size refs from data sections are OK, but don't need a PLT entry.
4889     case elfcpp::R_POWERPC_ADDR32:
4890     case elfcpp::R_POWERPC_UADDR32:
4891       if (size == 32)
4892         return false;
4893       break;
4894
4895     case elfcpp::R_PPC64_ADDR64:
4896     case elfcpp::R_PPC64_UADDR64:
4897       if (size == 64)
4898         return false;
4899       break;
4900
4901     // GOT refs are good, but also don't need a PLT entry.
4902     case elfcpp::R_POWERPC_GOT16:
4903     case elfcpp::R_POWERPC_GOT16_LO:
4904     case elfcpp::R_POWERPC_GOT16_HI:
4905     case elfcpp::R_POWERPC_GOT16_HA:
4906     case elfcpp::R_PPC64_GOT16_DS:
4907     case elfcpp::R_PPC64_GOT16_LO_DS:
4908       return false;
4909
4910     // Function calls are good, and these do need a PLT entry.
4911     case elfcpp::R_POWERPC_ADDR24:
4912     case elfcpp::R_POWERPC_ADDR14:
4913     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
4914     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
4915     case elfcpp::R_POWERPC_REL24:
4916     case elfcpp::R_PPC_PLTREL24:
4917     case elfcpp::R_POWERPC_REL14:
4918     case elfcpp::R_POWERPC_REL14_BRTAKEN:
4919     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
4920       return true;
4921
4922     default:
4923       break;
4924     }
4925
4926   // Anything else is a problem.
4927   // If we are building a static executable, the libc startup function
4928   // responsible for applying indirect function relocations is going
4929   // to complain about the reloc type.
4930   // If we are building a dynamic executable, we will have a text
4931   // relocation.  The dynamic loader will set the text segment
4932   // writable and non-executable to apply text relocations.  So we'll
4933   // segfault when trying to run the indirection function to resolve
4934   // the reloc.
4935   if (report_err)
4936     gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
4937                object->name().c_str(), r_type);
4938   return false;
4939 }
4940
4941 // Scan a relocation for a local symbol.
4942
4943 template<int size, bool big_endian>
4944 inline void
4945 Target_powerpc<size, big_endian>::Scan::local(
4946     Symbol_table* symtab,
4947     Layout* layout,
4948     Target_powerpc<size, big_endian>* target,
4949     Sized_relobj_file<size, big_endian>* object,
4950     unsigned int data_shndx,
4951     Output_section* output_section,
4952     const elfcpp::Rela<size, big_endian>& reloc,
4953     unsigned int r_type,
4954     const elfcpp::Sym<size, big_endian>& lsym,
4955     bool is_discarded)
4956 {
4957   this->maybe_skip_tls_get_addr_call(r_type, NULL);
4958
4959   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4960       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4961     {
4962       this->expect_tls_get_addr_call();
4963       const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
4964       if (tls_type != tls::TLSOPT_NONE)
4965         this->skip_next_tls_get_addr_call();
4966     }
4967   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4968            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4969     {
4970       this->expect_tls_get_addr_call();
4971       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
4972       if (tls_type != tls::TLSOPT_NONE)
4973         this->skip_next_tls_get_addr_call();
4974     }
4975
4976   Powerpc_relobj<size, big_endian>* ppc_object
4977     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
4978
4979   if (is_discarded)
4980     {
4981       if (size == 64
4982           && data_shndx == ppc_object->opd_shndx()
4983           && r_type == elfcpp::R_PPC64_ADDR64)
4984         ppc_object->set_opd_discard(reloc.get_r_offset());
4985       return;
4986     }
4987
4988   // A local STT_GNU_IFUNC symbol may require a PLT entry.
4989   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
4990   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
4991     {
4992       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
4993       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
4994                           r_type, r_sym, reloc.get_r_addend());
4995       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
4996     }
4997
4998   switch (r_type)
4999     {
5000     case elfcpp::R_POWERPC_NONE:
5001     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5002     case elfcpp::R_POWERPC_GNU_VTENTRY:
5003     case elfcpp::R_PPC64_TOCSAVE:
5004     case elfcpp::R_PPC_EMB_MRKREF:
5005     case elfcpp::R_POWERPC_TLS:
5006       break;
5007
5008     case elfcpp::R_PPC64_TOC:
5009       {
5010         Output_data_got_powerpc<size, big_endian>* got
5011           = target->got_section(symtab, layout);
5012         if (parameters->options().output_is_position_independent())
5013           {
5014             Address off = reloc.get_r_offset();
5015             if (size == 64
5016                 && data_shndx == ppc_object->opd_shndx()
5017                 && ppc_object->get_opd_discard(off - 8))
5018               break;
5019
5020             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5021             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5022             rela_dyn->add_output_section_relative(got->output_section(),
5023                                                   elfcpp::R_POWERPC_RELATIVE,
5024                                                   output_section,
5025                                                   object, data_shndx, off,
5026                                                   symobj->toc_base_offset());
5027           }
5028       }
5029       break;
5030
5031     case elfcpp::R_PPC64_ADDR64:
5032     case elfcpp::R_PPC64_UADDR64:
5033     case elfcpp::R_POWERPC_ADDR32:
5034     case elfcpp::R_POWERPC_UADDR32:
5035     case elfcpp::R_POWERPC_ADDR24:
5036     case elfcpp::R_POWERPC_ADDR16:
5037     case elfcpp::R_POWERPC_ADDR16_LO:
5038     case elfcpp::R_POWERPC_ADDR16_HI:
5039     case elfcpp::R_POWERPC_ADDR16_HA:
5040     case elfcpp::R_POWERPC_UADDR16:
5041     case elfcpp::R_PPC64_ADDR16_HIGHER:
5042     case elfcpp::R_PPC64_ADDR16_HIGHERA:
5043     case elfcpp::R_PPC64_ADDR16_HIGHEST:
5044     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5045     case elfcpp::R_PPC64_ADDR16_DS:
5046     case elfcpp::R_PPC64_ADDR16_LO_DS:
5047     case elfcpp::R_POWERPC_ADDR14:
5048     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5049     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5050       // If building a shared library (or a position-independent
5051       // executable), we need to create a dynamic relocation for
5052       // this location.
5053       if (parameters->options().output_is_position_independent()
5054           || (size == 64 && is_ifunc))
5055         {
5056           Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5057                                                              is_ifunc);
5058           if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5059               || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5060             {
5061               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5062               unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5063                                      : elfcpp::R_POWERPC_RELATIVE);
5064               rela_dyn->add_local_relative(object, r_sym, dynrel,
5065                                            output_section, data_shndx,
5066                                            reloc.get_r_offset(),
5067                                            reloc.get_r_addend(), false);
5068             }
5069           else
5070             {
5071               check_non_pic(object, r_type);
5072               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5073               rela_dyn->add_local(object, r_sym, r_type, output_section,
5074                                   data_shndx, reloc.get_r_offset(),
5075                                   reloc.get_r_addend());
5076             }
5077         }
5078       break;
5079
5080     case elfcpp::R_POWERPC_REL24:
5081     case elfcpp::R_PPC_PLTREL24:
5082     case elfcpp::R_PPC_LOCAL24PC:
5083     case elfcpp::R_POWERPC_REL14:
5084     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5085     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5086       if (!is_ifunc)
5087         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5088                             r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5089                             reloc.get_r_addend());
5090       break;
5091
5092     case elfcpp::R_PPC64_REL64:
5093     case elfcpp::R_POWERPC_REL32:
5094     case elfcpp::R_POWERPC_REL16:
5095     case elfcpp::R_POWERPC_REL16_LO:
5096     case elfcpp::R_POWERPC_REL16_HI:
5097     case elfcpp::R_POWERPC_REL16_HA:
5098     case elfcpp::R_POWERPC_SECTOFF:
5099     case elfcpp::R_POWERPC_TPREL16:
5100     case elfcpp::R_POWERPC_DTPREL16:
5101     case elfcpp::R_POWERPC_SECTOFF_LO:
5102     case elfcpp::R_POWERPC_TPREL16_LO:
5103     case elfcpp::R_POWERPC_DTPREL16_LO:
5104     case elfcpp::R_POWERPC_SECTOFF_HI:
5105     case elfcpp::R_POWERPC_TPREL16_HI:
5106     case elfcpp::R_POWERPC_DTPREL16_HI:
5107     case elfcpp::R_POWERPC_SECTOFF_HA:
5108     case elfcpp::R_POWERPC_TPREL16_HA:
5109     case elfcpp::R_POWERPC_DTPREL16_HA:
5110     case elfcpp::R_PPC64_DTPREL16_HIGHER:
5111     case elfcpp::R_PPC64_TPREL16_HIGHER:
5112     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5113     case elfcpp::R_PPC64_TPREL16_HIGHERA:
5114     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5115     case elfcpp::R_PPC64_TPREL16_HIGHEST:
5116     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5117     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5118     case elfcpp::R_PPC64_TPREL16_DS:
5119     case elfcpp::R_PPC64_TPREL16_LO_DS:
5120     case elfcpp::R_PPC64_DTPREL16_DS:
5121     case elfcpp::R_PPC64_DTPREL16_LO_DS:
5122     case elfcpp::R_PPC64_SECTOFF_DS:
5123     case elfcpp::R_PPC64_SECTOFF_LO_DS:
5124     case elfcpp::R_PPC64_TLSGD:
5125     case elfcpp::R_PPC64_TLSLD:
5126       break;
5127
5128     case elfcpp::R_POWERPC_GOT16:
5129     case elfcpp::R_POWERPC_GOT16_LO:
5130     case elfcpp::R_POWERPC_GOT16_HI:
5131     case elfcpp::R_POWERPC_GOT16_HA:
5132     case elfcpp::R_PPC64_GOT16_DS:
5133     case elfcpp::R_PPC64_GOT16_LO_DS:
5134       {
5135         // The symbol requires a GOT entry.
5136         Output_data_got_powerpc<size, big_endian>* got
5137           = target->got_section(symtab, layout);
5138         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5139
5140         if (!parameters->options().output_is_position_independent())
5141           {
5142             if (size == 32 && is_ifunc)
5143               got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5144             else
5145               got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5146           }
5147         else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5148           {
5149             // If we are generating a shared object or a pie, this
5150             // symbol's GOT entry will be set by a dynamic relocation.
5151             unsigned int off;
5152             off = got->add_constant(0);
5153             object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5154
5155             Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5156                                                                is_ifunc);
5157             unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5158                                    : elfcpp::R_POWERPC_RELATIVE);
5159             rela_dyn->add_local_relative(object, r_sym, dynrel,
5160                                          got, off, 0, false);
5161           }
5162       }
5163       break;
5164
5165     case elfcpp::R_PPC64_TOC16:
5166     case elfcpp::R_PPC64_TOC16_LO:
5167     case elfcpp::R_PPC64_TOC16_HI:
5168     case elfcpp::R_PPC64_TOC16_HA:
5169     case elfcpp::R_PPC64_TOC16_DS:
5170     case elfcpp::R_PPC64_TOC16_LO_DS:
5171       // We need a GOT section.
5172       target->got_section(symtab, layout);
5173       break;
5174
5175     case elfcpp::R_POWERPC_GOT_TLSGD16:
5176     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5177     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5178     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5179       {
5180         const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5181         if (tls_type == tls::TLSOPT_NONE)
5182           {
5183             Output_data_got_powerpc<size, big_endian>* got
5184               = target->got_section(symtab, layout);
5185             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5186             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5187             got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5188                                     rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5189           }
5190         else if (tls_type == tls::TLSOPT_TO_LE)
5191           {
5192             // no GOT relocs needed for Local Exec.
5193           }
5194         else
5195           gold_unreachable();
5196       }
5197       break;
5198
5199     case elfcpp::R_POWERPC_GOT_TLSLD16:
5200     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5201     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5202     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5203       {
5204         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5205         if (tls_type == tls::TLSOPT_NONE)
5206           target->tlsld_got_offset(symtab, layout, object);
5207         else if (tls_type == tls::TLSOPT_TO_LE)
5208           {
5209             // no GOT relocs needed for Local Exec.
5210             if (parameters->options().emit_relocs())
5211               {
5212                 Output_section* os = layout->tls_segment()->first_section();
5213                 gold_assert(os != NULL);
5214                 os->set_needs_symtab_index();
5215               }
5216           }
5217         else
5218           gold_unreachable();
5219       }
5220       break;
5221
5222     case elfcpp::R_POWERPC_GOT_DTPREL16:
5223     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5224     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5225     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5226       {
5227         Output_data_got_powerpc<size, big_endian>* got
5228           = target->got_section(symtab, layout);
5229         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5230         got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5231       }
5232       break;
5233
5234     case elfcpp::R_POWERPC_GOT_TPREL16:
5235     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5236     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5237     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5238       {
5239         const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5240         if (tls_type == tls::TLSOPT_NONE)
5241           {
5242             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5243             if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5244               {
5245                 Output_data_got_powerpc<size, big_endian>* got
5246                   = target->got_section(symtab, layout);
5247                 unsigned int off = got->add_constant(0);
5248                 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5249
5250                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5251                 rela_dyn->add_symbolless_local_addend(object, r_sym,
5252                                                       elfcpp::R_POWERPC_TPREL,
5253                                                       got, off, 0);
5254               }
5255           }
5256         else if (tls_type == tls::TLSOPT_TO_LE)
5257           {
5258             // no GOT relocs needed for Local Exec.
5259           }
5260         else
5261           gold_unreachable();
5262       }
5263       break;
5264
5265     default:
5266       unsupported_reloc_local(object, r_type);
5267       break;
5268     }
5269
5270   switch (r_type)
5271     {
5272     case elfcpp::R_POWERPC_GOT_TLSLD16:
5273     case elfcpp::R_POWERPC_GOT_TLSGD16:
5274     case elfcpp::R_POWERPC_GOT_TPREL16:
5275     case elfcpp::R_POWERPC_GOT_DTPREL16:
5276     case elfcpp::R_POWERPC_GOT16:
5277     case elfcpp::R_PPC64_GOT16_DS:
5278     case elfcpp::R_PPC64_TOC16:
5279     case elfcpp::R_PPC64_TOC16_DS:
5280       ppc_object->set_has_small_toc_reloc();
5281     default:
5282       break;
5283     }
5284 }
5285
5286 // Report an unsupported relocation against a global symbol.
5287
5288 template<int size, bool big_endian>
5289 void
5290 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5291     Sized_relobj_file<size, big_endian>* object,
5292     unsigned int r_type,
5293     Symbol* gsym)
5294 {
5295   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5296              object->name().c_str(), r_type, gsym->demangled_name().c_str());
5297 }
5298
5299 // Scan a relocation for a global symbol.
5300
5301 template<int size, bool big_endian>
5302 inline void
5303 Target_powerpc<size, big_endian>::Scan::global(
5304     Symbol_table* symtab,
5305     Layout* layout,
5306     Target_powerpc<size, big_endian>* target,
5307     Sized_relobj_file<size, big_endian>* object,
5308     unsigned int data_shndx,
5309     Output_section* output_section,
5310     const elfcpp::Rela<size, big_endian>& reloc,
5311     unsigned int r_type,
5312     Symbol* gsym)
5313 {
5314   if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5315     return;
5316
5317   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5318       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5319     {
5320       this->expect_tls_get_addr_call();
5321       const bool final = gsym->final_value_is_known();
5322       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5323       if (tls_type != tls::TLSOPT_NONE)
5324         this->skip_next_tls_get_addr_call();
5325     }
5326   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5327            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5328     {
5329       this->expect_tls_get_addr_call();
5330       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5331       if (tls_type != tls::TLSOPT_NONE)
5332         this->skip_next_tls_get_addr_call();
5333     }
5334
5335   Powerpc_relobj<size, big_endian>* ppc_object
5336     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5337
5338   // A STT_GNU_IFUNC symbol may require a PLT entry.
5339   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5340   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
5341     {
5342       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5343                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5344                           reloc.get_r_addend());
5345       target->make_plt_entry(symtab, layout, gsym);
5346     }
5347
5348   switch (r_type)
5349     {
5350     case elfcpp::R_POWERPC_NONE:
5351     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5352     case elfcpp::R_POWERPC_GNU_VTENTRY:
5353     case elfcpp::R_PPC_LOCAL24PC:
5354     case elfcpp::R_PPC_EMB_MRKREF:
5355     case elfcpp::R_POWERPC_TLS:
5356       break;
5357
5358     case elfcpp::R_PPC64_TOC:
5359       {
5360         Output_data_got_powerpc<size, big_endian>* got
5361           = target->got_section(symtab, layout);
5362         if (parameters->options().output_is_position_independent())
5363           {
5364             Address off = reloc.get_r_offset();
5365             if (size == 64
5366                 && data_shndx == ppc_object->opd_shndx()
5367                 && ppc_object->get_opd_discard(off - 8))
5368               break;
5369
5370             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5371             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5372             if (data_shndx != ppc_object->opd_shndx())
5373               symobj = static_cast
5374                 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5375             rela_dyn->add_output_section_relative(got->output_section(),
5376                                                   elfcpp::R_POWERPC_RELATIVE,
5377                                                   output_section,
5378                                                   object, data_shndx, off,
5379                                                   symobj->toc_base_offset());
5380           }
5381       }
5382       break;
5383
5384     case elfcpp::R_PPC64_ADDR64:
5385       if (size == 64
5386           && data_shndx == ppc_object->opd_shndx()
5387           && (gsym->is_defined_in_discarded_section()
5388               || gsym->object() != object))
5389         {
5390           ppc_object->set_opd_discard(reloc.get_r_offset());
5391           break;
5392         }
5393       // Fall thru
5394     case elfcpp::R_PPC64_UADDR64:
5395     case elfcpp::R_POWERPC_ADDR32:
5396     case elfcpp::R_POWERPC_UADDR32:
5397     case elfcpp::R_POWERPC_ADDR24:
5398     case elfcpp::R_POWERPC_ADDR16:
5399     case elfcpp::R_POWERPC_ADDR16_LO:
5400     case elfcpp::R_POWERPC_ADDR16_HI:
5401     case elfcpp::R_POWERPC_ADDR16_HA:
5402     case elfcpp::R_POWERPC_UADDR16:
5403     case elfcpp::R_PPC64_ADDR16_HIGHER:
5404     case elfcpp::R_PPC64_ADDR16_HIGHERA:
5405     case elfcpp::R_PPC64_ADDR16_HIGHEST:
5406     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5407     case elfcpp::R_PPC64_ADDR16_DS:
5408     case elfcpp::R_PPC64_ADDR16_LO_DS:
5409     case elfcpp::R_POWERPC_ADDR14:
5410     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5411     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5412       {
5413         // Make a PLT entry if necessary.
5414         if (gsym->needs_plt_entry())
5415           {
5416             if (!is_ifunc)
5417               {
5418                 target->push_branch(ppc_object, data_shndx,
5419                                     reloc.get_r_offset(), r_type,
5420                                     elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5421                                     reloc.get_r_addend());
5422                 target->make_plt_entry(symtab, layout, gsym);
5423               }
5424             // Since this is not a PC-relative relocation, we may be
5425             // taking the address of a function. In that case we need to
5426             // set the entry in the dynamic symbol table to the address of
5427             // the PLT call stub.
5428             if (size == 32
5429                 && gsym->is_from_dynobj()
5430                 && !parameters->options().output_is_position_independent())
5431               gsym->set_needs_dynsym_value();
5432           }
5433         // Make a dynamic relocation if necessary.
5434         if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
5435             || (size == 64 && is_ifunc))
5436           {
5437             if (gsym->may_need_copy_reloc())
5438               {
5439                 target->copy_reloc(symtab, layout, object,
5440                                    data_shndx, output_section, gsym, reloc);
5441               }
5442             else if ((size == 32
5443                       && r_type == elfcpp::R_POWERPC_ADDR32
5444                       && gsym->can_use_relative_reloc(false)
5445                       && !(gsym->visibility() == elfcpp::STV_PROTECTED
5446                            && parameters->options().shared()))
5447                      || (size == 64
5448                          && r_type == elfcpp::R_PPC64_ADDR64
5449                          && (gsym->can_use_relative_reloc(false)
5450                              || data_shndx == ppc_object->opd_shndx())))
5451               {
5452                 Reloc_section* rela_dyn
5453                   = target->rela_dyn_section(symtab, layout, is_ifunc);
5454                 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5455                                        : elfcpp::R_POWERPC_RELATIVE);
5456                 rela_dyn->add_symbolless_global_addend(
5457                     gsym, dynrel, output_section, object, data_shndx,
5458                     reloc.get_r_offset(), reloc.get_r_addend());
5459               }
5460             else
5461               {
5462                 Reloc_section* rela_dyn
5463                   = target->rela_dyn_section(symtab, layout, is_ifunc);
5464                 check_non_pic(object, r_type);
5465                 rela_dyn->add_global(gsym, r_type, output_section,
5466                                      object, data_shndx,
5467                                      reloc.get_r_offset(),
5468                                      reloc.get_r_addend());
5469               }
5470           }
5471       }
5472       break;
5473
5474     case elfcpp::R_PPC_PLTREL24:
5475     case elfcpp::R_POWERPC_REL24:
5476       if (!is_ifunc)
5477         {
5478           target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5479                               r_type,
5480                               elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5481                               reloc.get_r_addend());
5482           if (gsym->needs_plt_entry()
5483               || (!gsym->final_value_is_known()
5484                   && (gsym->is_undefined()
5485                       || gsym->is_from_dynobj()
5486                       || gsym->is_preemptible())))
5487             target->make_plt_entry(symtab, layout, gsym);
5488         }
5489       // Fall thru
5490
5491     case elfcpp::R_PPC64_REL64:
5492     case elfcpp::R_POWERPC_REL32:
5493       // Make a dynamic relocation if necessary.
5494       if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
5495         {
5496           if (gsym->may_need_copy_reloc())
5497             {
5498               target->copy_reloc(symtab, layout, object,
5499                                  data_shndx, output_section, gsym,
5500                                  reloc);
5501             }
5502           else
5503             {
5504               Reloc_section* rela_dyn
5505                 = target->rela_dyn_section(symtab, layout, is_ifunc);
5506               check_non_pic(object, r_type);
5507               rela_dyn->add_global(gsym, r_type, output_section, object,
5508                                    data_shndx, reloc.get_r_offset(),
5509                                    reloc.get_r_addend());
5510             }
5511         }
5512       break;
5513
5514     case elfcpp::R_POWERPC_REL14:
5515     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5516     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5517       if (!is_ifunc)
5518         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5519                             r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5520                             reloc.get_r_addend());
5521       break;
5522
5523     case elfcpp::R_POWERPC_REL16:
5524     case elfcpp::R_POWERPC_REL16_LO:
5525     case elfcpp::R_POWERPC_REL16_HI:
5526     case elfcpp::R_POWERPC_REL16_HA:
5527     case elfcpp::R_POWERPC_SECTOFF:
5528     case elfcpp::R_POWERPC_TPREL16:
5529     case elfcpp::R_POWERPC_DTPREL16:
5530     case elfcpp::R_POWERPC_SECTOFF_LO:
5531     case elfcpp::R_POWERPC_TPREL16_LO:
5532     case elfcpp::R_POWERPC_DTPREL16_LO:
5533     case elfcpp::R_POWERPC_SECTOFF_HI:
5534     case elfcpp::R_POWERPC_TPREL16_HI:
5535     case elfcpp::R_POWERPC_DTPREL16_HI:
5536     case elfcpp::R_POWERPC_SECTOFF_HA:
5537     case elfcpp::R_POWERPC_TPREL16_HA:
5538     case elfcpp::R_POWERPC_DTPREL16_HA:
5539     case elfcpp::R_PPC64_DTPREL16_HIGHER:
5540     case elfcpp::R_PPC64_TPREL16_HIGHER:
5541     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5542     case elfcpp::R_PPC64_TPREL16_HIGHERA:
5543     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5544     case elfcpp::R_PPC64_TPREL16_HIGHEST:
5545     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5546     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5547     case elfcpp::R_PPC64_TPREL16_DS:
5548     case elfcpp::R_PPC64_TPREL16_LO_DS:
5549     case elfcpp::R_PPC64_DTPREL16_DS:
5550     case elfcpp::R_PPC64_DTPREL16_LO_DS:
5551     case elfcpp::R_PPC64_SECTOFF_DS:
5552     case elfcpp::R_PPC64_SECTOFF_LO_DS:
5553     case elfcpp::R_PPC64_TLSGD:
5554     case elfcpp::R_PPC64_TLSLD:
5555       break;
5556
5557     case elfcpp::R_POWERPC_GOT16:
5558     case elfcpp::R_POWERPC_GOT16_LO:
5559     case elfcpp::R_POWERPC_GOT16_HI:
5560     case elfcpp::R_POWERPC_GOT16_HA:
5561     case elfcpp::R_PPC64_GOT16_DS:
5562     case elfcpp::R_PPC64_GOT16_LO_DS:
5563       {
5564         // The symbol requires a GOT entry.
5565         Output_data_got_powerpc<size, big_endian>* got;
5566
5567         got = target->got_section(symtab, layout);
5568         if (gsym->final_value_is_known())
5569           {
5570             if (size == 32 && is_ifunc)
5571               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
5572             else
5573               got->add_global(gsym, GOT_TYPE_STANDARD);
5574           }
5575         else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
5576           {
5577             // If we are generating a shared object or a pie, this
5578             // symbol's GOT entry will be set by a dynamic relocation.
5579             unsigned int off = got->add_constant(0);
5580             gsym->set_got_offset(GOT_TYPE_STANDARD, off);
5581
5582             Reloc_section* rela_dyn
5583               = target->rela_dyn_section(symtab, layout, is_ifunc);
5584
5585             if (gsym->can_use_relative_reloc(false)
5586                 && !(size == 32
5587                      && gsym->visibility() == elfcpp::STV_PROTECTED
5588                      && parameters->options().shared()))
5589               {
5590                 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5591                                        : elfcpp::R_POWERPC_RELATIVE);
5592                 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
5593               }
5594             else
5595               {
5596                 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
5597                 rela_dyn->add_global(gsym, dynrel, got, off, 0);
5598               }
5599           }
5600       }
5601       break;
5602
5603     case elfcpp::R_PPC64_TOC16:
5604     case elfcpp::R_PPC64_TOC16_LO:
5605     case elfcpp::R_PPC64_TOC16_HI:
5606     case elfcpp::R_PPC64_TOC16_HA:
5607     case elfcpp::R_PPC64_TOC16_DS:
5608     case elfcpp::R_PPC64_TOC16_LO_DS:
5609       // We need a GOT section.
5610       target->got_section(symtab, layout);
5611       break;
5612
5613     case elfcpp::R_POWERPC_GOT_TLSGD16:
5614     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5615     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5616     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5617       {
5618         const bool final = gsym->final_value_is_known();
5619         const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5620         if (tls_type == tls::TLSOPT_NONE)
5621           {
5622             Output_data_got_powerpc<size, big_endian>* got
5623               = target->got_section(symtab, layout);
5624             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5625             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
5626                                           elfcpp::R_POWERPC_DTPMOD,
5627                                           elfcpp::R_POWERPC_DTPREL);
5628           }
5629         else if (tls_type == tls::TLSOPT_TO_IE)
5630           {
5631             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5632               {
5633                 Output_data_got_powerpc<size, big_endian>* got
5634                   = target->got_section(symtab, layout);
5635                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5636                 if (gsym->is_undefined()
5637                     || gsym->is_from_dynobj())
5638                   {
5639                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5640                                              elfcpp::R_POWERPC_TPREL);
5641                   }
5642                 else
5643                   {
5644                     unsigned int off = got->add_constant(0);
5645                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
5646                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5647                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5648                                                            got, off, 0);
5649                   }
5650               }
5651           }
5652         else if (tls_type == tls::TLSOPT_TO_LE)
5653           {
5654             // no GOT relocs needed for Local Exec.
5655           }
5656         else
5657           gold_unreachable();
5658       }
5659       break;
5660
5661     case elfcpp::R_POWERPC_GOT_TLSLD16:
5662     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5663     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5664     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5665       {
5666         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5667         if (tls_type == tls::TLSOPT_NONE)
5668           target->tlsld_got_offset(symtab, layout, object);
5669         else if (tls_type == tls::TLSOPT_TO_LE)
5670           {
5671             // no GOT relocs needed for Local Exec.
5672             if (parameters->options().emit_relocs())
5673               {
5674                 Output_section* os = layout->tls_segment()->first_section();
5675                 gold_assert(os != NULL);
5676                 os->set_needs_symtab_index();
5677               }
5678           }
5679         else
5680           gold_unreachable();
5681       }
5682       break;
5683
5684     case elfcpp::R_POWERPC_GOT_DTPREL16:
5685     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5686     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5687     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5688       {
5689         Output_data_got_powerpc<size, big_endian>* got
5690           = target->got_section(symtab, layout);
5691         if (!gsym->final_value_is_known()
5692             && (gsym->is_from_dynobj()
5693                 || gsym->is_undefined()
5694                 || gsym->is_preemptible()))
5695           got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
5696                                    target->rela_dyn_section(layout),
5697                                    elfcpp::R_POWERPC_DTPREL);
5698         else
5699           got->add_global_tls(gsym, GOT_TYPE_DTPREL);
5700       }
5701       break;
5702
5703     case elfcpp::R_POWERPC_GOT_TPREL16:
5704     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5705     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5706     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5707       {
5708         const bool final = gsym->final_value_is_known();
5709         const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
5710         if (tls_type == tls::TLSOPT_NONE)
5711           {
5712             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
5713               {
5714                 Output_data_got_powerpc<size, big_endian>* got
5715                   = target->got_section(symtab, layout);
5716                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5717                 if (gsym->is_undefined()
5718                     || gsym->is_from_dynobj())
5719                   {
5720                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
5721                                              elfcpp::R_POWERPC_TPREL);
5722                   }
5723                 else
5724                   {
5725                     unsigned int off = got->add_constant(0);
5726                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
5727                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
5728                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
5729                                                            got, off, 0);
5730                   }
5731               }
5732           }
5733         else if (tls_type == tls::TLSOPT_TO_LE)
5734           {
5735             // no GOT relocs needed for Local Exec.
5736           }
5737         else
5738           gold_unreachable();
5739       }
5740       break;
5741
5742     default:
5743       unsupported_reloc_global(object, r_type, gsym);
5744       break;
5745     }
5746
5747   switch (r_type)
5748     {
5749     case elfcpp::R_POWERPC_GOT_TLSLD16:
5750     case elfcpp::R_POWERPC_GOT_TLSGD16:
5751     case elfcpp::R_POWERPC_GOT_TPREL16:
5752     case elfcpp::R_POWERPC_GOT_DTPREL16:
5753     case elfcpp::R_POWERPC_GOT16:
5754     case elfcpp::R_PPC64_GOT16_DS:
5755     case elfcpp::R_PPC64_TOC16:
5756     case elfcpp::R_PPC64_TOC16_DS:
5757       ppc_object->set_has_small_toc_reloc();
5758     default:
5759       break;
5760     }
5761 }
5762
5763 // Process relocations for gc.
5764
5765 template<int size, bool big_endian>
5766 void
5767 Target_powerpc<size, big_endian>::gc_process_relocs(
5768     Symbol_table* symtab,
5769     Layout* layout,
5770     Sized_relobj_file<size, big_endian>* object,
5771     unsigned int data_shndx,
5772     unsigned int,
5773     const unsigned char* prelocs,
5774     size_t reloc_count,
5775     Output_section* output_section,
5776     bool needs_special_offset_handling,
5777     size_t local_symbol_count,
5778     const unsigned char* plocal_symbols)
5779 {
5780   typedef Target_powerpc<size, big_endian> Powerpc;
5781   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5782   Powerpc_relobj<size, big_endian>* ppc_object
5783     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5784   if (size == 64)
5785     ppc_object->set_opd_valid();
5786   if (size == 64 && data_shndx == ppc_object->opd_shndx())
5787     {
5788       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
5789       for (p = ppc_object->access_from_map()->begin();
5790            p != ppc_object->access_from_map()->end();
5791            ++p)
5792         {
5793           Address dst_off = p->first;
5794           unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5795           typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
5796           for (s = p->second.begin(); s != p->second.end(); ++s)
5797             {
5798               Object* src_obj = s->first;
5799               unsigned int src_indx = s->second;
5800               symtab->gc()->add_reference(src_obj, src_indx,
5801                                           ppc_object, dst_indx);
5802             }
5803           p->second.clear();
5804         }
5805       ppc_object->access_from_map()->clear();
5806       ppc_object->process_gc_mark(symtab);
5807       // Don't look at .opd relocs as .opd will reference everything.
5808       return;
5809     }
5810
5811   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
5812                           typename Target_powerpc::Relocatable_size_for_reloc>(
5813     symtab,
5814     layout,
5815     this,
5816     object,
5817     data_shndx,
5818     prelocs,
5819     reloc_count,
5820     output_section,
5821     needs_special_offset_handling,
5822     local_symbol_count,
5823     plocal_symbols);
5824 }
5825
5826 // Handle target specific gc actions when adding a gc reference from
5827 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
5828 // and DST_OFF.  For powerpc64, this adds a referenc to the code
5829 // section of a function descriptor.
5830
5831 template<int size, bool big_endian>
5832 void
5833 Target_powerpc<size, big_endian>::do_gc_add_reference(
5834     Symbol_table* symtab,
5835     Object* src_obj,
5836     unsigned int src_shndx,
5837     Object* dst_obj,
5838     unsigned int dst_shndx,
5839     Address dst_off) const
5840 {
5841   if (size != 64 || dst_obj->is_dynamic())
5842     return;
5843
5844   Powerpc_relobj<size, big_endian>* ppc_object
5845     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
5846   if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
5847     {
5848       if (ppc_object->opd_valid())
5849         {
5850           dst_shndx = ppc_object->get_opd_ent(dst_off);
5851           symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
5852         }
5853       else
5854         {
5855           // If we haven't run scan_opd_relocs, we must delay
5856           // processing this function descriptor reference.
5857           ppc_object->add_reference(src_obj, src_shndx, dst_off);
5858         }
5859     }
5860 }
5861
5862 // Add any special sections for this symbol to the gc work list.
5863 // For powerpc64, this adds the code section of a function
5864 // descriptor.
5865
5866 template<int size, bool big_endian>
5867 void
5868 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
5869     Symbol_table* symtab,
5870     Symbol* sym) const
5871 {
5872   if (size == 64)
5873     {
5874       Powerpc_relobj<size, big_endian>* ppc_object
5875         = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
5876       bool is_ordinary;
5877       unsigned int shndx = sym->shndx(&is_ordinary);
5878       if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
5879         {
5880           Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
5881           Address dst_off = gsym->value();
5882           if (ppc_object->opd_valid())
5883             {
5884               unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
5885               symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
5886             }
5887           else
5888             ppc_object->add_gc_mark(dst_off);
5889         }
5890     }
5891 }
5892
5893 // For a symbol location in .opd, set LOC to the location of the
5894 // function entry.
5895
5896 template<int size, bool big_endian>
5897 void
5898 Target_powerpc<size, big_endian>::do_function_location(
5899     Symbol_location* loc) const
5900 {
5901   if (size == 64 && loc->shndx != 0)
5902     {
5903       if (loc->object->is_dynamic())
5904         {
5905           Powerpc_dynobj<size, big_endian>* ppc_object
5906             = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
5907           if (loc->shndx == ppc_object->opd_shndx())
5908             {
5909               Address dest_off;
5910               Address off = loc->offset - ppc_object->opd_address();
5911               loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
5912               loc->offset = dest_off;
5913             }
5914         }
5915       else
5916         {
5917           const Powerpc_relobj<size, big_endian>* ppc_object
5918             = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
5919           if (loc->shndx == ppc_object->opd_shndx())
5920             {
5921               Address dest_off;
5922               loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
5923               loc->offset = dest_off;
5924             }
5925         }
5926     }
5927 }
5928
5929 // Scan relocations for a section.
5930
5931 template<int size, bool big_endian>
5932 void
5933 Target_powerpc<size, big_endian>::scan_relocs(
5934     Symbol_table* symtab,
5935     Layout* layout,
5936     Sized_relobj_file<size, big_endian>* object,
5937     unsigned int data_shndx,
5938     unsigned int sh_type,
5939     const unsigned char* prelocs,
5940     size_t reloc_count,
5941     Output_section* output_section,
5942     bool needs_special_offset_handling,
5943     size_t local_symbol_count,
5944     const unsigned char* plocal_symbols)
5945 {
5946   typedef Target_powerpc<size, big_endian> Powerpc;
5947   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
5948
5949   if (sh_type == elfcpp::SHT_REL)
5950     {
5951       gold_error(_("%s: unsupported REL reloc section"),
5952                  object->name().c_str());
5953       return;
5954     }
5955
5956   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
5957     symtab,
5958     layout,
5959     this,
5960     object,
5961     data_shndx,
5962     prelocs,
5963     reloc_count,
5964     output_section,
5965     needs_special_offset_handling,
5966     local_symbol_count,
5967     plocal_symbols);
5968 }
5969
5970 // Functor class for processing the global symbol table.
5971 // Removes symbols defined on discarded opd entries.
5972
5973 template<bool big_endian>
5974 class Global_symbol_visitor_opd
5975 {
5976  public:
5977   Global_symbol_visitor_opd()
5978   { }
5979
5980   void
5981   operator()(Sized_symbol<64>* sym)
5982   {
5983     if (sym->has_symtab_index()
5984         || sym->source() != Symbol::FROM_OBJECT
5985         || !sym->in_real_elf())
5986       return;
5987
5988     if (sym->object()->is_dynamic())
5989       return;
5990
5991     Powerpc_relobj<64, big_endian>* symobj
5992       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
5993     if (symobj->opd_shndx() == 0)
5994       return;
5995
5996     bool is_ordinary;
5997     unsigned int shndx = sym->shndx(&is_ordinary);
5998     if (shndx == symobj->opd_shndx()
5999         && symobj->get_opd_discard(sym->value()))
6000       sym->set_symtab_index(-1U);
6001   }
6002 };
6003
6004 template<int size, bool big_endian>
6005 void
6006 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6007     Layout* layout,
6008     Symbol_table* symtab)
6009 {
6010   if (size == 64)
6011     {
6012       Output_data_save_res<64, big_endian>* savres
6013         = new Output_data_save_res<64, big_endian>(symtab);
6014       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6015                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6016                                       savres, ORDER_TEXT, false);
6017     }
6018 }
6019
6020 // Sort linker created .got section first (for the header), then input
6021 // sections belonging to files using small model code.
6022
6023 template<bool big_endian>
6024 class Sort_toc_sections
6025 {
6026  public:
6027   bool
6028   operator()(const Output_section::Input_section& is1,
6029              const Output_section::Input_section& is2) const
6030   {
6031     if (!is1.is_input_section() && is2.is_input_section())
6032       return true;
6033     bool small1
6034       = (is1.is_input_section()
6035          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6036              ->has_small_toc_reloc()));
6037     bool small2
6038       = (is2.is_input_section()
6039          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6040              ->has_small_toc_reloc()));
6041     return small1 && !small2;
6042   }
6043 };
6044
6045 // Finalize the sections.
6046
6047 template<int size, bool big_endian>
6048 void
6049 Target_powerpc<size, big_endian>::do_finalize_sections(
6050     Layout* layout,
6051     const Input_objects*,
6052     Symbol_table* symtab)
6053 {
6054   if (parameters->doing_static_link())
6055     {
6056       // At least some versions of glibc elf-init.o have a strong
6057       // reference to __rela_iplt marker syms.  A weak ref would be
6058       // better..
6059       if (this->iplt_ != NULL)
6060         {
6061           Reloc_section* rel = this->iplt_->rel_plt();
6062           symtab->define_in_output_data("__rela_iplt_start", NULL,
6063                                         Symbol_table::PREDEFINED, rel, 0, 0,
6064                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6065                                         elfcpp::STV_HIDDEN, 0, false, true);
6066           symtab->define_in_output_data("__rela_iplt_end", NULL,
6067                                         Symbol_table::PREDEFINED, rel, 0, 0,
6068                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6069                                         elfcpp::STV_HIDDEN, 0, true, true);
6070         }
6071       else
6072         {
6073           symtab->define_as_constant("__rela_iplt_start", NULL,
6074                                      Symbol_table::PREDEFINED, 0, 0,
6075                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6076                                      elfcpp::STV_HIDDEN, 0, true, false);
6077           symtab->define_as_constant("__rela_iplt_end", NULL,
6078                                      Symbol_table::PREDEFINED, 0, 0,
6079                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6080                                      elfcpp::STV_HIDDEN, 0, true, false);
6081         }
6082     }
6083
6084   if (size == 64)
6085     {
6086       typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6087       symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6088
6089       if (!parameters->options().relocatable())
6090         {
6091           this->define_save_restore_funcs(layout, symtab);
6092
6093           // Annoyingly, we need to make these sections now whether or
6094           // not we need them.  If we delay until do_relax then we
6095           // need to mess with the relaxation machinery checkpointing.
6096           this->got_section(symtab, layout);
6097           this->make_brlt_section(layout);
6098
6099           if (parameters->options().toc_sort())
6100             {
6101               Output_section* os = this->got_->output_section();
6102               if (os != NULL && os->input_sections().size() > 1)
6103                 std::stable_sort(os->input_sections().begin(),
6104                                  os->input_sections().end(),
6105                                  Sort_toc_sections<big_endian>());
6106             }
6107         }
6108     }
6109
6110   // Fill in some more dynamic tags.
6111   Output_data_dynamic* odyn = layout->dynamic_data();
6112   if (odyn != NULL)
6113     {
6114       const Reloc_section* rel_plt = (this->plt_ == NULL
6115                                       ? NULL
6116                                       : this->plt_->rel_plt());
6117       layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6118                                       this->rela_dyn_, true, size == 32);
6119
6120       if (size == 32)
6121         {
6122           if (this->got_ != NULL)
6123             {
6124               this->got_->finalize_data_size();
6125               odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6126                                             this->got_, this->got_->g_o_t());
6127             }
6128         }
6129       else
6130         {
6131           if (this->glink_ != NULL)
6132             {
6133               this->glink_->finalize_data_size();
6134               odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6135                                             this->glink_,
6136                                             (this->glink_->pltresolve_size
6137                                              - 32));
6138             }
6139         }
6140     }
6141
6142   // Emit any relocs we saved in an attempt to avoid generating COPY
6143   // relocs.
6144   if (this->copy_relocs_.any_saved_relocs())
6145     this->copy_relocs_.emit(this->rela_dyn_section(layout));
6146 }
6147
6148 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6149 // reloc.
6150
6151 static bool
6152 ok_lo_toc_insn(uint32_t insn)
6153 {
6154   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6155           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6156           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6157           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6158           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6159           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6160           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6161           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6162           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6163           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6164           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6165           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6166           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6167           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6168           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6169               && (insn & 3) != 1)
6170           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6171               && ((insn & 3) == 0 || (insn & 3) == 3))
6172           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6173 }
6174
6175 // Return the value to use for a branch relocation.
6176
6177 template<int size, bool big_endian>
6178 typename Target_powerpc<size, big_endian>::Address
6179 Target_powerpc<size, big_endian>::symval_for_branch(
6180     const Symbol_table* symtab,
6181     Address value,
6182     const Sized_symbol<size>* gsym,
6183     Powerpc_relobj<size, big_endian>* object,
6184     unsigned int *dest_shndx)
6185 {
6186   *dest_shndx = 0;
6187   if (size == 32)
6188     return value;
6189
6190   // If the symbol is defined in an opd section, ie. is a function
6191   // descriptor, use the function descriptor code entry address
6192   Powerpc_relobj<size, big_endian>* symobj = object;
6193   if (gsym != NULL
6194       && gsym->source() != Symbol::FROM_OBJECT)
6195     return value;
6196   if (gsym != NULL)
6197     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6198   unsigned int shndx = symobj->opd_shndx();
6199   if (shndx == 0)
6200     return value;
6201   Address opd_addr = symobj->get_output_section_offset(shndx);
6202   if (opd_addr == invalid_address)
6203     return value;
6204   opd_addr += symobj->output_section_address(shndx);
6205   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
6206     {
6207       Address sec_off;
6208       *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
6209       if (symtab->is_section_folded(symobj, *dest_shndx))
6210         {
6211           Section_id folded
6212             = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6213           symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6214           *dest_shndx = folded.second;
6215         }
6216       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6217       gold_assert(sec_addr != invalid_address);
6218       sec_addr += symobj->output_section(*dest_shndx)->address();
6219       value = sec_addr + sec_off;
6220     }
6221   return value;
6222 }
6223
6224 // Perform a relocation.
6225
6226 template<int size, bool big_endian>
6227 inline bool
6228 Target_powerpc<size, big_endian>::Relocate::relocate(
6229     const Relocate_info<size, big_endian>* relinfo,
6230     Target_powerpc* target,
6231     Output_section* os,
6232     size_t relnum,
6233     const elfcpp::Rela<size, big_endian>& rela,
6234     unsigned int r_type,
6235     const Sized_symbol<size>* gsym,
6236     const Symbol_value<size>* psymval,
6237     unsigned char* view,
6238     Address address,
6239     section_size_type view_size)
6240 {
6241   if (view == NULL)
6242     return true;
6243
6244   switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6245     {
6246     case Track_tls::NOT_EXPECTED:
6247       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6248                              _("__tls_get_addr call lacks marker reloc"));
6249       break;
6250     case Track_tls::EXPECTED:
6251       // We have already complained.
6252       break;
6253     case Track_tls::SKIP:
6254       return true;
6255     case Track_tls::NORMAL:
6256       break;
6257     }
6258
6259   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6260   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6261   Powerpc_relobj<size, big_endian>* const object
6262     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6263   Address value = 0;
6264   bool has_plt_value = false;
6265   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6266   if ((gsym != NULL
6267        ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
6268        : object->local_has_plt_offset(r_sym))
6269       && (!psymval->is_ifunc_symbol()
6270           || Scan::reloc_needs_plt_for_ifunc(object, r_type, false)))
6271     {
6272       Stub_table<size, big_endian>* stub_table
6273         = object->stub_table(relinfo->data_shndx);
6274       if (stub_table == NULL)
6275         {
6276           // This is a ref from a data section to an ifunc symbol.
6277           if (target->stub_tables().size() != 0)
6278             stub_table = target->stub_tables()[0];
6279         }
6280       gold_assert(stub_table != NULL);
6281       Address off;
6282       if (gsym != NULL)
6283         off = stub_table->find_plt_call_entry(object, gsym, r_type,
6284                                               rela.get_r_addend());
6285       else
6286         off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6287                                               rela.get_r_addend());
6288       gold_assert(off != invalid_address);
6289       value = stub_table->stub_address() + off;
6290       has_plt_value = true;
6291     }
6292
6293   if (r_type == elfcpp::R_POWERPC_GOT16
6294       || r_type == elfcpp::R_POWERPC_GOT16_LO
6295       || r_type == elfcpp::R_POWERPC_GOT16_HI
6296       || r_type == elfcpp::R_POWERPC_GOT16_HA
6297       || r_type == elfcpp::R_PPC64_GOT16_DS
6298       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6299     {
6300       if (gsym != NULL)
6301         {
6302           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6303           value = gsym->got_offset(GOT_TYPE_STANDARD);
6304         }
6305       else
6306         {
6307           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6308           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6309           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6310         }
6311       value -= target->got_section()->got_base_offset(object);
6312     }
6313   else if (r_type == elfcpp::R_PPC64_TOC)
6314     {
6315       value = (target->got_section()->output_section()->address()
6316                + object->toc_base_offset());
6317     }
6318   else if (gsym != NULL
6319            && (r_type == elfcpp::R_POWERPC_REL24
6320                || r_type == elfcpp::R_PPC_PLTREL24)
6321            && has_plt_value)
6322     {
6323       if (size == 64)
6324         {
6325           typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6326           Valtype* wv = reinterpret_cast<Valtype*>(view);
6327           bool can_plt_call = false;
6328           if (rela.get_r_offset() + 8 <= view_size)
6329             {
6330               Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6331               Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6332               if ((insn & 1) != 0
6333                   && (insn2 == nop
6334                       || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6335                 {
6336                   elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
6337                   can_plt_call = true;
6338                 }
6339             }
6340           if (!can_plt_call)
6341             {
6342               // If we don't have a branch and link followed by a nop,
6343               // we can't go via the plt because there is no place to
6344               // put a toc restoring instruction.
6345               // Unless we know we won't be returning.
6346               if (strcmp(gsym->name(), "__libc_start_main") == 0)
6347                 can_plt_call = true;
6348             }
6349           if (!can_plt_call)
6350             {
6351               // g++ as of 20130507 emits self-calls without a
6352               // following nop.  This is arguably wrong since we have
6353               // conflicting information.  On the one hand a global
6354               // symbol and on the other a local call sequence, but
6355               // don't error for this special case.
6356               // It isn't possible to cheaply verify we have exactly
6357               // such a call.  Allow all calls to the same section.
6358               bool ok = false;
6359               Address code = value;
6360               if (gsym->source() == Symbol::FROM_OBJECT
6361                   && gsym->object() == object)
6362                 {
6363                   Address addend = rela.get_r_addend();
6364                   unsigned int dest_shndx;
6365                   Address opdent = psymval->value(object, addend);
6366                   code = target->symval_for_branch(relinfo->symtab, opdent,
6367                                                    gsym, object, &dest_shndx);
6368                   bool is_ordinary;
6369                   if (dest_shndx == 0)
6370                     dest_shndx = gsym->shndx(&is_ordinary);
6371                   ok = dest_shndx == relinfo->data_shndx;
6372                 }
6373               if (!ok)
6374                 {
6375                   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6376                                          _("call lacks nop, can't restore toc; "
6377                                            "recompile with -fPIC"));
6378                   value = code;
6379                 }
6380             }
6381         }
6382     }
6383   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6384            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6385            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6386            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6387     {
6388       // First instruction of a global dynamic sequence, arg setup insn.
6389       const bool final = gsym == NULL || gsym->final_value_is_known();
6390       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6391       enum Got_type got_type = GOT_TYPE_STANDARD;
6392       if (tls_type == tls::TLSOPT_NONE)
6393         got_type = GOT_TYPE_TLSGD;
6394       else if (tls_type == tls::TLSOPT_TO_IE)
6395         got_type = GOT_TYPE_TPREL;
6396       if (got_type != GOT_TYPE_STANDARD)
6397         {
6398           if (gsym != NULL)
6399             {
6400               gold_assert(gsym->has_got_offset(got_type));
6401               value = gsym->got_offset(got_type);
6402             }
6403           else
6404             {
6405               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6406               gold_assert(object->local_has_got_offset(r_sym, got_type));
6407               value = object->local_got_offset(r_sym, got_type);
6408             }
6409           value -= target->got_section()->got_base_offset(object);
6410         }
6411       if (tls_type == tls::TLSOPT_TO_IE)
6412         {
6413           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6414               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6415             {
6416               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6417               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6418               insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6419               if (size == 32)
6420                 insn |= 32 << 26; // lwz
6421               else
6422                 insn |= 58 << 26; // ld
6423               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6424             }
6425           r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6426                      - elfcpp::R_POWERPC_GOT_TLSGD16);
6427         }
6428       else if (tls_type == tls::TLSOPT_TO_LE)
6429         {
6430           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6431               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6432             {
6433               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6434               Insn insn = addis_3_13;
6435               if (size == 32)
6436                 insn = addis_3_2;
6437               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6438               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6439               value = psymval->value(object, rela.get_r_addend());
6440             }
6441           else
6442             {
6443               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6444               Insn insn = nop;
6445               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6446               r_type = elfcpp::R_POWERPC_NONE;
6447             }
6448         }
6449     }
6450   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6451            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6452            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6453            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6454     {
6455       // First instruction of a local dynamic sequence, arg setup insn.
6456       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6457       if (tls_type == tls::TLSOPT_NONE)
6458         {
6459           value = target->tlsld_got_offset();
6460           value -= target->got_section()->got_base_offset(object);
6461         }
6462       else
6463         {
6464           gold_assert(tls_type == tls::TLSOPT_TO_LE);
6465           if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6466               || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6467             {
6468               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6469               Insn insn = addis_3_13;
6470               if (size == 32)
6471                 insn = addis_3_2;
6472               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6473               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6474               value = dtp_offset;
6475             }
6476           else
6477             {
6478               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6479               Insn insn = nop;
6480               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6481               r_type = elfcpp::R_POWERPC_NONE;
6482             }
6483         }
6484     }
6485   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6486            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6487            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6488            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6489     {
6490       // Accesses relative to a local dynamic sequence address,
6491       // no optimisation here.
6492       if (gsym != NULL)
6493         {
6494           gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6495           value = gsym->got_offset(GOT_TYPE_DTPREL);
6496         }
6497       else
6498         {
6499           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6500           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6501           value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6502         }
6503       value -= target->got_section()->got_base_offset(object);
6504     }
6505   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6506            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6507            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6508            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6509     {
6510       // First instruction of initial exec sequence.
6511       const bool final = gsym == NULL || gsym->final_value_is_known();
6512       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6513       if (tls_type == tls::TLSOPT_NONE)
6514         {
6515           if (gsym != NULL)
6516             {
6517               gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
6518               value = gsym->got_offset(GOT_TYPE_TPREL);
6519             }
6520           else
6521             {
6522               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6523               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
6524               value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
6525             }
6526           value -= target->got_section()->got_base_offset(object);
6527         }
6528       else
6529         {
6530           gold_assert(tls_type == tls::TLSOPT_TO_LE);
6531           if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6532               || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
6533             {
6534               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6535               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6536               insn &= (1 << 26) - (1 << 21); // extract rt from ld
6537               if (size == 32)
6538                 insn |= addis_0_2;
6539               else
6540                 insn |= addis_0_13;
6541               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6542               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6543               value = psymval->value(object, rela.get_r_addend());
6544             }
6545           else
6546             {
6547               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6548               Insn insn = nop;
6549               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6550               r_type = elfcpp::R_POWERPC_NONE;
6551             }
6552         }
6553     }
6554   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
6555            || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
6556     {
6557       // Second instruction of a global dynamic sequence,
6558       // the __tls_get_addr call
6559       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6560       const bool final = gsym == NULL || gsym->final_value_is_known();
6561       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6562       if (tls_type != tls::TLSOPT_NONE)
6563         {
6564           if (tls_type == tls::TLSOPT_TO_IE)
6565             {
6566               Insn* iview = reinterpret_cast<Insn*>(view);
6567               Insn insn = add_3_3_13;
6568               if (size == 32)
6569                 insn = add_3_3_2;
6570               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6571               r_type = elfcpp::R_POWERPC_NONE;
6572             }
6573           else
6574             {
6575               Insn* iview = reinterpret_cast<Insn*>(view);
6576               Insn insn = addi_3_3;
6577               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6578               r_type = elfcpp::R_POWERPC_TPREL16_LO;
6579               view += 2 * big_endian;
6580               value = psymval->value(object, rela.get_r_addend());
6581             }
6582           this->skip_next_tls_get_addr_call();
6583         }
6584     }
6585   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
6586            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
6587     {
6588       // Second instruction of a local dynamic sequence,
6589       // the __tls_get_addr call
6590       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
6591       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6592       if (tls_type == tls::TLSOPT_TO_LE)
6593         {
6594           Insn* iview = reinterpret_cast<Insn*>(view);
6595           Insn insn = addi_3_3;
6596           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6597           this->skip_next_tls_get_addr_call();
6598           r_type = elfcpp::R_POWERPC_TPREL16_LO;
6599           view += 2 * big_endian;
6600           value = dtp_offset;
6601         }
6602     }
6603   else if (r_type == elfcpp::R_POWERPC_TLS)
6604     {
6605       // Second instruction of an initial exec sequence
6606       const bool final = gsym == NULL || gsym->final_value_is_known();
6607       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6608       if (tls_type == tls::TLSOPT_TO_LE)
6609         {
6610           Insn* iview = reinterpret_cast<Insn*>(view);
6611           Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6612           unsigned int reg = size == 32 ? 2 : 13;
6613           insn = at_tls_transform(insn, reg);
6614           gold_assert(insn != 0);
6615           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6616           r_type = elfcpp::R_POWERPC_TPREL16_LO;
6617           view += 2 * big_endian;
6618           value = psymval->value(object, rela.get_r_addend());
6619         }
6620     }
6621   else if (!has_plt_value)
6622     {
6623       Address addend = 0;
6624       unsigned int dest_shndx;
6625       if (r_type != elfcpp::R_PPC_PLTREL24)
6626         addend = rela.get_r_addend();
6627       value = psymval->value(object, addend);
6628       if (size == 64 && is_branch_reloc(r_type))
6629         value = target->symval_for_branch(relinfo->symtab, value,
6630                                           gsym, object, &dest_shndx);
6631       unsigned int max_branch_offset = 0;
6632       if (r_type == elfcpp::R_POWERPC_REL24
6633           || r_type == elfcpp::R_PPC_PLTREL24
6634           || r_type == elfcpp::R_PPC_LOCAL24PC)
6635         max_branch_offset = 1 << 25;
6636       else if (r_type == elfcpp::R_POWERPC_REL14
6637                || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
6638                || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
6639         max_branch_offset = 1 << 15;
6640       if (max_branch_offset != 0
6641           && value - address + max_branch_offset >= 2 * max_branch_offset)
6642         {
6643           Stub_table<size, big_endian>* stub_table
6644             = object->stub_table(relinfo->data_shndx);
6645           if (stub_table != NULL)
6646             {
6647               Address off = stub_table->find_long_branch_entry(object, value);
6648               if (off != invalid_address)
6649                 value = (stub_table->stub_address() + stub_table->plt_size()
6650                          + off);
6651             }
6652         }
6653     }
6654
6655   switch (r_type)
6656     {
6657     case elfcpp::R_PPC64_REL64:
6658     case elfcpp::R_POWERPC_REL32:
6659     case elfcpp::R_POWERPC_REL24:
6660     case elfcpp::R_PPC_PLTREL24:
6661     case elfcpp::R_PPC_LOCAL24PC:
6662     case elfcpp::R_POWERPC_REL16:
6663     case elfcpp::R_POWERPC_REL16_LO:
6664     case elfcpp::R_POWERPC_REL16_HI:
6665     case elfcpp::R_POWERPC_REL16_HA:
6666     case elfcpp::R_POWERPC_REL14:
6667     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6668     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6669       value -= address;
6670       break;
6671
6672     case elfcpp::R_PPC64_TOC16:
6673     case elfcpp::R_PPC64_TOC16_LO:
6674     case elfcpp::R_PPC64_TOC16_HI:
6675     case elfcpp::R_PPC64_TOC16_HA:
6676     case elfcpp::R_PPC64_TOC16_DS:
6677     case elfcpp::R_PPC64_TOC16_LO_DS:
6678       // Subtract the TOC base address.
6679       value -= (target->got_section()->output_section()->address()
6680                 + object->toc_base_offset());
6681       break;
6682
6683     case elfcpp::R_POWERPC_SECTOFF:
6684     case elfcpp::R_POWERPC_SECTOFF_LO:
6685     case elfcpp::R_POWERPC_SECTOFF_HI:
6686     case elfcpp::R_POWERPC_SECTOFF_HA:
6687     case elfcpp::R_PPC64_SECTOFF_DS:
6688     case elfcpp::R_PPC64_SECTOFF_LO_DS:
6689       if (os != NULL)
6690         value -= os->address();
6691       break;
6692
6693     case elfcpp::R_PPC64_TPREL16_DS:
6694     case elfcpp::R_PPC64_TPREL16_LO_DS:
6695       if (size != 64)
6696         // R_PPC_TLSGD and R_PPC_TLSLD
6697         break;
6698     case elfcpp::R_POWERPC_TPREL16:
6699     case elfcpp::R_POWERPC_TPREL16_LO:
6700     case elfcpp::R_POWERPC_TPREL16_HI:
6701     case elfcpp::R_POWERPC_TPREL16_HA:
6702     case elfcpp::R_POWERPC_TPREL:
6703     case elfcpp::R_PPC64_TPREL16_HIGHER:
6704     case elfcpp::R_PPC64_TPREL16_HIGHERA:
6705     case elfcpp::R_PPC64_TPREL16_HIGHEST:
6706     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6707       // tls symbol values are relative to tls_segment()->vaddr()
6708       value -= tp_offset;
6709       break;
6710
6711     case elfcpp::R_PPC64_DTPREL16_DS:
6712     case elfcpp::R_PPC64_DTPREL16_LO_DS:
6713     case elfcpp::R_PPC64_DTPREL16_HIGHER:
6714     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6715     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6716     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6717       if (size != 64)
6718         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
6719         // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
6720         break;
6721     case elfcpp::R_POWERPC_DTPREL16:
6722     case elfcpp::R_POWERPC_DTPREL16_LO:
6723     case elfcpp::R_POWERPC_DTPREL16_HI:
6724     case elfcpp::R_POWERPC_DTPREL16_HA:
6725     case elfcpp::R_POWERPC_DTPREL:
6726       // tls symbol values are relative to tls_segment()->vaddr()
6727       value -= dtp_offset;
6728       break;
6729
6730     default:
6731       break;
6732     }
6733
6734   Insn branch_bit = 0;
6735   switch (r_type)
6736     {
6737     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6738     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6739       branch_bit = 1 << 21;
6740     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6741     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6742       {
6743         Insn* iview = reinterpret_cast<Insn*>(view);
6744         Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6745         insn &= ~(1 << 21);
6746         insn |= branch_bit;
6747         if (this->is_isa_v2)
6748           {
6749             // Set 'a' bit.  This is 0b00010 in BO field for branch
6750             // on CR(BI) insns (BO == 001at or 011at), and 0b01000
6751             // for branch on CTR insns (BO == 1a00t or 1a01t).
6752             if ((insn & (0x14 << 21)) == (0x04 << 21))
6753               insn |= 0x02 << 21;
6754             else if ((insn & (0x14 << 21)) == (0x10 << 21))
6755               insn |= 0x08 << 21;
6756             else
6757               break;
6758           }
6759         else
6760           {
6761             // Invert 'y' bit if not the default.
6762             if (static_cast<Signed_address>(value) < 0)
6763               insn ^= 1 << 21;
6764           }
6765         elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6766       }
6767       break;
6768
6769     default:
6770       break;
6771     }
6772
6773   if (size == 64)
6774     {
6775       // Multi-instruction sequences that access the TOC can be
6776       // optimized, eg. addis ra,r2,0; addi rb,ra,x;
6777       // to             nop;           addi rb,r2,x;
6778       switch (r_type)
6779         {
6780         default:
6781           break;
6782
6783         case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6784         case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6785         case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6786         case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6787         case elfcpp::R_POWERPC_GOT16_HA:
6788         case elfcpp::R_PPC64_TOC16_HA:
6789           if (parameters->options().toc_optimize())
6790             {
6791               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6792               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6793               if ((insn & ((0x3f << 26) | 0x1f << 16))
6794                   != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
6795                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6796                                        _("toc optimization is not supported "
6797                                          "for %#08x instruction"), insn);
6798               else if (value + 0x8000 < 0x10000)
6799                 {
6800                   elfcpp::Swap<32, big_endian>::writeval(iview, nop);
6801                   return true;
6802                 }
6803             }
6804           break;
6805
6806         case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6807         case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6808         case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6809         case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6810         case elfcpp::R_POWERPC_GOT16_LO:
6811         case elfcpp::R_PPC64_GOT16_LO_DS:
6812         case elfcpp::R_PPC64_TOC16_LO:
6813         case elfcpp::R_PPC64_TOC16_LO_DS:
6814           if (parameters->options().toc_optimize())
6815             {
6816               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6817               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6818               if (!ok_lo_toc_insn(insn))
6819                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6820                                        _("toc optimization is not supported "
6821                                          "for %#08x instruction"), insn);
6822               else if (value + 0x8000 < 0x10000)
6823                 {
6824                   if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
6825                     {
6826                       // Transform addic to addi when we change reg.
6827                       insn &= ~((0x3f << 26) | (0x1f << 16));
6828                       insn |= (14u << 26) | (2 << 16);
6829                     }
6830                   else
6831                     {
6832                       insn &= ~(0x1f << 16);
6833                       insn |= 2 << 16;
6834                     }
6835                   elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6836                 }
6837             }
6838           break;
6839         }
6840     }
6841
6842   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
6843   switch (r_type)
6844     {
6845     case elfcpp::R_POWERPC_ADDR32:
6846     case elfcpp::R_POWERPC_UADDR32:
6847       if (size == 64)
6848         overflow = Reloc::CHECK_BITFIELD;
6849       break;
6850
6851     case elfcpp::R_POWERPC_REL32:
6852       if (size == 64)
6853         overflow = Reloc::CHECK_SIGNED;
6854       break;
6855
6856     case elfcpp::R_POWERPC_ADDR24:
6857     case elfcpp::R_POWERPC_ADDR16:
6858     case elfcpp::R_POWERPC_UADDR16:
6859     case elfcpp::R_PPC64_ADDR16_DS:
6860     case elfcpp::R_POWERPC_ADDR14:
6861     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
6862     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
6863       overflow = Reloc::CHECK_BITFIELD;
6864       break;
6865
6866     case elfcpp::R_POWERPC_REL24:
6867     case elfcpp::R_PPC_PLTREL24:
6868     case elfcpp::R_PPC_LOCAL24PC:
6869     case elfcpp::R_POWERPC_REL16:
6870     case elfcpp::R_PPC64_TOC16:
6871     case elfcpp::R_POWERPC_GOT16:
6872     case elfcpp::R_POWERPC_SECTOFF:
6873     case elfcpp::R_POWERPC_TPREL16:
6874     case elfcpp::R_POWERPC_DTPREL16:
6875     case elfcpp::R_PPC64_TPREL16_DS:
6876     case elfcpp::R_PPC64_DTPREL16_DS:
6877     case elfcpp::R_PPC64_TOC16_DS:
6878     case elfcpp::R_PPC64_GOT16_DS:
6879     case elfcpp::R_PPC64_SECTOFF_DS:
6880     case elfcpp::R_POWERPC_REL14:
6881     case elfcpp::R_POWERPC_REL14_BRTAKEN:
6882     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
6883     case elfcpp::R_POWERPC_GOT_TLSGD16:
6884     case elfcpp::R_POWERPC_GOT_TLSLD16:
6885     case elfcpp::R_POWERPC_GOT_TPREL16:
6886     case elfcpp::R_POWERPC_GOT_DTPREL16:
6887       overflow = Reloc::CHECK_SIGNED;
6888       break;
6889     }
6890
6891   typename Powerpc_relocate_functions<size, big_endian>::Status status
6892     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
6893   switch (r_type)
6894     {
6895     case elfcpp::R_POWERPC_NONE:
6896     case elfcpp::R_POWERPC_TLS:
6897     case elfcpp::R_POWERPC_GNU_VTINHERIT:
6898     case elfcpp::R_POWERPC_GNU_VTENTRY:
6899     case elfcpp::R_PPC_EMB_MRKREF:
6900       break;
6901
6902     case elfcpp::R_PPC64_ADDR64:
6903     case elfcpp::R_PPC64_REL64:
6904     case elfcpp::R_PPC64_TOC:
6905       Reloc::addr64(view, value);
6906       break;
6907
6908     case elfcpp::R_POWERPC_TPREL:
6909     case elfcpp::R_POWERPC_DTPREL:
6910       if (size == 64)
6911         Reloc::addr64(view, value);
6912       else
6913         status = Reloc::addr32(view, value, overflow);
6914       break;
6915
6916     case elfcpp::R_PPC64_UADDR64:
6917       Reloc::addr64_u(view, value);
6918       break;
6919
6920     case elfcpp::R_POWERPC_ADDR32:
6921       status = Reloc::addr32(view, value, overflow);
6922       break;
6923
6924     case elfcpp::R_POWERPC_REL32:
6925     case elfcpp::R_POWERPC_UADDR32:
6926       status = Reloc::addr32_u(view, value, overflow);
6927       break;
6928
6929     case elfcpp::R_POWERPC_ADDR24:
6930     case elfcpp::R_POWERPC_REL24:
6931     case elfcpp::R_PPC_PLTREL24:
6932     case elfcpp::R_PPC_LOCAL24PC:
6933       status = Reloc::addr24(view, value, overflow);
6934       break;
6935
6936     case elfcpp::R_POWERPC_GOT_DTPREL16:
6937     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6938       if (size == 64)
6939         {
6940           status = Reloc::addr16_ds(view, value, overflow);
6941           break;
6942         }
6943     case elfcpp::R_POWERPC_ADDR16:
6944     case elfcpp::R_POWERPC_REL16:
6945     case elfcpp::R_PPC64_TOC16:
6946     case elfcpp::R_POWERPC_GOT16:
6947     case elfcpp::R_POWERPC_SECTOFF:
6948     case elfcpp::R_POWERPC_TPREL16:
6949     case elfcpp::R_POWERPC_DTPREL16:
6950     case elfcpp::R_POWERPC_GOT_TLSGD16:
6951     case elfcpp::R_POWERPC_GOT_TLSLD16:
6952     case elfcpp::R_POWERPC_GOT_TPREL16:
6953     case elfcpp::R_POWERPC_ADDR16_LO:
6954     case elfcpp::R_POWERPC_REL16_LO:
6955     case elfcpp::R_PPC64_TOC16_LO:
6956     case elfcpp::R_POWERPC_GOT16_LO:
6957     case elfcpp::R_POWERPC_SECTOFF_LO:
6958     case elfcpp::R_POWERPC_TPREL16_LO:
6959     case elfcpp::R_POWERPC_DTPREL16_LO:
6960     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6961     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6962     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6963       status = Reloc::addr16(view, value, overflow);
6964       break;
6965
6966     case elfcpp::R_POWERPC_UADDR16:
6967       status = Reloc::addr16_u(view, value, overflow);
6968       break;
6969
6970     case elfcpp::R_POWERPC_ADDR16_HI:
6971     case elfcpp::R_POWERPC_REL16_HI:
6972     case elfcpp::R_PPC64_TOC16_HI:
6973     case elfcpp::R_POWERPC_GOT16_HI:
6974     case elfcpp::R_POWERPC_SECTOFF_HI:
6975     case elfcpp::R_POWERPC_TPREL16_HI:
6976     case elfcpp::R_POWERPC_DTPREL16_HI:
6977     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6978     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6979     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6980     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6981       Reloc::addr16_hi(view, value);
6982       break;
6983
6984     case elfcpp::R_POWERPC_ADDR16_HA:
6985     case elfcpp::R_POWERPC_REL16_HA:
6986     case elfcpp::R_PPC64_TOC16_HA:
6987     case elfcpp::R_POWERPC_GOT16_HA:
6988     case elfcpp::R_POWERPC_SECTOFF_HA:
6989     case elfcpp::R_POWERPC_TPREL16_HA:
6990     case elfcpp::R_POWERPC_DTPREL16_HA:
6991     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6992     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6993     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6994     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6995       Reloc::addr16_ha(view, value);
6996       break;
6997
6998     case elfcpp::R_PPC64_DTPREL16_HIGHER:
6999       if (size == 32)
7000         // R_PPC_EMB_NADDR16_LO
7001         goto unsupp;
7002     case elfcpp::R_PPC64_ADDR16_HIGHER:
7003     case elfcpp::R_PPC64_TPREL16_HIGHER:
7004       Reloc::addr16_hi2(view, value);
7005       break;
7006
7007     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7008       if (size == 32)
7009         // R_PPC_EMB_NADDR16_HI
7010         goto unsupp;
7011     case elfcpp::R_PPC64_ADDR16_HIGHERA:
7012     case elfcpp::R_PPC64_TPREL16_HIGHERA:
7013       Reloc::addr16_ha2(view, value);
7014       break;
7015
7016     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7017       if (size == 32)
7018         // R_PPC_EMB_NADDR16_HA
7019         goto unsupp;
7020     case elfcpp::R_PPC64_ADDR16_HIGHEST:
7021     case elfcpp::R_PPC64_TPREL16_HIGHEST:
7022       Reloc::addr16_hi3(view, value);
7023       break;
7024
7025     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7026       if (size == 32)
7027         // R_PPC_EMB_SDAI16
7028         goto unsupp;
7029     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7030     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7031       Reloc::addr16_ha3(view, value);
7032       break;
7033
7034     case elfcpp::R_PPC64_DTPREL16_DS:
7035     case elfcpp::R_PPC64_DTPREL16_LO_DS:
7036       if (size == 32)
7037         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7038         goto unsupp;
7039     case elfcpp::R_PPC64_TPREL16_DS:
7040     case elfcpp::R_PPC64_TPREL16_LO_DS:
7041       if (size == 32)
7042         // R_PPC_TLSGD, R_PPC_TLSLD
7043         break;
7044     case elfcpp::R_PPC64_ADDR16_DS:
7045     case elfcpp::R_PPC64_ADDR16_LO_DS:
7046     case elfcpp::R_PPC64_TOC16_DS:
7047     case elfcpp::R_PPC64_TOC16_LO_DS:
7048     case elfcpp::R_PPC64_GOT16_DS:
7049     case elfcpp::R_PPC64_GOT16_LO_DS:
7050     case elfcpp::R_PPC64_SECTOFF_DS:
7051     case elfcpp::R_PPC64_SECTOFF_LO_DS:
7052       status = Reloc::addr16_ds(view, value, overflow);
7053       break;
7054
7055     case elfcpp::R_POWERPC_ADDR14:
7056     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7057     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7058     case elfcpp::R_POWERPC_REL14:
7059     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7060     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7061       status = Reloc::addr14(view, value, overflow);
7062       break;
7063
7064     case elfcpp::R_POWERPC_COPY:
7065     case elfcpp::R_POWERPC_GLOB_DAT:
7066     case elfcpp::R_POWERPC_JMP_SLOT:
7067     case elfcpp::R_POWERPC_RELATIVE:
7068     case elfcpp::R_POWERPC_DTPMOD:
7069     case elfcpp::R_PPC64_JMP_IREL:
7070     case elfcpp::R_POWERPC_IRELATIVE:
7071       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7072                              _("unexpected reloc %u in object file"),
7073                              r_type);
7074       break;
7075
7076     case elfcpp::R_PPC_EMB_SDA21:
7077       if (size == 32)
7078         goto unsupp;
7079       else
7080         {
7081           // R_PPC64_TOCSAVE.  For the time being this can be ignored.
7082         }
7083       break;
7084
7085     case elfcpp::R_PPC_EMB_SDA2I16:
7086     case elfcpp::R_PPC_EMB_SDA2REL:
7087       if (size == 32)
7088         goto unsupp;
7089       // R_PPC64_TLSGD, R_PPC64_TLSLD
7090       break;
7091
7092     case elfcpp::R_POWERPC_PLT32:
7093     case elfcpp::R_POWERPC_PLTREL32:
7094     case elfcpp::R_POWERPC_PLT16_LO:
7095     case elfcpp::R_POWERPC_PLT16_HI:
7096     case elfcpp::R_POWERPC_PLT16_HA:
7097     case elfcpp::R_PPC_SDAREL16:
7098     case elfcpp::R_POWERPC_ADDR30:
7099     case elfcpp::R_PPC64_PLT64:
7100     case elfcpp::R_PPC64_PLTREL64:
7101     case elfcpp::R_PPC64_PLTGOT16:
7102     case elfcpp::R_PPC64_PLTGOT16_LO:
7103     case elfcpp::R_PPC64_PLTGOT16_HI:
7104     case elfcpp::R_PPC64_PLTGOT16_HA:
7105     case elfcpp::R_PPC64_PLT16_LO_DS:
7106     case elfcpp::R_PPC64_PLTGOT16_DS:
7107     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7108     case elfcpp::R_PPC_EMB_RELSEC16:
7109     case elfcpp::R_PPC_EMB_RELST_LO:
7110     case elfcpp::R_PPC_EMB_RELST_HI:
7111     case elfcpp::R_PPC_EMB_RELST_HA:
7112     case elfcpp::R_PPC_EMB_BIT_FLD:
7113     case elfcpp::R_PPC_EMB_RELSDA:
7114     case elfcpp::R_PPC_TOC16:
7115     default:
7116     unsupp:
7117       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7118                              _("unsupported reloc %u"),
7119                              r_type);
7120       break;
7121     }
7122   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
7123     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7124                            _("relocation overflow"));
7125
7126   return true;
7127 }
7128
7129 // Relocate section data.
7130
7131 template<int size, bool big_endian>
7132 void
7133 Target_powerpc<size, big_endian>::relocate_section(
7134     const Relocate_info<size, big_endian>* relinfo,
7135     unsigned int sh_type,
7136     const unsigned char* prelocs,
7137     size_t reloc_count,
7138     Output_section* output_section,
7139     bool needs_special_offset_handling,
7140     unsigned char* view,
7141     Address address,
7142     section_size_type view_size,
7143     const Reloc_symbol_changes* reloc_symbol_changes)
7144 {
7145   typedef Target_powerpc<size, big_endian> Powerpc;
7146   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7147   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7148     Powerpc_comdat_behavior;
7149
7150   gold_assert(sh_type == elfcpp::SHT_RELA);
7151
7152   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7153                          Powerpc_relocate, Powerpc_comdat_behavior>(
7154     relinfo,
7155     this,
7156     prelocs,
7157     reloc_count,
7158     output_section,
7159     needs_special_offset_handling,
7160     view,
7161     address,
7162     view_size,
7163     reloc_symbol_changes);
7164 }
7165
7166 class Powerpc_scan_relocatable_reloc
7167 {
7168 public:
7169   // Return the strategy to use for a local symbol which is not a
7170   // section symbol, given the relocation type.
7171   inline Relocatable_relocs::Reloc_strategy
7172   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7173   {
7174     if (r_type == 0 && r_sym == 0)
7175       return Relocatable_relocs::RELOC_DISCARD;
7176     return Relocatable_relocs::RELOC_COPY;
7177   }
7178
7179   // Return the strategy to use for a local symbol which is a section
7180   // symbol, given the relocation type.
7181   inline Relocatable_relocs::Reloc_strategy
7182   local_section_strategy(unsigned int, Relobj*)
7183   {
7184     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7185   }
7186
7187   // Return the strategy to use for a global symbol, given the
7188   // relocation type, the object, and the symbol index.
7189   inline Relocatable_relocs::Reloc_strategy
7190   global_strategy(unsigned int r_type, Relobj*, unsigned int)
7191   {
7192     if (r_type == elfcpp::R_PPC_PLTREL24)
7193       return Relocatable_relocs::RELOC_SPECIAL;
7194     return Relocatable_relocs::RELOC_COPY;
7195   }
7196 };
7197
7198 // Scan the relocs during a relocatable link.
7199
7200 template<int size, bool big_endian>
7201 void
7202 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7203     Symbol_table* symtab,
7204     Layout* layout,
7205     Sized_relobj_file<size, big_endian>* object,
7206     unsigned int data_shndx,
7207     unsigned int sh_type,
7208     const unsigned char* prelocs,
7209     size_t reloc_count,
7210     Output_section* output_section,
7211     bool needs_special_offset_handling,
7212     size_t local_symbol_count,
7213     const unsigned char* plocal_symbols,
7214     Relocatable_relocs* rr)
7215 {
7216   gold_assert(sh_type == elfcpp::SHT_RELA);
7217
7218   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7219                                 Powerpc_scan_relocatable_reloc>(
7220     symtab,
7221     layout,
7222     object,
7223     data_shndx,
7224     prelocs,
7225     reloc_count,
7226     output_section,
7227     needs_special_offset_handling,
7228     local_symbol_count,
7229     plocal_symbols,
7230     rr);
7231 }
7232
7233 // Emit relocations for a section.
7234 // This is a modified version of the function by the same name in
7235 // target-reloc.h.  Using relocate_special_relocatable for
7236 // R_PPC_PLTREL24 would require duplication of the entire body of the
7237 // loop, so we may as well duplicate the whole thing.
7238
7239 template<int size, bool big_endian>
7240 void
7241 Target_powerpc<size, big_endian>::relocate_relocs(
7242     const Relocate_info<size, big_endian>* relinfo,
7243     unsigned int sh_type,
7244     const unsigned char* prelocs,
7245     size_t reloc_count,
7246     Output_section* output_section,
7247     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7248     const Relocatable_relocs* rr,
7249     unsigned char*,
7250     Address view_address,
7251     section_size_type,
7252     unsigned char* reloc_view,
7253     section_size_type reloc_view_size)
7254 {
7255   gold_assert(sh_type == elfcpp::SHT_RELA);
7256
7257   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7258     Reltype;
7259   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7260     Reltype_write;
7261   const int reloc_size
7262     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7263
7264   Powerpc_relobj<size, big_endian>* const object
7265     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7266   const unsigned int local_count = object->local_symbol_count();
7267   unsigned int got2_shndx = object->got2_shndx();
7268   Address got2_addend = 0;
7269   if (got2_shndx != 0)
7270     {
7271       got2_addend = object->get_output_section_offset(got2_shndx);
7272       gold_assert(got2_addend != invalid_address);
7273     }
7274
7275   unsigned char* pwrite = reloc_view;
7276   bool zap_next = false;
7277   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7278     {
7279       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7280       if (strategy == Relocatable_relocs::RELOC_DISCARD)
7281         continue;
7282
7283       Reltype reloc(prelocs);
7284       Reltype_write reloc_write(pwrite);
7285
7286       Address offset = reloc.get_r_offset();
7287       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7288       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7289       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7290       const unsigned int orig_r_sym = r_sym;
7291       typename elfcpp::Elf_types<size>::Elf_Swxword addend
7292         = reloc.get_r_addend();
7293       const Symbol* gsym = NULL;
7294
7295       if (zap_next)
7296         {
7297           // We could arrange to discard these and other relocs for
7298           // tls optimised sequences in the strategy methods, but for
7299           // now do as BFD ld does.
7300           r_type = elfcpp::R_POWERPC_NONE;
7301           zap_next = false;
7302         }
7303
7304       // Get the new symbol index.
7305       if (r_sym < local_count)
7306         {
7307           switch (strategy)
7308             {
7309             case Relocatable_relocs::RELOC_COPY:
7310             case Relocatable_relocs::RELOC_SPECIAL:
7311               if (r_sym != 0)
7312                 {
7313                   r_sym = object->symtab_index(r_sym);
7314                   gold_assert(r_sym != -1U);
7315                 }
7316               break;
7317
7318             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7319               {
7320                 // We are adjusting a section symbol.  We need to find
7321                 // the symbol table index of the section symbol for
7322                 // the output section corresponding to input section
7323                 // in which this symbol is defined.
7324                 gold_assert(r_sym < local_count);
7325                 bool is_ordinary;
7326                 unsigned int shndx =
7327                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
7328                 gold_assert(is_ordinary);
7329                 Output_section* os = object->output_section(shndx);
7330                 gold_assert(os != NULL);
7331                 gold_assert(os->needs_symtab_index());
7332                 r_sym = os->symtab_index();
7333               }
7334               break;
7335
7336             default:
7337               gold_unreachable();
7338             }
7339         }
7340       else
7341         {
7342           gsym = object->global_symbol(r_sym);
7343           gold_assert(gsym != NULL);
7344           if (gsym->is_forwarder())
7345             gsym = relinfo->symtab->resolve_forwards(gsym);
7346
7347           gold_assert(gsym->has_symtab_index());
7348           r_sym = gsym->symtab_index();
7349         }
7350
7351       // Get the new offset--the location in the output section where
7352       // this relocation should be applied.
7353       if (static_cast<Address>(offset_in_output_section) != invalid_address)
7354         offset += offset_in_output_section;
7355       else
7356         {
7357           section_offset_type sot_offset =
7358             convert_types<section_offset_type, Address>(offset);
7359           section_offset_type new_sot_offset =
7360             output_section->output_offset(object, relinfo->data_shndx,
7361                                           sot_offset);
7362           gold_assert(new_sot_offset != -1);
7363           offset = new_sot_offset;
7364         }
7365
7366       // In an object file, r_offset is an offset within the section.
7367       // In an executable or dynamic object, generated by
7368       // --emit-relocs, r_offset is an absolute address.
7369       if (!parameters->options().relocatable())
7370         {
7371           offset += view_address;
7372           if (static_cast<Address>(offset_in_output_section) != invalid_address)
7373             offset -= offset_in_output_section;
7374         }
7375
7376       // Handle the reloc addend based on the strategy.
7377       if (strategy == Relocatable_relocs::RELOC_COPY)
7378         ;
7379       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7380         {
7381           const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7382           addend = psymval->value(object, addend);
7383         }
7384       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7385         {
7386           if (addend >= 32768)
7387             addend += got2_addend;
7388         }
7389       else
7390         gold_unreachable();
7391
7392       if (!parameters->options().relocatable())
7393         {
7394           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7395               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7396               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7397               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7398             {
7399               // First instruction of a global dynamic sequence,
7400               // arg setup insn.
7401               const bool final = gsym == NULL || gsym->final_value_is_known();
7402               switch (this->optimize_tls_gd(final))
7403                 {
7404                 case tls::TLSOPT_TO_IE:
7405                   r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7406                              - elfcpp::R_POWERPC_GOT_TLSGD16);
7407                   break;
7408                 case tls::TLSOPT_TO_LE:
7409                   if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7410                       || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7411                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
7412                   else
7413                     {
7414                       r_type = elfcpp::R_POWERPC_NONE;
7415                       offset -= 2 * big_endian;
7416                     }
7417                   break;
7418                 default:
7419                   break;
7420                 }
7421             }
7422           else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7423                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
7424                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
7425                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
7426             {
7427               // First instruction of a local dynamic sequence,
7428               // arg setup insn.
7429               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7430                 {
7431                   if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
7432                       || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
7433                     {
7434                       r_type = elfcpp::R_POWERPC_TPREL16_HA;
7435                       const Output_section* os = relinfo->layout->tls_segment()
7436                         ->first_section();
7437                       gold_assert(os != NULL);
7438                       gold_assert(os->needs_symtab_index());
7439                       r_sym = os->symtab_index();
7440                       addend = dtp_offset;
7441                     }
7442                   else
7443                     {
7444                       r_type = elfcpp::R_POWERPC_NONE;
7445                       offset -= 2 * big_endian;
7446                     }
7447                 }
7448             }
7449           else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7450                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
7451                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
7452                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
7453             {
7454               // First instruction of initial exec sequence.
7455               const bool final = gsym == NULL || gsym->final_value_is_known();
7456               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7457                 {
7458                   if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7459                       || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7460                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
7461                   else
7462                     {
7463                       r_type = elfcpp::R_POWERPC_NONE;
7464                       offset -= 2 * big_endian;
7465                     }
7466                 }
7467             }
7468           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7469                    || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7470             {
7471               // Second instruction of a global dynamic sequence,
7472               // the __tls_get_addr call
7473               const bool final = gsym == NULL || gsym->final_value_is_known();
7474               switch (this->optimize_tls_gd(final))
7475                 {
7476                 case tls::TLSOPT_TO_IE:
7477                   r_type = elfcpp::R_POWERPC_NONE;
7478                   zap_next = true;
7479                   break;
7480                 case tls::TLSOPT_TO_LE:
7481                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
7482                   offset += 2 * big_endian;
7483                   zap_next = true;
7484                   break;
7485                 default:
7486                   break;
7487                 }
7488             }
7489           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7490                    || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7491             {
7492               // Second instruction of a local dynamic sequence,
7493               // the __tls_get_addr call
7494               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
7495                 {
7496                   const Output_section* os = relinfo->layout->tls_segment()
7497                     ->first_section();
7498                   gold_assert(os != NULL);
7499                   gold_assert(os->needs_symtab_index());
7500                   r_sym = os->symtab_index();
7501                   addend = dtp_offset;
7502                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
7503                   offset += 2 * big_endian;
7504                   zap_next = true;
7505                 }
7506             }
7507           else if (r_type == elfcpp::R_POWERPC_TLS)
7508             {
7509               // Second instruction of an initial exec sequence
7510               const bool final = gsym == NULL || gsym->final_value_is_known();
7511               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
7512                 {
7513                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
7514                   offset += 2 * big_endian;
7515                 }
7516             }
7517         }
7518
7519       reloc_write.put_r_offset(offset);
7520       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
7521       reloc_write.put_r_addend(addend);
7522
7523       pwrite += reloc_size;
7524     }
7525
7526   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
7527               == reloc_view_size);
7528 }
7529
7530 // Return the value to use for a dynamic symbol which requires special
7531 // treatment.  This is how we support equality comparisons of function
7532 // pointers across shared library boundaries, as described in the
7533 // processor specific ABI supplement.
7534
7535 template<int size, bool big_endian>
7536 uint64_t
7537 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
7538 {
7539   if (size == 32)
7540     {
7541       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
7542       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7543            p != this->stub_tables_.end();
7544            ++p)
7545         {
7546           Address off = (*p)->find_plt_call_entry(gsym);
7547           if (off != invalid_address)
7548             return (*p)->stub_address() + off;
7549         }
7550     }
7551   gold_unreachable();
7552 }
7553
7554 // Return the PLT address to use for a local symbol.
7555 template<int size, bool big_endian>
7556 uint64_t
7557 Target_powerpc<size, big_endian>::do_plt_address_for_local(
7558     const Relobj* object,
7559     unsigned int symndx) const
7560 {
7561   if (size == 32)
7562     {
7563       const Sized_relobj<size, big_endian>* relobj
7564         = static_cast<const Sized_relobj<size, big_endian>*>(object);
7565       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7566            p != this->stub_tables_.end();
7567            ++p)
7568         {
7569           Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
7570                                                   symndx);
7571           if (off != invalid_address)
7572             return (*p)->stub_address() + off;
7573         }
7574     }
7575   gold_unreachable();
7576 }
7577
7578 // Return the PLT address to use for a global symbol.
7579 template<int size, bool big_endian>
7580 uint64_t
7581 Target_powerpc<size, big_endian>::do_plt_address_for_global(
7582     const Symbol* gsym) const
7583 {
7584   if (size == 32)
7585     {
7586       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
7587            p != this->stub_tables_.end();
7588            ++p)
7589         {
7590           Address off = (*p)->find_plt_call_entry(gsym);
7591           if (off != invalid_address)
7592             return (*p)->stub_address() + off;
7593         }
7594     }
7595   gold_unreachable();
7596 }
7597
7598 // Return the offset to use for the GOT_INDX'th got entry which is
7599 // for a local tls symbol specified by OBJECT, SYMNDX.
7600 template<int size, bool big_endian>
7601 int64_t
7602 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
7603     const Relobj* object,
7604     unsigned int symndx,
7605     unsigned int got_indx) const
7606 {
7607   const Powerpc_relobj<size, big_endian>* ppc_object
7608     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
7609   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
7610     {
7611       for (Got_type got_type = GOT_TYPE_TLSGD;
7612            got_type <= GOT_TYPE_TPREL;
7613            got_type = Got_type(got_type + 1))
7614         if (ppc_object->local_has_got_offset(symndx, got_type))
7615           {
7616             unsigned int off = ppc_object->local_got_offset(symndx, got_type);
7617             if (got_type == GOT_TYPE_TLSGD)
7618               off += size / 8;
7619             if (off == got_indx * (size / 8))
7620               {
7621                 if (got_type == GOT_TYPE_TPREL)
7622                   return -tp_offset;
7623                 else
7624                   return -dtp_offset;
7625               }
7626           }
7627     }
7628   gold_unreachable();
7629 }
7630
7631 // Return the offset to use for the GOT_INDX'th got entry which is
7632 // for global tls symbol GSYM.
7633 template<int size, bool big_endian>
7634 int64_t
7635 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
7636     Symbol* gsym,
7637     unsigned int got_indx) const
7638 {
7639   if (gsym->type() == elfcpp::STT_TLS)
7640     {
7641       for (Got_type got_type = GOT_TYPE_TLSGD;
7642            got_type <= GOT_TYPE_TPREL;
7643            got_type = Got_type(got_type + 1))
7644         if (gsym->has_got_offset(got_type))
7645           {
7646             unsigned int off = gsym->got_offset(got_type);
7647             if (got_type == GOT_TYPE_TLSGD)
7648               off += size / 8;
7649             if (off == got_indx * (size / 8))
7650               {
7651                 if (got_type == GOT_TYPE_TPREL)
7652                   return -tp_offset;
7653                 else
7654                   return -dtp_offset;
7655               }
7656           }
7657     }
7658   gold_unreachable();
7659 }
7660
7661 // The selector for powerpc object files.
7662
7663 template<int size, bool big_endian>
7664 class Target_selector_powerpc : public Target_selector
7665 {
7666 public:
7667   Target_selector_powerpc()
7668     : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
7669                       size, big_endian,
7670                       (size == 64
7671                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
7672                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
7673                       (size == 64
7674                        ? (big_endian ? "elf64ppc" : "elf64lppc")
7675                        : (big_endian ? "elf32ppc" : "elf32lppc")))
7676   { }
7677
7678   virtual Target*
7679   do_instantiate_target()
7680   { return new Target_powerpc<size, big_endian>(); }
7681 };
7682
7683 Target_selector_powerpc<32, true> target_selector_ppc32;
7684 Target_selector_powerpc<32, false> target_selector_ppc32le;
7685 Target_selector_powerpc<64, true> target_selector_ppc64;
7686 Target_selector_powerpc<64, false> target_selector_ppc64le;
7687
7688 // Instantiate these constants for -O0
7689 template<int size, bool big_endian>
7690 const int Output_data_glink<size, big_endian>::pltresolve_size;
7691 template<int size, bool big_endian>
7692 const typename Stub_table<size, big_endian>::Address
7693   Stub_table<size, big_endian>::invalid_address;
7694 template<int size, bool big_endian>
7695 const typename Target_powerpc<size, big_endian>::Address
7696   Target_powerpc<size, big_endian>::invalid_address;
7697
7698 } // End anonymous namespace.