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