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