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