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