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