* target.h (Target::output_section_name): New function.
[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   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
61                  const typename elfcpp::Ehdr<size, big_endian>& ehdr)
62     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
63       got2_section_(0)
64   { }
65
66   ~Powerpc_relobj()
67   { }
68
69   unsigned int
70   got2_shndx() const
71   {
72     if (size == 32)
73       return this->got2_section_;
74     else
75       return 0;
76   }
77
78   bool
79   do_find_special_sections(Read_symbols_data* sd);
80
81 private:
82   unsigned int got2_section_;
83 };
84
85 template<int size, bool big_endian>
86 class Target_powerpc : public Sized_target<size, big_endian>
87 {
88  public:
89   typedef
90     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
91
92   Target_powerpc()
93     : Sized_target<size, big_endian>(&powerpc_info),
94       got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
95       copy_relocs_(elfcpp::R_POWERPC_COPY),
96       dynbss_(NULL), got_mod_index_offset_(-1U)
97   {
98   }
99
100   // Process the relocations to determine unreferenced sections for
101   // garbage collection.
102   void
103   gc_process_relocs(Symbol_table* symtab,
104                     Layout* layout,
105                     Sized_relobj_file<size, big_endian>* object,
106                     unsigned int data_shndx,
107                     unsigned int sh_type,
108                     const unsigned char* prelocs,
109                     size_t reloc_count,
110                     Output_section* output_section,
111                     bool needs_special_offset_handling,
112                     size_t local_symbol_count,
113                     const unsigned char* plocal_symbols);
114
115   // Scan the relocations to look for symbol adjustments.
116   void
117   scan_relocs(Symbol_table* symtab,
118               Layout* layout,
119               Sized_relobj_file<size, big_endian>* object,
120               unsigned int data_shndx,
121               unsigned int sh_type,
122               const unsigned char* prelocs,
123               size_t reloc_count,
124               Output_section* output_section,
125               bool needs_special_offset_handling,
126               size_t local_symbol_count,
127               const unsigned char* plocal_symbols);
128
129   // Map input .toc section to output .got section.
130   const char*
131   do_output_section_name(const Relobj*, const char* name, size_t* plen) const
132   {
133     if (size == 64 && strcmp(name, ".toc") == 0)
134       {
135         *plen = 4;
136         return ".got";
137       }
138     return NULL;
139   }
140
141   // Finalize the sections.
142   void
143   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
144
145   // Return the value to use for a dynamic which requires special
146   // treatment.
147   uint64_t
148   do_dynsym_value(const Symbol*) const;
149
150   // Relocate a section.
151   void
152   relocate_section(const Relocate_info<size, big_endian>*,
153                    unsigned int sh_type,
154                    const unsigned char* prelocs,
155                    size_t reloc_count,
156                    Output_section* output_section,
157                    bool needs_special_offset_handling,
158                    unsigned char* view,
159                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
160                    section_size_type view_size,
161                    const Reloc_symbol_changes*);
162
163   // Scan the relocs during a relocatable link.
164   void
165   scan_relocatable_relocs(Symbol_table* symtab,
166                           Layout* layout,
167                           Sized_relobj_file<size, big_endian>* object,
168                           unsigned int data_shndx,
169                           unsigned int sh_type,
170                           const unsigned char* prelocs,
171                           size_t reloc_count,
172                           Output_section* output_section,
173                           bool needs_special_offset_handling,
174                           size_t local_symbol_count,
175                           const unsigned char* plocal_symbols,
176                           Relocatable_relocs*);
177
178   // Relocate a section during a relocatable link.
179   void
180   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
181                            unsigned int sh_type,
182                            const unsigned char* prelocs,
183                            size_t reloc_count,
184                            Output_section* output_section,
185                            off_t offset_in_output_section,
186                            const Relocatable_relocs*,
187                            unsigned char*,
188                            typename elfcpp::Elf_types<size>::Elf_Addr,
189                            section_size_type,
190                            unsigned char* reloc_view,
191                            section_size_type reloc_view_size);
192
193   // Return whether SYM is defined by the ABI.
194   bool
195   do_is_defined_by_abi(const Symbol* sym) const
196   {
197     return strcmp(sym->name(), "__tls_get_addr") == 0;
198   }
199
200   // Return the size of the GOT section.
201   section_size_type
202   got_size() const
203   {
204     gold_assert(this->got_ != NULL);
205     return this->got_->data_size();
206   }
207
208   // Get the PLT section.
209   const Output_data_plt_powerpc<size, big_endian>*
210   plt_section() const
211   {
212     gold_assert(this->plt_ != NULL);
213     return this->plt_;
214   }
215
216   // Get the .glink section.
217   const Output_data_glink<size, big_endian>*
218   glink_section() const
219   {
220     gold_assert(this->glink_ != NULL);
221     return this->glink_;
222   }
223
224   // Get the GOT section.
225   const Output_data_got_powerpc<size, big_endian>*
226   got_section() const
227   {
228     gold_assert(this->got_ != NULL);
229     return this->got_;
230   }
231
232  protected:
233   Object*
234   do_make_elf_object(const std::string&, Input_file*, off_t,
235                      const elfcpp::Ehdr<size, big_endian>&);
236
237   // Return the number of entries in the GOT.
238   unsigned int
239   got_entry_count() const
240   {
241     if (this->got_ == NULL)
242       return 0;
243     return this->got_size() / (size / 8);
244   }
245
246   // Return the number of entries in the PLT.
247   unsigned int
248   plt_entry_count() const;
249
250   // Return the offset of the first non-reserved PLT entry.
251   unsigned int
252   first_plt_entry_offset() const;
253
254   // Return the size of each PLT entry.
255   unsigned int
256   plt_entry_size() const;
257
258  private:
259
260   // The class which scans relocations.
261   class Scan
262   {
263   public:
264     Scan()
265       : issued_non_pic_error_(false)
266     { }
267
268     static inline int
269     get_reference_flags(unsigned int r_type);
270
271     inline void
272     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
273           Sized_relobj_file<size, big_endian>* object,
274           unsigned int data_shndx,
275           Output_section* output_section,
276           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
277           const elfcpp::Sym<size, big_endian>& lsym);
278
279     inline void
280     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
281            Sized_relobj_file<size, big_endian>* object,
282            unsigned int data_shndx,
283            Output_section* output_section,
284            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
285            Symbol* gsym);
286
287     inline bool
288     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
289                                         Target_powerpc* ,
290                                         Sized_relobj_file<size, big_endian>* ,
291                                         unsigned int ,
292                                         Output_section* ,
293                                         const elfcpp::Rela<size, big_endian>& ,
294                                         unsigned int ,
295                                         const elfcpp::Sym<size, big_endian>&)
296     { return false; }
297
298     inline bool
299     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
300                                          Target_powerpc* ,
301                                          Sized_relobj_file<size, big_endian>* ,
302                                          unsigned int ,
303                                          Output_section* ,
304                                          const elfcpp::Rela<size,
305                                                             big_endian>& ,
306                                          unsigned int , Symbol*)
307     { return false; }
308
309   private:
310     static void
311     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
312                             unsigned int r_type);
313
314     static void
315     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
316                              unsigned int r_type, Symbol*);
317
318     static void
319     generate_tls_call(Symbol_table* symtab, Layout* layout,
320                       Target_powerpc* target);
321
322     void
323     check_non_pic(Relobj*, unsigned int r_type);
324
325     // Whether we have issued an error about a non-PIC compilation.
326     bool issued_non_pic_error_;
327   };
328
329   // The class which implements relocation.
330   class Relocate
331   {
332    public:
333     // Do a relocation.  Return false if the caller should not issue
334     // any warnings about this relocation.
335     inline bool
336     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
337              Output_section*, size_t relnum,
338              const elfcpp::Rela<size, big_endian>&,
339              unsigned int r_type, const Sized_symbol<size>*,
340              const Symbol_value<size>*,
341              unsigned char*,
342              typename elfcpp::Elf_types<size>::Elf_Addr,
343              section_size_type);
344
345    private:
346     // Do a TLS relocation.
347     inline void
348     relocate_tls(const Relocate_info<size, big_endian>*,
349                  Target_powerpc* target,
350                  size_t relnum, const elfcpp::Rela<size, big_endian>&,
351                  unsigned int r_type, const Sized_symbol<size>*,
352                  const Symbol_value<size>*,
353                  unsigned char*,
354                  typename elfcpp::Elf_types<size>::Elf_Addr,
355                  section_size_type);
356   };
357
358   // A class which returns the size required for a relocation type,
359   // used while scanning relocs during a relocatable link.
360   class Relocatable_size_for_reloc
361   {
362    public:
363     unsigned int
364     get_size_for_reloc(unsigned int, Relobj*)
365     {
366       gold_unreachable();
367       return 0;
368     }
369   };
370
371   // Adjust TLS relocation type based on the options and whether this
372   // is a local symbol.
373   static tls::Tls_optimization
374   optimize_tls_reloc(bool is_final, int r_type);
375
376   // Get the GOT section, creating it if necessary.
377   Output_data_got_powerpc<size, big_endian>*
378   got_section(Symbol_table*, Layout*);
379
380   // Create glink.
381   void
382   make_glink_section(Layout*);
383
384   // Create the PLT section.
385   void
386   make_plt_section(Layout*);
387
388   // Create a PLT entry for a global symbol.
389   void
390   make_plt_entry(Layout*, Symbol*,
391                  const elfcpp::Rela<size, big_endian>&,
392                  const Sized_relobj<size, big_endian>* object);
393
394   // Create a GOT entry for the TLS module index.
395   unsigned int
396   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
397                       Sized_relobj_file<size, big_endian>* object);
398
399   // Get the dynamic reloc section, creating it if necessary.
400   Reloc_section*
401   rela_dyn_section(Layout*);
402
403   // Copy a relocation against a global symbol.
404   void
405   copy_reloc(Symbol_table* symtab, Layout* layout,
406              Sized_relobj_file<size, big_endian>* object,
407              unsigned int shndx, Output_section* output_section,
408              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
409   {
410     this->copy_relocs_.copy_reloc(symtab, layout,
411                                   symtab->get_sized_symbol<size>(sym),
412                                   object, shndx, output_section,
413                                   reloc, this->rela_dyn_section(layout));
414   }
415
416   // Information about this specific target which we pass to the
417   // general Target structure.
418   static Target::Target_info powerpc_info;
419
420   // The types of GOT entries needed for this platform.
421   // These values are exposed to the ABI in an incremental link.
422   // Do not renumber existing values without changing the version
423   // number of the .gnu_incremental_inputs section.
424   enum Got_type
425   {
426     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
427     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
428     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
429   };
430
431   // The GOT output section.
432   Output_data_got_powerpc<size, big_endian>* got_;
433   // The PLT output section.
434   Output_data_plt_powerpc<size, big_endian>* plt_;
435   // The .glink output section.
436   Output_data_glink<size, big_endian>* glink_;
437   // The dynamic reloc output section.
438   Reloc_section* rela_dyn_;
439   // Relocs saved to avoid a COPY reloc.
440   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
441   // Space for variables copied with a COPY reloc.
442   Output_data_space* dynbss_;
443   // Offset of the GOT entry for the TLS module index;
444   unsigned int got_mod_index_offset_;
445 };
446
447 template<>
448 Target::Target_info Target_powerpc<32, true>::powerpc_info =
449 {
450   32,                   // size
451   true,                 // is_big_endian
452   elfcpp::EM_PPC,       // machine_code
453   false,                // has_make_symbol
454   false,                // has_resolve
455   false,                // has_code_fill
456   true,                 // is_default_stack_executable
457   false,                // can_icf_inline_merge_sections
458   '\0',                 // wrap_char
459   "/usr/lib/ld.so.1",   // dynamic_linker
460   0x10000000,           // default_text_segment_address
461   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
462   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
463   false,                // isolate_execinstr
464   0,                    // rosegment_gap
465   elfcpp::SHN_UNDEF,    // small_common_shndx
466   elfcpp::SHN_UNDEF,    // large_common_shndx
467   0,                    // small_common_section_flags
468   0,                    // large_common_section_flags
469   NULL,                 // attributes_section
470   NULL                  // attributes_vendor
471 };
472
473 template<>
474 Target::Target_info Target_powerpc<32, false>::powerpc_info =
475 {
476   32,                   // size
477   false,                // is_big_endian
478   elfcpp::EM_PPC,       // machine_code
479   false,                // has_make_symbol
480   false,                // has_resolve
481   false,                // has_code_fill
482   true,                 // is_default_stack_executable
483   false,                // can_icf_inline_merge_sections
484   '\0',                 // wrap_char
485   "/usr/lib/ld.so.1",   // dynamic_linker
486   0x10000000,           // default_text_segment_address
487   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
488   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
489   false,                // isolate_execinstr
490   0,                    // rosegment_gap
491   elfcpp::SHN_UNDEF,    // small_common_shndx
492   elfcpp::SHN_UNDEF,    // large_common_shndx
493   0,                    // small_common_section_flags
494   0,                    // large_common_section_flags
495   NULL,                 // attributes_section
496   NULL                  // attributes_vendor
497 };
498
499 template<>
500 Target::Target_info Target_powerpc<64, true>::powerpc_info =
501 {
502   64,                   // size
503   true,                 // is_big_endian
504   elfcpp::EM_PPC64,     // machine_code
505   false,                // has_make_symbol
506   false,                // has_resolve
507   false,                // has_code_fill
508   true,                 // is_default_stack_executable
509   false,                // can_icf_inline_merge_sections
510   '\0',                 // wrap_char
511   "/usr/lib/ld.so.1",   // dynamic_linker
512   0x10000000,           // default_text_segment_address
513   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
514   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
515   false,                // isolate_execinstr
516   0,                    // rosegment_gap
517   elfcpp::SHN_UNDEF,    // small_common_shndx
518   elfcpp::SHN_UNDEF,    // large_common_shndx
519   0,                    // small_common_section_flags
520   0,                    // large_common_section_flags
521   NULL,                 // attributes_section
522   NULL                  // attributes_vendor
523 };
524
525 template<>
526 Target::Target_info Target_powerpc<64, false>::powerpc_info =
527 {
528   64,                   // size
529   false,                // is_big_endian
530   elfcpp::EM_PPC64,     // machine_code
531   false,                // has_make_symbol
532   false,                // has_resolve
533   false,                // has_code_fill
534   true,                 // is_default_stack_executable
535   false,                // can_icf_inline_merge_sections
536   '\0',                 // wrap_char
537   "/usr/lib/ld.so.1",   // dynamic_linker
538   0x10000000,           // default_text_segment_address
539   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
540   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
541   false,                // isolate_execinstr
542   0,                    // rosegment_gap
543   elfcpp::SHN_UNDEF,    // small_common_shndx
544   elfcpp::SHN_UNDEF,    // large_common_shndx
545   0,                    // small_common_section_flags
546   0,                    // large_common_section_flags
547   NULL,                 // attributes_section
548   NULL                  // attributes_vendor
549 };
550
551 template<int size, bool big_endian>
552 class Powerpc_relocate_functions
553 {
554 private:
555   // Do a simple RELA relocation
556   template<int valsize>
557   static inline void
558   rela(unsigned char* view,
559        unsigned int right_shift,
560        elfcpp::Elf_Xword dst_mask,
561        typename elfcpp::Swap<size, big_endian>::Valtype value,
562        typename elfcpp::Swap<size, big_endian>::Valtype addend)
563   {
564     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
565     Valtype* wv = reinterpret_cast<Valtype*>(view);
566     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
567     Valtype reloc = (value + addend) >> right_shift;
568
569     val &= ~dst_mask;
570     reloc &= dst_mask;
571
572     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
573   }
574
575   // Do a simple RELA relocation, unaligned.
576   template<int valsize>
577   static inline void
578   rela_ua(unsigned char* view,
579           unsigned int right_shift,
580           elfcpp::Elf_Xword dst_mask,
581           typename elfcpp::Swap<size, big_endian>::Valtype value,
582           typename elfcpp::Swap<size, big_endian>::Valtype addend)
583   {
584     typedef typename elfcpp::Swap_unaligned<valsize,
585                                             big_endian>::Valtype Valtype;
586     Valtype* wv = reinterpret_cast<Valtype*>(view);
587     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
588     Valtype reloc = (value + addend) >> right_shift;
589
590     val &= ~dst_mask;
591     reloc &= dst_mask;
592
593     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
594   }
595
596   typedef Powerpc_relocate_functions<size, big_endian> This;
597   typedef Relocate_functions<size, big_endian> This_reloc;
598 public:
599   // R_POWERPC_REL32: (Symbol + Addend - Address)
600   static inline void
601   rel32(unsigned char* view,
602         typename elfcpp::Elf_types<size>::Elf_Addr value,
603         typename elfcpp::Elf_types<size>::Elf_Addr addend,
604         typename elfcpp::Elf_types<size>::Elf_Addr address)
605   { This_reloc::pcrela32(view, value, addend, address); }
606
607   // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
608   static inline void
609   rel24(unsigned char* view,
610         typename elfcpp::Elf_types<size>::Elf_Addr value,
611         typename elfcpp::Elf_types<size>::Elf_Addr addend,
612         typename elfcpp::Elf_types<size>::Elf_Addr address)
613   {
614     This::template rela<32>(view, 0, 0x03fffffc, value - address, addend);
615   }
616
617   // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
618   static inline void
619   rel14(unsigned char* view,
620         typename elfcpp::Elf_types<size>::Elf_Addr value,
621         typename elfcpp::Elf_types<size>::Elf_Addr addend,
622         typename elfcpp::Elf_types<size>::Elf_Addr address)
623   {
624     This::template rela<32>(view, 0, 0xfffc, value - address, addend);
625   }
626
627   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
628   static inline void
629   addr16(unsigned char* view,
630          typename elfcpp::Elf_types<size>::Elf_Addr value,
631          typename elfcpp::Elf_types<size>::Elf_Addr addend)
632   { This_reloc::rela16(view, value, addend); }
633
634   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
635   static inline void
636   addr16_ds(unsigned char* view,
637             typename elfcpp::Elf_types<size>::Elf_Addr value,
638             typename elfcpp::Elf_types<size>::Elf_Addr addend)
639   {
640     This::template rela<16>(view, 0, 0xfffc, value, addend);
641   }
642
643   // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
644   static inline void
645   addr16_lo(unsigned char* view,
646          typename elfcpp::Elf_types<size>::Elf_Addr value,
647          typename elfcpp::Elf_types<size>::Elf_Addr addend)
648   { This_reloc::rela16(view, value, addend); }
649
650   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
651   static inline void
652   addr16_hi(unsigned char* view,
653             typename elfcpp::Elf_types<size>::Elf_Addr value,
654             typename elfcpp::Elf_types<size>::Elf_Addr addend)
655   {
656     This::template rela<16>(view, 16, 0xffff, value, addend);
657   }
658
659   // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
660   //                      final value of the low 16 bits of the
661   //                      relocation is negative, add one.
662   static inline void
663   addr16_ha(unsigned char* view,
664             typename elfcpp::Elf_types<size>::Elf_Addr value,
665             typename elfcpp::Elf_types<size>::Elf_Addr addend)
666   {
667     This::addr16_hi(view, value + 0x8000, addend);
668   }
669
670   // R_POWERPC_REL16: (Symbol + Addend - Address) & 0xffff
671   static inline void
672   rel16(unsigned char* view,
673         typename elfcpp::Elf_types<size>::Elf_Addr value,
674         typename elfcpp::Elf_types<size>::Elf_Addr addend,
675         typename elfcpp::Elf_types<size>::Elf_Addr address)
676   { This_reloc::pcrela16(view, value, addend, address); }
677
678   // R_POWERPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
679   static inline void
680   rel16_lo(unsigned char* view,
681            typename elfcpp::Elf_types<size>::Elf_Addr value,
682            typename elfcpp::Elf_types<size>::Elf_Addr addend,
683            typename elfcpp::Elf_types<size>::Elf_Addr address)
684   { This_reloc::pcrela16(view, value, addend, address); }
685
686   // R_POWERPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
687   static inline void
688   rel16_hi(unsigned char* view,
689            typename elfcpp::Elf_types<size>::Elf_Addr value,
690            typename elfcpp::Elf_types<size>::Elf_Addr addend,
691            typename elfcpp::Elf_types<size>::Elf_Addr address)
692   {
693     This::template rela<16>(view, 16, 0xffff, value - address, addend);
694   }
695
696   // R_POWERPC_REL16_HA: Same as R_POWERPC_REL16_HI except that if the
697   //                 final value of the low 16 bits of the
698   //                 relocation is negative, add one.
699   static inline void
700   rel16_ha(unsigned char* view,
701            typename elfcpp::Elf_types<size>::Elf_Addr value,
702            typename elfcpp::Elf_types<size>::Elf_Addr addend,
703            typename elfcpp::Elf_types<size>::Elf_Addr address)
704   {
705     This::rel16_hi(view, value + 0x8000, addend, address);
706   }
707 };
708
709 // Stash away the index of .got2 in a relocatable object, if such
710 // a section exists.
711
712 template<int size, bool big_endian>
713 bool
714 Powerpc_relobj<size, big_endian>::do_find_special_sections(
715     Read_symbols_data* sd)
716 {
717   if (size == 32)
718     {
719       const unsigned char* const pshdrs = sd->section_headers->data();
720       const unsigned char* namesu = sd->section_names->data();
721       const char* names = reinterpret_cast<const char*>(namesu);
722       section_size_type names_size = sd->section_names_size;
723       const unsigned char* s;
724
725       s = this->find_shdr(pshdrs, ".got2", names, names_size, NULL);
726       if (s != NULL)
727         {
728           unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
729           this->got2_section_ = ndx;
730         }
731     }
732   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
733 }
734
735 // Set up PowerPC target specific relobj.
736
737 template<int size, bool big_endian>
738 Object*
739 Target_powerpc<size, big_endian>::do_make_elf_object(
740     const std::string& name,
741     Input_file* input_file,
742     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
743 {
744   int et = ehdr.get_e_type();
745   if (et == elfcpp::ET_REL)
746     {
747       Powerpc_relobj<size, big_endian>* obj =
748         new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
749       obj->setup();
750       return obj;
751     }
752   else if (et == elfcpp::ET_DYN)
753     {
754       Sized_dynobj<size, big_endian>* obj =
755         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
756       obj->setup();
757       return obj;
758     }
759   else
760     {
761       gold_error(_("%s: unsupported ELF file type %d"),
762                  name.c_str(), et);
763       return NULL;
764     }
765 }
766
767 template<int size, bool big_endian>
768 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
769 {
770 public:
771   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
772   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
773
774   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
775     : Output_data_got<size, big_endian>(),
776       symtab_(symtab), layout_(layout),
777       header_ent_cnt_(size == 32 ? 3 : 1),
778       header_index_(size == 32 ? 0x2000 : 0)
779   {}
780
781   class Got_entry;
782
783   // Create a new GOT entry and return its offset.
784   unsigned int
785   add_got_entry(Got_entry got_entry)
786   {
787     this->reserve_ent();
788     return Output_data_got<size, big_endian>::add_got_entry(got_entry);
789   }
790
791   // Create a pair of new GOT entries and return the offset of the first.
792   unsigned int
793   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
794   {
795     this->reserve_ent(2);
796     return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
797                                                                  got_entry_2);
798   }
799
800   // Value of _GLOBAL_OFFSET_TABLE_
801   unsigned int
802   g_o_t() const
803   {
804     return this->got_offset(this->header_index_);
805   }
806
807   // Ensure our GOT has a header.
808   void
809   set_final_data_size()
810   {
811     if (this->header_ent_cnt_ != 0)
812       this->make_header();
813     Output_data_got<size, big_endian>::set_final_data_size();
814   }
815
816   // First word of GOT header needs some values that are not
817   // handled by Output_data_got so poke them in here.
818   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
819   void
820   do_write(Output_file* of)
821   {
822     this->replace_constant(this->header_index_,
823                            (size == 32
824                             ? this->layout_->dynamic_section()->address()
825                             : this->address() + 0x8000));
826
827     Output_data_got<size, big_endian>::do_write(of);
828   }
829
830 private:
831   void
832   reserve_ent(unsigned int cnt = 1)
833   {
834     if (this->header_ent_cnt_ == 0)
835       return;
836     if (this->num_entries() + cnt > this->header_index_)
837       this->make_header();
838   }
839
840   void
841   make_header()
842   {
843     this->header_ent_cnt_ = 0;
844     this->header_index_ = this->num_entries();
845     if (size == 32)
846       {
847         Output_data_got<size, big_endian>::add_constant(0);
848         Output_data_got<size, big_endian>::add_constant(0);
849         Output_data_got<size, big_endian>::add_constant(0);
850
851         // Define _GLOBAL_OFFSET_TABLE_ at the header
852         this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
853                                              Symbol_table::PREDEFINED,
854                                              this, this->g_o_t(), 0,
855                                              elfcpp::STT_OBJECT,
856                                              elfcpp::STB_LOCAL,
857                                              elfcpp::STV_HIDDEN,
858                                              0, false, false);
859       }
860     else
861       Output_data_got<size, big_endian>::add_constant(0);
862   }
863
864   // Stashed pointers.
865   Symbol_table* symtab_;
866   Layout* layout_;
867
868   // GOT header size.
869   unsigned int header_ent_cnt_;
870   // GOT header index.
871   unsigned int header_index_;
872 };
873
874 // Get the GOT section, creating it if necessary.
875
876 template<int size, bool big_endian>
877 Output_data_got_powerpc<size, big_endian>*
878 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
879                                               Layout* layout)
880 {
881   if (this->got_ == NULL)
882     {
883       gold_assert(symtab != NULL && layout != NULL);
884
885       this->got_
886         = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
887
888       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
889                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
890                                       this->got_, ORDER_DATA, false);
891     }
892
893   return this->got_;
894 }
895
896 // Get the dynamic reloc section, creating it if necessary.
897
898 template<int size, bool big_endian>
899 typename Target_powerpc<size, big_endian>::Reloc_section*
900 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
901 {
902   if (this->rela_dyn_ == NULL)
903     {
904       gold_assert(layout != NULL);
905       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
906       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
907                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
908                                       ORDER_DYNAMIC_RELOCS, false);
909     }
910   return this->rela_dyn_;
911 }
912
913 // A class to handle the PLT data.
914
915 template<int size, bool big_endian>
916 class Output_data_plt_powerpc : public Output_section_data_build
917 {
918  public:
919   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
920                             size, big_endian> Reloc_section;
921
922   Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
923
924   // Add an entry to the PLT.
925   void
926   add_entry(Symbol*);
927
928   // Return the .rela.plt section data.
929   const Reloc_section*
930   rel_plt() const
931   {
932     return this->rel_;
933   }
934
935   // Return the number of PLT entries.
936   unsigned int
937   entry_count() const
938   {
939     return ((this->current_data_size() - initial_plt_entry_size)
940             / plt_entry_size);
941   }
942
943   // Return the offset of the first non-reserved PLT entry.
944   static unsigned int
945   first_plt_entry_offset()
946   { return initial_plt_entry_size; }
947
948   // Return the size of a PLT entry.
949   static unsigned int
950   get_plt_entry_size()
951   { return plt_entry_size; }
952
953  protected:
954   void
955   do_adjust_output_section(Output_section* os)
956   {
957     os->set_entsize(0);
958   }
959
960   // Write to a map file.
961   void
962   do_print_to_mapfile(Mapfile* mapfile) const
963   { mapfile->print_output_data(this, _("** PLT")); }
964
965  private:
966   // The size of an entry in the PLT.
967   static const int plt_entry_size = size == 32 ? 4 : 24;
968   // The size of the first reserved entry.
969   static const int initial_plt_entry_size = size == 32 ? 0 : 24;
970
971   // Write out the PLT data.
972   void
973   do_write(Output_file*);
974
975   // The reloc section.
976   Reloc_section* rel_;
977   // Allows access to .glink for do_write.
978   Target_powerpc<size, big_endian>* targ_;
979 };
980
981 // Create the PLT section.
982
983 template<int size, bool big_endian>
984 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
985     Layout* layout,
986     Target_powerpc<size, big_endian>* targ)
987   : Output_section_data_build(size == 32 ? 4 : 8),
988     targ_(targ)
989 {
990   this->rel_ = new Reloc_section(false);
991   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
992                                   elfcpp::SHF_ALLOC, this->rel_,
993                                   ORDER_DYNAMIC_PLT_RELOCS, false);
994 }
995
996 // Add an entry to the PLT.
997
998 template<int size, bool big_endian>
999 void
1000 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1001 {
1002   if (!gsym->has_plt_offset())
1003     {
1004       off_t off = this->current_data_size();
1005
1006       if (off == 0)
1007         off += initial_plt_entry_size;
1008       gsym->set_plt_offset(off);
1009       gsym->set_needs_dynsym_entry();
1010       this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1011       off += plt_entry_size;
1012       this->set_current_data_size(off);
1013     }
1014 }
1015
1016 static const uint32_t addis_11_11       = 0x3d6b0000;
1017 static const uint32_t addis_11_30       = 0x3d7e0000;
1018 static const uint32_t addis_12_12       = 0x3d8c0000;
1019 static const uint32_t addi_11_11        = 0x396b0000;
1020 static const uint32_t add_0_11_11       = 0x7c0b5a14;
1021 static const uint32_t add_11_0_11       = 0x7d605a14;
1022 static const uint32_t b                 = 0x48000000;
1023 static const uint32_t bcl_20_31         = 0x429f0005;
1024 static const uint32_t bctr              = 0x4e800420;
1025 static const uint32_t blrl              = 0x4e800021;
1026 static const uint32_t lis_11            = 0x3d600000;
1027 static const uint32_t lis_12            = 0x3d800000;
1028 static const uint32_t lwzu_0_12         = 0x840c0000;
1029 static const uint32_t lwz_0_12          = 0x800c0000;
1030 static const uint32_t lwz_11_11         = 0x816b0000;
1031 static const uint32_t lwz_11_30         = 0x817e0000;
1032 static const uint32_t lwz_12_12         = 0x818c0000;
1033 static const uint32_t mflr_0            = 0x7c0802a6;
1034 static const uint32_t mflr_12           = 0x7d8802a6;
1035 static const uint32_t mtctr_0           = 0x7c0903a6;
1036 static const uint32_t mtctr_11          = 0x7d6903a6;
1037 static const uint32_t mtlr_0            = 0x7c0803a6;
1038 static const uint32_t nop               = 0x60000000;
1039 static const uint32_t sub_11_11_12      = 0x7d6c5850;
1040 static const uint32_t addis_12_2        = 0x3d820000;
1041 static const uint32_t std_2_1           = 0xf8410000;
1042 static const uint32_t ld_11_12          = 0xe96c0000;
1043 static const uint32_t ld_2_12           = 0xe84c0000;
1044 static const uint32_t addi_12_12        = 0x398c0000;
1045 static const uint32_t ld_11_2           = 0xe9620000;
1046 static const uint32_t addi_2_2          = 0x38420000;
1047 static const uint32_t ld_2_2            = 0xe8420000;
1048 static const uint32_t mflr_11           = 0x7d6802a6;
1049 static const uint32_t ld_2_11           = 0xe84b0000;
1050 static const uint32_t mtlr_12           = 0x7d8803a6;
1051 static const uint32_t add_12_2_11       = 0x7d825a14;
1052 static const uint32_t li_0_0            = 0x38000000;
1053 static const uint32_t lis_0_0           = 0x3c000000;
1054 static const uint32_t ori_0_0_0         = 0x60000000;
1055
1056 // Write out the PLT.
1057
1058 template<int size, bool big_endian>
1059 void
1060 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1061 {
1062   if (size == 32)
1063     {
1064       const off_t offset = this->offset();
1065       const section_size_type oview_size
1066         = convert_to_section_size_type(this->data_size());
1067       unsigned char* const oview = of->get_output_view(offset, oview_size);
1068       unsigned char* pov = oview;
1069       unsigned char* endpov = oview + oview_size;
1070
1071       // The address the .glink branch table
1072       const Output_data_glink<size, big_endian>* glink
1073         = this->targ_->glink_section();
1074       elfcpp::Elf_types<32>::Elf_Addr branch_tab
1075         = glink->address() + glink->pltresolve();
1076
1077       while (pov < endpov)
1078         {
1079           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1080           pov += 4;
1081           branch_tab += 4;
1082         }
1083
1084       of->write_output_view(offset, oview_size, oview);
1085     }
1086 }
1087
1088 // Create the PLT section.
1089
1090 template<int size, bool big_endian>
1091 void
1092 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1093 {
1094   if (this->plt_ == NULL)
1095     {
1096       if (this->glink_ == NULL)
1097         make_glink_section(layout);
1098
1099       // Ensure that .rela.dyn always appears before .rela.plt  This is
1100       // necessary due to how, on PowerPC and some other targets, .rela.dyn
1101       // needs to include .rela.plt in it's range.
1102       this->rela_dyn_section(layout);
1103
1104       this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1105       layout->add_output_section_data(".plt",
1106                                       (size == 32
1107                                        ? elfcpp::SHT_PROGBITS
1108                                        : elfcpp::SHT_NOBITS),
1109                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1110                                       this->plt_,
1111                                       (size == 32
1112                                        ? ORDER_SMALL_DATA
1113                                        : ORDER_SMALL_BSS),
1114                                       false);
1115     }
1116 }
1117
1118 // A class to handle .glink.
1119
1120 template<int size, bool big_endian>
1121 class Output_data_glink : public Output_section_data
1122 {
1123  public:
1124   Output_data_glink(Target_powerpc<size, big_endian>*);
1125
1126   // Add an entry
1127   void
1128   add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1129             const Sized_relobj<size, big_endian>*);
1130
1131   unsigned int
1132   find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1133              const Sized_relobj<size, big_endian>*) const;
1134
1135   unsigned int
1136   glink_entry_size() const
1137   {
1138     if (size == 32)
1139       return 4 * 4;
1140     else
1141       // FIXME: We should be using multiple glink sections for
1142       // stubs to support > 33M applications.
1143       return 8 * 4;
1144   }
1145
1146   off_t
1147   pltresolve() const
1148   {
1149     return this->pltresolve_;
1150   }
1151
1152  protected:
1153   // Write to a map file.
1154   void
1155   do_print_to_mapfile(Mapfile* mapfile) const
1156   { mapfile->print_output_data(this, _("** glink")); }
1157
1158  private:
1159   static const int pltresolve_size = 16*4;
1160
1161   void
1162   set_final_data_size();
1163
1164   // Write out .glink
1165   void
1166   do_write(Output_file*);
1167
1168   class Glink_sym_ent
1169   {
1170   public:
1171     Glink_sym_ent(const Symbol* sym,
1172                   const elfcpp::Rela<size, big_endian>& reloc,
1173                   const Sized_relobj<size, big_endian>* object)
1174       : sym_(sym), object_(0), addend_(0)
1175     {
1176       if (size != 32)
1177         this->addend_ = reloc.get_r_addend();
1178       else if (parameters->options().output_is_position_independent()
1179                && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1180                    == elfcpp::R_PPC_PLTREL24))
1181         {
1182           this->addend_ = reloc.get_r_addend();
1183           if (this->addend_ != 0)
1184             this->object_ = object;
1185         }
1186     }
1187
1188     const Symbol* sym_;
1189     const Sized_relobj<size, big_endian>* object_;
1190     unsigned int addend_;
1191
1192     bool operator==(const Glink_sym_ent& that) const
1193     {
1194       return (this->sym_ == that.sym_
1195               && this->object_ == that.object_
1196               && this->addend_ == that.addend_);
1197     }
1198   };
1199
1200   class Glink_sym_ent_hash
1201   {
1202   public:
1203     size_t operator()(const Glink_sym_ent& ent) const
1204     {
1205       return (reinterpret_cast<uintptr_t>(ent.sym_)
1206               ^ reinterpret_cast<uintptr_t>(ent.object_)
1207               ^ ent.addend_);
1208     }
1209   };
1210
1211   // Map sym/object/addend to index.
1212   typedef Unordered_map<Glink_sym_ent, unsigned int,
1213                         Glink_sym_ent_hash> Glink_entries;
1214   Glink_entries glink_entries_;
1215
1216   // Offset of pltresolve stub (actually, branch table for 32-bit)
1217   off_t pltresolve_;
1218
1219   // Allows access to .got and .plt for do_write.
1220   Target_powerpc<size, big_endian>* targ_;
1221 };
1222
1223 // Create the glink section.
1224
1225 template<int size, bool big_endian>
1226 Output_data_glink<size, big_endian>::Output_data_glink(
1227     Target_powerpc<size, big_endian>* targ)
1228   : Output_section_data(16),
1229     pltresolve_(0), targ_(targ)
1230 {
1231 }
1232
1233 // Add an entry to glink, if we do not already have one for this
1234 // sym/object/addend combo.
1235
1236 template<int size, bool big_endian>
1237 void
1238 Output_data_glink<size, big_endian>::add_entry(
1239     const Symbol* gsym,
1240     const elfcpp::Rela<size, big_endian>& reloc,
1241     const Sized_relobj<size, big_endian>* object)
1242 {
1243   Glink_sym_ent ent(gsym, reloc, object);
1244   unsigned int indx = this->glink_entries_.size();
1245   this->glink_entries_.insert(std::make_pair(ent, indx));
1246 }
1247
1248 template<int size, bool big_endian>
1249 unsigned int
1250 Output_data_glink<size, big_endian>::find_entry(
1251     const Symbol* gsym,
1252     const elfcpp::Rela<size, big_endian>& reloc,
1253     const Sized_relobj<size, big_endian>* object) const
1254 {
1255   Glink_sym_ent ent(gsym, reloc, object);
1256   typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1257   gold_assert(p != this->glink_entries_.end());
1258   return p->second;
1259 }
1260
1261 template<int size, bool big_endian>
1262 void
1263 Output_data_glink<size, big_endian>::set_final_data_size()
1264 {
1265   unsigned int count = this->glink_entries_.size();
1266   off_t total = count;
1267
1268   if (count != 0)
1269     {
1270       if (size == 32)
1271         {
1272           total *= 16;
1273           this->pltresolve_ = total;
1274
1275           // space for branch table
1276           total += 4 * (count - 1);
1277
1278           total += -total & 15;
1279           total += this->pltresolve_size;
1280         }
1281       else
1282         {
1283           total *= 32;
1284           this->pltresolve_ = total;
1285           total += this->pltresolve_size;
1286
1287           // space for branch table
1288           total += 8 * count;
1289           if (count > 0x8000)
1290             total += 4 * (count - 0x8000);
1291         }
1292     }
1293
1294   this->set_data_size(total);
1295 }
1296
1297 static inline uint32_t
1298 l(uint32_t a)
1299 {
1300   return a & 0xffff;
1301 }
1302
1303 static inline uint32_t
1304 hi(uint32_t a)
1305 {
1306   return l(a >> 16);
1307 }
1308
1309 static inline uint32_t
1310 ha(uint32_t a)
1311 {
1312   return hi(a + 0x8000);
1313 }
1314
1315 template<bool big_endian>
1316 static inline void
1317 write_insn(unsigned char *p, uint32_t v)
1318 {
1319   elfcpp::Swap<32, big_endian>::writeval(p, v);
1320 }
1321
1322 // Write out .glink.
1323
1324 template<int size, bool big_endian>
1325 void
1326 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1327 {
1328   const off_t off = this->offset();
1329   const section_size_type oview_size =
1330     convert_to_section_size_type(this->data_size());
1331   unsigned char* const oview = of->get_output_view(off, oview_size);
1332   unsigned char *p;
1333
1334   // The base address of the .plt section.
1335   uint32_t plt_base = this->targ_->plt_section()->address();
1336
1337   // The address of _GLOBAL_OFFSET_TABLE_.
1338   const Output_data_got_powerpc<size, big_endian> *got;
1339   typename elfcpp::Elf_types<size>::Elf_Addr g_o_t;
1340   got = this->targ_->got_section();
1341   g_o_t = got->address() + got->g_o_t();
1342
1343   if (size == 64)
1344     {
1345       // Write out call stubs.
1346       typename Glink_entries::const_iterator g;
1347       for (g = this->glink_entries_.begin();
1348            g != this->glink_entries_.end();
1349            ++g)
1350         {
1351           uint64_t plt_addr = plt_base + g->first.sym_->plt_offset();
1352           uint64_t got_addr = g_o_t;
1353           uint64_t pltoff = plt_addr - got_addr;
1354
1355           if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1356             gold_error(_("%s: linkage table error against `%s'"),
1357                        g->first.object_->name().c_str(),
1358                        g->first.sym_->demangled_name().c_str());
1359
1360           p = oview + g->second * this->glink_entry_size();
1361           if (ha(pltoff) != 0)
1362             {
1363               write_insn<big_endian>(p, addis_12_2 + ha(pltoff)),       p += 4;
1364               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1365               write_insn<big_endian>(p, ld_11_12 + l(pltoff)),          p += 4;
1366               if (ha(pltoff + 16) != ha(pltoff))
1367                 {
1368                   write_insn<big_endian>(p, addi_12_12 + l(pltoff)),    p += 4;
1369                   pltoff = 0;
1370                 }
1371               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1372               write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)),       p += 4;
1373               write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)),     p += 4;
1374               write_insn<big_endian>(p, bctr),                          p += 4;
1375             }
1376           else
1377             {
1378               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1379               write_insn<big_endian>(p, ld_11_2 + l(pltoff)),           p += 4;
1380               if (ha(pltoff + 16) != ha(pltoff))
1381                 {
1382                   write_insn<big_endian>(p, addi_2_2 + l(pltoff)),      p += 4;
1383                   pltoff = 0;
1384                 }
1385               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1386               write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)),      p += 4;
1387               write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)),        p += 4;
1388               write_insn<big_endian>(p, bctr),                          p += 4;
1389             }
1390         }
1391
1392       // Write pltresolve stub.
1393       p = oview + this->pltresolve_;
1394       uint64_t after_bcl = this->address() + this->pltresolve_ + 16;
1395       uint64_t pltoff = plt_base - after_bcl;
1396
1397       elfcpp::Swap<64, big_endian>::writeval(p, pltoff),        p += 8;
1398
1399       write_insn<big_endian>(p, mflr_12),                       p += 4;
1400       write_insn<big_endian>(p, bcl_20_31),                     p += 4;
1401       write_insn<big_endian>(p, mflr_11),                       p += 4;
1402       write_insn<big_endian>(p, ld_2_11 + l(-16)),              p += 4;
1403       write_insn<big_endian>(p, mtlr_12),                       p += 4;
1404       write_insn<big_endian>(p, add_12_2_11),                   p += 4;
1405       write_insn<big_endian>(p, ld_11_12 + 0),                  p += 4;
1406       write_insn<big_endian>(p, ld_2_12 + 8),                   p += 4;
1407       write_insn<big_endian>(p, mtctr_11),                      p += 4;
1408       write_insn<big_endian>(p, ld_11_12 + 16),                 p += 4;
1409       write_insn<big_endian>(p, bctr),                          p += 4;
1410       while (p < oview + this->pltresolve_ + this->pltresolve_size)
1411         write_insn<big_endian>(p, nop), p += 4;
1412
1413       // Write lazy link call stubs.
1414       uint32_t indx = 0;
1415       while (p < oview + oview_size)
1416         {
1417           if (indx < 0x8000)
1418             {
1419               write_insn<big_endian>(p, li_0_0 + indx),                 p += 4;
1420             }
1421           else
1422             {
1423               write_insn<big_endian>(p, lis_0_0 + hi(indx)),            p += 4;
1424               write_insn<big_endian>(p, ori_0_0_0 + l(indx)),           p += 4;
1425             }
1426           uint16_t branch_off = this->pltresolve_ + 8 - (p - oview);
1427           write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),      p += 4;
1428           indx++;
1429         }
1430     }
1431   else
1432     {
1433       // Write out call stubs.
1434       typename Glink_entries::const_iterator g;
1435       for (g = this->glink_entries_.begin();
1436            g != this->glink_entries_.end();
1437            ++g)
1438         {
1439           uint32_t plt_addr = plt_base + g->first.sym_->plt_offset();
1440           uint32_t got_addr;
1441
1442           p = oview + g->second * this->glink_entry_size();
1443           if (parameters->options().output_is_position_independent())
1444             {
1445               const Powerpc_relobj<size, big_endian>* object = static_cast
1446                 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1447               if (object != NULL)
1448                 {
1449                   unsigned int got2 = object->got2_shndx();
1450                   got_addr = (g->first.object_->output_section(got2)->address()
1451                               + g->first.object_->output_section_offset(got2)
1452                               + g->first.addend_);
1453                 }
1454               else
1455                 got_addr = g_o_t;
1456
1457               uint32_t pltoff = plt_addr - got_addr;
1458               if (ha(pltoff) == 0)
1459                 {
1460                   write_insn<big_endian>(p +  0, lwz_11_30 + l(pltoff));
1461                   write_insn<big_endian>(p +  4, mtctr_11);
1462                   write_insn<big_endian>(p +  8, bctr);
1463                 }
1464               else
1465                 {
1466                   write_insn<big_endian>(p +  0, addis_11_30 + ha(pltoff));
1467                   write_insn<big_endian>(p +  4, lwz_11_11 + l(pltoff));
1468                   write_insn<big_endian>(p +  8, mtctr_11);
1469                   write_insn<big_endian>(p + 12, bctr);
1470                 }
1471             }
1472           else
1473             {
1474               write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
1475               write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
1476               write_insn<big_endian>(p +  8, mtctr_11);
1477               write_insn<big_endian>(p + 12, bctr);
1478             }
1479         }
1480
1481       // Write out pltresolve branch table.
1482       p = oview + this->pltresolve_;
1483       unsigned int the_end = oview_size - this->pltresolve_size;
1484       unsigned char *end_p = oview + the_end;
1485       while (p < end_p - 8 * 4)
1486         write_insn<big_endian>(p, b + end_p - p), p += 4;
1487       while (p < end_p)
1488         write_insn<big_endian>(p, nop), p += 4;
1489
1490       // Write out pltresolve call stub.
1491       if (parameters->options().output_is_position_independent())
1492         {
1493           uint32_t res0_off = this->pltresolve_;
1494           uint32_t after_bcl_off = the_end + 12;
1495           uint32_t bcl_res0 = after_bcl_off - res0_off;
1496
1497           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
1498           write_insn<big_endian>(p +  4, mflr_0);
1499           write_insn<big_endian>(p +  8, bcl_20_31);
1500           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1501           write_insn<big_endian>(p + 16, mflr_12);
1502           write_insn<big_endian>(p + 20, mtlr_0);
1503           write_insn<big_endian>(p + 24, sub_11_11_12);
1504
1505           uint32_t got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1506
1507           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1508           if (ha(got_bcl) == ha(got_bcl + 4))
1509             {
1510               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1511               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1512             }
1513           else
1514             {
1515               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1516               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1517             }
1518           write_insn<big_endian>(p + 40, mtctr_0);
1519           write_insn<big_endian>(p + 44, add_0_11_11);
1520           write_insn<big_endian>(p + 48, add_11_0_11);
1521           write_insn<big_endian>(p + 52, bctr);
1522           write_insn<big_endian>(p + 56, nop);
1523           write_insn<big_endian>(p + 60, nop);
1524         }
1525       else
1526         {
1527           uint32_t res0 = this->pltresolve_ + this->address();
1528
1529           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1530           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1531           if (ha(g_o_t + 4) == ha(g_o_t + 8))
1532             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1533           else
1534             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1535           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1536           write_insn<big_endian>(p + 16, mtctr_0);
1537           write_insn<big_endian>(p + 20, add_0_11_11);
1538           if (ha(g_o_t + 4) == ha(g_o_t + 8))
1539             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1540           else
1541             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1542           write_insn<big_endian>(p + 28, add_11_0_11);
1543           write_insn<big_endian>(p + 32, bctr);
1544           write_insn<big_endian>(p + 36, nop);
1545           write_insn<big_endian>(p + 40, nop);
1546           write_insn<big_endian>(p + 44, nop);
1547           write_insn<big_endian>(p + 48, nop);
1548           write_insn<big_endian>(p + 52, nop);
1549           write_insn<big_endian>(p + 56, nop);
1550           write_insn<big_endian>(p + 60, nop);
1551         }
1552       p += 64;
1553     }
1554
1555   of->write_output_view(off, oview_size, oview);
1556 }
1557
1558 // Create the glink section.
1559
1560 template<int size, bool big_endian>
1561 void
1562 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
1563 {
1564   if (this->glink_ == NULL)
1565     {
1566       this->glink_ = new Output_data_glink<size, big_endian>(this);
1567       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
1568                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1569                                       this->glink_, ORDER_TEXT, false);
1570     }
1571 }
1572
1573 // Create a PLT entry for a global symbol.
1574
1575 template<int size, bool big_endian>
1576 void
1577 Target_powerpc<size, big_endian>::make_plt_entry(
1578     Layout* layout,
1579     Symbol* gsym,
1580     const elfcpp::Rela<size, big_endian>& reloc,
1581     const Sized_relobj<size, big_endian>* object)
1582 {
1583   if (this->plt_ == NULL)
1584     this->make_plt_section(layout);
1585
1586   this->plt_->add_entry(gsym);
1587
1588   this->glink_->add_entry(gsym, reloc, object);
1589 }
1590
1591 // Return the number of entries in the PLT.
1592
1593 template<int size, bool big_endian>
1594 unsigned int
1595 Target_powerpc<size, big_endian>::plt_entry_count() const
1596 {
1597   if (this->plt_ == NULL)
1598     return 0;
1599   return this->plt_->entry_count();
1600 }
1601
1602 // Return the offset of the first non-reserved PLT entry.
1603
1604 template<int size, bool big_endian>
1605 unsigned int
1606 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1607 {
1608   return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1609 }
1610
1611 // Return the size of each PLT entry.
1612
1613 template<int size, bool big_endian>
1614 unsigned int
1615 Target_powerpc<size, big_endian>::plt_entry_size() const
1616 {
1617   return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1618 }
1619
1620 // Create a GOT entry for the TLS module index.
1621
1622 template<int size, bool big_endian>
1623 unsigned int
1624 Target_powerpc<size, big_endian>::got_mod_index_entry(
1625     Symbol_table* symtab,
1626     Layout* layout,
1627     Sized_relobj_file<size, big_endian>* object)
1628 {
1629   if (this->got_mod_index_offset_ == -1U)
1630     {
1631       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1632       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1633       Output_data_got_powerpc<size, big_endian>* got;
1634       unsigned int got_offset;
1635
1636       got = this->got_section(symtab, layout);
1637       got->reserve_ent(2);
1638       got_offset = got->add_constant(0);
1639       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1640                           got_offset, 0);
1641       got->add_constant(0);
1642       this->got_mod_index_offset_ = got_offset;
1643     }
1644   return this->got_mod_index_offset_;
1645 }
1646
1647 // Optimize the TLS relocation type based on what we know about the
1648 // symbol.  IS_FINAL is true if the final address of this symbol is
1649 // known at link time.
1650
1651 template<int size, bool big_endian>
1652 tls::Tls_optimization
1653 Target_powerpc<size, big_endian>::optimize_tls_reloc(bool, int)
1654 {
1655   // If we are generating a shared library, then we can't do anything
1656   // in the linker.
1657   if (parameters->options().shared())
1658     return tls::TLSOPT_NONE;
1659   // FIXME
1660   return tls::TLSOPT_NONE;
1661 }
1662
1663 // Get the Reference_flags for a particular relocation.
1664
1665 template<int size, bool big_endian>
1666 int
1667 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
1668 {
1669   switch (r_type)
1670     {
1671     case elfcpp::R_POWERPC_NONE:
1672     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1673     case elfcpp::R_POWERPC_GNU_VTENTRY:
1674     case elfcpp::R_PPC64_TOC:
1675       // No symbol reference.
1676       return 0;
1677
1678     case elfcpp::R_POWERPC_ADDR16:
1679     case elfcpp::R_POWERPC_ADDR16_LO:
1680     case elfcpp::R_POWERPC_ADDR16_HI:
1681     case elfcpp::R_POWERPC_ADDR16_HA:
1682     case elfcpp::R_POWERPC_ADDR32:
1683     case elfcpp::R_PPC64_ADDR64:
1684       return Symbol::ABSOLUTE_REF;
1685
1686     case elfcpp::R_POWERPC_REL24:
1687     case elfcpp::R_PPC_LOCAL24PC:
1688     case elfcpp::R_POWERPC_REL16:
1689     case elfcpp::R_POWERPC_REL16_LO:
1690     case elfcpp::R_POWERPC_REL16_HI:
1691     case elfcpp::R_POWERPC_REL16_HA:
1692       return Symbol::RELATIVE_REF;
1693
1694     case elfcpp::R_PPC_PLTREL24:
1695       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1696
1697     case elfcpp::R_POWERPC_GOT16:
1698     case elfcpp::R_POWERPC_GOT16_LO:
1699     case elfcpp::R_POWERPC_GOT16_HI:
1700     case elfcpp::R_POWERPC_GOT16_HA:
1701     case elfcpp::R_PPC64_TOC16:
1702     case elfcpp::R_PPC64_TOC16_LO:
1703     case elfcpp::R_PPC64_TOC16_HI:
1704     case elfcpp::R_PPC64_TOC16_HA:
1705     case elfcpp::R_PPC64_TOC16_DS:
1706     case elfcpp::R_PPC64_TOC16_LO_DS:
1707       // Absolute in GOT.
1708       return Symbol::ABSOLUTE_REF;
1709
1710     case elfcpp::R_POWERPC_GOT_TPREL16:
1711     case elfcpp::R_POWERPC_TLS:
1712       return Symbol::TLS_REF;
1713
1714     case elfcpp::R_POWERPC_COPY:
1715     case elfcpp::R_POWERPC_GLOB_DAT:
1716     case elfcpp::R_POWERPC_JMP_SLOT:
1717     case elfcpp::R_POWERPC_RELATIVE:
1718     case elfcpp::R_POWERPC_DTPMOD:
1719     default:
1720       // Not expected.  We will give an error later.
1721       return 0;
1722     }
1723 }
1724
1725 // Report an unsupported relocation against a local symbol.
1726
1727 template<int size, bool big_endian>
1728 void
1729 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1730     Sized_relobj_file<size, big_endian>* object,
1731     unsigned int r_type)
1732 {
1733   gold_error(_("%s: unsupported reloc %u against local symbol"),
1734              object->name().c_str(), r_type);
1735 }
1736
1737 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1738 // dynamic linker does not support it, issue an error.
1739
1740 template<int size, bool big_endian>
1741 void
1742 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1743                                                       unsigned int r_type)
1744 {
1745   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1746
1747   // These are the relocation types supported by glibc for both 32-bit
1748   // and 64-bit powerpc.
1749   switch (r_type)
1750     {
1751     case elfcpp::R_POWERPC_RELATIVE:
1752     case elfcpp::R_POWERPC_GLOB_DAT:
1753     case elfcpp::R_POWERPC_DTPMOD:
1754     case elfcpp::R_POWERPC_DTPREL:
1755     case elfcpp::R_POWERPC_TPREL:
1756     case elfcpp::R_POWERPC_JMP_SLOT:
1757     case elfcpp::R_POWERPC_COPY:
1758     case elfcpp::R_POWERPC_ADDR32:
1759     case elfcpp::R_POWERPC_ADDR24:
1760     case elfcpp::R_POWERPC_REL24:
1761       return;
1762
1763     default:
1764       break;
1765     }
1766
1767   if (size == 64)
1768     {
1769       switch (r_type)
1770         {
1771           // These are the relocation types supported only on 64-bit.
1772         case elfcpp::R_PPC64_ADDR64:
1773         case elfcpp::R_PPC64_TPREL16_LO_DS:
1774         case elfcpp::R_PPC64_TPREL16_DS:
1775         case elfcpp::R_POWERPC_TPREL16:
1776         case elfcpp::R_POWERPC_TPREL16_LO:
1777         case elfcpp::R_POWERPC_TPREL16_HI:
1778         case elfcpp::R_POWERPC_TPREL16_HA:
1779         case elfcpp::R_PPC64_TPREL16_HIGHER:
1780         case elfcpp::R_PPC64_TPREL16_HIGHEST:
1781         case elfcpp::R_PPC64_TPREL16_HIGHERA:
1782         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1783         case elfcpp::R_PPC64_ADDR16_LO_DS:
1784         case elfcpp::R_POWERPC_ADDR16_LO:
1785         case elfcpp::R_POWERPC_ADDR16_HI:
1786         case elfcpp::R_POWERPC_ADDR16_HA:
1787         case elfcpp::R_POWERPC_ADDR30:
1788         case elfcpp::R_PPC64_UADDR64:
1789         case elfcpp::R_POWERPC_UADDR32:
1790         case elfcpp::R_POWERPC_ADDR16:
1791         case elfcpp::R_POWERPC_UADDR16:
1792         case elfcpp::R_PPC64_ADDR16_DS:
1793         case elfcpp::R_PPC64_ADDR16_HIGHER:
1794         case elfcpp::R_PPC64_ADDR16_HIGHEST:
1795         case elfcpp::R_PPC64_ADDR16_HIGHERA:
1796         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1797         case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1798         case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1799         case elfcpp::R_POWERPC_REL32:
1800         case elfcpp::R_PPC64_REL64:
1801           return;
1802
1803         default:
1804           break;
1805         }
1806     }
1807   else
1808     {
1809       switch (r_type)
1810         {
1811           // These are the relocation types supported only on 32-bit.
1812
1813         default:
1814           break;
1815         }
1816     }
1817
1818   // This prevents us from issuing more than one error per reloc
1819   // section.  But we can still wind up issuing more than one
1820   // error per object file.
1821   if (this->issued_non_pic_error_)
1822     return;
1823   gold_assert(parameters->options().output_is_position_independent());
1824   object->error(_("requires unsupported dynamic reloc; "
1825                   "recompile with -fPIC"));
1826   this->issued_non_pic_error_ = true;
1827   return;
1828 }
1829
1830 // Scan a relocation for a local symbol.
1831
1832 template<int size, bool big_endian>
1833 inline void
1834 Target_powerpc<size, big_endian>::Scan::local(
1835     Symbol_table* symtab,
1836     Layout* layout,
1837     Target_powerpc<size, big_endian>* target,
1838     Sized_relobj_file<size, big_endian>* object,
1839     unsigned int data_shndx,
1840     Output_section* output_section,
1841     const elfcpp::Rela<size, big_endian>& reloc,
1842     unsigned int r_type,
1843     const elfcpp::Sym<size, big_endian>& lsym)
1844 {
1845   switch (r_type)
1846     {
1847     case elfcpp::R_POWERPC_NONE:
1848     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1849     case elfcpp::R_POWERPC_GNU_VTENTRY:
1850     case elfcpp::R_PPC64_TOCSAVE:
1851       break;
1852
1853     case elfcpp::R_PPC64_ADDR64:
1854     case elfcpp::R_POWERPC_ADDR32:
1855     case elfcpp::R_POWERPC_ADDR16_HA:
1856     case elfcpp::R_POWERPC_ADDR16_LO:
1857       // If building a shared library (or a position-independent
1858       // executable), we need to create a dynamic relocation for
1859       // this location.
1860       if (parameters->options().output_is_position_independent())
1861         {
1862           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1863
1864           check_non_pic(object, r_type);
1865           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1866             {
1867               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1868               rela_dyn->add_local(object, r_sym, r_type, output_section,
1869                                   data_shndx, reloc.get_r_offset(),
1870                                   reloc.get_r_addend());
1871             }
1872           else
1873             {
1874               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1875               gold_assert(lsym.get_st_value() == 0);
1876               rela_dyn->add_local_relative(object, r_sym, r_type,
1877                                            output_section, data_shndx,
1878                                            reloc.get_r_offset(),
1879                                            reloc.get_r_addend(), false);
1880             }
1881         }
1882       break;
1883
1884     case elfcpp::R_POWERPC_REL24:
1885     case elfcpp::R_PPC_LOCAL24PC:
1886     case elfcpp::R_POWERPC_REL32:
1887     case elfcpp::R_POWERPC_REL16_LO:
1888     case elfcpp::R_POWERPC_REL16_HA:
1889       break;
1890
1891     case elfcpp::R_POWERPC_GOT16:
1892     case elfcpp::R_POWERPC_GOT16_LO:
1893     case elfcpp::R_POWERPC_GOT16_HI:
1894     case elfcpp::R_POWERPC_GOT16_HA:
1895       {
1896         // The symbol requires a GOT entry.
1897         Output_data_got_powerpc<size, big_endian>* got;
1898         unsigned int r_sym;
1899
1900         got = target->got_section(symtab, layout);
1901         r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1902
1903         // If we are generating a shared object, we need to add a
1904         // dynamic relocation for this symbol's GOT entry.
1905         if (parameters->options().output_is_position_independent())
1906           {
1907             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1908               {
1909                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1910                 unsigned int off;
1911
1912                 off = got->add_constant(0);
1913                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1914                 rela_dyn->add_local_relative(object, r_sym,
1915                                              elfcpp::R_POWERPC_RELATIVE,
1916                                              got, off, 0, false);
1917               }
1918           }
1919         else
1920           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1921       }
1922       break;
1923
1924     case elfcpp::R_PPC64_TOC16:
1925     case elfcpp::R_PPC64_TOC16_LO:
1926     case elfcpp::R_PPC64_TOC16_HI:
1927     case elfcpp::R_PPC64_TOC16_HA:
1928     case elfcpp::R_PPC64_TOC16_DS:
1929     case elfcpp::R_PPC64_TOC16_LO_DS:
1930     case elfcpp::R_PPC64_TOC:
1931       // We need a GOT section.
1932       target->got_section(symtab, layout);
1933       break;
1934
1935       // These are relocations which should only be seen by the
1936       // dynamic linker, and should never be seen here.
1937     case elfcpp::R_POWERPC_COPY:
1938     case elfcpp::R_POWERPC_GLOB_DAT:
1939     case elfcpp::R_POWERPC_JMP_SLOT:
1940     case elfcpp::R_POWERPC_RELATIVE:
1941     case elfcpp::R_POWERPC_DTPMOD:
1942       gold_error(_("%s: unexpected reloc %u in object file"),
1943                  object->name().c_str(), r_type);
1944       break;
1945
1946     default:
1947       unsupported_reloc_local(object, r_type);
1948       break;
1949     }
1950 }
1951
1952 // Report an unsupported relocation against a global symbol.
1953
1954 template<int size, bool big_endian>
1955 void
1956 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1957     Sized_relobj_file<size, big_endian>* object,
1958     unsigned int r_type,
1959     Symbol* gsym)
1960 {
1961   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1962              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1963 }
1964
1965 // Scan a relocation for a global symbol.
1966
1967 template<int size, bool big_endian>
1968 inline void
1969 Target_powerpc<size, big_endian>::Scan::global(
1970     Symbol_table* symtab,
1971     Layout* layout,
1972     Target_powerpc<size, big_endian>* target,
1973     Sized_relobj_file<size, big_endian>* object,
1974     unsigned int data_shndx,
1975     Output_section* output_section,
1976     const elfcpp::Rela<size, big_endian>& reloc,
1977     unsigned int r_type,
1978     Symbol* gsym)
1979 {
1980   switch (r_type)
1981     {
1982     case elfcpp::R_POWERPC_NONE:
1983     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1984     case elfcpp::R_POWERPC_GNU_VTENTRY:
1985     case elfcpp::R_PPC_LOCAL24PC:
1986       break;
1987
1988     case elfcpp::R_POWERPC_ADDR16:
1989     case elfcpp::R_POWERPC_ADDR16_LO:
1990     case elfcpp::R_POWERPC_ADDR16_HI:
1991     case elfcpp::R_POWERPC_ADDR16_HA:
1992     case elfcpp::R_POWERPC_ADDR32:
1993     case elfcpp::R_PPC64_ADDR64:
1994       {
1995         // Make a PLT entry if necessary.
1996         if (gsym->needs_plt_entry())
1997           {
1998             target->make_plt_entry(layout, gsym, reloc, 0);
1999             // Since this is not a PC-relative relocation, we may be
2000             // taking the address of a function. In that case we need to
2001             // set the entry in the dynamic symbol table to the address of
2002             // the PLT entry.
2003             if (size == 32
2004                 && gsym->is_from_dynobj() && !parameters->options().shared())
2005               gsym->set_needs_dynsym_value();
2006           }
2007         // Make a dynamic relocation if necessary.
2008         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2009           {
2010             if (gsym->may_need_copy_reloc())
2011               {
2012                 target->copy_reloc(symtab, layout, object,
2013                                    data_shndx, output_section, gsym, reloc);
2014               }
2015             else if ((r_type == elfcpp::R_POWERPC_ADDR32
2016                       || r_type == elfcpp::R_PPC64_ADDR64)
2017                      && gsym->can_use_relative_reloc(false))
2018               {
2019                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2020                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2021                                               output_section, object,
2022                                               data_shndx, reloc.get_r_offset(),
2023                                               reloc.get_r_addend(), false);
2024               }
2025             else
2026               {
2027                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2028
2029                 check_non_pic(object, r_type);
2030                 if (gsym->is_from_dynobj()
2031                     || gsym->is_undefined()
2032                     || gsym->is_preemptible())
2033                   rela_dyn->add_global(gsym, r_type, output_section,
2034                                        object, data_shndx,
2035                                        reloc.get_r_offset(),
2036                                        reloc.get_r_addend());
2037                 else
2038                   rela_dyn->add_global_relative(gsym, r_type,
2039                                                 output_section, object,
2040                                                 data_shndx,
2041                                                 reloc.get_r_offset(),
2042                                                 reloc.get_r_addend(), false);
2043               }
2044           }
2045       }
2046       break;
2047
2048     case elfcpp::R_PPC_PLTREL24:
2049     case elfcpp::R_POWERPC_REL24:
2050       {
2051         if (gsym->needs_plt_entry()
2052             || (!gsym->final_value_is_known()
2053                  && !(gsym->is_defined()
2054                       && !gsym->is_from_dynobj()
2055                       && !gsym->is_preemptible())))
2056           target->make_plt_entry(layout, gsym, reloc, object);
2057         // Make a dynamic relocation if necessary.
2058         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2059           {
2060             if (gsym->may_need_copy_reloc())
2061               {
2062                 target->copy_reloc(symtab, layout, object,
2063                                    data_shndx, output_section, gsym,
2064                                    reloc);
2065               }
2066             else
2067               {
2068                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2069                 check_non_pic(object, r_type);
2070                 rela_dyn->add_global(gsym, r_type, output_section, object,
2071                                      data_shndx, reloc.get_r_offset(),
2072                                      reloc.get_r_addend());
2073               }
2074           }
2075       }
2076       break;
2077
2078     case elfcpp::R_POWERPC_REL16:
2079     case elfcpp::R_POWERPC_REL16_LO:
2080     case elfcpp::R_POWERPC_REL16_HI:
2081     case elfcpp::R_POWERPC_REL16_HA:
2082       break;
2083
2084     case elfcpp::R_POWERPC_GOT16:
2085     case elfcpp::R_POWERPC_GOT16_LO:
2086     case elfcpp::R_POWERPC_GOT16_HI:
2087     case elfcpp::R_POWERPC_GOT16_HA:
2088       {
2089         // The symbol requires a GOT entry.
2090         Output_data_got_powerpc<size, big_endian>* got;
2091
2092         got = target->got_section(symtab, layout);
2093         if (gsym->final_value_is_known())
2094           got->add_global(gsym, GOT_TYPE_STANDARD);
2095         else
2096           {
2097             // If this symbol is not fully resolved, we need to add a
2098             // dynamic relocation for it.
2099             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2100             if (gsym->is_from_dynobj()
2101                 || gsym->is_undefined()
2102                 || gsym->is_preemptible())
2103               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2104                                        elfcpp::R_POWERPC_GLOB_DAT);
2105             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2106               {
2107                 unsigned int off = got->add_constant(0);
2108
2109                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2110                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2111                                               got, off, 0, false);
2112               }
2113           }
2114       }
2115       break;
2116
2117     case elfcpp::R_PPC64_TOC:
2118     case elfcpp::R_PPC64_TOC16:
2119     case elfcpp::R_PPC64_TOC16_LO:
2120     case elfcpp::R_PPC64_TOC16_HI:
2121     case elfcpp::R_PPC64_TOC16_HA:
2122     case elfcpp::R_PPC64_TOC16_DS:
2123     case elfcpp::R_PPC64_TOC16_LO_DS:
2124       // We need a GOT section.
2125       target->got_section(symtab, layout);
2126       break;
2127
2128     case elfcpp::R_POWERPC_GOT_TPREL16:
2129     case elfcpp::R_POWERPC_TLS:
2130       // XXX TLS
2131       break;
2132
2133       // These are relocations which should only be seen by the
2134       // dynamic linker, and should never be seen here.
2135     case elfcpp::R_POWERPC_COPY:
2136     case elfcpp::R_POWERPC_GLOB_DAT:
2137     case elfcpp::R_POWERPC_JMP_SLOT:
2138     case elfcpp::R_POWERPC_RELATIVE:
2139     case elfcpp::R_POWERPC_DTPMOD:
2140       gold_error(_("%s: unexpected reloc %u in object file"),
2141                  object->name().c_str(), r_type);
2142       break;
2143
2144     default:
2145       unsupported_reloc_global(object, r_type, gsym);
2146       break;
2147     }
2148 }
2149
2150 // Process relocations for gc.
2151
2152 template<int size, bool big_endian>
2153 void
2154 Target_powerpc<size, big_endian>::gc_process_relocs(
2155     Symbol_table* symtab,
2156     Layout* layout,
2157     Sized_relobj_file<size, big_endian>* object,
2158     unsigned int data_shndx,
2159     unsigned int,
2160     const unsigned char* prelocs,
2161     size_t reloc_count,
2162     Output_section* output_section,
2163     bool needs_special_offset_handling,
2164     size_t local_symbol_count,
2165     const unsigned char* plocal_symbols)
2166 {
2167   typedef Target_powerpc<size, big_endian> Powerpc;
2168   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2169
2170   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2171                           typename Target_powerpc::Relocatable_size_for_reloc>(
2172     symtab,
2173     layout,
2174     this,
2175     object,
2176     data_shndx,
2177     prelocs,
2178     reloc_count,
2179     output_section,
2180     needs_special_offset_handling,
2181     local_symbol_count,
2182     plocal_symbols);
2183 }
2184
2185 // Scan relocations for a section.
2186
2187 template<int size, bool big_endian>
2188 void
2189 Target_powerpc<size, big_endian>::scan_relocs(
2190     Symbol_table* symtab,
2191     Layout* layout,
2192     Sized_relobj_file<size, big_endian>* object,
2193     unsigned int data_shndx,
2194     unsigned int sh_type,
2195     const unsigned char* prelocs,
2196     size_t reloc_count,
2197     Output_section* output_section,
2198     bool needs_special_offset_handling,
2199     size_t local_symbol_count,
2200     const unsigned char* plocal_symbols)
2201 {
2202   typedef Target_powerpc<size, big_endian> Powerpc;
2203   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2204
2205   if (sh_type == elfcpp::SHT_REL)
2206     {
2207       gold_error(_("%s: unsupported REL reloc section"),
2208                  object->name().c_str());
2209       return;
2210     }
2211
2212   if (size == 32)
2213     {
2214       static Output_data_space* sdata;
2215
2216       // Define _SDA_BASE_ at the start of the .sdata section.
2217       if (sdata == NULL)
2218         {
2219           // layout->find_output_section(".sdata") == NULL
2220           sdata = new Output_data_space(4, "** sdata");
2221           Output_section* os
2222             = layout->add_output_section_data(".sdata", 0,
2223                                               elfcpp::SHF_ALLOC
2224                                               | elfcpp::SHF_WRITE,
2225                                               sdata, ORDER_SMALL_DATA, false);
2226           symtab->define_in_output_data("_SDA_BASE_", NULL,
2227                                         Symbol_table::PREDEFINED,
2228                                         os, 32768, 0, elfcpp::STT_OBJECT,
2229                                         elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2230                                         0, false, false);
2231         }
2232     }
2233
2234   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2235     symtab,
2236     layout,
2237     this,
2238     object,
2239     data_shndx,
2240     prelocs,
2241     reloc_count,
2242     output_section,
2243     needs_special_offset_handling,
2244     local_symbol_count,
2245     plocal_symbols);
2246 }
2247
2248 // Finalize the sections.
2249
2250 template<int size, bool big_endian>
2251 void
2252 Target_powerpc<size, big_endian>::do_finalize_sections(
2253     Layout* layout,
2254     const Input_objects*,
2255     Symbol_table*)
2256 {
2257   // Fill in some more dynamic tags.
2258   const Reloc_section* rel_plt = (this->plt_ == NULL
2259                                   ? NULL
2260                                   : this->plt_->rel_plt());
2261   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2262                                   this->rela_dyn_, true, size == 32);
2263
2264   if (size == 32)
2265     {
2266       this->got_->finalize_data_size();
2267       Output_data_dynamic* odyn = layout->dynamic_data();
2268       odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2269                                     this->got_, this->got_->g_o_t());
2270     }
2271
2272   // Emit any relocs we saved in an attempt to avoid generating COPY
2273   // relocs.
2274   if (this->copy_relocs_.any_saved_relocs())
2275     this->copy_relocs_.emit(this->rela_dyn_section(layout));
2276 }
2277
2278 // Perform a relocation.
2279
2280 template<int size, bool big_endian>
2281 inline bool
2282 Target_powerpc<size, big_endian>::Relocate::relocate(
2283     const Relocate_info<size, big_endian>* relinfo,
2284     Target_powerpc* target,
2285     Output_section* os,
2286     size_t relnum,
2287     const elfcpp::Rela<size, big_endian>& rela,
2288     unsigned int r_type,
2289     const Sized_symbol<size>* gsym,
2290     const Symbol_value<size>* psymval,
2291     unsigned char* view,
2292     typename elfcpp::Elf_types<size>::Elf_Addr address,
2293     section_size_type /* view_size */)
2294 {
2295   const unsigned int toc_base_offset = 0x8000;
2296   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
2297   const Powerpc_relobj<size, big_endian>* const object
2298     = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
2299   elfcpp::Elf_Xword value;
2300
2301   if (r_type == elfcpp::R_POWERPC_GOT16
2302       || r_type == elfcpp::R_POWERPC_GOT16_LO
2303       || r_type == elfcpp::R_POWERPC_GOT16_HI
2304       || r_type == elfcpp::R_POWERPC_GOT16_HA
2305       || r_type == elfcpp::R_PPC64_GOT16_DS
2306       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
2307     {
2308       if (gsym != NULL)
2309         {
2310           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2311           value = gsym->got_offset(GOT_TYPE_STANDARD);
2312         }
2313       else
2314         {
2315           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2316           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2317           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2318         }
2319       value -= target->got_section()->g_o_t();
2320     }
2321   else if (r_type == elfcpp::R_PPC64_TOC)
2322     {
2323       value = target->got_section()->address() + toc_base_offset;
2324     }
2325   else if (gsym != NULL
2326            && (r_type == elfcpp::R_POWERPC_REL24
2327                || r_type == elfcpp::R_PPC_PLTREL24)
2328            && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2329     {
2330       const Output_data_glink<size, big_endian>* glink;
2331
2332       glink = target->glink_section();
2333       unsigned int glink_index = glink->find_entry(gsym, rela, object);
2334       value = glink->address() + glink_index * glink->glink_entry_size();
2335     }
2336   else
2337     {
2338       elfcpp::Elf_Xword addend = 0;
2339       if (r_type != elfcpp::R_PPC_PLTREL24)
2340         addend = rela.get_r_addend();
2341       value = psymval->value(object, addend);
2342     }
2343
2344   switch (r_type)
2345     {
2346     case elfcpp::R_PPC64_TOC16:
2347     case elfcpp::R_PPC64_TOC16_LO:
2348     case elfcpp::R_PPC64_TOC16_HI:
2349     case elfcpp::R_PPC64_TOC16_HA:
2350     case elfcpp::R_PPC64_TOC16_DS:
2351     case elfcpp::R_PPC64_TOC16_LO_DS:
2352       // Subtract the TOC base address.
2353       value -= target->got_section()->address() + toc_base_offset;
2354       break;
2355
2356     case elfcpp::R_POWERPC_SECTOFF:
2357     case elfcpp::R_POWERPC_SECTOFF_LO:
2358     case elfcpp::R_POWERPC_SECTOFF_HI:
2359     case elfcpp::R_POWERPC_SECTOFF_HA:
2360     case elfcpp::R_PPC64_SECTOFF_DS:
2361     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2362       if (os != NULL)
2363         value -= os->address();
2364       break;
2365
2366     default:
2367       break;
2368     }
2369
2370   switch (r_type)
2371     {
2372     case elfcpp::R_POWERPC_NONE:
2373     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2374     case elfcpp::R_POWERPC_GNU_VTENTRY:
2375       break;
2376
2377     case elfcpp::R_POWERPC_REL32:
2378       Reloc::rel32(view, value, 0, address);
2379       break;
2380
2381     case elfcpp::R_POWERPC_REL24:
2382     case elfcpp::R_PPC_PLTREL24:
2383     case elfcpp::R_PPC_LOCAL24PC:
2384       Reloc::rel24(view, value, 0, address);
2385       break;
2386
2387     case elfcpp::R_POWERPC_REL14:
2388       Reloc::rel14(view, value, 0, address);
2389       break;
2390
2391     case elfcpp::R_PPC64_ADDR64:
2392     case elfcpp::R_PPC64_TOC:
2393       Relocate_functions<size, big_endian>::rela64(view, value, 0);
2394       break;
2395
2396     case elfcpp::R_POWERPC_ADDR32:
2397       Relocate_functions<size, big_endian>::rela32(view, value, 0);
2398       break;
2399
2400     case elfcpp::R_POWERPC_ADDR16:
2401     case elfcpp::R_PPC64_TOC16:
2402     case elfcpp::R_POWERPC_GOT16:
2403     case elfcpp::R_POWERPC_SECTOFF:
2404       Reloc::addr16(view, value, 0);
2405       break;
2406
2407     case elfcpp::R_POWERPC_ADDR16_LO:
2408     case elfcpp::R_PPC64_TOC16_LO:
2409     case elfcpp::R_POWERPC_GOT16_LO:
2410     case elfcpp::R_POWERPC_SECTOFF_LO:
2411       Reloc::addr16_lo(view, value, 0);
2412       break;
2413
2414     case elfcpp::R_POWERPC_ADDR16_HI:
2415     case elfcpp::R_PPC64_TOC16_HI:
2416     case elfcpp::R_POWERPC_GOT16_HI:
2417     case elfcpp::R_POWERPC_SECTOFF_HI:
2418       Reloc::addr16_hi(view, value, 0);
2419       break;
2420
2421     case elfcpp::R_POWERPC_ADDR16_HA:
2422     case elfcpp::R_PPC64_TOC16_HA:
2423     case elfcpp::R_POWERPC_GOT16_HA:
2424     case elfcpp::R_POWERPC_SECTOFF_HA:
2425       Reloc::addr16_ha(view, value, 0);
2426       break;
2427
2428     case elfcpp::R_POWERPC_REL16_LO:
2429       Reloc::rel16_lo(view, value, 0, address);
2430       break;
2431
2432     case elfcpp::R_POWERPC_REL16_HI:
2433       Reloc::rel16_hi(view, value, 0, address);
2434       break;
2435
2436     case elfcpp::R_POWERPC_REL16_HA:
2437       Reloc::rel16_ha(view, value, 0, address);
2438       break;
2439
2440     case elfcpp::R_PPC64_ADDR16_DS:
2441     case elfcpp::R_PPC64_ADDR16_LO_DS:
2442     case elfcpp::R_PPC64_TOC16_DS:
2443     case elfcpp::R_PPC64_TOC16_LO_DS:
2444     case elfcpp::R_PPC64_GOT16_DS:
2445     case elfcpp::R_PPC64_GOT16_LO_DS:
2446     case elfcpp::R_PPC64_SECTOFF_DS:
2447     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2448       Reloc::addr16_ds(view, value, 0);
2449       break;
2450
2451     case elfcpp::R_POWERPC_COPY:
2452     case elfcpp::R_POWERPC_GLOB_DAT:
2453     case elfcpp::R_POWERPC_JMP_SLOT:
2454     case elfcpp::R_POWERPC_RELATIVE:
2455       // This is an outstanding tls reloc, which is unexpected when
2456       // linking.
2457     case elfcpp::R_POWERPC_DTPMOD:
2458       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2459                              _("unexpected reloc %u in object file"),
2460                              r_type);
2461       break;
2462
2463     case elfcpp::R_PPC64_TOCSAVE:
2464       // For the time being this can be ignored.
2465       break;
2466
2467     default:
2468       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2469                              _("unsupported reloc %u"),
2470                              r_type);
2471       break;
2472     }
2473
2474   return true;
2475 }
2476
2477 // Perform a TLS relocation.
2478
2479 template<int size, bool big_endian>
2480 inline void
2481 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
2482     const Relocate_info<size, big_endian>* relinfo,
2483     Target_powerpc<size, big_endian>* target,
2484     size_t relnum,
2485     const elfcpp::Rela<size, big_endian>& rela,
2486     unsigned int r_type,
2487     const Sized_symbol<size>* gsym,
2488     const Symbol_value<size>* psymval,
2489     unsigned char* view,
2490     typename elfcpp::Elf_types<size>::Elf_Addr address,
2491     section_size_type)
2492 {
2493   Output_segment* tls_segment = relinfo->layout->tls_segment();
2494   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2495
2496   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2497   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2498
2499   const bool is_final =
2500     (gsym == NULL
2501      ? !parameters->options().output_is_position_independent()
2502      : gsym->final_value_is_known());
2503
2504   switch (r_type)
2505     {
2506       // XXX
2507     }
2508 }
2509
2510 // Relocate section data.
2511
2512 template<int size, bool big_endian>
2513 void
2514 Target_powerpc<size, big_endian>::relocate_section(
2515     const Relocate_info<size, big_endian>* relinfo,
2516     unsigned int sh_type,
2517     const unsigned char* prelocs,
2518     size_t reloc_count,
2519     Output_section* output_section,
2520     bool needs_special_offset_handling,
2521     unsigned char* view,
2522     typename elfcpp::Elf_types<size>::Elf_Addr address,
2523     section_size_type view_size,
2524     const Reloc_symbol_changes* reloc_symbol_changes)
2525 {
2526   typedef Target_powerpc<size, big_endian> Powerpc;
2527   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2528
2529   gold_assert(sh_type == elfcpp::SHT_RELA);
2530
2531   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2532                          Powerpc_relocate>(
2533     relinfo,
2534     this,
2535     prelocs,
2536     reloc_count,
2537     output_section,
2538     needs_special_offset_handling,
2539     view,
2540     address,
2541     view_size,
2542     reloc_symbol_changes);
2543 }
2544
2545 class Powerpc_scan_relocatable_reloc
2546 {
2547 public:
2548   // Return the strategy to use for a local symbol which is not a
2549   // section symbol, given the relocation type.
2550   inline Relocatable_relocs::Reloc_strategy
2551   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
2552   {
2553     if (r_type == 0 && r_sym == 0)
2554       return Relocatable_relocs::RELOC_DISCARD;
2555     return Relocatable_relocs::RELOC_COPY;
2556   }
2557
2558   // Return the strategy to use for a local symbol which is a section
2559   // symbol, given the relocation type.
2560   inline Relocatable_relocs::Reloc_strategy
2561   local_section_strategy(unsigned int, Relobj*)
2562   {
2563     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2564   }
2565
2566   // Return the strategy to use for a global symbol, given the
2567   // relocation type, the object, and the symbol index.
2568   inline Relocatable_relocs::Reloc_strategy
2569   global_strategy(unsigned int r_type, Relobj*, unsigned int)
2570   {
2571     if (r_type == elfcpp::R_PPC_PLTREL24)
2572       return Relocatable_relocs::RELOC_SPECIAL;
2573     return Relocatable_relocs::RELOC_COPY;
2574   }
2575 };
2576
2577 // Scan the relocs during a relocatable link.
2578
2579 template<int size, bool big_endian>
2580 void
2581 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2582     Symbol_table* symtab,
2583     Layout* layout,
2584     Sized_relobj_file<size, big_endian>* object,
2585     unsigned int data_shndx,
2586     unsigned int sh_type,
2587     const unsigned char* prelocs,
2588     size_t reloc_count,
2589     Output_section* output_section,
2590     bool needs_special_offset_handling,
2591     size_t local_symbol_count,
2592     const unsigned char* plocal_symbols,
2593     Relocatable_relocs* rr)
2594 {
2595   gold_assert(sh_type == elfcpp::SHT_RELA);
2596
2597   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2598                                 Powerpc_scan_relocatable_reloc>(
2599     symtab,
2600     layout,
2601     object,
2602     data_shndx,
2603     prelocs,
2604     reloc_count,
2605     output_section,
2606     needs_special_offset_handling,
2607     local_symbol_count,
2608     plocal_symbols,
2609     rr);
2610 }
2611
2612 // Relocate a section during a relocatable link.
2613
2614 template<int size, bool big_endian>
2615 void
2616 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2617     const Relocate_info<size, big_endian>* relinfo,
2618     unsigned int sh_type,
2619     const unsigned char* prelocs,
2620     size_t reloc_count,
2621     Output_section* output_section,
2622     off_t offset_in_output_section,
2623     const Relocatable_relocs* rr,
2624     unsigned char*,
2625     typename elfcpp::Elf_types<size>::Elf_Addr,
2626     section_size_type,
2627     unsigned char* reloc_view,
2628     section_size_type reloc_view_size)
2629 {
2630   gold_assert(sh_type == elfcpp::SHT_RELA);
2631
2632   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2633   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
2634     Reltype;
2635   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
2636     Reltype_write;
2637   const int reloc_size
2638     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2639   const Address invalid_address = static_cast<Address>(0) - 1;
2640
2641   Powerpc_relobj<size, big_endian>* const object
2642     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
2643   const unsigned int local_count = object->local_symbol_count();
2644   unsigned int got2_shndx = object->got2_shndx();
2645   typename elfcpp::Elf_types<size>::Elf_Swxword got2_addend = 0;
2646   if (got2_shndx != 0)
2647     got2_addend = object->get_output_section_offset(got2_shndx);
2648
2649   unsigned char* pwrite = reloc_view;
2650
2651   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2652     {
2653       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
2654       if (strategy == Relocatable_relocs::RELOC_DISCARD)
2655         continue;
2656
2657       Reltype reloc(prelocs);
2658       Reltype_write reloc_write(pwrite);
2659
2660       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2661       const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2662       const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2663
2664       // Get the new symbol index.
2665
2666       unsigned int new_symndx;
2667       if (r_sym < local_count)
2668         {
2669           switch (strategy)
2670             {
2671             case Relocatable_relocs::RELOC_COPY:
2672             case Relocatable_relocs::RELOC_SPECIAL:
2673               new_symndx = object->symtab_index(r_sym);
2674               gold_assert(new_symndx != -1U);
2675               break;
2676
2677             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
2678               {
2679                 // We are adjusting a section symbol.  We need to find
2680                 // the symbol table index of the section symbol for
2681                 // the output section corresponding to input section
2682                 // in which this symbol is defined.
2683                 gold_assert(r_sym < local_count);
2684                 bool is_ordinary;
2685                 unsigned int shndx =
2686                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
2687                 gold_assert(is_ordinary);
2688                 Output_section* os = object->output_section(shndx);
2689                 gold_assert(os != NULL);
2690                 gold_assert(os->needs_symtab_index());
2691                 new_symndx = os->symtab_index();
2692               }
2693               break;
2694
2695             default:
2696               gold_unreachable();
2697             }
2698         }
2699       else
2700         {
2701           const Symbol* gsym = object->global_symbol(r_sym);
2702           gold_assert(gsym != NULL);
2703           if (gsym->is_forwarder())
2704             gsym = relinfo->symtab->resolve_forwards(gsym);
2705
2706           gold_assert(gsym->has_symtab_index());
2707           new_symndx = gsym->symtab_index();
2708         }
2709
2710       // Get the new offset--the location in the output section where
2711       // this relocation should be applied.
2712
2713       Address offset = reloc.get_r_offset();
2714       Address new_offset;
2715       if (static_cast<Address>(offset_in_output_section) != invalid_address)
2716         new_offset = offset + offset_in_output_section;
2717       else
2718         {
2719           section_offset_type sot_offset =
2720               convert_types<section_offset_type, Address>(offset);
2721           section_offset_type new_sot_offset =
2722               output_section->output_offset(object, relinfo->data_shndx,
2723                                             sot_offset);
2724           gold_assert(new_sot_offset != -1);
2725           new_offset = new_sot_offset;
2726         }
2727
2728       reloc_write.put_r_offset(new_offset);
2729       reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
2730
2731       // Handle the reloc addend based on the strategy.
2732       typename elfcpp::Elf_types<size>::Elf_Swxword addend;
2733       addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2734         get_reloc_addend(&reloc);
2735
2736       if (strategy == Relocatable_relocs::RELOC_COPY)
2737         ;
2738       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
2739         {
2740           const Symbol_value<size>* psymval = object->local_symbol(r_sym);
2741
2742           addend = psymval->value(object, addend);
2743         }
2744       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
2745         {
2746           if (addend >= 32768)
2747             addend += got2_addend;
2748         }
2749       else
2750         gold_unreachable();
2751
2752       Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2753         set_reloc_addend(&reloc_write, addend);
2754
2755       pwrite += reloc_size;
2756     }
2757
2758   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
2759               == reloc_view_size);
2760 }
2761
2762 // Return the value to use for a dynamic which requires special
2763 // treatment.  This is how we support equality comparisons of function
2764 // pointers across shared library boundaries, as described in the
2765 // processor specific ABI supplement.
2766
2767 template<int size, bool big_endian>
2768 uint64_t
2769 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2770 {
2771   if (size == 32)
2772     {
2773       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2774       return this->plt_section()->address() + gsym->plt_offset();
2775     }
2776   else
2777     gold_unreachable();
2778 }
2779
2780 // The selector for powerpc object files.
2781
2782 template<int size, bool big_endian>
2783 class Target_selector_powerpc : public Target_selector
2784 {
2785 public:
2786   Target_selector_powerpc()
2787     : Target_selector(elfcpp::EM_NONE, size, big_endian,
2788                       (size == 64
2789                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2790                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2791                       (size == 64
2792                        ? (big_endian ? "elf64ppc" : "elf64lppc")
2793                        : (big_endian ? "elf32ppc" : "elf32lppc")))
2794   { }
2795
2796   virtual Target*
2797   do_recognize(Input_file*, off_t, int machine, int, int)
2798   {
2799     switch (size)
2800       {
2801       case 64:
2802         if (machine != elfcpp::EM_PPC64)
2803           return NULL;
2804         break;
2805
2806       case 32:
2807         if (machine != elfcpp::EM_PPC)
2808           return NULL;
2809         break;
2810
2811       default:
2812         return NULL;
2813       }
2814
2815     return this->instantiate_target();
2816   }
2817
2818   virtual Target*
2819   do_instantiate_target()
2820   { return new Target_powerpc<size, big_endian>(); }
2821 };
2822
2823 Target_selector_powerpc<32, true> target_selector_ppc32;
2824 Target_selector_powerpc<32, false> target_selector_ppc32le;
2825 Target_selector_powerpc<64, true> target_selector_ppc64;
2826 Target_selector_powerpc<64, false> target_selector_ppc64le;
2827
2828 } // End anonymous namespace.