792905e3dbe0cb182eed5ed8093f3a5e691ad8ef
[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   Address
1019   symval_for_branch(const Symbol_table* symtab, Address value,
1020                     const Sized_symbol<size>* gsym,
1021                     Powerpc_relobj<size, big_endian>* object,
1022                     unsigned int *dest_shndx);
1023
1024   // The class which implements relocation.
1025   class Relocate : protected Track_tls
1026   {
1027    public:
1028     // Use 'at' branch hints when true, 'y' when false.
1029     // FIXME maybe: set this with an option.
1030     static const bool is_isa_v2 = true;
1031
1032     Relocate()
1033       : Track_tls()
1034     { }
1035
1036     // Do a relocation.  Return false if the caller should not issue
1037     // any warnings about this relocation.
1038     inline bool
1039     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
1040              Output_section*, size_t relnum,
1041              const elfcpp::Rela<size, big_endian>&,
1042              unsigned int r_type, const Sized_symbol<size>*,
1043              const Symbol_value<size>*,
1044              unsigned char*,
1045              typename elfcpp::Elf_types<size>::Elf_Addr,
1046              section_size_type);
1047   };
1048
1049   class Relocate_comdat_behavior
1050   {
1051    public:
1052     // Decide what the linker should do for relocations that refer to
1053     // discarded comdat sections.
1054     inline Comdat_behavior
1055     get(const char* name)
1056     {
1057       gold::Default_comdat_behavior default_behavior;
1058       Comdat_behavior ret = default_behavior.get(name);
1059       if (ret == CB_WARNING)
1060         {
1061           if (size == 32
1062               && (strcmp(name, ".fixup") == 0
1063                   || strcmp(name, ".got2") == 0))
1064             ret = CB_IGNORE;
1065           if (size == 64
1066               && (strcmp(name, ".opd") == 0
1067                   || strcmp(name, ".toc") == 0
1068                   || strcmp(name, ".toc1") == 0))
1069             ret = CB_IGNORE;
1070         }
1071       return ret;
1072     }
1073   };
1074
1075   // A class which returns the size required for a relocation type,
1076   // used while scanning relocs during a relocatable link.
1077   class Relocatable_size_for_reloc
1078   {
1079    public:
1080     unsigned int
1081     get_size_for_reloc(unsigned int, Relobj*)
1082     {
1083       gold_unreachable();
1084       return 0;
1085     }
1086   };
1087
1088   // Optimize the TLS relocation type based on what we know about the
1089   // symbol.  IS_FINAL is true if the final address of this symbol is
1090   // known at link time.
1091
1092   tls::Tls_optimization
1093   optimize_tls_gd(bool is_final)
1094   {
1095     // If we are generating a shared library, then we can't do anything
1096     // in the linker.
1097     if (parameters->options().shared())
1098       return tls::TLSOPT_NONE;
1099
1100     if (!is_final)
1101       return tls::TLSOPT_TO_IE;
1102     return tls::TLSOPT_TO_LE;
1103   }
1104
1105   tls::Tls_optimization
1106   optimize_tls_ld()
1107   {
1108     if (parameters->options().shared())
1109       return tls::TLSOPT_NONE;
1110
1111     return tls::TLSOPT_TO_LE;
1112   }
1113
1114   tls::Tls_optimization
1115   optimize_tls_ie(bool is_final)
1116   {
1117     if (!is_final || parameters->options().shared())
1118       return tls::TLSOPT_NONE;
1119
1120     return tls::TLSOPT_TO_LE;
1121   }
1122
1123   // Create glink.
1124   void
1125   make_glink_section(Layout*);
1126
1127   // Create the PLT section.
1128   void
1129   make_plt_section(Symbol_table*, Layout*);
1130
1131   void
1132   make_iplt_section(Symbol_table*, Layout*);
1133
1134   void
1135   make_brlt_section(Layout*);
1136
1137   // Create a PLT entry for a global symbol.
1138   void
1139   make_plt_entry(Symbol_table*, Layout*, Symbol*);
1140
1141   // Create a PLT entry for a local IFUNC symbol.
1142   void
1143   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1144                              Sized_relobj_file<size, big_endian>*,
1145                              unsigned int);
1146
1147
1148   // Create a GOT entry for local dynamic __tls_get_addr.
1149   unsigned int
1150   tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1151                    Sized_relobj_file<size, big_endian>* object);
1152
1153   unsigned int
1154   tlsld_got_offset() const
1155   {
1156     return this->tlsld_got_offset_;
1157   }
1158
1159   // Get the dynamic reloc section, creating it if necessary.
1160   Reloc_section*
1161   rela_dyn_section(Layout*);
1162
1163   // Similarly, but for ifunc symbols get the one for ifunc.
1164   Reloc_section*
1165   rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1166
1167   // Copy a relocation against a global symbol.
1168   void
1169   copy_reloc(Symbol_table* symtab, Layout* layout,
1170              Sized_relobj_file<size, big_endian>* object,
1171              unsigned int shndx, Output_section* output_section,
1172              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1173   {
1174     this->copy_relocs_.copy_reloc(symtab, layout,
1175                                   symtab->get_sized_symbol<size>(sym),
1176                                   object, shndx, output_section,
1177                                   reloc, this->rela_dyn_section(layout));
1178   }
1179
1180   // Look over all the input sections, deciding where to place stubs.
1181   void
1182   group_sections(Layout*, const Task*);
1183
1184   // Sort output sections by address.
1185   struct Sort_sections
1186   {
1187     bool
1188     operator()(const Output_section* sec1, const Output_section* sec2)
1189     { return sec1->address() < sec2->address(); }
1190   };
1191
1192   class Branch_info
1193   {
1194    public:
1195     Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1196                 unsigned int data_shndx,
1197                 Address r_offset,
1198                 unsigned int r_type,
1199                 unsigned int r_sym,
1200                 Address addend)
1201       : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1202         r_type_(r_type), r_sym_(r_sym), addend_(addend)
1203     { }
1204
1205     ~Branch_info()
1206     { }
1207
1208     // If this branch needs a plt call stub, or a long branch stub, make one.
1209     void
1210     make_stub(Stub_table<size, big_endian>*,
1211               Stub_table<size, big_endian>*,
1212               Symbol_table*) const;
1213
1214    private:
1215     // The branch location..
1216     Powerpc_relobj<size, big_endian>* object_;
1217     unsigned int shndx_;
1218     Address offset_;
1219     // ..and the branch type and destination.
1220     unsigned int r_type_;
1221     unsigned int r_sym_;
1222     Address addend_;
1223   };
1224
1225   // Information about this specific target which we pass to the
1226   // general Target structure.
1227   static Target::Target_info powerpc_info;
1228
1229   // The types of GOT entries needed for this platform.
1230   // These values are exposed to the ABI in an incremental link.
1231   // Do not renumber existing values without changing the version
1232   // number of the .gnu_incremental_inputs section.
1233   enum Got_type
1234   {
1235     GOT_TYPE_STANDARD,
1236     GOT_TYPE_TLSGD,     // double entry for @got@tlsgd
1237     GOT_TYPE_DTPREL,    // entry for @got@dtprel
1238     GOT_TYPE_TPREL      // entry for @got@tprel
1239   };
1240
1241   // The GOT section.
1242   Output_data_got_powerpc<size, big_endian>* got_;
1243   // The PLT section.  This is a container for a table of addresses,
1244   // and their relocations.  Each address in the PLT has a dynamic
1245   // relocation (R_*_JMP_SLOT) and each address will have a
1246   // corresponding entry in .glink for lazy resolution of the PLT.
1247   // ppc32 initialises the PLT to point at the .glink entry, while
1248   // ppc64 leaves this to ld.so.  To make a call via the PLT, the
1249   // linker adds a stub that loads the PLT entry into ctr then
1250   // branches to ctr.  There may be more than one stub for each PLT
1251   // entry.  DT_JMPREL points at the first PLT dynamic relocation and
1252   // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1253   Output_data_plt_powerpc<size, big_endian>* plt_;
1254   // The IPLT section.  Like plt_, this is a container for a table of
1255   // addresses and their relocations, specifically for STT_GNU_IFUNC
1256   // functions that resolve locally (STT_GNU_IFUNC functions that
1257   // don't resolve locally go in PLT).  Unlike plt_, these have no
1258   // entry in .glink for lazy resolution, and the relocation section
1259   // does not have a 1-1 correspondence with IPLT addresses.  In fact,
1260   // the relocation section may contain relocations against
1261   // STT_GNU_IFUNC symbols at locations outside of IPLT.  The
1262   // relocation section will appear at the end of other dynamic
1263   // relocations, so that ld.so applies these relocations after other
1264   // dynamic relocations.  In a static executable, the relocation
1265   // section is emitted and marked with __rela_iplt_start and
1266   // __rela_iplt_end symbols.
1267   Output_data_plt_powerpc<size, big_endian>* iplt_;
1268   // Section holding long branch destinations.
1269   Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1270   // The .glink section.
1271   Output_data_glink<size, big_endian>* glink_;
1272   // The dynamic reloc section.
1273   Reloc_section* rela_dyn_;
1274   // Relocs saved to avoid a COPY reloc.
1275   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1276   // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1277   unsigned int tlsld_got_offset_;
1278
1279   Stub_tables stub_tables_;
1280   typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1281   Branch_lookup_table branch_lookup_table_;
1282
1283   typedef std::vector<Branch_info> Branches;
1284   Branches branch_info_;
1285
1286   bool plt_thread_safe_;
1287 };
1288
1289 template<>
1290 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1291 {
1292   32,                   // size
1293   true,                 // is_big_endian
1294   elfcpp::EM_PPC,       // machine_code
1295   false,                // has_make_symbol
1296   false,                // has_resolve
1297   false,                // has_code_fill
1298   true,                 // is_default_stack_executable
1299   false,                // can_icf_inline_merge_sections
1300   '\0',                 // wrap_char
1301   "/usr/lib/ld.so.1",   // dynamic_linker
1302   0x10000000,           // default_text_segment_address
1303   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1304   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1305   false,                // isolate_execinstr
1306   0,                    // rosegment_gap
1307   elfcpp::SHN_UNDEF,    // small_common_shndx
1308   elfcpp::SHN_UNDEF,    // large_common_shndx
1309   0,                    // small_common_section_flags
1310   0,                    // large_common_section_flags
1311   NULL,                 // attributes_section
1312   NULL,                 // attributes_vendor
1313   "_start"              // entry_symbol_name
1314 };
1315
1316 template<>
1317 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1318 {
1319   32,                   // size
1320   false,                // is_big_endian
1321   elfcpp::EM_PPC,       // machine_code
1322   false,                // has_make_symbol
1323   false,                // has_resolve
1324   false,                // has_code_fill
1325   true,                 // is_default_stack_executable
1326   false,                // can_icf_inline_merge_sections
1327   '\0',                 // wrap_char
1328   "/usr/lib/ld.so.1",   // dynamic_linker
1329   0x10000000,           // default_text_segment_address
1330   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1331   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1332   false,                // isolate_execinstr
1333   0,                    // rosegment_gap
1334   elfcpp::SHN_UNDEF,    // small_common_shndx
1335   elfcpp::SHN_UNDEF,    // large_common_shndx
1336   0,                    // small_common_section_flags
1337   0,                    // large_common_section_flags
1338   NULL,                 // attributes_section
1339   NULL,                 // attributes_vendor
1340   "_start"              // entry_symbol_name
1341 };
1342
1343 template<>
1344 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1345 {
1346   64,                   // size
1347   true,                 // is_big_endian
1348   elfcpp::EM_PPC64,     // machine_code
1349   false,                // has_make_symbol
1350   false,                // has_resolve
1351   false,                // has_code_fill
1352   true,                 // is_default_stack_executable
1353   false,                // can_icf_inline_merge_sections
1354   '\0',                 // wrap_char
1355   "/usr/lib/ld.so.1",   // dynamic_linker
1356   0x10000000,           // default_text_segment_address
1357   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1358   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1359   false,                // isolate_execinstr
1360   0,                    // rosegment_gap
1361   elfcpp::SHN_UNDEF,    // small_common_shndx
1362   elfcpp::SHN_UNDEF,    // large_common_shndx
1363   0,                    // small_common_section_flags
1364   0,                    // large_common_section_flags
1365   NULL,                 // attributes_section
1366   NULL,                 // attributes_vendor
1367   "_start"              // entry_symbol_name
1368 };
1369
1370 template<>
1371 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1372 {
1373   64,                   // size
1374   false,                // is_big_endian
1375   elfcpp::EM_PPC64,     // machine_code
1376   false,                // has_make_symbol
1377   false,                // has_resolve
1378   false,                // has_code_fill
1379   true,                 // is_default_stack_executable
1380   false,                // can_icf_inline_merge_sections
1381   '\0',                 // wrap_char
1382   "/usr/lib/ld.so.1",   // dynamic_linker
1383   0x10000000,           // default_text_segment_address
1384   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
1385   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
1386   false,                // isolate_execinstr
1387   0,                    // rosegment_gap
1388   elfcpp::SHN_UNDEF,    // small_common_shndx
1389   elfcpp::SHN_UNDEF,    // large_common_shndx
1390   0,                    // small_common_section_flags
1391   0,                    // large_common_section_flags
1392   NULL,                 // attributes_section
1393   NULL,                 // attributes_vendor
1394   "_start"              // entry_symbol_name
1395 };
1396
1397 inline bool
1398 is_branch_reloc(unsigned int r_type)
1399 {
1400   return (r_type == elfcpp::R_POWERPC_REL24
1401           || r_type == elfcpp::R_PPC_PLTREL24
1402           || r_type == elfcpp::R_PPC_LOCAL24PC
1403           || r_type == elfcpp::R_POWERPC_REL14
1404           || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1405           || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1406           || r_type == elfcpp::R_POWERPC_ADDR24
1407           || r_type == elfcpp::R_POWERPC_ADDR14
1408           || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1409           || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1410 }
1411
1412 // If INSN is an opcode that may be used with an @tls operand, return
1413 // the transformed insn for TLS optimisation, otherwise return 0.  If
1414 // REG is non-zero only match an insn with RB or RA equal to REG.
1415 uint32_t
1416 at_tls_transform(uint32_t insn, unsigned int reg)
1417 {
1418   if ((insn & (0x3f << 26)) != 31 << 26)
1419     return 0;
1420
1421   unsigned int rtra;
1422   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1423     rtra = insn & ((1 << 26) - (1 << 16));
1424   else if (((insn >> 16) & 0x1f) == reg)
1425     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1426   else
1427     return 0;
1428
1429   if ((insn & (0x3ff << 1)) == 266 << 1)
1430     // add -> addi
1431     insn = 14 << 26;
1432   else if ((insn & (0x1f << 1)) == 23 << 1
1433            && ((insn & (0x1f << 6)) < 14 << 6
1434                || ((insn & (0x1f << 6)) >= 16 << 6
1435                    && (insn & (0x1f << 6)) < 24 << 6)))
1436     // load and store indexed -> dform
1437     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1438   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1439     // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1440     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1441   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1442     // lwax -> lwa
1443     insn = (58 << 26) | 2;
1444   else
1445     return 0;
1446   insn |= rtra;
1447   return insn;
1448 }
1449
1450
1451 template<int size, bool big_endian>
1452 class Powerpc_relocate_functions
1453 {
1454 public:
1455   enum Overflow_check
1456   {
1457     CHECK_NONE,
1458     CHECK_SIGNED,
1459     CHECK_UNSIGNED,
1460     CHECK_BITFIELD,
1461     CHECK_LOW_INSN,
1462     CHECK_HIGH_INSN
1463   };
1464
1465   enum Status
1466   {
1467     STATUS_OK,
1468     STATUS_OVERFLOW
1469   };
1470
1471 private:
1472   typedef Powerpc_relocate_functions<size, big_endian> This;
1473   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1474
1475   template<int valsize>
1476   static inline bool
1477   has_overflow_signed(Address value)
1478   {
1479     // limit = 1 << (valsize - 1) without shift count exceeding size of type
1480     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1481     limit <<= ((valsize - 1) >> 1);
1482     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1483     return value + limit > (limit << 1) - 1;
1484   }
1485
1486   template<int valsize>
1487   static inline bool
1488   has_overflow_unsigned(Address value)
1489   {
1490     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
1491     limit <<= ((valsize - 1) >> 1);
1492     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
1493     return value > (limit << 1) - 1;
1494   }
1495
1496   template<int valsize>
1497   static inline bool
1498   has_overflow_bitfield(Address value)
1499   {
1500     return (has_overflow_unsigned<valsize>(value)
1501             && has_overflow_signed<valsize>(value));
1502   }
1503
1504   template<int valsize>
1505   static inline Status
1506   overflowed(Address value, Overflow_check overflow)
1507   {
1508     if (overflow == CHECK_SIGNED)
1509       {
1510         if (has_overflow_signed<valsize>(value))
1511           return STATUS_OVERFLOW;
1512       }
1513     else if (overflow == CHECK_UNSIGNED)
1514       {
1515         if (has_overflow_unsigned<valsize>(value))
1516           return STATUS_OVERFLOW;
1517       }
1518     else if (overflow == CHECK_BITFIELD)
1519       {
1520         if (has_overflow_bitfield<valsize>(value))
1521           return STATUS_OVERFLOW;
1522       }
1523     return STATUS_OK;
1524   }
1525
1526   // Do a simple RELA relocation
1527   template<int 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       unsigned long 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       to += this->addend_;
2666       if (stub_table == NULL)
2667         stub_table = this->object_->stub_table(this->shndx_);
2668       if (size == 64 && target->abiversion() < 2)
2669         {
2670           unsigned int dest_shndx;
2671           to = target->symval_for_branch(symtab, to, gsym,
2672                                          this->object_, &dest_shndx);
2673         }
2674       Address delta = to - from;
2675       if (delta + max_branch_offset >= 2 * max_branch_offset)
2676         {
2677           if (stub_table == NULL)
2678             {
2679               gold_warning(_("%s:%s: branch in non-executable section,"
2680                              " no long branch stub for you"),
2681                            this->object_->name().c_str(),
2682                            this->object_->section_name(this->shndx_).c_str());
2683               return;
2684             }
2685           stub_table->add_long_branch_entry(this->object_, to);
2686         }
2687     }
2688 }
2689
2690 // Relaxation hook.  This is where we do stub generation.
2691
2692 template<int size, bool big_endian>
2693 bool
2694 Target_powerpc<size, big_endian>::do_relax(int pass,
2695                                            const Input_objects*,
2696                                            Symbol_table* symtab,
2697                                            Layout* layout,
2698                                            const Task* task)
2699 {
2700   unsigned int prev_brlt_size = 0;
2701   if (pass == 1)
2702     {
2703       bool thread_safe
2704         = this->abiversion() < 2 && parameters->options().plt_thread_safe();
2705       if (size == 64
2706           && this->abiversion() < 2
2707           && !thread_safe
2708           && !parameters->options().user_set_plt_thread_safe())
2709         {
2710           static const char* const thread_starter[] =
2711             {
2712               "pthread_create",
2713               /* libstdc++ */
2714               "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
2715               /* librt */
2716               "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
2717               "mq_notify", "create_timer",
2718               /* libanl */
2719               "getaddrinfo_a",
2720               /* libgomp */
2721               "GOMP_parallel",
2722               "GOMP_parallel_start",
2723               "GOMP_parallel_loop_static",
2724               "GOMP_parallel_loop_static_start",
2725               "GOMP_parallel_loop_dynamic",
2726               "GOMP_parallel_loop_dynamic_start",
2727               "GOMP_parallel_loop_guided",
2728               "GOMP_parallel_loop_guided_start",
2729               "GOMP_parallel_loop_runtime",
2730               "GOMP_parallel_loop_runtime_start",
2731               "GOMP_parallel_sections",
2732               "GOMP_parallel_sections_start",
2733               /* libgo */
2734               "__go_go",
2735             };
2736
2737           if (parameters->options().shared())
2738             thread_safe = true;
2739           else
2740             {
2741               for (unsigned int i = 0;
2742                    i < sizeof(thread_starter) / sizeof(thread_starter[0]);
2743                    i++)
2744                 {
2745                   Symbol* sym = symtab->lookup(thread_starter[i], NULL);
2746                   thread_safe = (sym != NULL
2747                                  && sym->in_reg()
2748                                  && sym->in_real_elf());
2749                   if (thread_safe)
2750                     break;
2751                 }
2752             }
2753         }
2754       this->plt_thread_safe_ = thread_safe;
2755       this->group_sections(layout, task);
2756     }
2757
2758   // We need address of stub tables valid for make_stub.
2759   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2760        p != this->stub_tables_.end();
2761        ++p)
2762     {
2763       const Powerpc_relobj<size, big_endian>* object
2764         = static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
2765       Address off = object->get_output_section_offset((*p)->shndx());
2766       gold_assert(off != invalid_address);
2767       Output_section* os = (*p)->output_section();
2768       (*p)->set_address_and_size(os, off);
2769     }
2770
2771   if (pass != 1)
2772     {
2773       // Clear plt call stubs, long branch stubs and branch lookup table.
2774       prev_brlt_size = this->branch_lookup_table_.size();
2775       this->branch_lookup_table_.clear();
2776       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2777            p != this->stub_tables_.end();
2778            ++p)
2779         {
2780           (*p)->clear_stubs();
2781         }
2782     }
2783
2784   // Build all the stubs.
2785   Stub_table<size, big_endian>* ifunc_stub_table
2786     = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
2787   Stub_table<size, big_endian>* one_stub_table
2788     = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
2789   for (typename Branches::const_iterator b = this->branch_info_.begin();
2790        b != this->branch_info_.end();
2791        b++)
2792     {
2793       b->make_stub(one_stub_table, ifunc_stub_table, symtab);
2794     }
2795
2796   // Did anything change size?
2797   unsigned int num_huge_branches = this->branch_lookup_table_.size();
2798   bool again = num_huge_branches != prev_brlt_size;
2799   if (size == 64 && num_huge_branches != 0)
2800     this->make_brlt_section(layout);
2801   if (size == 64 && again)
2802     this->brlt_section_->set_current_size(num_huge_branches);
2803
2804   typedef Unordered_set<Output_section*> Output_sections;
2805   Output_sections os_need_update;
2806   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
2807        p != this->stub_tables_.end();
2808        ++p)
2809     {
2810       if ((*p)->size_update())
2811         {
2812           again = true;
2813           (*p)->add_eh_frame(layout);
2814           os_need_update.insert((*p)->output_section());
2815         }
2816     }
2817
2818   // Set output section offsets for all input sections in an output
2819   // section that just changed size.  Anything past the stubs will
2820   // need updating.
2821   for (typename Output_sections::iterator p = os_need_update.begin();
2822        p != os_need_update.end();
2823        p++)
2824     {
2825       Output_section* os = *p;
2826       Address off = 0;
2827       typedef Output_section::Input_section_list Input_section_list;
2828       for (Input_section_list::const_iterator i = os->input_sections().begin();
2829            i != os->input_sections().end();
2830            ++i)
2831         {
2832           off = align_address(off, i->addralign());
2833           if (i->is_input_section() || i->is_relaxed_input_section())
2834             i->relobj()->set_section_offset(i->shndx(), off);
2835           if (i->is_relaxed_input_section())
2836             {
2837               Stub_table<size, big_endian>* stub_table
2838                 = static_cast<Stub_table<size, big_endian>*>(
2839                     i->relaxed_input_section());
2840               off += stub_table->set_address_and_size(os, off);
2841             }
2842           else
2843             off += i->data_size();
2844         }
2845       // If .branch_lt is part of this output section, then we have
2846       // just done the offset adjustment.
2847       os->clear_section_offsets_need_adjustment();
2848     }
2849
2850   if (size == 64
2851       && !again
2852       && num_huge_branches != 0
2853       && parameters->options().output_is_position_independent())
2854     {
2855       // Fill in the BRLT relocs.
2856       this->brlt_section_->reset_brlt_sizes();
2857       for (typename Branch_lookup_table::const_iterator p
2858              = this->branch_lookup_table_.begin();
2859            p != this->branch_lookup_table_.end();
2860            ++p)
2861         {
2862           this->brlt_section_->add_reloc(p->first, p->second);
2863         }
2864       this->brlt_section_->finalize_brlt_sizes();
2865     }
2866   return again;
2867 }
2868
2869 template<int size, bool big_endian>
2870 void
2871 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
2872                                                       unsigned char* oview,
2873                                                       uint64_t* paddress,
2874                                                       off_t* plen) const
2875 {
2876   uint64_t address = plt->address();
2877   off_t len = plt->data_size();
2878
2879   if (plt == this->glink_)
2880     {
2881       // See Output_data_glink::do_write() for glink contents.
2882       if (len == 0)
2883         {
2884           gold_assert(parameters->doing_static_link());
2885           // Static linking may need stubs, to support ifunc and long
2886           // branches.  We need to create an output section for
2887           // .eh_frame early in the link process, to have a place to
2888           // attach stub .eh_frame info.  We also need to have
2889           // registered a CIE that matches the stub CIE.  Both of
2890           // these requirements are satisfied by creating an FDE and
2891           // CIE for .glink, even though static linking will leave
2892           // .glink zero length.
2893           // ??? Hopefully generating an FDE with a zero address range
2894           // won't confuse anything that consumes .eh_frame info.
2895         }
2896       else if (size == 64)
2897         {
2898           // There is one word before __glink_PLTresolve
2899           address += 8;
2900           len -= 8;
2901         }
2902       else if (parameters->options().output_is_position_independent())
2903         {
2904           // There are two FDEs for a position independent glink.
2905           // The first covers the branch table, the second
2906           // __glink_PLTresolve at the end of glink.
2907           off_t resolve_size = this->glink_->pltresolve_size;
2908           if (oview[9] == elfcpp::DW_CFA_nop)
2909             len -= resolve_size;
2910           else
2911             {
2912               address += len - resolve_size;
2913               len = resolve_size;
2914             }
2915         }
2916     }
2917   else
2918     {
2919       // Must be a stub table.
2920       const Stub_table<size, big_endian>* stub_table
2921         = static_cast<const Stub_table<size, big_endian>*>(plt);
2922       uint64_t stub_address = stub_table->stub_address();
2923       len -= stub_address - address;
2924       address = stub_address;
2925     }
2926
2927   *paddress = address;
2928   *plen = len;
2929 }
2930
2931 // A class to handle the PLT data.
2932
2933 template<int size, bool big_endian>
2934 class Output_data_plt_powerpc : public Output_section_data_build
2935 {
2936  public:
2937   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
2938                             size, big_endian> Reloc_section;
2939
2940   Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
2941                           Reloc_section* plt_rel,
2942                           const char* name)
2943     : Output_section_data_build(size == 32 ? 4 : 8),
2944       rel_(plt_rel),
2945       targ_(targ),
2946       name_(name)
2947   { }
2948
2949   // Add an entry to the PLT.
2950   void
2951   add_entry(Symbol*);
2952
2953   void
2954   add_ifunc_entry(Symbol*);
2955
2956   void
2957   add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
2958
2959   // Return the .rela.plt section data.
2960   Reloc_section*
2961   rel_plt() const
2962   {
2963     return this->rel_;
2964   }
2965
2966   // Return the number of PLT entries.
2967   unsigned int
2968   entry_count() const
2969   {
2970     if (this->current_data_size() == 0)
2971       return 0;
2972     return ((this->current_data_size() - this->first_plt_entry_offset())
2973             / this->plt_entry_size());
2974   }
2975
2976  protected:
2977   void
2978   do_adjust_output_section(Output_section* os)
2979   {
2980     os->set_entsize(0);
2981   }
2982
2983   // Write to a map file.
2984   void
2985   do_print_to_mapfile(Mapfile* mapfile) const
2986   { mapfile->print_output_data(this, this->name_); }
2987
2988  private:
2989   // Return the offset of the first non-reserved PLT entry.
2990   unsigned int
2991   first_plt_entry_offset() const
2992   {
2993     // IPLT has no reserved entry.
2994     if (this->name_[3] == 'I')
2995       return 0;
2996     return this->targ_->first_plt_entry_offset();
2997   }
2998
2999   // Return the size of each PLT entry.
3000   unsigned int
3001   plt_entry_size() const
3002   {
3003     return this->targ_->plt_entry_size();
3004   }
3005
3006   // Write out the PLT data.
3007   void
3008   do_write(Output_file*);
3009
3010   // The reloc section.
3011   Reloc_section* rel_;
3012   // Allows access to .glink for do_write.
3013   Target_powerpc<size, big_endian>* targ_;
3014   // What to report in map file.
3015   const char *name_;
3016 };
3017
3018 // Add an entry to the PLT.
3019
3020 template<int size, bool big_endian>
3021 void
3022 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
3023 {
3024   if (!gsym->has_plt_offset())
3025     {
3026       section_size_type off = this->current_data_size();
3027       if (off == 0)
3028         off += this->first_plt_entry_offset();
3029       gsym->set_plt_offset(off);
3030       gsym->set_needs_dynsym_entry();
3031       unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
3032       this->rel_->add_global(gsym, dynrel, this, off, 0);
3033       off += this->plt_entry_size();
3034       this->set_current_data_size(off);
3035     }
3036 }
3037
3038 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
3039
3040 template<int size, bool big_endian>
3041 void
3042 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
3043 {
3044   if (!gsym->has_plt_offset())
3045     {
3046       section_size_type off = this->current_data_size();
3047       gsym->set_plt_offset(off);
3048       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3049       if (size == 64 && this->targ_->abiversion() < 2)
3050         dynrel = elfcpp::R_PPC64_JMP_IREL;
3051       this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
3052       off += this->plt_entry_size();
3053       this->set_current_data_size(off);
3054     }
3055 }
3056
3057 // Add an entry for a local ifunc symbol to the IPLT.
3058
3059 template<int size, bool big_endian>
3060 void
3061 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
3062     Sized_relobj_file<size, big_endian>* relobj,
3063     unsigned int local_sym_index)
3064 {
3065   if (!relobj->local_has_plt_offset(local_sym_index))
3066     {
3067       section_size_type off = this->current_data_size();
3068       relobj->set_local_plt_offset(local_sym_index, off);
3069       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
3070       if (size == 64 && this->targ_->abiversion() < 2)
3071         dynrel = elfcpp::R_PPC64_JMP_IREL;
3072       this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
3073                                               this, off, 0);
3074       off += this->plt_entry_size();
3075       this->set_current_data_size(off);
3076     }
3077 }
3078
3079 static const uint32_t add_0_11_11       = 0x7c0b5a14;
3080 static const uint32_t add_2_2_11        = 0x7c425a14;
3081 static const uint32_t add_3_3_2         = 0x7c631214;
3082 static const uint32_t add_3_3_13        = 0x7c636a14;
3083 static const uint32_t add_11_0_11       = 0x7d605a14;
3084 static const uint32_t add_11_2_11       = 0x7d625a14;
3085 static const uint32_t add_11_11_2       = 0x7d6b1214;
3086 static const uint32_t addi_0_12         = 0x380c0000;
3087 static const uint32_t addi_2_2          = 0x38420000;
3088 static const uint32_t addi_3_3          = 0x38630000;
3089 static const uint32_t addi_11_11        = 0x396b0000;
3090 static const uint32_t addi_12_12        = 0x398c0000;
3091 static const uint32_t addis_0_2         = 0x3c020000;
3092 static const uint32_t addis_0_13        = 0x3c0d0000;
3093 static const uint32_t addis_3_2         = 0x3c620000;
3094 static const uint32_t addis_3_13        = 0x3c6d0000;
3095 static const uint32_t addis_11_2        = 0x3d620000;
3096 static const uint32_t addis_11_11       = 0x3d6b0000;
3097 static const uint32_t addis_11_30       = 0x3d7e0000;
3098 static const uint32_t addis_12_2        = 0x3d820000;
3099 static const uint32_t addis_12_12       = 0x3d8c0000;
3100 static const uint32_t b                 = 0x48000000;
3101 static const uint32_t bcl_20_31         = 0x429f0005;
3102 static const uint32_t bctr              = 0x4e800420;
3103 static const uint32_t blr               = 0x4e800020;
3104 static const uint32_t bnectr_p4         = 0x4ce20420;
3105 static const uint32_t cmpldi_2_0        = 0x28220000;
3106 static const uint32_t cror_15_15_15     = 0x4def7b82;
3107 static const uint32_t cror_31_31_31     = 0x4ffffb82;
3108 static const uint32_t ld_0_1            = 0xe8010000;
3109 static const uint32_t ld_0_12           = 0xe80c0000;
3110 static const uint32_t ld_2_1            = 0xe8410000;
3111 static const uint32_t ld_2_2            = 0xe8420000;
3112 static const uint32_t ld_2_11           = 0xe84b0000;
3113 static const uint32_t ld_11_2           = 0xe9620000;
3114 static const uint32_t ld_11_11          = 0xe96b0000;
3115 static const uint32_t ld_12_2           = 0xe9820000;
3116 static const uint32_t ld_12_11          = 0xe98b0000;
3117 static const uint32_t ld_12_12          = 0xe98c0000;
3118 static const uint32_t lfd_0_1           = 0xc8010000;
3119 static const uint32_t li_0_0            = 0x38000000;
3120 static const uint32_t li_12_0           = 0x39800000;
3121 static const uint32_t lis_0_0           = 0x3c000000;
3122 static const uint32_t lis_11            = 0x3d600000;
3123 static const uint32_t lis_12            = 0x3d800000;
3124 static const uint32_t lvx_0_12_0        = 0x7c0c00ce;
3125 static const uint32_t lwz_0_12          = 0x800c0000;
3126 static const uint32_t lwz_11_11         = 0x816b0000;
3127 static const uint32_t lwz_11_30         = 0x817e0000;
3128 static const uint32_t lwz_12_12         = 0x818c0000;
3129 static const uint32_t lwzu_0_12         = 0x840c0000;
3130 static const uint32_t mflr_0            = 0x7c0802a6;
3131 static const uint32_t mflr_11           = 0x7d6802a6;
3132 static const uint32_t mflr_12           = 0x7d8802a6;
3133 static const uint32_t mtctr_0           = 0x7c0903a6;
3134 static const uint32_t mtctr_11          = 0x7d6903a6;
3135 static const uint32_t mtctr_12          = 0x7d8903a6;
3136 static const uint32_t mtlr_0            = 0x7c0803a6;
3137 static const uint32_t mtlr_12           = 0x7d8803a6;
3138 static const uint32_t nop               = 0x60000000;
3139 static const uint32_t ori_0_0_0         = 0x60000000;
3140 static const uint32_t srdi_0_0_2        = 0x7800f082;
3141 static const uint32_t std_0_1           = 0xf8010000;
3142 static const uint32_t std_0_12          = 0xf80c0000;
3143 static const uint32_t std_2_1           = 0xf8410000;
3144 static const uint32_t stfd_0_1          = 0xd8010000;
3145 static const uint32_t stvx_0_12_0       = 0x7c0c01ce;
3146 static const uint32_t sub_11_11_12      = 0x7d6c5850;
3147 static const uint32_t sub_12_12_11      = 0x7d8b6050;
3148 static const uint32_t xor_2_12_12       = 0x7d826278;
3149 static const uint32_t xor_11_12_12      = 0x7d8b6278;
3150
3151 // Write out the PLT.
3152
3153 template<int size, bool big_endian>
3154 void
3155 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
3156 {
3157   if (size == 32 && this->name_[3] != 'I')
3158     {
3159       const section_size_type offset = this->offset();
3160       const section_size_type oview_size
3161         = convert_to_section_size_type(this->data_size());
3162       unsigned char* const oview = of->get_output_view(offset, oview_size);
3163       unsigned char* pov = oview;
3164       unsigned char* endpov = oview + oview_size;
3165
3166       // The address of the .glink branch table
3167       const Output_data_glink<size, big_endian>* glink
3168         = this->targ_->glink_section();
3169       elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
3170
3171       while (pov < endpov)
3172         {
3173           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
3174           pov += 4;
3175           branch_tab += 4;
3176         }
3177
3178       of->write_output_view(offset, oview_size, oview);
3179     }
3180 }
3181
3182 // Create the PLT section.
3183
3184 template<int size, bool big_endian>
3185 void
3186 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
3187                                                    Layout* layout)
3188 {
3189   if (this->plt_ == NULL)
3190     {
3191       if (this->got_ == NULL)
3192         this->got_section(symtab, layout);
3193
3194       if (this->glink_ == NULL)
3195         make_glink_section(layout);
3196
3197       // Ensure that .rela.dyn always appears before .rela.plt  This is
3198       // necessary due to how, on PowerPC and some other targets, .rela.dyn
3199       // needs to include .rela.plt in its range.
3200       this->rela_dyn_section(layout);
3201
3202       Reloc_section* plt_rel = new Reloc_section(false);
3203       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
3204                                       elfcpp::SHF_ALLOC, plt_rel,
3205                                       ORDER_DYNAMIC_PLT_RELOCS, false);
3206       this->plt_
3207         = new Output_data_plt_powerpc<size, big_endian>(this, plt_rel,
3208                                                         "** PLT");
3209       layout->add_output_section_data(".plt",
3210                                       (size == 32
3211                                        ? elfcpp::SHT_PROGBITS
3212                                        : elfcpp::SHT_NOBITS),
3213                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3214                                       this->plt_,
3215                                       (size == 32
3216                                        ? ORDER_SMALL_DATA
3217                                        : ORDER_SMALL_BSS),
3218                                       false);
3219     }
3220 }
3221
3222 // Create the IPLT section.
3223
3224 template<int size, bool big_endian>
3225 void
3226 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
3227                                                     Layout* layout)
3228 {
3229   if (this->iplt_ == NULL)
3230     {
3231       this->make_plt_section(symtab, layout);
3232
3233       Reloc_section* iplt_rel = new Reloc_section(false);
3234       this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
3235       this->iplt_
3236         = new Output_data_plt_powerpc<size, big_endian>(this, iplt_rel,
3237                                                         "** IPLT");
3238       this->plt_->output_section()->add_output_section_data(this->iplt_);
3239     }
3240 }
3241
3242 // A section for huge long branch addresses, similar to plt section.
3243
3244 template<int size, bool big_endian>
3245 class Output_data_brlt_powerpc : public Output_section_data_build
3246 {
3247  public:
3248   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3249   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
3250                             size, big_endian> Reloc_section;
3251
3252   Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
3253                            Reloc_section* brlt_rel)
3254     : Output_section_data_build(size == 32 ? 4 : 8),
3255       rel_(brlt_rel),
3256       targ_(targ)
3257   { }
3258
3259   void
3260   reset_brlt_sizes()
3261   {
3262     this->reset_data_size();
3263     this->rel_->reset_data_size();
3264   }
3265
3266   void
3267   finalize_brlt_sizes()
3268   {
3269     this->finalize_data_size();
3270     this->rel_->finalize_data_size();
3271   }
3272
3273   // Add a reloc for an entry in the BRLT.
3274   void
3275   add_reloc(Address to, unsigned int off)
3276   { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
3277
3278   // Update section and reloc section size.
3279   void
3280   set_current_size(unsigned int num_branches)
3281   {
3282     this->reset_address_and_file_offset();
3283     this->set_current_data_size(num_branches * 16);
3284     this->finalize_data_size();
3285     Output_section* os = this->output_section();
3286     os->set_section_offsets_need_adjustment();
3287     if (this->rel_ != NULL)
3288       {
3289         unsigned int reloc_size
3290           = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
3291         this->rel_->reset_address_and_file_offset();
3292         this->rel_->set_current_data_size(num_branches * reloc_size);
3293         this->rel_->finalize_data_size();
3294         Output_section* os = this->rel_->output_section();
3295         os->set_section_offsets_need_adjustment();
3296       }
3297   }
3298
3299  protected:
3300   void
3301   do_adjust_output_section(Output_section* os)
3302   {
3303     os->set_entsize(0);
3304   }
3305
3306   // Write to a map file.
3307   void
3308   do_print_to_mapfile(Mapfile* mapfile) const
3309   { mapfile->print_output_data(this, "** BRLT"); }
3310
3311  private:
3312   // Write out the BRLT data.
3313   void
3314   do_write(Output_file*);
3315
3316   // The reloc section.
3317   Reloc_section* rel_;
3318   Target_powerpc<size, big_endian>* targ_;
3319 };
3320
3321 // Make the branch lookup table section.
3322
3323 template<int size, bool big_endian>
3324 void
3325 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
3326 {
3327   if (size == 64 && this->brlt_section_ == NULL)
3328     {
3329       Reloc_section* brlt_rel = NULL;
3330       bool is_pic = parameters->options().output_is_position_independent();
3331       if (is_pic)
3332         {
3333           // When PIC we can't fill in .branch_lt (like .plt it can be
3334           // a bss style section) but must initialise at runtime via
3335           // dynamic relocats.
3336           this->rela_dyn_section(layout);
3337           brlt_rel = new Reloc_section(false);
3338           this->rela_dyn_->output_section()->add_output_section_data(brlt_rel);
3339         }
3340       this->brlt_section_
3341         = new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
3342       if (this->plt_ && is_pic)
3343         this->plt_->output_section()
3344           ->add_output_section_data(this->brlt_section_);
3345       else
3346         layout->add_output_section_data(".branch_lt",
3347                                         (is_pic ? elfcpp::SHT_NOBITS
3348                                          : elfcpp::SHT_PROGBITS),
3349                                         elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3350                                         this->brlt_section_,
3351                                         (is_pic ? ORDER_SMALL_BSS
3352                                          : ORDER_SMALL_DATA),
3353                                         false);
3354     }
3355 }
3356
3357 // Write out .branch_lt when non-PIC.
3358
3359 template<int size, bool big_endian>
3360 void
3361 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
3362 {
3363   if (size == 64 && !parameters->options().output_is_position_independent())
3364     {
3365       const section_size_type offset = this->offset();
3366       const section_size_type oview_size
3367         = convert_to_section_size_type(this->data_size());
3368       unsigned char* const oview = of->get_output_view(offset, oview_size);
3369
3370       this->targ_->write_branch_lookup_table(oview);
3371       of->write_output_view(offset, oview_size, oview);
3372     }
3373 }
3374
3375 static inline uint32_t
3376 l(uint32_t a)
3377 {
3378   return a & 0xffff;
3379 }
3380
3381 static inline uint32_t
3382 hi(uint32_t a)
3383 {
3384   return l(a >> 16);
3385 }
3386
3387 static inline uint32_t
3388 ha(uint32_t a)
3389 {
3390   return hi(a + 0x8000);
3391 }
3392
3393 template<int size>
3394 struct Eh_cie
3395 {
3396   static const unsigned char eh_frame_cie[12];
3397 };
3398
3399 template<int size>
3400 const unsigned char Eh_cie<size>::eh_frame_cie[] =
3401 {
3402   1,                                    // CIE version.
3403   'z', 'R', 0,                          // Augmentation string.
3404   4,                                    // Code alignment.
3405   0x80 - size / 8 ,                     // Data alignment.
3406   65,                                   // RA reg.
3407   1,                                    // Augmentation size.
3408   (elfcpp::DW_EH_PE_pcrel
3409    | elfcpp::DW_EH_PE_sdata4),          // FDE encoding.
3410   elfcpp::DW_CFA_def_cfa, 1, 0          // def_cfa: r1 offset 0.
3411 };
3412
3413 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
3414 static const unsigned char glink_eh_frame_fde_64v1[] =
3415 {
3416   0, 0, 0, 0,                           // Replaced with offset to .glink.
3417   0, 0, 0, 0,                           // Replaced with size of .glink.
3418   0,                                    // Augmentation size.
3419   elfcpp::DW_CFA_advance_loc + 1,
3420   elfcpp::DW_CFA_register, 65, 12,
3421   elfcpp::DW_CFA_advance_loc + 4,
3422   elfcpp::DW_CFA_restore_extended, 65
3423 };
3424
3425 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
3426 static const unsigned char glink_eh_frame_fde_64v2[] =
3427 {
3428   0, 0, 0, 0,                           // Replaced with offset to .glink.
3429   0, 0, 0, 0,                           // Replaced with size of .glink.
3430   0,                                    // Augmentation size.
3431   elfcpp::DW_CFA_advance_loc + 1,
3432   elfcpp::DW_CFA_register, 65, 0,
3433   elfcpp::DW_CFA_advance_loc + 4,
3434   elfcpp::DW_CFA_restore_extended, 65
3435 };
3436
3437 // Describe __glink_PLTresolve use of LR, 32-bit version.
3438 static const unsigned char glink_eh_frame_fde_32[] =
3439 {
3440   0, 0, 0, 0,                           // Replaced with offset to .glink.
3441   0, 0, 0, 0,                           // Replaced with size of .glink.
3442   0,                                    // Augmentation size.
3443   elfcpp::DW_CFA_advance_loc + 2,
3444   elfcpp::DW_CFA_register, 65, 0,
3445   elfcpp::DW_CFA_advance_loc + 4,
3446   elfcpp::DW_CFA_restore_extended, 65
3447 };
3448
3449 static const unsigned char default_fde[] =
3450 {
3451   0, 0, 0, 0,                           // Replaced with offset to stubs.
3452   0, 0, 0, 0,                           // Replaced with size of stubs.
3453   0,                                    // Augmentation size.
3454   elfcpp::DW_CFA_nop,                   // Pad.
3455   elfcpp::DW_CFA_nop,
3456   elfcpp::DW_CFA_nop
3457 };
3458
3459 template<bool big_endian>
3460 static inline void
3461 write_insn(unsigned char* p, uint32_t v)
3462 {
3463   elfcpp::Swap<32, big_endian>::writeval(p, v);
3464 }
3465
3466 // Stub_table holds information about plt and long branch stubs.
3467 // Stubs are built in an area following some input section determined
3468 // by group_sections().  This input section is converted to a relaxed
3469 // input section allowing it to be resized to accommodate the stubs
3470
3471 template<int size, bool big_endian>
3472 class Stub_table : public Output_relaxed_input_section
3473 {
3474  public:
3475   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3476   static const Address invalid_address = static_cast<Address>(0) - 1;
3477
3478   Stub_table(Target_powerpc<size, big_endian>* targ)
3479     : Output_relaxed_input_section(NULL, 0, 0),
3480       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
3481       orig_data_size_(0), plt_size_(0), last_plt_size_(0),
3482       branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
3483   { }
3484
3485   // Delayed Output_relaxed_input_section init.
3486   void
3487   init(const Output_section::Input_section*, Output_section*);
3488
3489   // Add a plt call stub.
3490   void
3491   add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3492                      const Symbol*,
3493                      unsigned int,
3494                      Address);
3495
3496   void
3497   add_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3498                      unsigned int,
3499                      unsigned int,
3500                      Address);
3501
3502   // Find a given plt call stub.
3503   Address
3504   find_plt_call_entry(const Symbol*) const;
3505
3506   Address
3507   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3508                       unsigned int) const;
3509
3510   Address
3511   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3512                       const Symbol*,
3513                       unsigned int,
3514                       Address) const;
3515
3516   Address
3517   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
3518                       unsigned int,
3519                       unsigned int,
3520                       Address) const;
3521
3522   // Add a long branch stub.
3523   void
3524   add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
3525
3526   Address
3527   find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
3528                          Address) const;
3529
3530   void
3531   clear_stubs()
3532   {
3533     this->plt_call_stubs_.clear();
3534     this->plt_size_ = 0;
3535     this->long_branch_stubs_.clear();
3536     this->branch_size_ = 0;
3537   }
3538
3539   Address
3540   set_address_and_size(const Output_section* os, Address off)
3541   {
3542     Address start_off = off;
3543     off += this->orig_data_size_;
3544     Address my_size = this->plt_size_ + this->branch_size_;
3545     if (my_size != 0)
3546       off = align_address(off, this->stub_align());
3547     // Include original section size and alignment padding in size
3548     my_size += off - start_off;
3549     this->reset_address_and_file_offset();
3550     this->set_current_data_size(my_size);
3551     this->set_address_and_file_offset(os->address() + start_off,
3552                                       os->offset() + start_off);
3553     return my_size;
3554   }
3555
3556   Address
3557   stub_address() const
3558   {
3559     return align_address(this->address() + this->orig_data_size_,
3560                          this->stub_align());
3561   }
3562
3563   Address
3564   stub_offset() const
3565   {
3566     return align_address(this->offset() + this->orig_data_size_,
3567                          this->stub_align());
3568   }
3569
3570   section_size_type
3571   plt_size() const
3572   { return this->plt_size_; }
3573
3574   bool
3575   size_update()
3576   {
3577     Output_section* os = this->output_section();
3578     if (os->addralign() < this->stub_align())
3579       {
3580         os->set_addralign(this->stub_align());
3581         // FIXME: get rid of the insane checkpointing.
3582         // We can't increase alignment of the input section to which
3583         // stubs are attached;  The input section may be .init which
3584         // is pasted together with other .init sections to form a
3585         // function.  Aligning might insert zero padding resulting in
3586         // sigill.  However we do need to increase alignment of the
3587         // output section so that the align_address() on offset in
3588         // set_address_and_size() adds the same padding as the
3589         // align_address() on address in stub_address().
3590         // What's more, we need this alignment for the layout done in
3591         // relaxation_loop_body() so that the output section starts at
3592         // a suitably aligned address.
3593         os->checkpoint_set_addralign(this->stub_align());
3594       }
3595     if (this->last_plt_size_ != this->plt_size_
3596         || this->last_branch_size_ != this->branch_size_)
3597       {
3598         this->last_plt_size_ = this->plt_size_;
3599         this->last_branch_size_ = this->branch_size_;
3600         return true;
3601       }
3602     return false;
3603   }
3604
3605   // Add .eh_frame info for this stub section.  Unlike other linker
3606   // generated .eh_frame this is added late in the link, because we
3607   // only want the .eh_frame info if this particular stub section is
3608   // non-empty.
3609   void
3610   add_eh_frame(Layout* layout)
3611   {
3612     if (!this->eh_frame_added_)
3613       {
3614         if (!parameters->options().ld_generated_unwind_info())
3615           return;
3616
3617         // Since we add stub .eh_frame info late, it must be placed
3618         // after all other linker generated .eh_frame info so that
3619         // merge mapping need not be updated for input sections.
3620         // There is no provision to use a different CIE to that used
3621         // by .glink.
3622         if (!this->targ_->has_glink())
3623           return;
3624
3625         layout->add_eh_frame_for_plt(this,
3626                                      Eh_cie<size>::eh_frame_cie,
3627                                      sizeof (Eh_cie<size>::eh_frame_cie),
3628                                      default_fde,
3629                                      sizeof (default_fde));
3630         this->eh_frame_added_ = true;
3631       }
3632   }
3633
3634   Target_powerpc<size, big_endian>*
3635   targ() const
3636   { return targ_; }
3637
3638  private:
3639   class Plt_stub_ent;
3640   class Plt_stub_ent_hash;
3641   typedef Unordered_map<Plt_stub_ent, unsigned int,
3642                         Plt_stub_ent_hash> Plt_stub_entries;
3643
3644   // Alignment of stub section.
3645   unsigned int
3646   stub_align() const
3647   {
3648     if (size == 32)
3649       return 16;
3650     unsigned int min_align = 32;
3651     unsigned int user_align = 1 << parameters->options().plt_align();
3652     return std::max(user_align, min_align);
3653   }
3654
3655   // Return the plt offset for the given call stub.
3656   Address
3657   plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
3658   {
3659     const Symbol* gsym = p->first.sym_;
3660     if (gsym != NULL)
3661       {
3662         *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
3663                     && gsym->can_use_relative_reloc(false));
3664         return gsym->plt_offset();
3665       }
3666     else
3667       {
3668         *is_iplt = true;
3669         const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
3670         unsigned int local_sym_index = p->first.locsym_;
3671         return relobj->local_plt_offset(local_sym_index);
3672       }
3673   }
3674
3675   // Size of a given plt call stub.
3676   unsigned int
3677   plt_call_size(typename Plt_stub_entries::const_iterator p) const
3678   {
3679     if (size == 32)
3680       return 16;
3681
3682     bool is_iplt;
3683     Address plt_addr = this->plt_off(p, &is_iplt);
3684     if (is_iplt)
3685       plt_addr += this->targ_->iplt_section()->address();
3686     else
3687       plt_addr += this->targ_->plt_section()->address();
3688     Address got_addr = this->targ_->got_section()->output_section()->address();
3689     const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3690       <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
3691     got_addr += ppcobj->toc_base_offset();
3692     Address off = plt_addr - got_addr;
3693     unsigned int bytes = 4 * 4 + 4 * (ha(off) != 0);
3694     if (this->targ_->abiversion() < 2)
3695       {
3696         bool static_chain = parameters->options().plt_static_chain();
3697         bool thread_safe = this->targ_->plt_thread_safe();
3698         bytes += (4
3699                   + 4 * static_chain
3700                   + 8 * thread_safe
3701                   + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
3702       }
3703     unsigned int align = 1 << parameters->options().plt_align();
3704     if (align > 1)
3705       bytes = (bytes + align - 1) & -align;
3706     return bytes;
3707   }
3708
3709   // Return long branch stub size.
3710   unsigned int
3711   branch_stub_size(Address to)
3712   {
3713     Address loc
3714       = this->stub_address() + this->last_plt_size_ + this->branch_size_;
3715     if (to - loc + (1 << 25) < 2 << 25)
3716       return 4;
3717     if (size == 64 || !parameters->options().output_is_position_independent())
3718       return 16;
3719     return 32;
3720   }
3721
3722   // Write out stubs.
3723   void
3724   do_write(Output_file*);
3725
3726   // Plt call stub keys.
3727   class Plt_stub_ent
3728   {
3729   public:
3730     Plt_stub_ent(const Symbol* sym)
3731       : sym_(sym), object_(0), addend_(0), locsym_(0)
3732     { }
3733
3734     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3735                  unsigned int locsym_index)
3736       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3737     { }
3738
3739     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3740                  const Symbol* sym,
3741                  unsigned int r_type,
3742                  Address addend)
3743       : sym_(sym), object_(0), addend_(0), locsym_(0)
3744     {
3745       if (size != 32)
3746         this->addend_ = addend;
3747       else if (parameters->options().output_is_position_independent()
3748                && r_type == elfcpp::R_PPC_PLTREL24)
3749         {
3750           this->addend_ = addend;
3751           if (this->addend_ >= 32768)
3752             this->object_ = object;
3753         }
3754     }
3755
3756     Plt_stub_ent(const Sized_relobj_file<size, big_endian>* object,
3757                  unsigned int locsym_index,
3758                  unsigned int r_type,
3759                  Address addend)
3760       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
3761     {
3762       if (size != 32)
3763         this->addend_ = addend;
3764       else if (parameters->options().output_is_position_independent()
3765                && r_type == elfcpp::R_PPC_PLTREL24)
3766         this->addend_ = addend;
3767     }
3768
3769     bool operator==(const Plt_stub_ent& that) const
3770     {
3771       return (this->sym_ == that.sym_
3772               && this->object_ == that.object_
3773               && this->addend_ == that.addend_
3774               && this->locsym_ == that.locsym_);
3775     }
3776
3777     const Symbol* sym_;
3778     const Sized_relobj_file<size, big_endian>* object_;
3779     typename elfcpp::Elf_types<size>::Elf_Addr addend_;
3780     unsigned int locsym_;
3781   };
3782
3783   class Plt_stub_ent_hash
3784   {
3785   public:
3786     size_t operator()(const Plt_stub_ent& ent) const
3787     {
3788       return (reinterpret_cast<uintptr_t>(ent.sym_)
3789               ^ reinterpret_cast<uintptr_t>(ent.object_)
3790               ^ ent.addend_
3791               ^ ent.locsym_);
3792     }
3793   };
3794
3795   // Long branch stub keys.
3796   class Branch_stub_ent
3797   {
3798   public:
3799     Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
3800       : dest_(to), toc_base_off_(0)
3801     {
3802       if (size == 64)
3803         toc_base_off_ = obj->toc_base_offset();
3804     }
3805
3806     bool operator==(const Branch_stub_ent& that) const
3807     {
3808       return (this->dest_ == that.dest_
3809               && (size == 32
3810                   || this->toc_base_off_ == that.toc_base_off_));
3811     }
3812
3813     Address dest_;
3814     unsigned int toc_base_off_;
3815   };
3816
3817   class Branch_stub_ent_hash
3818   {
3819   public:
3820     size_t operator()(const Branch_stub_ent& ent) const
3821     { return ent.dest_ ^ ent.toc_base_off_; }
3822   };
3823
3824   // In a sane world this would be a global.
3825   Target_powerpc<size, big_endian>* targ_;
3826   // Map sym/object/addend to stub offset.
3827   Plt_stub_entries plt_call_stubs_;
3828   // Map destination address to stub offset.
3829   typedef Unordered_map<Branch_stub_ent, unsigned int,
3830                         Branch_stub_ent_hash> Branch_stub_entries;
3831   Branch_stub_entries long_branch_stubs_;
3832   // size of input section
3833   section_size_type orig_data_size_;
3834   // size of stubs
3835   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
3836   // Whether .eh_frame info has been created for this stub section.
3837   bool eh_frame_added_;
3838 };
3839
3840 // Make a new stub table, and record.
3841
3842 template<int size, bool big_endian>
3843 Stub_table<size, big_endian>*
3844 Target_powerpc<size, big_endian>::new_stub_table()
3845 {
3846   Stub_table<size, big_endian>* stub_table
3847     = new Stub_table<size, big_endian>(this);
3848   this->stub_tables_.push_back(stub_table);
3849   return stub_table;
3850 }
3851
3852 // Delayed stub table initialisation, because we create the stub table
3853 // before we know to which section it will be attached.
3854
3855 template<int size, bool big_endian>
3856 void
3857 Stub_table<size, big_endian>::init(
3858     const Output_section::Input_section* owner,
3859     Output_section* output_section)
3860 {
3861   this->set_relobj(owner->relobj());
3862   this->set_shndx(owner->shndx());
3863   this->set_addralign(this->relobj()->section_addralign(this->shndx()));
3864   this->set_output_section(output_section);
3865   this->orig_data_size_ = owner->current_data_size();
3866
3867   std::vector<Output_relaxed_input_section*> new_relaxed;
3868   new_relaxed.push_back(this);
3869   output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
3870 }
3871
3872 // Add a plt call stub, if we do not already have one for this
3873 // sym/object/addend combo.
3874
3875 template<int size, bool big_endian>
3876 void
3877 Stub_table<size, big_endian>::add_plt_call_entry(
3878     const Sized_relobj_file<size, big_endian>* object,
3879     const Symbol* gsym,
3880     unsigned int r_type,
3881     Address addend)
3882 {
3883   Plt_stub_ent ent(object, gsym, r_type, addend);
3884   unsigned int off = this->plt_size_;
3885   std::pair<typename Plt_stub_entries::iterator, bool> p
3886     = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3887   if (p.second)
3888     this->plt_size_ = off + this->plt_call_size(p.first);
3889 }
3890
3891 template<int size, bool big_endian>
3892 void
3893 Stub_table<size, big_endian>::add_plt_call_entry(
3894     const Sized_relobj_file<size, big_endian>* object,
3895     unsigned int locsym_index,
3896     unsigned int r_type,
3897     Address addend)
3898 {
3899   Plt_stub_ent ent(object, locsym_index, r_type, addend);
3900   unsigned int off = this->plt_size_;
3901   std::pair<typename Plt_stub_entries::iterator, bool> p
3902     = this->plt_call_stubs_.insert(std::make_pair(ent, off));
3903   if (p.second)
3904     this->plt_size_ = off + this->plt_call_size(p.first);
3905 }
3906
3907 // Find a plt call stub.
3908
3909 template<int size, bool big_endian>
3910 typename Stub_table<size, big_endian>::Address
3911 Stub_table<size, big_endian>::find_plt_call_entry(
3912     const Sized_relobj_file<size, big_endian>* object,
3913     const Symbol* gsym,
3914     unsigned int r_type,
3915     Address addend) const
3916 {
3917   Plt_stub_ent ent(object, gsym, r_type, addend);
3918   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3919   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3920 }
3921
3922 template<int size, bool big_endian>
3923 typename Stub_table<size, big_endian>::Address
3924 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
3925 {
3926   Plt_stub_ent ent(gsym);
3927   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3928   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3929 }
3930
3931 template<int size, bool big_endian>
3932 typename Stub_table<size, big_endian>::Address
3933 Stub_table<size, big_endian>::find_plt_call_entry(
3934     const Sized_relobj_file<size, big_endian>* object,
3935     unsigned int locsym_index,
3936     unsigned int r_type,
3937     Address addend) const
3938 {
3939   Plt_stub_ent ent(object, locsym_index, r_type, addend);
3940   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3941   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3942 }
3943
3944 template<int size, bool big_endian>
3945 typename Stub_table<size, big_endian>::Address
3946 Stub_table<size, big_endian>::find_plt_call_entry(
3947     const Sized_relobj_file<size, big_endian>* object,
3948     unsigned int locsym_index) const
3949 {
3950   Plt_stub_ent ent(object, locsym_index);
3951   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(ent);
3952   return p == this->plt_call_stubs_.end() ? invalid_address : p->second;
3953 }
3954
3955 // Add a long branch stub if we don't already have one to given
3956 // destination.
3957
3958 template<int size, bool big_endian>
3959 void
3960 Stub_table<size, big_endian>::add_long_branch_entry(
3961     const Powerpc_relobj<size, big_endian>* object,
3962     Address to)
3963 {
3964   Branch_stub_ent ent(object, to);
3965   Address off = this->branch_size_;
3966   if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
3967     {
3968       unsigned int stub_size = this->branch_stub_size(to);
3969       this->branch_size_ = off + stub_size;
3970       if (size == 64 && stub_size != 4)
3971         this->targ_->add_branch_lookup_table(to);
3972     }
3973 }
3974
3975 // Find long branch stub.
3976
3977 template<int size, bool big_endian>
3978 typename Stub_table<size, big_endian>::Address
3979 Stub_table<size, big_endian>::find_long_branch_entry(
3980     const Powerpc_relobj<size, big_endian>* object,
3981     Address to) const
3982 {
3983   Branch_stub_ent ent(object, to);
3984   typename Branch_stub_entries::const_iterator p
3985     = this->long_branch_stubs_.find(ent);
3986   return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
3987 }
3988
3989 // A class to handle .glink.
3990
3991 template<int size, bool big_endian>
3992 class Output_data_glink : public Output_section_data
3993 {
3994  public:
3995   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
3996   static const Address invalid_address = static_cast<Address>(0) - 1;
3997   static const int pltresolve_size = 16*4;
3998
3999   Output_data_glink(Target_powerpc<size, big_endian>* targ)
4000     : Output_section_data(16), targ_(targ), global_entry_stubs_(),
4001       end_branch_table_(), ge_size_(0)
4002   { }
4003
4004   void
4005   add_eh_frame(Layout* layout);
4006
4007   void
4008   add_global_entry(const Symbol*);
4009
4010   Address
4011   find_global_entry(const Symbol*) const;
4012
4013   Address
4014   global_entry_address() const
4015   {
4016     gold_assert(this->is_data_size_valid());
4017     unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4018     return this->address() + global_entry_off;
4019   }
4020
4021  protected:
4022   // Write to a map file.
4023   void
4024   do_print_to_mapfile(Mapfile* mapfile) const
4025   { mapfile->print_output_data(this, _("** glink")); }
4026
4027  private:
4028   void
4029   set_final_data_size();
4030
4031   // Write out .glink
4032   void
4033   do_write(Output_file*);
4034
4035   // Allows access to .got and .plt for do_write.
4036   Target_powerpc<size, big_endian>* targ_;
4037
4038   // Map sym to stub offset.
4039   typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
4040   Global_entry_stub_entries global_entry_stubs_;
4041
4042   unsigned int end_branch_table_, ge_size_;
4043 };
4044
4045 template<int size, bool big_endian>
4046 void
4047 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
4048 {
4049   if (!parameters->options().ld_generated_unwind_info())
4050     return;
4051
4052   if (size == 64)
4053     {
4054       if (this->targ_->abiversion() < 2)
4055         layout->add_eh_frame_for_plt(this,
4056                                      Eh_cie<64>::eh_frame_cie,
4057                                      sizeof (Eh_cie<64>::eh_frame_cie),
4058                                      glink_eh_frame_fde_64v1,
4059                                      sizeof (glink_eh_frame_fde_64v1));
4060       else
4061         layout->add_eh_frame_for_plt(this,
4062                                      Eh_cie<64>::eh_frame_cie,
4063                                      sizeof (Eh_cie<64>::eh_frame_cie),
4064                                      glink_eh_frame_fde_64v2,
4065                                      sizeof (glink_eh_frame_fde_64v2));
4066     }
4067   else
4068     {
4069       // 32-bit .glink can use the default since the CIE return
4070       // address reg, LR, is valid.
4071       layout->add_eh_frame_for_plt(this,
4072                                    Eh_cie<32>::eh_frame_cie,
4073                                    sizeof (Eh_cie<32>::eh_frame_cie),
4074                                    default_fde,
4075                                    sizeof (default_fde));
4076       // Except where LR is used in a PIC __glink_PLTresolve.
4077       if (parameters->options().output_is_position_independent())
4078         layout->add_eh_frame_for_plt(this,
4079                                      Eh_cie<32>::eh_frame_cie,
4080                                      sizeof (Eh_cie<32>::eh_frame_cie),
4081                                      glink_eh_frame_fde_32,
4082                                      sizeof (glink_eh_frame_fde_32));
4083     }
4084 }
4085
4086 template<int size, bool big_endian>
4087 void
4088 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
4089 {
4090   std::pair<typename Global_entry_stub_entries::iterator, bool> p
4091     = this->global_entry_stubs_.insert(std::make_pair(gsym, this->ge_size_));
4092   if (p.second)
4093     this->ge_size_ += 16;
4094 }
4095
4096 template<int size, bool big_endian>
4097 typename Output_data_glink<size, big_endian>::Address
4098 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
4099 {
4100   typename Global_entry_stub_entries::const_iterator p
4101     = this->global_entry_stubs_.find(gsym);
4102   return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
4103 }
4104
4105 template<int size, bool big_endian>
4106 void
4107 Output_data_glink<size, big_endian>::set_final_data_size()
4108 {
4109   unsigned int count = this->targ_->plt_entry_count();
4110   section_size_type total = 0;
4111
4112   if (count != 0)
4113     {
4114       if (size == 32)
4115         {
4116           // space for branch table
4117           total += 4 * (count - 1);
4118
4119           total += -total & 15;
4120           total += this->pltresolve_size;
4121         }
4122       else
4123         {
4124           total += this->pltresolve_size;
4125
4126           // space for branch table
4127           total += 4 * count;
4128           if (this->targ_->abiversion() < 2)
4129             {
4130               total += 4 * count;
4131               if (count > 0x8000)
4132                 total += 4 * (count - 0x8000);
4133             }
4134         }
4135     }
4136   this->end_branch_table_ = total;
4137   total = (total + 15) & -16;
4138   total += this->ge_size_;
4139
4140   this->set_data_size(total);
4141 }
4142
4143 // Write out plt and long branch stub code.
4144
4145 template<int size, bool big_endian>
4146 void
4147 Stub_table<size, big_endian>::do_write(Output_file* of)
4148 {
4149   if (this->plt_call_stubs_.empty()
4150       && this->long_branch_stubs_.empty())
4151     return;
4152
4153   const section_size_type start_off = this->offset();
4154   const section_size_type off = this->stub_offset();
4155   const section_size_type oview_size =
4156     convert_to_section_size_type(this->data_size() - (off - start_off));
4157   unsigned char* const oview = of->get_output_view(off, oview_size);
4158   unsigned char* p;
4159
4160   if (size == 64)
4161     {
4162       const Output_data_got_powerpc<size, big_endian>* got
4163         = this->targ_->got_section();
4164       Address got_os_addr = got->output_section()->address();
4165
4166       if (!this->plt_call_stubs_.empty())
4167         {
4168           // The base address of the .plt section.
4169           Address plt_base = this->targ_->plt_section()->address();
4170           Address iplt_base = invalid_address;
4171
4172           // Write out plt call stubs.
4173           typename Plt_stub_entries::const_iterator cs;
4174           for (cs = this->plt_call_stubs_.begin();
4175                cs != this->plt_call_stubs_.end();
4176                ++cs)
4177             {
4178               bool is_iplt;
4179               Address pltoff = this->plt_off(cs, &is_iplt);
4180               Address plt_addr = pltoff;
4181               if (is_iplt)
4182                 {
4183                   if (iplt_base == invalid_address)
4184                     iplt_base = this->targ_->iplt_section()->address();
4185                   plt_addr += iplt_base;
4186                 }
4187               else
4188                 plt_addr += plt_base;
4189               const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
4190                 <const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
4191               Address got_addr = got_os_addr + ppcobj->toc_base_offset();
4192               Address off = plt_addr - got_addr;
4193
4194               if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
4195                 gold_error(_("%s: linkage table error against `%s'"),
4196                            cs->first.object_->name().c_str(),
4197                            cs->first.sym_->demangled_name().c_str());
4198
4199               bool plt_load_toc = this->targ_->abiversion() < 2;
4200               bool static_chain
4201                 = plt_load_toc && parameters->options().plt_static_chain();
4202               bool thread_safe
4203                 = plt_load_toc && this->targ_->plt_thread_safe();
4204               bool use_fake_dep = false;
4205               Address cmp_branch_off = 0;
4206               if (thread_safe)
4207                 {
4208                   unsigned int pltindex
4209                     = ((pltoff - this->targ_->first_plt_entry_offset())
4210                        / this->targ_->plt_entry_size());
4211                   Address glinkoff
4212                     = (this->targ_->glink_section()->pltresolve_size
4213                        + pltindex * 8);
4214                   if (pltindex > 32768)
4215                     glinkoff += (pltindex - 32768) * 4;
4216                   Address to
4217                     = this->targ_->glink_section()->address() + glinkoff;
4218                   Address from
4219                     = (this->stub_address() + cs->second + 24
4220                        + 4 * (ha(off) != 0)
4221                        + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
4222                        + 4 * static_chain);
4223                   cmp_branch_off = to - from;
4224                   use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
4225                 }
4226
4227               p = oview + cs->second;
4228               if (ha(off) != 0)
4229                 {
4230                   write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4231                   p += 4;
4232                   if (plt_load_toc)
4233                     {
4234                       write_insn<big_endian>(p, addis_11_2 + ha(off));
4235                       p += 4;
4236                       write_insn<big_endian>(p, ld_12_11 + l(off));
4237                       p += 4;
4238                     }
4239                   else
4240                     {
4241                       write_insn<big_endian>(p, addis_12_2 + ha(off));
4242                       p += 4;
4243                       write_insn<big_endian>(p, ld_12_12 + l(off));
4244                       p += 4;
4245                     }
4246                   if (plt_load_toc
4247                       && ha(off + 8 + 8 * static_chain) != ha(off))
4248                     {
4249                       write_insn<big_endian>(p, addi_11_11 + l(off));
4250                       p += 4;
4251                       off = 0;
4252                     }
4253                   write_insn<big_endian>(p, mtctr_12);
4254                   p += 4;
4255                   if (plt_load_toc)
4256                     {
4257                       if (use_fake_dep)
4258                         {
4259                           write_insn<big_endian>(p, xor_2_12_12);
4260                           p += 4;
4261                           write_insn<big_endian>(p, add_11_11_2);
4262                           p += 4;
4263                         }
4264                       write_insn<big_endian>(p, ld_2_11 + l(off + 8));
4265                       p += 4;
4266                       if (static_chain)
4267                         {
4268                           write_insn<big_endian>(p, ld_11_11 + l(off + 16));
4269                           p += 4;
4270                         }
4271                     }
4272                 }
4273               else
4274                 {
4275                   write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
4276                   p += 4;
4277                   write_insn<big_endian>(p, ld_12_2 + l(off));
4278                   p += 4;
4279                   if (plt_load_toc
4280                       && ha(off + 8 + 8 * static_chain) != ha(off))
4281                     {
4282                       write_insn<big_endian>(p, addi_2_2 + l(off));
4283                       p += 4;
4284                       off = 0;
4285                     }
4286                   write_insn<big_endian>(p, mtctr_12);
4287                   p += 4;
4288                   if (plt_load_toc)
4289                     {
4290                       if (use_fake_dep)
4291                         {
4292                           write_insn<big_endian>(p, xor_11_12_12);
4293                           p += 4;
4294                           write_insn<big_endian>(p, add_2_2_11);
4295                           p += 4;
4296                         }
4297                       if (static_chain)
4298                         {
4299                           write_insn<big_endian>(p, ld_11_2 + l(off + 16));
4300                           p += 4;
4301                         }
4302                       write_insn<big_endian>(p, ld_2_2 + l(off + 8));
4303                       p += 4;
4304                     }
4305                 }
4306               if (thread_safe && !use_fake_dep)
4307                 {
4308                   write_insn<big_endian>(p, cmpldi_2_0);
4309                   p += 4;
4310                   write_insn<big_endian>(p, bnectr_p4);
4311                   p += 4;
4312                   write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
4313                 }
4314               else
4315                 write_insn<big_endian>(p, bctr);
4316             }
4317         }
4318
4319       // Write out long branch stubs.
4320       typename Branch_stub_entries::const_iterator bs;
4321       for (bs = this->long_branch_stubs_.begin();
4322            bs != this->long_branch_stubs_.end();
4323            ++bs)
4324         {
4325           p = oview + this->plt_size_ + bs->second;
4326           Address loc = this->stub_address() + this->plt_size_ + bs->second;
4327           Address delta = bs->first.dest_ - loc;
4328           if (delta + (1 << 25) < 2 << 25)
4329             write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4330           else
4331             {
4332               Address brlt_addr
4333                 = this->targ_->find_branch_lookup_table(bs->first.dest_);
4334               gold_assert(brlt_addr != invalid_address);
4335               brlt_addr += this->targ_->brlt_section()->address();
4336               Address got_addr = got_os_addr + bs->first.toc_base_off_;
4337               Address brltoff = brlt_addr - got_addr;
4338               if (ha(brltoff) == 0)
4339                 {
4340                   write_insn<big_endian>(p, ld_12_2 + l(brltoff)),      p += 4;
4341                 }
4342               else
4343                 {
4344                   write_insn<big_endian>(p, addis_12_2 + ha(brltoff)),  p += 4;
4345                   write_insn<big_endian>(p, ld_12_12 + l(brltoff)),     p += 4;
4346                 }
4347               write_insn<big_endian>(p, mtctr_12),                      p += 4;
4348               write_insn<big_endian>(p, bctr);
4349             }
4350         }
4351     }
4352   else
4353     {
4354       if (!this->plt_call_stubs_.empty())
4355         {
4356           // The base address of the .plt section.
4357           Address plt_base = this->targ_->plt_section()->address();
4358           Address iplt_base = invalid_address;
4359           // The address of _GLOBAL_OFFSET_TABLE_.
4360           Address g_o_t = invalid_address;
4361
4362           // Write out plt call stubs.
4363           typename Plt_stub_entries::const_iterator cs;
4364           for (cs = this->plt_call_stubs_.begin();
4365                cs != this->plt_call_stubs_.end();
4366                ++cs)
4367             {
4368               bool is_iplt;
4369               Address plt_addr = this->plt_off(cs, &is_iplt);
4370               if (is_iplt)
4371                 {
4372                   if (iplt_base == invalid_address)
4373                     iplt_base = this->targ_->iplt_section()->address();
4374                   plt_addr += iplt_base;
4375                 }
4376               else
4377                 plt_addr += plt_base;
4378
4379               p = oview + cs->second;
4380               if (parameters->options().output_is_position_independent())
4381                 {
4382                   Address got_addr;
4383                   const Powerpc_relobj<size, big_endian>* ppcobj
4384                     = (static_cast<const Powerpc_relobj<size, big_endian>*>
4385                        (cs->first.object_));
4386                   if (ppcobj != NULL && cs->first.addend_ >= 32768)
4387                     {
4388                       unsigned int got2 = ppcobj->got2_shndx();
4389                       got_addr = ppcobj->get_output_section_offset(got2);
4390                       gold_assert(got_addr != invalid_address);
4391                       got_addr += (ppcobj->output_section(got2)->address()
4392                                    + cs->first.addend_);
4393                     }
4394                   else
4395                     {
4396                       if (g_o_t == invalid_address)
4397                         {
4398                           const Output_data_got_powerpc<size, big_endian>* got
4399                             = this->targ_->got_section();
4400                           g_o_t = got->address() + got->g_o_t();
4401                         }
4402                       got_addr = g_o_t;
4403                     }
4404
4405                   Address off = plt_addr - got_addr;
4406                   if (ha(off) == 0)
4407                     {
4408                       write_insn<big_endian>(p +  0, lwz_11_30 + l(off));
4409                       write_insn<big_endian>(p +  4, mtctr_11);
4410                       write_insn<big_endian>(p +  8, bctr);
4411                     }
4412                   else
4413                     {
4414                       write_insn<big_endian>(p +  0, addis_11_30 + ha(off));
4415                       write_insn<big_endian>(p +  4, lwz_11_11 + l(off));
4416                       write_insn<big_endian>(p +  8, mtctr_11);
4417                       write_insn<big_endian>(p + 12, bctr);
4418                     }
4419                 }
4420               else
4421                 {
4422                   write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
4423                   write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
4424                   write_insn<big_endian>(p +  8, mtctr_11);
4425                   write_insn<big_endian>(p + 12, bctr);
4426                 }
4427             }
4428         }
4429
4430       // Write out long branch stubs.
4431       typename Branch_stub_entries::const_iterator bs;
4432       for (bs = this->long_branch_stubs_.begin();
4433            bs != this->long_branch_stubs_.end();
4434            ++bs)
4435         {
4436           p = oview + this->plt_size_ + bs->second;
4437           Address loc = this->stub_address() + this->plt_size_ + bs->second;
4438           Address delta = bs->first.dest_ - loc;
4439           if (delta + (1 << 25) < 2 << 25)
4440             write_insn<big_endian>(p, b | (delta & 0x3fffffc));
4441           else if (!parameters->options().output_is_position_independent())
4442             {
4443               write_insn<big_endian>(p +  0, lis_12 + ha(bs->first.dest_));
4444               write_insn<big_endian>(p +  4, addi_12_12 + l(bs->first.dest_));
4445               write_insn<big_endian>(p +  8, mtctr_12);
4446               write_insn<big_endian>(p + 12, bctr);
4447             }
4448           else
4449             {
4450               delta -= 8;
4451               write_insn<big_endian>(p +  0, mflr_0);
4452               write_insn<big_endian>(p +  4, bcl_20_31);
4453               write_insn<big_endian>(p +  8, mflr_12);
4454               write_insn<big_endian>(p + 12, addis_12_12 + ha(delta));
4455               write_insn<big_endian>(p + 16, addi_12_12 + l(delta));
4456               write_insn<big_endian>(p + 20, mtlr_0);
4457               write_insn<big_endian>(p + 24, mtctr_12);
4458               write_insn<big_endian>(p + 28, bctr);
4459             }
4460         }
4461     }
4462 }
4463
4464 // Write out .glink.
4465
4466 template<int size, bool big_endian>
4467 void
4468 Output_data_glink<size, big_endian>::do_write(Output_file* of)
4469 {
4470   const section_size_type off = this->offset();
4471   const section_size_type oview_size =
4472     convert_to_section_size_type(this->data_size());
4473   unsigned char* const oview = of->get_output_view(off, oview_size);
4474   unsigned char* p;
4475
4476   // The base address of the .plt section.
4477   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4478   Address plt_base = this->targ_->plt_section()->address();
4479
4480   if (size == 64)
4481     {
4482       if (this->end_branch_table_ != 0)
4483         {
4484           // Write pltresolve stub.
4485           p = oview;
4486           Address after_bcl = this->address() + 16;
4487           Address pltoff = plt_base - after_bcl;
4488
4489           elfcpp::Swap<64, big_endian>::writeval(p, pltoff),    p += 8;
4490
4491           if (this->targ_->abiversion() < 2)
4492             {
4493               write_insn<big_endian>(p, mflr_12),               p += 4;
4494               write_insn<big_endian>(p, bcl_20_31),             p += 4;
4495               write_insn<big_endian>(p, mflr_11),               p += 4;
4496               write_insn<big_endian>(p, ld_2_11 + l(-16)),      p += 4;
4497               write_insn<big_endian>(p, mtlr_12),               p += 4;
4498               write_insn<big_endian>(p, add_11_2_11),           p += 4;
4499               write_insn<big_endian>(p, ld_12_11 + 0),          p += 4;
4500               write_insn<big_endian>(p, ld_2_11 + 8),           p += 4;
4501               write_insn<big_endian>(p, mtctr_12),              p += 4;
4502               write_insn<big_endian>(p, ld_11_11 + 16),         p += 4;
4503             }
4504           else
4505             {
4506               write_insn<big_endian>(p, mflr_0),                p += 4;
4507               write_insn<big_endian>(p, bcl_20_31),             p += 4;
4508               write_insn<big_endian>(p, mflr_11),               p += 4;
4509               write_insn<big_endian>(p, ld_2_11 + l(-16)),      p += 4;
4510               write_insn<big_endian>(p, mtlr_0),                p += 4;
4511               write_insn<big_endian>(p, sub_12_12_11),          p += 4;
4512               write_insn<big_endian>(p, add_11_2_11),           p += 4;
4513               write_insn<big_endian>(p, addi_0_12 + l(-48)),    p += 4;
4514               write_insn<big_endian>(p, ld_12_11 + 0),          p += 4;
4515               write_insn<big_endian>(p, srdi_0_0_2),            p += 4;
4516               write_insn<big_endian>(p, mtctr_12),              p += 4;
4517               write_insn<big_endian>(p, ld_11_11 + 8),          p += 4;
4518             }
4519           write_insn<big_endian>(p, bctr),                      p += 4;
4520           while (p < oview + this->pltresolve_size)
4521             write_insn<big_endian>(p, nop), p += 4;
4522
4523           // Write lazy link call stubs.
4524           uint32_t indx = 0;
4525           while (p < oview + this->end_branch_table_)
4526             {
4527               if (this->targ_->abiversion() < 2)
4528                 {
4529                   if (indx < 0x8000)
4530                     {
4531                       write_insn<big_endian>(p, li_0_0 + indx),         p += 4;
4532                     }
4533                   else
4534                     {
4535                       write_insn<big_endian>(p, lis_0_0 + hi(indx)),    p += 4;
4536                       write_insn<big_endian>(p, ori_0_0_0 + l(indx)),   p += 4;
4537                     }
4538                 }
4539               uint32_t branch_off = 8 - (p - oview);
4540               write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),  p += 4;
4541               indx++;
4542             }
4543         }
4544
4545       Address plt_base = this->targ_->plt_section()->address();
4546       Address iplt_base = invalid_address;
4547       unsigned int global_entry_off = (this->end_branch_table_ + 15) & -16;
4548       Address global_entry_base = this->address() + global_entry_off;
4549       typename Global_entry_stub_entries::const_iterator ge;
4550       for (ge = this->global_entry_stubs_.begin();
4551            ge != this->global_entry_stubs_.end();
4552            ++ge)
4553         {
4554           p = oview + global_entry_off + ge->second;
4555           Address plt_addr = ge->first->plt_offset();
4556           if (ge->first->type() == elfcpp::STT_GNU_IFUNC
4557               && ge->first->can_use_relative_reloc(false))
4558             {
4559               if (iplt_base == invalid_address)
4560                 iplt_base = this->targ_->iplt_section()->address();
4561               plt_addr += iplt_base;
4562             }
4563           else
4564             plt_addr += plt_base;
4565           Address my_addr = global_entry_base + ge->second;
4566           Address off = plt_addr - my_addr;
4567
4568           if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
4569             gold_error(_("%s: linkage table error against `%s'"),
4570                        ge->first->object()->name().c_str(),
4571                        ge->first->demangled_name().c_str());
4572
4573           write_insn<big_endian>(p, addis_12_12 + ha(off)),     p += 4;
4574           write_insn<big_endian>(p, ld_12_12 + l(off)),         p += 4;
4575           write_insn<big_endian>(p, mtctr_12),                  p += 4;
4576           write_insn<big_endian>(p, bctr);
4577         }
4578     }
4579   else
4580     {
4581       const Output_data_got_powerpc<size, big_endian>* got
4582         = this->targ_->got_section();
4583       // The address of _GLOBAL_OFFSET_TABLE_.
4584       Address g_o_t = got->address() + got->g_o_t();
4585
4586       // Write out pltresolve branch table.
4587       p = oview;
4588       unsigned int the_end = oview_size - this->pltresolve_size;
4589       unsigned char* end_p = oview + the_end;
4590       while (p < end_p - 8 * 4)
4591         write_insn<big_endian>(p, b + end_p - p), p += 4;
4592       while (p < end_p)
4593         write_insn<big_endian>(p, nop), p += 4;
4594
4595       // Write out pltresolve call stub.
4596       if (parameters->options().output_is_position_independent())
4597         {
4598           Address res0_off = 0;
4599           Address after_bcl_off = the_end + 12;
4600           Address bcl_res0 = after_bcl_off - res0_off;
4601
4602           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
4603           write_insn<big_endian>(p +  4, mflr_0);
4604           write_insn<big_endian>(p +  8, bcl_20_31);
4605           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
4606           write_insn<big_endian>(p + 16, mflr_12);
4607           write_insn<big_endian>(p + 20, mtlr_0);
4608           write_insn<big_endian>(p + 24, sub_11_11_12);
4609
4610           Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
4611
4612           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
4613           if (ha(got_bcl) == ha(got_bcl + 4))
4614             {
4615               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
4616               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
4617             }
4618           else
4619             {
4620               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
4621               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
4622             }
4623           write_insn<big_endian>(p + 40, mtctr_0);
4624           write_insn<big_endian>(p + 44, add_0_11_11);
4625           write_insn<big_endian>(p + 48, add_11_0_11);
4626           write_insn<big_endian>(p + 52, bctr);
4627           write_insn<big_endian>(p + 56, nop);
4628           write_insn<big_endian>(p + 60, nop);
4629         }
4630       else
4631         {
4632           Address res0 = this->address();
4633
4634           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
4635           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
4636           if (ha(g_o_t + 4) == ha(g_o_t + 8))
4637             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
4638           else
4639             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
4640           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
4641           write_insn<big_endian>(p + 16, mtctr_0);
4642           write_insn<big_endian>(p + 20, add_0_11_11);
4643           if (ha(g_o_t + 4) == ha(g_o_t + 8))
4644             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
4645           else
4646             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
4647           write_insn<big_endian>(p + 28, add_11_0_11);
4648           write_insn<big_endian>(p + 32, bctr);
4649           write_insn<big_endian>(p + 36, nop);
4650           write_insn<big_endian>(p + 40, nop);
4651           write_insn<big_endian>(p + 44, nop);
4652           write_insn<big_endian>(p + 48, nop);
4653           write_insn<big_endian>(p + 52, nop);
4654           write_insn<big_endian>(p + 56, nop);
4655           write_insn<big_endian>(p + 60, nop);
4656         }
4657       p += 64;
4658     }
4659
4660   of->write_output_view(off, oview_size, oview);
4661 }
4662
4663
4664 // A class to handle linker generated save/restore functions.
4665
4666 template<int size, bool big_endian>
4667 class Output_data_save_res : public Output_section_data_build
4668 {
4669  public:
4670   Output_data_save_res(Symbol_table* symtab);
4671
4672  protected:
4673   // Write to a map file.
4674   void
4675   do_print_to_mapfile(Mapfile* mapfile) const
4676   { mapfile->print_output_data(this, _("** save/restore")); }
4677
4678   void
4679   do_write(Output_file*);
4680
4681  private:
4682   // The maximum size of save/restore contents.
4683   static const unsigned int savres_max = 218*4;
4684
4685   void
4686   savres_define(Symbol_table* symtab,
4687                 const char *name,
4688                 unsigned int lo, unsigned int hi,
4689                 unsigned char* write_ent(unsigned char*, int),
4690                 unsigned char* write_tail(unsigned char*, int));
4691
4692   unsigned char *contents_;
4693 };
4694
4695 template<bool big_endian>
4696 static unsigned char*
4697 savegpr0(unsigned char* p, int r)
4698 {
4699   uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4700   write_insn<big_endian>(p, insn);
4701   return p + 4;
4702 }
4703
4704 template<bool big_endian>
4705 static unsigned char*
4706 savegpr0_tail(unsigned char* p, int r)
4707 {
4708   p = savegpr0<big_endian>(p, r);
4709   uint32_t insn = std_0_1 + 16;
4710   write_insn<big_endian>(p, insn);
4711   p = p + 4;
4712   write_insn<big_endian>(p, blr);
4713   return p + 4;
4714 }
4715
4716 template<bool big_endian>
4717 static unsigned char*
4718 restgpr0(unsigned char* p, int r)
4719 {
4720   uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4721   write_insn<big_endian>(p, insn);
4722   return p + 4;
4723 }
4724
4725 template<bool big_endian>
4726 static unsigned char*
4727 restgpr0_tail(unsigned char* p, int r)
4728 {
4729   uint32_t insn = ld_0_1 + 16;
4730   write_insn<big_endian>(p, insn);
4731   p = p + 4;
4732   p = restgpr0<big_endian>(p, r);
4733   write_insn<big_endian>(p, mtlr_0);
4734   p = p + 4;
4735   if (r == 29)
4736     {
4737       p = restgpr0<big_endian>(p, 30);
4738       p = restgpr0<big_endian>(p, 31);
4739     }
4740   write_insn<big_endian>(p, blr);
4741   return p + 4;
4742 }
4743
4744 template<bool big_endian>
4745 static unsigned char*
4746 savegpr1(unsigned char* p, int r)
4747 {
4748   uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4749   write_insn<big_endian>(p, insn);
4750   return p + 4;
4751 }
4752
4753 template<bool big_endian>
4754 static unsigned char*
4755 savegpr1_tail(unsigned char* p, int r)
4756 {
4757   p = savegpr1<big_endian>(p, r);
4758   write_insn<big_endian>(p, blr);
4759   return p + 4;
4760 }
4761
4762 template<bool big_endian>
4763 static unsigned char*
4764 restgpr1(unsigned char* p, int r)
4765 {
4766   uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
4767   write_insn<big_endian>(p, insn);
4768   return p + 4;
4769 }
4770
4771 template<bool big_endian>
4772 static unsigned char*
4773 restgpr1_tail(unsigned char* p, int r)
4774 {
4775   p = restgpr1<big_endian>(p, r);
4776   write_insn<big_endian>(p, blr);
4777   return p + 4;
4778 }
4779
4780 template<bool big_endian>
4781 static unsigned char*
4782 savefpr(unsigned char* p, int r)
4783 {
4784   uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4785   write_insn<big_endian>(p, insn);
4786   return p + 4;
4787 }
4788
4789 template<bool big_endian>
4790 static unsigned char*
4791 savefpr0_tail(unsigned char* p, int r)
4792 {
4793   p = savefpr<big_endian>(p, r);
4794   write_insn<big_endian>(p, std_0_1 + 16);
4795   p = p + 4;
4796   write_insn<big_endian>(p, blr);
4797   return p + 4;
4798 }
4799
4800 template<bool big_endian>
4801 static unsigned char*
4802 restfpr(unsigned char* p, int r)
4803 {
4804   uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
4805   write_insn<big_endian>(p, insn);
4806   return p + 4;
4807 }
4808
4809 template<bool big_endian>
4810 static unsigned char*
4811 restfpr0_tail(unsigned char* p, int r)
4812 {
4813   write_insn<big_endian>(p, ld_0_1 + 16);
4814   p = p + 4;
4815   p = restfpr<big_endian>(p, r);
4816   write_insn<big_endian>(p, mtlr_0);
4817   p = p + 4;
4818   if (r == 29)
4819     {
4820       p = restfpr<big_endian>(p, 30);
4821       p = restfpr<big_endian>(p, 31);
4822     }
4823   write_insn<big_endian>(p, blr);
4824   return p + 4;
4825 }
4826
4827 template<bool big_endian>
4828 static unsigned char*
4829 savefpr1_tail(unsigned char* p, int r)
4830 {
4831   p = savefpr<big_endian>(p, r);
4832   write_insn<big_endian>(p, blr);
4833   return p + 4;
4834 }
4835
4836 template<bool big_endian>
4837 static unsigned char*
4838 restfpr1_tail(unsigned char* p, int r)
4839 {
4840   p = restfpr<big_endian>(p, r);
4841   write_insn<big_endian>(p, blr);
4842   return p + 4;
4843 }
4844
4845 template<bool big_endian>
4846 static unsigned char*
4847 savevr(unsigned char* p, int r)
4848 {
4849   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4850   write_insn<big_endian>(p, insn);
4851   p = p + 4;
4852   insn = stvx_0_12_0 + (r << 21);
4853   write_insn<big_endian>(p, insn);
4854   return p + 4;
4855 }
4856
4857 template<bool big_endian>
4858 static unsigned char*
4859 savevr_tail(unsigned char* p, int r)
4860 {
4861   p = savevr<big_endian>(p, r);
4862   write_insn<big_endian>(p, blr);
4863   return p + 4;
4864 }
4865
4866 template<bool big_endian>
4867 static unsigned char*
4868 restvr(unsigned char* p, int r)
4869 {
4870   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
4871   write_insn<big_endian>(p, insn);
4872   p = p + 4;
4873   insn = lvx_0_12_0 + (r << 21);
4874   write_insn<big_endian>(p, insn);
4875   return p + 4;
4876 }
4877
4878 template<bool big_endian>
4879 static unsigned char*
4880 restvr_tail(unsigned char* p, int r)
4881 {
4882   p = restvr<big_endian>(p, r);
4883   write_insn<big_endian>(p, blr);
4884   return p + 4;
4885 }
4886
4887
4888 template<int size, bool big_endian>
4889 Output_data_save_res<size, big_endian>::Output_data_save_res(
4890     Symbol_table* symtab)
4891   : Output_section_data_build(4),
4892     contents_(NULL)
4893 {
4894   this->savres_define(symtab,
4895                       "_savegpr0_", 14, 31,
4896                       savegpr0<big_endian>, savegpr0_tail<big_endian>);
4897   this->savres_define(symtab,
4898                       "_restgpr0_", 14, 29,
4899                       restgpr0<big_endian>, restgpr0_tail<big_endian>);
4900   this->savres_define(symtab,
4901                       "_restgpr0_", 30, 31,
4902                       restgpr0<big_endian>, restgpr0_tail<big_endian>);
4903   this->savres_define(symtab,
4904                       "_savegpr1_", 14, 31,
4905                       savegpr1<big_endian>, savegpr1_tail<big_endian>);
4906   this->savres_define(symtab,
4907                       "_restgpr1_", 14, 31,
4908                       restgpr1<big_endian>, restgpr1_tail<big_endian>);
4909   this->savres_define(symtab,
4910                       "_savefpr_", 14, 31,
4911                       savefpr<big_endian>, savefpr0_tail<big_endian>);
4912   this->savres_define(symtab,
4913                       "_restfpr_", 14, 29,
4914                       restfpr<big_endian>, restfpr0_tail<big_endian>);
4915   this->savres_define(symtab,
4916                       "_restfpr_", 30, 31,
4917                       restfpr<big_endian>, restfpr0_tail<big_endian>);
4918   this->savres_define(symtab,
4919                       "._savef", 14, 31,
4920                       savefpr<big_endian>, savefpr1_tail<big_endian>);
4921   this->savres_define(symtab,
4922                       "._restf", 14, 31,
4923                       restfpr<big_endian>, restfpr1_tail<big_endian>);
4924   this->savres_define(symtab,
4925                       "_savevr_", 20, 31,
4926                       savevr<big_endian>, savevr_tail<big_endian>);
4927   this->savres_define(symtab,
4928                       "_restvr_", 20, 31,
4929                       restvr<big_endian>, restvr_tail<big_endian>);
4930 }
4931
4932 template<int size, bool big_endian>
4933 void
4934 Output_data_save_res<size, big_endian>::savres_define(
4935     Symbol_table* symtab,
4936     const char *name,
4937     unsigned int lo, unsigned int hi,
4938     unsigned char* write_ent(unsigned char*, int),
4939     unsigned char* write_tail(unsigned char*, int))
4940 {
4941   size_t len = strlen(name);
4942   bool writing = false;
4943   char sym[16];
4944
4945   memcpy(sym, name, len);
4946   sym[len + 2] = 0;
4947
4948   for (unsigned int i = lo; i <= hi; i++)
4949     {
4950       sym[len + 0] = i / 10 + '0';
4951       sym[len + 1] = i % 10 + '0';
4952       Symbol* gsym = symtab->lookup(sym);
4953       bool refd = gsym != NULL && gsym->is_undefined();
4954       writing = writing || refd;
4955       if (writing)
4956         {
4957           if (this->contents_ == NULL)
4958             this->contents_ = new unsigned char[this->savres_max];
4959
4960           section_size_type value = this->current_data_size();
4961           unsigned char* p = this->contents_ + value;
4962           if (i != hi)
4963             p = write_ent(p, i);
4964           else
4965             p = write_tail(p, i);
4966           section_size_type cur_size = p - this->contents_;
4967           this->set_current_data_size(cur_size);
4968           if (refd)
4969             symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
4970                                           this, value, cur_size - value,
4971                                           elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
4972                                           elfcpp::STV_HIDDEN, 0, false, false);
4973         }
4974     }
4975 }
4976
4977 // Write out save/restore.
4978
4979 template<int size, bool big_endian>
4980 void
4981 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
4982 {
4983   const section_size_type off = this->offset();
4984   const section_size_type oview_size =
4985     convert_to_section_size_type(this->data_size());
4986   unsigned char* const oview = of->get_output_view(off, oview_size);
4987   memcpy(oview, this->contents_, oview_size);
4988   of->write_output_view(off, oview_size, oview);
4989 }
4990
4991
4992 // Create the glink section.
4993
4994 template<int size, bool big_endian>
4995 void
4996 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
4997 {
4998   if (this->glink_ == NULL)
4999     {
5000       this->glink_ = new Output_data_glink<size, big_endian>(this);
5001       this->glink_->add_eh_frame(layout);
5002       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
5003                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
5004                                       this->glink_, ORDER_TEXT, false);
5005     }
5006 }
5007
5008 // Create a PLT entry for a global symbol.
5009
5010 template<int size, bool big_endian>
5011 void
5012 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
5013                                                  Layout* layout,
5014                                                  Symbol* gsym)
5015 {
5016   if (gsym->type() == elfcpp::STT_GNU_IFUNC
5017       && gsym->can_use_relative_reloc(false))
5018     {
5019       if (this->iplt_ == NULL)
5020         this->make_iplt_section(symtab, layout);
5021       this->iplt_->add_ifunc_entry(gsym);
5022     }
5023   else
5024     {
5025       if (this->plt_ == NULL)
5026         this->make_plt_section(symtab, layout);
5027       this->plt_->add_entry(gsym);
5028     }
5029 }
5030
5031 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
5032
5033 template<int size, bool big_endian>
5034 void
5035 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
5036     Symbol_table* symtab,
5037     Layout* layout,
5038     Sized_relobj_file<size, big_endian>* relobj,
5039     unsigned int r_sym)
5040 {
5041   if (this->iplt_ == NULL)
5042     this->make_iplt_section(symtab, layout);
5043   this->iplt_->add_local_ifunc_entry(relobj, r_sym);
5044 }
5045
5046 // Return the number of entries in the PLT.
5047
5048 template<int size, bool big_endian>
5049 unsigned int
5050 Target_powerpc<size, big_endian>::plt_entry_count() const
5051 {
5052   if (this->plt_ == NULL)
5053     return 0;
5054   return this->plt_->entry_count();
5055 }
5056
5057 // Create a GOT entry for local dynamic __tls_get_addr calls.
5058
5059 template<int size, bool big_endian>
5060 unsigned int
5061 Target_powerpc<size, big_endian>::tlsld_got_offset(
5062     Symbol_table* symtab,
5063     Layout* layout,
5064     Sized_relobj_file<size, big_endian>* object)
5065 {
5066   if (this->tlsld_got_offset_ == -1U)
5067     {
5068       gold_assert(symtab != NULL && layout != NULL && object != NULL);
5069       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
5070       Output_data_got_powerpc<size, big_endian>* got
5071         = this->got_section(symtab, layout);
5072       unsigned int got_offset = got->add_constant_pair(0, 0);
5073       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
5074                           got_offset, 0);
5075       this->tlsld_got_offset_ = got_offset;
5076     }
5077   return this->tlsld_got_offset_;
5078 }
5079
5080 // Get the Reference_flags for a particular relocation.
5081
5082 template<int size, bool big_endian>
5083 int
5084 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
5085     unsigned int r_type,
5086     const Target_powerpc* target)
5087 {
5088   int ref = 0;
5089
5090   switch (r_type)
5091     {
5092     case elfcpp::R_POWERPC_NONE:
5093     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5094     case elfcpp::R_POWERPC_GNU_VTENTRY:
5095     case elfcpp::R_PPC64_TOC:
5096       // No symbol reference.
5097       break;
5098
5099     case elfcpp::R_PPC64_ADDR64:
5100     case elfcpp::R_PPC64_UADDR64:
5101     case elfcpp::R_POWERPC_ADDR32:
5102     case elfcpp::R_POWERPC_UADDR32:
5103     case elfcpp::R_POWERPC_ADDR16:
5104     case elfcpp::R_POWERPC_UADDR16:
5105     case elfcpp::R_POWERPC_ADDR16_LO:
5106     case elfcpp::R_POWERPC_ADDR16_HI:
5107     case elfcpp::R_POWERPC_ADDR16_HA:
5108       ref = Symbol::ABSOLUTE_REF;
5109       break;
5110
5111     case elfcpp::R_POWERPC_ADDR24:
5112     case elfcpp::R_POWERPC_ADDR14:
5113     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5114     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5115       ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
5116       break;
5117
5118     case elfcpp::R_PPC64_REL64:
5119     case elfcpp::R_POWERPC_REL32:
5120     case elfcpp::R_PPC_LOCAL24PC:
5121     case elfcpp::R_POWERPC_REL16:
5122     case elfcpp::R_POWERPC_REL16_LO:
5123     case elfcpp::R_POWERPC_REL16_HI:
5124     case elfcpp::R_POWERPC_REL16_HA:
5125       ref = Symbol::RELATIVE_REF;
5126       break;
5127
5128     case elfcpp::R_POWERPC_REL24:
5129     case elfcpp::R_PPC_PLTREL24:
5130     case elfcpp::R_POWERPC_REL14:
5131     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5132     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5133       ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
5134       break;
5135
5136     case elfcpp::R_POWERPC_GOT16:
5137     case elfcpp::R_POWERPC_GOT16_LO:
5138     case elfcpp::R_POWERPC_GOT16_HI:
5139     case elfcpp::R_POWERPC_GOT16_HA:
5140     case elfcpp::R_PPC64_GOT16_DS:
5141     case elfcpp::R_PPC64_GOT16_LO_DS:
5142     case elfcpp::R_PPC64_TOC16:
5143     case elfcpp::R_PPC64_TOC16_LO:
5144     case elfcpp::R_PPC64_TOC16_HI:
5145     case elfcpp::R_PPC64_TOC16_HA:
5146     case elfcpp::R_PPC64_TOC16_DS:
5147     case elfcpp::R_PPC64_TOC16_LO_DS:
5148       // Absolute in GOT.
5149       ref = Symbol::ABSOLUTE_REF;
5150       break;
5151
5152     case elfcpp::R_POWERPC_GOT_TPREL16:
5153     case elfcpp::R_POWERPC_TLS:
5154       ref = Symbol::TLS_REF;
5155       break;
5156
5157     case elfcpp::R_POWERPC_COPY:
5158     case elfcpp::R_POWERPC_GLOB_DAT:
5159     case elfcpp::R_POWERPC_JMP_SLOT:
5160     case elfcpp::R_POWERPC_RELATIVE:
5161     case elfcpp::R_POWERPC_DTPMOD:
5162     default:
5163       // Not expected.  We will give an error later.
5164       break;
5165     }
5166
5167   if (size == 64 && target->abiversion() < 2)
5168     ref |= Symbol::FUNC_DESC_ABI;
5169   return ref;
5170 }
5171
5172 // Report an unsupported relocation against a local symbol.
5173
5174 template<int size, bool big_endian>
5175 void
5176 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
5177     Sized_relobj_file<size, big_endian>* object,
5178     unsigned int r_type)
5179 {
5180   gold_error(_("%s: unsupported reloc %u against local symbol"),
5181              object->name().c_str(), r_type);
5182 }
5183
5184 // We are about to emit a dynamic relocation of type R_TYPE.  If the
5185 // dynamic linker does not support it, issue an error.
5186
5187 template<int size, bool big_endian>
5188 void
5189 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
5190                                                       unsigned int r_type)
5191 {
5192   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
5193
5194   // These are the relocation types supported by glibc for both 32-bit
5195   // and 64-bit powerpc.
5196   switch (r_type)
5197     {
5198     case elfcpp::R_POWERPC_NONE:
5199     case elfcpp::R_POWERPC_RELATIVE:
5200     case elfcpp::R_POWERPC_GLOB_DAT:
5201     case elfcpp::R_POWERPC_DTPMOD:
5202     case elfcpp::R_POWERPC_DTPREL:
5203     case elfcpp::R_POWERPC_TPREL:
5204     case elfcpp::R_POWERPC_JMP_SLOT:
5205     case elfcpp::R_POWERPC_COPY:
5206     case elfcpp::R_POWERPC_IRELATIVE:
5207     case elfcpp::R_POWERPC_ADDR32:
5208     case elfcpp::R_POWERPC_UADDR32:
5209     case elfcpp::R_POWERPC_ADDR24:
5210     case elfcpp::R_POWERPC_ADDR16:
5211     case elfcpp::R_POWERPC_UADDR16:
5212     case elfcpp::R_POWERPC_ADDR16_LO:
5213     case elfcpp::R_POWERPC_ADDR16_HI:
5214     case elfcpp::R_POWERPC_ADDR16_HA:
5215     case elfcpp::R_POWERPC_ADDR14:
5216     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5217     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5218     case elfcpp::R_POWERPC_REL32:
5219     case elfcpp::R_POWERPC_REL24:
5220     case elfcpp::R_POWERPC_TPREL16:
5221     case elfcpp::R_POWERPC_TPREL16_LO:
5222     case elfcpp::R_POWERPC_TPREL16_HI:
5223     case elfcpp::R_POWERPC_TPREL16_HA:
5224       return;
5225
5226     default:
5227       break;
5228     }
5229
5230   if (size == 64)
5231     {
5232       switch (r_type)
5233         {
5234           // These are the relocation types supported only on 64-bit.
5235         case elfcpp::R_PPC64_ADDR64:
5236         case elfcpp::R_PPC64_UADDR64:
5237         case elfcpp::R_PPC64_JMP_IREL:
5238         case elfcpp::R_PPC64_ADDR16_DS:
5239         case elfcpp::R_PPC64_ADDR16_LO_DS:
5240         case elfcpp::R_PPC64_ADDR16_HIGH:
5241         case elfcpp::R_PPC64_ADDR16_HIGHA:
5242         case elfcpp::R_PPC64_ADDR16_HIGHER:
5243         case elfcpp::R_PPC64_ADDR16_HIGHEST:
5244         case elfcpp::R_PPC64_ADDR16_HIGHERA:
5245         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5246         case elfcpp::R_PPC64_REL64:
5247         case elfcpp::R_POWERPC_ADDR30:
5248         case elfcpp::R_PPC64_TPREL16_DS:
5249         case elfcpp::R_PPC64_TPREL16_LO_DS:
5250         case elfcpp::R_PPC64_TPREL16_HIGH:
5251         case elfcpp::R_PPC64_TPREL16_HIGHA:
5252         case elfcpp::R_PPC64_TPREL16_HIGHER:
5253         case elfcpp::R_PPC64_TPREL16_HIGHEST:
5254         case elfcpp::R_PPC64_TPREL16_HIGHERA:
5255         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5256           return;
5257
5258         default:
5259           break;
5260         }
5261     }
5262   else
5263     {
5264       switch (r_type)
5265         {
5266           // These are the relocation types supported only on 32-bit.
5267           // ??? glibc ld.so doesn't need to support these.
5268         case elfcpp::R_POWERPC_DTPREL16:
5269         case elfcpp::R_POWERPC_DTPREL16_LO:
5270         case elfcpp::R_POWERPC_DTPREL16_HI:
5271         case elfcpp::R_POWERPC_DTPREL16_HA:
5272           return;
5273
5274         default:
5275           break;
5276         }
5277     }
5278
5279   // This prevents us from issuing more than one error per reloc
5280   // section.  But we can still wind up issuing more than one
5281   // error per object file.
5282   if (this->issued_non_pic_error_)
5283     return;
5284   gold_assert(parameters->options().output_is_position_independent());
5285   object->error(_("requires unsupported dynamic reloc; "
5286                   "recompile with -fPIC"));
5287   this->issued_non_pic_error_ = true;
5288   return;
5289 }
5290
5291 // Return whether we need to make a PLT entry for a relocation of the
5292 // given type against a STT_GNU_IFUNC symbol.
5293
5294 template<int size, bool big_endian>
5295 bool
5296 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
5297      Target_powerpc<size, big_endian>* target,
5298      Sized_relobj_file<size, big_endian>* object,
5299      unsigned int r_type,
5300      bool report_err)
5301 {
5302   // In non-pic code any reference will resolve to the plt call stub
5303   // for the ifunc symbol.
5304   if ((size == 32 || target->abiversion() >= 2)
5305       && !parameters->options().output_is_position_independent())
5306     return true;
5307
5308   switch (r_type)
5309     {
5310     // Word size refs from data sections are OK, but don't need a PLT entry.
5311     case elfcpp::R_POWERPC_ADDR32:
5312     case elfcpp::R_POWERPC_UADDR32:
5313       if (size == 32)
5314         return false;
5315       break;
5316
5317     case elfcpp::R_PPC64_ADDR64:
5318     case elfcpp::R_PPC64_UADDR64:
5319       if (size == 64)
5320         return false;
5321       break;
5322
5323     // GOT refs are good, but also don't need a PLT entry.
5324     case elfcpp::R_POWERPC_GOT16:
5325     case elfcpp::R_POWERPC_GOT16_LO:
5326     case elfcpp::R_POWERPC_GOT16_HI:
5327     case elfcpp::R_POWERPC_GOT16_HA:
5328     case elfcpp::R_PPC64_GOT16_DS:
5329     case elfcpp::R_PPC64_GOT16_LO_DS:
5330       return false;
5331
5332     // Function calls are good, and these do need a PLT entry.
5333     case elfcpp::R_POWERPC_ADDR24:
5334     case elfcpp::R_POWERPC_ADDR14:
5335     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5336     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5337     case elfcpp::R_POWERPC_REL24:
5338     case elfcpp::R_PPC_PLTREL24:
5339     case elfcpp::R_POWERPC_REL14:
5340     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5341     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5342       return true;
5343
5344     default:
5345       break;
5346     }
5347
5348   // Anything else is a problem.
5349   // If we are building a static executable, the libc startup function
5350   // responsible for applying indirect function relocations is going
5351   // to complain about the reloc type.
5352   // If we are building a dynamic executable, we will have a text
5353   // relocation.  The dynamic loader will set the text segment
5354   // writable and non-executable to apply text relocations.  So we'll
5355   // segfault when trying to run the indirection function to resolve
5356   // the reloc.
5357   if (report_err)
5358     gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
5359                object->name().c_str(), r_type);
5360   return false;
5361 }
5362
5363 // Scan a relocation for a local symbol.
5364
5365 template<int size, bool big_endian>
5366 inline void
5367 Target_powerpc<size, big_endian>::Scan::local(
5368     Symbol_table* symtab,
5369     Layout* layout,
5370     Target_powerpc<size, big_endian>* target,
5371     Sized_relobj_file<size, big_endian>* object,
5372     unsigned int data_shndx,
5373     Output_section* output_section,
5374     const elfcpp::Rela<size, big_endian>& reloc,
5375     unsigned int r_type,
5376     const elfcpp::Sym<size, big_endian>& lsym,
5377     bool is_discarded)
5378 {
5379   this->maybe_skip_tls_get_addr_call(r_type, NULL);
5380
5381   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5382       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5383     {
5384       this->expect_tls_get_addr_call();
5385       const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5386       if (tls_type != tls::TLSOPT_NONE)
5387         this->skip_next_tls_get_addr_call();
5388     }
5389   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5390            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5391     {
5392       this->expect_tls_get_addr_call();
5393       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5394       if (tls_type != tls::TLSOPT_NONE)
5395         this->skip_next_tls_get_addr_call();
5396     }
5397
5398   Powerpc_relobj<size, big_endian>* ppc_object
5399     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5400
5401   if (is_discarded)
5402     {
5403       if (size == 64
5404           && data_shndx == ppc_object->opd_shndx()
5405           && r_type == elfcpp::R_PPC64_ADDR64)
5406         ppc_object->set_opd_discard(reloc.get_r_offset());
5407       return;
5408     }
5409
5410   // A local STT_GNU_IFUNC symbol may require a PLT entry.
5411   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
5412   if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5413     {
5414       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5415       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5416                           r_type, r_sym, reloc.get_r_addend());
5417       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
5418     }
5419
5420   switch (r_type)
5421     {
5422     case elfcpp::R_POWERPC_NONE:
5423     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5424     case elfcpp::R_POWERPC_GNU_VTENTRY:
5425     case elfcpp::R_PPC64_TOCSAVE:
5426     case elfcpp::R_POWERPC_TLS:
5427       break;
5428
5429     case elfcpp::R_PPC64_TOC:
5430       {
5431         Output_data_got_powerpc<size, big_endian>* got
5432           = target->got_section(symtab, layout);
5433         if (parameters->options().output_is_position_independent())
5434           {
5435             Address off = reloc.get_r_offset();
5436             if (size == 64
5437                 && target->abiversion() < 2
5438                 && data_shndx == ppc_object->opd_shndx()
5439                 && ppc_object->get_opd_discard(off - 8))
5440               break;
5441
5442             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5443             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5444             rela_dyn->add_output_section_relative(got->output_section(),
5445                                                   elfcpp::R_POWERPC_RELATIVE,
5446                                                   output_section,
5447                                                   object, data_shndx, off,
5448                                                   symobj->toc_base_offset());
5449           }
5450       }
5451       break;
5452
5453     case elfcpp::R_PPC64_ADDR64:
5454     case elfcpp::R_PPC64_UADDR64:
5455     case elfcpp::R_POWERPC_ADDR32:
5456     case elfcpp::R_POWERPC_UADDR32:
5457     case elfcpp::R_POWERPC_ADDR24:
5458     case elfcpp::R_POWERPC_ADDR16:
5459     case elfcpp::R_POWERPC_ADDR16_LO:
5460     case elfcpp::R_POWERPC_ADDR16_HI:
5461     case elfcpp::R_POWERPC_ADDR16_HA:
5462     case elfcpp::R_POWERPC_UADDR16:
5463     case elfcpp::R_PPC64_ADDR16_HIGH:
5464     case elfcpp::R_PPC64_ADDR16_HIGHA:
5465     case elfcpp::R_PPC64_ADDR16_HIGHER:
5466     case elfcpp::R_PPC64_ADDR16_HIGHERA:
5467     case elfcpp::R_PPC64_ADDR16_HIGHEST:
5468     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5469     case elfcpp::R_PPC64_ADDR16_DS:
5470     case elfcpp::R_PPC64_ADDR16_LO_DS:
5471     case elfcpp::R_POWERPC_ADDR14:
5472     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5473     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5474       // If building a shared library (or a position-independent
5475       // executable), we need to create a dynamic relocation for
5476       // this location.
5477       if (parameters->options().output_is_position_independent()
5478           || (size == 64 && is_ifunc && target->abiversion() < 2))
5479         {
5480           Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5481                                                              is_ifunc);
5482           unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5483           if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
5484               || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
5485             {
5486               unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5487                                      : elfcpp::R_POWERPC_RELATIVE);
5488               rela_dyn->add_local_relative(object, r_sym, dynrel,
5489                                            output_section, data_shndx,
5490                                            reloc.get_r_offset(),
5491                                            reloc.get_r_addend(), false);
5492             }
5493           else if (lsym.get_st_type() != elfcpp::STT_SECTION)
5494             {
5495               check_non_pic(object, r_type);
5496               rela_dyn->add_local(object, r_sym, r_type, output_section,
5497                                   data_shndx, reloc.get_r_offset(),
5498                                   reloc.get_r_addend());
5499             }
5500           else
5501             {
5502               gold_assert(lsym.get_st_value() == 0);
5503               unsigned int shndx = lsym.get_st_shndx();
5504               bool is_ordinary;
5505               shndx = object->adjust_sym_shndx(r_sym, shndx,
5506                                                &is_ordinary);
5507               if (!is_ordinary)
5508                 object->error(_("section symbol %u has bad shndx %u"),
5509                               r_sym, shndx);
5510               else
5511                 rela_dyn->add_local_section(object, shndx, r_type,
5512                                             output_section, data_shndx,
5513                                             reloc.get_r_offset());
5514             }
5515         }
5516       break;
5517
5518     case elfcpp::R_POWERPC_REL24:
5519     case elfcpp::R_PPC_PLTREL24:
5520     case elfcpp::R_PPC_LOCAL24PC:
5521     case elfcpp::R_POWERPC_REL14:
5522     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5523     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5524       if (!is_ifunc)
5525         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5526                             r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5527                             reloc.get_r_addend());
5528       break;
5529
5530     case elfcpp::R_PPC64_REL64:
5531     case elfcpp::R_POWERPC_REL32:
5532     case elfcpp::R_POWERPC_REL16:
5533     case elfcpp::R_POWERPC_REL16_LO:
5534     case elfcpp::R_POWERPC_REL16_HI:
5535     case elfcpp::R_POWERPC_REL16_HA:
5536     case elfcpp::R_POWERPC_SECTOFF:
5537     case elfcpp::R_POWERPC_SECTOFF_LO:
5538     case elfcpp::R_POWERPC_SECTOFF_HI:
5539     case elfcpp::R_POWERPC_SECTOFF_HA:
5540     case elfcpp::R_PPC64_SECTOFF_DS:
5541     case elfcpp::R_PPC64_SECTOFF_LO_DS:
5542     case elfcpp::R_POWERPC_TPREL16:
5543     case elfcpp::R_POWERPC_TPREL16_LO:
5544     case elfcpp::R_POWERPC_TPREL16_HI:
5545     case elfcpp::R_POWERPC_TPREL16_HA:
5546     case elfcpp::R_PPC64_TPREL16_DS:
5547     case elfcpp::R_PPC64_TPREL16_LO_DS:
5548     case elfcpp::R_PPC64_TPREL16_HIGH:
5549     case elfcpp::R_PPC64_TPREL16_HIGHA:
5550     case elfcpp::R_PPC64_TPREL16_HIGHER:
5551     case elfcpp::R_PPC64_TPREL16_HIGHERA:
5552     case elfcpp::R_PPC64_TPREL16_HIGHEST:
5553     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
5554     case elfcpp::R_POWERPC_DTPREL16:
5555     case elfcpp::R_POWERPC_DTPREL16_LO:
5556     case elfcpp::R_POWERPC_DTPREL16_HI:
5557     case elfcpp::R_POWERPC_DTPREL16_HA:
5558     case elfcpp::R_PPC64_DTPREL16_DS:
5559     case elfcpp::R_PPC64_DTPREL16_LO_DS:
5560     case elfcpp::R_PPC64_DTPREL16_HIGH:
5561     case elfcpp::R_PPC64_DTPREL16_HIGHA:
5562     case elfcpp::R_PPC64_DTPREL16_HIGHER:
5563     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
5564     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
5565     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
5566     case elfcpp::R_PPC64_TLSGD:
5567     case elfcpp::R_PPC64_TLSLD:
5568     case elfcpp::R_PPC64_ADDR64_LOCAL:
5569       break;
5570
5571     case elfcpp::R_POWERPC_GOT16:
5572     case elfcpp::R_POWERPC_GOT16_LO:
5573     case elfcpp::R_POWERPC_GOT16_HI:
5574     case elfcpp::R_POWERPC_GOT16_HA:
5575     case elfcpp::R_PPC64_GOT16_DS:
5576     case elfcpp::R_PPC64_GOT16_LO_DS:
5577       {
5578         // The symbol requires a GOT entry.
5579         Output_data_got_powerpc<size, big_endian>* got
5580           = target->got_section(symtab, layout);
5581         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5582
5583         if (!parameters->options().output_is_position_independent())
5584           {
5585             if ((size == 32 && is_ifunc)
5586                 || (size == 64 && target->abiversion() >= 2))
5587               got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
5588             else
5589               got->add_local(object, r_sym, GOT_TYPE_STANDARD);
5590           }
5591         else if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
5592           {
5593             // If we are generating a shared object or a pie, this
5594             // symbol's GOT entry will be set by a dynamic relocation.
5595             unsigned int off;
5596             off = got->add_constant(0);
5597             object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
5598
5599             Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
5600                                                                is_ifunc);
5601             unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5602                                    : elfcpp::R_POWERPC_RELATIVE);
5603             rela_dyn->add_local_relative(object, r_sym, dynrel,
5604                                          got, off, 0, false);
5605           }
5606       }
5607       break;
5608
5609     case elfcpp::R_PPC64_TOC16:
5610     case elfcpp::R_PPC64_TOC16_LO:
5611     case elfcpp::R_PPC64_TOC16_HI:
5612     case elfcpp::R_PPC64_TOC16_HA:
5613     case elfcpp::R_PPC64_TOC16_DS:
5614     case elfcpp::R_PPC64_TOC16_LO_DS:
5615       // We need a GOT section.
5616       target->got_section(symtab, layout);
5617       break;
5618
5619     case elfcpp::R_POWERPC_GOT_TLSGD16:
5620     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
5621     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
5622     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
5623       {
5624         const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
5625         if (tls_type == tls::TLSOPT_NONE)
5626           {
5627             Output_data_got_powerpc<size, big_endian>* got
5628               = target->got_section(symtab, layout);
5629             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5630             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5631             got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
5632                                     rela_dyn, elfcpp::R_POWERPC_DTPMOD);
5633           }
5634         else if (tls_type == tls::TLSOPT_TO_LE)
5635           {
5636             // no GOT relocs needed for Local Exec.
5637           }
5638         else
5639           gold_unreachable();
5640       }
5641       break;
5642
5643     case elfcpp::R_POWERPC_GOT_TLSLD16:
5644     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
5645     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
5646     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
5647       {
5648         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5649         if (tls_type == tls::TLSOPT_NONE)
5650           target->tlsld_got_offset(symtab, layout, object);
5651         else if (tls_type == tls::TLSOPT_TO_LE)
5652           {
5653             // no GOT relocs needed for Local Exec.
5654             if (parameters->options().emit_relocs())
5655               {
5656                 Output_section* os = layout->tls_segment()->first_section();
5657                 gold_assert(os != NULL);
5658                 os->set_needs_symtab_index();
5659               }
5660           }
5661         else
5662           gold_unreachable();
5663       }
5664       break;
5665
5666     case elfcpp::R_POWERPC_GOT_DTPREL16:
5667     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
5668     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
5669     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
5670       {
5671         Output_data_got_powerpc<size, big_endian>* got
5672           = target->got_section(symtab, layout);
5673         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5674         got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
5675       }
5676       break;
5677
5678     case elfcpp::R_POWERPC_GOT_TPREL16:
5679     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
5680     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
5681     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
5682       {
5683         const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
5684         if (tls_type == tls::TLSOPT_NONE)
5685           {
5686             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
5687             if (!object->local_has_got_offset(r_sym, GOT_TYPE_TPREL))
5688               {
5689                 Output_data_got_powerpc<size, big_endian>* got
5690                   = target->got_section(symtab, layout);
5691                 unsigned int off = got->add_constant(0);
5692                 object->set_local_got_offset(r_sym, GOT_TYPE_TPREL, off);
5693
5694                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5695                 rela_dyn->add_symbolless_local_addend(object, r_sym,
5696                                                       elfcpp::R_POWERPC_TPREL,
5697                                                       got, off, 0);
5698               }
5699           }
5700         else if (tls_type == tls::TLSOPT_TO_LE)
5701           {
5702             // no GOT relocs needed for Local Exec.
5703           }
5704         else
5705           gold_unreachable();
5706       }
5707       break;
5708
5709     default:
5710       unsupported_reloc_local(object, r_type);
5711       break;
5712     }
5713
5714   switch (r_type)
5715     {
5716     case elfcpp::R_POWERPC_GOT_TLSLD16:
5717     case elfcpp::R_POWERPC_GOT_TLSGD16:
5718     case elfcpp::R_POWERPC_GOT_TPREL16:
5719     case elfcpp::R_POWERPC_GOT_DTPREL16:
5720     case elfcpp::R_POWERPC_GOT16:
5721     case elfcpp::R_PPC64_GOT16_DS:
5722     case elfcpp::R_PPC64_TOC16:
5723     case elfcpp::R_PPC64_TOC16_DS:
5724       ppc_object->set_has_small_toc_reloc();
5725     default:
5726       break;
5727     }
5728 }
5729
5730 // Report an unsupported relocation against a global symbol.
5731
5732 template<int size, bool big_endian>
5733 void
5734 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
5735     Sized_relobj_file<size, big_endian>* object,
5736     unsigned int r_type,
5737     Symbol* gsym)
5738 {
5739   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
5740              object->name().c_str(), r_type, gsym->demangled_name().c_str());
5741 }
5742
5743 // Scan a relocation for a global symbol.
5744
5745 template<int size, bool big_endian>
5746 inline void
5747 Target_powerpc<size, big_endian>::Scan::global(
5748     Symbol_table* symtab,
5749     Layout* layout,
5750     Target_powerpc<size, big_endian>* target,
5751     Sized_relobj_file<size, big_endian>* object,
5752     unsigned int data_shndx,
5753     Output_section* output_section,
5754     const elfcpp::Rela<size, big_endian>& reloc,
5755     unsigned int r_type,
5756     Symbol* gsym)
5757 {
5758   if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
5759     return;
5760
5761   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
5762       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
5763     {
5764       this->expect_tls_get_addr_call();
5765       const bool final = gsym->final_value_is_known();
5766       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
5767       if (tls_type != tls::TLSOPT_NONE)
5768         this->skip_next_tls_get_addr_call();
5769     }
5770   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
5771            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
5772     {
5773       this->expect_tls_get_addr_call();
5774       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
5775       if (tls_type != tls::TLSOPT_NONE)
5776         this->skip_next_tls_get_addr_call();
5777     }
5778
5779   Powerpc_relobj<size, big_endian>* ppc_object
5780     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
5781
5782   // A STT_GNU_IFUNC symbol may require a PLT entry.
5783   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
5784   bool pushed_ifunc = false;
5785   if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
5786     {
5787       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5788                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5789                           reloc.get_r_addend());
5790       target->make_plt_entry(symtab, layout, gsym);
5791       pushed_ifunc = true;
5792     }
5793
5794   switch (r_type)
5795     {
5796     case elfcpp::R_POWERPC_NONE:
5797     case elfcpp::R_POWERPC_GNU_VTINHERIT:
5798     case elfcpp::R_POWERPC_GNU_VTENTRY:
5799     case elfcpp::R_PPC_LOCAL24PC:
5800     case elfcpp::R_POWERPC_TLS:
5801       break;
5802
5803     case elfcpp::R_PPC64_TOC:
5804       {
5805         Output_data_got_powerpc<size, big_endian>* got
5806           = target->got_section(symtab, layout);
5807         if (parameters->options().output_is_position_independent())
5808           {
5809             Address off = reloc.get_r_offset();
5810             if (size == 64
5811                 && data_shndx == ppc_object->opd_shndx()
5812                 && ppc_object->get_opd_discard(off - 8))
5813               break;
5814
5815             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
5816             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
5817             if (data_shndx != ppc_object->opd_shndx())
5818               symobj = static_cast
5819                 <Powerpc_relobj<size, big_endian>*>(gsym->object());
5820             rela_dyn->add_output_section_relative(got->output_section(),
5821                                                   elfcpp::R_POWERPC_RELATIVE,
5822                                                   output_section,
5823                                                   object, data_shndx, off,
5824                                                   symobj->toc_base_offset());
5825           }
5826       }
5827       break;
5828
5829     case elfcpp::R_PPC64_ADDR64:
5830       if (size == 64
5831           && target->abiversion() < 2
5832           && data_shndx == ppc_object->opd_shndx()
5833           && (gsym->is_defined_in_discarded_section()
5834               || gsym->object() != object))
5835         {
5836           ppc_object->set_opd_discard(reloc.get_r_offset());
5837           break;
5838         }
5839       // Fall thru
5840     case elfcpp::R_PPC64_UADDR64:
5841     case elfcpp::R_POWERPC_ADDR32:
5842     case elfcpp::R_POWERPC_UADDR32:
5843     case elfcpp::R_POWERPC_ADDR24:
5844     case elfcpp::R_POWERPC_ADDR16:
5845     case elfcpp::R_POWERPC_ADDR16_LO:
5846     case elfcpp::R_POWERPC_ADDR16_HI:
5847     case elfcpp::R_POWERPC_ADDR16_HA:
5848     case elfcpp::R_POWERPC_UADDR16:
5849     case elfcpp::R_PPC64_ADDR16_HIGH:
5850     case elfcpp::R_PPC64_ADDR16_HIGHA:
5851     case elfcpp::R_PPC64_ADDR16_HIGHER:
5852     case elfcpp::R_PPC64_ADDR16_HIGHERA:
5853     case elfcpp::R_PPC64_ADDR16_HIGHEST:
5854     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
5855     case elfcpp::R_PPC64_ADDR16_DS:
5856     case elfcpp::R_PPC64_ADDR16_LO_DS:
5857     case elfcpp::R_POWERPC_ADDR14:
5858     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
5859     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
5860       {
5861         // Make a PLT entry if necessary.
5862         if (gsym->needs_plt_entry())
5863           {
5864             // Since this is not a PC-relative relocation, we may be
5865             // taking the address of a function. In that case we need to
5866             // set the entry in the dynamic symbol table to the address of
5867             // the PLT call stub.
5868             bool need_ifunc_plt = false;
5869             if ((size == 32 || target->abiversion() >= 2)
5870                 && gsym->is_from_dynobj()
5871                 && !parameters->options().output_is_position_independent())
5872               {
5873                 gsym->set_needs_dynsym_value();
5874                 need_ifunc_plt = true;
5875               }
5876             if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
5877               {
5878                 target->push_branch(ppc_object, data_shndx,
5879                                     reloc.get_r_offset(), r_type,
5880                                     elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5881                                     reloc.get_r_addend());
5882                 target->make_plt_entry(symtab, layout, gsym);
5883               }
5884           }
5885         // Make a dynamic relocation if necessary.
5886         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
5887             || (size == 64 && is_ifunc && target->abiversion() < 2))
5888           {
5889             if (!parameters->options().output_is_position_independent()
5890                 && gsym->may_need_copy_reloc())
5891               {
5892                 target->copy_reloc(symtab, layout, object,
5893                                    data_shndx, output_section, gsym, reloc);
5894               }
5895             else if ((((size == 32
5896                         && r_type == elfcpp::R_POWERPC_ADDR32)
5897                        || (size == 64
5898                            && r_type == elfcpp::R_PPC64_ADDR64
5899                            && target->abiversion() >= 2))
5900                       && gsym->can_use_relative_reloc(false)
5901                       && !(gsym->visibility() == elfcpp::STV_PROTECTED
5902                            && parameters->options().shared()))
5903                      || (size == 64
5904                          && r_type == elfcpp::R_PPC64_ADDR64
5905                          && target->abiversion() < 2
5906                          && (gsym->can_use_relative_reloc(false)
5907                              || data_shndx == ppc_object->opd_shndx())))
5908               {
5909                 Reloc_section* rela_dyn
5910                   = target->rela_dyn_section(symtab, layout, is_ifunc);
5911                 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
5912                                        : elfcpp::R_POWERPC_RELATIVE);
5913                 rela_dyn->add_symbolless_global_addend(
5914                     gsym, dynrel, output_section, object, data_shndx,
5915                     reloc.get_r_offset(), reloc.get_r_addend());
5916               }
5917             else
5918               {
5919                 Reloc_section* rela_dyn
5920                   = target->rela_dyn_section(symtab, layout, is_ifunc);
5921                 check_non_pic(object, r_type);
5922                 rela_dyn->add_global(gsym, r_type, output_section,
5923                                      object, data_shndx,
5924                                      reloc.get_r_offset(),
5925                                      reloc.get_r_addend());
5926               }
5927           }
5928       }
5929       break;
5930
5931     case elfcpp::R_PPC_PLTREL24:
5932     case elfcpp::R_POWERPC_REL24:
5933       if (!is_ifunc)
5934         {
5935           target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5936                               r_type,
5937                               elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5938                               reloc.get_r_addend());
5939           if (gsym->needs_plt_entry()
5940               || (!gsym->final_value_is_known()
5941                   && (gsym->is_undefined()
5942                       || gsym->is_from_dynobj()
5943                       || gsym->is_preemptible())))
5944             target->make_plt_entry(symtab, layout, gsym);
5945         }
5946       // Fall thru
5947
5948     case elfcpp::R_PPC64_REL64:
5949     case elfcpp::R_POWERPC_REL32:
5950       // Make a dynamic relocation if necessary.
5951       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
5952         {
5953           if (!parameters->options().output_is_position_independent()
5954               && gsym->may_need_copy_reloc())
5955             {
5956               target->copy_reloc(symtab, layout, object,
5957                                  data_shndx, output_section, gsym,
5958                                  reloc);
5959             }
5960           else
5961             {
5962               Reloc_section* rela_dyn
5963                 = target->rela_dyn_section(symtab, layout, is_ifunc);
5964               check_non_pic(object, r_type);
5965               rela_dyn->add_global(gsym, r_type, output_section, object,
5966                                    data_shndx, reloc.get_r_offset(),
5967                                    reloc.get_r_addend());
5968             }
5969         }
5970       break;
5971
5972     case elfcpp::R_POWERPC_REL14:
5973     case elfcpp::R_POWERPC_REL14_BRTAKEN:
5974     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
5975       if (!is_ifunc)
5976         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
5977                             r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
5978                             reloc.get_r_addend());
5979       break;
5980
5981     case elfcpp::R_POWERPC_REL16:
5982     case elfcpp::R_POWERPC_REL16_LO:
5983     case elfcpp::R_POWERPC_REL16_HI:
5984     case elfcpp::R_POWERPC_REL16_HA:
5985     case elfcpp::R_POWERPC_SECTOFF:
5986     case elfcpp::R_POWERPC_SECTOFF_LO:
5987     case elfcpp::R_POWERPC_SECTOFF_HI:
5988     case elfcpp::R_POWERPC_SECTOFF_HA:
5989     case elfcpp::R_PPC64_SECTOFF_DS:
5990     case elfcpp::R_PPC64_SECTOFF_LO_DS:
5991     case elfcpp::R_POWERPC_TPREL16:
5992     case elfcpp::R_POWERPC_TPREL16_LO:
5993     case elfcpp::R_POWERPC_TPREL16_HI:
5994     case elfcpp::R_POWERPC_TPREL16_HA:
5995     case elfcpp::R_PPC64_TPREL16_DS:
5996     case elfcpp::R_PPC64_TPREL16_LO_DS:
5997     case elfcpp::R_PPC64_TPREL16_HIGH:
5998     case elfcpp::R_PPC64_TPREL16_HIGHA:
5999     case elfcpp::R_PPC64_TPREL16_HIGHER:
6000     case elfcpp::R_PPC64_TPREL16_HIGHERA:
6001     case elfcpp::R_PPC64_TPREL16_HIGHEST:
6002     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
6003     case elfcpp::R_POWERPC_DTPREL16:
6004     case elfcpp::R_POWERPC_DTPREL16_LO:
6005     case elfcpp::R_POWERPC_DTPREL16_HI:
6006     case elfcpp::R_POWERPC_DTPREL16_HA:
6007     case elfcpp::R_PPC64_DTPREL16_DS:
6008     case elfcpp::R_PPC64_DTPREL16_LO_DS:
6009     case elfcpp::R_PPC64_DTPREL16_HIGH:
6010     case elfcpp::R_PPC64_DTPREL16_HIGHA:
6011     case elfcpp::R_PPC64_DTPREL16_HIGHER:
6012     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
6013     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
6014     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
6015     case elfcpp::R_PPC64_TLSGD:
6016     case elfcpp::R_PPC64_TLSLD:
6017     case elfcpp::R_PPC64_ADDR64_LOCAL:
6018       break;
6019
6020     case elfcpp::R_POWERPC_GOT16:
6021     case elfcpp::R_POWERPC_GOT16_LO:
6022     case elfcpp::R_POWERPC_GOT16_HI:
6023     case elfcpp::R_POWERPC_GOT16_HA:
6024     case elfcpp::R_PPC64_GOT16_DS:
6025     case elfcpp::R_PPC64_GOT16_LO_DS:
6026       {
6027         // The symbol requires a GOT entry.
6028         Output_data_got_powerpc<size, big_endian>* got;
6029
6030         got = target->got_section(symtab, layout);
6031         if (gsym->final_value_is_known())
6032           {
6033             if ((size == 32 && is_ifunc)
6034                 || (size == 64 && target->abiversion() >= 2))
6035               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
6036             else
6037               got->add_global(gsym, GOT_TYPE_STANDARD);
6038           }
6039         else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
6040           {
6041             // If we are generating a shared object or a pie, this
6042             // symbol's GOT entry will be set by a dynamic relocation.
6043             unsigned int off = got->add_constant(0);
6044             gsym->set_got_offset(GOT_TYPE_STANDARD, off);
6045
6046             Reloc_section* rela_dyn
6047               = target->rela_dyn_section(symtab, layout, is_ifunc);
6048
6049             if (gsym->can_use_relative_reloc(false)
6050                 && !((size == 32
6051                       || target->abiversion() >= 2)
6052                      && gsym->visibility() == elfcpp::STV_PROTECTED
6053                      && parameters->options().shared()))
6054               {
6055                 unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
6056                                        : elfcpp::R_POWERPC_RELATIVE);
6057                 rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
6058               }
6059             else
6060               {
6061                 unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
6062                 rela_dyn->add_global(gsym, dynrel, got, off, 0);
6063               }
6064           }
6065       }
6066       break;
6067
6068     case elfcpp::R_PPC64_TOC16:
6069     case elfcpp::R_PPC64_TOC16_LO:
6070     case elfcpp::R_PPC64_TOC16_HI:
6071     case elfcpp::R_PPC64_TOC16_HA:
6072     case elfcpp::R_PPC64_TOC16_DS:
6073     case elfcpp::R_PPC64_TOC16_LO_DS:
6074       // We need a GOT section.
6075       target->got_section(symtab, layout);
6076       break;
6077
6078     case elfcpp::R_POWERPC_GOT_TLSGD16:
6079     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
6080     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
6081     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
6082       {
6083         const bool final = gsym->final_value_is_known();
6084         const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6085         if (tls_type == tls::TLSOPT_NONE)
6086           {
6087             Output_data_got_powerpc<size, big_endian>* got
6088               = target->got_section(symtab, layout);
6089             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6090             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
6091                                           elfcpp::R_POWERPC_DTPMOD,
6092                                           elfcpp::R_POWERPC_DTPREL);
6093           }
6094         else if (tls_type == tls::TLSOPT_TO_IE)
6095           {
6096             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6097               {
6098                 Output_data_got_powerpc<size, big_endian>* got
6099                   = target->got_section(symtab, layout);
6100                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6101                 if (gsym->is_undefined()
6102                     || gsym->is_from_dynobj())
6103                   {
6104                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6105                                              elfcpp::R_POWERPC_TPREL);
6106                   }
6107                 else
6108                   {
6109                     unsigned int off = got->add_constant(0);
6110                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
6111                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6112                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6113                                                            got, off, 0);
6114                   }
6115               }
6116           }
6117         else if (tls_type == tls::TLSOPT_TO_LE)
6118           {
6119             // no GOT relocs needed for Local Exec.
6120           }
6121         else
6122           gold_unreachable();
6123       }
6124       break;
6125
6126     case elfcpp::R_POWERPC_GOT_TLSLD16:
6127     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
6128     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
6129     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
6130       {
6131         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6132         if (tls_type == tls::TLSOPT_NONE)
6133           target->tlsld_got_offset(symtab, layout, object);
6134         else if (tls_type == tls::TLSOPT_TO_LE)
6135           {
6136             // no GOT relocs needed for Local Exec.
6137             if (parameters->options().emit_relocs())
6138               {
6139                 Output_section* os = layout->tls_segment()->first_section();
6140                 gold_assert(os != NULL);
6141                 os->set_needs_symtab_index();
6142               }
6143           }
6144         else
6145           gold_unreachable();
6146       }
6147       break;
6148
6149     case elfcpp::R_POWERPC_GOT_DTPREL16:
6150     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
6151     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
6152     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
6153       {
6154         Output_data_got_powerpc<size, big_endian>* got
6155           = target->got_section(symtab, layout);
6156         if (!gsym->final_value_is_known()
6157             && (gsym->is_from_dynobj()
6158                 || gsym->is_undefined()
6159                 || gsym->is_preemptible()))
6160           got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
6161                                    target->rela_dyn_section(layout),
6162                                    elfcpp::R_POWERPC_DTPREL);
6163         else
6164           got->add_global_tls(gsym, GOT_TYPE_DTPREL);
6165       }
6166       break;
6167
6168     case elfcpp::R_POWERPC_GOT_TPREL16:
6169     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
6170     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
6171     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
6172       {
6173         const bool final = gsym->final_value_is_known();
6174         const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6175         if (tls_type == tls::TLSOPT_NONE)
6176           {
6177             if (!gsym->has_got_offset(GOT_TYPE_TPREL))
6178               {
6179                 Output_data_got_powerpc<size, big_endian>* got
6180                   = target->got_section(symtab, layout);
6181                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
6182                 if (gsym->is_undefined()
6183                     || gsym->is_from_dynobj())
6184                   {
6185                     got->add_global_with_rel(gsym, GOT_TYPE_TPREL, rela_dyn,
6186                                              elfcpp::R_POWERPC_TPREL);
6187                   }
6188                 else
6189                   {
6190                     unsigned int off = got->add_constant(0);
6191                     gsym->set_got_offset(GOT_TYPE_TPREL, off);
6192                     unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
6193                     rela_dyn->add_symbolless_global_addend(gsym, dynrel,
6194                                                            got, off, 0);
6195                   }
6196               }
6197           }
6198         else if (tls_type == tls::TLSOPT_TO_LE)
6199           {
6200             // no GOT relocs needed for Local Exec.
6201           }
6202         else
6203           gold_unreachable();
6204       }
6205       break;
6206
6207     default:
6208       unsupported_reloc_global(object, r_type, gsym);
6209       break;
6210     }
6211
6212   switch (r_type)
6213     {
6214     case elfcpp::R_POWERPC_GOT_TLSLD16:
6215     case elfcpp::R_POWERPC_GOT_TLSGD16:
6216     case elfcpp::R_POWERPC_GOT_TPREL16:
6217     case elfcpp::R_POWERPC_GOT_DTPREL16:
6218     case elfcpp::R_POWERPC_GOT16:
6219     case elfcpp::R_PPC64_GOT16_DS:
6220     case elfcpp::R_PPC64_TOC16:
6221     case elfcpp::R_PPC64_TOC16_DS:
6222       ppc_object->set_has_small_toc_reloc();
6223     default:
6224       break;
6225     }
6226 }
6227
6228 // Process relocations for gc.
6229
6230 template<int size, bool big_endian>
6231 void
6232 Target_powerpc<size, big_endian>::gc_process_relocs(
6233     Symbol_table* symtab,
6234     Layout* layout,
6235     Sized_relobj_file<size, big_endian>* object,
6236     unsigned int data_shndx,
6237     unsigned int,
6238     const unsigned char* prelocs,
6239     size_t reloc_count,
6240     Output_section* output_section,
6241     bool needs_special_offset_handling,
6242     size_t local_symbol_count,
6243     const unsigned char* plocal_symbols)
6244 {
6245   typedef Target_powerpc<size, big_endian> Powerpc;
6246   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6247   Powerpc_relobj<size, big_endian>* ppc_object
6248     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
6249   if (size == 64)
6250     ppc_object->set_opd_valid();
6251   if (size == 64 && data_shndx == ppc_object->opd_shndx())
6252     {
6253       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
6254       for (p = ppc_object->access_from_map()->begin();
6255            p != ppc_object->access_from_map()->end();
6256            ++p)
6257         {
6258           Address dst_off = p->first;
6259           unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6260           typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
6261           for (s = p->second.begin(); s != p->second.end(); ++s)
6262             {
6263               Object* src_obj = s->first;
6264               unsigned int src_indx = s->second;
6265               symtab->gc()->add_reference(src_obj, src_indx,
6266                                           ppc_object, dst_indx);
6267             }
6268           p->second.clear();
6269         }
6270       ppc_object->access_from_map()->clear();
6271       ppc_object->process_gc_mark(symtab);
6272       // Don't look at .opd relocs as .opd will reference everything.
6273       return;
6274     }
6275
6276   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
6277                           typename Target_powerpc::Relocatable_size_for_reloc>(
6278     symtab,
6279     layout,
6280     this,
6281     object,
6282     data_shndx,
6283     prelocs,
6284     reloc_count,
6285     output_section,
6286     needs_special_offset_handling,
6287     local_symbol_count,
6288     plocal_symbols);
6289 }
6290
6291 // Handle target specific gc actions when adding a gc reference from
6292 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
6293 // and DST_OFF.  For powerpc64, this adds a referenc to the code
6294 // section of a function descriptor.
6295
6296 template<int size, bool big_endian>
6297 void
6298 Target_powerpc<size, big_endian>::do_gc_add_reference(
6299     Symbol_table* symtab,
6300     Object* src_obj,
6301     unsigned int src_shndx,
6302     Object* dst_obj,
6303     unsigned int dst_shndx,
6304     Address dst_off) const
6305 {
6306   if (size != 64 || dst_obj->is_dynamic())
6307     return;
6308
6309   Powerpc_relobj<size, big_endian>* ppc_object
6310     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
6311   if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
6312     {
6313       if (ppc_object->opd_valid())
6314         {
6315           dst_shndx = ppc_object->get_opd_ent(dst_off);
6316           symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
6317         }
6318       else
6319         {
6320           // If we haven't run scan_opd_relocs, we must delay
6321           // processing this function descriptor reference.
6322           ppc_object->add_reference(src_obj, src_shndx, dst_off);
6323         }
6324     }
6325 }
6326
6327 // Add any special sections for this symbol to the gc work list.
6328 // For powerpc64, this adds the code section of a function
6329 // descriptor.
6330
6331 template<int size, bool big_endian>
6332 void
6333 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
6334     Symbol_table* symtab,
6335     Symbol* sym) const
6336 {
6337   if (size == 64)
6338     {
6339       Powerpc_relobj<size, big_endian>* ppc_object
6340         = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
6341       bool is_ordinary;
6342       unsigned int shndx = sym->shndx(&is_ordinary);
6343       if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
6344         {
6345           Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
6346           Address dst_off = gsym->value();
6347           if (ppc_object->opd_valid())
6348             {
6349               unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
6350               symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
6351             }
6352           else
6353             ppc_object->add_gc_mark(dst_off);
6354         }
6355     }
6356 }
6357
6358 // For a symbol location in .opd, set LOC to the location of the
6359 // function entry.
6360
6361 template<int size, bool big_endian>
6362 void
6363 Target_powerpc<size, big_endian>::do_function_location(
6364     Symbol_location* loc) const
6365 {
6366   if (size == 64 && loc->shndx != 0)
6367     {
6368       if (loc->object->is_dynamic())
6369         {
6370           Powerpc_dynobj<size, big_endian>* ppc_object
6371             = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
6372           if (loc->shndx == ppc_object->opd_shndx())
6373             {
6374               Address dest_off;
6375               Address off = loc->offset - ppc_object->opd_address();
6376               loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
6377               loc->offset = dest_off;
6378             }
6379         }
6380       else
6381         {
6382           const Powerpc_relobj<size, big_endian>* ppc_object
6383             = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
6384           if (loc->shndx == ppc_object->opd_shndx())
6385             {
6386               Address dest_off;
6387               loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
6388               loc->offset = dest_off;
6389             }
6390         }
6391     }
6392 }
6393
6394 // Scan relocations for a section.
6395
6396 template<int size, bool big_endian>
6397 void
6398 Target_powerpc<size, big_endian>::scan_relocs(
6399     Symbol_table* symtab,
6400     Layout* layout,
6401     Sized_relobj_file<size, big_endian>* object,
6402     unsigned int data_shndx,
6403     unsigned int sh_type,
6404     const unsigned char* prelocs,
6405     size_t reloc_count,
6406     Output_section* output_section,
6407     bool needs_special_offset_handling,
6408     size_t local_symbol_count,
6409     const unsigned char* plocal_symbols)
6410 {
6411   typedef Target_powerpc<size, big_endian> Powerpc;
6412   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
6413
6414   if (sh_type == elfcpp::SHT_REL)
6415     {
6416       gold_error(_("%s: unsupported REL reloc section"),
6417                  object->name().c_str());
6418       return;
6419     }
6420
6421   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
6422     symtab,
6423     layout,
6424     this,
6425     object,
6426     data_shndx,
6427     prelocs,
6428     reloc_count,
6429     output_section,
6430     needs_special_offset_handling,
6431     local_symbol_count,
6432     plocal_symbols);
6433 }
6434
6435 // Functor class for processing the global symbol table.
6436 // Removes symbols defined on discarded opd entries.
6437
6438 template<bool big_endian>
6439 class Global_symbol_visitor_opd
6440 {
6441  public:
6442   Global_symbol_visitor_opd()
6443   { }
6444
6445   void
6446   operator()(Sized_symbol<64>* sym)
6447   {
6448     if (sym->has_symtab_index()
6449         || sym->source() != Symbol::FROM_OBJECT
6450         || !sym->in_real_elf())
6451       return;
6452
6453     if (sym->object()->is_dynamic())
6454       return;
6455
6456     Powerpc_relobj<64, big_endian>* symobj
6457       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
6458     if (symobj->opd_shndx() == 0)
6459       return;
6460
6461     bool is_ordinary;
6462     unsigned int shndx = sym->shndx(&is_ordinary);
6463     if (shndx == symobj->opd_shndx()
6464         && symobj->get_opd_discard(sym->value()))
6465       sym->set_symtab_index(-1U);
6466   }
6467 };
6468
6469 template<int size, bool big_endian>
6470 void
6471 Target_powerpc<size, big_endian>::define_save_restore_funcs(
6472     Layout* layout,
6473     Symbol_table* symtab)
6474 {
6475   if (size == 64)
6476     {
6477       Output_data_save_res<64, big_endian>* savres
6478         = new Output_data_save_res<64, big_endian>(symtab);
6479       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
6480                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
6481                                       savres, ORDER_TEXT, false);
6482     }
6483 }
6484
6485 // Sort linker created .got section first (for the header), then input
6486 // sections belonging to files using small model code.
6487
6488 template<bool big_endian>
6489 class Sort_toc_sections
6490 {
6491  public:
6492   bool
6493   operator()(const Output_section::Input_section& is1,
6494              const Output_section::Input_section& is2) const
6495   {
6496     if (!is1.is_input_section() && is2.is_input_section())
6497       return true;
6498     bool small1
6499       = (is1.is_input_section()
6500          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
6501              ->has_small_toc_reloc()));
6502     bool small2
6503       = (is2.is_input_section()
6504          && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
6505              ->has_small_toc_reloc()));
6506     return small1 && !small2;
6507   }
6508 };
6509
6510 // Finalize the sections.
6511
6512 template<int size, bool big_endian>
6513 void
6514 Target_powerpc<size, big_endian>::do_finalize_sections(
6515     Layout* layout,
6516     const Input_objects*,
6517     Symbol_table* symtab)
6518 {
6519   if (parameters->doing_static_link())
6520     {
6521       // At least some versions of glibc elf-init.o have a strong
6522       // reference to __rela_iplt marker syms.  A weak ref would be
6523       // better..
6524       if (this->iplt_ != NULL)
6525         {
6526           Reloc_section* rel = this->iplt_->rel_plt();
6527           symtab->define_in_output_data("__rela_iplt_start", NULL,
6528                                         Symbol_table::PREDEFINED, rel, 0, 0,
6529                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6530                                         elfcpp::STV_HIDDEN, 0, false, true);
6531           symtab->define_in_output_data("__rela_iplt_end", NULL,
6532                                         Symbol_table::PREDEFINED, rel, 0, 0,
6533                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6534                                         elfcpp::STV_HIDDEN, 0, true, true);
6535         }
6536       else
6537         {
6538           symtab->define_as_constant("__rela_iplt_start", NULL,
6539                                      Symbol_table::PREDEFINED, 0, 0,
6540                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6541                                      elfcpp::STV_HIDDEN, 0, true, false);
6542           symtab->define_as_constant("__rela_iplt_end", NULL,
6543                                      Symbol_table::PREDEFINED, 0, 0,
6544                                      elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
6545                                      elfcpp::STV_HIDDEN, 0, true, false);
6546         }
6547     }
6548
6549   if (size == 64)
6550     {
6551       typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
6552       symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
6553
6554       if (!parameters->options().relocatable())
6555         {
6556           this->define_save_restore_funcs(layout, symtab);
6557
6558           // Annoyingly, we need to make these sections now whether or
6559           // not we need them.  If we delay until do_relax then we
6560           // need to mess with the relaxation machinery checkpointing.
6561           this->got_section(symtab, layout);
6562           this->make_brlt_section(layout);
6563
6564           if (parameters->options().toc_sort())
6565             {
6566               Output_section* os = this->got_->output_section();
6567               if (os != NULL && os->input_sections().size() > 1)
6568                 std::stable_sort(os->input_sections().begin(),
6569                                  os->input_sections().end(),
6570                                  Sort_toc_sections<big_endian>());
6571             }
6572         }
6573     }
6574
6575   // Fill in some more dynamic tags.
6576   Output_data_dynamic* odyn = layout->dynamic_data();
6577   if (odyn != NULL)
6578     {
6579       const Reloc_section* rel_plt = (this->plt_ == NULL
6580                                       ? NULL
6581                                       : this->plt_->rel_plt());
6582       layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
6583                                       this->rela_dyn_, true, size == 32);
6584
6585       if (size == 32)
6586         {
6587           if (this->got_ != NULL)
6588             {
6589               this->got_->finalize_data_size();
6590               odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
6591                                             this->got_, this->got_->g_o_t());
6592             }
6593         }
6594       else
6595         {
6596           if (this->glink_ != NULL)
6597             {
6598               this->glink_->finalize_data_size();
6599               odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
6600                                             this->glink_,
6601                                             (this->glink_->pltresolve_size
6602                                              - 32));
6603             }
6604         }
6605     }
6606
6607   // Emit any relocs we saved in an attempt to avoid generating COPY
6608   // relocs.
6609   if (this->copy_relocs_.any_saved_relocs())
6610     this->copy_relocs_.emit(this->rela_dyn_section(layout));
6611 }
6612
6613 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
6614 // reloc.
6615
6616 static bool
6617 ok_lo_toc_insn(uint32_t insn)
6618 {
6619   return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
6620           || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
6621           || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
6622           || (insn & (0x3f << 26)) == 36u << 26 /* stw */
6623           || (insn & (0x3f << 26)) == 38u << 26 /* stb */
6624           || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
6625           || (insn & (0x3f << 26)) == 42u << 26 /* lha */
6626           || (insn & (0x3f << 26)) == 44u << 26 /* sth */
6627           || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
6628           || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
6629           || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
6630           || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
6631           || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
6632           || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
6633           || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
6634               && (insn & 3) != 1)
6635           || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
6636               && ((insn & 3) == 0 || (insn & 3) == 3))
6637           || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
6638 }
6639
6640 // Return the value to use for a branch relocation.
6641
6642 template<int size, bool big_endian>
6643 typename Target_powerpc<size, big_endian>::Address
6644 Target_powerpc<size, big_endian>::symval_for_branch(
6645     const Symbol_table* symtab,
6646     Address value,
6647     const Sized_symbol<size>* gsym,
6648     Powerpc_relobj<size, big_endian>* object,
6649     unsigned int *dest_shndx)
6650 {
6651   if (size == 32 || this->abiversion() >= 2)
6652     gold_unreachable();
6653   *dest_shndx = 0;
6654
6655   // If the symbol is defined in an opd section, ie. is a function
6656   // descriptor, use the function descriptor code entry address
6657   Powerpc_relobj<size, big_endian>* symobj = object;
6658   if (gsym != NULL
6659       && gsym->source() != Symbol::FROM_OBJECT)
6660     return value;
6661   if (gsym != NULL)
6662     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
6663   unsigned int shndx = symobj->opd_shndx();
6664   if (shndx == 0)
6665     return value;
6666   Address opd_addr = symobj->get_output_section_offset(shndx);
6667   if (opd_addr == invalid_address)
6668     return value;
6669   opd_addr += symobj->output_section_address(shndx);
6670   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
6671     {
6672       Address sec_off;
6673       *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
6674       if (symtab->is_section_folded(symobj, *dest_shndx))
6675         {
6676           Section_id folded
6677             = symtab->icf()->get_folded_section(symobj, *dest_shndx);
6678           symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
6679           *dest_shndx = folded.second;
6680         }
6681       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
6682       gold_assert(sec_addr != invalid_address);
6683       sec_addr += symobj->output_section(*dest_shndx)->address();
6684       value = sec_addr + sec_off;
6685     }
6686   return value;
6687 }
6688
6689 // Perform a relocation.
6690
6691 template<int size, bool big_endian>
6692 inline bool
6693 Target_powerpc<size, big_endian>::Relocate::relocate(
6694     const Relocate_info<size, big_endian>* relinfo,
6695     Target_powerpc* target,
6696     Output_section* os,
6697     size_t relnum,
6698     const elfcpp::Rela<size, big_endian>& rela,
6699     unsigned int r_type,
6700     const Sized_symbol<size>* gsym,
6701     const Symbol_value<size>* psymval,
6702     unsigned char* view,
6703     Address address,
6704     section_size_type view_size)
6705 {
6706   if (view == NULL)
6707     return true;
6708
6709   switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
6710     {
6711     case Track_tls::NOT_EXPECTED:
6712       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6713                              _("__tls_get_addr call lacks marker reloc"));
6714       break;
6715     case Track_tls::EXPECTED:
6716       // We have already complained.
6717       break;
6718     case Track_tls::SKIP:
6719       return true;
6720     case Track_tls::NORMAL:
6721       break;
6722     }
6723
6724   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
6725   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
6726   Powerpc_relobj<size, big_endian>* const object
6727     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
6728   Address value = 0;
6729   bool has_stub_value = false;
6730   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6731   if ((gsym != NULL
6732        ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
6733        : object->local_has_plt_offset(r_sym))
6734       && (!psymval->is_ifunc_symbol()
6735           || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
6736     {
6737       if (size == 64
6738           && gsym != NULL
6739           && target->abiversion() >= 2
6740           && !parameters->options().output_is_position_independent()
6741           && !is_branch_reloc(r_type))
6742         {
6743           unsigned int off = target->glink_section()->find_global_entry(gsym);
6744           gold_assert(off != (unsigned int)-1);
6745           value = target->glink_section()->global_entry_address() + off;
6746         }
6747       else
6748         {
6749           Stub_table<size, big_endian>* stub_table
6750             = object->stub_table(relinfo->data_shndx);
6751           if (stub_table == NULL)
6752             {
6753               // This is a ref from a data section to an ifunc symbol.
6754               if (target->stub_tables().size() != 0)
6755                 stub_table = target->stub_tables()[0];
6756             }
6757           gold_assert(stub_table != NULL);
6758           Address off;
6759           if (gsym != NULL)
6760             off = stub_table->find_plt_call_entry(object, gsym, r_type,
6761                                                   rela.get_r_addend());
6762           else
6763             off = stub_table->find_plt_call_entry(object, r_sym, r_type,
6764                                                   rela.get_r_addend());
6765           gold_assert(off != invalid_address);
6766           value = stub_table->stub_address() + off;
6767         }
6768       has_stub_value = true;
6769     }
6770
6771   if (r_type == elfcpp::R_POWERPC_GOT16
6772       || r_type == elfcpp::R_POWERPC_GOT16_LO
6773       || r_type == elfcpp::R_POWERPC_GOT16_HI
6774       || r_type == elfcpp::R_POWERPC_GOT16_HA
6775       || r_type == elfcpp::R_PPC64_GOT16_DS
6776       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
6777     {
6778       if (gsym != NULL)
6779         {
6780           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
6781           value = gsym->got_offset(GOT_TYPE_STANDARD);
6782         }
6783       else
6784         {
6785           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6786           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
6787           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
6788         }
6789       value -= target->got_section()->got_base_offset(object);
6790     }
6791   else if (r_type == elfcpp::R_PPC64_TOC)
6792     {
6793       value = (target->got_section()->output_section()->address()
6794                + object->toc_base_offset());
6795     }
6796   else if (gsym != NULL
6797            && (r_type == elfcpp::R_POWERPC_REL24
6798                || r_type == elfcpp::R_PPC_PLTREL24)
6799            && has_stub_value)
6800     {
6801       if (size == 64)
6802         {
6803           typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
6804           Valtype* wv = reinterpret_cast<Valtype*>(view);
6805           bool can_plt_call = false;
6806           if (rela.get_r_offset() + 8 <= view_size)
6807             {
6808               Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
6809               Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
6810               if ((insn & 1) != 0
6811                   && (insn2 == nop
6812                       || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
6813                 {
6814                   elfcpp::Swap<32, big_endian>::
6815                     writeval(wv + 1, ld_2_1 + target->stk_toc());
6816                   can_plt_call = true;
6817                 }
6818             }
6819           if (!can_plt_call)
6820             {
6821               // If we don't have a branch and link followed by a nop,
6822               // we can't go via the plt because there is no place to
6823               // put a toc restoring instruction.
6824               // Unless we know we won't be returning.
6825               if (strcmp(gsym->name(), "__libc_start_main") == 0)
6826                 can_plt_call = true;
6827             }
6828           if (!can_plt_call)
6829             {
6830               // g++ as of 20130507 emits self-calls without a
6831               // following nop.  This is arguably wrong since we have
6832               // conflicting information.  On the one hand a global
6833               // symbol and on the other a local call sequence, but
6834               // don't error for this special case.
6835               // It isn't possible to cheaply verify we have exactly
6836               // such a call.  Allow all calls to the same section.
6837               bool ok = false;
6838               Address code = value;
6839               if (gsym->source() == Symbol::FROM_OBJECT
6840                   && gsym->object() == object)
6841                 {
6842                   unsigned int dest_shndx = 0;
6843                   if (target->abiversion() < 2)
6844                     {
6845                       Address addend = rela.get_r_addend();
6846                       Address opdent = psymval->value(object, addend);
6847                       code = target->symval_for_branch(relinfo->symtab,
6848                                                        opdent, gsym, object,
6849                                                        &dest_shndx);
6850                     }
6851                   bool is_ordinary;
6852                   if (dest_shndx == 0)
6853                     dest_shndx = gsym->shndx(&is_ordinary);
6854                   ok = dest_shndx == relinfo->data_shndx;
6855                 }
6856               if (!ok)
6857                 {
6858                   gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
6859                                          _("call lacks nop, can't restore toc; "
6860                                            "recompile with -fPIC"));
6861                   value = code;
6862                 }
6863             }
6864         }
6865     }
6866   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6867            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
6868            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
6869            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
6870     {
6871       // First instruction of a global dynamic sequence, arg setup insn.
6872       const bool final = gsym == NULL || gsym->final_value_is_known();
6873       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
6874       enum Got_type got_type = GOT_TYPE_STANDARD;
6875       if (tls_type == tls::TLSOPT_NONE)
6876         got_type = GOT_TYPE_TLSGD;
6877       else if (tls_type == tls::TLSOPT_TO_IE)
6878         got_type = GOT_TYPE_TPREL;
6879       if (got_type != GOT_TYPE_STANDARD)
6880         {
6881           if (gsym != NULL)
6882             {
6883               gold_assert(gsym->has_got_offset(got_type));
6884               value = gsym->got_offset(got_type);
6885             }
6886           else
6887             {
6888               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6889               gold_assert(object->local_has_got_offset(r_sym, got_type));
6890               value = object->local_got_offset(r_sym, got_type);
6891             }
6892           value -= target->got_section()->got_base_offset(object);
6893         }
6894       if (tls_type == tls::TLSOPT_TO_IE)
6895         {
6896           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6897               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6898             {
6899               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6900               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
6901               insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
6902               if (size == 32)
6903                 insn |= 32 << 26; // lwz
6904               else
6905                 insn |= 58 << 26; // ld
6906               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6907             }
6908           r_type += (elfcpp::R_POWERPC_GOT_TPREL16
6909                      - elfcpp::R_POWERPC_GOT_TLSGD16);
6910         }
6911       else if (tls_type == tls::TLSOPT_TO_LE)
6912         {
6913           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
6914               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
6915             {
6916               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6917               Insn insn = addis_3_13;
6918               if (size == 32)
6919                 insn = addis_3_2;
6920               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6921               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6922               value = psymval->value(object, rela.get_r_addend());
6923             }
6924           else
6925             {
6926               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6927               Insn insn = nop;
6928               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6929               r_type = elfcpp::R_POWERPC_NONE;
6930             }
6931         }
6932     }
6933   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6934            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
6935            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
6936            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
6937     {
6938       // First instruction of a local dynamic sequence, arg setup insn.
6939       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
6940       if (tls_type == tls::TLSOPT_NONE)
6941         {
6942           value = target->tlsld_got_offset();
6943           value -= target->got_section()->got_base_offset(object);
6944         }
6945       else
6946         {
6947           gold_assert(tls_type == tls::TLSOPT_TO_LE);
6948           if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
6949               || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
6950             {
6951               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6952               Insn insn = addis_3_13;
6953               if (size == 32)
6954                 insn = addis_3_2;
6955               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6956               r_type = elfcpp::R_POWERPC_TPREL16_HA;
6957               value = dtp_offset;
6958             }
6959           else
6960             {
6961               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
6962               Insn insn = nop;
6963               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
6964               r_type = elfcpp::R_POWERPC_NONE;
6965             }
6966         }
6967     }
6968   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
6969            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
6970            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
6971            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
6972     {
6973       // Accesses relative to a local dynamic sequence address,
6974       // no optimisation here.
6975       if (gsym != NULL)
6976         {
6977           gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
6978           value = gsym->got_offset(GOT_TYPE_DTPREL);
6979         }
6980       else
6981         {
6982           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
6983           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
6984           value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
6985         }
6986       value -= target->got_section()->got_base_offset(object);
6987     }
6988   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
6989            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
6990            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
6991            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
6992     {
6993       // First instruction of initial exec sequence.
6994       const bool final = gsym == NULL || gsym->final_value_is_known();
6995       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
6996       if (tls_type == tls::TLSOPT_NONE)
6997         {
6998           if (gsym != NULL)
6999             {
7000               gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
7001               value = gsym->got_offset(GOT_TYPE_TPREL);
7002             }
7003           else
7004             {
7005               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
7006               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
7007               value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
7008             }
7009           value -= target->got_section()->got_base_offset(object);
7010         }
7011       else
7012         {
7013           gold_assert(tls_type == tls::TLSOPT_TO_LE);
7014           if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
7015               || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
7016             {
7017               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7018               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7019               insn &= (1 << 26) - (1 << 21); // extract rt from ld
7020               if (size == 32)
7021                 insn |= addis_0_2;
7022               else
7023                 insn |= addis_0_13;
7024               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7025               r_type = elfcpp::R_POWERPC_TPREL16_HA;
7026               value = psymval->value(object, rela.get_r_addend());
7027             }
7028           else
7029             {
7030               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7031               Insn insn = nop;
7032               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7033               r_type = elfcpp::R_POWERPC_NONE;
7034             }
7035         }
7036     }
7037   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
7038            || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
7039     {
7040       // Second instruction of a global dynamic sequence,
7041       // the __tls_get_addr call
7042       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7043       const bool final = gsym == NULL || gsym->final_value_is_known();
7044       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
7045       if (tls_type != tls::TLSOPT_NONE)
7046         {
7047           if (tls_type == tls::TLSOPT_TO_IE)
7048             {
7049               Insn* iview = reinterpret_cast<Insn*>(view);
7050               Insn insn = add_3_3_13;
7051               if (size == 32)
7052                 insn = add_3_3_2;
7053               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7054               r_type = elfcpp::R_POWERPC_NONE;
7055             }
7056           else
7057             {
7058               Insn* iview = reinterpret_cast<Insn*>(view);
7059               Insn insn = addi_3_3;
7060               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7061               r_type = elfcpp::R_POWERPC_TPREL16_LO;
7062               view += 2 * big_endian;
7063               value = psymval->value(object, rela.get_r_addend());
7064             }
7065           this->skip_next_tls_get_addr_call();
7066         }
7067     }
7068   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
7069            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
7070     {
7071       // Second instruction of a local dynamic sequence,
7072       // the __tls_get_addr call
7073       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
7074       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
7075       if (tls_type == tls::TLSOPT_TO_LE)
7076         {
7077           Insn* iview = reinterpret_cast<Insn*>(view);
7078           Insn insn = addi_3_3;
7079           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7080           this->skip_next_tls_get_addr_call();
7081           r_type = elfcpp::R_POWERPC_TPREL16_LO;
7082           view += 2 * big_endian;
7083           value = dtp_offset;
7084         }
7085     }
7086   else if (r_type == elfcpp::R_POWERPC_TLS)
7087     {
7088       // Second instruction of an initial exec sequence
7089       const bool final = gsym == NULL || gsym->final_value_is_known();
7090       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
7091       if (tls_type == tls::TLSOPT_TO_LE)
7092         {
7093           Insn* iview = reinterpret_cast<Insn*>(view);
7094           Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7095           unsigned int reg = size == 32 ? 2 : 13;
7096           insn = at_tls_transform(insn, reg);
7097           gold_assert(insn != 0);
7098           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7099           r_type = elfcpp::R_POWERPC_TPREL16_LO;
7100           view += 2 * big_endian;
7101           value = psymval->value(object, rela.get_r_addend());
7102         }
7103     }
7104   else if (!has_stub_value)
7105     {
7106       Address addend = 0;
7107       unsigned int dest_shndx;
7108       if (r_type != elfcpp::R_PPC_PLTREL24)
7109         addend = rela.get_r_addend();
7110       value = psymval->value(object, addend);
7111       if (size == 64 && is_branch_reloc(r_type))
7112         {
7113           if (target->abiversion() >= 2)
7114             {
7115               if (gsym != NULL)
7116                 value += object->ppc64_local_entry_offset(gsym);
7117               else
7118                 value += object->ppc64_local_entry_offset(r_sym);
7119             }
7120           else
7121             value = target->symval_for_branch(relinfo->symtab, value,
7122                                               gsym, object, &dest_shndx);
7123         }
7124       unsigned int max_branch_offset = 0;
7125       if (r_type == elfcpp::R_POWERPC_REL24
7126           || r_type == elfcpp::R_PPC_PLTREL24
7127           || r_type == elfcpp::R_PPC_LOCAL24PC)
7128         max_branch_offset = 1 << 25;
7129       else if (r_type == elfcpp::R_POWERPC_REL14
7130                || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
7131                || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
7132         max_branch_offset = 1 << 15;
7133       if (max_branch_offset != 0
7134           && value - address + max_branch_offset >= 2 * max_branch_offset)
7135         {
7136           Stub_table<size, big_endian>* stub_table
7137             = object->stub_table(relinfo->data_shndx);
7138           if (stub_table != NULL)
7139             {
7140               Address off = stub_table->find_long_branch_entry(object, value);
7141               if (off != invalid_address)
7142                 {
7143                   value = (stub_table->stub_address() + stub_table->plt_size()
7144                            + off);
7145                   has_stub_value = true;
7146                 }
7147             }
7148         }
7149     }
7150
7151   switch (r_type)
7152     {
7153     case elfcpp::R_PPC64_REL64:
7154     case elfcpp::R_POWERPC_REL32:
7155     case elfcpp::R_POWERPC_REL24:
7156     case elfcpp::R_PPC_PLTREL24:
7157     case elfcpp::R_PPC_LOCAL24PC:
7158     case elfcpp::R_POWERPC_REL16:
7159     case elfcpp::R_POWERPC_REL16_LO:
7160     case elfcpp::R_POWERPC_REL16_HI:
7161     case elfcpp::R_POWERPC_REL16_HA:
7162     case elfcpp::R_POWERPC_REL14:
7163     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7164     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7165       value -= address;
7166       break;
7167
7168     case elfcpp::R_PPC64_TOC16:
7169     case elfcpp::R_PPC64_TOC16_LO:
7170     case elfcpp::R_PPC64_TOC16_HI:
7171     case elfcpp::R_PPC64_TOC16_HA:
7172     case elfcpp::R_PPC64_TOC16_DS:
7173     case elfcpp::R_PPC64_TOC16_LO_DS:
7174       // Subtract the TOC base address.
7175       value -= (target->got_section()->output_section()->address()
7176                 + object->toc_base_offset());
7177       break;
7178
7179     case elfcpp::R_POWERPC_SECTOFF:
7180     case elfcpp::R_POWERPC_SECTOFF_LO:
7181     case elfcpp::R_POWERPC_SECTOFF_HI:
7182     case elfcpp::R_POWERPC_SECTOFF_HA:
7183     case elfcpp::R_PPC64_SECTOFF_DS:
7184     case elfcpp::R_PPC64_SECTOFF_LO_DS:
7185       if (os != NULL)
7186         value -= os->address();
7187       break;
7188
7189     case elfcpp::R_PPC64_TPREL16_DS:
7190     case elfcpp::R_PPC64_TPREL16_LO_DS:
7191     case elfcpp::R_PPC64_TPREL16_HIGH:
7192     case elfcpp::R_PPC64_TPREL16_HIGHA:
7193       if (size != 64)
7194         // R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
7195         break;
7196     case elfcpp::R_POWERPC_TPREL16:
7197     case elfcpp::R_POWERPC_TPREL16_LO:
7198     case elfcpp::R_POWERPC_TPREL16_HI:
7199     case elfcpp::R_POWERPC_TPREL16_HA:
7200     case elfcpp::R_POWERPC_TPREL:
7201     case elfcpp::R_PPC64_TPREL16_HIGHER:
7202     case elfcpp::R_PPC64_TPREL16_HIGHERA:
7203     case elfcpp::R_PPC64_TPREL16_HIGHEST:
7204     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7205       // tls symbol values are relative to tls_segment()->vaddr()
7206       value -= tp_offset;
7207       break;
7208
7209     case elfcpp::R_PPC64_DTPREL16_DS:
7210     case elfcpp::R_PPC64_DTPREL16_LO_DS:
7211     case elfcpp::R_PPC64_DTPREL16_HIGHER:
7212     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7213     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7214     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7215       if (size != 64)
7216         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
7217         // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
7218         break;
7219     case elfcpp::R_POWERPC_DTPREL16:
7220     case elfcpp::R_POWERPC_DTPREL16_LO:
7221     case elfcpp::R_POWERPC_DTPREL16_HI:
7222     case elfcpp::R_POWERPC_DTPREL16_HA:
7223     case elfcpp::R_POWERPC_DTPREL:
7224     case elfcpp::R_PPC64_DTPREL16_HIGH:
7225     case elfcpp::R_PPC64_DTPREL16_HIGHA:
7226       // tls symbol values are relative to tls_segment()->vaddr()
7227       value -= dtp_offset;
7228       break;
7229
7230     case elfcpp::R_PPC64_ADDR64_LOCAL:
7231       if (gsym != NULL)
7232         value += object->ppc64_local_entry_offset(gsym);
7233       else
7234         value += object->ppc64_local_entry_offset(r_sym);
7235       break;
7236
7237     default:
7238       break;
7239     }
7240
7241   Insn branch_bit = 0;
7242   switch (r_type)
7243     {
7244     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7245     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7246       branch_bit = 1 << 21;
7247     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7248     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7249       {
7250         Insn* iview = reinterpret_cast<Insn*>(view);
7251         Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7252         insn &= ~(1 << 21);
7253         insn |= branch_bit;
7254         if (this->is_isa_v2)
7255           {
7256             // Set 'a' bit.  This is 0b00010 in BO field for branch
7257             // on CR(BI) insns (BO == 001at or 011at), and 0b01000
7258             // for branch on CTR insns (BO == 1a00t or 1a01t).
7259             if ((insn & (0x14 << 21)) == (0x04 << 21))
7260               insn |= 0x02 << 21;
7261             else if ((insn & (0x14 << 21)) == (0x10 << 21))
7262               insn |= 0x08 << 21;
7263             else
7264               break;
7265           }
7266         else
7267           {
7268             // Invert 'y' bit if not the default.
7269             if (static_cast<Signed_address>(value) < 0)
7270               insn ^= 1 << 21;
7271           }
7272         elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7273       }
7274       break;
7275
7276     default:
7277       break;
7278     }
7279
7280   if (size == 64)
7281     {
7282       // Multi-instruction sequences that access the TOC can be
7283       // optimized, eg. addis ra,r2,0; addi rb,ra,x;
7284       // to             nop;           addi rb,r2,x;
7285       switch (r_type)
7286         {
7287         default:
7288           break;
7289
7290         case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7291         case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7292         case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7293         case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7294         case elfcpp::R_POWERPC_GOT16_HA:
7295         case elfcpp::R_PPC64_TOC16_HA:
7296           if (parameters->options().toc_optimize())
7297             {
7298               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7299               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7300               if ((insn & ((0x3f << 26) | 0x1f << 16))
7301                   != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
7302                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7303                                        _("toc optimization is not supported "
7304                                          "for %#08x instruction"), insn);
7305               else if (value + 0x8000 < 0x10000)
7306                 {
7307                   elfcpp::Swap<32, big_endian>::writeval(iview, nop);
7308                   return true;
7309                 }
7310             }
7311           break;
7312
7313         case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7314         case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7315         case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7316         case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7317         case elfcpp::R_POWERPC_GOT16_LO:
7318         case elfcpp::R_PPC64_GOT16_LO_DS:
7319         case elfcpp::R_PPC64_TOC16_LO:
7320         case elfcpp::R_PPC64_TOC16_LO_DS:
7321           if (parameters->options().toc_optimize())
7322             {
7323               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7324               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7325               if (!ok_lo_toc_insn(insn))
7326                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7327                                        _("toc optimization is not supported "
7328                                          "for %#08x instruction"), insn);
7329               else if (value + 0x8000 < 0x10000)
7330                 {
7331                   if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
7332                     {
7333                       // Transform addic to addi when we change reg.
7334                       insn &= ~((0x3f << 26) | (0x1f << 16));
7335                       insn |= (14u << 26) | (2 << 16);
7336                     }
7337                   else
7338                     {
7339                       insn &= ~(0x1f << 16);
7340                       insn |= 2 << 16;
7341                     }
7342                   elfcpp::Swap<32, big_endian>::writeval(iview, insn);
7343                 }
7344             }
7345           break;
7346         }
7347     }
7348
7349   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
7350   elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
7351   switch (r_type)
7352     {
7353     case elfcpp::R_POWERPC_ADDR32:
7354     case elfcpp::R_POWERPC_UADDR32:
7355       if (size == 64)
7356         overflow = Reloc::CHECK_BITFIELD;
7357       break;
7358
7359     case elfcpp::R_POWERPC_REL32:
7360       if (size == 64)
7361         overflow = Reloc::CHECK_SIGNED;
7362       break;
7363
7364     case elfcpp::R_POWERPC_UADDR16:
7365       overflow = Reloc::CHECK_BITFIELD;
7366       break;
7367
7368     case elfcpp::R_POWERPC_ADDR16:
7369       // We really should have three separate relocations,
7370       // one for 16-bit data, one for insns with 16-bit signed fields,
7371       // and one for insns with 16-bit unsigned fields.
7372       overflow = Reloc::CHECK_BITFIELD;
7373       if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
7374         overflow = Reloc::CHECK_LOW_INSN;
7375       break;
7376
7377     case elfcpp::R_POWERPC_ADDR16_HI:
7378     case elfcpp::R_POWERPC_ADDR16_HA:
7379     case elfcpp::R_POWERPC_GOT16_HI:
7380     case elfcpp::R_POWERPC_GOT16_HA:
7381     case elfcpp::R_POWERPC_PLT16_HI:
7382     case elfcpp::R_POWERPC_PLT16_HA:
7383     case elfcpp::R_POWERPC_SECTOFF_HI:
7384     case elfcpp::R_POWERPC_SECTOFF_HA:
7385     case elfcpp::R_PPC64_TOC16_HI:
7386     case elfcpp::R_PPC64_TOC16_HA:
7387     case elfcpp::R_PPC64_PLTGOT16_HI:
7388     case elfcpp::R_PPC64_PLTGOT16_HA:
7389     case elfcpp::R_POWERPC_TPREL16_HI:
7390     case elfcpp::R_POWERPC_TPREL16_HA:
7391     case elfcpp::R_POWERPC_DTPREL16_HI:
7392     case elfcpp::R_POWERPC_DTPREL16_HA:
7393     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7394     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7395     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7396     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7397     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7398     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7399     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7400     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7401     case elfcpp::R_POWERPC_REL16_HI:
7402     case elfcpp::R_POWERPC_REL16_HA:
7403       if (size != 32)
7404         overflow = Reloc::CHECK_HIGH_INSN;
7405       break;
7406
7407     case elfcpp::R_POWERPC_REL16:
7408     case elfcpp::R_PPC64_TOC16:
7409     case elfcpp::R_POWERPC_GOT16:
7410     case elfcpp::R_POWERPC_SECTOFF:
7411     case elfcpp::R_POWERPC_TPREL16:
7412     case elfcpp::R_POWERPC_DTPREL16:
7413     case elfcpp::R_POWERPC_GOT_TLSGD16:
7414     case elfcpp::R_POWERPC_GOT_TLSLD16:
7415     case elfcpp::R_POWERPC_GOT_TPREL16:
7416     case elfcpp::R_POWERPC_GOT_DTPREL16:
7417       overflow = Reloc::CHECK_LOW_INSN;
7418       break;
7419
7420     case elfcpp::R_POWERPC_ADDR24:
7421     case elfcpp::R_POWERPC_ADDR14:
7422     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7423     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7424     case elfcpp::R_PPC64_ADDR16_DS:
7425     case elfcpp::R_POWERPC_REL24:
7426     case elfcpp::R_PPC_PLTREL24:
7427     case elfcpp::R_PPC_LOCAL24PC:
7428     case elfcpp::R_PPC64_TPREL16_DS:
7429     case elfcpp::R_PPC64_DTPREL16_DS:
7430     case elfcpp::R_PPC64_TOC16_DS:
7431     case elfcpp::R_PPC64_GOT16_DS:
7432     case elfcpp::R_PPC64_SECTOFF_DS:
7433     case elfcpp::R_POWERPC_REL14:
7434     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7435     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7436       overflow = Reloc::CHECK_SIGNED;
7437       break;
7438     }
7439
7440   if (overflow == Reloc::CHECK_LOW_INSN
7441       || overflow == Reloc::CHECK_HIGH_INSN)
7442     {
7443       Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
7444       Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
7445
7446       overflow = Reloc::CHECK_SIGNED;
7447       if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
7448         overflow = Reloc::CHECK_BITFIELD;
7449       else if (overflow == Reloc::CHECK_LOW_INSN
7450                ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
7451                   || (insn & (0x3f << 26)) == 24u << 26 /* ori */
7452                   || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
7453                : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
7454                   || (insn & (0x3f << 26)) == 25u << 26 /* oris */
7455                   || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
7456         overflow = Reloc::CHECK_UNSIGNED;
7457     }
7458
7459   typename Powerpc_relocate_functions<size, big_endian>::Status status
7460     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
7461   switch (r_type)
7462     {
7463     case elfcpp::R_POWERPC_NONE:
7464     case elfcpp::R_POWERPC_TLS:
7465     case elfcpp::R_POWERPC_GNU_VTINHERIT:
7466     case elfcpp::R_POWERPC_GNU_VTENTRY:
7467       break;
7468
7469     case elfcpp::R_PPC64_ADDR64:
7470     case elfcpp::R_PPC64_REL64:
7471     case elfcpp::R_PPC64_TOC:
7472     case elfcpp::R_PPC64_ADDR64_LOCAL:
7473       Reloc::addr64(view, value);
7474       break;
7475
7476     case elfcpp::R_POWERPC_TPREL:
7477     case elfcpp::R_POWERPC_DTPREL:
7478       if (size == 64)
7479         Reloc::addr64(view, value);
7480       else
7481         status = Reloc::addr32(view, value, overflow);
7482       break;
7483
7484     case elfcpp::R_PPC64_UADDR64:
7485       Reloc::addr64_u(view, value);
7486       break;
7487
7488     case elfcpp::R_POWERPC_ADDR32:
7489       status = Reloc::addr32(view, value, overflow);
7490       break;
7491
7492     case elfcpp::R_POWERPC_REL32:
7493     case elfcpp::R_POWERPC_UADDR32:
7494       status = Reloc::addr32_u(view, value, overflow);
7495       break;
7496
7497     case elfcpp::R_POWERPC_ADDR24:
7498     case elfcpp::R_POWERPC_REL24:
7499     case elfcpp::R_PPC_PLTREL24:
7500     case elfcpp::R_PPC_LOCAL24PC:
7501       status = Reloc::addr24(view, value, overflow);
7502       break;
7503
7504     case elfcpp::R_POWERPC_GOT_DTPREL16:
7505     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
7506       if (size == 64)
7507         {
7508           status = Reloc::addr16_ds(view, value, overflow);
7509           break;
7510         }
7511     case elfcpp::R_POWERPC_ADDR16:
7512     case elfcpp::R_POWERPC_REL16:
7513     case elfcpp::R_PPC64_TOC16:
7514     case elfcpp::R_POWERPC_GOT16:
7515     case elfcpp::R_POWERPC_SECTOFF:
7516     case elfcpp::R_POWERPC_TPREL16:
7517     case elfcpp::R_POWERPC_DTPREL16:
7518     case elfcpp::R_POWERPC_GOT_TLSGD16:
7519     case elfcpp::R_POWERPC_GOT_TLSLD16:
7520     case elfcpp::R_POWERPC_GOT_TPREL16:
7521     case elfcpp::R_POWERPC_ADDR16_LO:
7522     case elfcpp::R_POWERPC_REL16_LO:
7523     case elfcpp::R_PPC64_TOC16_LO:
7524     case elfcpp::R_POWERPC_GOT16_LO:
7525     case elfcpp::R_POWERPC_SECTOFF_LO:
7526     case elfcpp::R_POWERPC_TPREL16_LO:
7527     case elfcpp::R_POWERPC_DTPREL16_LO:
7528     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
7529     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
7530     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
7531       status = Reloc::addr16(view, value, overflow);
7532       break;
7533
7534     case elfcpp::R_POWERPC_UADDR16:
7535       status = Reloc::addr16_u(view, value, overflow);
7536       break;
7537
7538     case elfcpp::R_PPC64_ADDR16_HIGH:
7539     case elfcpp::R_PPC64_TPREL16_HIGH:
7540     case elfcpp::R_PPC64_DTPREL16_HIGH:
7541       if (size == 32)
7542         // R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
7543         goto unsupp;
7544     case elfcpp::R_POWERPC_ADDR16_HI:
7545     case elfcpp::R_POWERPC_REL16_HI:
7546     case elfcpp::R_PPC64_TOC16_HI:
7547     case elfcpp::R_POWERPC_GOT16_HI:
7548     case elfcpp::R_POWERPC_SECTOFF_HI:
7549     case elfcpp::R_POWERPC_TPREL16_HI:
7550     case elfcpp::R_POWERPC_DTPREL16_HI:
7551     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
7552     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
7553     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
7554     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
7555       Reloc::addr16_hi(view, value);
7556       break;
7557
7558     case elfcpp::R_PPC64_ADDR16_HIGHA:
7559     case elfcpp::R_PPC64_TPREL16_HIGHA:
7560     case elfcpp::R_PPC64_DTPREL16_HIGHA:
7561       if (size == 32)
7562         // R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
7563         goto unsupp;
7564     case elfcpp::R_POWERPC_ADDR16_HA:
7565     case elfcpp::R_POWERPC_REL16_HA:
7566     case elfcpp::R_PPC64_TOC16_HA:
7567     case elfcpp::R_POWERPC_GOT16_HA:
7568     case elfcpp::R_POWERPC_SECTOFF_HA:
7569     case elfcpp::R_POWERPC_TPREL16_HA:
7570     case elfcpp::R_POWERPC_DTPREL16_HA:
7571     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
7572     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
7573     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
7574     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
7575       Reloc::addr16_ha(view, value);
7576       break;
7577
7578     case elfcpp::R_PPC64_DTPREL16_HIGHER:
7579       if (size == 32)
7580         // R_PPC_EMB_NADDR16_LO
7581         goto unsupp;
7582     case elfcpp::R_PPC64_ADDR16_HIGHER:
7583     case elfcpp::R_PPC64_TPREL16_HIGHER:
7584       Reloc::addr16_hi2(view, value);
7585       break;
7586
7587     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
7588       if (size == 32)
7589         // R_PPC_EMB_NADDR16_HI
7590         goto unsupp;
7591     case elfcpp::R_PPC64_ADDR16_HIGHERA:
7592     case elfcpp::R_PPC64_TPREL16_HIGHERA:
7593       Reloc::addr16_ha2(view, value);
7594       break;
7595
7596     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
7597       if (size == 32)
7598         // R_PPC_EMB_NADDR16_HA
7599         goto unsupp;
7600     case elfcpp::R_PPC64_ADDR16_HIGHEST:
7601     case elfcpp::R_PPC64_TPREL16_HIGHEST:
7602       Reloc::addr16_hi3(view, value);
7603       break;
7604
7605     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
7606       if (size == 32)
7607         // R_PPC_EMB_SDAI16
7608         goto unsupp;
7609     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7610     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7611       Reloc::addr16_ha3(view, value);
7612       break;
7613
7614     case elfcpp::R_PPC64_DTPREL16_DS:
7615     case elfcpp::R_PPC64_DTPREL16_LO_DS:
7616       if (size == 32)
7617         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
7618         goto unsupp;
7619     case elfcpp::R_PPC64_TPREL16_DS:
7620     case elfcpp::R_PPC64_TPREL16_LO_DS:
7621       if (size == 32)
7622         // R_PPC_TLSGD, R_PPC_TLSLD
7623         break;
7624     case elfcpp::R_PPC64_ADDR16_DS:
7625     case elfcpp::R_PPC64_ADDR16_LO_DS:
7626     case elfcpp::R_PPC64_TOC16_DS:
7627     case elfcpp::R_PPC64_TOC16_LO_DS:
7628     case elfcpp::R_PPC64_GOT16_DS:
7629     case elfcpp::R_PPC64_GOT16_LO_DS:
7630     case elfcpp::R_PPC64_SECTOFF_DS:
7631     case elfcpp::R_PPC64_SECTOFF_LO_DS:
7632       status = Reloc::addr16_ds(view, value, overflow);
7633       break;
7634
7635     case elfcpp::R_POWERPC_ADDR14:
7636     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7637     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7638     case elfcpp::R_POWERPC_REL14:
7639     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7640     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7641       status = Reloc::addr14(view, value, overflow);
7642       break;
7643
7644     case elfcpp::R_POWERPC_COPY:
7645     case elfcpp::R_POWERPC_GLOB_DAT:
7646     case elfcpp::R_POWERPC_JMP_SLOT:
7647     case elfcpp::R_POWERPC_RELATIVE:
7648     case elfcpp::R_POWERPC_DTPMOD:
7649     case elfcpp::R_PPC64_JMP_IREL:
7650     case elfcpp::R_POWERPC_IRELATIVE:
7651       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7652                              _("unexpected reloc %u in object file"),
7653                              r_type);
7654       break;
7655
7656     case elfcpp::R_PPC_EMB_SDA21:
7657       if (size == 32)
7658         goto unsupp;
7659       else
7660         {
7661           // R_PPC64_TOCSAVE.  For the time being this can be ignored.
7662         }
7663       break;
7664
7665     case elfcpp::R_PPC_EMB_SDA2I16:
7666     case elfcpp::R_PPC_EMB_SDA2REL:
7667       if (size == 32)
7668         goto unsupp;
7669       // R_PPC64_TLSGD, R_PPC64_TLSLD
7670       break;
7671
7672     case elfcpp::R_POWERPC_PLT32:
7673     case elfcpp::R_POWERPC_PLTREL32:
7674     case elfcpp::R_POWERPC_PLT16_LO:
7675     case elfcpp::R_POWERPC_PLT16_HI:
7676     case elfcpp::R_POWERPC_PLT16_HA:
7677     case elfcpp::R_PPC_SDAREL16:
7678     case elfcpp::R_POWERPC_ADDR30:
7679     case elfcpp::R_PPC64_PLT64:
7680     case elfcpp::R_PPC64_PLTREL64:
7681     case elfcpp::R_PPC64_PLTGOT16:
7682     case elfcpp::R_PPC64_PLTGOT16_LO:
7683     case elfcpp::R_PPC64_PLTGOT16_HI:
7684     case elfcpp::R_PPC64_PLTGOT16_HA:
7685     case elfcpp::R_PPC64_PLT16_LO_DS:
7686     case elfcpp::R_PPC64_PLTGOT16_DS:
7687     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
7688     case elfcpp::R_PPC_EMB_RELSDA:
7689     case elfcpp::R_PPC_TOC16:
7690     default:
7691     unsupp:
7692       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7693                              _("unsupported reloc %u"),
7694                              r_type);
7695       break;
7696     }
7697   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
7698       && (has_stub_value
7699           || !(gsym != NULL
7700                && gsym->is_weak_undefined()
7701                && is_branch_reloc(r_type))))
7702     {
7703       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
7704                              _("relocation overflow"));
7705       if (has_stub_value)
7706         gold_info(_("try relinking with a smaller --stub-group-size"));
7707     }
7708
7709   return true;
7710 }
7711
7712 // Relocate section data.
7713
7714 template<int size, bool big_endian>
7715 void
7716 Target_powerpc<size, big_endian>::relocate_section(
7717     const Relocate_info<size, big_endian>* relinfo,
7718     unsigned int sh_type,
7719     const unsigned char* prelocs,
7720     size_t reloc_count,
7721     Output_section* output_section,
7722     bool needs_special_offset_handling,
7723     unsigned char* view,
7724     Address address,
7725     section_size_type view_size,
7726     const Reloc_symbol_changes* reloc_symbol_changes)
7727 {
7728   typedef Target_powerpc<size, big_endian> Powerpc;
7729   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
7730   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
7731     Powerpc_comdat_behavior;
7732
7733   gold_assert(sh_type == elfcpp::SHT_RELA);
7734
7735   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
7736                          Powerpc_relocate, Powerpc_comdat_behavior>(
7737     relinfo,
7738     this,
7739     prelocs,
7740     reloc_count,
7741     output_section,
7742     needs_special_offset_handling,
7743     view,
7744     address,
7745     view_size,
7746     reloc_symbol_changes);
7747 }
7748
7749 class Powerpc_scan_relocatable_reloc
7750 {
7751 public:
7752   // Return the strategy to use for a local symbol which is not a
7753   // section symbol, given the relocation type.
7754   inline Relocatable_relocs::Reloc_strategy
7755   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
7756   {
7757     if (r_type == 0 && r_sym == 0)
7758       return Relocatable_relocs::RELOC_DISCARD;
7759     return Relocatable_relocs::RELOC_COPY;
7760   }
7761
7762   // Return the strategy to use for a local symbol which is a section
7763   // symbol, given the relocation type.
7764   inline Relocatable_relocs::Reloc_strategy
7765   local_section_strategy(unsigned int, Relobj*)
7766   {
7767     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
7768   }
7769
7770   // Return the strategy to use for a global symbol, given the
7771   // relocation type, the object, and the symbol index.
7772   inline Relocatable_relocs::Reloc_strategy
7773   global_strategy(unsigned int r_type, Relobj*, unsigned int)
7774   {
7775     if (r_type == elfcpp::R_PPC_PLTREL24)
7776       return Relocatable_relocs::RELOC_SPECIAL;
7777     return Relocatable_relocs::RELOC_COPY;
7778   }
7779 };
7780
7781 // Scan the relocs during a relocatable link.
7782
7783 template<int size, bool big_endian>
7784 void
7785 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
7786     Symbol_table* symtab,
7787     Layout* layout,
7788     Sized_relobj_file<size, big_endian>* object,
7789     unsigned int data_shndx,
7790     unsigned int sh_type,
7791     const unsigned char* prelocs,
7792     size_t reloc_count,
7793     Output_section* output_section,
7794     bool needs_special_offset_handling,
7795     size_t local_symbol_count,
7796     const unsigned char* plocal_symbols,
7797     Relocatable_relocs* rr)
7798 {
7799   gold_assert(sh_type == elfcpp::SHT_RELA);
7800
7801   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
7802                                 Powerpc_scan_relocatable_reloc>(
7803     symtab,
7804     layout,
7805     object,
7806     data_shndx,
7807     prelocs,
7808     reloc_count,
7809     output_section,
7810     needs_special_offset_handling,
7811     local_symbol_count,
7812     plocal_symbols,
7813     rr);
7814 }
7815
7816 // Emit relocations for a section.
7817 // This is a modified version of the function by the same name in
7818 // target-reloc.h.  Using relocate_special_relocatable for
7819 // R_PPC_PLTREL24 would require duplication of the entire body of the
7820 // loop, so we may as well duplicate the whole thing.
7821
7822 template<int size, bool big_endian>
7823 void
7824 Target_powerpc<size, big_endian>::relocate_relocs(
7825     const Relocate_info<size, big_endian>* relinfo,
7826     unsigned int sh_type,
7827     const unsigned char* prelocs,
7828     size_t reloc_count,
7829     Output_section* output_section,
7830     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
7831     const Relocatable_relocs* rr,
7832     unsigned char*,
7833     Address view_address,
7834     section_size_type,
7835     unsigned char* reloc_view,
7836     section_size_type reloc_view_size)
7837 {
7838   gold_assert(sh_type == elfcpp::SHT_RELA);
7839
7840   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
7841     Reltype;
7842   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
7843     Reltype_write;
7844   const int reloc_size
7845     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
7846
7847   Powerpc_relobj<size, big_endian>* const object
7848     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
7849   const unsigned int local_count = object->local_symbol_count();
7850   unsigned int got2_shndx = object->got2_shndx();
7851   Address got2_addend = 0;
7852   if (got2_shndx != 0)
7853     {
7854       got2_addend = object->get_output_section_offset(got2_shndx);
7855       gold_assert(got2_addend != invalid_address);
7856     }
7857
7858   unsigned char* pwrite = reloc_view;
7859   bool zap_next = false;
7860   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
7861     {
7862       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
7863       if (strategy == Relocatable_relocs::RELOC_DISCARD)
7864         continue;
7865
7866       Reltype reloc(prelocs);
7867       Reltype_write reloc_write(pwrite);
7868
7869       Address offset = reloc.get_r_offset();
7870       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
7871       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
7872       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
7873       const unsigned int orig_r_sym = r_sym;
7874       typename elfcpp::Elf_types<size>::Elf_Swxword addend
7875         = reloc.get_r_addend();
7876       const Symbol* gsym = NULL;
7877
7878       if (zap_next)
7879         {
7880           // We could arrange to discard these and other relocs for
7881           // tls optimised sequences in the strategy methods, but for
7882           // now do as BFD ld does.
7883           r_type = elfcpp::R_POWERPC_NONE;
7884           zap_next = false;
7885         }
7886
7887       // Get the new symbol index.
7888       if (r_sym < local_count)
7889         {
7890           switch (strategy)
7891             {
7892             case Relocatable_relocs::RELOC_COPY:
7893             case Relocatable_relocs::RELOC_SPECIAL:
7894               if (r_sym != 0)
7895                 {
7896                   r_sym = object->symtab_index(r_sym);
7897                   gold_assert(r_sym != -1U);
7898                 }
7899               break;
7900
7901             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
7902               {
7903                 // We are adjusting a section symbol.  We need to find
7904                 // the symbol table index of the section symbol for
7905                 // the output section corresponding to input section
7906                 // in which this symbol is defined.
7907                 gold_assert(r_sym < local_count);
7908                 bool is_ordinary;
7909                 unsigned int shndx =
7910                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
7911                 gold_assert(is_ordinary);
7912                 Output_section* os = object->output_section(shndx);
7913                 gold_assert(os != NULL);
7914                 gold_assert(os->needs_symtab_index());
7915                 r_sym = os->symtab_index();
7916               }
7917               break;
7918
7919             default:
7920               gold_unreachable();
7921             }
7922         }
7923       else
7924         {
7925           gsym = object->global_symbol(r_sym);
7926           gold_assert(gsym != NULL);
7927           if (gsym->is_forwarder())
7928             gsym = relinfo->symtab->resolve_forwards(gsym);
7929
7930           gold_assert(gsym->has_symtab_index());
7931           r_sym = gsym->symtab_index();
7932         }
7933
7934       // Get the new offset--the location in the output section where
7935       // this relocation should be applied.
7936       if (static_cast<Address>(offset_in_output_section) != invalid_address)
7937         offset += offset_in_output_section;
7938       else
7939         {
7940           section_offset_type sot_offset =
7941             convert_types<section_offset_type, Address>(offset);
7942           section_offset_type new_sot_offset =
7943             output_section->output_offset(object, relinfo->data_shndx,
7944                                           sot_offset);
7945           gold_assert(new_sot_offset != -1);
7946           offset = new_sot_offset;
7947         }
7948
7949       // In an object file, r_offset is an offset within the section.
7950       // In an executable or dynamic object, generated by
7951       // --emit-relocs, r_offset is an absolute address.
7952       if (!parameters->options().relocatable())
7953         {
7954           offset += view_address;
7955           if (static_cast<Address>(offset_in_output_section) != invalid_address)
7956             offset -= offset_in_output_section;
7957         }
7958
7959       // Handle the reloc addend based on the strategy.
7960       if (strategy == Relocatable_relocs::RELOC_COPY)
7961         ;
7962       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
7963         {
7964           const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
7965           addend = psymval->value(object, addend);
7966         }
7967       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
7968         {
7969           if (addend >= 32768)
7970             addend += got2_addend;
7971         }
7972       else
7973         gold_unreachable();
7974
7975       if (!parameters->options().relocatable())
7976         {
7977           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7978               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
7979               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
7980               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
7981             {
7982               // First instruction of a global dynamic sequence,
7983               // arg setup insn.
7984               const bool final = gsym == NULL || gsym->final_value_is_known();
7985               switch (this->optimize_tls_gd(final))
7986                 {
7987                 case tls::TLSOPT_TO_IE:
7988                   r_type += (elfcpp::R_POWERPC_GOT_TPREL16
7989                              - elfcpp::R_POWERPC_GOT_TLSGD16);
7990                   break;
7991                 case tls::TLSOPT_TO_LE:
7992                   if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
7993                       || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
7994                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
7995                   else
7996                     {
7997                       r_type = elfcpp::R_POWERPC_NONE;
7998                       offset -= 2 * big_endian;
7999                     }
8000                   break;
8001                 default:
8002                   break;
8003                 }
8004             }
8005           else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8006                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
8007                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
8008                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
8009             {
8010               // First instruction of a local dynamic sequence,
8011               // arg setup insn.
8012               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8013                 {
8014                   if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
8015                       || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
8016                     {
8017                       r_type = elfcpp::R_POWERPC_TPREL16_HA;
8018                       const Output_section* os = relinfo->layout->tls_segment()
8019                         ->first_section();
8020                       gold_assert(os != NULL);
8021                       gold_assert(os->needs_symtab_index());
8022                       r_sym = os->symtab_index();
8023                       addend = dtp_offset;
8024                     }
8025                   else
8026                     {
8027                       r_type = elfcpp::R_POWERPC_NONE;
8028                       offset -= 2 * big_endian;
8029                     }
8030                 }
8031             }
8032           else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8033                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
8034                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
8035                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
8036             {
8037               // First instruction of initial exec sequence.
8038               const bool final = gsym == NULL || gsym->final_value_is_known();
8039               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8040                 {
8041                   if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
8042                       || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
8043                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
8044                   else
8045                     {
8046                       r_type = elfcpp::R_POWERPC_NONE;
8047                       offset -= 2 * big_endian;
8048                     }
8049                 }
8050             }
8051           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8052                    || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8053             {
8054               // Second instruction of a global dynamic sequence,
8055               // the __tls_get_addr call
8056               const bool final = gsym == NULL || gsym->final_value_is_known();
8057               switch (this->optimize_tls_gd(final))
8058                 {
8059                 case tls::TLSOPT_TO_IE:
8060                   r_type = elfcpp::R_POWERPC_NONE;
8061                   zap_next = true;
8062                   break;
8063                 case tls::TLSOPT_TO_LE:
8064                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
8065                   offset += 2 * big_endian;
8066                   zap_next = true;
8067                   break;
8068                 default:
8069                   break;
8070                 }
8071             }
8072           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8073                    || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8074             {
8075               // Second instruction of a local dynamic sequence,
8076               // the __tls_get_addr call
8077               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
8078                 {
8079                   const Output_section* os = relinfo->layout->tls_segment()
8080                     ->first_section();
8081                   gold_assert(os != NULL);
8082                   gold_assert(os->needs_symtab_index());
8083                   r_sym = os->symtab_index();
8084                   addend = dtp_offset;
8085                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
8086                   offset += 2 * big_endian;
8087                   zap_next = true;
8088                 }
8089             }
8090           else if (r_type == elfcpp::R_POWERPC_TLS)
8091             {
8092               // Second instruction of an initial exec sequence
8093               const bool final = gsym == NULL || gsym->final_value_is_known();
8094               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
8095                 {
8096                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
8097                   offset += 2 * big_endian;
8098                 }
8099             }
8100         }
8101
8102       reloc_write.put_r_offset(offset);
8103       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
8104       reloc_write.put_r_addend(addend);
8105
8106       pwrite += reloc_size;
8107     }
8108
8109   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
8110               == reloc_view_size);
8111 }
8112
8113 // Return the value to use for a dynamic symbol which requires special
8114 // treatment.  This is how we support equality comparisons of function
8115 // pointers across shared library boundaries, as described in the
8116 // processor specific ABI supplement.
8117
8118 template<int size, bool big_endian>
8119 uint64_t
8120 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
8121 {
8122   if (size == 32)
8123     {
8124       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
8125       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8126            p != this->stub_tables_.end();
8127            ++p)
8128         {
8129           Address off = (*p)->find_plt_call_entry(gsym);
8130           if (off != invalid_address)
8131             return (*p)->stub_address() + off;
8132         }
8133     }
8134   else if (this->abiversion() >= 2)
8135     {
8136       unsigned int off = this->glink_section()->find_global_entry(gsym);
8137       if (off != (unsigned int)-1)
8138         return this->glink_section()->global_entry_address() + off;
8139     }
8140   gold_unreachable();
8141 }
8142
8143 // Return the PLT address to use for a local symbol.
8144 template<int size, bool big_endian>
8145 uint64_t
8146 Target_powerpc<size, big_endian>::do_plt_address_for_local(
8147     const Relobj* object,
8148     unsigned int symndx) const
8149 {
8150   if (size == 32)
8151     {
8152       const Sized_relobj<size, big_endian>* relobj
8153         = static_cast<const Sized_relobj<size, big_endian>*>(object);
8154       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8155            p != this->stub_tables_.end();
8156            ++p)
8157         {
8158           Address off = (*p)->find_plt_call_entry(relobj->sized_relobj(),
8159                                                   symndx);
8160           if (off != invalid_address)
8161             return (*p)->stub_address() + off;
8162         }
8163     }
8164   gold_unreachable();
8165 }
8166
8167 // Return the PLT address to use for a global symbol.
8168 template<int size, bool big_endian>
8169 uint64_t
8170 Target_powerpc<size, big_endian>::do_plt_address_for_global(
8171     const Symbol* gsym) const
8172 {
8173   if (size == 32)
8174     {
8175       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
8176            p != this->stub_tables_.end();
8177            ++p)
8178         {
8179           Address off = (*p)->find_plt_call_entry(gsym);
8180           if (off != invalid_address)
8181             return (*p)->stub_address() + off;
8182         }
8183     }
8184   else if (this->abiversion() >= 2)
8185     {
8186       unsigned int off = this->glink_section()->find_global_entry(gsym);
8187       if (off != (unsigned int)-1)
8188         return this->glink_section()->global_entry_address() + off;
8189     }
8190   gold_unreachable();
8191 }
8192
8193 // Return the offset to use for the GOT_INDX'th got entry which is
8194 // for a local tls symbol specified by OBJECT, SYMNDX.
8195 template<int size, bool big_endian>
8196 int64_t
8197 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
8198     const Relobj* object,
8199     unsigned int symndx,
8200     unsigned int got_indx) const
8201 {
8202   const Powerpc_relobj<size, big_endian>* ppc_object
8203     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
8204   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
8205     {
8206       for (Got_type got_type = GOT_TYPE_TLSGD;
8207            got_type <= GOT_TYPE_TPREL;
8208            got_type = Got_type(got_type + 1))
8209         if (ppc_object->local_has_got_offset(symndx, got_type))
8210           {
8211             unsigned int off = ppc_object->local_got_offset(symndx, got_type);
8212             if (got_type == GOT_TYPE_TLSGD)
8213               off += size / 8;
8214             if (off == got_indx * (size / 8))
8215               {
8216                 if (got_type == GOT_TYPE_TPREL)
8217                   return -tp_offset;
8218                 else
8219                   return -dtp_offset;
8220               }
8221           }
8222     }
8223   gold_unreachable();
8224 }
8225
8226 // Return the offset to use for the GOT_INDX'th got entry which is
8227 // for global tls symbol GSYM.
8228 template<int size, bool big_endian>
8229 int64_t
8230 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
8231     Symbol* gsym,
8232     unsigned int got_indx) const
8233 {
8234   if (gsym->type() == elfcpp::STT_TLS)
8235     {
8236       for (Got_type got_type = GOT_TYPE_TLSGD;
8237            got_type <= GOT_TYPE_TPREL;
8238            got_type = Got_type(got_type + 1))
8239         if (gsym->has_got_offset(got_type))
8240           {
8241             unsigned int off = gsym->got_offset(got_type);
8242             if (got_type == GOT_TYPE_TLSGD)
8243               off += size / 8;
8244             if (off == got_indx * (size / 8))
8245               {
8246                 if (got_type == GOT_TYPE_TPREL)
8247                   return -tp_offset;
8248                 else
8249                   return -dtp_offset;
8250               }
8251           }
8252     }
8253   gold_unreachable();
8254 }
8255
8256 // The selector for powerpc object files.
8257
8258 template<int size, bool big_endian>
8259 class Target_selector_powerpc : public Target_selector
8260 {
8261 public:
8262   Target_selector_powerpc()
8263     : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
8264                       size, big_endian,
8265                       (size == 64
8266                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
8267                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
8268                       (size == 64
8269                        ? (big_endian ? "elf64ppc" : "elf64lppc")
8270                        : (big_endian ? "elf32ppc" : "elf32lppc")))
8271   { }
8272
8273   virtual Target*
8274   do_instantiate_target()
8275   { return new Target_powerpc<size, big_endian>(); }
8276 };
8277
8278 Target_selector_powerpc<32, true> target_selector_ppc32;
8279 Target_selector_powerpc<32, false> target_selector_ppc32le;
8280 Target_selector_powerpc<64, true> target_selector_ppc64;
8281 Target_selector_powerpc<64, false> target_selector_ppc64le;
8282
8283 // Instantiate these constants for -O0
8284 template<int size, bool big_endian>
8285 const int Output_data_glink<size, big_endian>::pltresolve_size;
8286 template<int size, bool big_endian>
8287 const typename Output_data_glink<size, big_endian>::Address
8288   Output_data_glink<size, big_endian>::invalid_address;
8289 template<int size, bool big_endian>
8290 const typename Stub_table<size, big_endian>::Address
8291   Stub_table<size, big_endian>::invalid_address;
8292 template<int size, bool big_endian>
8293 const typename Target_powerpc<size, big_endian>::Address
8294   Target_powerpc<size, big_endian>::invalid_address;
8295
8296 } // End anonymous namespace.