* incremental-dump.cc (dump_incremental_inputs): Print dynamic reloc
[platform/upstream/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_file<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_file<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_file<size, big_endian>* object,
120                           unsigned int data_shndx,
121                           unsigned int sh_type,
122                           const unsigned char* prelocs,
123                           size_t reloc_count,
124                           Output_section* output_section,
125                           bool needs_special_offset_handling,
126                           size_t local_symbol_count,
127                           const unsigned char* plocal_symbols,
128                           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_file<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_file<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_file<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_file<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_file<size, big_endian>*,
235                             unsigned int r_type);
236
237     static void
238     unsupported_reloc_global(Sized_relobj_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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_file<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(
1072     Symbol_table* symtab,
1073     Layout* layout,
1074     Sized_relobj_file<size, big_endian>* object)
1075 {
1076   if (this->got_mod_index_offset_ == -1U)
1077     {
1078       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1079       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1080       Output_data_got<size, big_endian>* got;
1081       unsigned int got_offset;
1082
1083       got = this->got_section(symtab, layout);
1084       got_offset = got->add_constant(0);
1085       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1086                           got_offset, 0);
1087       got->add_constant(0);
1088       this->got_mod_index_offset_ = got_offset;
1089     }
1090   return this->got_mod_index_offset_;
1091 }
1092
1093 // Optimize the TLS relocation type based on what we know about the
1094 // symbol.  IS_FINAL is true if the final address of this symbol is
1095 // known at link time.
1096
1097 static tls::Tls_optimization
1098 optimize_tls_reloc(bool /* is_final */, int r_type)
1099 {
1100   // If we are generating a shared library, then we can't do anything
1101   // in the linker.
1102   if (parameters->options().shared())
1103     return tls::TLSOPT_NONE;
1104   switch (r_type)
1105     {
1106       // XXX
1107     default:
1108       gold_unreachable();
1109     }
1110 }
1111
1112 // Get the Reference_flags for a particular relocation.
1113
1114 template<int size, bool big_endian>
1115 int
1116 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
1117                         unsigned int r_type)
1118 {
1119   switch (r_type)
1120     {
1121     case elfcpp::R_POWERPC_NONE:
1122     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1123     case elfcpp::R_POWERPC_GNU_VTENTRY:
1124     case elfcpp::R_PPC64_TOC:
1125       // No symbol reference.
1126       return 0;
1127
1128     case elfcpp::R_POWERPC_ADDR16:
1129     case elfcpp::R_POWERPC_ADDR16_LO:
1130     case elfcpp::R_POWERPC_ADDR16_HI:
1131     case elfcpp::R_POWERPC_ADDR16_HA:
1132     case elfcpp::R_POWERPC_ADDR32:
1133     case elfcpp::R_PPC64_ADDR64:
1134       return Symbol::ABSOLUTE_REF;
1135
1136     case elfcpp::R_POWERPC_REL24:
1137     case elfcpp::R_PPC_LOCAL24PC:
1138     case elfcpp::R_PPC_REL16:
1139     case elfcpp::R_PPC_REL16_LO:
1140     case elfcpp::R_PPC_REL16_HI:
1141     case elfcpp::R_PPC_REL16_HA:
1142       return Symbol::RELATIVE_REF;
1143
1144     case elfcpp::R_PPC_PLTREL24:
1145       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1146
1147     case elfcpp::R_POWERPC_GOT16:
1148     case elfcpp::R_POWERPC_GOT16_LO:
1149     case elfcpp::R_POWERPC_GOT16_HI:
1150     case elfcpp::R_POWERPC_GOT16_HA:
1151     case elfcpp::R_PPC64_TOC16:
1152     case elfcpp::R_PPC64_TOC16_LO:
1153     case elfcpp::R_PPC64_TOC16_HI:
1154     case elfcpp::R_PPC64_TOC16_HA:
1155     case elfcpp::R_PPC64_TOC16_DS:
1156     case elfcpp::R_PPC64_TOC16_LO_DS:
1157       // Absolute in GOT.
1158       return Symbol::ABSOLUTE_REF;
1159
1160     case elfcpp::R_POWERPC_GOT_TPREL16:
1161     case elfcpp::R_POWERPC_TLS:
1162       return Symbol::TLS_REF;
1163
1164     case elfcpp::R_POWERPC_COPY:
1165     case elfcpp::R_POWERPC_GLOB_DAT:
1166     case elfcpp::R_POWERPC_JMP_SLOT:
1167     case elfcpp::R_POWERPC_RELATIVE:
1168     case elfcpp::R_POWERPC_DTPMOD:
1169     default:
1170       // Not expected.  We will give an error later.
1171       return 0;
1172     }
1173 }
1174
1175 // Report an unsupported relocation against a local symbol.
1176
1177 template<int size, bool big_endian>
1178 void
1179 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1180                         Sized_relobj_file<size, big_endian>* object,
1181                         unsigned int r_type)
1182 {
1183   gold_error(_("%s: unsupported reloc %u against local symbol"),
1184              object->name().c_str(), r_type);
1185 }
1186
1187 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1188 // dynamic linker does not support it, issue an error.
1189
1190 template<int size, bool big_endian>
1191 void
1192 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1193                                                       unsigned int r_type)
1194 {
1195   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1196
1197   // These are the relocation types supported by glibc for both 32-bit
1198   // and 64-bit powerpc.
1199   switch (r_type)
1200     {
1201     case elfcpp::R_POWERPC_RELATIVE:
1202     case elfcpp::R_POWERPC_GLOB_DAT:
1203     case elfcpp::R_POWERPC_DTPMOD:
1204     case elfcpp::R_POWERPC_DTPREL:
1205     case elfcpp::R_POWERPC_TPREL:
1206     case elfcpp::R_POWERPC_JMP_SLOT:
1207     case elfcpp::R_POWERPC_COPY:
1208     case elfcpp::R_POWERPC_ADDR32:
1209     case elfcpp::R_POWERPC_ADDR24:
1210     case elfcpp::R_POWERPC_REL24:
1211       return;
1212
1213     default:
1214       break;
1215     }
1216
1217   if (size == 64)
1218     {
1219       switch (r_type)
1220         {
1221           // These are the relocation types supported only on 64-bit.
1222         case elfcpp::R_PPC64_ADDR64:
1223         case elfcpp::R_PPC64_TPREL16_LO_DS:
1224         case elfcpp::R_PPC64_TPREL16_DS:
1225         case elfcpp::R_POWERPC_TPREL16:
1226         case elfcpp::R_POWERPC_TPREL16_LO:
1227         case elfcpp::R_POWERPC_TPREL16_HI:
1228         case elfcpp::R_POWERPC_TPREL16_HA:
1229         case elfcpp::R_PPC64_TPREL16_HIGHER:
1230         case elfcpp::R_PPC64_TPREL16_HIGHEST:
1231         case elfcpp::R_PPC64_TPREL16_HIGHERA:
1232         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1233         case elfcpp::R_PPC64_ADDR16_LO_DS:
1234         case elfcpp::R_POWERPC_ADDR16_LO:
1235         case elfcpp::R_POWERPC_ADDR16_HI:
1236         case elfcpp::R_POWERPC_ADDR16_HA:
1237         case elfcpp::R_POWERPC_ADDR30:
1238         case elfcpp::R_PPC64_UADDR64:
1239         case elfcpp::R_POWERPC_UADDR32:
1240         case elfcpp::R_POWERPC_ADDR16:
1241         case elfcpp::R_POWERPC_UADDR16:
1242         case elfcpp::R_PPC64_ADDR16_DS:
1243         case elfcpp::R_PPC64_ADDR16_HIGHER:
1244         case elfcpp::R_PPC64_ADDR16_HIGHEST:
1245         case elfcpp::R_PPC64_ADDR16_HIGHERA:
1246         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1247         case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1248         case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1249         case elfcpp::R_POWERPC_REL32:
1250         case elfcpp::R_PPC64_REL64:
1251           return;
1252
1253         default:
1254           break;
1255         }
1256     }
1257   else
1258     {
1259       switch (r_type)
1260         {
1261           // These are the relocation types supported only on 32-bit.
1262
1263         default:
1264           break;
1265         }
1266     }
1267
1268   // This prevents us from issuing more than one error per reloc
1269   // section.  But we can still wind up issuing more than one
1270   // error per object file.
1271   if (this->issued_non_pic_error_)
1272     return;
1273   gold_assert(parameters->options().output_is_position_independent());
1274   object->error(_("requires unsupported dynamic reloc; "
1275                   "recompile with -fPIC"));
1276   this->issued_non_pic_error_ = true;
1277   return;
1278 }
1279
1280 // Scan a relocation for a local symbol.
1281
1282 template<int size, bool big_endian>
1283 inline void
1284 Target_powerpc<size, big_endian>::Scan::local(
1285                         Symbol_table* symtab,
1286                         Layout* layout,
1287                         Target_powerpc<size, big_endian>* target,
1288                         Sized_relobj_file<size, big_endian>* object,
1289                         unsigned int data_shndx,
1290                         Output_section* output_section,
1291                         const elfcpp::Rela<size, big_endian>& reloc,
1292                         unsigned int r_type,
1293                         const elfcpp::Sym<size, big_endian>& lsym)
1294 {
1295   switch (r_type)
1296     {
1297     case elfcpp::R_POWERPC_NONE:
1298     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1299     case elfcpp::R_POWERPC_GNU_VTENTRY:
1300       break;
1301
1302     case elfcpp::R_PPC64_ADDR64:
1303     case elfcpp::R_POWERPC_ADDR32:
1304     case elfcpp::R_POWERPC_ADDR16_HA:
1305     case elfcpp::R_POWERPC_ADDR16_LO:
1306       // If building a shared library (or a position-independent
1307       // executable), we need to create a dynamic relocation for
1308       // this location.
1309       if (parameters->options().output_is_position_independent())
1310         {
1311           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1312
1313           check_non_pic(object, r_type);
1314           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1315             {
1316               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1317               rela_dyn->add_local(object, r_sym, r_type, output_section,
1318                                   data_shndx, reloc.get_r_offset(),
1319                                   reloc.get_r_addend());
1320             }
1321           else
1322             {
1323               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1324               gold_assert(lsym.get_st_value() == 0);
1325               rela_dyn->add_local_relative(object, r_sym, r_type,
1326                                            output_section, data_shndx,
1327                                            reloc.get_r_offset(),
1328                                            reloc.get_r_addend());
1329             }
1330         }
1331       break;
1332
1333     case elfcpp::R_POWERPC_REL24:
1334     case elfcpp::R_PPC_LOCAL24PC:
1335     case elfcpp::R_POWERPC_REL32:
1336     case elfcpp::R_PPC_REL16_LO:
1337     case elfcpp::R_PPC_REL16_HA:
1338       break;
1339
1340     case elfcpp::R_POWERPC_GOT16:
1341     case elfcpp::R_POWERPC_GOT16_LO:
1342     case elfcpp::R_POWERPC_GOT16_HI:
1343     case elfcpp::R_POWERPC_GOT16_HA:
1344     case elfcpp::R_PPC64_TOC16:
1345     case elfcpp::R_PPC64_TOC16_LO:
1346     case elfcpp::R_PPC64_TOC16_HI:
1347     case elfcpp::R_PPC64_TOC16_HA:
1348     case elfcpp::R_PPC64_TOC16_DS:
1349     case elfcpp::R_PPC64_TOC16_LO_DS:
1350       {
1351         // The symbol requires a GOT entry.
1352         Output_data_got<size, big_endian>* got;
1353         unsigned int r_sym;
1354
1355         got = target->got_section(symtab, layout);
1356         r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1357
1358         // If we are generating a shared object, we need to add a
1359         // dynamic relocation for this symbol's GOT entry.
1360         if (parameters->options().output_is_position_independent())
1361           {
1362             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1363               {
1364                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1365                 unsigned int off;
1366
1367                 off = got->add_constant(0);
1368                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1369                 rela_dyn->add_local_relative(object, r_sym,
1370                                              elfcpp::R_POWERPC_RELATIVE,
1371                                              got, off, 0);
1372               }
1373           }
1374         else
1375           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1376       }
1377       break;
1378
1379     case elfcpp::R_PPC64_TOC:
1380       // We need a GOT section.
1381       target->got_section(symtab, layout);
1382       break;
1383
1384       // These are relocations which should only be seen by the
1385       // dynamic linker, and should never be seen here.
1386     case elfcpp::R_POWERPC_COPY:
1387     case elfcpp::R_POWERPC_GLOB_DAT:
1388     case elfcpp::R_POWERPC_JMP_SLOT:
1389     case elfcpp::R_POWERPC_RELATIVE:
1390     case elfcpp::R_POWERPC_DTPMOD:
1391       gold_error(_("%s: unexpected reloc %u in object file"),
1392                  object->name().c_str(), r_type);
1393       break;
1394
1395     default:
1396       unsupported_reloc_local(object, r_type);
1397       break;
1398     }
1399 }
1400
1401 // Report an unsupported relocation against a global symbol.
1402
1403 template<int size, bool big_endian>
1404 void
1405 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1406                         Sized_relobj_file<size, big_endian>* object,
1407                         unsigned int r_type,
1408                         Symbol* gsym)
1409 {
1410   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1411              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1412 }
1413
1414 // Scan a relocation for a global symbol.
1415
1416 template<int size, bool big_endian>
1417 inline void
1418 Target_powerpc<size, big_endian>::Scan::global(
1419                                 Symbol_table* symtab,
1420                                 Layout* layout,
1421                                 Target_powerpc<size, big_endian>* target,
1422                                 Sized_relobj_file<size, big_endian>* object,
1423                                 unsigned int data_shndx,
1424                                 Output_section* output_section,
1425                                 const elfcpp::Rela<size, big_endian>& reloc,
1426                                 unsigned int r_type,
1427                                 Symbol* gsym)
1428 {
1429   switch (r_type)
1430     {
1431     case elfcpp::R_POWERPC_NONE:
1432     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1433     case elfcpp::R_POWERPC_GNU_VTENTRY:
1434       break;
1435
1436     case elfcpp::R_PPC_PLTREL24:
1437       // If the symbol is fully resolved, this is just a PC32 reloc.
1438       // Otherwise we need a PLT entry.
1439       if (gsym->final_value_is_known())
1440         break;
1441       // If building a shared library, we can also skip the PLT entry
1442       // if the symbol is defined in the output file and is protected
1443       // or hidden.
1444       if (gsym->is_defined()
1445           && !gsym->is_from_dynobj()
1446           && !gsym->is_preemptible())
1447         break;
1448       target->make_plt_entry(symtab, layout, gsym);
1449       break;
1450
1451     case elfcpp::R_POWERPC_ADDR16:
1452     case elfcpp::R_POWERPC_ADDR16_LO:
1453     case elfcpp::R_POWERPC_ADDR16_HI:
1454     case elfcpp::R_POWERPC_ADDR16_HA:
1455     case elfcpp::R_POWERPC_ADDR32:
1456     case elfcpp::R_PPC64_ADDR64:
1457       {
1458         // Make a PLT entry if necessary.
1459         if (gsym->needs_plt_entry())
1460           {
1461             target->make_plt_entry(symtab, layout, gsym);
1462             // Since this is not a PC-relative relocation, we may be
1463             // taking the address of a function. In that case we need to
1464             // set the entry in the dynamic symbol table to the address of
1465             // the PLT entry.
1466             if (gsym->is_from_dynobj() && !parameters->options().shared())
1467               gsym->set_needs_dynsym_value();
1468           }
1469         // Make a dynamic relocation if necessary.
1470         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1471           {
1472             if (gsym->may_need_copy_reloc())
1473               {
1474                 target->copy_reloc(symtab, layout, object,
1475                                    data_shndx, output_section, gsym, reloc);
1476               }
1477             else if ((r_type == elfcpp::R_POWERPC_ADDR32
1478                       || r_type == elfcpp::R_PPC64_ADDR64)
1479                      && gsym->can_use_relative_reloc(false))
1480               {
1481                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1482                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1483                                               output_section, object,
1484                                               data_shndx, reloc.get_r_offset(),
1485                                               reloc.get_r_addend());
1486               }
1487             else
1488               {
1489                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1490
1491                 check_non_pic(object, r_type);
1492                 if (gsym->is_from_dynobj()
1493                     || gsym->is_undefined()
1494                     || gsym->is_preemptible())
1495                   rela_dyn->add_global(gsym, r_type, output_section,
1496                                        object, data_shndx,
1497                                        reloc.get_r_offset(),
1498                                        reloc.get_r_addend());
1499                 else
1500                   rela_dyn->add_global_relative(gsym, r_type,
1501                                                 output_section, object,
1502                                                 data_shndx,
1503                                                 reloc.get_r_offset(),
1504                                                 reloc.get_r_addend());
1505               }
1506           }
1507       }
1508       break;
1509
1510     case elfcpp::R_POWERPC_REL24:
1511     case elfcpp::R_PPC_LOCAL24PC:
1512     case elfcpp::R_PPC_REL16:
1513     case elfcpp::R_PPC_REL16_LO:
1514     case elfcpp::R_PPC_REL16_HI:
1515     case elfcpp::R_PPC_REL16_HA:
1516       {
1517         if (gsym->needs_plt_entry())
1518           target->make_plt_entry(symtab, layout, gsym);
1519         // Make a dynamic relocation if necessary.
1520         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1521           {
1522             if (gsym->may_need_copy_reloc())
1523               {
1524                 target->copy_reloc(symtab, layout, object,
1525                                    data_shndx, output_section, gsym,
1526                                    reloc);
1527               }
1528             else
1529               {
1530                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1531                 check_non_pic(object, r_type);
1532                 rela_dyn->add_global(gsym, r_type, output_section, object,
1533                                      data_shndx, reloc.get_r_offset(),
1534                                      reloc.get_r_addend());
1535               }
1536           }
1537       }
1538       break;
1539
1540     case elfcpp::R_POWERPC_GOT16:
1541     case elfcpp::R_POWERPC_GOT16_LO:
1542     case elfcpp::R_POWERPC_GOT16_HI:
1543     case elfcpp::R_POWERPC_GOT16_HA:
1544     case elfcpp::R_PPC64_TOC16:
1545     case elfcpp::R_PPC64_TOC16_LO:
1546     case elfcpp::R_PPC64_TOC16_HI:
1547     case elfcpp::R_PPC64_TOC16_HA:
1548     case elfcpp::R_PPC64_TOC16_DS:
1549     case elfcpp::R_PPC64_TOC16_LO_DS:
1550       {
1551         // The symbol requires a GOT entry.
1552         Output_data_got<size, big_endian>* got;
1553
1554         got = target->got_section(symtab, layout);
1555         if (gsym->final_value_is_known())
1556           got->add_global(gsym, GOT_TYPE_STANDARD);
1557         else
1558           {
1559             // If this symbol is not fully resolved, we need to add a
1560             // dynamic relocation for it.
1561             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1562             if (gsym->is_from_dynobj()
1563                 || gsym->is_undefined()
1564                 || gsym->is_preemptible())
1565               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1566                                         elfcpp::R_POWERPC_GLOB_DAT);
1567             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1568               {
1569                 unsigned int off = got->add_constant(0);
1570
1571                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1572                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1573                                               got, off, 0);
1574               }
1575           }
1576       }
1577       break;
1578
1579     case elfcpp::R_PPC64_TOC:
1580       // We need a GOT section.
1581       target->got_section(symtab, layout);
1582       break;
1583
1584     case elfcpp::R_POWERPC_GOT_TPREL16:
1585     case elfcpp::R_POWERPC_TLS:
1586       // XXX TLS
1587       break;
1588
1589       // These are relocations which should only be seen by the
1590       // dynamic linker, and should never be seen here.
1591     case elfcpp::R_POWERPC_COPY:
1592     case elfcpp::R_POWERPC_GLOB_DAT:
1593     case elfcpp::R_POWERPC_JMP_SLOT:
1594     case elfcpp::R_POWERPC_RELATIVE:
1595     case elfcpp::R_POWERPC_DTPMOD:
1596       gold_error(_("%s: unexpected reloc %u in object file"),
1597                  object->name().c_str(), r_type);
1598       break;
1599
1600     default:
1601       unsupported_reloc_global(object, r_type, gsym);
1602       break;
1603     }
1604 }
1605
1606 // Process relocations for gc.
1607
1608 template<int size, bool big_endian>
1609 void
1610 Target_powerpc<size, big_endian>::gc_process_relocs(
1611                         Symbol_table* symtab,
1612                         Layout* layout,
1613                         Sized_relobj_file<size, big_endian>* object,
1614                         unsigned int data_shndx,
1615                         unsigned int,
1616                         const unsigned char* prelocs,
1617                         size_t reloc_count,
1618                         Output_section* output_section,
1619                         bool needs_special_offset_handling,
1620                         size_t local_symbol_count,
1621                         const unsigned char* plocal_symbols)
1622 {
1623   typedef Target_powerpc<size, big_endian> Powerpc;
1624   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1625
1626   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
1627                           typename Target_powerpc::Relocatable_size_for_reloc>(
1628     symtab,
1629     layout,
1630     this,
1631     object,
1632     data_shndx,
1633     prelocs,
1634     reloc_count,
1635     output_section,
1636     needs_special_offset_handling,
1637     local_symbol_count,
1638     plocal_symbols);
1639 }
1640
1641 // Scan relocations for a section.
1642
1643 template<int size, bool big_endian>
1644 void
1645 Target_powerpc<size, big_endian>::scan_relocs(
1646                         Symbol_table* symtab,
1647                         Layout* layout,
1648                         Sized_relobj_file<size, big_endian>* object,
1649                         unsigned int data_shndx,
1650                         unsigned int sh_type,
1651                         const unsigned char* prelocs,
1652                         size_t reloc_count,
1653                         Output_section* output_section,
1654                         bool needs_special_offset_handling,
1655                         size_t local_symbol_count,
1656                         const unsigned char* plocal_symbols)
1657 {
1658   typedef Target_powerpc<size, big_endian> Powerpc;
1659   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
1660   static Output_data_space* sdata;
1661
1662   if (sh_type == elfcpp::SHT_REL)
1663     {
1664       gold_error(_("%s: unsupported REL reloc section"),
1665                  object->name().c_str());
1666       return;
1667     }
1668
1669   // Define _SDA_BASE_ at the start of the .sdata section.
1670   if (sdata == NULL)
1671   {
1672     // layout->find_output_section(".sdata") == NULL
1673     sdata = new Output_data_space(4, "** sdata");
1674     Output_section* os = layout->add_output_section_data(".sdata", 0,
1675                                                          elfcpp::SHF_ALLOC
1676                                                          | elfcpp::SHF_WRITE,
1677                                                          sdata,
1678                                                          ORDER_SMALL_DATA,
1679                                                          false);
1680     symtab->define_in_output_data("_SDA_BASE_", NULL,
1681                                   Symbol_table::PREDEFINED,
1682                                   os,
1683                                   32768, 0,
1684                                   elfcpp::STT_OBJECT,
1685                                   elfcpp::STB_LOCAL,
1686                                   elfcpp::STV_HIDDEN, 0,
1687                                   false, false);
1688   }
1689
1690   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
1691     symtab,
1692     layout,
1693     this,
1694     object,
1695     data_shndx,
1696     prelocs,
1697     reloc_count,
1698     output_section,
1699     needs_special_offset_handling,
1700     local_symbol_count,
1701     plocal_symbols);
1702 }
1703
1704 // Finalize the sections.
1705
1706 template<int size, bool big_endian>
1707 void
1708 Target_powerpc<size, big_endian>::do_finalize_sections(
1709     Layout* layout,
1710     const Input_objects*,
1711     Symbol_table*)
1712 {
1713   // Fill in some more dynamic tags.
1714   const Reloc_section* rel_plt = (this->plt_ == NULL
1715                                   ? NULL
1716                                   : this->plt_->rel_plt());
1717   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
1718                                   this->rela_dyn_, true, size == 32);
1719
1720   // Emit any relocs we saved in an attempt to avoid generating COPY
1721   // relocs.
1722   if (this->copy_relocs_.any_saved_relocs())
1723     this->copy_relocs_.emit(this->rela_dyn_section(layout));
1724 }
1725
1726 // Perform a relocation.
1727
1728 template<int size, bool big_endian>
1729 inline bool
1730 Target_powerpc<size, big_endian>::Relocate::relocate(
1731                         const Relocate_info<size, big_endian>* relinfo,
1732                         Target_powerpc* target,
1733                         Output_section*,
1734                         size_t relnum,
1735                         const elfcpp::Rela<size, big_endian>& rela,
1736                         unsigned int r_type,
1737                         const Sized_symbol<size>* gsym,
1738                         const Symbol_value<size>* psymval,
1739                         unsigned char* view,
1740                         typename elfcpp::Elf_types<size>::Elf_Addr address,
1741                         section_size_type /* view_size */)
1742 {
1743   const unsigned int toc_base_offset = 0x8000;
1744   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1745
1746   // Pick the value to use for symbols defined in shared objects.
1747   Symbol_value<size> symval;
1748   if (gsym != NULL
1749       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
1750     {
1751       elfcpp::Elf_Xword value;
1752
1753       value = target->plt_section()->address() + gsym->plt_offset();
1754
1755       symval.set_output_value(value);
1756
1757       psymval = &symval;
1758     }
1759
1760   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1761   elfcpp::Elf_Xword addend = rela.get_r_addend();
1762
1763   // Get the GOT offset if needed.  Unlike i386 and x86_64, our GOT
1764   // pointer points to the beginning, not the end, of the table.
1765   // So we just use the plain offset.
1766   unsigned int got_offset = 0;
1767   unsigned int got2_offset = 0;
1768   switch (r_type)
1769     {
1770     case elfcpp::R_PPC64_TOC16:
1771     case elfcpp::R_PPC64_TOC16_LO:
1772     case elfcpp::R_PPC64_TOC16_HI:
1773     case elfcpp::R_PPC64_TOC16_HA:
1774     case elfcpp::R_PPC64_TOC16_DS:
1775     case elfcpp::R_PPC64_TOC16_LO_DS:
1776         // Subtract the TOC base address.
1777         addend -= target->toc_section()->address() + toc_base_offset;
1778         /* FALLTHRU */
1779
1780     case elfcpp::R_POWERPC_GOT16:
1781     case elfcpp::R_POWERPC_GOT16_LO:
1782     case elfcpp::R_POWERPC_GOT16_HI:
1783     case elfcpp::R_POWERPC_GOT16_HA:
1784     case elfcpp::R_PPC64_GOT16_DS:
1785     case elfcpp::R_PPC64_GOT16_LO_DS:
1786       if (gsym != NULL)
1787         {
1788           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1789           got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
1790         }
1791       else
1792         {
1793           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
1794           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1795           got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1796         }
1797       break;
1798
1799       // R_PPC_PLTREL24 is rather special.  If non-zero,
1800       // the addend specifies the GOT pointer offset within .got2.  
1801     case elfcpp::R_PPC_PLTREL24:
1802       if (addend >= 32768)
1803         {
1804           Output_data_space* got2;
1805           got2 = target->got2_section();
1806           got2_offset = got2->offset();
1807           addend += got2_offset;
1808         }
1809       break;
1810
1811     default:
1812       break;
1813     }
1814
1815   switch (r_type)
1816     {
1817     case elfcpp::R_POWERPC_NONE:
1818     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1819     case elfcpp::R_POWERPC_GNU_VTENTRY:
1820       break;
1821
1822     case elfcpp::R_POWERPC_REL32:
1823       Reloc::rel32(view, object, psymval, addend, address);
1824       break;
1825
1826     case elfcpp::R_POWERPC_REL24:
1827       Reloc::rel24(view, object, psymval, addend, address);
1828       break;
1829
1830     case elfcpp::R_POWERPC_REL14:
1831       Reloc::rel14(view, object, psymval, addend, address);
1832       break;
1833
1834     case elfcpp::R_PPC_PLTREL24:
1835       Reloc::rel24(view, object, psymval, addend, address);
1836       break;
1837
1838     case elfcpp::R_PPC_LOCAL24PC:
1839       Reloc::rel24(view, object, psymval, addend, address);
1840       break;
1841
1842     case elfcpp::R_PPC64_ADDR64:
1843       if (!parameters->options().output_is_position_independent())
1844         Relocate_functions<size, big_endian>::rela64(view, object,
1845                                                      psymval, addend);
1846       break;
1847
1848     case elfcpp::R_POWERPC_ADDR32:
1849       if (!parameters->options().output_is_position_independent())
1850         Relocate_functions<size, big_endian>::rela32(view, object,
1851                                                      psymval, addend);
1852       break;
1853
1854     case elfcpp::R_POWERPC_ADDR16_LO:
1855       Reloc::addr16_lo(view, object, psymval, addend);
1856       break;
1857
1858     case elfcpp::R_POWERPC_ADDR16_HI:
1859       Reloc::addr16_hi(view, object, psymval, addend);
1860       break;
1861
1862     case elfcpp::R_POWERPC_ADDR16_HA:
1863       Reloc::addr16_ha(view, object, psymval, addend);
1864       break;
1865
1866     case elfcpp::R_PPC_REL16_LO:
1867       Reloc::rel16_lo(view, object, psymval, addend, address);
1868       break;
1869
1870     case elfcpp::R_PPC_REL16_HI:
1871       Reloc::rel16_lo(view, object, psymval, addend, address);
1872       break;
1873
1874     case elfcpp::R_PPC_REL16_HA:
1875       Reloc::rel16_ha(view, object, psymval, addend, address);
1876       break;
1877
1878     case elfcpp::R_POWERPC_GOT16:
1879       Reloc::addr16(view, got_offset, addend);
1880       break;
1881
1882     case elfcpp::R_POWERPC_GOT16_LO:
1883       Reloc::addr16_lo(view, got_offset, addend);
1884       break;
1885
1886     case elfcpp::R_POWERPC_GOT16_HI:
1887       Reloc::addr16_hi(view, got_offset, addend);
1888       break;
1889
1890     case elfcpp::R_POWERPC_GOT16_HA:
1891       Reloc::addr16_ha(view, got_offset, addend);
1892       break;
1893
1894     case elfcpp::R_PPC64_TOC16:
1895       Reloc::addr16(view, got_offset, addend);
1896       break;
1897
1898     case elfcpp::R_PPC64_TOC16_LO:
1899       Reloc::addr16_lo(view, got_offset, addend);
1900       break;
1901
1902     case elfcpp::R_PPC64_TOC16_HI:
1903       Reloc::addr16_hi(view, got_offset, addend);
1904       break;
1905
1906     case elfcpp::R_PPC64_TOC16_HA:
1907       Reloc::addr16_ha(view, got_offset, addend);
1908       break;
1909
1910     case elfcpp::R_PPC64_TOC16_DS:
1911     case elfcpp::R_PPC64_TOC16_LO_DS:
1912       Reloc::addr16_ds(view, got_offset, addend);
1913       break;
1914
1915     case elfcpp::R_PPC64_TOC:
1916       {
1917         elfcpp::Elf_types<64>::Elf_Addr value;
1918         value = target->toc_section()->address() + toc_base_offset;
1919         Relocate_functions<64, false>::rela64(view, value, addend);
1920       }
1921       break;
1922
1923     case elfcpp::R_POWERPC_COPY:
1924     case elfcpp::R_POWERPC_GLOB_DAT:
1925     case elfcpp::R_POWERPC_JMP_SLOT:
1926     case elfcpp::R_POWERPC_RELATIVE:
1927       // This is an outstanding tls reloc, which is unexpected when
1928       // linking.
1929     case elfcpp::R_POWERPC_DTPMOD:
1930       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1931                              _("unexpected reloc %u in object file"),
1932                              r_type);
1933       break;
1934
1935     default:
1936       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1937                              _("unsupported reloc %u"),
1938                              r_type);
1939       break;
1940     }
1941
1942   return true;
1943 }
1944
1945 // Perform a TLS relocation.
1946
1947 template<int size, bool big_endian>
1948 inline void
1949 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
1950                         const Relocate_info<size, big_endian>* relinfo,
1951                         Target_powerpc<size, big_endian>* target,
1952                         size_t relnum,
1953                         const elfcpp::Rela<size, big_endian>& rela,
1954                         unsigned int r_type,
1955                         const Sized_symbol<size>* gsym,
1956                         const Symbol_value<size>* psymval,
1957                         unsigned char* view,
1958                         typename elfcpp::Elf_types<size>::Elf_Addr address,
1959                         section_size_type)
1960 {
1961   Output_segment* tls_segment = relinfo->layout->tls_segment();
1962   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
1963   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
1964
1965   const elfcpp::Elf_Xword addend = rela.get_r_addend();
1966   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
1967
1968   const bool is_final =
1969     (gsym == NULL
1970      ? !parameters->options().output_is_position_independent()
1971      : gsym->final_value_is_known());
1972   const tls::Tls_optimization optimized_type
1973       = optimize_tls_reloc(is_final, r_type);
1974
1975   switch (r_type)
1976     {
1977       // XXX
1978     }
1979 }
1980
1981 // Relocate section data.
1982
1983 template<int size, bool big_endian>
1984 void
1985 Target_powerpc<size, big_endian>::relocate_section(
1986                         const Relocate_info<size, big_endian>* relinfo,
1987                         unsigned int sh_type,
1988                         const unsigned char* prelocs,
1989                         size_t reloc_count,
1990                         Output_section* output_section,
1991                         bool needs_special_offset_handling,
1992                         unsigned char* view,
1993                         typename elfcpp::Elf_types<size>::Elf_Addr address,
1994                         section_size_type view_size,
1995                         const Reloc_symbol_changes* reloc_symbol_changes)
1996 {
1997   typedef Target_powerpc<size, big_endian> Powerpc;
1998   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
1999
2000   gold_assert(sh_type == elfcpp::SHT_RELA);
2001
2002   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2003     Powerpc_relocate>(
2004     relinfo,
2005     this,
2006     prelocs,
2007     reloc_count,
2008     output_section,
2009     needs_special_offset_handling,
2010     view,
2011     address,
2012     view_size,
2013     reloc_symbol_changes);
2014 }
2015
2016 // Return the size of a relocation while scanning during a relocatable
2017 // link.
2018
2019 template<int size, bool big_endian>
2020 unsigned int
2021 Target_powerpc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2022     unsigned int,
2023     Relobj*)
2024 {
2025   // We are always SHT_RELA, so we should never get here.
2026   gold_unreachable();
2027   return 0;
2028 }
2029
2030 // Scan the relocs during a relocatable link.
2031
2032 template<int size, bool big_endian>
2033 void
2034 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2035                         Symbol_table* symtab,
2036                         Layout* layout,
2037                         Sized_relobj_file<size, big_endian>* object,
2038                         unsigned int data_shndx,
2039                         unsigned int sh_type,
2040                         const unsigned char* prelocs,
2041                         size_t reloc_count,
2042                         Output_section* output_section,
2043                         bool needs_special_offset_handling,
2044                         size_t local_symbol_count,
2045                         const unsigned char* plocal_symbols,
2046                         Relocatable_relocs* rr)
2047 {
2048   gold_assert(sh_type == elfcpp::SHT_RELA);
2049
2050   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2051     Relocatable_size_for_reloc> Scan_relocatable_relocs;
2052
2053   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2054       Scan_relocatable_relocs>(
2055     symtab,
2056     layout,
2057     object,
2058     data_shndx,
2059     prelocs,
2060     reloc_count,
2061     output_section,
2062     needs_special_offset_handling,
2063     local_symbol_count,
2064     plocal_symbols,
2065     rr);
2066 }
2067
2068 // Relocate a section during a relocatable link.
2069
2070 template<int size, bool big_endian>
2071 void
2072 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2073     const Relocate_info<size, big_endian>* relinfo,
2074     unsigned int sh_type,
2075     const unsigned char* prelocs,
2076     size_t reloc_count,
2077     Output_section* output_section,
2078     off_t offset_in_output_section,
2079     const Relocatable_relocs* rr,
2080     unsigned char* view,
2081     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
2082     section_size_type view_size,
2083     unsigned char* reloc_view,
2084     section_size_type reloc_view_size)
2085 {
2086   gold_assert(sh_type == elfcpp::SHT_RELA);
2087
2088   gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
2089     relinfo,
2090     prelocs,
2091     reloc_count,
2092     output_section,
2093     offset_in_output_section,
2094     rr,
2095     view,
2096     view_address,
2097     view_size,
2098     reloc_view,
2099     reloc_view_size);
2100 }
2101
2102 // Return the value to use for a dynamic which requires special
2103 // treatment.  This is how we support equality comparisons of function
2104 // pointers across shared library boundaries, as described in the
2105 // processor specific ABI supplement.
2106
2107 template<int size, bool big_endian>
2108 uint64_t
2109 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2110 {
2111   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2112   return this->plt_section()->address() + gsym->plt_offset();
2113 }
2114
2115 // The selector for powerpc object files.
2116
2117 template<int size, bool big_endian>
2118 class Target_selector_powerpc : public Target_selector
2119 {
2120 public:
2121   Target_selector_powerpc()
2122     : Target_selector(elfcpp::EM_NONE, size, big_endian,
2123                       (size == 64 ?
2124                        (big_endian ? "elf64-powerpc" : "elf64-powerpcle") :
2125                        (big_endian ? "elf32-powerpc" : "elf32-powerpcle")))
2126   { }
2127
2128   Target* do_recognize(int machine, int, int)
2129   {
2130     switch (size)
2131       {
2132       case 64:
2133         if (machine != elfcpp::EM_PPC64)
2134           return NULL;
2135         break;
2136
2137       case 32:
2138         if (machine != elfcpp::EM_PPC)
2139           return NULL;
2140         break;
2141
2142       default:
2143         return NULL;
2144       }
2145
2146     return this->instantiate_target();
2147   }
2148
2149   Target* do_instantiate_target()
2150   { return new Target_powerpc<size, big_endian>(); }
2151 };
2152
2153 Target_selector_powerpc<32, true> target_selector_ppc32;
2154 Target_selector_powerpc<32, false> target_selector_ppc32le;
2155 Target_selector_powerpc<64, true> target_selector_ppc64;
2156 Target_selector_powerpc<64, false> target_selector_ppc64le;
2157
2158 } // End anonymous namespace.