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