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