* layout.cc (Layout::Layout): Initialize increase_relro_.
[platform/upstream/binutils.git] / gold / powerpc.cc
1 // powerpc.cc -- powerpc target support for gold.
2
3 // Copyright 2008, 2009 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 Target_powerpc : public Sized_target<size, big_endian>
52 {
53  public:
54   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
55
56   Target_powerpc()
57     : Sized_target<size, big_endian>(&powerpc_info),
58       got_(NULL), got2_(NULL), toc_(NULL),
59       plt_(NULL), rela_dyn_(NULL),
60       copy_relocs_(elfcpp::R_POWERPC_COPY),
61       dynbss_(NULL), got_mod_index_offset_(-1U)
62   {
63   }
64
65   // Process the relocations to determine unreferenced sections for 
66   // garbage collection.
67   void
68   gc_process_relocs(Symbol_table* symtab,
69                     Layout* layout,
70                     Sized_relobj<size, big_endian>* object,
71                     unsigned int data_shndx,
72                     unsigned int sh_type,
73                     const unsigned char* prelocs,
74                     size_t reloc_count,
75                     Output_section* output_section,
76                     bool needs_special_offset_handling,
77                     size_t local_symbol_count,
78                     const unsigned char* plocal_symbols);
79
80   // Scan the relocations to look for symbol adjustments.
81   void
82   scan_relocs(Symbol_table* symtab,
83               Layout* layout,
84               Sized_relobj<size, big_endian>* object,
85               unsigned int data_shndx,
86               unsigned int sh_type,
87               const unsigned char* prelocs,
88               size_t reloc_count,
89               Output_section* output_section,
90               bool needs_special_offset_handling,
91               size_t local_symbol_count,
92               const unsigned char* plocal_symbols);
93   // Finalize the sections.
94   void
95   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
96
97   // Return the value to use for a dynamic which requires special
98   // treatment.
99   uint64_t
100   do_dynsym_value(const Symbol*) const;
101
102   // Relocate a section.
103   void
104   relocate_section(const Relocate_info<size, big_endian>*,
105                    unsigned int sh_type,
106                    const unsigned char* prelocs,
107                    size_t reloc_count,
108                    Output_section* output_section,
109                    bool needs_special_offset_handling,
110                    unsigned char* view,
111                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
112                    section_size_type view_size,
113                    const Reloc_symbol_changes*);
114
115   // Scan the relocs during a relocatable link.
116   void
117   scan_relocatable_relocs(Symbol_table* symtab,
118                           Layout* layout,
119                           Sized_relobj<size, big_endian>* object,
120                           unsigned int data_shndx,
121                           unsigned int sh_type,
122                           const unsigned char* prelocs,
123                           size_t reloc_count,
124                           Output_section* output_section,
125                           bool needs_special_offset_handling,
126                           size_t local_symbol_count,
127                           const unsigned char* plocal_symbols,
128                           Relocatable_relocs*);
129
130   // Relocate a section during a relocatable link.
131   void
132   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
133                            unsigned int sh_type,
134                            const unsigned char* prelocs,
135                            size_t reloc_count,
136                            Output_section* output_section,
137                            off_t offset_in_output_section,
138                            const Relocatable_relocs*,
139                            unsigned char* view,
140                            typename elfcpp::Elf_types<size>::Elf_Addr view_address,
141                            section_size_type view_size,
142                            unsigned char* reloc_view,
143                            section_size_type reloc_view_size);
144
145   // Return whether SYM is defined by the ABI.
146   bool
147   do_is_defined_by_abi(const Symbol* sym) const
148   {
149     return strcmp(sym->name(), "___tls_get_addr") == 0;
150   }
151
152   // Return the size of the GOT section.
153   section_size_type
154   got_size()
155   {
156     gold_assert(this->got_ != NULL);
157     return this->got_->data_size();
158   }
159
160  private:
161
162   // The class which scans relocations.
163   class Scan
164   {
165   public:
166     Scan()
167       : issued_non_pic_error_(false)
168     { }
169
170     inline void
171     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
172           Sized_relobj<size, big_endian>* object,
173           unsigned int data_shndx,
174           Output_section* output_section,
175           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
176           const elfcpp::Sym<size, big_endian>& lsym);
177
178     inline void
179     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
180            Sized_relobj<size, big_endian>* object,
181            unsigned int data_shndx,
182            Output_section* output_section,
183            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
184            Symbol* gsym);
185
186   private:
187     static void
188     unsupported_reloc_local(Sized_relobj<size, big_endian>*,
189                             unsigned int r_type);
190
191     static void
192     unsupported_reloc_global(Sized_relobj<size, big_endian>*,
193                              unsigned int r_type, Symbol*);
194
195     static void
196     generate_tls_call(Symbol_table* symtab, Layout* layout,
197                       Target_powerpc* target);
198
199     void
200     check_non_pic(Relobj*, unsigned int r_type);
201
202     // Whether we have issued an error about a non-PIC compilation.
203     bool issued_non_pic_error_;
204   };
205
206   // The class which implements relocation.
207   class Relocate
208   {
209    public:
210     // Do a relocation.  Return false if the caller should not issue
211     // any warnings about this relocation.
212     inline bool
213     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
214              Output_section*, size_t relnum,
215              const elfcpp::Rela<size, big_endian>&,
216              unsigned int r_type, const Sized_symbol<size>*,
217              const Symbol_value<size>*,
218              unsigned char*,
219              typename elfcpp::Elf_types<size>::Elf_Addr,
220              section_size_type);
221
222    private:
223     // Do a TLS relocation.
224     inline void
225     relocate_tls(const Relocate_info<size, big_endian>*,
226                  Target_powerpc* target,
227                  size_t relnum, const elfcpp::Rela<size, big_endian>&,
228                  unsigned int r_type, const Sized_symbol<size>*,
229                  const Symbol_value<size>*,
230                  unsigned char*,
231                  typename elfcpp::Elf_types<size>::Elf_Addr,
232                  section_size_type);
233   };
234
235   // A class which returns the size required for a relocation type,
236   // used while scanning relocs during a relocatable link.
237   class Relocatable_size_for_reloc
238   {
239    public:
240     unsigned int
241     get_size_for_reloc(unsigned int, Relobj*);
242   };
243
244   // Get the GOT section, creating it if necessary.
245   Output_data_got<size, big_endian>*
246   got_section(Symbol_table*, Layout*);
247
248   Output_data_space*
249   got2_section() const
250   {
251     gold_assert (this->got2_ != NULL);
252     return this->got2_;
253   }
254
255   // Get the TOC section.
256   Output_data_space*
257   toc_section() const
258   {
259     gold_assert (this->toc_ != NULL);
260     return this->toc_;
261   }
262
263   // Create a PLT entry for a global symbol.
264   void
265   make_plt_entry(Symbol_table*, Layout*, Symbol*);
266
267   // Create a GOT entry for the TLS module index.
268   unsigned int
269   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
270                       Sized_relobj<size, big_endian>* object);
271
272   // Get the PLT section.
273   const Output_data_plt_powerpc<size, big_endian>*
274   plt_section() const
275   {
276     gold_assert(this->plt_ != NULL);
277     return this->plt_;
278   }
279
280   // Get the dynamic reloc section, creating it if necessary.
281   Reloc_section*
282   rela_dyn_section(Layout*);
283
284   // Copy a relocation against a global symbol.
285   void
286   copy_reloc(Symbol_table* symtab, Layout* layout,
287              Sized_relobj<size, big_endian>* object,
288              unsigned int shndx, Output_section* output_section,
289              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
290   {
291     this->copy_relocs_.copy_reloc(symtab, layout,
292                                   symtab->get_sized_symbol<size>(sym),
293                                   object, shndx, output_section,
294                                   reloc, this->rela_dyn_section(layout));
295   }
296
297   // Information about this specific target which we pass to the
298   // general Target structure.
299   static Target::Target_info powerpc_info;
300
301   // The types of GOT entries needed for this platform.
302   enum Got_type
303   {
304     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
305     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
306     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
307   };
308
309   // The GOT section.
310   Output_data_got<size, big_endian>* got_;
311   // The GOT2 section.
312   Output_data_space* got2_;
313   // The TOC section.
314   Output_data_space* toc_;
315   // The PLT section.
316   Output_data_plt_powerpc<size, big_endian>* plt_;
317   // The dynamic reloc section.
318   Reloc_section* rela_dyn_;
319   // Relocs saved to avoid a COPY reloc.
320   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
321   // Space for variables copied with a COPY reloc.
322   Output_data_space* dynbss_;
323   // Offset of the GOT entry for the TLS module index;
324   unsigned int got_mod_index_offset_;
325 };
326
327 template<>
328 Target::Target_info Target_powerpc<32, true>::powerpc_info =
329 {
330   32,                   // size
331   true,                 // is_big_endian
332   elfcpp::EM_PPC,       // machine_code
333   false,                // has_make_symbol
334   false,                // has_resolve
335   false,                // has_code_fill
336   true,                 // is_default_stack_executable
337   '\0',                 // wrap_char
338   "/usr/lib/ld.so.1",   // dynamic_linker
339   0x10000000,           // default_text_segment_address
340   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
341   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
342   elfcpp::SHN_UNDEF,    // small_common_shndx
343   elfcpp::SHN_UNDEF,    // large_common_shndx
344   0,                    // small_common_section_flags
345   0,                    // large_common_section_flags
346   NULL,                 // attributes_section
347   NULL                  // attributes_vendor
348 };
349
350 template<>
351 Target::Target_info Target_powerpc<32, false>::powerpc_info =
352 {
353   32,                   // size
354   false,                // is_big_endian
355   elfcpp::EM_PPC,       // machine_code
356   false,                // has_make_symbol
357   false,                // has_resolve
358   false,                // has_code_fill
359   true,                 // is_default_stack_executable
360   '\0',                 // wrap_char
361   "/usr/lib/ld.so.1",   // dynamic_linker
362   0x10000000,           // default_text_segment_address
363   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
364   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
365   elfcpp::SHN_UNDEF,    // small_common_shndx
366   elfcpp::SHN_UNDEF,    // large_common_shndx
367   0,                    // small_common_section_flags
368   0,                    // large_common_section_flags
369   NULL,                 // attributes_section
370   NULL                  // attributes_vendor
371 };
372
373 template<>
374 Target::Target_info Target_powerpc<64, true>::powerpc_info =
375 {
376   64,                   // size
377   true,                 // is_big_endian
378   elfcpp::EM_PPC64,     // machine_code
379   false,                // has_make_symbol
380   false,                // has_resolve
381   false,                // has_code_fill
382   true,                 // is_default_stack_executable
383   '\0',                 // wrap_char
384   "/usr/lib/ld.so.1",   // dynamic_linker
385   0x10000000,           // default_text_segment_address
386   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
387   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
388   elfcpp::SHN_UNDEF,    // small_common_shndx
389   elfcpp::SHN_UNDEF,    // large_common_shndx
390   0,                    // small_common_section_flags
391   0,                    // large_common_section_flags
392   NULL,                 // attributes_section
393   NULL                  // attributes_vendor
394 };
395
396 template<>
397 Target::Target_info Target_powerpc<64, false>::powerpc_info =
398 {
399   64,                   // size
400   false,                // is_big_endian
401   elfcpp::EM_PPC64,     // machine_code
402   false,                // has_make_symbol
403   false,                // has_resolve
404   false,                // has_code_fill
405   true,                 // is_default_stack_executable
406   '\0',                 // wrap_char
407   "/usr/lib/ld.so.1",   // dynamic_linker
408   0x10000000,           // default_text_segment_address
409   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
410   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
411   elfcpp::SHN_UNDEF,    // small_common_shndx
412   elfcpp::SHN_UNDEF,    // large_common_shndx
413   0,                    // small_common_section_flags
414   0,                    // large_common_section_flags
415   NULL,                 // attributes_section
416   NULL                  // attributes_vendor
417 };
418
419 template<int size, bool big_endian>
420 class Powerpc_relocate_functions
421 {
422 private:
423   // Do a simple relocation with the addend in the relocation.
424   template<int valsize>
425   static inline void
426   rela(unsigned char* view,
427        unsigned int right_shift,
428        elfcpp::Elf_Xword dst_mask,
429        typename elfcpp::Swap<size, big_endian>::Valtype value,
430        typename elfcpp::Swap<size, big_endian>::Valtype addend)
431   {
432     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
433     Valtype* wv = reinterpret_cast<Valtype*>(view);
434     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
435     Valtype reloc = ((value + addend) >> right_shift);
436
437     val &= ~dst_mask;
438     reloc &= dst_mask;
439
440     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
441   }
442
443   // Do a simple relocation using a symbol value with the addend in
444   // the relocation.
445   template<int valsize>
446   static inline void
447   rela(unsigned char* view,
448        unsigned int right_shift,
449        elfcpp::Elf_Xword dst_mask,
450        const Sized_relobj<size, big_endian>* object,
451        const Symbol_value<size>* psymval,
452        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
453   {
454     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
455     Valtype* wv = reinterpret_cast<Valtype*>(view);
456     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
457     Valtype reloc = (psymval->value(object, addend) >> right_shift);
458
459     val &= ~dst_mask;
460     reloc &= dst_mask;
461
462     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
463   }
464
465   // Do a simple relocation using a symbol value with the addend in
466   // the relocation, unaligned.
467   template<int valsize>
468   static inline void
469   rela_ua(unsigned char* view, unsigned int right_shift,
470           elfcpp::Elf_Xword dst_mask,
471           const Sized_relobj<size, big_endian>* object,
472           const Symbol_value<size>* psymval,
473           typename elfcpp::Swap<size, big_endian>::Valtype addend)
474   {
475     typedef typename elfcpp::Swap_unaligned<valsize,
476             big_endian>::Valtype Valtype;
477     unsigned char* wv = view;
478     Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
479     Valtype reloc = (psymval->value(object, addend) >> right_shift);
480
481     val &= ~dst_mask;
482     reloc &= dst_mask;
483
484     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
485   }
486
487   // Do a simple PC relative relocation with a Symbol_value with the
488   // addend in the relocation.
489   template<int valsize>
490   static inline void
491   pcrela(unsigned char* view, unsigned int right_shift,
492          elfcpp::Elf_Xword dst_mask,
493          const Sized_relobj<size, big_endian>* object,
494          const Symbol_value<size>* psymval,
495          typename elfcpp::Swap<size, big_endian>::Valtype addend,
496          typename elfcpp::Elf_types<size>::Elf_Addr address)
497   {
498     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
499     Valtype* wv = reinterpret_cast<Valtype*>(view);
500     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
501     Valtype reloc = ((psymval->value(object, addend) - address)
502                      >> right_shift);
503
504     val &= ~dst_mask;
505     reloc &= dst_mask;
506
507     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
508   }
509
510   template<int valsize>
511   static inline void
512   pcrela_unaligned(unsigned char* view,
513                    const Sized_relobj<size, big_endian>* object,
514                    const Symbol_value<size>* psymval,
515                    typename elfcpp::Swap<size, big_endian>::Valtype addend,
516                    typename elfcpp::Elf_types<size>::Elf_Addr address)
517   {
518     typedef typename elfcpp::Swap_unaligned<valsize,
519             big_endian>::Valtype Valtype;
520     unsigned char* wv = view;
521     Valtype reloc = (psymval->value(object, addend) - address);
522
523     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
524   }
525
526   typedef Powerpc_relocate_functions<size, big_endian> This;
527   typedef Relocate_functions<size, big_endian> This_reloc;
528 public:
529   // R_POWERPC_REL32: (Symbol + Addend - Address)
530   static inline void
531   rel32(unsigned char* view,
532         const Sized_relobj<size, big_endian>* object,
533         const Symbol_value<size>* psymval,
534         typename elfcpp::Elf_types<size>::Elf_Addr addend,
535         typename elfcpp::Elf_types<size>::Elf_Addr address)
536   { This_reloc::pcrela32(view, object, psymval, addend, address); }
537
538   // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
539   static inline void
540   rel24(unsigned char* view,
541         const Sized_relobj<size, big_endian>* object,
542         const Symbol_value<size>* psymval,
543         typename elfcpp::Elf_types<size>::Elf_Addr addend,
544         typename elfcpp::Elf_types<size>::Elf_Addr address)
545   {
546     This::template pcrela<32>(view, 0, 0x03fffffc, object,
547                               psymval, addend, address);
548   }
549
550   // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
551   static inline void
552   rel14(unsigned char* view,
553         const Sized_relobj<size, big_endian>* object,
554         const Symbol_value<size>* psymval,
555         typename elfcpp::Elf_types<size>::Elf_Addr addend,
556         typename elfcpp::Elf_types<size>::Elf_Addr address)
557   {
558     This::template pcrela<32>(view, 0, 0x0000fffc, object,
559                               psymval, addend, address);
560   }
561
562   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
563   static inline void
564   addr16(unsigned char* view,
565          typename elfcpp::Elf_types<size>::Elf_Addr value,
566          typename elfcpp::Elf_types<size>::Elf_Addr addend)
567   { This_reloc::rela16(view, value, addend); }
568
569   static inline void
570   addr16(unsigned char* view,
571          const Sized_relobj<size, big_endian>* object,
572          const Symbol_value<size>* psymval,
573          typename elfcpp::Elf_types<size>::Elf_Addr addend)
574   { This_reloc::rela16(view, object, psymval, addend); }
575
576   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
577   static inline void
578   addr16_ds(unsigned char* view,
579             typename elfcpp::Elf_types<size>::Elf_Addr value,
580             typename elfcpp::Elf_types<size>::Elf_Addr addend)
581   {
582     This::template rela<16>(view, 0, 0xfffc, value, addend);
583   }
584
585   // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
586   static inline void
587   addr16_lo(unsigned char* view,
588          typename elfcpp::Elf_types<size>::Elf_Addr value,
589          typename elfcpp::Elf_types<size>::Elf_Addr addend)
590   { This_reloc::rela16(view, value, addend); }
591
592   static inline void
593   addr16_lo(unsigned char* view,
594             const Sized_relobj<size, big_endian>* object,
595             const Symbol_value<size>* psymval,
596             typename elfcpp::Elf_types<size>::Elf_Addr addend)
597   { This_reloc::rela16(view, object, psymval, addend); }
598
599   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
600   static inline void
601   addr16_hi(unsigned char* view,
602             typename elfcpp::Elf_types<size>::Elf_Addr value,
603             typename elfcpp::Elf_types<size>::Elf_Addr addend)
604   {
605     This::template rela<16>(view, 16, 0xffff, value, addend);
606   }
607
608   static inline void
609   addr16_hi(unsigned char* view,
610             const Sized_relobj<size, big_endian>* object,
611             const Symbol_value<size>* psymval,
612             typename elfcpp::Elf_types<size>::Elf_Addr addend)
613   {
614     This::template rela<16>(view, 16, 0xffff, object, psymval, addend);
615   }
616
617   // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
618   //                      final value of the low 16 bits of the
619   //                      relocation is negative, add one.
620   static inline void
621   addr16_ha(unsigned char* view,
622             typename elfcpp::Elf_types<size>::Elf_Addr value,
623             typename elfcpp::Elf_types<size>::Elf_Addr addend)
624   {
625     typename elfcpp::Elf_types<size>::Elf_Addr reloc;
626
627     reloc = value + addend;
628
629     if (reloc & 0x8000)
630       reloc += 0x10000;
631     reloc >>= 16;
632
633     elfcpp::Swap<16, big_endian>::writeval(view, reloc);
634   }
635
636   static inline void
637   addr16_ha(unsigned char* view,
638             const Sized_relobj<size, big_endian>* object,
639             const Symbol_value<size>* psymval,
640             typename elfcpp::Elf_types<size>::Elf_Addr addend)
641   {
642     typename elfcpp::Elf_types<size>::Elf_Addr reloc;
643
644     reloc = psymval->value(object, addend);
645
646     if (reloc & 0x8000)
647       reloc += 0x10000;
648     reloc >>= 16;
649
650     elfcpp::Swap<16, big_endian>::writeval(view, reloc);
651   }
652
653   // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
654   static inline void
655   rel16(unsigned char* view,
656         const Sized_relobj<size, big_endian>* object,
657         const Symbol_value<size>* psymval,
658         typename elfcpp::Elf_types<size>::Elf_Addr addend,
659         typename elfcpp::Elf_types<size>::Elf_Addr address)
660   { This_reloc::pcrela16(view, object, psymval, addend, address); }
661
662   // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
663   static inline void
664   rel16_lo(unsigned char* view,
665            const Sized_relobj<size, big_endian>* object,
666            const Symbol_value<size>* psymval,
667            typename elfcpp::Elf_types<size>::Elf_Addr addend,
668            typename elfcpp::Elf_types<size>::Elf_Addr address)
669   { This_reloc::pcrela16(view, object, psymval, addend, address); }
670
671   // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
672   static inline void
673   rel16_hi(unsigned char* view,
674            const Sized_relobj<size, big_endian>* object,
675            const Symbol_value<size>* psymval,
676            typename elfcpp::Elf_types<size>::Elf_Addr addend,
677            typename elfcpp::Elf_types<size>::Elf_Addr address)
678   {
679     This::template pcrela<16>(view, 16, 0xffff, object,
680                               psymval, addend, address);
681   }
682
683   // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
684   //                 final value of the low 16 bits of the
685   //                 relocation is negative, add one.
686   static inline void
687   rel16_ha(unsigned char* view,
688            const Sized_relobj<size, big_endian>* object,
689            const Symbol_value<size>* psymval,
690            typename elfcpp::Elf_types<size>::Elf_Addr addend,
691            typename elfcpp::Elf_types<size>::Elf_Addr address)
692   {
693     typename elfcpp::Elf_types<size>::Elf_Addr reloc;
694
695     reloc = (psymval->value(object, addend) - address);
696     if (reloc & 0x8000)
697       reloc += 0x10000;
698     reloc >>= 16;
699
700     elfcpp::Swap<16, big_endian>::writeval(view, reloc);
701   }
702 };
703
704 // Get the GOT section, creating it if necessary.
705
706 template<int size, bool big_endian>
707 Output_data_got<size, big_endian>*
708 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
709                                               Layout* layout)
710 {
711   if (this->got_ == NULL)
712     {
713       gold_assert(symtab != NULL && layout != NULL);
714
715       this->got_ = new Output_data_got<size, big_endian>();
716
717       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
718                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
719                                       this->got_, false, false, false, false);
720
721       // Create the GOT2 or TOC in the .got section.
722       if (size == 32)
723         {
724           this->got2_ = new Output_data_space(4, "** GOT2");
725           layout->add_output_section_data(".got2", elfcpp::SHT_PROGBITS,
726                                           elfcpp::SHF_ALLOC
727                                           | elfcpp::SHF_WRITE,
728                                           this->got2_, false, false, false,
729                                           false);
730         }
731       else
732         {
733           this->toc_ = new Output_data_space(8, "** TOC");
734           layout->add_output_section_data(".toc", elfcpp::SHT_PROGBITS,
735                                           elfcpp::SHF_ALLOC
736                                           | elfcpp::SHF_WRITE,
737                                           this->toc_, false, false, false,
738                                           false);
739         }
740
741       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
742       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
743                                     Symbol_table::PREDEFINED,
744                                     this->got_,
745                                     0, 0, elfcpp::STT_OBJECT,
746                                     elfcpp::STB_LOCAL,
747                                     elfcpp::STV_HIDDEN, 0,
748                                     false, false);
749     }
750
751   return this->got_;
752 }
753
754 // Get the dynamic reloc section, creating it if necessary.
755
756 template<int size, bool big_endian>
757 typename Target_powerpc<size, big_endian>::Reloc_section*
758 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
759 {
760   if (this->rela_dyn_ == NULL)
761     {
762       gold_assert(layout != NULL);
763       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
764       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
765                                       elfcpp::SHF_ALLOC, this->rela_dyn_, true,
766                                       false, false, false);
767     }
768   return this->rela_dyn_;
769 }
770
771 // A class to handle the PLT data.
772
773 template<int size, bool big_endian>
774 class Output_data_plt_powerpc : public Output_section_data
775 {
776  public:
777   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
778                             size, big_endian> Reloc_section;
779
780   Output_data_plt_powerpc(Layout*);
781
782   // Add an entry to the PLT.
783   void add_entry(Symbol* gsym);
784
785   // Return the .rela.plt section data.
786   const Reloc_section* rel_plt() const
787  {
788     return this->rel_;
789   }
790
791  protected:
792   void do_adjust_output_section(Output_section* os);
793
794  private:
795   // The size of an entry in the PLT.
796   static const int base_plt_entry_size = (size == 32 ? 16 : 24);
797
798   // Set the final size.
799   void
800   set_final_data_size()
801   {
802     unsigned int full_count = this->count_ + 4;
803
804     this->set_data_size(full_count * base_plt_entry_size);
805   }
806
807   // Write out the PLT data.
808   void
809   do_write(Output_file*);
810
811   // The reloc section.
812   Reloc_section* rel_;
813   // The number of PLT entries.
814   unsigned int count_;
815 };
816
817 // Create the PLT section.  The ordinary .got section is an argument,
818 // since we need to refer to the start.
819
820 template<int size, bool big_endian>
821 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(Layout* layout)
822   : Output_section_data(size == 32 ? 4 : 8), count_(0)
823 {
824   this->rel_ = new Reloc_section(false);
825   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
826                                   elfcpp::SHF_ALLOC, this->rel_, true, false,
827                                   false, false);
828 }
829
830 template<int size, bool big_endian>
831 void
832 Output_data_plt_powerpc<size, big_endian>::do_adjust_output_section(Output_section* os)
833 {
834   os->set_entsize(0);
835 }
836
837 // Add an entry to the PLT.
838
839 template<int size, bool big_endian>
840 void
841 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
842 {
843   gold_assert(!gsym->has_plt_offset());
844   unsigned int index = this->count_+ + 4;
845   section_offset_type plt_offset;
846
847   if (index < 8192)
848     plt_offset = index * base_plt_entry_size;
849   else
850     gold_unreachable();
851
852   gsym->set_plt_offset(plt_offset);
853
854   ++this->count_;
855
856   gsym->set_needs_dynsym_entry();
857   this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this,
858                          plt_offset, 0);
859 }
860
861 static const unsigned int addis_11_11     = 0x3d6b0000;
862 static const unsigned int addis_11_30     = 0x3d7e0000;
863 static const unsigned int addis_12_12     = 0x3d8c0000;
864 static const unsigned int addi_11_11      = 0x396b0000;
865 static const unsigned int add_0_11_11     = 0x7c0b5a14;
866 static const unsigned int add_11_0_11     = 0x7d605a14;
867 static const unsigned int b               = 0x48000000;
868 static const unsigned int bcl_20_31       = 0x429f0005;
869 static const unsigned int bctr            = 0x4e800420;
870 static const unsigned int lis_11          = 0x3d600000;
871 static const unsigned int lis_12          = 0x3d800000;
872 static const unsigned int lwzu_0_12       = 0x840c0000;
873 static const unsigned int lwz_0_12        = 0x800c0000;
874 static const unsigned int lwz_11_11       = 0x816b0000;
875 static const unsigned int lwz_11_30       = 0x817e0000;
876 static const unsigned int lwz_12_12       = 0x818c0000;
877 static const unsigned int mflr_0          = 0x7c0802a6;
878 static const unsigned int mflr_12         = 0x7d8802a6;
879 static const unsigned int mtctr_0         = 0x7c0903a6;
880 static const unsigned int mtctr_11        = 0x7d6903a6;
881 static const unsigned int mtlr_0          = 0x7c0803a6;
882 static const unsigned int nop             = 0x60000000;
883 static const unsigned int sub_11_11_12    = 0x7d6c5850;
884
885 static const unsigned int addis_r12_r2    = 0x3d820000;  /* addis %r12,%r2,xxx@ha     */
886 static const unsigned int std_r2_40r1     = 0xf8410028;  /* std   %r2,40(%r1)         */
887 static const unsigned int ld_r11_0r12     = 0xe96c0000;  /* ld    %r11,xxx+0@l(%r12)  */
888 static const unsigned int ld_r2_0r12      = 0xe84c0000;  /* ld    %r2,xxx+8@l(%r12)   */
889                                                          /* ld    %r11,xxx+16@l(%r12) */
890
891
892 // Write out the PLT.
893
894 template<int size, bool big_endian>
895 void
896 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
897 {
898   const off_t offset = this->offset();
899   const section_size_type oview_size =
900     convert_to_section_size_type(this->data_size());
901   unsigned char* const oview = of->get_output_view(offset, oview_size);
902   unsigned char* pov = oview;
903
904   memset(pov, 0, base_plt_entry_size * 4);
905   pov += base_plt_entry_size * 4;
906
907   unsigned int plt_offset = base_plt_entry_size * 4;
908   const unsigned int count = this->count_;
909
910   if (size == 64)
911     {
912       for (unsigned int i = 0; i < count; i++)
913         {
914         }
915     }
916   else
917     {
918       for (unsigned int i = 0; i < count; i++)
919         {
920           elfcpp::Swap<32, true>::writeval(pov + 0x00,
921                                            lwz_11_30 + plt_offset);
922           elfcpp::Swap<32, true>::writeval(pov + 0x04, mtctr_11);
923           elfcpp::Swap<32, true>::writeval(pov + 0x08, bctr);
924           elfcpp::Swap<32, true>::writeval(pov + 0x0c, nop);
925           pov += base_plt_entry_size;
926           plt_offset += base_plt_entry_size;
927         }
928     }
929
930   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
931
932   of->write_output_view(offset, oview_size, oview);
933 }
934
935 // Create a PLT entry for a global symbol.
936
937 template<int size, bool big_endian>
938 void
939 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
940                                                  Layout* layout,
941                                                  Symbol* gsym)
942 {
943   if (gsym->has_plt_offset())
944     return;
945
946   if (this->plt_ == NULL)
947     {
948       // Create the GOT section first.
949       this->got_section(symtab, layout);
950
951       this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout);
952       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
953                                       (elfcpp::SHF_ALLOC
954                                        | elfcpp::SHF_EXECINSTR
955                                        | elfcpp::SHF_WRITE),
956                                       this->plt_, false, false, false, false);
957
958       // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
959       symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
960                                     Symbol_table::PREDEFINED,
961                                     this->plt_,
962                                     0, 0, elfcpp::STT_OBJECT,
963                                     elfcpp::STB_LOCAL,
964                                     elfcpp::STV_HIDDEN, 0,
965                                     false, false);
966     }
967
968   this->plt_->add_entry(gsym);
969 }
970
971 // Create a GOT entry for the TLS module index.
972
973 template<int size, bool big_endian>
974 unsigned int
975 Target_powerpc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
976                                                       Layout* layout,
977                                                       Sized_relobj<size, big_endian>* object)
978 {
979   if (this->got_mod_index_offset_ == -1U)
980     {
981       gold_assert(symtab != NULL && layout != NULL && object != NULL);
982       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
983       Output_data_got<size, big_endian>* got;
984       unsigned int got_offset;
985
986       got = this->got_section(symtab, layout);
987       got_offset = got->add_constant(0);
988       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
989                           got_offset, 0);
990       got->add_constant(0);
991       this->got_mod_index_offset_ = got_offset;
992     }
993   return this->got_mod_index_offset_;
994 }
995
996 // Optimize the TLS relocation type based on what we know about the
997 // symbol.  IS_FINAL is true if the final address of this symbol is
998 // known at link time.
999
1000 static tls::Tls_optimization
1001 optimize_tls_reloc(bool /* is_final */, int r_type)
1002 {
1003   // If we are generating a shared library, then we can't do anything
1004   // in the linker.
1005   if (parameters->options().shared())
1006     return tls::TLSOPT_NONE;
1007   switch (r_type)
1008     {
1009       // XXX
1010     default:
1011       gold_unreachable();
1012     }
1013 }
1014
1015 // Report an unsupported relocation against a local symbol.
1016
1017 template<int size, bool big_endian>
1018 void
1019 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1020                         Sized_relobj<size, big_endian>* object,
1021                         unsigned int r_type)
1022 {
1023   gold_error(_("%s: unsupported reloc %u against local symbol"),
1024              object->name().c_str(), r_type);
1025 }
1026
1027 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1028 // dynamic linker does not support it, issue an error.
1029
1030 template<int size, bool big_endian>
1031 void
1032 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1033                                                       unsigned int r_type)
1034 {
1035   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1036
1037   // These are the relocation types supported by glibc for both 32-bit
1038   // and 64-bit powerpc.
1039   switch (r_type)
1040     {
1041     case elfcpp::R_POWERPC_RELATIVE:
1042     case elfcpp::R_POWERPC_GLOB_DAT:
1043     case elfcpp::R_POWERPC_DTPMOD:
1044     case elfcpp::R_POWERPC_DTPREL:
1045     case elfcpp::R_POWERPC_TPREL:
1046     case elfcpp::R_POWERPC_JMP_SLOT:
1047     case elfcpp::R_POWERPC_COPY:
1048     case elfcpp::R_POWERPC_ADDR32:
1049     case elfcpp::R_POWERPC_ADDR24:
1050     case elfcpp::R_POWERPC_REL24:
1051       return;
1052
1053     default:
1054       break;
1055     }
1056
1057   if (size == 64)
1058     {
1059       switch (r_type)
1060         {
1061           // These are the relocation types supported only on 64-bit.
1062         case elfcpp::R_PPC64_ADDR64:
1063         case elfcpp::R_PPC64_TPREL16_LO_DS:
1064         case elfcpp::R_PPC64_TPREL16_DS:
1065         case elfcpp::R_POWERPC_TPREL16:
1066         case elfcpp::R_POWERPC_TPREL16_LO:
1067         case elfcpp::R_POWERPC_TPREL16_HI:
1068         case elfcpp::R_POWERPC_TPREL16_HA:
1069         case elfcpp::R_PPC64_TPREL16_HIGHER:
1070         case elfcpp::R_PPC64_TPREL16_HIGHEST:
1071         case elfcpp::R_PPC64_TPREL16_HIGHERA:
1072         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1073         case elfcpp::R_PPC64_ADDR16_LO_DS:
1074         case elfcpp::R_POWERPC_ADDR16_LO:
1075         case elfcpp::R_POWERPC_ADDR16_HI:
1076         case elfcpp::R_POWERPC_ADDR16_HA:
1077         case elfcpp::R_POWERPC_ADDR30:
1078         case elfcpp::R_PPC64_UADDR64:
1079         case elfcpp::R_POWERPC_UADDR32:
1080         case elfcpp::R_POWERPC_ADDR16:
1081         case elfcpp::R_POWERPC_UADDR16:
1082         case elfcpp::R_PPC64_ADDR16_DS:
1083         case elfcpp::R_PPC64_ADDR16_HIGHER:
1084         case elfcpp::R_PPC64_ADDR16_HIGHEST:
1085         case elfcpp::R_PPC64_ADDR16_HIGHERA:
1086         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1087         case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1088         case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1089         case elfcpp::R_POWERPC_REL32:
1090         case elfcpp::R_PPC64_REL64:
1091           return;
1092
1093         default:
1094           break;
1095         }
1096     }
1097   else
1098     {
1099       switch (r_type)
1100         {
1101           // These are the relocation types supported only on 32-bit.
1102
1103         default:
1104           break;
1105         }
1106     }
1107
1108   // This prevents us from issuing more than one error per reloc
1109   // section.  But we can still wind up issuing more than one
1110   // error per object file.
1111   if (this->issued_non_pic_error_)
1112     return;
1113   gold_assert(parameters->options().output_is_position_independent());
1114   object->error(_("requires unsupported dynamic reloc; "
1115                   "recompile with -fPIC"));
1116   this->issued_non_pic_error_ = true;
1117   return;
1118 }
1119
1120 // Scan a relocation for a local symbol.
1121
1122 template<int size, bool big_endian>
1123 inline void
1124 Target_powerpc<size, big_endian>::Scan::local(
1125                         Symbol_table* symtab,
1126                         Layout* layout,
1127                         Target_powerpc<size, big_endian>* target,
1128                         Sized_relobj<size, big_endian>* object,
1129                         unsigned int data_shndx,
1130                         Output_section* output_section,
1131                         const elfcpp::Rela<size, big_endian>& reloc,
1132                         unsigned int r_type,
1133                         const elfcpp::Sym<size, big_endian>& lsym)
1134 {
1135   switch (r_type)
1136     {
1137     case elfcpp::R_POWERPC_NONE:
1138     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1139     case elfcpp::R_POWERPC_GNU_VTENTRY:
1140       break;
1141
1142     case elfcpp::R_PPC64_ADDR64:
1143     case elfcpp::R_POWERPC_ADDR32:
1144     case elfcpp::R_POWERPC_ADDR16_HA:
1145     case elfcpp::R_POWERPC_ADDR16_LO:
1146       // If building a shared library (or a position-independent
1147       // executable), we need to create a dynamic relocation for
1148       // this location.
1149       if (parameters->options().output_is_position_independent())
1150         {
1151           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1152
1153           check_non_pic(object, r_type);
1154           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1155             {
1156               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1157               rela_dyn->add_local(object, r_sym, r_type, output_section,
1158                                   data_shndx, reloc.get_r_offset(),
1159                                   reloc.get_r_addend());
1160             }
1161           else
1162             {
1163               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1164               gold_assert(lsym.get_st_value() == 0);
1165               rela_dyn->add_local_relative(object, r_sym, r_type,
1166                                            output_section, data_shndx,
1167                                            reloc.get_r_offset(),
1168                                            reloc.get_r_addend());
1169             }
1170         }
1171       break;
1172
1173     case elfcpp::R_POWERPC_REL24:
1174     case elfcpp::R_PPC_LOCAL24PC:
1175     case elfcpp::R_POWERPC_REL32:
1176     case elfcpp::R_PPC_REL16_LO:
1177     case elfcpp::R_PPC_REL16_HA:
1178       break;
1179
1180     case elfcpp::R_POWERPC_GOT16:
1181     case elfcpp::R_POWERPC_GOT16_LO:
1182     case elfcpp::R_POWERPC_GOT16_HI:
1183     case elfcpp::R_POWERPC_GOT16_HA:
1184     case elfcpp::R_PPC64_TOC16:
1185     case elfcpp::R_PPC64_TOC16_LO:
1186     case elfcpp::R_PPC64_TOC16_HI:
1187     case elfcpp::R_PPC64_TOC16_HA:
1188     case elfcpp::R_PPC64_TOC16_DS:
1189     case elfcpp::R_PPC64_TOC16_LO_DS:
1190       {
1191         // The symbol requires a GOT entry.
1192         Output_data_got<size, big_endian>* got;
1193         unsigned int r_sym;
1194
1195         got = target->got_section(symtab, layout);
1196         r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1197
1198         // If we are generating a shared object, we need to add a
1199         // dynamic relocation for this symbol's GOT entry.
1200         if (parameters->options().output_is_position_independent())
1201           {
1202             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1203               {
1204                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1205                 unsigned int off;
1206
1207                 off = got->add_constant(0);
1208                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1209                 rela_dyn->add_local_relative(object, r_sym,
1210                                              elfcpp::R_POWERPC_RELATIVE,
1211                                              got, off, 0);
1212               }
1213           }
1214         else
1215           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1216       }
1217       break;
1218
1219     case elfcpp::R_PPC64_TOC:
1220       // We need a GOT section.
1221       target->got_section(symtab, layout);
1222       break;
1223
1224       // These are relocations which should only be seen by the
1225       // dynamic linker, and should never be seen here.
1226     case elfcpp::R_POWERPC_COPY:
1227     case elfcpp::R_POWERPC_GLOB_DAT:
1228     case elfcpp::R_POWERPC_JMP_SLOT:
1229     case elfcpp::R_POWERPC_RELATIVE:
1230     case elfcpp::R_POWERPC_DTPMOD:
1231       gold_error(_("%s: unexpected reloc %u in object file"),
1232                  object->name().c_str(), r_type);
1233       break;
1234
1235     default:
1236       unsupported_reloc_local(object, r_type);
1237       break;
1238     }
1239 }
1240
1241 // Report an unsupported relocation against a global symbol.
1242
1243 template<int size, bool big_endian>
1244 void
1245 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1246                         Sized_relobj<size, big_endian>* object,
1247                         unsigned int r_type,
1248                         Symbol* gsym)
1249 {
1250   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1251              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1252 }
1253
1254 // Scan a relocation for a global symbol.
1255
1256 template<int size, bool big_endian>
1257 inline void
1258 Target_powerpc<size, big_endian>::Scan::global(
1259                                 Symbol_table* symtab,
1260                                 Layout* layout,
1261                                 Target_powerpc<size, big_endian>* target,
1262                                 Sized_relobj<size, big_endian>* object,
1263                                 unsigned int data_shndx,
1264                                 Output_section* output_section,
1265                                 const elfcpp::Rela<size, big_endian>& reloc,
1266                                 unsigned int r_type,
1267                                 Symbol* gsym)
1268 {
1269   switch (r_type)
1270     {
1271     case elfcpp::R_POWERPC_NONE:
1272     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1273     case elfcpp::R_POWERPC_GNU_VTENTRY:
1274       break;
1275
1276     case elfcpp::R_PPC_PLTREL24:
1277       // If the symbol is fully resolved, this is just a PC32 reloc.
1278       // Otherwise we need a PLT entry.
1279       if (gsym->final_value_is_known())
1280         break;
1281       // If building a shared library, we can also skip the PLT entry
1282       // if the symbol is defined in the output file and is protected
1283       // or hidden.
1284       if (gsym->is_defined()
1285           && !gsym->is_from_dynobj()
1286           && !gsym->is_preemptible())
1287         break;
1288       target->make_plt_entry(symtab, layout, gsym);
1289       break;
1290
1291     case elfcpp::R_POWERPC_ADDR16:
1292     case elfcpp::R_POWERPC_ADDR16_LO:
1293     case elfcpp::R_POWERPC_ADDR16_HI:
1294     case elfcpp::R_POWERPC_ADDR16_HA:
1295     case elfcpp::R_POWERPC_ADDR32:
1296     case elfcpp::R_PPC64_ADDR64:
1297       {
1298         // Make a PLT entry if necessary.
1299         if (gsym->needs_plt_entry())
1300           {
1301             target->make_plt_entry(symtab, layout, gsym);
1302             // Since this is not a PC-relative relocation, we may be
1303             // taking the address of a function. In that case we need to
1304             // set the entry in the dynamic symbol table to the address of
1305             // the PLT entry.
1306             if (gsym->is_from_dynobj() && !parameters->options().shared())
1307               gsym->set_needs_dynsym_value();
1308           }
1309         // Make a dynamic relocation if necessary.
1310         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1311           {
1312             if (gsym->may_need_copy_reloc())
1313               {
1314                 target->copy_reloc(symtab, layout, object,
1315                                    data_shndx, output_section, gsym, reloc);
1316               }
1317             else if ((r_type == elfcpp::R_POWERPC_ADDR32
1318                       || r_type == elfcpp::R_PPC64_ADDR64)
1319                      && gsym->can_use_relative_reloc(false))
1320               {
1321                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1322                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1323                                               output_section, object,
1324                                               data_shndx, reloc.get_r_offset(),
1325                                               reloc.get_r_addend());
1326               }
1327             else
1328               {
1329                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1330
1331                 check_non_pic(object, r_type);
1332                 if (gsym->is_from_dynobj()
1333                     || gsym->is_undefined()
1334                     || gsym->is_preemptible())
1335                   rela_dyn->add_global(gsym, r_type, output_section,
1336                                        object, data_shndx,
1337                                        reloc.get_r_offset(),
1338                                        reloc.get_r_addend());
1339                 else
1340                   rela_dyn->add_global_relative(gsym, r_type,
1341                                                 output_section, object,
1342                                                 data_shndx,
1343                                                 reloc.get_r_offset(),
1344                                                 reloc.get_r_addend());
1345               }
1346           }
1347       }
1348       break;
1349
1350     case elfcpp::R_POWERPC_REL24:
1351     case elfcpp::R_PPC_LOCAL24PC:
1352     case elfcpp::R_PPC_REL16:
1353     case elfcpp::R_PPC_REL16_LO:
1354     case elfcpp::R_PPC_REL16_HI:
1355     case elfcpp::R_PPC_REL16_HA:
1356       {
1357         if (gsym->needs_plt_entry())
1358           target->make_plt_entry(symtab, layout, gsym);
1359         // Make a dynamic relocation if necessary.
1360         int flags = Symbol::NON_PIC_REF;
1361         if (gsym->type() == elfcpp::STT_FUNC)
1362           flags |= Symbol::FUNCTION_CALL;
1363         if (gsym->needs_dynamic_reloc(flags))
1364           {
1365             if (gsym->may_need_copy_reloc())
1366               {
1367                 target->copy_reloc(symtab, layout, object,
1368                                    data_shndx, output_section, gsym,
1369                                    reloc);
1370               }
1371             else
1372               {
1373                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1374                 check_non_pic(object, r_type);
1375                 rela_dyn->add_global(gsym, r_type, output_section, object,
1376                                      data_shndx, reloc.get_r_offset(),
1377                                      reloc.get_r_addend());
1378               }
1379           }
1380       }
1381       break;
1382
1383     case elfcpp::R_POWERPC_GOT16:
1384     case elfcpp::R_POWERPC_GOT16_LO:
1385     case elfcpp::R_POWERPC_GOT16_HI:
1386     case elfcpp::R_POWERPC_GOT16_HA:
1387     case elfcpp::R_PPC64_TOC16:
1388     case elfcpp::R_PPC64_TOC16_LO:
1389     case elfcpp::R_PPC64_TOC16_HI:
1390     case elfcpp::R_PPC64_TOC16_HA:
1391     case elfcpp::R_PPC64_TOC16_DS:
1392     case elfcpp::R_PPC64_TOC16_LO_DS:
1393       {
1394         // The symbol requires a GOT entry.
1395         Output_data_got<size, big_endian>* got;
1396
1397         got = target->got_section(symtab, layout);
1398         if (gsym->final_value_is_known())
1399           got->add_global(gsym, GOT_TYPE_STANDARD);
1400         else
1401           {
1402             // If this symbol is not fully resolved, we need to add a
1403             // dynamic relocation for it.
1404             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1405             if (gsym->is_from_dynobj()
1406                 || gsym->is_undefined()
1407                 || gsym->is_preemptible())
1408               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1409                                         elfcpp::R_POWERPC_GLOB_DAT);
1410             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1411               {
1412                 unsigned int off = got->add_constant(0);
1413
1414                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1415                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1416                                               got, off, 0);
1417               }
1418           }
1419       }
1420       break;
1421
1422     case elfcpp::R_PPC64_TOC:
1423       // We need a GOT section.
1424       target->got_section(symtab, layout);
1425       break;
1426
1427     case elfcpp::R_POWERPC_GOT_TPREL16:
1428     case elfcpp::R_POWERPC_TLS:
1429       // XXX TLS
1430       break;
1431
1432       // These are relocations which should only be seen by the
1433       // dynamic linker, and should never be seen here.
1434     case elfcpp::R_POWERPC_COPY:
1435     case elfcpp::R_POWERPC_GLOB_DAT:
1436     case elfcpp::R_POWERPC_JMP_SLOT:
1437     case elfcpp::R_POWERPC_RELATIVE:
1438     case elfcpp::R_POWERPC_DTPMOD:
1439       gold_error(_("%s: unexpected reloc %u in object file"),
1440                  object->name().c_str(), r_type);
1441       break;
1442
1443     default:
1444       unsupported_reloc_global(object, r_type, gsym);
1445       break;
1446     }
1447 }
1448
1449 // Process relocations for gc.
1450
1451 template<int size, bool big_endian>
1452 void
1453 Target_powerpc<size, big_endian>::gc_process_relocs(
1454                         Symbol_table* symtab,
1455                         Layout* layout,
1456                         Sized_relobj<size, big_endian>* object,
1457                         unsigned int data_shndx,
1458                         unsigned int,
1459                         const unsigned char* prelocs,
1460                         size_t reloc_count,
1461                         Output_section* output_section,
1462                         bool needs_special_offset_handling,
1463                         size_t local_symbol_count,
1464                         const unsigned char* plocal_symbols)
1465 {
1466   typedef Target_powerpc<size, big_endian> Powerpc;
1467   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1468
1469   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1470     symtab,
1471     layout,
1472     this,
1473     object,
1474     data_shndx,
1475     prelocs,
1476     reloc_count,
1477     output_section,
1478     needs_special_offset_handling,
1479     local_symbol_count,
1480     plocal_symbols);
1481 }
1482
1483 // Scan relocations for a section.
1484
1485 template<int size, bool big_endian>
1486 void
1487 Target_powerpc<size, big_endian>::scan_relocs(
1488                         Symbol_table* symtab,
1489                         Layout* layout,
1490                         Sized_relobj<size, big_endian>* object,
1491                         unsigned int data_shndx,
1492                         unsigned int sh_type,
1493                         const unsigned char* prelocs,
1494                         size_t reloc_count,
1495                         Output_section* output_section,
1496                         bool needs_special_offset_handling,
1497                         size_t local_symbol_count,
1498                         const unsigned char* plocal_symbols)
1499 {
1500   typedef Target_powerpc<size, big_endian> Powerpc;
1501   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1502   static Output_data_space* sdata;
1503
1504   if (sh_type == elfcpp::SHT_REL)
1505     {
1506       gold_error(_("%s: unsupported REL reloc section"),
1507                  object->name().c_str());
1508       return;
1509     }
1510
1511   // Define _SDA_BASE_ at the start of the .sdata section.
1512   if (sdata == NULL)
1513   {
1514     // layout->find_output_section(".sdata") == NULL
1515     sdata = new Output_data_space(4, "** sdata");
1516     Output_section* os = layout->add_output_section_data(".sdata", 0,
1517                                                          elfcpp::SHF_ALLOC
1518                                                          | elfcpp::SHF_WRITE,
1519                                                          sdata, false,
1520                                                          false, false, false);
1521     symtab->define_in_output_data("_SDA_BASE_", NULL,
1522                                   Symbol_table::PREDEFINED,
1523                                   os,
1524                                   32768, 0,
1525                                   elfcpp::STT_OBJECT,
1526                                   elfcpp::STB_LOCAL,
1527                                   elfcpp::STV_HIDDEN, 0,
1528                                   false, false);
1529   }
1530
1531   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1532     symtab,
1533     layout,
1534     this,
1535     object,
1536     data_shndx,
1537     prelocs,
1538     reloc_count,
1539     output_section,
1540     needs_special_offset_handling,
1541     local_symbol_count,
1542     plocal_symbols);
1543 }
1544
1545 // Finalize the sections.
1546
1547 template<int size, bool big_endian>
1548 void
1549 Target_powerpc<size, big_endian>::do_finalize_sections(
1550     Layout* layout,
1551     const Input_objects*,
1552     Symbol_table*)
1553 {
1554   // Fill in some more dynamic tags.
1555   Output_data_dynamic* const odyn = layout->dynamic_data();
1556   if (odyn != NULL)
1557     {
1558       if (this->plt_ != NULL
1559           && this->plt_->output_section() != NULL)
1560         {
1561           const Output_data* od = this->plt_->rel_plt();
1562           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1563           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1564           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1565
1566           odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
1567         }
1568
1569       if (this->rela_dyn_ != NULL
1570           && this->rela_dyn_->output_section() != NULL)
1571         {
1572           const Output_data* od = this->rela_dyn_;
1573           odyn->add_section_address(elfcpp::DT_RELA, od);
1574           odyn->add_section_size(elfcpp::DT_RELASZ, od);
1575           odyn->add_constant(elfcpp::DT_RELAENT,
1576                              elfcpp::Elf_sizes<size>::rela_size);
1577         }
1578
1579       if (!parameters->options().shared())
1580         {
1581           // The value of the DT_DEBUG tag is filled in by the dynamic
1582           // linker at run time, and used by the debugger.
1583           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1584         }
1585     }
1586
1587   // Emit any relocs we saved in an attempt to avoid generating COPY
1588   // relocs.
1589   if (this->copy_relocs_.any_saved_relocs())
1590     this->copy_relocs_.emit(this->rela_dyn_section(layout));
1591 }
1592
1593 // Perform a relocation.
1594
1595 template<int size, bool big_endian>
1596 inline bool
1597 Target_powerpc<size, big_endian>::Relocate::relocate(
1598                         const Relocate_info<size, big_endian>* relinfo,
1599                         Target_powerpc* target,
1600                         Output_section*,
1601                         size_t relnum,
1602                         const elfcpp::Rela<size, big_endian>& rela,
1603                         unsigned int r_type,
1604                         const Sized_symbol<size>* gsym,
1605                         const Symbol_value<size>* psymval,
1606                         unsigned char* view,
1607                         typename elfcpp::Elf_types<size>::Elf_Addr address,
1608                         section_size_type /* view_size */)
1609 {
1610   const unsigned int toc_base_offset = 0x8000;
1611   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1612
1613   // Pick the value to use for symbols defined in shared objects.
1614   Symbol_value<size> symval;
1615   if (gsym != NULL
1616       && gsym->use_plt_offset(r_type == elfcpp::R_POWERPC_REL24
1617                               || r_type == elfcpp::R_PPC_LOCAL24PC
1618                               || r_type == elfcpp::R_PPC_REL16
1619                               || r_type == elfcpp::R_PPC_REL16_LO
1620                               || r_type == elfcpp::R_PPC_REL16_HI
1621                               || r_type == elfcpp::R_PPC_REL16_HA))
1622     {
1623       elfcpp::Elf_Xword value;
1624
1625       value = target->plt_section()->address() + gsym->plt_offset();
1626
1627       symval.set_output_value(value);
1628
1629       psymval = &symval;
1630     }
1631
1632   const Sized_relobj<size, big_endian>* object = relinfo->object;
1633   elfcpp::Elf_Xword addend = rela.get_r_addend();
1634
1635   // Get the GOT offset if needed.  Unlike i386 and x86_64, our GOT
1636   // pointer points to the beginning, not the end, of the table.
1637   // So we just use the plain offset.
1638   bool have_got_offset = false;
1639   unsigned int got_offset = 0;
1640   unsigned int got2_offset = 0;
1641   switch (r_type)
1642     {
1643     case elfcpp::R_PPC64_TOC16:
1644     case elfcpp::R_PPC64_TOC16_LO:
1645     case elfcpp::R_PPC64_TOC16_HI:
1646     case elfcpp::R_PPC64_TOC16_HA:
1647     case elfcpp::R_PPC64_TOC16_DS:
1648     case elfcpp::R_PPC64_TOC16_LO_DS:
1649         // Subtract the TOC base address.
1650         addend -= target->toc_section()->address() + toc_base_offset;
1651         /* FALLTHRU */
1652
1653     case elfcpp::R_POWERPC_GOT16:
1654     case elfcpp::R_POWERPC_GOT16_LO:
1655     case elfcpp::R_POWERPC_GOT16_HI:
1656     case elfcpp::R_POWERPC_GOT16_HA:
1657     case elfcpp::R_PPC64_GOT16_DS:
1658     case elfcpp::R_PPC64_GOT16_LO_DS:
1659       if (gsym != NULL)
1660         {
1661           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1662           got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1663         }
1664       else
1665         {
1666           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1667           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1668           got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1669         }
1670       have_got_offset = true;
1671       break;
1672
1673       // R_PPC_PLTREL24 is rather special.  If non-zero,
1674       // the addend specifies the GOT pointer offset within .got2.  
1675     case elfcpp::R_PPC_PLTREL24:
1676       if (addend >= 32768)
1677         {
1678           Output_data_space* got2;
1679           got2 = target->got2_section();
1680           got2_offset = got2->offset();
1681           addend += got2_offset;
1682         }
1683       have_got_offset = true;
1684       break;
1685
1686     default:
1687       break;
1688     }
1689
1690   switch (r_type)
1691     {
1692     case elfcpp::R_POWERPC_NONE:
1693     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1694     case elfcpp::R_POWERPC_GNU_VTENTRY:
1695       break;
1696
1697     case elfcpp::R_POWERPC_REL32:
1698       Reloc::rel32(view, object, psymval, addend, address);
1699       break;
1700
1701     case elfcpp::R_POWERPC_REL24:
1702       Reloc::rel24(view, object, psymval, addend, address);
1703       break;
1704
1705     case elfcpp::R_POWERPC_REL14:
1706       Reloc::rel14(view, object, psymval, addend, address);
1707       break;
1708
1709     case elfcpp::R_PPC_PLTREL24:
1710       Reloc::rel24(view, object, psymval, addend, address);
1711       break;
1712
1713     case elfcpp::R_PPC_LOCAL24PC:
1714       Reloc::rel24(view, object, psymval, addend, address);
1715       break;
1716
1717     case elfcpp::R_PPC64_ADDR64:
1718       if (!parameters->options().output_is_position_independent())
1719         Relocate_functions<size, big_endian>::rela64(view, object,
1720                                                      psymval, addend);
1721       break;
1722
1723     case elfcpp::R_POWERPC_ADDR32:
1724       if (!parameters->options().output_is_position_independent())
1725         Relocate_functions<size, big_endian>::rela32(view, object,
1726                                                      psymval, addend);
1727       break;
1728
1729     case elfcpp::R_POWERPC_ADDR16_LO:
1730       Reloc::addr16_lo(view, object, psymval, addend);
1731       break;
1732
1733     case elfcpp::R_POWERPC_ADDR16_HI:
1734       Reloc::addr16_hi(view, object, psymval, addend);
1735       break;
1736
1737     case elfcpp::R_POWERPC_ADDR16_HA:
1738       Reloc::addr16_ha(view, object, psymval, addend);
1739       break;
1740
1741     case elfcpp::R_PPC_REL16_LO:
1742       Reloc::rel16_lo(view, object, psymval, addend, address);
1743       break;
1744
1745     case elfcpp::R_PPC_REL16_HI:
1746       Reloc::rel16_lo(view, object, psymval, addend, address);
1747       break;
1748
1749     case elfcpp::R_PPC_REL16_HA:
1750       Reloc::rel16_ha(view, object, psymval, addend, address);
1751       break;
1752
1753     case elfcpp::R_POWERPC_GOT16:
1754       Reloc::addr16(view, got_offset, addend);
1755       break;
1756
1757     case elfcpp::R_POWERPC_GOT16_LO:
1758       Reloc::addr16_lo(view, got_offset, addend);
1759       break;
1760
1761     case elfcpp::R_POWERPC_GOT16_HI:
1762       Reloc::addr16_hi(view, got_offset, addend);
1763       break;
1764
1765     case elfcpp::R_POWERPC_GOT16_HA:
1766       Reloc::addr16_ha(view, got_offset, addend);
1767       break;
1768
1769     case elfcpp::R_PPC64_TOC16:
1770       Reloc::addr16(view, got_offset, addend);
1771       break;
1772
1773     case elfcpp::R_PPC64_TOC16_LO:
1774       Reloc::addr16_lo(view, got_offset, addend);
1775       break;
1776
1777     case elfcpp::R_PPC64_TOC16_HI:
1778       Reloc::addr16_hi(view, got_offset, addend);
1779       break;
1780
1781     case elfcpp::R_PPC64_TOC16_HA:
1782       Reloc::addr16_ha(view, got_offset, addend);
1783       break;
1784
1785     case elfcpp::R_PPC64_TOC16_DS:
1786     case elfcpp::R_PPC64_TOC16_LO_DS:
1787       Reloc::addr16_ds(view, got_offset, addend);
1788       break;
1789
1790     case elfcpp::R_PPC64_TOC:
1791       {
1792         elfcpp::Elf_types<64>::Elf_Addr value;
1793         value = target->toc_section()->address() + toc_base_offset;
1794         Relocate_functions<64, false>::rela64(view, value, addend);
1795       }
1796       break;
1797
1798     case elfcpp::R_POWERPC_COPY:
1799     case elfcpp::R_POWERPC_GLOB_DAT:
1800     case elfcpp::R_POWERPC_JMP_SLOT:
1801     case elfcpp::R_POWERPC_RELATIVE:
1802       // This is an outstanding tls reloc, which is unexpected when
1803       // linking.
1804     case elfcpp::R_POWERPC_DTPMOD:
1805       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1806                              _("unexpected reloc %u in object file"),
1807                              r_type);
1808       break;
1809
1810     default:
1811       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1812                              _("unsupported reloc %u"),
1813                              r_type);
1814       break;
1815     }
1816
1817   return true;
1818 }
1819
1820 // Perform a TLS relocation.
1821
1822 template<int size, bool big_endian>
1823 inline void
1824 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1825                         const Relocate_info<size, big_endian>* relinfo,
1826                         Target_powerpc<size, big_endian>* target,
1827                         size_t relnum,
1828                         const elfcpp::Rela<size, big_endian>& rela,
1829                         unsigned int r_type,
1830                         const Sized_symbol<size>* gsym,
1831                         const Symbol_value<size>* psymval,
1832                         unsigned char* view,
1833                         typename elfcpp::Elf_types<size>::Elf_Addr address,
1834                         section_size_type)
1835 {
1836   Output_segment* tls_segment = relinfo->layout->tls_segment();
1837   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1838   const Sized_relobj<size, big_endian>* object = relinfo->object;
1839
1840   const elfcpp::Elf_Xword addend = rela.get_r_addend();
1841   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1842
1843   const bool is_final =
1844     (gsym == NULL
1845      ? !parameters->options().output_is_position_independent()
1846      : gsym->final_value_is_known());
1847   const tls::Tls_optimization optimized_type
1848       = optimize_tls_reloc(is_final, r_type);
1849
1850   switch (r_type)
1851     {
1852       // XXX
1853     }
1854 }
1855
1856 // Relocate section data.
1857
1858 template<int size, bool big_endian>
1859 void
1860 Target_powerpc<size, big_endian>::relocate_section(
1861                         const Relocate_info<size, big_endian>* relinfo,
1862                         unsigned int sh_type,
1863                         const unsigned char* prelocs,
1864                         size_t reloc_count,
1865                         Output_section* output_section,
1866                         bool needs_special_offset_handling,
1867                         unsigned char* view,
1868                         typename elfcpp::Elf_types<size>::Elf_Addr address,
1869                         section_size_type view_size,
1870                         const Reloc_symbol_changes* reloc_symbol_changes)
1871 {
1872   typedef Target_powerpc<size, big_endian> Powerpc;
1873   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1874
1875   gold_assert(sh_type == elfcpp::SHT_RELA);
1876
1877   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
1878     Powerpc_relocate>(
1879     relinfo,
1880     this,
1881     prelocs,
1882     reloc_count,
1883     output_section,
1884     needs_special_offset_handling,
1885     view,
1886     address,
1887     view_size,
1888     reloc_symbol_changes);
1889 }
1890
1891 // Return the size of a relocation while scanning during a relocatable
1892 // link.
1893
1894 template<int size, bool big_endian>
1895 unsigned int
1896 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
1897     unsigned int,
1898     Relobj*)
1899 {
1900   // We are always SHT_RELA, so we should never get here.
1901   gold_unreachable();
1902   return 0;
1903 }
1904
1905 // Scan the relocs during a relocatable link.
1906
1907 template<int size, bool big_endian>
1908 void
1909 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
1910                         Symbol_table* symtab,
1911                         Layout* layout,
1912                         Sized_relobj<size, big_endian>* object,
1913                         unsigned int data_shndx,
1914                         unsigned int sh_type,
1915                         const unsigned char* prelocs,
1916                         size_t reloc_count,
1917                         Output_section* output_section,
1918                         bool needs_special_offset_handling,
1919                         size_t local_symbol_count,
1920                         const unsigned char* plocal_symbols,
1921                         Relocatable_relocs* rr)
1922 {
1923   gold_assert(sh_type == elfcpp::SHT_RELA);
1924
1925   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
1926     Relocatable_size_for_reloc> Scan_relocatable_relocs;
1927
1928   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
1929       Scan_relocatable_relocs>(
1930     symtab,
1931     layout,
1932     object,
1933     data_shndx,
1934     prelocs,
1935     reloc_count,
1936     output_section,
1937     needs_special_offset_handling,
1938     local_symbol_count,
1939     plocal_symbols,
1940     rr);
1941 }
1942
1943 // Relocate a section during a relocatable link.
1944
1945 template<int size, bool big_endian>
1946 void
1947 Target_powerpc<size, big_endian>::relocate_for_relocatable(
1948     const Relocate_info<size, big_endian>* relinfo,
1949     unsigned int sh_type,
1950     const unsigned char* prelocs,
1951     size_t reloc_count,
1952     Output_section* output_section,
1953     off_t offset_in_output_section,
1954     const Relocatable_relocs* rr,
1955     unsigned char* view,
1956     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
1957     section_size_type view_size,
1958     unsigned char* reloc_view,
1959     section_size_type reloc_view_size)
1960 {
1961   gold_assert(sh_type == elfcpp::SHT_RELA);
1962
1963   gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
1964     relinfo,
1965     prelocs,
1966     reloc_count,
1967     output_section,
1968     offset_in_output_section,
1969     rr,
1970     view,
1971     view_address,
1972     view_size,
1973     reloc_view,
1974     reloc_view_size);
1975 }
1976
1977 // Return the value to use for a dynamic which requires special
1978 // treatment.  This is how we support equality comparisons of function
1979 // pointers across shared library boundaries, as described in the
1980 // processor specific ABI supplement.
1981
1982 template<int size, bool big_endian>
1983 uint64_t
1984 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
1985 {
1986   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1987   return this->plt_section()->address() + gsym->plt_offset();
1988 }
1989
1990 // The selector for powerpc object files.
1991
1992 template<int size, bool big_endian>
1993 class Target_selector_powerpc : public Target_selector
1994 {
1995 public:
1996   Target_selector_powerpc()
1997     : Target_selector(elfcpp::EM_NONE, size, big_endian,
1998                       (size == 64 ?
1999                        (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
2000                        (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
2001   { }
2002
2003   Target* do_recognize(int machine, int, int)
2004   {
2005     switch (size)
2006       {
2007       case 64:
2008         if (machine != elfcpp::EM_PPC64)
2009           return NULL;
2010         break;
2011
2012       case 32:
2013         if (machine != elfcpp::EM_PPC)
2014           return NULL;
2015         break;
2016
2017       default:
2018         return NULL;
2019       }
2020
2021     return this->instantiate_target();
2022   }
2023
2024   Target* do_instantiate_target()
2025   { return new Target_powerpc<size, big_endian>(); }
2026 };
2027
2028 Target_selector_powerpc<32, true> target_selector_ppc32;
2029 Target_selector_powerpc<32, false> target_selector_ppc32le;
2030 Target_selector_powerpc<64, true> target_selector_ppc64;
2031 Target_selector_powerpc<64, false> target_selector_ppc64le;
2032
2033 } // End anonymous namespace.