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