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