* powerpc.cc (Powerpc_relobj::get_opd_ent): Make const.
[platform/upstream/binutils.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright 2008, 2009, 2010, 2011, 2012 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 "elfcpp.h"
27 #include "parameters.h"
28 #include "reloc.h"
29 #include "powerpc.h"
30 #include "object.h"
31 #include "symtab.h"
32 #include "layout.h"
33 #include "output.h"
34 #include "copy-relocs.h"
35 #include "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
39 #include "errors.h"
40 #include "gc.h"
41
42 namespace
43 {
44
45 using namespace gold;
46
47 template<int size, bool big_endian>
48 class Output_data_plt_powerpc;
49
50 template<int size, bool big_endian>
51 class Output_data_got_powerpc;
52
53 template<int size, bool big_endian>
54 class Output_data_glink;
55
56 template<int size, bool big_endian>
57 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
58 {
59 public:
60   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
61   typedef typename elfcpp::Elf_types<size>::Elf_Off Offset;
62
63   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
64                  const typename elfcpp::Ehdr<size, big_endian>& ehdr)
65     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
66       special_(0), opd_ent_shndx_(), opd_ent_off_()
67   { }
68
69   ~Powerpc_relobj()
70   { }
71
72   // The .got2 section shndx.
73   unsigned int
74   got2_shndx() const
75   {
76     if (size == 32)
77       return this->special_;
78     else
79       return 0;
80   }
81
82   // The .opd section shndx.
83   unsigned int
84   opd_shndx() const
85   {
86     if (size == 32)
87       return 0;
88     else
89       return this->special_;
90   }
91
92   // Init OPD entry arrays.
93   void
94   init_opd(size_t opd_size)
95   {
96     size_t count = this->opd_ent_ndx(opd_size);
97     this->opd_ent_shndx_.resize(count);
98     this->opd_ent_off_.reserve(count);
99   }
100
101   // Return section and offset of function entry for .opd + R_OFF.
102   void
103   get_opd_ent(Address r_off, unsigned int* shndx, Address* value) const
104   {
105     size_t ndx = this->opd_ent_ndx(r_off);
106     gold_assert(ndx < this->opd_ent_shndx_.size());
107     gold_assert(this->opd_ent_shndx_[ndx] != 0);
108     *shndx = this->opd_ent_shndx_[ndx];
109     *value = this->opd_ent_off_[ndx];
110   }
111
112   // Set section and offset of function entry for .opd + R_OFF.
113   void
114   set_opd_ent(Address r_off, unsigned int shndx, Address value)
115   {
116     size_t ndx = this->opd_ent_ndx(r_off);
117     gold_assert(ndx < this->opd_ent_shndx_.size());
118     this->opd_ent_shndx_[ndx] = shndx;
119     this->opd_ent_off_[ndx] = value;
120   }
121
122   // Examine .rela.opd to build info about function entry points.
123   void
124   scan_opd_relocs(size_t reloc_count,
125                   const unsigned char* prelocs,
126                   const unsigned char* plocal_syms);
127
128   void
129   do_read_relocs(Read_relocs_data*);
130
131   bool
132   do_find_special_sections(Read_symbols_data* sd);
133
134   // Return offset in output GOT section that this object will use
135   // as a TOC pointer.  Won't be just a constant with multi-toc support.
136   Address
137   toc_base_offset() const
138   { return 0x8000; }
139
140 private:
141   // Return index into opd_ent_shndx or opd_ent_off array for .opd entry
142   // at OFF.  .opd entries are 24 bytes long, but they can be spaced
143   // 16 bytes apart when the language doesn't use the last 8-byte
144   // word, the environment pointer.  Thus dividing the entry section
145   // offset by 16 will give an index into opd_ent_shndx_ and
146   // opd_ent_off_ that works for either layout of .opd.  (It leaves
147   // some elements of the vectors unused when .opd entries are spaced
148   // 24 bytes apart, but we don't know the spacing until relocations
149   // are processed, and in any case it is possible for an object to
150   // have some entries spaced 16 bytes apart and others 24 bytes apart.)
151   size_t
152   opd_ent_ndx(size_t off) const
153   { return off >> 4;}
154
155   // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
156   unsigned int special_;
157   // The first 8-byte word of an OPD entry gives the address of the
158   // entry point of the function.  Relocatable object files have a
159   // relocation on this word.  The following two vectors record the
160   // section and offset specified by these relocations.
161   std::vector<unsigned int> opd_ent_shndx_;
162   std::vector<Offset> opd_ent_off_;
163 };
164
165 template<int size, bool big_endian>
166 class Target_powerpc : public Sized_target<size, big_endian>
167 {
168  public:
169   typedef
170     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
171   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
172   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
173   static const Address invalid_address = static_cast<Address>(0) - 1;
174   // Offset of tp and dtp pointers from start of TLS block.
175   static const Address tp_offset = 0x7000;
176   static const Address dtp_offset = 0x8000;
177
178   Target_powerpc()
179     : Sized_target<size, big_endian>(&powerpc_info),
180       got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
181       copy_relocs_(elfcpp::R_POWERPC_COPY),
182       dynbss_(NULL), tlsld_got_offset_(-1U)
183   {
184   }
185
186   // Process the relocations to determine unreferenced sections for
187   // garbage collection.
188   void
189   gc_process_relocs(Symbol_table* symtab,
190                     Layout* layout,
191                     Sized_relobj_file<size, big_endian>* object,
192                     unsigned int data_shndx,
193                     unsigned int sh_type,
194                     const unsigned char* prelocs,
195                     size_t reloc_count,
196                     Output_section* output_section,
197                     bool needs_special_offset_handling,
198                     size_t local_symbol_count,
199                     const unsigned char* plocal_symbols);
200
201   // Scan the relocations to look for symbol adjustments.
202   void
203   scan_relocs(Symbol_table* symtab,
204               Layout* layout,
205               Sized_relobj_file<size, big_endian>* object,
206               unsigned int data_shndx,
207               unsigned int sh_type,
208               const unsigned char* prelocs,
209               size_t reloc_count,
210               Output_section* output_section,
211               bool needs_special_offset_handling,
212               size_t local_symbol_count,
213               const unsigned char* plocal_symbols);
214
215   // Map input .toc section to output .got section.
216   const char*
217   do_output_section_name(const Relobj*, const char* name, size_t* plen) const
218   {
219     if (size == 64 && strcmp(name, ".toc") == 0)
220       {
221         *plen = 4;
222         return ".got";
223       }
224     return NULL;
225   }
226
227   // Finalize the sections.
228   void
229   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
230
231   // Return the value to use for a dynamic which requires special
232   // treatment.
233   uint64_t
234   do_dynsym_value(const Symbol*) const;
235
236   // Relocate a section.
237   void
238   relocate_section(const Relocate_info<size, big_endian>*,
239                    unsigned int sh_type,
240                    const unsigned char* prelocs,
241                    size_t reloc_count,
242                    Output_section* output_section,
243                    bool needs_special_offset_handling,
244                    unsigned char* view,
245                    Address view_address,
246                    section_size_type view_size,
247                    const Reloc_symbol_changes*);
248
249   // Scan the relocs during a relocatable link.
250   void
251   scan_relocatable_relocs(Symbol_table* symtab,
252                           Layout* layout,
253                           Sized_relobj_file<size, big_endian>* object,
254                           unsigned int data_shndx,
255                           unsigned int sh_type,
256                           const unsigned char* prelocs,
257                           size_t reloc_count,
258                           Output_section* output_section,
259                           bool needs_special_offset_handling,
260                           size_t local_symbol_count,
261                           const unsigned char* plocal_symbols,
262                           Relocatable_relocs*);
263
264   // Emit relocations for a section.
265   void
266   relocate_relocs(const Relocate_info<size, big_endian>*,
267                   unsigned int sh_type,
268                   const unsigned char* prelocs,
269                   size_t reloc_count,
270                   Output_section* output_section,
271                   off_t offset_in_output_section,
272                   const Relocatable_relocs*,
273                   unsigned char*,
274                   Address view_address,
275                   section_size_type,
276                   unsigned char* reloc_view,
277                   section_size_type reloc_view_size);
278
279   // Return whether SYM is defined by the ABI.
280   bool
281   do_is_defined_by_abi(const Symbol* sym) const
282   {
283     return strcmp(sym->name(), "__tls_get_addr") == 0;
284   }
285
286   // Return the size of the GOT section.
287   section_size_type
288   got_size() const
289   {
290     gold_assert(this->got_ != NULL);
291     return this->got_->data_size();
292   }
293
294   // Get the PLT section.
295   const Output_data_plt_powerpc<size, big_endian>*
296   plt_section() const
297   {
298     gold_assert(this->plt_ != NULL);
299     return this->plt_;
300   }
301
302   // Get the .glink section.
303   const Output_data_glink<size, big_endian>*
304   glink_section() const
305   {
306     gold_assert(this->glink_ != NULL);
307     return this->glink_;
308   }
309
310   // Get the GOT section.
311   const Output_data_got_powerpc<size, big_endian>*
312   got_section() const
313   {
314     gold_assert(this->got_ != NULL);
315     return this->got_;
316   }
317
318   Object*
319   do_make_elf_object(const std::string&, Input_file*, off_t,
320                      const elfcpp::Ehdr<size, big_endian>&);
321
322   // Return the number of entries in the GOT.
323   unsigned int
324   got_entry_count() const
325   {
326     if (this->got_ == NULL)
327       return 0;
328     return this->got_size() / (size / 8);
329   }
330
331   // Return the number of entries in the PLT.
332   unsigned int
333   plt_entry_count() const;
334
335   // Return the offset of the first non-reserved PLT entry.
336   unsigned int
337   first_plt_entry_offset() const;
338
339   // Return the size of each PLT entry.
340   unsigned int
341   plt_entry_size() const;
342
343  private:
344
345   // The class which scans relocations.
346   class Scan
347   {
348   public:
349     Scan()
350       : issued_non_pic_error_(false)
351     { }
352
353     static inline int
354     get_reference_flags(unsigned int r_type);
355
356     inline void
357     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
358           Sized_relobj_file<size, big_endian>* object,
359           unsigned int data_shndx,
360           Output_section* output_section,
361           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
362           const elfcpp::Sym<size, big_endian>& lsym);
363
364     inline void
365     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
366            Sized_relobj_file<size, big_endian>* object,
367            unsigned int data_shndx,
368            Output_section* output_section,
369            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
370            Symbol* gsym);
371
372     inline bool
373     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
374                                         Target_powerpc* ,
375                                         Sized_relobj_file<size, big_endian>* ,
376                                         unsigned int ,
377                                         Output_section* ,
378                                         const elfcpp::Rela<size, big_endian>& ,
379                                         unsigned int ,
380                                         const elfcpp::Sym<size, big_endian>&)
381     { return false; }
382
383     inline bool
384     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
385                                          Target_powerpc* ,
386                                          Sized_relobj_file<size, big_endian>* ,
387                                          unsigned int ,
388                                          Output_section* ,
389                                          const elfcpp::Rela<size,
390                                                             big_endian>& ,
391                                          unsigned int , Symbol*)
392     { return false; }
393
394   private:
395     static void
396     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
397                             unsigned int r_type);
398
399     static void
400     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
401                              unsigned int r_type, Symbol*);
402
403     static void
404     generate_tls_call(Symbol_table* symtab, Layout* layout,
405                       Target_powerpc* target);
406
407     void
408     check_non_pic(Relobj*, unsigned int r_type);
409
410     // Whether we have issued an error about a non-PIC compilation.
411     bool issued_non_pic_error_;
412   };
413
414   Address
415   symval_for_branch(Address value, const Sized_symbol<size>* gsym,
416                     Powerpc_relobj<size, big_endian>* object,
417                     unsigned int *dest_shndx);
418
419   // The class which implements relocation.
420   class Relocate
421   {
422    public:
423     // Use 'at' branch hints when true, 'y' when false.
424     // FIXME maybe: set this with an option.
425     static const bool is_isa_v2 = true;
426
427     enum skip_tls
428     {
429       CALL_NOT_EXPECTED = 0,
430       CALL_EXPECTED = 1,
431       CALL_SKIP = 2
432     };
433
434     Relocate()
435       : call_tls_get_addr_(CALL_NOT_EXPECTED)
436     { }
437
438     ~Relocate()
439     {
440       if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
441         {
442           // FIXME: This needs to specify the location somehow.
443           gold_error(_("missing expected __tls_get_addr call"));
444         }
445     }
446
447     // Do a relocation.  Return false if the caller should not issue
448     // any warnings about this relocation.
449     inline bool
450     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
451              Output_section*, size_t relnum,
452              const elfcpp::Rela<size, big_endian>&,
453              unsigned int r_type, const Sized_symbol<size>*,
454              const Symbol_value<size>*,
455              unsigned char*,
456              typename elfcpp::Elf_types<size>::Elf_Addr,
457              section_size_type);
458
459     // This is set if we should skip the next reloc, which should be a
460     // call to __tls_get_addr.
461     enum skip_tls call_tls_get_addr_;
462   };
463
464   // A class which returns the size required for a relocation type,
465   // used while scanning relocs during a relocatable link.
466   class Relocatable_size_for_reloc
467   {
468    public:
469     unsigned int
470     get_size_for_reloc(unsigned int, Relobj*)
471     {
472       gold_unreachable();
473       return 0;
474     }
475   };
476
477   // Optimize the TLS relocation type based on what we know about the
478   // symbol.  IS_FINAL is true if the final address of this symbol is
479   // known at link time.
480
481   tls::Tls_optimization
482   optimize_tls_gd(bool is_final)
483   {
484     // If we are generating a shared library, then we can't do anything
485     // in the linker.
486     if (parameters->options().shared())
487       return tls::TLSOPT_NONE;
488
489     if (!is_final)
490       return tls::TLSOPT_TO_IE;
491     return tls::TLSOPT_TO_LE;
492   }
493
494   tls::Tls_optimization
495   optimize_tls_ld()
496   {
497     if (parameters->options().shared())
498       return tls::TLSOPT_NONE;
499
500     return tls::TLSOPT_TO_LE;
501   }
502
503   tls::Tls_optimization
504   optimize_tls_ie(bool is_final)
505   {
506     if (!is_final || parameters->options().shared())
507       return tls::TLSOPT_NONE;
508
509     return tls::TLSOPT_TO_LE;
510   }
511
512   // Get the GOT section, creating it if necessary.
513   Output_data_got_powerpc<size, big_endian>*
514   got_section(Symbol_table*, Layout*);
515
516   // Create glink.
517   void
518   make_glink_section(Layout*);
519
520   // Create the PLT section.
521   void
522   make_plt_section(Layout*);
523
524   // Create a PLT entry for a global symbol.
525   void
526   make_plt_entry(Layout*, Symbol*,
527                  const elfcpp::Rela<size, big_endian>&,
528                  const Sized_relobj<size, big_endian>* object);
529
530   // Create a GOT entry for local dynamic __tls_get_addr.
531   unsigned int
532   tlsld_got_offset(Symbol_table* symtab, Layout* layout,
533                    Sized_relobj_file<size, big_endian>* object);
534
535   unsigned int
536   tlsld_got_offset() const
537   {
538     return this->tlsld_got_offset_;
539   }
540
541   // Get the dynamic reloc section, creating it if necessary.
542   Reloc_section*
543   rela_dyn_section(Layout*);
544
545   // Copy a relocation against a global symbol.
546   void
547   copy_reloc(Symbol_table* symtab, Layout* layout,
548              Sized_relobj_file<size, big_endian>* object,
549              unsigned int shndx, Output_section* output_section,
550              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
551   {
552     this->copy_relocs_.copy_reloc(symtab, layout,
553                                   symtab->get_sized_symbol<size>(sym),
554                                   object, shndx, output_section,
555                                   reloc, this->rela_dyn_section(layout));
556   }
557
558   // Information about this specific target which we pass to the
559   // general Target structure.
560   static Target::Target_info powerpc_info;
561
562   // The types of GOT entries needed for this platform.
563   // These values are exposed to the ABI in an incremental link.
564   // Do not renumber existing values without changing the version
565   // number of the .gnu_incremental_inputs section.
566   enum Got_type
567   {
568     GOT_TYPE_STANDARD,
569     GOT_TYPE_TLSGD,     // double entry for @got@tlsgd
570     GOT_TYPE_DTPREL,    // entry for @got@dtprel
571     GOT_TYPE_TPREL      // entry for @got@tprel
572   };
573
574   // The GOT output section.
575   Output_data_got_powerpc<size, big_endian>* got_;
576   // The PLT output section.
577   Output_data_plt_powerpc<size, big_endian>* plt_;
578   // The .glink output section.
579   Output_data_glink<size, big_endian>* glink_;
580   // The dynamic reloc output section.
581   Reloc_section* rela_dyn_;
582   // Relocs saved to avoid a COPY reloc.
583   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
584   // Space for variables copied with a COPY reloc.
585   Output_data_space* dynbss_;
586   // Offset of the GOT entry for local dynamic __tls_get_addr calls.
587   unsigned int tlsld_got_offset_;
588 };
589
590 template<>
591 Target::Target_info Target_powerpc<32, true>::powerpc_info =
592 {
593   32,                   // size
594   true,                 // is_big_endian
595   elfcpp::EM_PPC,       // machine_code
596   false,                // has_make_symbol
597   false,                // has_resolve
598   false,                // has_code_fill
599   true,                 // is_default_stack_executable
600   false,                // can_icf_inline_merge_sections
601   '\0',                 // wrap_char
602   "/usr/lib/ld.so.1",   // dynamic_linker
603   0x10000000,           // default_text_segment_address
604   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
605   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
606   false,                // isolate_execinstr
607   0,                    // rosegment_gap
608   elfcpp::SHN_UNDEF,    // small_common_shndx
609   elfcpp::SHN_UNDEF,    // large_common_shndx
610   0,                    // small_common_section_flags
611   0,                    // large_common_section_flags
612   NULL,                 // attributes_section
613   NULL                  // attributes_vendor
614 };
615
616 template<>
617 Target::Target_info Target_powerpc<32, false>::powerpc_info =
618 {
619   32,                   // size
620   false,                // is_big_endian
621   elfcpp::EM_PPC,       // machine_code
622   false,                // has_make_symbol
623   false,                // has_resolve
624   false,                // has_code_fill
625   true,                 // is_default_stack_executable
626   false,                // can_icf_inline_merge_sections
627   '\0',                 // wrap_char
628   "/usr/lib/ld.so.1",   // dynamic_linker
629   0x10000000,           // default_text_segment_address
630   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
631   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
632   false,                // isolate_execinstr
633   0,                    // rosegment_gap
634   elfcpp::SHN_UNDEF,    // small_common_shndx
635   elfcpp::SHN_UNDEF,    // large_common_shndx
636   0,                    // small_common_section_flags
637   0,                    // large_common_section_flags
638   NULL,                 // attributes_section
639   NULL                  // attributes_vendor
640 };
641
642 template<>
643 Target::Target_info Target_powerpc<64, true>::powerpc_info =
644 {
645   64,                   // size
646   true,                 // is_big_endian
647   elfcpp::EM_PPC64,     // machine_code
648   false,                // has_make_symbol
649   false,                // has_resolve
650   false,                // has_code_fill
651   true,                 // is_default_stack_executable
652   false,                // can_icf_inline_merge_sections
653   '\0',                 // wrap_char
654   "/usr/lib/ld.so.1",   // dynamic_linker
655   0x10000000,           // default_text_segment_address
656   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
657   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
658   false,                // isolate_execinstr
659   0,                    // rosegment_gap
660   elfcpp::SHN_UNDEF,    // small_common_shndx
661   elfcpp::SHN_UNDEF,    // large_common_shndx
662   0,                    // small_common_section_flags
663   0,                    // large_common_section_flags
664   NULL,                 // attributes_section
665   NULL                  // attributes_vendor
666 };
667
668 template<>
669 Target::Target_info Target_powerpc<64, false>::powerpc_info =
670 {
671   64,                   // size
672   false,                // is_big_endian
673   elfcpp::EM_PPC64,     // machine_code
674   false,                // has_make_symbol
675   false,                // has_resolve
676   false,                // has_code_fill
677   true,                 // is_default_stack_executable
678   false,                // can_icf_inline_merge_sections
679   '\0',                 // wrap_char
680   "/usr/lib/ld.so.1",   // dynamic_linker
681   0x10000000,           // default_text_segment_address
682   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
683   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
684   false,                // isolate_execinstr
685   0,                    // rosegment_gap
686   elfcpp::SHN_UNDEF,    // small_common_shndx
687   elfcpp::SHN_UNDEF,    // large_common_shndx
688   0,                    // small_common_section_flags
689   0,                    // large_common_section_flags
690   NULL,                 // attributes_section
691   NULL                  // attributes_vendor
692 };
693
694 inline bool
695 is_branch_reloc(unsigned int r_type)
696 {
697   return (r_type == elfcpp::R_POWERPC_REL24
698           || r_type == elfcpp::R_PPC_PLTREL24
699           || r_type == elfcpp::R_PPC_LOCAL24PC
700           || r_type == elfcpp::R_POWERPC_REL14
701           || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
702           || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
703           || r_type == elfcpp::R_POWERPC_ADDR24
704           || r_type == elfcpp::R_POWERPC_ADDR14
705           || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
706           || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
707 }
708
709 // If INSN is an opcode that may be used with an @tls operand, return
710 // the transformed insn for TLS optimisation, otherwise return 0.  If
711 // REG is non-zero only match an insn with RB or RA equal to REG.
712 uint32_t
713 at_tls_transform(uint32_t insn, unsigned int reg)
714 {
715   if ((insn & (0x3f << 26)) != 31 << 26)
716     return 0;
717
718   unsigned int rtra;
719   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
720     rtra = insn & ((1 << 26) - (1 << 16));
721   else if (((insn >> 16) & 0x1f) == reg)
722     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
723   else
724     return 0;
725
726   if ((insn & (0x3ff << 1)) == 266 << 1)
727     // add -> addi
728     insn = 14 << 26;
729   else if ((insn & (0x1f << 1)) == 23 << 1
730            && ((insn & (0x1f << 6)) < 14 << 6
731                || ((insn & (0x1f << 6)) >= 16 << 6
732                    && (insn & (0x1f << 6)) < 24 << 6)))
733     // load and store indexed -> dform
734     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
735   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
736     // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
737     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
738   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
739     // lwax -> lwa
740     insn = (58 << 26) | 2;
741   else
742     return 0;
743   insn |= rtra;
744   return insn;
745 }
746
747 // Modified version of symtab.h class Symbol member
748 // Given a direct absolute or pc-relative static relocation against
749 // the global symbol, this function returns whether a dynamic relocation
750 // is needed.
751
752 template<int size>
753 bool
754 needs_dynamic_reloc(const Symbol* gsym, int flags)
755 {
756   // No dynamic relocations in a static link!
757   if (parameters->doing_static_link())
758     return false;
759
760   // A reference to an undefined symbol from an executable should be
761   // statically resolved to 0, and does not need a dynamic relocation.
762   // This matches gnu ld behavior.
763   if (gsym->is_undefined() && !parameters->options().shared())
764     return false;
765
766   // A reference to an absolute symbol does not need a dynamic relocation.
767   if (gsym->is_absolute())
768     return false;
769
770   // An absolute reference within a position-independent output file
771   // will need a dynamic relocation.
772   if ((flags & Symbol::ABSOLUTE_REF)
773       && parameters->options().output_is_position_independent())
774     return true;
775
776   // A function call that can branch to a local PLT entry does not need
777   // a dynamic relocation.
778   if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
779     return false;
780
781   // A reference to any PLT entry in a non-position-independent executable
782   // does not need a dynamic relocation.
783   // Except due to having function descriptors on powerpc64 we don't define
784   // functions to their plt code in an executable, so this doesn't apply.
785   if (size == 32
786       && !parameters->options().output_is_position_independent()
787       && gsym->has_plt_offset())
788     return false;
789
790   // A reference to a symbol defined in a dynamic object or to a
791   // symbol that is preemptible will need a dynamic relocation.
792   if (gsym->is_from_dynobj()
793       || gsym->is_undefined()
794       || gsym->is_preemptible())
795     return true;
796
797   // For all other cases, return FALSE.
798   return false;
799 }
800
801 // Modified version of symtab.h class Symbol member
802 // Whether we should use the PLT offset associated with a symbol for
803 // a relocation.  FLAGS is a set of Reference_flags.
804
805 template<int size>
806 bool
807 use_plt_offset(const Symbol* gsym, int flags)
808 {
809   // If the symbol doesn't have a PLT offset, then naturally we
810   // don't want to use it.
811   if (!gsym->has_plt_offset())
812     return false;
813
814   // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
815   if (gsym->type() == elfcpp::STT_GNU_IFUNC)
816     return true;
817
818   // If we are going to generate a dynamic relocation, then we will
819   // wind up using that, so no need to use the PLT entry.
820   if (needs_dynamic_reloc<size>(gsym, flags))
821     return false;
822
823   // If the symbol is from a dynamic object, we need to use the PLT
824   // entry.
825   if (gsym->is_from_dynobj())
826     return true;
827
828   // If we are generating a shared object, and gsym symbol is
829   // undefined or preemptible, we need to use the PLT entry.
830   if (parameters->options().shared()
831       && (gsym->is_undefined() || gsym->is_preemptible()))
832     return true;
833
834   // If gsym is a call to a weak undefined symbol, we need to use
835   // the PLT entry; the symbol may be defined by a library loaded
836   // at runtime.
837   if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
838     return true;
839
840   // Otherwise we can use the regular definition.
841   return false;
842 }
843
844 template<int size, bool big_endian>
845 class Powerpc_relocate_functions
846 {
847 public:
848   enum overflow_check
849   {
850     check_none,
851     check_signed,
852     check_bitfield
853   };
854
855   typedef enum overflow_status
856   {
857     status_ok,
858     status_overflow
859   } Status;
860
861 private:
862   typedef Powerpc_relocate_functions<size, big_endian> This;
863   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
864
865   template<int valsize>
866   static inline bool
867   has_overflow_signed(Address value)
868   {
869     // limit = 1 << (valsize - 1) without shift count exceeding size of type
870     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
871     limit <<= ((valsize - 1) >> 1);
872     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
873     return value + limit > (limit << 1) - 1;
874   }
875
876   template<int valsize>
877   static inline bool
878   has_overflow_bitfield(Address value)
879   {
880     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
881     limit <<= ((valsize - 1) >> 1);
882     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
883     return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
884   }
885
886   template<int valsize>
887   static inline enum overflow_status
888   overflowed(Address value, enum overflow_check overflow)
889   {
890     if (overflow == check_signed)
891       {
892         if (has_overflow_signed<valsize>(value))
893           return status_overflow;
894       }
895     else if (overflow == check_bitfield)
896       {
897         if (has_overflow_bitfield<valsize>(value))
898           return status_overflow;
899       }
900     return status_ok;
901   }
902
903   // Do a simple RELA relocation
904   template<int valsize>
905   static inline enum overflow_status
906   rela(unsigned char* view, Address value, enum overflow_check overflow)
907   {
908     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
909     Valtype* wv = reinterpret_cast<Valtype*>(view);
910     elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
911     return overflowed<valsize>(value, overflow);
912   }
913
914   template<int valsize>
915   static inline enum overflow_status
916   rela(unsigned char* view,
917        unsigned int right_shift,
918        typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
919        Address value,
920        enum overflow_check overflow)
921   {
922     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
923     Valtype* wv = reinterpret_cast<Valtype*>(view);
924     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
925     Valtype reloc = value >> right_shift;
926     val &= ~dst_mask;
927     reloc &= dst_mask;
928     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
929     return overflowed<valsize>(value >> right_shift, overflow);
930   }
931
932   // Do a simple RELA relocation, unaligned.
933   template<int valsize>
934   static inline enum overflow_status
935   rela_ua(unsigned char* view, Address value, enum overflow_check overflow)
936   {
937     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
938     return overflowed<valsize>(value, overflow);
939   }
940
941   template<int valsize>
942   static inline enum overflow_status
943   rela_ua(unsigned char* view,
944           unsigned int right_shift,
945           typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
946           Address value,
947           enum overflow_check overflow)
948   {
949     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
950       Valtype;
951     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
952     Valtype reloc = value >> right_shift;
953     val &= ~dst_mask;
954     reloc &= dst_mask;
955     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
956     return overflowed<valsize>(value >> right_shift, overflow);
957   }
958
959 public:
960   // R_PPC64_ADDR64: (Symbol + Addend)
961   static inline void
962   addr64(unsigned char* view, Address value)
963   { This::template rela<64>(view, value, check_none); }
964
965   // R_PPC64_UADDR64: (Symbol + Addend) unaligned
966   static inline void
967   addr64_u(unsigned char* view, Address value)
968   { This::template rela_ua<64>(view, value, check_none); }
969
970   // R_POWERPC_ADDR32: (Symbol + Addend)
971   static inline enum overflow_status
972   addr32(unsigned char* view, Address value, enum overflow_check overflow)
973   { return This::template rela<32>(view, value, overflow); }
974
975   // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
976   static inline enum overflow_status
977   addr32_u(unsigned char* view, Address value, enum overflow_check overflow)
978   { return This::template rela_ua<32>(view, value, overflow); }
979
980   // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
981   static inline enum overflow_status
982   addr24(unsigned char* view, Address value, enum overflow_check overflow)
983   {
984     enum overflow_status stat
985       = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
986     if (overflow != check_none && (value & 3) != 0)
987       stat = status_overflow;
988     return stat;
989   }
990
991   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
992   static inline enum overflow_status
993   addr16(unsigned char* view, Address value, enum overflow_check overflow)
994   { return This::template rela<16>(view, value, overflow); }
995
996   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
997   static inline enum overflow_status
998   addr16_u(unsigned char* view, Address value, enum overflow_check overflow)
999   { return This::template rela_ua<16>(view, value, overflow); }
1000
1001   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1002   static inline enum overflow_status
1003   addr16_ds(unsigned char* view, Address value, enum overflow_check overflow)
1004   {
1005     enum overflow_status stat
1006       = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1007     if (overflow != check_none && (value & 3) != 0)
1008       stat = status_overflow;
1009     return stat;
1010   }
1011
1012   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1013   static inline void
1014   addr16_hi(unsigned char* view, Address value)
1015   { This::template rela<16>(view, 16, 0xffff, value, check_none); }
1016
1017   // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1018   static inline void
1019   addr16_ha(unsigned char* view, Address value)
1020   { This::addr16_hi(view, value + 0x8000); }
1021
1022   // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1023   static inline void
1024   addr16_hi2(unsigned char* view, Address value)
1025   { This::template rela<16>(view, 32, 0xffff, value, check_none); }
1026
1027   // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1028   static inline void
1029   addr16_ha2(unsigned char* view, Address value)
1030   { This::addr16_hi2(view, value + 0x8000); }
1031
1032   // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1033   static inline void
1034   addr16_hi3(unsigned char* view, Address value)
1035   { This::template rela<16>(view, 48, 0xffff, value, check_none); }
1036
1037   // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1038   static inline void
1039   addr16_ha3(unsigned char* view, Address value)
1040   { This::addr16_hi3(view, value + 0x8000); }
1041
1042   // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1043   static inline enum overflow_status
1044   addr14(unsigned char* view, Address value, enum overflow_check overflow)
1045   {
1046     enum overflow_status stat
1047       = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1048     if (overflow != check_none && (value & 3) != 0)
1049       stat = status_overflow;
1050     return stat;
1051   }
1052 };
1053
1054 // Stash away the index of .got2 or .opd in a relocatable object, if
1055 // such a section exists.
1056
1057 template<int size, bool big_endian>
1058 bool
1059 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1060     Read_symbols_data* sd)
1061 {
1062   const unsigned char* const pshdrs = sd->section_headers->data();
1063   const unsigned char* namesu = sd->section_names->data();
1064   const char* names = reinterpret_cast<const char*>(namesu);
1065   section_size_type names_size = sd->section_names_size;
1066   const unsigned char* s;
1067
1068   s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1069                       names, names_size, NULL);
1070   if (s != NULL)
1071     {
1072       unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1073       this->special_ = ndx;
1074     }
1075   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1076 }
1077
1078 // Examine .rela.opd to build info about function entry points.
1079
1080 template<int size, bool big_endian>
1081 void
1082 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1083     size_t reloc_count,
1084     const unsigned char* prelocs,
1085     const unsigned char* plocal_syms)
1086 {
1087   if (size == 64)
1088     {
1089       typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1090         Reltype;
1091       const int reloc_size
1092         = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1093       const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1094
1095       for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1096         {
1097           Reltype reloc(prelocs);
1098           typename elfcpp::Elf_types<size>::Elf_WXword r_info
1099             = reloc.get_r_info();
1100           unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1101           if (r_type == elfcpp::R_PPC64_ADDR64)
1102             {
1103               unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1104               typename elfcpp::Elf_types<size>::Elf_Addr value;
1105               bool is_ordinary;
1106               unsigned int shndx;
1107               if (r_sym < this->local_symbol_count())
1108                 {
1109                   typename elfcpp::Sym<size, big_endian>
1110                     lsym(plocal_syms + r_sym * sym_size);
1111                   shndx = lsym.get_st_shndx();
1112                   shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1113                   value = lsym.get_st_value();
1114                 }
1115               else
1116                 shndx = this->symbol_section_and_value(r_sym, &value,
1117                                                        &is_ordinary);
1118               this->set_opd_ent(reloc.get_r_offset(), shndx,
1119                                 value + reloc.get_r_addend());
1120             }
1121         }
1122     }
1123 }
1124
1125 template<int size, bool big_endian>
1126 void
1127 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1128 {
1129   Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1130   if (size == 64)
1131     {
1132       for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1133            p != rd->relocs.end();
1134            ++p)
1135         {
1136           if (p->data_shndx == this->opd_shndx())
1137             {
1138               this->init_opd(this->section_size(this->opd_shndx()));
1139               this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1140                                     rd->local_symbols->data());
1141               break;
1142             }
1143         }
1144     }
1145 }
1146
1147 // Set up PowerPC target specific relobj.
1148
1149 template<int size, bool big_endian>
1150 Object*
1151 Target_powerpc<size, big_endian>::do_make_elf_object(
1152     const std::string& name,
1153     Input_file* input_file,
1154     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1155 {
1156   int et = ehdr.get_e_type();
1157   // ET_EXEC files are valid input for --just-symbols/-R,
1158   // and we treat them as relocatable objects.
1159   if (et == elfcpp::ET_REL
1160       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1161     {
1162       Powerpc_relobj<size, big_endian>* obj =
1163         new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1164       obj->setup();
1165       return obj;
1166     }
1167   else if (et == elfcpp::ET_DYN)
1168     {
1169       Sized_dynobj<size, big_endian>* obj =
1170         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1171       obj->setup();
1172       return obj;
1173     }
1174   else
1175     {
1176       gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1177       return NULL;
1178     }
1179 }
1180
1181 template<int size, bool big_endian>
1182 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1183 {
1184 public:
1185   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1186   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1187
1188   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1189     : Output_data_got<size, big_endian>(),
1190       symtab_(symtab), layout_(layout),
1191       header_ent_cnt_(size == 32 ? 3 : 1),
1192       header_index_(size == 32 ? 0x2000 : 0)
1193   {}
1194
1195   class Got_entry;
1196
1197   // Create a new GOT entry and return its offset.
1198   unsigned int
1199   add_got_entry(Got_entry got_entry)
1200   {
1201     this->reserve_ent();
1202     return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1203   }
1204
1205   // Create a pair of new GOT entries and return the offset of the first.
1206   unsigned int
1207   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1208   {
1209     this->reserve_ent(2);
1210     return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1211                                                                  got_entry_2);
1212   }
1213
1214   unsigned int
1215   add_constant_pair(Valtype c1, Valtype c2)
1216   {
1217     this->reserve_ent(2);
1218     unsigned int got_offset = this->add_constant(c1);
1219     this->add_constant(c2);
1220     return got_offset;
1221   }
1222
1223   // Offset of _GLOBAL_OFFSET_TABLE_.
1224   unsigned int
1225   g_o_t() const
1226   {
1227     return this->got_offset(this->header_index_);
1228   }
1229
1230   // Offset of base used to access the GOT/TOC.
1231   // The got/toc pointer reg will be set to this value.
1232   typename elfcpp::Elf_types<size>::Elf_Off
1233   got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1234   {
1235     if (size == 32)
1236       return this->g_o_t();
1237     else
1238       return (this->output_section()->address()
1239               + object->toc_base_offset()
1240               - this->address());
1241   }
1242
1243   // Ensure our GOT has a header.
1244   void
1245   set_final_data_size()
1246   {
1247     if (this->header_ent_cnt_ != 0)
1248       this->make_header();
1249     Output_data_got<size, big_endian>::set_final_data_size();
1250   }
1251
1252   // First word of GOT header needs some values that are not
1253   // handled by Output_data_got so poke them in here.
1254   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1255   void
1256   do_write(Output_file* of)
1257   {
1258     this->replace_constant(this->header_index_,
1259                            (size == 32
1260                             ? this->layout_->dynamic_section()->address()
1261                             : this->output_section()->address() + 0x8000));
1262
1263     Output_data_got<size, big_endian>::do_write(of);
1264   }
1265
1266 private:
1267   void
1268   reserve_ent(unsigned int cnt = 1)
1269   {
1270     if (this->header_ent_cnt_ == 0)
1271       return;
1272     if (this->num_entries() + cnt > this->header_index_)
1273       this->make_header();
1274   }
1275
1276   void
1277   make_header()
1278   {
1279     this->header_ent_cnt_ = 0;
1280     this->header_index_ = this->num_entries();
1281     if (size == 32)
1282       {
1283         Output_data_got<size, big_endian>::add_constant(0);
1284         Output_data_got<size, big_endian>::add_constant(0);
1285         Output_data_got<size, big_endian>::add_constant(0);
1286
1287         // Define _GLOBAL_OFFSET_TABLE_ at the header
1288         this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1289                                              Symbol_table::PREDEFINED,
1290                                              this, this->g_o_t(), 0,
1291                                              elfcpp::STT_OBJECT,
1292                                              elfcpp::STB_LOCAL,
1293                                              elfcpp::STV_HIDDEN,
1294                                              0, false, false);
1295       }
1296     else
1297       Output_data_got<size, big_endian>::add_constant(0);
1298   }
1299
1300   // Stashed pointers.
1301   Symbol_table* symtab_;
1302   Layout* layout_;
1303
1304   // GOT header size.
1305   unsigned int header_ent_cnt_;
1306   // GOT header index.
1307   unsigned int header_index_;
1308 };
1309
1310 // Get the GOT section, creating it if necessary.
1311
1312 template<int size, bool big_endian>
1313 Output_data_got_powerpc<size, big_endian>*
1314 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1315                                               Layout* layout)
1316 {
1317   if (this->got_ == NULL)
1318     {
1319       gold_assert(symtab != NULL && layout != NULL);
1320
1321       this->got_
1322         = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1323
1324       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1325                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1326                                       this->got_, ORDER_DATA, false);
1327     }
1328
1329   return this->got_;
1330 }
1331
1332 // Get the dynamic reloc section, creating it if necessary.
1333
1334 template<int size, bool big_endian>
1335 typename Target_powerpc<size, big_endian>::Reloc_section*
1336 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1337 {
1338   if (this->rela_dyn_ == NULL)
1339     {
1340       gold_assert(layout != NULL);
1341       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1342       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1343                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
1344                                       ORDER_DYNAMIC_RELOCS, false);
1345     }
1346   return this->rela_dyn_;
1347 }
1348
1349 // A class to handle the PLT data.
1350
1351 template<int size, bool big_endian>
1352 class Output_data_plt_powerpc : public Output_section_data_build
1353 {
1354  public:
1355   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1356                             size, big_endian> Reloc_section;
1357
1358   Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
1359
1360   // Add an entry to the PLT.
1361   void
1362   add_entry(Symbol*);
1363
1364   // Return the .rela.plt section data.
1365   const Reloc_section*
1366   rel_plt() const
1367   {
1368     return this->rel_;
1369   }
1370
1371   // Return the number of PLT entries.
1372   unsigned int
1373   entry_count() const
1374   {
1375     return ((this->current_data_size() - initial_plt_entry_size)
1376             / plt_entry_size);
1377   }
1378
1379   // Return the offset of the first non-reserved PLT entry.
1380   static unsigned int
1381   first_plt_entry_offset()
1382   { return initial_plt_entry_size; }
1383
1384   // Return the size of a PLT entry.
1385   static unsigned int
1386   get_plt_entry_size()
1387   { return plt_entry_size; }
1388
1389  protected:
1390   void
1391   do_adjust_output_section(Output_section* os)
1392   {
1393     os->set_entsize(0);
1394   }
1395
1396   // Write to a map file.
1397   void
1398   do_print_to_mapfile(Mapfile* mapfile) const
1399   { mapfile->print_output_data(this, _("** PLT")); }
1400
1401  private:
1402   // The size of an entry in the PLT.
1403   static const int plt_entry_size = size == 32 ? 4 : 24;
1404   // The size of the first reserved entry.
1405   static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1406
1407   // Write out the PLT data.
1408   void
1409   do_write(Output_file*);
1410
1411   // The reloc section.
1412   Reloc_section* rel_;
1413   // Allows access to .glink for do_write.
1414   Target_powerpc<size, big_endian>* targ_;
1415 };
1416
1417 // Create the PLT section.
1418
1419 template<int size, bool big_endian>
1420 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1421     Layout* layout,
1422     Target_powerpc<size, big_endian>* targ)
1423   : Output_section_data_build(size == 32 ? 4 : 8),
1424     targ_(targ)
1425 {
1426   this->rel_ = new Reloc_section(false);
1427   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1428                                   elfcpp::SHF_ALLOC, this->rel_,
1429                                   ORDER_DYNAMIC_PLT_RELOCS, false);
1430 }
1431
1432 // Add an entry to the PLT.
1433
1434 template<int size, bool big_endian>
1435 void
1436 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1437 {
1438   if (!gsym->has_plt_offset())
1439     {
1440       off_t off = this->current_data_size();
1441
1442       if (off == 0)
1443         off += initial_plt_entry_size;
1444       gsym->set_plt_offset(off);
1445       gsym->set_needs_dynsym_entry();
1446       this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1447       off += plt_entry_size;
1448       this->set_current_data_size(off);
1449     }
1450 }
1451
1452 static const uint32_t add_0_11_11       = 0x7c0b5a14;
1453 static const uint32_t add_3_3_2         = 0x7c631214;
1454 static const uint32_t add_3_3_13        = 0x7c636a14;
1455 static const uint32_t add_11_0_11       = 0x7d605a14;
1456 static const uint32_t add_12_2_11       = 0x7d825a14;
1457 static const uint32_t addi_11_11        = 0x396b0000;
1458 static const uint32_t addi_12_12        = 0x398c0000;
1459 static const uint32_t addi_2_2          = 0x38420000;
1460 static const uint32_t addi_3_2          = 0x38620000;
1461 static const uint32_t addi_3_3          = 0x38630000;
1462 static const uint32_t addis_0_2         = 0x3c020000;
1463 static const uint32_t addis_0_13        = 0x3c0d0000;
1464 static const uint32_t addis_11_11       = 0x3d6b0000;
1465 static const uint32_t addis_11_30       = 0x3d7e0000;
1466 static const uint32_t addis_12_12       = 0x3d8c0000;
1467 static const uint32_t addis_12_2        = 0x3d820000;
1468 static const uint32_t addis_3_2         = 0x3c620000;
1469 static const uint32_t addis_3_13        = 0x3c6d0000;
1470 static const uint32_t b                 = 0x48000000;
1471 static const uint32_t bcl_20_31         = 0x429f0005;
1472 static const uint32_t bctr              = 0x4e800420;
1473 static const uint32_t blrl              = 0x4e800021;
1474 static const uint32_t cror_15_15_15     = 0x4def7b82;
1475 static const uint32_t cror_31_31_31     = 0x4ffffb82;
1476 static const uint32_t ld_11_12          = 0xe96c0000;
1477 static const uint32_t ld_11_2           = 0xe9620000;
1478 static const uint32_t ld_2_1            = 0xe8410000;
1479 static const uint32_t ld_2_11           = 0xe84b0000;
1480 static const uint32_t ld_2_12           = 0xe84c0000;
1481 static const uint32_t ld_2_2            = 0xe8420000;
1482 static const uint32_t li_0_0            = 0x38000000;
1483 static const uint32_t lis_0_0           = 0x3c000000;
1484 static const uint32_t lis_11            = 0x3d600000;
1485 static const uint32_t lis_12            = 0x3d800000;
1486 static const uint32_t lwz_0_12          = 0x800c0000;
1487 static const uint32_t lwz_11_11         = 0x816b0000;
1488 static const uint32_t lwz_11_30         = 0x817e0000;
1489 static const uint32_t lwz_12_12         = 0x818c0000;
1490 static const uint32_t lwzu_0_12         = 0x840c0000;
1491 static const uint32_t mflr_0            = 0x7c0802a6;
1492 static const uint32_t mflr_11           = 0x7d6802a6;
1493 static const uint32_t mflr_12           = 0x7d8802a6;
1494 static const uint32_t mtctr_0           = 0x7c0903a6;
1495 static const uint32_t mtctr_11          = 0x7d6903a6;
1496 static const uint32_t mtlr_0            = 0x7c0803a6;
1497 static const uint32_t mtlr_12           = 0x7d8803a6;
1498 static const uint32_t nop               = 0x60000000;
1499 static const uint32_t ori_0_0_0         = 0x60000000;
1500 static const uint32_t std_2_1           = 0xf8410000;
1501 static const uint32_t sub_11_11_12      = 0x7d6c5850;
1502
1503 // Write out the PLT.
1504
1505 template<int size, bool big_endian>
1506 void
1507 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1508 {
1509   if (size == 32)
1510     {
1511       const off_t offset = this->offset();
1512       const section_size_type oview_size
1513         = convert_to_section_size_type(this->data_size());
1514       unsigned char* const oview = of->get_output_view(offset, oview_size);
1515       unsigned char* pov = oview;
1516       unsigned char* endpov = oview + oview_size;
1517
1518       // The address the .glink branch table
1519       const Output_data_glink<size, big_endian>* glink
1520         = this->targ_->glink_section();
1521       elfcpp::Elf_types<32>::Elf_Addr branch_tab
1522         = glink->address() + glink->pltresolve();
1523
1524       while (pov < endpov)
1525         {
1526           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1527           pov += 4;
1528           branch_tab += 4;
1529         }
1530
1531       of->write_output_view(offset, oview_size, oview);
1532     }
1533 }
1534
1535 // Create the PLT section.
1536
1537 template<int size, bool big_endian>
1538 void
1539 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1540 {
1541   if (this->plt_ == NULL)
1542     {
1543       if (this->glink_ == NULL)
1544         make_glink_section(layout);
1545
1546       // Ensure that .rela.dyn always appears before .rela.plt  This is
1547       // necessary due to how, on PowerPC and some other targets, .rela.dyn
1548       // needs to include .rela.plt in it's range.
1549       this->rela_dyn_section(layout);
1550
1551       this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1552       layout->add_output_section_data(".plt",
1553                                       (size == 32
1554                                        ? elfcpp::SHT_PROGBITS
1555                                        : elfcpp::SHT_NOBITS),
1556                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1557                                       this->plt_,
1558                                       (size == 32
1559                                        ? ORDER_SMALL_DATA
1560                                        : ORDER_SMALL_BSS),
1561                                       false);
1562     }
1563 }
1564
1565 // A class to handle .glink.
1566
1567 template<int size, bool big_endian>
1568 class Output_data_glink : public Output_section_data
1569 {
1570  public:
1571   static const int pltresolve_size = 16*4;
1572
1573   Output_data_glink(Target_powerpc<size, big_endian>*);
1574
1575   // Add an entry
1576   void
1577   add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1578             const Sized_relobj<size, big_endian>*);
1579
1580   unsigned int
1581   find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1582              const Sized_relobj<size, big_endian>*) const;
1583
1584   unsigned int
1585   glink_entry_size() const
1586   {
1587     if (size == 32)
1588       return 4 * 4;
1589     else
1590       // FIXME: We should be using multiple glink sections for
1591       // stubs to support > 33M applications.
1592       return 8 * 4;
1593   }
1594
1595   off_t
1596   pltresolve() const
1597   {
1598     return this->pltresolve_;
1599   }
1600
1601  protected:
1602   // Write to a map file.
1603   void
1604   do_print_to_mapfile(Mapfile* mapfile) const
1605   { mapfile->print_output_data(this, _("** glink")); }
1606
1607  private:
1608   void
1609   set_final_data_size();
1610
1611   // Write out .glink
1612   void
1613   do_write(Output_file*);
1614
1615   class Glink_sym_ent
1616   {
1617   public:
1618     Glink_sym_ent(const Symbol* sym,
1619                   const elfcpp::Rela<size, big_endian>& reloc,
1620                   const Sized_relobj<size, big_endian>* object)
1621       : sym_(sym), addend_(0), object_(0)
1622     {
1623       if (size != 32)
1624         this->addend_ = reloc.get_r_addend();
1625       else if (parameters->options().output_is_position_independent()
1626                && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1627                    == elfcpp::R_PPC_PLTREL24))
1628         {
1629           this->addend_ = reloc.get_r_addend();
1630           if (this->addend_ != 0)
1631             this->object_ = object;
1632         }
1633     }
1634
1635     bool operator==(const Glink_sym_ent& that) const
1636     {
1637       return (this->sym_ == that.sym_
1638               && this->object_ == that.object_
1639               && this->addend_ == that.addend_);
1640     }
1641
1642     const Symbol* sym_;
1643     unsigned int addend_;
1644     const Sized_relobj<size, big_endian>* object_;
1645   };
1646
1647   class Glink_sym_ent_hash
1648   {
1649   public:
1650     size_t operator()(const Glink_sym_ent& ent) const
1651     {
1652       return (reinterpret_cast<uintptr_t>(ent.sym_)
1653               ^ reinterpret_cast<uintptr_t>(ent.object_)
1654               ^ ent.addend_);
1655     }
1656   };
1657
1658   // Map sym/object/addend to index.
1659   typedef Unordered_map<Glink_sym_ent, unsigned int,
1660                         Glink_sym_ent_hash> Glink_entries;
1661   Glink_entries glink_entries_;
1662
1663   // Offset of pltresolve stub (actually, branch table for 32-bit)
1664   off_t pltresolve_;
1665
1666   // Allows access to .got and .plt for do_write.
1667   Target_powerpc<size, big_endian>* targ_;
1668 };
1669
1670 // Create the glink section.
1671
1672 template<int size, bool big_endian>
1673 Output_data_glink<size, big_endian>::Output_data_glink(
1674     Target_powerpc<size, big_endian>* targ)
1675   : Output_section_data(16),
1676     pltresolve_(0), targ_(targ)
1677 {
1678 }
1679
1680 // Add an entry to glink, if we do not already have one for this
1681 // sym/object/addend combo.
1682
1683 template<int size, bool big_endian>
1684 void
1685 Output_data_glink<size, big_endian>::add_entry(
1686     const Symbol* gsym,
1687     const elfcpp::Rela<size, big_endian>& reloc,
1688     const Sized_relobj<size, big_endian>* object)
1689 {
1690   Glink_sym_ent ent(gsym, reloc, object);
1691   unsigned int indx = this->glink_entries_.size();
1692   this->glink_entries_.insert(std::make_pair(ent, indx));
1693 }
1694
1695 template<int size, bool big_endian>
1696 unsigned int
1697 Output_data_glink<size, big_endian>::find_entry(
1698     const Symbol* gsym,
1699     const elfcpp::Rela<size, big_endian>& reloc,
1700     const Sized_relobj<size, big_endian>* object) const
1701 {
1702   Glink_sym_ent ent(gsym, reloc, object);
1703   typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1704   gold_assert(p != this->glink_entries_.end());
1705   return p->second;
1706 }
1707
1708 template<int size, bool big_endian>
1709 void
1710 Output_data_glink<size, big_endian>::set_final_data_size()
1711 {
1712   unsigned int count = this->glink_entries_.size();
1713   off_t total = count;
1714
1715   if (count != 0)
1716     {
1717       if (size == 32)
1718         {
1719           total *= 16;
1720           this->pltresolve_ = total;
1721
1722           // space for branch table
1723           total += 4 * (count - 1);
1724
1725           total += -total & 15;
1726           total += this->pltresolve_size;
1727         }
1728       else
1729         {
1730           total *= 32;
1731           this->pltresolve_ = total;
1732           total += this->pltresolve_size;
1733
1734           // space for branch table
1735           total += 8 * count;
1736           if (count > 0x8000)
1737             total += 4 * (count - 0x8000);
1738         }
1739     }
1740
1741   this->set_data_size(total);
1742 }
1743
1744 static inline uint32_t
1745 l(uint32_t a)
1746 {
1747   return a & 0xffff;
1748 }
1749
1750 static inline uint32_t
1751 hi(uint32_t a)
1752 {
1753   return l(a >> 16);
1754 }
1755
1756 static inline uint32_t
1757 ha(uint32_t a)
1758 {
1759   return hi(a + 0x8000);
1760 }
1761
1762 template<bool big_endian>
1763 static inline void
1764 write_insn(unsigned char* p, uint32_t v)
1765 {
1766   elfcpp::Swap<32, big_endian>::writeval(p, v);
1767 }
1768
1769 // Write out .glink.
1770
1771 template<int size, bool big_endian>
1772 void
1773 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1774 {
1775   const off_t off = this->offset();
1776   const section_size_type oview_size =
1777     convert_to_section_size_type(this->data_size());
1778   unsigned char* const oview = of->get_output_view(off, oview_size);
1779   unsigned char* p;
1780
1781   // The base address of the .plt section.
1782   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1783   Address plt_base = this->targ_->plt_section()->address();
1784
1785   const Output_data_got_powerpc<size, big_endian>* got
1786     = this->targ_->got_section();
1787
1788   if (size == 64)
1789     {
1790       Address got_os_addr = got->output_section()->address();
1791
1792       // Write out call stubs.
1793       typename Glink_entries::const_iterator g;
1794       for (g = this->glink_entries_.begin();
1795            g != this->glink_entries_.end();
1796            ++g)
1797         {
1798           Address plt_addr = plt_base + g->first.sym_->plt_offset();
1799           const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1800             <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1801           Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1802           Address pltoff = plt_addr - got_addr;
1803
1804           if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1805             gold_error(_("%s: linkage table error against `%s'"),
1806                        g->first.object_->name().c_str(),
1807                        g->first.sym_->demangled_name().c_str());
1808
1809           p = oview + g->second * this->glink_entry_size();
1810           if (ha(pltoff) != 0)
1811             {
1812               write_insn<big_endian>(p, addis_12_2 + ha(pltoff)),       p += 4;
1813               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1814               write_insn<big_endian>(p, ld_11_12 + l(pltoff)),          p += 4;
1815               if (ha(pltoff + 16) != ha(pltoff))
1816                 {
1817                   write_insn<big_endian>(p, addi_12_12 + l(pltoff)),    p += 4;
1818                   pltoff = 0;
1819                 }
1820               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1821               write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)),       p += 4;
1822               write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)),     p += 4;
1823               write_insn<big_endian>(p, bctr),                          p += 4;
1824             }
1825           else
1826             {
1827               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1828               write_insn<big_endian>(p, ld_11_2 + l(pltoff)),           p += 4;
1829               if (ha(pltoff + 16) != ha(pltoff))
1830                 {
1831                   write_insn<big_endian>(p, addi_2_2 + l(pltoff)),      p += 4;
1832                   pltoff = 0;
1833                 }
1834               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1835               write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)),      p += 4;
1836               write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)),        p += 4;
1837               write_insn<big_endian>(p, bctr),                          p += 4;
1838             }
1839         }
1840
1841       // Write pltresolve stub.
1842       p = oview + this->pltresolve_;
1843       Address after_bcl = this->address() + this->pltresolve_ + 16;
1844       Address pltoff = plt_base - after_bcl;
1845
1846       elfcpp::Swap<64, big_endian>::writeval(p, pltoff),        p += 8;
1847
1848       write_insn<big_endian>(p, mflr_12),                       p += 4;
1849       write_insn<big_endian>(p, bcl_20_31),                     p += 4;
1850       write_insn<big_endian>(p, mflr_11),                       p += 4;
1851       write_insn<big_endian>(p, ld_2_11 + l(-16)),              p += 4;
1852       write_insn<big_endian>(p, mtlr_12),                       p += 4;
1853       write_insn<big_endian>(p, add_12_2_11),                   p += 4;
1854       write_insn<big_endian>(p, ld_11_12 + 0),                  p += 4;
1855       write_insn<big_endian>(p, ld_2_12 + 8),                   p += 4;
1856       write_insn<big_endian>(p, mtctr_11),                      p += 4;
1857       write_insn<big_endian>(p, ld_11_12 + 16),                 p += 4;
1858       write_insn<big_endian>(p, bctr),                          p += 4;
1859       while (p < oview + this->pltresolve_ + this->pltresolve_size)
1860         write_insn<big_endian>(p, nop), p += 4;
1861
1862       // Write lazy link call stubs.
1863       uint32_t indx = 0;
1864       while (p < oview + oview_size)
1865         {
1866           if (indx < 0x8000)
1867             {
1868               write_insn<big_endian>(p, li_0_0 + indx),                 p += 4;
1869             }
1870           else
1871             {
1872               write_insn<big_endian>(p, lis_0_0 + hi(indx)),            p += 4;
1873               write_insn<big_endian>(p, ori_0_0_0 + l(indx)),           p += 4;
1874             }
1875           uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
1876           write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),      p += 4;
1877           indx++;
1878         }
1879     }
1880   else
1881     {
1882       // The address of _GLOBAL_OFFSET_TABLE_.
1883       Address g_o_t = got->address() + got->g_o_t();
1884
1885       // Write out call stubs.
1886       typename Glink_entries::const_iterator g;
1887       for (g = this->glink_entries_.begin();
1888            g != this->glink_entries_.end();
1889            ++g)
1890         {
1891           Address plt_addr = plt_base + g->first.sym_->plt_offset();
1892           Address got_addr;
1893           const Address invalid_address = static_cast<Address>(-1);
1894
1895           p = oview + g->second * this->glink_entry_size();
1896           if (parameters->options().output_is_position_independent())
1897             {
1898               const Powerpc_relobj<size, big_endian>* object = static_cast
1899                 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1900               if (object != NULL)
1901                 {
1902                   unsigned int got2 = object->got2_shndx();
1903                   got_addr = g->first.object_->get_output_section_offset(got2);
1904                   gold_assert(got_addr != invalid_address);
1905                   got_addr += (g->first.object_->output_section(got2)->address()
1906                                + g->first.addend_);
1907                 }
1908               else
1909                 got_addr = g_o_t;
1910
1911               Address pltoff = plt_addr - got_addr;
1912               if (ha(pltoff) == 0)
1913                 {
1914                   write_insn<big_endian>(p +  0, lwz_11_30 + l(pltoff));
1915                   write_insn<big_endian>(p +  4, mtctr_11);
1916                   write_insn<big_endian>(p +  8, bctr);
1917                 }
1918               else
1919                 {
1920                   write_insn<big_endian>(p +  0, addis_11_30 + ha(pltoff));
1921                   write_insn<big_endian>(p +  4, lwz_11_11 + l(pltoff));
1922                   write_insn<big_endian>(p +  8, mtctr_11);
1923                   write_insn<big_endian>(p + 12, bctr);
1924                 }
1925             }
1926           else
1927             {
1928               write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
1929               write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
1930               write_insn<big_endian>(p +  8, mtctr_11);
1931               write_insn<big_endian>(p + 12, bctr);
1932             }
1933         }
1934
1935       // Write out pltresolve branch table.
1936       p = oview + this->pltresolve_;
1937       unsigned int the_end = oview_size - this->pltresolve_size;
1938       unsigned char* end_p = oview + the_end;
1939       while (p < end_p - 8 * 4)
1940         write_insn<big_endian>(p, b + end_p - p), p += 4;
1941       while (p < end_p)
1942         write_insn<big_endian>(p, nop), p += 4;
1943
1944       // Write out pltresolve call stub.
1945       if (parameters->options().output_is_position_independent())
1946         {
1947           Address res0_off = this->pltresolve_;
1948           Address after_bcl_off = the_end + 12;
1949           Address bcl_res0 = after_bcl_off - res0_off;
1950
1951           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
1952           write_insn<big_endian>(p +  4, mflr_0);
1953           write_insn<big_endian>(p +  8, bcl_20_31);
1954           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1955           write_insn<big_endian>(p + 16, mflr_12);
1956           write_insn<big_endian>(p + 20, mtlr_0);
1957           write_insn<big_endian>(p + 24, sub_11_11_12);
1958
1959           Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1960
1961           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1962           if (ha(got_bcl) == ha(got_bcl + 4))
1963             {
1964               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1965               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1966             }
1967           else
1968             {
1969               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1970               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1971             }
1972           write_insn<big_endian>(p + 40, mtctr_0);
1973           write_insn<big_endian>(p + 44, add_0_11_11);
1974           write_insn<big_endian>(p + 48, add_11_0_11);
1975           write_insn<big_endian>(p + 52, bctr);
1976           write_insn<big_endian>(p + 56, nop);
1977           write_insn<big_endian>(p + 60, nop);
1978         }
1979       else
1980         {
1981           Address res0 = this->pltresolve_ + this->address();
1982
1983           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1984           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1985           if (ha(g_o_t + 4) == ha(g_o_t + 8))
1986             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1987           else
1988             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1989           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1990           write_insn<big_endian>(p + 16, mtctr_0);
1991           write_insn<big_endian>(p + 20, add_0_11_11);
1992           if (ha(g_o_t + 4) == ha(g_o_t + 8))
1993             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1994           else
1995             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1996           write_insn<big_endian>(p + 28, add_11_0_11);
1997           write_insn<big_endian>(p + 32, bctr);
1998           write_insn<big_endian>(p + 36, nop);
1999           write_insn<big_endian>(p + 40, nop);
2000           write_insn<big_endian>(p + 44, nop);
2001           write_insn<big_endian>(p + 48, nop);
2002           write_insn<big_endian>(p + 52, nop);
2003           write_insn<big_endian>(p + 56, nop);
2004           write_insn<big_endian>(p + 60, nop);
2005         }
2006       p += 64;
2007     }
2008
2009   of->write_output_view(off, oview_size, oview);
2010 }
2011
2012 // Create the glink section.
2013
2014 template<int size, bool big_endian>
2015 void
2016 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2017 {
2018   if (this->glink_ == NULL)
2019     {
2020       this->glink_ = new Output_data_glink<size, big_endian>(this);
2021       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2022                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2023                                       this->glink_, ORDER_TEXT, false);
2024     }
2025 }
2026
2027 // Create a PLT entry for a global symbol.
2028
2029 template<int size, bool big_endian>
2030 void
2031 Target_powerpc<size, big_endian>::make_plt_entry(
2032     Layout* layout,
2033     Symbol* gsym,
2034     const elfcpp::Rela<size, big_endian>& reloc,
2035     const Sized_relobj<size, big_endian>* object)
2036 {
2037   if (this->plt_ == NULL)
2038     this->make_plt_section(layout);
2039
2040   this->plt_->add_entry(gsym);
2041
2042   this->glink_->add_entry(gsym, reloc, object);
2043 }
2044
2045 // Return the number of entries in the PLT.
2046
2047 template<int size, bool big_endian>
2048 unsigned int
2049 Target_powerpc<size, big_endian>::plt_entry_count() const
2050 {
2051   if (this->plt_ == NULL)
2052     return 0;
2053   return this->plt_->entry_count();
2054 }
2055
2056 // Return the offset of the first non-reserved PLT entry.
2057
2058 template<int size, bool big_endian>
2059 unsigned int
2060 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2061 {
2062   return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2063 }
2064
2065 // Return the size of each PLT entry.
2066
2067 template<int size, bool big_endian>
2068 unsigned int
2069 Target_powerpc<size, big_endian>::plt_entry_size() const
2070 {
2071   return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2072 }
2073
2074 // Create a GOT entry for local dynamic __tls_get_addr calls.
2075
2076 template<int size, bool big_endian>
2077 unsigned int
2078 Target_powerpc<size, big_endian>::tlsld_got_offset(
2079     Symbol_table* symtab,
2080     Layout* layout,
2081     Sized_relobj_file<size, big_endian>* object)
2082 {
2083   if (this->tlsld_got_offset_ == -1U)
2084     {
2085       gold_assert(symtab != NULL && layout != NULL && object != NULL);
2086       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2087       Output_data_got_powerpc<size, big_endian>* got
2088         = this->got_section(symtab, layout);
2089       unsigned int got_offset = got->add_constant_pair(0, 0);
2090       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2091                           got_offset, 0);
2092       this->tlsld_got_offset_ = got_offset;
2093     }
2094   return this->tlsld_got_offset_;
2095 }
2096
2097 // Get the Reference_flags for a particular relocation.
2098
2099 template<int size, bool big_endian>
2100 int
2101 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2102 {
2103   switch (r_type)
2104     {
2105     case elfcpp::R_POWERPC_NONE:
2106     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2107     case elfcpp::R_POWERPC_GNU_VTENTRY:
2108     case elfcpp::R_PPC64_TOC:
2109       // No symbol reference.
2110       return 0;
2111
2112     case elfcpp::R_PPC64_ADDR64:
2113     case elfcpp::R_PPC64_UADDR64:
2114     case elfcpp::R_POWERPC_ADDR32:
2115     case elfcpp::R_POWERPC_UADDR32:
2116     case elfcpp::R_POWERPC_ADDR16:
2117     case elfcpp::R_POWERPC_UADDR16:
2118     case elfcpp::R_POWERPC_ADDR16_LO:
2119     case elfcpp::R_POWERPC_ADDR16_HI:
2120     case elfcpp::R_POWERPC_ADDR16_HA:
2121       return Symbol::ABSOLUTE_REF;
2122
2123     case elfcpp::R_POWERPC_ADDR24:
2124     case elfcpp::R_POWERPC_ADDR14:
2125     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2126     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2127       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2128
2129     case elfcpp::R_POWERPC_REL32:
2130     case elfcpp::R_PPC_LOCAL24PC:
2131     case elfcpp::R_POWERPC_REL16:
2132     case elfcpp::R_POWERPC_REL16_LO:
2133     case elfcpp::R_POWERPC_REL16_HI:
2134     case elfcpp::R_POWERPC_REL16_HA:
2135       return Symbol::RELATIVE_REF;
2136
2137     case elfcpp::R_POWERPC_REL24:
2138     case elfcpp::R_PPC_PLTREL24:
2139     case elfcpp::R_POWERPC_REL14:
2140     case elfcpp::R_POWERPC_REL14_BRTAKEN:
2141     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2142       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2143
2144     case elfcpp::R_POWERPC_GOT16:
2145     case elfcpp::R_POWERPC_GOT16_LO:
2146     case elfcpp::R_POWERPC_GOT16_HI:
2147     case elfcpp::R_POWERPC_GOT16_HA:
2148     case elfcpp::R_PPC64_TOC16:
2149     case elfcpp::R_PPC64_TOC16_LO:
2150     case elfcpp::R_PPC64_TOC16_HI:
2151     case elfcpp::R_PPC64_TOC16_HA:
2152     case elfcpp::R_PPC64_TOC16_DS:
2153     case elfcpp::R_PPC64_TOC16_LO_DS:
2154       // Absolute in GOT.
2155       return Symbol::ABSOLUTE_REF;
2156
2157     case elfcpp::R_POWERPC_GOT_TPREL16:
2158     case elfcpp::R_POWERPC_TLS:
2159       return Symbol::TLS_REF;
2160
2161     case elfcpp::R_POWERPC_COPY:
2162     case elfcpp::R_POWERPC_GLOB_DAT:
2163     case elfcpp::R_POWERPC_JMP_SLOT:
2164     case elfcpp::R_POWERPC_RELATIVE:
2165     case elfcpp::R_POWERPC_DTPMOD:
2166     default:
2167       // Not expected.  We will give an error later.
2168       return 0;
2169     }
2170 }
2171
2172 // Report an unsupported relocation against a local symbol.
2173
2174 template<int size, bool big_endian>
2175 void
2176 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2177     Sized_relobj_file<size, big_endian>* object,
2178     unsigned int r_type)
2179 {
2180   gold_error(_("%s: unsupported reloc %u against local symbol"),
2181              object->name().c_str(), r_type);
2182 }
2183
2184 // We are about to emit a dynamic relocation of type R_TYPE.  If the
2185 // dynamic linker does not support it, issue an error.
2186
2187 template<int size, bool big_endian>
2188 void
2189 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2190                                                       unsigned int r_type)
2191 {
2192   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2193
2194   // These are the relocation types supported by glibc for both 32-bit
2195   // and 64-bit powerpc.
2196   switch (r_type)
2197     {
2198     case elfcpp::R_POWERPC_NONE:
2199     case elfcpp::R_POWERPC_RELATIVE:
2200     case elfcpp::R_POWERPC_GLOB_DAT:
2201     case elfcpp::R_POWERPC_DTPMOD:
2202     case elfcpp::R_POWERPC_DTPREL:
2203     case elfcpp::R_POWERPC_TPREL:
2204     case elfcpp::R_POWERPC_JMP_SLOT:
2205     case elfcpp::R_POWERPC_COPY:
2206     case elfcpp::R_POWERPC_IRELATIVE:
2207     case elfcpp::R_POWERPC_ADDR32:
2208     case elfcpp::R_POWERPC_UADDR32:
2209     case elfcpp::R_POWERPC_ADDR24:
2210     case elfcpp::R_POWERPC_ADDR16:
2211     case elfcpp::R_POWERPC_UADDR16:
2212     case elfcpp::R_POWERPC_ADDR16_LO:
2213     case elfcpp::R_POWERPC_ADDR16_HI:
2214     case elfcpp::R_POWERPC_ADDR16_HA:
2215     case elfcpp::R_POWERPC_ADDR14:
2216     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2217     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2218     case elfcpp::R_POWERPC_REL32:
2219     case elfcpp::R_POWERPC_REL24:
2220     case elfcpp::R_POWERPC_TPREL16:
2221     case elfcpp::R_POWERPC_TPREL16_LO:
2222     case elfcpp::R_POWERPC_TPREL16_HI:
2223     case elfcpp::R_POWERPC_TPREL16_HA:
2224       return;
2225
2226     default:
2227       break;
2228     }
2229
2230   if (size == 64)
2231     {
2232       switch (r_type)
2233         {
2234           // These are the relocation types supported only on 64-bit.
2235         case elfcpp::R_PPC64_ADDR64:
2236         case elfcpp::R_PPC64_UADDR64:
2237         case elfcpp::R_PPC64_JMP_IREL:
2238         case elfcpp::R_PPC64_ADDR16_DS:
2239         case elfcpp::R_PPC64_ADDR16_LO_DS:
2240         case elfcpp::R_PPC64_ADDR16_HIGHER:
2241         case elfcpp::R_PPC64_ADDR16_HIGHEST:
2242         case elfcpp::R_PPC64_ADDR16_HIGHERA:
2243         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2244         case elfcpp::R_PPC64_REL64:
2245         case elfcpp::R_POWERPC_ADDR30:
2246         case elfcpp::R_PPC64_TPREL16_DS:
2247         case elfcpp::R_PPC64_TPREL16_LO_DS:
2248         case elfcpp::R_PPC64_TPREL16_HIGHER:
2249         case elfcpp::R_PPC64_TPREL16_HIGHEST:
2250         case elfcpp::R_PPC64_TPREL16_HIGHERA:
2251         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2252           return;
2253
2254         default:
2255           break;
2256         }
2257     }
2258   else
2259     {
2260       switch (r_type)
2261         {
2262           // These are the relocation types supported only on 32-bit.
2263           // ??? glibc ld.so doesn't need to support these.
2264         case elfcpp::R_POWERPC_DTPREL16:
2265         case elfcpp::R_POWERPC_DTPREL16_LO:
2266         case elfcpp::R_POWERPC_DTPREL16_HI:
2267         case elfcpp::R_POWERPC_DTPREL16_HA:
2268           return;
2269
2270         default:
2271           break;
2272         }
2273     }
2274
2275   // This prevents us from issuing more than one error per reloc
2276   // section.  But we can still wind up issuing more than one
2277   // error per object file.
2278   if (this->issued_non_pic_error_)
2279     return;
2280   gold_assert(parameters->options().output_is_position_independent());
2281   object->error(_("requires unsupported dynamic reloc; "
2282                   "recompile with -fPIC"));
2283   this->issued_non_pic_error_ = true;
2284   return;
2285 }
2286
2287 // Scan a relocation for a local symbol.
2288
2289 template<int size, bool big_endian>
2290 inline void
2291 Target_powerpc<size, big_endian>::Scan::local(
2292     Symbol_table* symtab,
2293     Layout* layout,
2294     Target_powerpc<size, big_endian>* target,
2295     Sized_relobj_file<size, big_endian>* object,
2296     unsigned int data_shndx,
2297     Output_section* output_section,
2298     const elfcpp::Rela<size, big_endian>& reloc,
2299     unsigned int r_type,
2300     const elfcpp::Sym<size, big_endian>& lsym)
2301 {
2302   Powerpc_relobj<size, big_endian>* ppc_object
2303     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2304
2305   switch (r_type)
2306     {
2307     case elfcpp::R_POWERPC_NONE:
2308     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2309     case elfcpp::R_POWERPC_GNU_VTENTRY:
2310     case elfcpp::R_PPC64_TOCSAVE:
2311     case elfcpp::R_PPC_EMB_MRKREF:
2312     case elfcpp::R_POWERPC_TLS:
2313       break;
2314
2315     case elfcpp::R_PPC64_TOC:
2316       {
2317         Output_data_got_powerpc<size, big_endian>* got
2318           = target->got_section(symtab, layout);
2319         if (parameters->options().output_is_position_independent())
2320           {
2321             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2322             rela_dyn->add_output_section_relative(got->output_section(),
2323                                                   elfcpp::R_POWERPC_RELATIVE,
2324                                                   output_section,
2325                                                   object, data_shndx,
2326                                                   reloc.get_r_offset(),
2327                                                   ppc_object->toc_base_offset());
2328           }
2329       }
2330       break;
2331
2332     case elfcpp::R_PPC64_ADDR64:
2333     case elfcpp::R_PPC64_UADDR64:
2334     case elfcpp::R_POWERPC_ADDR32:
2335     case elfcpp::R_POWERPC_UADDR32:
2336     case elfcpp::R_POWERPC_ADDR24:
2337     case elfcpp::R_POWERPC_ADDR16:
2338     case elfcpp::R_POWERPC_ADDR16_LO:
2339     case elfcpp::R_POWERPC_ADDR16_HI:
2340     case elfcpp::R_POWERPC_ADDR16_HA:
2341     case elfcpp::R_POWERPC_UADDR16:
2342     case elfcpp::R_PPC64_ADDR16_HIGHER:
2343     case elfcpp::R_PPC64_ADDR16_HIGHERA:
2344     case elfcpp::R_PPC64_ADDR16_HIGHEST:
2345     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2346     case elfcpp::R_PPC64_ADDR16_DS:
2347     case elfcpp::R_PPC64_ADDR16_LO_DS:
2348     case elfcpp::R_POWERPC_ADDR14:
2349     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2350     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2351       // If building a shared library (or a position-independent
2352       // executable), we need to create a dynamic relocation for
2353       // this location.
2354       if (parameters->options().output_is_position_independent())
2355         {
2356           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2357
2358           if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2359               || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2360             {
2361               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2362               rela_dyn->add_local_relative(object, r_sym,
2363                                            elfcpp::R_POWERPC_RELATIVE,
2364                                            output_section, data_shndx,
2365                                            reloc.get_r_offset(),
2366                                            reloc.get_r_addend(), false);
2367             }
2368           else
2369             {
2370               check_non_pic(object, r_type);
2371               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2372               rela_dyn->add_local(object, r_sym, r_type, output_section,
2373                                   data_shndx, reloc.get_r_offset(),
2374                                   reloc.get_r_addend());
2375             }
2376         }
2377       break;
2378
2379     case elfcpp::R_PPC64_REL64:
2380     case elfcpp::R_POWERPC_REL32:
2381     case elfcpp::R_POWERPC_REL24:
2382     case elfcpp::R_PPC_LOCAL24PC:
2383     case elfcpp::R_POWERPC_REL16:
2384     case elfcpp::R_POWERPC_REL16_LO:
2385     case elfcpp::R_POWERPC_REL16_HI:
2386     case elfcpp::R_POWERPC_REL16_HA:
2387     case elfcpp::R_POWERPC_REL14:
2388     case elfcpp::R_POWERPC_REL14_BRTAKEN:
2389     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2390     case elfcpp::R_POWERPC_SECTOFF:
2391     case elfcpp::R_POWERPC_TPREL16:
2392     case elfcpp::R_POWERPC_DTPREL16:
2393     case elfcpp::R_POWERPC_SECTOFF_LO:
2394     case elfcpp::R_POWERPC_TPREL16_LO:
2395     case elfcpp::R_POWERPC_DTPREL16_LO:
2396     case elfcpp::R_POWERPC_SECTOFF_HI:
2397     case elfcpp::R_POWERPC_TPREL16_HI:
2398     case elfcpp::R_POWERPC_DTPREL16_HI:
2399     case elfcpp::R_POWERPC_SECTOFF_HA:
2400     case elfcpp::R_POWERPC_TPREL16_HA:
2401     case elfcpp::R_POWERPC_DTPREL16_HA:
2402     case elfcpp::R_PPC64_DTPREL16_HIGHER:
2403     case elfcpp::R_PPC64_TPREL16_HIGHER:
2404     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2405     case elfcpp::R_PPC64_TPREL16_HIGHERA:
2406     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2407     case elfcpp::R_PPC64_TPREL16_HIGHEST:
2408     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2409     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2410     case elfcpp::R_PPC64_TPREL16_DS:
2411     case elfcpp::R_PPC64_TPREL16_LO_DS:
2412     case elfcpp::R_PPC64_DTPREL16_DS:
2413     case elfcpp::R_PPC64_DTPREL16_LO_DS:
2414     case elfcpp::R_PPC64_SECTOFF_DS:
2415     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2416     case elfcpp::R_PPC64_TLSGD:
2417     case elfcpp::R_PPC64_TLSLD:
2418       break;
2419
2420     case elfcpp::R_POWERPC_GOT16:
2421     case elfcpp::R_POWERPC_GOT16_LO:
2422     case elfcpp::R_POWERPC_GOT16_HI:
2423     case elfcpp::R_POWERPC_GOT16_HA:
2424     case elfcpp::R_PPC64_GOT16_DS:
2425     case elfcpp::R_PPC64_GOT16_LO_DS:
2426       {
2427         // The symbol requires a GOT entry.
2428         Output_data_got_powerpc<size, big_endian>* got
2429           = target->got_section(symtab, layout);
2430         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2431
2432         // If we are generating a shared object, we need to add a
2433         // dynamic relocation for this symbol's GOT entry.
2434         if (parameters->options().output_is_position_independent())
2435           {
2436             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2437               {
2438                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2439                 unsigned int off;
2440
2441                 off = got->add_constant(0);
2442                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2443                 rela_dyn->add_local_relative(object, r_sym,
2444                                              elfcpp::R_POWERPC_RELATIVE,
2445                                              got, off, 0, false);
2446               }
2447           }
2448         else
2449           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2450       }
2451       break;
2452
2453     case elfcpp::R_PPC64_TOC16:
2454     case elfcpp::R_PPC64_TOC16_LO:
2455     case elfcpp::R_PPC64_TOC16_HI:
2456     case elfcpp::R_PPC64_TOC16_HA:
2457     case elfcpp::R_PPC64_TOC16_DS:
2458     case elfcpp::R_PPC64_TOC16_LO_DS:
2459       // We need a GOT section.
2460       target->got_section(symtab, layout);
2461       break;
2462
2463     case elfcpp::R_POWERPC_GOT_TLSGD16:
2464     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2465     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2466     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2467       {
2468         const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2469         if (tls_type == tls::TLSOPT_NONE)
2470           {
2471             Output_data_got_powerpc<size, big_endian>* got
2472               = target->got_section(symtab, layout);
2473             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2474             unsigned int shndx = lsym.get_st_shndx();
2475             bool is_ordinary;
2476             shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2477             gold_assert(is_ordinary);
2478             got->add_local_pair_with_rel(object, r_sym,
2479                                          shndx,
2480                                          GOT_TYPE_TLSGD,
2481                                          target->rela_dyn_section(layout),
2482                                          elfcpp::R_POWERPC_DTPMOD,
2483                                          elfcpp::R_POWERPC_DTPREL);
2484           }
2485         else if (tls_type == tls::TLSOPT_TO_LE)
2486           {
2487             // no GOT relocs needed for Local Exec.
2488           }
2489         else
2490           gold_unreachable();
2491       }
2492       break;
2493
2494     case elfcpp::R_POWERPC_GOT_TLSLD16:
2495     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2496     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2497     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2498       {
2499         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2500         if (tls_type == tls::TLSOPT_NONE)
2501           target->tlsld_got_offset(symtab, layout, object);
2502         else if (tls_type == tls::TLSOPT_TO_LE)
2503           {
2504             // no GOT relocs needed for Local Exec.
2505             if (parameters->options().emit_relocs())
2506               {
2507                 Output_section* os = layout->tls_segment()->first_section();
2508                 gold_assert(os != NULL);
2509                 os->set_needs_symtab_index();
2510               }
2511           }
2512         else
2513           gold_unreachable();
2514       }
2515       break;
2516
2517     case elfcpp::R_POWERPC_GOT_DTPREL16:
2518     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2519     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2520     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2521       {
2522         Output_data_got_powerpc<size, big_endian>* got
2523           = target->got_section(symtab, layout);
2524         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2525         got->add_local_with_rel(object, r_sym, GOT_TYPE_DTPREL,
2526                                 target->rela_dyn_section(layout),
2527                                 elfcpp::R_POWERPC_DTPREL);
2528       }
2529       break;
2530
2531     case elfcpp::R_POWERPC_GOT_TPREL16:
2532     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2533     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2534     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2535       {
2536         const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2537         if (tls_type == tls::TLSOPT_NONE)
2538           {
2539             Output_data_got_powerpc<size, big_endian>* got
2540               = target->got_section(symtab, layout);
2541             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2542             got->add_local_with_rel(object, r_sym, GOT_TYPE_TPREL,
2543                                     target->rela_dyn_section(layout),
2544                                     elfcpp::R_POWERPC_TPREL);
2545           }
2546         else if (tls_type == tls::TLSOPT_TO_LE)
2547           {
2548             // no GOT relocs needed for Local Exec.
2549           }
2550         else
2551           gold_unreachable();
2552       }
2553       break;
2554
2555     default:
2556       unsupported_reloc_local(object, r_type);
2557       break;
2558     }
2559 }
2560
2561 // Report an unsupported relocation against a global symbol.
2562
2563 template<int size, bool big_endian>
2564 void
2565 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2566     Sized_relobj_file<size, big_endian>* object,
2567     unsigned int r_type,
2568     Symbol* gsym)
2569 {
2570   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2571              object->name().c_str(), r_type, gsym->demangled_name().c_str());
2572 }
2573
2574 // Scan a relocation for a global symbol.
2575
2576 template<int size, bool big_endian>
2577 inline void
2578 Target_powerpc<size, big_endian>::Scan::global(
2579     Symbol_table* symtab,
2580     Layout* layout,
2581     Target_powerpc<size, big_endian>* target,
2582     Sized_relobj_file<size, big_endian>* object,
2583     unsigned int data_shndx,
2584     Output_section* output_section,
2585     const elfcpp::Rela<size, big_endian>& reloc,
2586     unsigned int r_type,
2587     Symbol* gsym)
2588 {
2589   Powerpc_relobj<size, big_endian>* ppc_object
2590     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2591
2592   switch (r_type)
2593     {
2594     case elfcpp::R_POWERPC_NONE:
2595     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2596     case elfcpp::R_POWERPC_GNU_VTENTRY:
2597     case elfcpp::R_PPC_LOCAL24PC:
2598     case elfcpp::R_PPC_EMB_MRKREF:
2599     case elfcpp::R_POWERPC_TLS:
2600       break;
2601
2602     case elfcpp::R_PPC64_TOC:
2603       {
2604         Output_data_got_powerpc<size, big_endian>* got
2605           = target->got_section(symtab, layout);
2606         if (parameters->options().output_is_position_independent())
2607           {
2608             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2609             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2610             if (data_shndx != ppc_object->opd_shndx())
2611               symobj = static_cast
2612                 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2613             rela_dyn->add_output_section_relative(got->output_section(),
2614                                                   elfcpp::R_POWERPC_RELATIVE,
2615                                                   output_section,
2616                                                   object, data_shndx,
2617                                                   reloc.get_r_offset(),
2618                                                   symobj->toc_base_offset());
2619           }
2620       }
2621       break;
2622
2623     case elfcpp::R_PPC64_ADDR64:
2624     case elfcpp::R_PPC64_UADDR64:
2625     case elfcpp::R_POWERPC_ADDR32:
2626     case elfcpp::R_POWERPC_UADDR32:
2627     case elfcpp::R_POWERPC_ADDR24:
2628     case elfcpp::R_POWERPC_ADDR16:
2629     case elfcpp::R_POWERPC_ADDR16_LO:
2630     case elfcpp::R_POWERPC_ADDR16_HI:
2631     case elfcpp::R_POWERPC_ADDR16_HA:
2632     case elfcpp::R_POWERPC_UADDR16:
2633     case elfcpp::R_PPC64_ADDR16_HIGHER:
2634     case elfcpp::R_PPC64_ADDR16_HIGHERA:
2635     case elfcpp::R_PPC64_ADDR16_HIGHEST:
2636     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2637     case elfcpp::R_PPC64_ADDR16_DS:
2638     case elfcpp::R_PPC64_ADDR16_LO_DS:
2639     case elfcpp::R_POWERPC_ADDR14:
2640     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2641     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2642       {
2643         // Make a PLT entry if necessary.
2644         if (gsym->needs_plt_entry())
2645           {
2646             target->make_plt_entry(layout, gsym, reloc, 0);
2647             // Since this is not a PC-relative relocation, we may be
2648             // taking the address of a function. In that case we need to
2649             // set the entry in the dynamic symbol table to the address of
2650             // the PLT entry.
2651             if (size == 32
2652                 && gsym->is_from_dynobj() && !parameters->options().shared())
2653               gsym->set_needs_dynsym_value();
2654           }
2655         // Make a dynamic relocation if necessary.
2656         if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2657           {
2658             if (gsym->may_need_copy_reloc())
2659               {
2660                 target->copy_reloc(symtab, layout, object,
2661                                    data_shndx, output_section, gsym, reloc);
2662               }
2663             else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2664                       || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2665                      && (gsym->can_use_relative_reloc(false)
2666                          || data_shndx == ppc_object->opd_shndx()))
2667               {
2668                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2669                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2670                                               output_section, object,
2671                                               data_shndx, reloc.get_r_offset(),
2672                                               reloc.get_r_addend(), false);
2673               }
2674             else
2675               {
2676                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2677                 check_non_pic(object, r_type);
2678                 rela_dyn->add_global(gsym, r_type, output_section,
2679                                      object, data_shndx,
2680                                      reloc.get_r_offset(),
2681                                      reloc.get_r_addend());
2682               }
2683           }
2684       }
2685       break;
2686
2687     case elfcpp::R_PPC_PLTREL24:
2688     case elfcpp::R_POWERPC_REL24:
2689       if (gsym->needs_plt_entry()
2690           || (!gsym->final_value_is_known()
2691               && (gsym->is_undefined()
2692                   || gsym->is_from_dynobj()
2693                   || gsym->is_preemptible())))
2694         target->make_plt_entry(layout, gsym, reloc, object);
2695       // Fall thru
2696
2697     case elfcpp::R_PPC64_REL64:
2698     case elfcpp::R_POWERPC_REL32:
2699       // Make a dynamic relocation if necessary.
2700       if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2701         {
2702           if (gsym->may_need_copy_reloc())
2703             {
2704               target->copy_reloc(symtab, layout, object,
2705                                  data_shndx, output_section, gsym,
2706                                  reloc);
2707             }
2708           else
2709             {
2710               Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2711               check_non_pic(object, r_type);
2712               rela_dyn->add_global(gsym, r_type, output_section, object,
2713                                    data_shndx, reloc.get_r_offset(),
2714                                    reloc.get_r_addend());
2715             }
2716         }
2717       break;
2718
2719     case elfcpp::R_POWERPC_REL16:
2720     case elfcpp::R_POWERPC_REL16_LO:
2721     case elfcpp::R_POWERPC_REL16_HI:
2722     case elfcpp::R_POWERPC_REL16_HA:
2723     case elfcpp::R_POWERPC_REL14:
2724     case elfcpp::R_POWERPC_REL14_BRTAKEN:
2725     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2726     case elfcpp::R_POWERPC_SECTOFF:
2727     case elfcpp::R_POWERPC_TPREL16:
2728     case elfcpp::R_POWERPC_DTPREL16:
2729     case elfcpp::R_POWERPC_SECTOFF_LO:
2730     case elfcpp::R_POWERPC_TPREL16_LO:
2731     case elfcpp::R_POWERPC_DTPREL16_LO:
2732     case elfcpp::R_POWERPC_SECTOFF_HI:
2733     case elfcpp::R_POWERPC_TPREL16_HI:
2734     case elfcpp::R_POWERPC_DTPREL16_HI:
2735     case elfcpp::R_POWERPC_SECTOFF_HA:
2736     case elfcpp::R_POWERPC_TPREL16_HA:
2737     case elfcpp::R_POWERPC_DTPREL16_HA:
2738     case elfcpp::R_PPC64_DTPREL16_HIGHER:
2739     case elfcpp::R_PPC64_TPREL16_HIGHER:
2740     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2741     case elfcpp::R_PPC64_TPREL16_HIGHERA:
2742     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2743     case elfcpp::R_PPC64_TPREL16_HIGHEST:
2744     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2745     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2746     case elfcpp::R_PPC64_TPREL16_DS:
2747     case elfcpp::R_PPC64_TPREL16_LO_DS:
2748     case elfcpp::R_PPC64_DTPREL16_DS:
2749     case elfcpp::R_PPC64_DTPREL16_LO_DS:
2750     case elfcpp::R_PPC64_SECTOFF_DS:
2751     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2752     case elfcpp::R_PPC64_TLSGD:
2753     case elfcpp::R_PPC64_TLSLD:
2754       break;
2755
2756     case elfcpp::R_POWERPC_GOT16:
2757     case elfcpp::R_POWERPC_GOT16_LO:
2758     case elfcpp::R_POWERPC_GOT16_HI:
2759     case elfcpp::R_POWERPC_GOT16_HA:
2760     case elfcpp::R_PPC64_GOT16_DS:
2761     case elfcpp::R_PPC64_GOT16_LO_DS:
2762       {
2763         // The symbol requires a GOT entry.
2764         Output_data_got_powerpc<size, big_endian>* got;
2765
2766         got = target->got_section(symtab, layout);
2767         if (gsym->final_value_is_known())
2768           got->add_global(gsym, GOT_TYPE_STANDARD);
2769         else
2770           {
2771             // If this symbol is not fully resolved, we need to add a
2772             // dynamic relocation for it.
2773             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2774             if (gsym->is_from_dynobj()
2775                 || gsym->is_undefined()
2776                 || gsym->is_preemptible())
2777               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2778                                        elfcpp::R_POWERPC_GLOB_DAT);
2779             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2780               {
2781                 unsigned int off = got->add_constant(0);
2782
2783                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2784                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2785                                               got, off, 0, false);
2786               }
2787           }
2788       }
2789       break;
2790
2791     case elfcpp::R_PPC64_TOC16:
2792     case elfcpp::R_PPC64_TOC16_LO:
2793     case elfcpp::R_PPC64_TOC16_HI:
2794     case elfcpp::R_PPC64_TOC16_HA:
2795     case elfcpp::R_PPC64_TOC16_DS:
2796     case elfcpp::R_PPC64_TOC16_LO_DS:
2797       // We need a GOT section.
2798       target->got_section(symtab, layout);
2799       break;
2800
2801     case elfcpp::R_POWERPC_GOT_TLSGD16:
2802     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2803     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2804     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2805       {
2806         const bool final = gsym->final_value_is_known();
2807         const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2808         if (tls_type == tls::TLSOPT_NONE)
2809           {
2810             Output_data_got_powerpc<size, big_endian>* got
2811               = target->got_section(symtab, layout);
2812             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2813                                           target->rela_dyn_section(layout),
2814                                           elfcpp::R_POWERPC_DTPMOD,
2815                                           elfcpp::R_POWERPC_DTPREL);
2816           }
2817         else if (tls_type == tls::TLSOPT_TO_IE)
2818           {
2819             Output_data_got_powerpc<size, big_endian>* got
2820               = target->got_section(symtab, layout);
2821             got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2822                                      target->rela_dyn_section(layout),
2823                                      elfcpp::R_POWERPC_TPREL);
2824           }
2825         else if (tls_type == tls::TLSOPT_TO_LE)
2826           {
2827             // no GOT relocs needed for Local Exec.
2828           }
2829         else
2830           gold_unreachable();
2831       }
2832       break;
2833
2834     case elfcpp::R_POWERPC_GOT_TLSLD16:
2835     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2836     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2837     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2838       {
2839         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2840         if (tls_type == tls::TLSOPT_NONE)
2841           target->tlsld_got_offset(symtab, layout, object);
2842         else if (tls_type == tls::TLSOPT_TO_LE)
2843           {
2844             // no GOT relocs needed for Local Exec.
2845             if (parameters->options().emit_relocs())
2846               {
2847                 Output_section* os = layout->tls_segment()->first_section();
2848                 gold_assert(os != NULL);
2849                 os->set_needs_symtab_index();
2850               }
2851           }
2852         else
2853           gold_unreachable();
2854       }
2855       break;
2856
2857     case elfcpp::R_POWERPC_GOT_DTPREL16:
2858     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2859     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2860     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2861       {
2862         Output_data_got_powerpc<size, big_endian>* got
2863           = target->got_section(symtab, layout);
2864         got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2865                                  target->rela_dyn_section(layout),
2866                                  elfcpp::R_POWERPC_DTPREL);
2867       }
2868       break;
2869
2870     case elfcpp::R_POWERPC_GOT_TPREL16:
2871     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2872     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2873     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2874       {
2875         const bool final = gsym->final_value_is_known();
2876         const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2877         if (tls_type == tls::TLSOPT_NONE)
2878           {
2879             Output_data_got_powerpc<size, big_endian>* got
2880               = target->got_section(symtab, layout);
2881             got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2882                                      target->rela_dyn_section(layout),
2883                                      elfcpp::R_POWERPC_TPREL);
2884           }
2885         else if (tls_type == tls::TLSOPT_TO_LE)
2886           {
2887             // no GOT relocs needed for Local Exec.
2888           }
2889         else
2890           gold_unreachable();
2891       }
2892       break;
2893
2894     default:
2895       unsupported_reloc_global(object, r_type, gsym);
2896       break;
2897     }
2898 }
2899
2900 // Process relocations for gc.
2901
2902 template<int size, bool big_endian>
2903 void
2904 Target_powerpc<size, big_endian>::gc_process_relocs(
2905     Symbol_table* symtab,
2906     Layout* layout,
2907     Sized_relobj_file<size, big_endian>* object,
2908     unsigned int data_shndx,
2909     unsigned int,
2910     const unsigned char* prelocs,
2911     size_t reloc_count,
2912     Output_section* output_section,
2913     bool needs_special_offset_handling,
2914     size_t local_symbol_count,
2915     const unsigned char* plocal_symbols)
2916 {
2917   typedef Target_powerpc<size, big_endian> Powerpc;
2918   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2919
2920   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2921                           typename Target_powerpc::Relocatable_size_for_reloc>(
2922     symtab,
2923     layout,
2924     this,
2925     object,
2926     data_shndx,
2927     prelocs,
2928     reloc_count,
2929     output_section,
2930     needs_special_offset_handling,
2931     local_symbol_count,
2932     plocal_symbols);
2933 }
2934
2935 // Scan relocations for a section.
2936
2937 template<int size, bool big_endian>
2938 void
2939 Target_powerpc<size, big_endian>::scan_relocs(
2940     Symbol_table* symtab,
2941     Layout* layout,
2942     Sized_relobj_file<size, big_endian>* object,
2943     unsigned int data_shndx,
2944     unsigned int sh_type,
2945     const unsigned char* prelocs,
2946     size_t reloc_count,
2947     Output_section* output_section,
2948     bool needs_special_offset_handling,
2949     size_t local_symbol_count,
2950     const unsigned char* plocal_symbols)
2951 {
2952   typedef Target_powerpc<size, big_endian> Powerpc;
2953   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2954
2955   if (sh_type == elfcpp::SHT_REL)
2956     {
2957       gold_error(_("%s: unsupported REL reloc section"),
2958                  object->name().c_str());
2959       return;
2960     }
2961
2962   if (size == 32)
2963     {
2964       static Output_data_space* sdata;
2965
2966       // Define _SDA_BASE_ at the start of the .sdata section.
2967       if (sdata == NULL)
2968         {
2969           // layout->find_output_section(".sdata") == NULL
2970           sdata = new Output_data_space(4, "** sdata");
2971           Output_section* os
2972             = layout->add_output_section_data(".sdata", 0,
2973                                               elfcpp::SHF_ALLOC
2974                                               | elfcpp::SHF_WRITE,
2975                                               sdata, ORDER_SMALL_DATA, false);
2976           symtab->define_in_output_data("_SDA_BASE_", NULL,
2977                                         Symbol_table::PREDEFINED,
2978                                         os, 32768, 0, elfcpp::STT_OBJECT,
2979                                         elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2980                                         0, false, false);
2981         }
2982     }
2983
2984   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2985     symtab,
2986     layout,
2987     this,
2988     object,
2989     data_shndx,
2990     prelocs,
2991     reloc_count,
2992     output_section,
2993     needs_special_offset_handling,
2994     local_symbol_count,
2995     plocal_symbols);
2996 }
2997
2998 // Finalize the sections.
2999
3000 template<int size, bool big_endian>
3001 void
3002 Target_powerpc<size, big_endian>::do_finalize_sections(
3003     Layout* layout,
3004     const Input_objects*,
3005     Symbol_table*)
3006 {
3007   // Fill in some more dynamic tags.
3008   const Reloc_section* rel_plt = (this->plt_ == NULL
3009                                   ? NULL
3010                                   : this->plt_->rel_plt());
3011   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3012                                   this->rela_dyn_, true, size == 32);
3013
3014   Output_data_dynamic* odyn = layout->dynamic_data();
3015   if (size == 32)
3016     {
3017       if (this->got_ != NULL)
3018         {
3019           this->got_->finalize_data_size();
3020           odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3021                                         this->got_, this->got_->g_o_t());
3022         }
3023     }
3024   else
3025     {
3026       if (this->glink_ != NULL)
3027         {
3028           this->glink_->finalize_data_size();
3029           odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3030                                         this->glink_,
3031                                         (this->glink_->pltresolve()
3032                                          + this->glink_->pltresolve_size - 32));
3033         }
3034     }
3035
3036   // Emit any relocs we saved in an attempt to avoid generating COPY
3037   // relocs.
3038   if (this->copy_relocs_.any_saved_relocs())
3039     this->copy_relocs_.emit(this->rela_dyn_section(layout));
3040 }
3041
3042 // Return the value to use for a branch relocation.
3043
3044 template<int size, bool big_endian>
3045 typename elfcpp::Elf_types<size>::Elf_Addr
3046 Target_powerpc<size, big_endian>::symval_for_branch(
3047     Address value,
3048     const Sized_symbol<size>* gsym,
3049     Powerpc_relobj<size, big_endian>* object,
3050     unsigned int *dest_shndx)
3051 {
3052   *dest_shndx = 0;
3053   if (size == 32)
3054     return value;
3055
3056   // If the symbol is defined in an opd section, ie. is a function
3057   // descriptor, use the function descriptor code entry address
3058   Powerpc_relobj<size, big_endian>* symobj = object;
3059   if (gsym != NULL)
3060     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
3061   unsigned int shndx = symobj->opd_shndx();
3062   if (shndx == 0)
3063     return value;
3064   Address opd_addr = symobj->get_output_section_offset(shndx);
3065   gold_assert(opd_addr != invalid_address);
3066   opd_addr += symobj->output_section(shndx)->address();
3067   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
3068     {
3069       Address sec_off;
3070       symobj->get_opd_ent(value - opd_addr, dest_shndx, &sec_off);
3071       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
3072       gold_assert(sec_addr != invalid_address);
3073       sec_addr += symobj->output_section(*dest_shndx)->address();
3074       value = sec_addr + sec_off;
3075     }
3076   return value;
3077 }
3078
3079 // Perform a relocation.
3080
3081 template<int size, bool big_endian>
3082 inline bool
3083 Target_powerpc<size, big_endian>::Relocate::relocate(
3084     const Relocate_info<size, big_endian>* relinfo,
3085     Target_powerpc* target,
3086     Output_section* os,
3087     size_t relnum,
3088     const elfcpp::Rela<size, big_endian>& rela,
3089     unsigned int r_type,
3090     const Sized_symbol<size>* gsym,
3091     const Symbol_value<size>* psymval,
3092     unsigned char* view,
3093     Address address,
3094     section_size_type view_size)
3095 {
3096
3097   bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3098                        || r_type == elfcpp::R_PPC_PLTREL24)
3099                       && gsym != NULL
3100                       && strcmp(gsym->name(), "__tls_get_addr") == 0);
3101   enum skip_tls last_tls = this->call_tls_get_addr_;
3102   this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3103   if (is_tls_call)
3104     {
3105       if (last_tls == CALL_NOT_EXPECTED)
3106         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3107                                _("__tls_get_addr call lacks marker reloc"));
3108       else if (last_tls == CALL_SKIP)
3109         return false;
3110     }
3111   else if (last_tls != CALL_NOT_EXPECTED)
3112     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3113                            _("missing expected __tls_get_addr call"));
3114
3115   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
3116   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3117   Powerpc_relobj<size, big_endian>* const object
3118     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3119   Address value = 0;
3120   bool has_plt_value = false;
3121   if (gsym != NULL
3122       && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3123     {
3124       const Output_data_glink<size, big_endian>* glink
3125         = target->glink_section();
3126       unsigned int glink_index = glink->find_entry(gsym, rela, object);
3127       value = glink->address() + glink_index * glink->glink_entry_size();
3128       has_plt_value = true;
3129     }
3130
3131   if (r_type == elfcpp::R_POWERPC_GOT16
3132       || r_type == elfcpp::R_POWERPC_GOT16_LO
3133       || r_type == elfcpp::R_POWERPC_GOT16_HI
3134       || r_type == elfcpp::R_POWERPC_GOT16_HA
3135       || r_type == elfcpp::R_PPC64_GOT16_DS
3136       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
3137     {
3138       if (gsym != NULL)
3139         {
3140           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3141           value = gsym->got_offset(GOT_TYPE_STANDARD);
3142         }
3143       else
3144         {
3145           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3146           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3147           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3148         }
3149       value -= target->got_section()->got_base_offset(object);
3150     }
3151   else if (r_type == elfcpp::R_PPC64_TOC)
3152     {
3153       value = (target->got_section()->output_section()->address()
3154                + object->toc_base_offset());
3155     }
3156   else if (gsym != NULL
3157            && (r_type == elfcpp::R_POWERPC_REL24
3158                || r_type == elfcpp::R_PPC_PLTREL24)
3159            && has_plt_value)
3160     {
3161       if (size == 64)
3162         {
3163           typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3164           Valtype* wv = reinterpret_cast<Valtype*>(view);
3165           bool can_plt_call = false;
3166           if (rela.get_r_offset() + 8 <= view_size)
3167             {
3168               Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3169               Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3170               if ((insn & 1) != 0
3171                   && (insn2 == nop
3172                       || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
3173                 {
3174                   elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3175                   can_plt_call = true;
3176                 }
3177             }
3178           if (!can_plt_call)
3179             {
3180               // If we don't have a branch and link followed by a nop,
3181               // we can't go via the plt because there is no place to
3182               // put a toc restoring instruction.
3183               // Unless we know we won't be returning.
3184               if (strcmp(gsym->name(), "__libc_start_main") == 0)
3185                 can_plt_call = true;
3186             }
3187           if (!can_plt_call)
3188             {
3189               // This is not an error in one special case: A self
3190               // call.  It isn't possible to cheaply verify we have
3191               // such a call so just check for a call to the same
3192               // section.
3193               bool ok = false;
3194               if (gsym->source() == Symbol::FROM_OBJECT
3195                   && gsym->object() == object)
3196                 {
3197                   Address addend = rela.get_r_addend();
3198                   unsigned int dest_shndx;
3199                   value = psymval->value(object, addend);
3200                   value = target->symval_for_branch(value, gsym, object,
3201                                                     &dest_shndx);
3202                   bool is_ordinary;
3203                   if (dest_shndx == 0)
3204                     dest_shndx = gsym->shndx(&is_ordinary);
3205                   ok = dest_shndx == relinfo->data_shndx;
3206                 }
3207               if (!ok)
3208                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3209                                        _("call lacks nop, can't restore toc; "
3210                                          "recompile with -fPIC"));
3211             }
3212         }
3213     }
3214   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3215            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3216            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3217            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3218     {
3219       // First instruction of a global dynamic sequence, arg setup insn.
3220       const bool final = gsym == NULL || gsym->final_value_is_known();
3221       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3222       enum Got_type got_type = GOT_TYPE_STANDARD;
3223       if (tls_type == tls::TLSOPT_NONE)
3224         got_type = GOT_TYPE_TLSGD;
3225       else if (tls_type == tls::TLSOPT_TO_IE)
3226         got_type = GOT_TYPE_TPREL;
3227       if (got_type != GOT_TYPE_STANDARD)
3228         {
3229           if (gsym != NULL)
3230             {
3231               gold_assert(gsym->has_got_offset(got_type));
3232               value = gsym->got_offset(got_type);
3233             }
3234           else
3235             {
3236               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3237               gold_assert(object->local_has_got_offset(r_sym, got_type));
3238               value = object->local_got_offset(r_sym, got_type);
3239             }
3240           value -= target->got_section()->got_base_offset(object);
3241         }
3242       if (tls_type == tls::TLSOPT_TO_IE)
3243         {
3244           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3245               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3246             {
3247               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3248               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3249               insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3250               if (size == 32)
3251                 insn |= 32 << 26; // lwz
3252               else
3253                 insn |= 58 << 26; // ld
3254               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3255             }
3256           r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3257                      - elfcpp::R_POWERPC_GOT_TLSGD16);
3258         }
3259       else if (tls_type == tls::TLSOPT_TO_LE)
3260         {
3261           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3262               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3263             {
3264               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3265               Insn insn = addis_3_13;
3266               if (size == 32)
3267                 insn = addis_3_2;
3268               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3269               r_type = elfcpp::R_POWERPC_TPREL16_HA;
3270               value = psymval->value(object, rela.get_r_addend());
3271             }
3272           else
3273             {
3274               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3275               Insn insn = nop;
3276               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3277               r_type = elfcpp::R_POWERPC_NONE;
3278             }
3279         }
3280     }
3281   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3282            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3283            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3284            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3285     {
3286       // First instruction of a local dynamic sequence, arg setup insn.
3287       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3288       if (tls_type == tls::TLSOPT_NONE)
3289         {
3290           value = target->tlsld_got_offset();
3291           value -= target->got_section()->got_base_offset(object);
3292         }
3293       else
3294         {
3295           gold_assert(tls_type == tls::TLSOPT_TO_LE);
3296           if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3297               || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3298             {
3299               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3300               Insn insn = addis_3_13;
3301               if (size == 32)
3302                 insn = addis_3_2;
3303               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3304               r_type = elfcpp::R_POWERPC_TPREL16_HA;
3305               value = dtp_offset;
3306             }
3307           else
3308             {
3309               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3310               Insn insn = nop;
3311               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3312               r_type = elfcpp::R_POWERPC_NONE;
3313             }
3314         }
3315     }
3316   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3317            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3318            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3319            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3320     {
3321       // Accesses relative to a local dynamic sequence address,
3322       // no optimisation here.
3323       if (gsym != NULL)
3324         {
3325           gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3326           value = gsym->got_offset(GOT_TYPE_DTPREL);
3327         }
3328       else
3329         {
3330           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3331           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3332           value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3333         }
3334       value -= target->got_section()->got_base_offset(object);
3335     }
3336   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3337            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3338            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3339            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3340     {
3341       // First instruction of initial exec sequence.
3342       const bool final = gsym == NULL || gsym->final_value_is_known();
3343       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3344       if (tls_type == tls::TLSOPT_NONE)
3345         {
3346           if (gsym != NULL)
3347             {
3348               gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3349               value = gsym->got_offset(GOT_TYPE_TPREL);
3350             }
3351           else
3352             {
3353               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3354               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3355               value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3356             }
3357           value -= target->got_section()->got_base_offset(object);
3358         }
3359       else
3360         {
3361           gold_assert(tls_type == tls::TLSOPT_TO_LE);
3362           if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3363               || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3364             {
3365               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3366               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3367               insn &= (1 << 26) - (1 << 21); // extract rt from ld
3368               if (size == 32)
3369                 insn |= addis_0_2;
3370               else
3371                 insn |= addis_0_13;
3372               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3373               r_type = elfcpp::R_POWERPC_TPREL16_HA;
3374               value = psymval->value(object, rela.get_r_addend());
3375             }
3376           else
3377             {
3378               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3379               Insn insn = nop;
3380               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3381               r_type = elfcpp::R_POWERPC_NONE;
3382             }
3383         }
3384     }
3385   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3386            || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3387     {
3388       // Second instruction of a global dynamic sequence,
3389       // the __tls_get_addr call
3390       this->call_tls_get_addr_ = CALL_EXPECTED;
3391       const bool final = gsym == NULL || gsym->final_value_is_known();
3392       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3393       if (tls_type != tls::TLSOPT_NONE)
3394         {
3395           if (tls_type == tls::TLSOPT_TO_IE)
3396             {
3397               Insn* iview = reinterpret_cast<Insn*>(view);
3398               Insn insn = add_3_3_13;
3399               if (size == 32)
3400                 insn = add_3_3_2;
3401               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3402               r_type = elfcpp::R_POWERPC_NONE;
3403             }
3404           else
3405             {
3406               Insn* iview = reinterpret_cast<Insn*>(view);
3407               Insn insn = addi_3_3;
3408               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3409               r_type = elfcpp::R_POWERPC_TPREL16_LO;
3410               view += 2 * big_endian;
3411               value = psymval->value(object, rela.get_r_addend());
3412             }
3413           this->call_tls_get_addr_ = CALL_SKIP;
3414         }
3415     }
3416   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3417            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3418     {
3419       // Second instruction of a local dynamic sequence,
3420       // the __tls_get_addr call
3421       this->call_tls_get_addr_ = CALL_EXPECTED;
3422       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3423       if (tls_type == tls::TLSOPT_TO_LE)
3424         {
3425           Insn* iview = reinterpret_cast<Insn*>(view);
3426           Insn insn = addi_3_3;
3427           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3428           this->call_tls_get_addr_ = CALL_SKIP;
3429           r_type = elfcpp::R_POWERPC_TPREL16_LO;
3430           view += 2 * big_endian;
3431           value = dtp_offset;
3432         }
3433     }
3434   else if (r_type == elfcpp::R_POWERPC_TLS)
3435     {
3436       // Second instruction of an initial exec sequence
3437       const bool final = gsym == NULL || gsym->final_value_is_known();
3438       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3439       if (tls_type == tls::TLSOPT_TO_LE)
3440         {
3441           Insn* iview = reinterpret_cast<Insn*>(view);
3442           Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3443           unsigned int reg = size == 32 ? 2 : 13;
3444           insn = at_tls_transform(insn, reg);
3445           gold_assert(insn != 0);
3446           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3447           r_type = elfcpp::R_POWERPC_TPREL16_LO;
3448           view += 2 * big_endian;
3449           value = psymval->value(object, rela.get_r_addend());
3450         }
3451     }
3452   else
3453     {
3454       Address addend = 0;
3455       unsigned int dest_shndx;
3456       if (r_type != elfcpp::R_PPC_PLTREL24)
3457         addend = rela.get_r_addend();
3458       if (size == 64 || !has_plt_value)
3459         value = psymval->value(object, addend);
3460       if (size == 64 && is_branch_reloc(r_type))
3461         value = target->symval_for_branch(value, gsym, object, &dest_shndx);
3462     }
3463
3464   switch (r_type)
3465     {
3466     case elfcpp::R_PPC64_REL64:
3467     case elfcpp::R_POWERPC_REL32:
3468     case elfcpp::R_POWERPC_REL24:
3469     case elfcpp::R_PPC_PLTREL24:
3470     case elfcpp::R_PPC_LOCAL24PC:
3471     case elfcpp::R_POWERPC_REL16:
3472     case elfcpp::R_POWERPC_REL16_LO:
3473     case elfcpp::R_POWERPC_REL16_HI:
3474     case elfcpp::R_POWERPC_REL16_HA:
3475     case elfcpp::R_POWERPC_REL14:
3476     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3477     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3478       value -= address;
3479       break;
3480
3481     case elfcpp::R_PPC64_TOC16:
3482     case elfcpp::R_PPC64_TOC16_LO:
3483     case elfcpp::R_PPC64_TOC16_HI:
3484     case elfcpp::R_PPC64_TOC16_HA:
3485     case elfcpp::R_PPC64_TOC16_DS:
3486     case elfcpp::R_PPC64_TOC16_LO_DS:
3487       // Subtract the TOC base address.
3488       value -= (target->got_section()->output_section()->address()
3489                 + object->toc_base_offset());
3490       break;
3491
3492     case elfcpp::R_POWERPC_SECTOFF:
3493     case elfcpp::R_POWERPC_SECTOFF_LO:
3494     case elfcpp::R_POWERPC_SECTOFF_HI:
3495     case elfcpp::R_POWERPC_SECTOFF_HA:
3496     case elfcpp::R_PPC64_SECTOFF_DS:
3497     case elfcpp::R_PPC64_SECTOFF_LO_DS:
3498       if (os != NULL)
3499         value -= os->address();
3500       break;
3501
3502     case elfcpp::R_PPC64_TPREL16_DS:
3503     case elfcpp::R_PPC64_TPREL16_LO_DS:
3504       if (size != 64)
3505         // R_PPC_TLSGD and R_PPC_TLSLD
3506         break;
3507     case elfcpp::R_POWERPC_TPREL16:
3508     case elfcpp::R_POWERPC_TPREL16_LO:
3509     case elfcpp::R_POWERPC_TPREL16_HI:
3510     case elfcpp::R_POWERPC_TPREL16_HA:
3511     case elfcpp::R_POWERPC_TPREL:
3512     case elfcpp::R_PPC64_TPREL16_HIGHER:
3513     case elfcpp::R_PPC64_TPREL16_HIGHERA:
3514     case elfcpp::R_PPC64_TPREL16_HIGHEST:
3515     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3516       // tls symbol values are relative to tls_segment()->vaddr()
3517       value -= tp_offset;
3518       break;
3519
3520     case elfcpp::R_PPC64_DTPREL16_DS:
3521     case elfcpp::R_PPC64_DTPREL16_LO_DS:
3522     case elfcpp::R_PPC64_DTPREL16_HIGHER:
3523     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3524     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3525     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3526       if (size != 64)
3527         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3528         // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3529         break;
3530     case elfcpp::R_POWERPC_DTPREL16:
3531     case elfcpp::R_POWERPC_DTPREL16_LO:
3532     case elfcpp::R_POWERPC_DTPREL16_HI:
3533     case elfcpp::R_POWERPC_DTPREL16_HA:
3534     case elfcpp::R_POWERPC_DTPREL:
3535       // tls symbol values are relative to tls_segment()->vaddr()
3536       value -= dtp_offset;
3537       break;
3538
3539     default:
3540       break;
3541     }
3542
3543   Insn branch_bit = 0;
3544   switch (r_type)
3545     {
3546     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3547     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3548       branch_bit = 1 << 21;
3549     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3550     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3551       {
3552         Insn* iview = reinterpret_cast<Insn*>(view);
3553         Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3554         insn &= ~(1 << 21);
3555         insn |= branch_bit;
3556         if (this->is_isa_v2)
3557           {
3558             // Set 'a' bit.  This is 0b00010 in BO field for branch
3559             // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3560             // for branch on CTR insns (BO == 1a00t or 1a01t).
3561             if ((insn & (0x14 << 21)) == (0x04 << 21))
3562               insn |= 0x02 << 21;
3563             else if ((insn & (0x14 << 21)) == (0x10 << 21))
3564               insn |= 0x08 << 21;
3565             else
3566               break;
3567           }
3568         else
3569           {
3570             // Invert 'y' bit if not the default.
3571             if (static_cast<Signed_address>(value) < 0)
3572               insn ^= 1 << 21;
3573           }
3574         elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3575       }
3576       break;
3577
3578     default:
3579       break;
3580     }
3581
3582   enum Reloc::overflow_check overflow = Reloc::check_none;
3583   switch (r_type)
3584     {
3585     case elfcpp::R_POWERPC_ADDR32:
3586     case elfcpp::R_POWERPC_UADDR32:
3587       if (size == 64)
3588         overflow = Reloc::check_bitfield;
3589       break;
3590
3591     case elfcpp::R_POWERPC_REL32:
3592       if (size == 64)
3593         overflow = Reloc::check_signed;
3594       break;
3595
3596     case elfcpp::R_POWERPC_ADDR24:
3597     case elfcpp::R_POWERPC_ADDR16:
3598     case elfcpp::R_POWERPC_UADDR16:
3599     case elfcpp::R_PPC64_ADDR16_DS:
3600     case elfcpp::R_POWERPC_ADDR14:
3601     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3602     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3603       overflow = Reloc::check_bitfield;
3604       break;
3605
3606     case elfcpp::R_POWERPC_REL24:
3607     case elfcpp::R_PPC_PLTREL24:
3608     case elfcpp::R_PPC_LOCAL24PC:
3609     case elfcpp::R_POWERPC_REL16:
3610     case elfcpp::R_PPC64_TOC16:
3611     case elfcpp::R_POWERPC_GOT16:
3612     case elfcpp::R_POWERPC_SECTOFF:
3613     case elfcpp::R_POWERPC_TPREL16:
3614     case elfcpp::R_POWERPC_DTPREL16:
3615     case elfcpp::R_PPC64_TPREL16_DS:
3616     case elfcpp::R_PPC64_DTPREL16_DS:
3617     case elfcpp::R_PPC64_TOC16_DS:
3618     case elfcpp::R_PPC64_GOT16_DS:
3619     case elfcpp::R_PPC64_SECTOFF_DS:
3620     case elfcpp::R_POWERPC_REL14:
3621     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3622     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3623     case elfcpp::R_POWERPC_GOT_TLSGD16:
3624     case elfcpp::R_POWERPC_GOT_TLSLD16:
3625     case elfcpp::R_POWERPC_GOT_TPREL16:
3626     case elfcpp::R_POWERPC_GOT_DTPREL16:
3627       overflow = Reloc::check_signed;
3628       break;
3629     }
3630
3631   typename Powerpc_relocate_functions<size, big_endian>::Status status
3632     = Powerpc_relocate_functions<size, big_endian>::status_ok;
3633   switch (r_type)
3634     {
3635     case elfcpp::R_POWERPC_NONE:
3636     case elfcpp::R_POWERPC_TLS:
3637     case elfcpp::R_POWERPC_GNU_VTINHERIT:
3638     case elfcpp::R_POWERPC_GNU_VTENTRY:
3639     case elfcpp::R_PPC_EMB_MRKREF:
3640       break;
3641
3642     case elfcpp::R_PPC64_ADDR64:
3643     case elfcpp::R_PPC64_REL64:
3644     case elfcpp::R_PPC64_TOC:
3645       Reloc::addr64(view, value);
3646       break;
3647
3648     case elfcpp::R_POWERPC_TPREL:
3649     case elfcpp::R_POWERPC_DTPREL:
3650       if (size == 64)
3651         Reloc::addr64(view, value);
3652       else
3653         status = Reloc::addr32(view, value, overflow);
3654       break;
3655
3656     case elfcpp::R_PPC64_UADDR64:
3657       Reloc::addr64_u(view, value);
3658       break;
3659
3660     case elfcpp::R_POWERPC_ADDR32:
3661     case elfcpp::R_POWERPC_REL32:
3662       status = Reloc::addr32(view, value, overflow);
3663       break;
3664
3665     case elfcpp::R_POWERPC_UADDR32:
3666       status = Reloc::addr32_u(view, value, overflow);
3667       break;
3668
3669     case elfcpp::R_POWERPC_ADDR24:
3670     case elfcpp::R_POWERPC_REL24:
3671     case elfcpp::R_PPC_PLTREL24:
3672     case elfcpp::R_PPC_LOCAL24PC:
3673       status = Reloc::addr24(view, value, overflow);
3674       break;
3675
3676     case elfcpp::R_POWERPC_GOT_DTPREL16:
3677     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3678       if (size == 64)
3679         {
3680           status = Reloc::addr16_ds(view, value, overflow);
3681           break;
3682         }
3683     case elfcpp::R_POWERPC_ADDR16:
3684     case elfcpp::R_POWERPC_REL16:
3685     case elfcpp::R_PPC64_TOC16:
3686     case elfcpp::R_POWERPC_GOT16:
3687     case elfcpp::R_POWERPC_SECTOFF:
3688     case elfcpp::R_POWERPC_TPREL16:
3689     case elfcpp::R_POWERPC_DTPREL16:
3690     case elfcpp::R_POWERPC_GOT_TLSGD16:
3691     case elfcpp::R_POWERPC_GOT_TLSLD16:
3692     case elfcpp::R_POWERPC_GOT_TPREL16:
3693     case elfcpp::R_POWERPC_ADDR16_LO:
3694     case elfcpp::R_POWERPC_REL16_LO:
3695     case elfcpp::R_PPC64_TOC16_LO:
3696     case elfcpp::R_POWERPC_GOT16_LO:
3697     case elfcpp::R_POWERPC_SECTOFF_LO:
3698     case elfcpp::R_POWERPC_TPREL16_LO:
3699     case elfcpp::R_POWERPC_DTPREL16_LO:
3700     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3701     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3702     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3703       status = Reloc::addr16(view, value, overflow);
3704       break;
3705
3706     case elfcpp::R_POWERPC_UADDR16:
3707       status = Reloc::addr16_u(view, value, overflow);
3708       break;
3709
3710     case elfcpp::R_POWERPC_ADDR16_HI:
3711     case elfcpp::R_POWERPC_REL16_HI:
3712     case elfcpp::R_PPC64_TOC16_HI:
3713     case elfcpp::R_POWERPC_GOT16_HI:
3714     case elfcpp::R_POWERPC_SECTOFF_HI:
3715     case elfcpp::R_POWERPC_TPREL16_HI:
3716     case elfcpp::R_POWERPC_DTPREL16_HI:
3717     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3718     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3719     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3720     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3721       Reloc::addr16_hi(view, value);
3722       break;
3723
3724     case elfcpp::R_POWERPC_ADDR16_HA:
3725     case elfcpp::R_POWERPC_REL16_HA:
3726     case elfcpp::R_PPC64_TOC16_HA:
3727     case elfcpp::R_POWERPC_GOT16_HA:
3728     case elfcpp::R_POWERPC_SECTOFF_HA:
3729     case elfcpp::R_POWERPC_TPREL16_HA:
3730     case elfcpp::R_POWERPC_DTPREL16_HA:
3731     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3732     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3733     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3734     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3735       Reloc::addr16_ha(view, value);
3736       break;
3737
3738     case elfcpp::R_PPC64_DTPREL16_HIGHER:
3739       if (size == 32)
3740         // R_PPC_EMB_NADDR16_LO
3741         goto unsupp;
3742     case elfcpp::R_PPC64_ADDR16_HIGHER:
3743     case elfcpp::R_PPC64_TPREL16_HIGHER:
3744       Reloc::addr16_hi2(view, value);
3745       break;
3746
3747     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3748       if (size == 32)
3749         // R_PPC_EMB_NADDR16_HI
3750         goto unsupp;
3751     case elfcpp::R_PPC64_ADDR16_HIGHERA:
3752     case elfcpp::R_PPC64_TPREL16_HIGHERA:
3753       Reloc::addr16_ha2(view, value);
3754       break;
3755
3756     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3757       if (size == 32)
3758         // R_PPC_EMB_NADDR16_HA
3759         goto unsupp;
3760     case elfcpp::R_PPC64_ADDR16_HIGHEST:
3761     case elfcpp::R_PPC64_TPREL16_HIGHEST:
3762       Reloc::addr16_hi3(view, value);
3763       break;
3764
3765     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3766       if (size == 32)
3767         // R_PPC_EMB_SDAI16
3768         goto unsupp;
3769     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3770     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3771       Reloc::addr16_ha3(view, value);
3772       break;
3773
3774     case elfcpp::R_PPC64_DTPREL16_DS:
3775     case elfcpp::R_PPC64_DTPREL16_LO_DS:
3776       if (size == 32)
3777         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3778         goto unsupp;
3779     case elfcpp::R_PPC64_TPREL16_DS:
3780     case elfcpp::R_PPC64_TPREL16_LO_DS:
3781       if (size == 32)
3782         // R_PPC_TLSGD, R_PPC_TLSLD
3783         break;
3784     case elfcpp::R_PPC64_ADDR16_DS:
3785     case elfcpp::R_PPC64_ADDR16_LO_DS:
3786     case elfcpp::R_PPC64_TOC16_DS:
3787     case elfcpp::R_PPC64_TOC16_LO_DS:
3788     case elfcpp::R_PPC64_GOT16_DS:
3789     case elfcpp::R_PPC64_GOT16_LO_DS:
3790     case elfcpp::R_PPC64_SECTOFF_DS:
3791     case elfcpp::R_PPC64_SECTOFF_LO_DS:
3792       status = Reloc::addr16_ds(view, value, overflow);
3793       break;
3794
3795     case elfcpp::R_POWERPC_ADDR14:
3796     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3797     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3798     case elfcpp::R_POWERPC_REL14:
3799     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3800     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3801       status = Reloc::addr14(view, value, overflow);
3802       break;
3803
3804     case elfcpp::R_POWERPC_COPY:
3805     case elfcpp::R_POWERPC_GLOB_DAT:
3806     case elfcpp::R_POWERPC_JMP_SLOT:
3807     case elfcpp::R_POWERPC_RELATIVE:
3808     case elfcpp::R_POWERPC_DTPMOD:
3809     case elfcpp::R_PPC64_JMP_IREL:
3810     case elfcpp::R_POWERPC_IRELATIVE:
3811       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3812                              _("unexpected reloc %u in object file"),
3813                              r_type);
3814       break;
3815
3816     case elfcpp::R_PPC_EMB_SDA21:
3817       if (size == 32)
3818         goto unsupp;
3819       else
3820         {
3821           // R_PPC64_TOCSAVE.  For the time being this can be ignored.
3822         }
3823       break;
3824
3825     case elfcpp::R_PPC_EMB_SDA2I16:
3826     case elfcpp::R_PPC_EMB_SDA2REL:
3827       if (size == 32)
3828         goto unsupp;
3829       // R_PPC64_TLSGD, R_PPC64_TLSLD
3830       break;
3831
3832     case elfcpp::R_POWERPC_PLT32:
3833     case elfcpp::R_POWERPC_PLTREL32:
3834     case elfcpp::R_POWERPC_PLT16_LO:
3835     case elfcpp::R_POWERPC_PLT16_HI:
3836     case elfcpp::R_POWERPC_PLT16_HA:
3837     case elfcpp::R_PPC_SDAREL16:
3838     case elfcpp::R_POWERPC_ADDR30:
3839     case elfcpp::R_PPC64_PLT64:
3840     case elfcpp::R_PPC64_PLTREL64:
3841     case elfcpp::R_PPC64_PLTGOT16:
3842     case elfcpp::R_PPC64_PLTGOT16_LO:
3843     case elfcpp::R_PPC64_PLTGOT16_HI:
3844     case elfcpp::R_PPC64_PLTGOT16_HA:
3845     case elfcpp::R_PPC64_PLT16_LO_DS:
3846     case elfcpp::R_PPC64_PLTGOT16_DS:
3847     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
3848     case elfcpp::R_PPC_EMB_RELSEC16:
3849     case elfcpp::R_PPC_EMB_RELST_LO:
3850     case elfcpp::R_PPC_EMB_RELST_HI:
3851     case elfcpp::R_PPC_EMB_RELST_HA:
3852     case elfcpp::R_PPC_EMB_BIT_FLD:
3853     case elfcpp::R_PPC_EMB_RELSDA:
3854     case elfcpp::R_PPC_TOC16:
3855     default:
3856     unsupp:
3857       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3858                              _("unsupported reloc %u"),
3859                              r_type);
3860       break;
3861     }
3862   if (status != Powerpc_relocate_functions<size, big_endian>::status_ok)
3863     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3864                            _("relocation overflow"));
3865
3866   return true;
3867 }
3868
3869 // Relocate section data.
3870
3871 template<int size, bool big_endian>
3872 void
3873 Target_powerpc<size, big_endian>::relocate_section(
3874     const Relocate_info<size, big_endian>* relinfo,
3875     unsigned int sh_type,
3876     const unsigned char* prelocs,
3877     size_t reloc_count,
3878     Output_section* output_section,
3879     bool needs_special_offset_handling,
3880     unsigned char* view,
3881     Address address,
3882     section_size_type view_size,
3883     const Reloc_symbol_changes* reloc_symbol_changes)
3884 {
3885   typedef Target_powerpc<size, big_endian> Powerpc;
3886   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
3887
3888   gold_assert(sh_type == elfcpp::SHT_RELA);
3889
3890   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
3891                          Powerpc_relocate>(
3892     relinfo,
3893     this,
3894     prelocs,
3895     reloc_count,
3896     output_section,
3897     needs_special_offset_handling,
3898     view,
3899     address,
3900     view_size,
3901     reloc_symbol_changes);
3902 }
3903
3904 class Powerpc_scan_relocatable_reloc
3905 {
3906 public:
3907   // Return the strategy to use for a local symbol which is not a
3908   // section symbol, given the relocation type.
3909   inline Relocatable_relocs::Reloc_strategy
3910   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
3911   {
3912     if (r_type == 0 && r_sym == 0)
3913       return Relocatable_relocs::RELOC_DISCARD;
3914     return Relocatable_relocs::RELOC_COPY;
3915   }
3916
3917   // Return the strategy to use for a local symbol which is a section
3918   // symbol, given the relocation type.
3919   inline Relocatable_relocs::Reloc_strategy
3920   local_section_strategy(unsigned int, Relobj*)
3921   {
3922     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
3923   }
3924
3925   // Return the strategy to use for a global symbol, given the
3926   // relocation type, the object, and the symbol index.
3927   inline Relocatable_relocs::Reloc_strategy
3928   global_strategy(unsigned int r_type, Relobj*, unsigned int)
3929   {
3930     if (r_type == elfcpp::R_PPC_PLTREL24)
3931       return Relocatable_relocs::RELOC_SPECIAL;
3932     return Relocatable_relocs::RELOC_COPY;
3933   }
3934 };
3935
3936 // Scan the relocs during a relocatable link.
3937
3938 template<int size, bool big_endian>
3939 void
3940 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
3941     Symbol_table* symtab,
3942     Layout* layout,
3943     Sized_relobj_file<size, big_endian>* object,
3944     unsigned int data_shndx,
3945     unsigned int sh_type,
3946     const unsigned char* prelocs,
3947     size_t reloc_count,
3948     Output_section* output_section,
3949     bool needs_special_offset_handling,
3950     size_t local_symbol_count,
3951     const unsigned char* plocal_symbols,
3952     Relocatable_relocs* rr)
3953 {
3954   gold_assert(sh_type == elfcpp::SHT_RELA);
3955
3956   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3957                                 Powerpc_scan_relocatable_reloc>(
3958     symtab,
3959     layout,
3960     object,
3961     data_shndx,
3962     prelocs,
3963     reloc_count,
3964     output_section,
3965     needs_special_offset_handling,
3966     local_symbol_count,
3967     plocal_symbols,
3968     rr);
3969 }
3970
3971 // Emit relocations for a section.
3972 // This is a modified version of the function by the same name in
3973 // target-reloc.h.  Using relocate_special_relocatable for
3974 // R_PPC_PLTREL24 would require duplication of the entire body of the
3975 // loop, so we may as well duplicate the whole thing.
3976
3977 template<int size, bool big_endian>
3978 void
3979 Target_powerpc<size, big_endian>::relocate_relocs(
3980     const Relocate_info<size, big_endian>* relinfo,
3981     unsigned int sh_type,
3982     const unsigned char* prelocs,
3983     size_t reloc_count,
3984     Output_section* output_section,
3985     off_t offset_in_output_section,
3986     const Relocatable_relocs* rr,
3987     unsigned char*,
3988     Address view_address,
3989     section_size_type,
3990     unsigned char* reloc_view,
3991     section_size_type reloc_view_size)
3992 {
3993   gold_assert(sh_type == elfcpp::SHT_RELA);
3994
3995   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
3996     Reltype;
3997   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
3998     Reltype_write;
3999   const int reloc_size
4000     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
4001
4002   Powerpc_relobj<size, big_endian>* const object
4003     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4004   const unsigned int local_count = object->local_symbol_count();
4005   unsigned int got2_shndx = object->got2_shndx();
4006   Address got2_addend = 0;
4007   if (got2_shndx != 0)
4008     {
4009       got2_addend = object->get_output_section_offset(got2_shndx);
4010       gold_assert(got2_addend != invalid_address);
4011     }
4012
4013   unsigned char* pwrite = reloc_view;
4014   bool zap_next = false;
4015   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
4016     {
4017       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
4018       if (strategy == Relocatable_relocs::RELOC_DISCARD)
4019         continue;
4020
4021       Reltype reloc(prelocs);
4022       Reltype_write reloc_write(pwrite);
4023
4024       Address offset = reloc.get_r_offset();
4025       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
4026       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4027       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4028       const unsigned int orig_r_sym = r_sym;
4029       typename elfcpp::Elf_types<size>::Elf_Swxword addend
4030         = reloc.get_r_addend();
4031       const Symbol* gsym = NULL;
4032
4033       if (zap_next)
4034         {
4035           // We could arrange to discard these and other relocs for
4036           // tls optimised sequences in the strategy methods, but for
4037           // now do as BFD ld does.
4038           r_type = elfcpp::R_POWERPC_NONE;
4039           zap_next = false;
4040         }
4041
4042       // Get the new symbol index.
4043       if (r_sym < local_count)
4044         {
4045           switch (strategy)
4046             {
4047             case Relocatable_relocs::RELOC_COPY:
4048             case Relocatable_relocs::RELOC_SPECIAL:
4049               if (r_sym != 0)
4050                 {
4051                   r_sym = object->symtab_index(r_sym);
4052                   gold_assert(r_sym != -1U);
4053                 }
4054               break;
4055
4056             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
4057               {
4058                 // We are adjusting a section symbol.  We need to find
4059                 // the symbol table index of the section symbol for
4060                 // the output section corresponding to input section
4061                 // in which this symbol is defined.
4062                 gold_assert(r_sym < local_count);
4063                 bool is_ordinary;
4064                 unsigned int shndx =
4065                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
4066                 gold_assert(is_ordinary);
4067                 Output_section* os = object->output_section(shndx);
4068                 gold_assert(os != NULL);
4069                 gold_assert(os->needs_symtab_index());
4070                 r_sym = os->symtab_index();
4071               }
4072               break;
4073
4074             default:
4075               gold_unreachable();
4076             }
4077         }
4078       else
4079         {
4080           gsym = object->global_symbol(r_sym);
4081           gold_assert(gsym != NULL);
4082           if (gsym->is_forwarder())
4083             gsym = relinfo->symtab->resolve_forwards(gsym);
4084
4085           gold_assert(gsym->has_symtab_index());
4086           r_sym = gsym->symtab_index();
4087         }
4088
4089       // Get the new offset--the location in the output section where
4090       // this relocation should be applied.
4091       if (static_cast<Address>(offset_in_output_section) != invalid_address)
4092         offset += offset_in_output_section;
4093       else
4094         {
4095           section_offset_type sot_offset =
4096             convert_types<section_offset_type, Address>(offset);
4097           section_offset_type new_sot_offset =
4098             output_section->output_offset(object, relinfo->data_shndx,
4099                                           sot_offset);
4100           gold_assert(new_sot_offset != -1);
4101           offset = new_sot_offset;
4102         }
4103
4104       // In an object file, r_offset is an offset within the section.
4105       // In an executable or dynamic object, generated by
4106       // --emit-relocs, r_offset is an absolute address.
4107       if (!parameters->options().relocatable())
4108         {
4109           offset += view_address;
4110           if (static_cast<Address>(offset_in_output_section) != invalid_address)
4111             offset -= offset_in_output_section;
4112         }
4113
4114       // Handle the reloc addend based on the strategy.
4115       if (strategy == Relocatable_relocs::RELOC_COPY)
4116         ;
4117       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4118         {
4119           const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
4120           addend = psymval->value(object, addend);
4121         }
4122       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4123         {
4124           if (addend >= 32768)
4125             addend += got2_addend;
4126         }
4127       else
4128         gold_unreachable();
4129
4130       if (!parameters->options().relocatable())
4131         {
4132           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4133               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4134               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4135               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4136             {
4137               // First instruction of a global dynamic sequence,
4138               // arg setup insn.
4139               const bool final = gsym == NULL || gsym->final_value_is_known();
4140               switch (this->optimize_tls_gd(final))
4141                 {
4142                 case tls::TLSOPT_TO_IE:
4143                   r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4144                              - elfcpp::R_POWERPC_GOT_TLSGD16);
4145                   break;
4146                 case tls::TLSOPT_TO_LE:
4147                   if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4148                       || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4149                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
4150                   else
4151                     {
4152                       r_type = elfcpp::R_POWERPC_NONE;
4153                       offset -= 2 * big_endian;
4154                     }
4155                   break;
4156                 default:
4157                   break;
4158                 }
4159             }
4160           else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4161                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4162                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4163                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4164             {
4165               // First instruction of a local dynamic sequence,
4166               // arg setup insn.
4167               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4168                 {
4169                   if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4170                       || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4171                     {
4172                       r_type = elfcpp::R_POWERPC_TPREL16_HA;
4173                       const Output_section* os = relinfo->layout->tls_segment()
4174                         ->first_section();
4175                       gold_assert(os != NULL);
4176                       gold_assert(os->needs_symtab_index());
4177                       r_sym = os->symtab_index();
4178                       addend = dtp_offset;
4179                     }
4180                   else
4181                     {
4182                       r_type = elfcpp::R_POWERPC_NONE;
4183                       offset -= 2 * big_endian;
4184                     }
4185                 }
4186             }
4187           else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4188                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4189                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4190                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4191             {
4192               // First instruction of initial exec sequence.
4193               const bool final = gsym == NULL || gsym->final_value_is_known();
4194               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4195                 {
4196                   if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4197                       || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4198                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
4199                   else
4200                     {
4201                       r_type = elfcpp::R_POWERPC_NONE;
4202                       offset -= 2 * big_endian;
4203                     }
4204                 }
4205             }
4206           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4207                    || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4208             {
4209               // Second instruction of a global dynamic sequence,
4210               // the __tls_get_addr call
4211               const bool final = gsym == NULL || gsym->final_value_is_known();
4212               switch (this->optimize_tls_gd(final))
4213                 {
4214                 case tls::TLSOPT_TO_IE:
4215                   r_type = elfcpp::R_POWERPC_NONE;
4216                   zap_next = true;
4217                   break;
4218                 case tls::TLSOPT_TO_LE:
4219                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
4220                   offset += 2 * big_endian;
4221                   zap_next = true;
4222                   break;
4223                 default:
4224                   break;
4225                 }
4226             }
4227           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4228                    || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4229             {
4230               // Second instruction of a local dynamic sequence,
4231               // the __tls_get_addr call
4232               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4233                 {
4234                   const Output_section* os = relinfo->layout->tls_segment()
4235                     ->first_section();
4236                   gold_assert(os != NULL);
4237                   gold_assert(os->needs_symtab_index());
4238                   r_sym = os->symtab_index();
4239                   addend = dtp_offset;
4240                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
4241                   offset += 2 * big_endian;
4242                   zap_next = true;
4243                 }
4244             }
4245           else if (r_type == elfcpp::R_POWERPC_TLS)
4246             {
4247               // Second instruction of an initial exec sequence
4248               const bool final = gsym == NULL || gsym->final_value_is_known();
4249               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4250                 {
4251                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
4252                   offset += 2 * big_endian;
4253                 }
4254             }
4255         }
4256
4257       reloc_write.put_r_offset(offset);
4258       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
4259       reloc_write.put_r_addend(addend);
4260
4261       pwrite += reloc_size;
4262     }
4263
4264   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4265               == reloc_view_size);
4266 }
4267
4268 // Return the value to use for a dynamic which requires special
4269 // treatment.  This is how we support equality comparisons of function
4270 // pointers across shared library boundaries, as described in the
4271 // processor specific ABI supplement.
4272
4273 template<int size, bool big_endian>
4274 uint64_t
4275 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4276 {
4277   if (size == 32)
4278     {
4279       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4280       return this->plt_section()->address() + gsym->plt_offset();
4281     }
4282   else
4283     gold_unreachable();
4284 }
4285
4286 // The selector for powerpc object files.
4287
4288 template<int size, bool big_endian>
4289 class Target_selector_powerpc : public Target_selector
4290 {
4291 public:
4292   Target_selector_powerpc()
4293     : Target_selector(elfcpp::EM_NONE, size, big_endian,
4294                       (size == 64
4295                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4296                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4297                       (size == 64
4298                        ? (big_endian ? "elf64ppc" : "elf64lppc")
4299                        : (big_endian ? "elf32ppc" : "elf32lppc")))
4300   { }
4301
4302   virtual Target*
4303   do_recognize(Input_file*, off_t, int machine, int, int)
4304   {
4305     switch (size)
4306       {
4307       case 64:
4308         if (machine != elfcpp::EM_PPC64)
4309           return NULL;
4310         break;
4311
4312       case 32:
4313         if (machine != elfcpp::EM_PPC)
4314           return NULL;
4315         break;
4316
4317       default:
4318         return NULL;
4319       }
4320
4321     return this->instantiate_target();
4322   }
4323
4324   virtual Target*
4325   do_instantiate_target()
4326   { return new Target_powerpc<size, big_endian>(); }
4327 };
4328
4329 Target_selector_powerpc<32, true> target_selector_ppc32;
4330 Target_selector_powerpc<32, false> target_selector_ppc32le;
4331 Target_selector_powerpc<64, true> target_selector_ppc64;
4332 Target_selector_powerpc<64, false> target_selector_ppc64le;
4333
4334 } // End anonymous namespace.