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