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