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