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