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