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