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