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