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