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