* powerpc.cc (Powerpc_relobj::set_got2_shndx): Delete.
[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 Output_data_got_powerpc;
52
53 template<int size, bool big_endian>
54 class Output_data_glink;
55
56 template<int size, bool big_endian>
57 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
58 {
59 public:
60   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
61                  const typename elfcpp::Ehdr<size, big_endian>& ehdr)
62     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
63       got2_section_(0)
64   { }
65
66   ~Powerpc_relobj()
67   { }
68
69   unsigned int
70   got2_shndx() const
71   {
72     if (size == 32)
73       return this->got2_section_;
74     else
75       return 0;
76   }
77
78   bool
79   do_find_special_sections(Read_symbols_data* sd);
80
81 private:
82   unsigned int got2_section_;
83 };
84
85 template<int size, bool big_endian>
86 class Target_powerpc : public Sized_target<size, big_endian>
87 {
88  public:
89   typedef
90     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
91
92   Target_powerpc()
93     : Sized_target<size, big_endian>(&powerpc_info),
94       got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
95       copy_relocs_(elfcpp::R_POWERPC_COPY),
96       dynbss_(NULL), got_mod_index_offset_(-1U)
97   {
98   }
99
100   // Process the relocations to determine unreferenced sections for
101   // garbage collection.
102   void
103   gc_process_relocs(Symbol_table* symtab,
104                     Layout* layout,
105                     Sized_relobj_file<size, big_endian>* object,
106                     unsigned int data_shndx,
107                     unsigned int sh_type,
108                     const unsigned char* prelocs,
109                     size_t reloc_count,
110                     Output_section* output_section,
111                     bool needs_special_offset_handling,
112                     size_t local_symbol_count,
113                     const unsigned char* plocal_symbols);
114
115   // Scan the relocations to look for symbol adjustments.
116   void
117   scan_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   // Finalize the sections.
129   void
130   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
131
132   // Return the value to use for a dynamic which requires special
133   // treatment.
134   uint64_t
135   do_dynsym_value(const Symbol*) const;
136
137   // Relocate a section.
138   void
139   relocate_section(const Relocate_info<size, big_endian>*,
140                    unsigned int sh_type,
141                    const unsigned char* prelocs,
142                    size_t reloc_count,
143                    Output_section* output_section,
144                    bool needs_special_offset_handling,
145                    unsigned char* view,
146                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
147                    section_size_type view_size,
148                    const Reloc_symbol_changes*);
149
150   // Scan the relocs during a relocatable link.
151   void
152   scan_relocatable_relocs(Symbol_table* symtab,
153                           Layout* layout,
154                           Sized_relobj_file<size, big_endian>* object,
155                           unsigned int data_shndx,
156                           unsigned int sh_type,
157                           const unsigned char* prelocs,
158                           size_t reloc_count,
159                           Output_section* output_section,
160                           bool needs_special_offset_handling,
161                           size_t local_symbol_count,
162                           const unsigned char* plocal_symbols,
163                           Relocatable_relocs*);
164
165   // Relocate a section during a relocatable link.
166   void
167   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
168                            unsigned int sh_type,
169                            const unsigned char* prelocs,
170                            size_t reloc_count,
171                            Output_section* output_section,
172                            off_t offset_in_output_section,
173                            const Relocatable_relocs*,
174                            unsigned char*,
175                            typename elfcpp::Elf_types<size>::Elf_Addr,
176                            section_size_type,
177                            unsigned char* reloc_view,
178                            section_size_type reloc_view_size);
179
180   // Return whether SYM is defined by the ABI.
181   bool
182   do_is_defined_by_abi(const Symbol* sym) const
183   {
184     return strcmp(sym->name(), "__tls_get_addr") == 0;
185   }
186
187   // Return the size of the GOT section.
188   section_size_type
189   got_size() const
190   {
191     gold_assert(this->got_ != NULL);
192     return this->got_->data_size();
193   }
194
195   // Get the PLT section.
196   const Output_data_plt_powerpc<size, big_endian>*
197   plt_section() const
198   {
199     gold_assert(this->plt_ != NULL);
200     return this->plt_;
201   }
202
203   // Get the .glink section.
204   const Output_data_glink<size, big_endian>*
205   glink_section() const
206   {
207     gold_assert(this->glink_ != NULL);
208     return this->glink_;
209   }
210
211   // Get the GOT section.
212   const Output_data_got_powerpc<size, big_endian>*
213   got_section() const
214   {
215     gold_assert(this->got_ != NULL);
216     return this->got_;
217   }
218
219  protected:
220   Object*
221   do_make_elf_object(const std::string&, Input_file*, off_t,
222                      const elfcpp::Ehdr<size, big_endian>&);
223
224   // Return the number of entries in the GOT.
225   unsigned int
226   got_entry_count() const
227   {
228     if (this->got_ == NULL)
229       return 0;
230     return this->got_size() / (size / 8);
231   }
232
233   // Return the number of entries in the PLT.
234   unsigned int
235   plt_entry_count() const;
236
237   // Return the offset of the first non-reserved PLT entry.
238   unsigned int
239   first_plt_entry_offset() const;
240
241   // Return the size of each PLT entry.
242   unsigned int
243   plt_entry_size() const;
244
245  private:
246
247   // The class which scans relocations.
248   class Scan
249   {
250   public:
251     Scan()
252       : issued_non_pic_error_(false)
253     { }
254
255     static inline int
256     get_reference_flags(unsigned int r_type);
257
258     inline void
259     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
260           Sized_relobj_file<size, big_endian>* object,
261           unsigned int data_shndx,
262           Output_section* output_section,
263           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
264           const elfcpp::Sym<size, big_endian>& lsym);
265
266     inline void
267     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
268            Sized_relobj_file<size, big_endian>* object,
269            unsigned int data_shndx,
270            Output_section* output_section,
271            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
272            Symbol* gsym);
273
274     inline bool
275     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
276                                         Target_powerpc* ,
277                                         Sized_relobj_file<size, big_endian>* ,
278                                         unsigned int ,
279                                         Output_section* ,
280                                         const elfcpp::Rela<size, big_endian>& ,
281                                         unsigned int ,
282                                         const elfcpp::Sym<size, big_endian>&)
283     { return false; }
284
285     inline bool
286     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
287                                          Target_powerpc* ,
288                                          Sized_relobj_file<size, big_endian>* ,
289                                          unsigned int ,
290                                          Output_section* ,
291                                          const elfcpp::Rela<size,
292                                                             big_endian>& ,
293                                          unsigned int , Symbol*)
294     { return false; }
295
296   private:
297     static void
298     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
299                             unsigned int r_type);
300
301     static void
302     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
303                              unsigned int r_type, Symbol*);
304
305     static void
306     generate_tls_call(Symbol_table* symtab, Layout* layout,
307                       Target_powerpc* target);
308
309     void
310     check_non_pic(Relobj*, unsigned int r_type);
311
312     // Whether we have issued an error about a non-PIC compilation.
313     bool issued_non_pic_error_;
314   };
315
316   // The class which implements relocation.
317   class Relocate
318   {
319    public:
320     // Do a relocation.  Return false if the caller should not issue
321     // any warnings about this relocation.
322     inline bool
323     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
324              Output_section*, size_t relnum,
325              const elfcpp::Rela<size, big_endian>&,
326              unsigned int r_type, const Sized_symbol<size>*,
327              const Symbol_value<size>*,
328              unsigned char*,
329              typename elfcpp::Elf_types<size>::Elf_Addr,
330              section_size_type);
331
332    private:
333     // Do a TLS relocation.
334     inline void
335     relocate_tls(const Relocate_info<size, big_endian>*,
336                  Target_powerpc* target,
337                  size_t relnum, const elfcpp::Rela<size, big_endian>&,
338                  unsigned int r_type, const Sized_symbol<size>*,
339                  const Symbol_value<size>*,
340                  unsigned char*,
341                  typename elfcpp::Elf_types<size>::Elf_Addr,
342                  section_size_type);
343   };
344
345   // A class which returns the size required for a relocation type,
346   // used while scanning relocs during a relocatable link.
347   class Relocatable_size_for_reloc
348   {
349    public:
350     unsigned int
351     get_size_for_reloc(unsigned int, Relobj*)
352     {
353       gold_unreachable();
354       return 0;
355     }
356   };
357
358   // Adjust TLS relocation type based on the options and whether this
359   // is a local symbol.
360   static tls::Tls_optimization
361   optimize_tls_reloc(bool is_final, int r_type);
362
363   // Get the GOT section, creating it if necessary.
364   Output_data_got_powerpc<size, big_endian>*
365   got_section(Symbol_table*, Layout*);
366
367   // Create glink.
368   void
369   make_glink_section(Layout*);
370
371   // Create the PLT section.
372   void
373   make_plt_section(Layout*);
374
375   // Create a PLT entry for a global symbol.
376   void
377   make_plt_entry(Layout*, Symbol*,
378                  const elfcpp::Rela<size, big_endian>&,
379                  const Sized_relobj<size, big_endian>* object);
380
381   // Create a GOT entry for the TLS module index.
382   unsigned int
383   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
384                       Sized_relobj_file<size, big_endian>* object);
385
386   // Get the dynamic reloc section, creating it if necessary.
387   Reloc_section*
388   rela_dyn_section(Layout*);
389
390   // Copy a relocation against a global symbol.
391   void
392   copy_reloc(Symbol_table* symtab, Layout* layout,
393              Sized_relobj_file<size, big_endian>* object,
394              unsigned int shndx, Output_section* output_section,
395              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
396   {
397     this->copy_relocs_.copy_reloc(symtab, layout,
398                                   symtab->get_sized_symbol<size>(sym),
399                                   object, shndx, output_section,
400                                   reloc, this->rela_dyn_section(layout));
401   }
402
403   // Information about this specific target which we pass to the
404   // general Target structure.
405   static Target::Target_info powerpc_info;
406
407   // The types of GOT entries needed for this platform.
408   // These values are exposed to the ABI in an incremental link.
409   // Do not renumber existing values without changing the version
410   // number of the .gnu_incremental_inputs section.
411   enum Got_type
412   {
413     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
414     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
415     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
416   };
417
418   // The GOT output section.
419   Output_data_got_powerpc<size, big_endian>* got_;
420   // The PLT output section.
421   Output_data_plt_powerpc<size, big_endian>* plt_;
422   // The .glink output section.
423   Output_data_glink<size, big_endian>* glink_;
424   // The dynamic reloc output section.
425   Reloc_section* rela_dyn_;
426   // Relocs saved to avoid a COPY reloc.
427   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
428   // Space for variables copied with a COPY reloc.
429   Output_data_space* dynbss_;
430   // Offset of the GOT entry for the TLS module index;
431   unsigned int got_mod_index_offset_;
432 };
433
434 template<>
435 Target::Target_info Target_powerpc<32, true>::powerpc_info =
436 {
437   32,                   // size
438   true,                 // is_big_endian
439   elfcpp::EM_PPC,       // machine_code
440   false,                // has_make_symbol
441   false,                // has_resolve
442   false,                // has_code_fill
443   true,                 // is_default_stack_executable
444   false,                // can_icf_inline_merge_sections
445   '\0',                 // wrap_char
446   "/usr/lib/ld.so.1",   // dynamic_linker
447   0x10000000,           // default_text_segment_address
448   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
449   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
450   false,                // isolate_execinstr
451   0,                    // rosegment_gap
452   elfcpp::SHN_UNDEF,    // small_common_shndx
453   elfcpp::SHN_UNDEF,    // large_common_shndx
454   0,                    // small_common_section_flags
455   0,                    // large_common_section_flags
456   NULL,                 // attributes_section
457   NULL                  // attributes_vendor
458 };
459
460 template<>
461 Target::Target_info Target_powerpc<32, false>::powerpc_info =
462 {
463   32,                   // size
464   false,                // is_big_endian
465   elfcpp::EM_PPC,       // machine_code
466   false,                // has_make_symbol
467   false,                // has_resolve
468   false,                // has_code_fill
469   true,                 // is_default_stack_executable
470   false,                // can_icf_inline_merge_sections
471   '\0',                 // wrap_char
472   "/usr/lib/ld.so.1",   // dynamic_linker
473   0x10000000,           // default_text_segment_address
474   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
475   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
476   false,                // isolate_execinstr
477   0,                    // rosegment_gap
478   elfcpp::SHN_UNDEF,    // small_common_shndx
479   elfcpp::SHN_UNDEF,    // large_common_shndx
480   0,                    // small_common_section_flags
481   0,                    // large_common_section_flags
482   NULL,                 // attributes_section
483   NULL                  // attributes_vendor
484 };
485
486 template<>
487 Target::Target_info Target_powerpc<64, true>::powerpc_info =
488 {
489   64,                   // size
490   true,                 // is_big_endian
491   elfcpp::EM_PPC64,     // machine_code
492   false,                // has_make_symbol
493   false,                // has_resolve
494   false,                // has_code_fill
495   true,                 // is_default_stack_executable
496   false,                // can_icf_inline_merge_sections
497   '\0',                 // wrap_char
498   "/usr/lib/ld.so.1",   // dynamic_linker
499   0x10000000,           // default_text_segment_address
500   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
501   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
502   false,                // isolate_execinstr
503   0,                    // rosegment_gap
504   elfcpp::SHN_UNDEF,    // small_common_shndx
505   elfcpp::SHN_UNDEF,    // large_common_shndx
506   0,                    // small_common_section_flags
507   0,                    // large_common_section_flags
508   NULL,                 // attributes_section
509   NULL                  // attributes_vendor
510 };
511
512 template<>
513 Target::Target_info Target_powerpc<64, false>::powerpc_info =
514 {
515   64,                   // size
516   false,                // is_big_endian
517   elfcpp::EM_PPC64,     // machine_code
518   false,                // has_make_symbol
519   false,                // has_resolve
520   false,                // has_code_fill
521   true,                 // is_default_stack_executable
522   false,                // can_icf_inline_merge_sections
523   '\0',                 // wrap_char
524   "/usr/lib/ld.so.1",   // dynamic_linker
525   0x10000000,           // default_text_segment_address
526   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
527   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
528   false,                // isolate_execinstr
529   0,                    // rosegment_gap
530   elfcpp::SHN_UNDEF,    // small_common_shndx
531   elfcpp::SHN_UNDEF,    // large_common_shndx
532   0,                    // small_common_section_flags
533   0,                    // large_common_section_flags
534   NULL,                 // attributes_section
535   NULL                  // attributes_vendor
536 };
537
538 template<int size, bool big_endian>
539 class Powerpc_relocate_functions
540 {
541 private:
542   // Do a simple RELA relocation
543   template<int valsize>
544   static inline void
545   rela(unsigned char* view,
546        unsigned int right_shift,
547        elfcpp::Elf_Xword dst_mask,
548        typename elfcpp::Swap<size, big_endian>::Valtype value,
549        typename elfcpp::Swap<size, big_endian>::Valtype addend)
550   {
551     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
552     Valtype* wv = reinterpret_cast<Valtype*>(view);
553     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
554     Valtype reloc = (value + addend) >> right_shift;
555
556     val &= ~dst_mask;
557     reloc &= dst_mask;
558
559     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
560   }
561
562   // Do a simple RELA relocation, unaligned.
563   template<int valsize>
564   static inline void
565   rela_ua(unsigned char* view,
566           unsigned int right_shift,
567           elfcpp::Elf_Xword dst_mask,
568           typename elfcpp::Swap<size, big_endian>::Valtype value,
569           typename elfcpp::Swap<size, big_endian>::Valtype addend)
570   {
571     typedef typename elfcpp::Swap_unaligned<valsize,
572                                             big_endian>::Valtype Valtype;
573     Valtype* wv = reinterpret_cast<Valtype*>(view);
574     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
575     Valtype reloc = (value + addend) >> right_shift;
576
577     val &= ~dst_mask;
578     reloc &= dst_mask;
579
580     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
581   }
582
583   typedef Powerpc_relocate_functions<size, big_endian> This;
584   typedef Relocate_functions<size, big_endian> This_reloc;
585 public:
586   // R_POWERPC_REL32: (Symbol + Addend - Address)
587   static inline void
588   rel32(unsigned char* view,
589         typename elfcpp::Elf_types<size>::Elf_Addr value,
590         typename elfcpp::Elf_types<size>::Elf_Addr addend,
591         typename elfcpp::Elf_types<size>::Elf_Addr address)
592   { This_reloc::pcrela32(view, value, addend, address); }
593
594   // R_POWERPC_REL24: (Symbol + Addend - Address) & 0x3fffffc
595   static inline void
596   rel24(unsigned char* view,
597         typename elfcpp::Elf_types<size>::Elf_Addr value,
598         typename elfcpp::Elf_types<size>::Elf_Addr addend,
599         typename elfcpp::Elf_types<size>::Elf_Addr address)
600   {
601     This::template rela<32>(view, 0, 0x03fffffc, value - address, addend);
602   }
603
604   // R_POWERPC_REL14: (Symbol + Addend - Address) & 0xfffc
605   static inline void
606   rel14(unsigned char* view,
607         typename elfcpp::Elf_types<size>::Elf_Addr value,
608         typename elfcpp::Elf_types<size>::Elf_Addr addend,
609         typename elfcpp::Elf_types<size>::Elf_Addr address)
610   {
611     This::template rela<32>(view, 0, 0xfffc, value - address, addend);
612   }
613
614   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
615   static inline void
616   addr16(unsigned char* view,
617          typename elfcpp::Elf_types<size>::Elf_Addr value,
618          typename elfcpp::Elf_types<size>::Elf_Addr addend)
619   { This_reloc::rela16(view, value, addend); }
620
621   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
622   static inline void
623   addr16_ds(unsigned char* view,
624             typename elfcpp::Elf_types<size>::Elf_Addr value,
625             typename elfcpp::Elf_types<size>::Elf_Addr addend)
626   {
627     This::template rela<16>(view, 0, 0xfffc, value, addend);
628   }
629
630   // R_POWERPC_ADDR16_LO: (Symbol + Addend) & 0xffff
631   static inline void
632   addr16_lo(unsigned char* view,
633          typename elfcpp::Elf_types<size>::Elf_Addr value,
634          typename elfcpp::Elf_types<size>::Elf_Addr addend)
635   { This_reloc::rela16(view, value, addend); }
636
637   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
638   static inline void
639   addr16_hi(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, 16, 0xffff, value, addend);
644   }
645
646   // R_POWERPC_ADDR16_HA: Same as R_POWERPC_ADDR16_HI except that if the
647   //                      final value of the low 16 bits of the
648   //                      relocation is negative, add one.
649   static inline void
650   addr16_ha(unsigned char* view,
651             typename elfcpp::Elf_types<size>::Elf_Addr value,
652             typename elfcpp::Elf_types<size>::Elf_Addr addend)
653   {
654     This::addr16_hi(view, value + 0x8000, addend);
655   }
656
657   // R_PPC_REL16: (Symbol + Addend - Address) & 0xffff
658   static inline void
659   rel16(unsigned char* view,
660         typename elfcpp::Elf_types<size>::Elf_Addr value,
661         typename elfcpp::Elf_types<size>::Elf_Addr addend,
662         typename elfcpp::Elf_types<size>::Elf_Addr address)
663   { This_reloc::pcrela16(view, value, addend, address); }
664
665   // R_PPC_REL16_LO: (Symbol + Addend - Address) & 0xffff
666   static inline void
667   rel16_lo(unsigned char* view,
668            typename elfcpp::Elf_types<size>::Elf_Addr value,
669            typename elfcpp::Elf_types<size>::Elf_Addr addend,
670            typename elfcpp::Elf_types<size>::Elf_Addr address)
671   { This_reloc::pcrela16(view, value, addend, address); }
672
673   // R_PPC_REL16_HI: ((Symbol + Addend - Address) >> 16) & 0xffff
674   static inline void
675   rel16_hi(unsigned char* view,
676            typename elfcpp::Elf_types<size>::Elf_Addr value,
677            typename elfcpp::Elf_types<size>::Elf_Addr addend,
678            typename elfcpp::Elf_types<size>::Elf_Addr address)
679   {
680     This::template rela<16>(view, 16, 0xffff, value - address, addend);
681   }
682
683   // R_PPC_REL16_HA: Same as R_PPC_REL16_HI except that if the
684   //                 final value of the low 16 bits of the
685   //                 relocation is negative, add one.
686   static inline void
687   rel16_ha(unsigned char* view,
688            typename elfcpp::Elf_types<size>::Elf_Addr value,
689            typename elfcpp::Elf_types<size>::Elf_Addr addend,
690            typename elfcpp::Elf_types<size>::Elf_Addr address)
691   {
692     This::rel16_hi(view, value + 0x8000, addend, address);
693   }
694 };
695
696 // Stash away the index of .got2 in a relocatable object, if such
697 // a section exists.
698
699 template<int size, bool big_endian>
700 bool
701 Powerpc_relobj<size, big_endian>::do_find_special_sections(
702     Read_symbols_data* sd)
703 {
704   if (size == 32)
705     {
706       const unsigned char* const pshdrs = sd->section_headers->data();
707       const unsigned char* namesu = sd->section_names->data();
708       const char* names = reinterpret_cast<const char*>(namesu);
709       section_size_type names_size = sd->section_names_size;
710       const unsigned char* s;
711
712       s = this->find_shdr(pshdrs, ".got2", names, names_size, NULL);
713       if (s != NULL)
714         {
715           unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
716           this->got2_section_ = ndx;
717         }
718     }
719   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
720 }
721
722 // Set up PowerPC target specific relobj.
723
724 template<int size, bool big_endian>
725 Object*
726 Target_powerpc<size, big_endian>::do_make_elf_object(
727     const std::string& name,
728     Input_file* input_file,
729     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
730 {
731   int et = ehdr.get_e_type();
732   if (et == elfcpp::ET_REL)
733     {
734       Powerpc_relobj<size, big_endian>* obj =
735         new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
736       obj->setup();
737       return obj;
738     }
739   else if (et == elfcpp::ET_DYN)
740     {
741       Sized_dynobj<size, big_endian>* obj =
742         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
743       obj->setup();
744       return obj;
745     }
746   else
747     {
748       gold_error(_("%s: unsupported ELF file type %d"),
749                  name.c_str(), et);
750       return NULL;
751     }
752 }
753
754 template<int size, bool big_endian>
755 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
756 {
757 public:
758   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
759   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
760
761   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
762     : Output_data_got<size, big_endian>(),
763       symtab_(symtab), layout_(layout),
764       header_ent_cnt_(size == 32 ? 3 : 1),
765       header_index_(size == 32 ? 0x2000 : 0)
766   {}
767
768   class Got_entry;
769
770   // Create a new GOT entry and return its offset.
771   unsigned int
772   add_got_entry(Got_entry got_entry)
773   {
774     this->reserve_ent();
775     return Output_data_got<size, big_endian>::add_got_entry(got_entry);
776   }
777
778   // Create a pair of new GOT entries and return the offset of the first.
779   unsigned int
780   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
781   {
782     this->reserve_ent(2);
783     return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
784                                                                  got_entry_2);
785   }
786
787   // Value of _GLOBAL_OFFSET_TABLE_
788   unsigned int
789   g_o_t() const
790   {
791     return this->got_offset(this->header_index_);
792   }
793
794   // Ensure our GOT has a header.
795   void
796   set_final_data_size()
797   {
798     if (this->header_ent_cnt_ != 0)
799       this->make_header();
800     Output_data_got<size, big_endian>::set_final_data_size();
801   }
802
803   // First word of GOT header needs some values that are not
804   // handled by Output_data_got so poke them in here.
805   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
806   void
807   do_write(Output_file* of)
808   {
809     replace_constant(this->header_index_,
810                      (size == 32
811                       ? this->layout_->dynamic_section()->address()
812                       : this->address() + 0x8000));
813
814     Output_data_got<size, big_endian>::do_write(of);
815   }
816
817 private:
818   void
819   reserve_ent(unsigned int cnt = 1)
820   {
821     if (this->header_ent_cnt_ == 0)
822       return;
823     if (this->num_entries() + cnt > this->header_index_)
824       this->make_header();
825   }
826
827   void
828   make_header()
829   {
830     this->header_ent_cnt_ = 0;
831     this->header_index_ = this->num_entries();
832     if (size == 32)
833       {
834         Output_data_got<size, big_endian>::add_constant(0);
835         Output_data_got<size, big_endian>::add_constant(0);
836         Output_data_got<size, big_endian>::add_constant(0);
837
838         // Define _GLOBAL_OFFSET_TABLE_ at the header
839         this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
840                                              Symbol_table::PREDEFINED,
841                                              this, this->g_o_t(), 0,
842                                              elfcpp::STT_OBJECT,
843                                              elfcpp::STB_LOCAL,
844                                              elfcpp::STV_HIDDEN,
845                                              0, false, false);
846       }
847     else
848       Output_data_got<size, big_endian>::add_constant(0);
849   }
850
851   // Stashed pointers.
852   Symbol_table* symtab_;
853   Layout* layout_;
854
855   // GOT header size.
856   unsigned int header_ent_cnt_;
857   // GOT header index.
858   unsigned int header_index_;
859 };
860
861 // Get the GOT section, creating it if necessary.
862
863 template<int size, bool big_endian>
864 Output_data_got_powerpc<size, big_endian>*
865 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
866                                               Layout* layout)
867 {
868   if (this->got_ == NULL)
869     {
870       gold_assert(symtab != NULL && layout != NULL);
871
872       this->got_
873         = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
874
875       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
876                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
877                                       this->got_, ORDER_DATA, false);
878     }
879
880   return this->got_;
881 }
882
883 // Get the dynamic reloc section, creating it if necessary.
884
885 template<int size, bool big_endian>
886 typename Target_powerpc<size, big_endian>::Reloc_section*
887 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
888 {
889   if (this->rela_dyn_ == NULL)
890     {
891       gold_assert(layout != NULL);
892       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
893       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
894                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
895                                       ORDER_DYNAMIC_RELOCS, false);
896     }
897   return this->rela_dyn_;
898 }
899
900 // A class to handle the PLT data.
901
902 template<int size, bool big_endian>
903 class Output_data_plt_powerpc : public Output_section_data_build
904 {
905  public:
906   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
907                             size, big_endian> Reloc_section;
908
909   Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
910
911   // Add an entry to the PLT.
912   void
913   add_entry(Symbol*);
914
915   // Return the .rela.plt section data.
916   const Reloc_section*
917   rel_plt() const
918   {
919     return this->rel_;
920   }
921
922   // Return the number of PLT entries.
923   unsigned int
924   entry_count() const
925   {
926     return ((this->current_data_size() - initial_plt_entry_size)
927             / plt_entry_size);
928   }
929
930   // Return the offset of the first non-reserved PLT entry.
931   static unsigned int
932   first_plt_entry_offset()
933   { return initial_plt_entry_size; }
934
935   // Return the size of a PLT entry.
936   static unsigned int
937   get_plt_entry_size()
938   { return plt_entry_size; }
939
940  protected:
941   void
942   do_adjust_output_section(Output_section* os)
943   {
944     os->set_entsize(0);
945   }
946
947  private:
948   // The size of an entry in the PLT.
949   static const int plt_entry_size = size == 32 ? 4 : 24;
950   // The size of the first reserved entry.
951   static const int initial_plt_entry_size = size == 32 ? 0 : 24;
952
953   // Write out the PLT data.
954   void
955   do_write(Output_file*);
956
957   // The reloc section.
958   Reloc_section* rel_;
959   // Allows access to .glink for do_write.
960   Target_powerpc<size, big_endian>* targ_;
961 };
962
963 // Create the PLT section.
964
965 template<int size, bool big_endian>
966 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
967     Layout* layout,
968     Target_powerpc<size, big_endian>* targ)
969   : Output_section_data_build(size == 32 ? 4 : 8),
970     targ_(targ)
971 {
972   this->rel_ = new Reloc_section(false);
973   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
974                                   elfcpp::SHF_ALLOC, this->rel_,
975                                   ORDER_DYNAMIC_PLT_RELOCS, false);
976 }
977
978 // Add an entry to the PLT.
979
980 template<int size, bool big_endian>
981 void
982 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
983 {
984   if (!gsym->has_plt_offset())
985     {
986       off_t off = this->current_data_size();
987
988       if (off == 0)
989         off += initial_plt_entry_size;
990       gsym->set_plt_offset(off);
991       gsym->set_needs_dynsym_entry();
992       this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
993       off += plt_entry_size;
994       this->set_current_data_size(off);
995     }
996 }
997
998 static const uint32_t addis_11_11       = 0x3d6b0000;
999 static const uint32_t addis_11_30       = 0x3d7e0000;
1000 static const uint32_t addis_12_12       = 0x3d8c0000;
1001 static const uint32_t addi_11_11        = 0x396b0000;
1002 static const uint32_t add_0_11_11       = 0x7c0b5a14;
1003 static const uint32_t add_11_0_11       = 0x7d605a14;
1004 static const uint32_t b                 = 0x48000000;
1005 static const uint32_t bcl_20_31         = 0x429f0005;
1006 static const uint32_t bctr              = 0x4e800420;
1007 static const uint32_t blrl              = 0x4e800021;
1008 static const uint32_t lis_11            = 0x3d600000;
1009 static const uint32_t lis_12            = 0x3d800000;
1010 static const uint32_t lwzu_0_12         = 0x840c0000;
1011 static const uint32_t lwz_0_12          = 0x800c0000;
1012 static const uint32_t lwz_11_11         = 0x816b0000;
1013 static const uint32_t lwz_11_30         = 0x817e0000;
1014 static const uint32_t lwz_12_12         = 0x818c0000;
1015 static const uint32_t mflr_0            = 0x7c0802a6;
1016 static const uint32_t mflr_12           = 0x7d8802a6;
1017 static const uint32_t mtctr_0           = 0x7c0903a6;
1018 static const uint32_t mtctr_11          = 0x7d6903a6;
1019 static const uint32_t mtlr_0            = 0x7c0803a6;
1020 static const uint32_t nop               = 0x60000000;
1021 static const uint32_t sub_11_11_12      = 0x7d6c5850;
1022 static const uint32_t addis_12_2        = 0x3d820000;
1023 static const uint32_t std_2_1           = 0xf8410000;
1024 static const uint32_t ld_11_12          = 0xe96c0000;
1025 static const uint32_t ld_2_12           = 0xe84c0000;
1026 static const uint32_t addi_12_12        = 0x398c0000;
1027 static const uint32_t ld_11_2           = 0xe9620000;
1028 static const uint32_t addi_2_2          = 0x38420000;
1029 static const uint32_t ld_2_2            = 0xe8420000;
1030 static const uint32_t mflr_11           = 0x7d6802a6;
1031 static const uint32_t ld_2_11           = 0xe84b0000;
1032 static const uint32_t mtlr_12           = 0x7d8803a6;
1033 static const uint32_t add_12_2_11       = 0x7d825a14;
1034 static const uint32_t li_0_0            = 0x38000000;
1035 static const uint32_t lis_0_0           = 0x3c000000;
1036 static const uint32_t ori_0_0_0         = 0x60000000;
1037
1038 // Write out the PLT.
1039
1040 template<int size, bool big_endian>
1041 void
1042 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1043 {
1044   if (size == 32)
1045     {
1046       const off_t offset = this->offset();
1047       const section_size_type oview_size
1048         = convert_to_section_size_type(this->data_size());
1049       unsigned char* const oview = of->get_output_view(offset, oview_size);
1050       unsigned char* pov = oview;
1051       unsigned char* endpov = oview + oview_size;
1052
1053       // The address the .glink branch table
1054       const Output_data_glink<size, big_endian>* glink
1055         = this->targ_->glink_section();
1056       elfcpp::Elf_types<32>::Elf_Addr branch_tab
1057         = glink->address() + glink->pltresolve();
1058
1059       while (pov < endpov)
1060         {
1061           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1062           pov += 4;
1063           branch_tab += 4;
1064         }
1065
1066       of->write_output_view(offset, oview_size, oview);
1067     }
1068 }
1069
1070 // Create the PLT section.
1071
1072 template<int size, bool big_endian>
1073 void
1074 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1075 {
1076   if (this->plt_ == NULL)
1077     {
1078       if (this->glink_ == NULL)
1079         make_glink_section(layout);
1080
1081       // Ensure that .rela.dyn always appears before .rela.plt  This is
1082       // necessary due to how, on PowerPC and some other targets, .rela.dyn
1083       // needs to include .rela.plt in it's range.
1084       this->rela_dyn_section(layout);
1085
1086       this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1087       layout->add_output_section_data(".plt",
1088                                       (size == 32
1089                                        ? elfcpp::SHT_PROGBITS
1090                                        : elfcpp::SHT_NOBITS),
1091                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1092                                       this->plt_,
1093                                       (size == 32
1094                                        ? ORDER_SMALL_DATA
1095                                        : ORDER_SMALL_BSS),
1096                                       false);
1097     }
1098 }
1099
1100 // A class to handle .glink.
1101
1102 template<int size, bool big_endian>
1103 class Output_data_glink : public Output_section_data
1104 {
1105  public:
1106   Output_data_glink(Target_powerpc<size, big_endian>*);
1107
1108   // Add an entry
1109   void
1110   add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1111             const Sized_relobj<size, big_endian>*);
1112
1113   unsigned int
1114   find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1115              const Sized_relobj<size, big_endian>*) const;
1116
1117   unsigned int
1118   glink_entry_size() const
1119   {
1120     if (size == 32)
1121       return 4 * 4;
1122     else
1123       // FIXME: We should be using multiple glink sections for
1124       // stubs to support > 33M applications.
1125       return 8 * 4;
1126   }
1127
1128   off_t
1129   pltresolve() const
1130   {
1131     return this->pltresolve_;
1132   }
1133
1134  private:
1135   static const int pltresolve_size = 16*4;
1136
1137   void
1138   set_final_data_size();
1139
1140   // Write out .glink
1141   void
1142   do_write(Output_file*);
1143
1144   class Glink_sym_ent
1145   {
1146   public:
1147     Glink_sym_ent(const Symbol* sym,
1148                   const elfcpp::Rela<size, big_endian>& reloc,
1149                   const Sized_relobj<size, big_endian>* object)
1150       : sym_(sym), object_(0), addend_(0)
1151     {
1152       if (size != 32)
1153         this->addend_ = reloc.get_r_addend();
1154       else if (parameters->options().output_is_position_independent()
1155                && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1156                    == elfcpp::R_PPC_PLTREL24))
1157         {
1158           this->addend_ = reloc.get_r_addend();
1159           if (this->addend_ != 0)
1160             this->object_ = object;
1161         }
1162     }
1163
1164     const Symbol* sym_;
1165     const Sized_relobj<size, big_endian>* object_;
1166     unsigned int addend_;
1167
1168     bool operator==(const Glink_sym_ent& that) const
1169     {
1170       return (this->sym_ == that.sym_
1171               && this->object_ == that.object_
1172               && this->addend_ == that.addend_);
1173     }
1174   };
1175
1176   class Glink_sym_ent_hash
1177   {
1178   public:
1179     size_t operator()(const Glink_sym_ent& ent) const
1180     {
1181       return (reinterpret_cast<uintptr_t>(ent.sym_)
1182               ^ reinterpret_cast<uintptr_t>(ent.object_)
1183               ^ ent.addend_);
1184     }
1185   };
1186
1187   // Map sym/object/addend to index.
1188   typedef Unordered_map<Glink_sym_ent, unsigned int,
1189                         Glink_sym_ent_hash> Glink_entries;
1190   Glink_entries glink_entries_;
1191
1192   // Offset of pltresolve stub (actually, branch table for 32-bit)
1193   off_t pltresolve_;
1194
1195   // Allows access to .got and .plt for do_write.
1196   Target_powerpc<size, big_endian>* targ_;
1197 };
1198
1199 // Create the glink section.
1200
1201 template<int size, bool big_endian>
1202 Output_data_glink<size, big_endian>::Output_data_glink(
1203     Target_powerpc<size, big_endian>* targ)
1204   : Output_section_data(16),
1205     pltresolve_(0), targ_(targ)
1206 {
1207 }
1208
1209 // Add an entry to glink, if we do not already have one for this
1210 // sym/object/addend combo.
1211
1212 template<int size, bool big_endian>
1213 void
1214 Output_data_glink<size, big_endian>::add_entry(
1215     const Symbol* gsym,
1216     const elfcpp::Rela<size, big_endian>& reloc,
1217     const Sized_relobj<size, big_endian>* object)
1218 {
1219   Glink_sym_ent ent(gsym, reloc, object);
1220   unsigned int indx = this->glink_entries_.size();
1221   this->glink_entries_.insert(std::make_pair(ent, indx));
1222 }
1223
1224 template<int size, bool big_endian>
1225 unsigned int
1226 Output_data_glink<size, big_endian>::find_entry(
1227     const Symbol* gsym,
1228     const elfcpp::Rela<size, big_endian>& reloc,
1229     const Sized_relobj<size, big_endian>* object) const
1230 {
1231   Glink_sym_ent ent(gsym, reloc, object);
1232   typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1233   gold_assert(p != this->glink_entries_.end());
1234   return p->second;
1235 }
1236
1237 template<int size, bool big_endian>
1238 void
1239 Output_data_glink<size, big_endian>::set_final_data_size()
1240 {
1241   unsigned int count = this->glink_entries_.size();
1242   off_t total = count;
1243
1244   if (count != 0)
1245     {
1246       if (size == 32)
1247         {
1248           total *= 16;
1249           this->pltresolve_ = total;
1250
1251           // space for branch table
1252           total += 4 * (count - 1);
1253
1254           total += -total & 15;
1255           total += this->pltresolve_size;
1256         }
1257       else
1258         {
1259           total *= 32;
1260           this->pltresolve_ = total;
1261           total += this->pltresolve_size;
1262
1263           // space for branch table
1264           total += 8 * count;
1265           if (count > 0x8000)
1266             total += 4 * (count - 0x8000);
1267         }
1268     }
1269
1270   this->set_data_size(total);
1271 }
1272
1273 static inline uint32_t
1274 l(uint32_t a)
1275 {
1276   return a & 0xffff;
1277 }
1278
1279 static inline uint32_t
1280 hi(uint32_t a)
1281 {
1282   return l(a >> 16);
1283 }
1284
1285 static inline uint32_t
1286 ha(uint32_t a)
1287 {
1288   return hi(a + 0x8000);
1289 }
1290
1291 template<bool big_endian>
1292 static inline void
1293 write_insn(unsigned char *p, uint32_t v)
1294 {
1295   elfcpp::Swap<32, big_endian>::writeval(p, v);
1296 }
1297
1298 // Write out .glink.
1299
1300 template<int size, bool big_endian>
1301 void
1302 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1303 {
1304   const off_t off = this->offset();
1305   const section_size_type oview_size =
1306     convert_to_section_size_type(this->data_size());
1307   unsigned char* const oview = of->get_output_view(off, oview_size);
1308   unsigned char *p;
1309
1310   // The base address of the .plt section.
1311   uint32_t plt_base = this->targ_->plt_section()->address();
1312
1313   // The address of _GLOBAL_OFFSET_TABLE_.
1314   const Output_data_got_powerpc<size, big_endian> *got;
1315   typename elfcpp::Elf_types<size>::Elf_Addr g_o_t;
1316   got = this->targ_->got_section();
1317   g_o_t = got->address() + got->g_o_t();
1318
1319   if (size == 64)
1320     {
1321       // Write out call stubs.
1322       typename Glink_entries::const_iterator g;
1323       for (g = this->glink_entries_.begin();
1324            g != this->glink_entries_.end();
1325            ++g)
1326         {
1327           uint64_t plt_addr = plt_base + g->first.sym_->plt_offset();
1328           uint64_t got_addr = g_o_t;
1329           uint64_t pltoff = plt_addr - got_addr;
1330
1331           if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1332             gold_error(_("%s: linkage table error against `%s'"),
1333                        g->first.object_->name().c_str(),
1334                        g->first.sym_->demangled_name().c_str());
1335
1336           p = oview + g->second * this->glink_entry_size();
1337           if (ha(pltoff) != 0)
1338             {
1339               write_insn<big_endian>(p, addis_12_2 + ha(pltoff)),       p += 4;
1340               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1341               write_insn<big_endian>(p, ld_11_12 + l(pltoff)),          p += 4;
1342               if (ha(pltoff + 16) != ha(pltoff))
1343                 {
1344                   write_insn<big_endian>(p, addi_12_12 + l(pltoff)),    p += 4;
1345                   pltoff = 0;
1346                 }
1347               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1348               write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)),       p += 4;
1349               write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)),     p += 4;
1350               write_insn<big_endian>(p, bctr),                          p += 4;
1351             }
1352           else
1353             {
1354               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1355               write_insn<big_endian>(p, ld_11_2 + l(pltoff)),           p += 4;
1356               if (ha(pltoff + 16) != ha(pltoff))
1357                 {
1358                   write_insn<big_endian>(p, addi_2_2 + l(pltoff)),      p += 4;
1359                   pltoff = 0;
1360                 }
1361               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1362               write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)),      p += 4;
1363               write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)),        p += 4;
1364               write_insn<big_endian>(p, bctr),                          p += 4;
1365             }
1366         }
1367
1368       // Write pltresolve stub.
1369       p = oview + this->pltresolve_;
1370       uint64_t after_bcl = this->address() + this->pltresolve_ + 16;
1371       uint64_t pltoff = plt_base - after_bcl;
1372
1373       elfcpp::Swap<64, big_endian>::writeval(p, pltoff),        p += 8;
1374
1375       write_insn<big_endian>(p, mflr_12),                       p += 4;
1376       write_insn<big_endian>(p, bcl_20_31),                     p += 4;
1377       write_insn<big_endian>(p, mflr_11),                       p += 4;
1378       write_insn<big_endian>(p, ld_2_11 + l(-16)),              p += 4;
1379       write_insn<big_endian>(p, mtlr_12),                       p += 4;
1380       write_insn<big_endian>(p, add_12_2_11),                   p += 4;
1381       write_insn<big_endian>(p, ld_11_12 + 0),                  p += 4;
1382       write_insn<big_endian>(p, ld_2_12 + 8),                   p += 4;
1383       write_insn<big_endian>(p, mtctr_11),                      p += 4;
1384       write_insn<big_endian>(p, ld_11_12 + 16),                 p += 4;
1385       write_insn<big_endian>(p, bctr),                          p += 4;
1386       while (p < oview + this->pltresolve_ + this->pltresolve_size)
1387         write_insn<big_endian>(p, nop), p += 4;
1388
1389       // Write lazy link call stubs.
1390       uint32_t indx = 0;
1391       while (p < oview + oview_size)
1392         {
1393           if (indx < 0x8000)
1394             {
1395               write_insn<big_endian>(p, li_0_0 + indx),                 p += 4;
1396             }
1397           else
1398             {
1399               write_insn<big_endian>(p, lis_0_0 + hi(indx)),            p += 4;
1400               write_insn<big_endian>(p, ori_0_0_0 + l(indx)),           p += 4;
1401             }
1402           uint16_t branch_off = this->pltresolve_ + 8 - (p - oview);
1403           write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),      p += 4;
1404           indx++;
1405         }
1406     }
1407   else
1408     {
1409       // Write out call stubs.
1410       typename Glink_entries::const_iterator g;
1411       for (g = this->glink_entries_.begin();
1412            g != this->glink_entries_.end();
1413            ++g)
1414         {
1415           uint32_t plt_addr = plt_base + g->first.sym_->plt_offset();
1416           uint32_t got_addr;
1417
1418           p = oview + g->second * this->glink_entry_size();
1419           if (parameters->options().output_is_position_independent())
1420             {
1421               const Powerpc_relobj<size, big_endian>* object = static_cast
1422                 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1423               if (object != NULL)
1424                 {
1425                   unsigned int got2 = object->got2_shndx();
1426                   got_addr = (g->first.object_->output_section(got2)->address()
1427                               + g->first.object_->output_section_offset(got2)
1428                               + g->first.addend_);
1429                 }
1430               else
1431                 got_addr = g_o_t;
1432
1433               uint32_t pltoff = plt_addr - got_addr;
1434               if (ha(pltoff) == 0)
1435                 {
1436                   write_insn<big_endian>(p +  0, lwz_11_30 + l(pltoff));
1437                   write_insn<big_endian>(p +  4, mtctr_11);
1438                   write_insn<big_endian>(p +  8, bctr);
1439                 }
1440               else
1441                 {
1442                   write_insn<big_endian>(p +  0, addis_11_30 + ha(pltoff));
1443                   write_insn<big_endian>(p +  4, lwz_11_11 + l(pltoff));
1444                   write_insn<big_endian>(p +  8, mtctr_11);
1445                   write_insn<big_endian>(p + 12, bctr);
1446                 }
1447             }
1448           else
1449             {
1450               write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
1451               write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
1452               write_insn<big_endian>(p +  8, mtctr_11);
1453               write_insn<big_endian>(p + 12, bctr);
1454             }
1455         }
1456
1457       // Write out pltresolve branch table.
1458       p = oview + this->pltresolve_;
1459       unsigned int the_end = oview_size - this->pltresolve_size;
1460       unsigned char *end_p = oview + the_end;
1461       while (p < end_p - 8 * 4)
1462         write_insn<big_endian>(p, b + end_p - p), p += 4;
1463       while (p < end_p)
1464         write_insn<big_endian>(p, nop), p += 4;
1465
1466       // Write out pltresolve call stub.
1467       if (parameters->options().output_is_position_independent())
1468         {
1469           uint32_t res0_off = this->pltresolve_;
1470           uint32_t after_bcl_off = the_end + 12;
1471           uint32_t bcl_res0 = after_bcl_off - res0_off;
1472
1473           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
1474           write_insn<big_endian>(p +  4, mflr_0);
1475           write_insn<big_endian>(p +  8, bcl_20_31);
1476           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
1477           write_insn<big_endian>(p + 16, mflr_12);
1478           write_insn<big_endian>(p + 20, mtlr_0);
1479           write_insn<big_endian>(p + 24, sub_11_11_12);
1480
1481           uint32_t got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
1482
1483           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
1484           if (ha(got_bcl) == ha(got_bcl + 4))
1485             {
1486               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
1487               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
1488             }
1489           else
1490             {
1491               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
1492               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
1493             }
1494           write_insn<big_endian>(p + 40, mtctr_0);
1495           write_insn<big_endian>(p + 44, add_0_11_11);
1496           write_insn<big_endian>(p + 48, add_11_0_11);
1497           write_insn<big_endian>(p + 52, bctr);
1498           write_insn<big_endian>(p + 56, nop);
1499           write_insn<big_endian>(p + 60, nop);
1500         }
1501       else
1502         {
1503           uint32_t res0 = this->pltresolve_ + this->address();
1504
1505           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
1506           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
1507           if (ha(g_o_t + 4) == ha(g_o_t + 8))
1508             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
1509           else
1510             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
1511           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
1512           write_insn<big_endian>(p + 16, mtctr_0);
1513           write_insn<big_endian>(p + 20, add_0_11_11);
1514           if (ha(g_o_t + 4) == ha(g_o_t + 8))
1515             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
1516           else
1517             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
1518           write_insn<big_endian>(p + 28, add_11_0_11);
1519           write_insn<big_endian>(p + 32, bctr);
1520           write_insn<big_endian>(p + 36, nop);
1521           write_insn<big_endian>(p + 40, nop);
1522           write_insn<big_endian>(p + 44, nop);
1523           write_insn<big_endian>(p + 48, nop);
1524           write_insn<big_endian>(p + 52, nop);
1525           write_insn<big_endian>(p + 56, nop);
1526           write_insn<big_endian>(p + 60, nop);
1527         }
1528       p += 64;
1529     }
1530
1531   of->write_output_view(off, oview_size, oview);
1532 }
1533
1534 // Create the glink section.
1535
1536 template<int size, bool big_endian>
1537 void
1538 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
1539 {
1540   if (this->glink_ == NULL)
1541     {
1542       this->glink_ = new Output_data_glink<size, big_endian>(this);
1543       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
1544                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1545                                       this->glink_, ORDER_TEXT, false);
1546     }
1547 }
1548
1549 // Create a PLT entry for a global symbol.
1550
1551 template<int size, bool big_endian>
1552 void
1553 Target_powerpc<size, big_endian>::make_plt_entry(
1554     Layout* layout,
1555     Symbol* gsym,
1556     const elfcpp::Rela<size, big_endian>& reloc,
1557     const Sized_relobj<size, big_endian>* object)
1558 {
1559   if (this->plt_ == NULL)
1560     this->make_plt_section(layout);
1561
1562   this->plt_->add_entry(gsym);
1563
1564   this->glink_->add_entry(gsym, reloc, object);
1565 }
1566
1567 // Return the number of entries in the PLT.
1568
1569 template<int size, bool big_endian>
1570 unsigned int
1571 Target_powerpc<size, big_endian>::plt_entry_count() const
1572 {
1573   if (this->plt_ == NULL)
1574     return 0;
1575   return this->plt_->entry_count();
1576 }
1577
1578 // Return the offset of the first non-reserved PLT entry.
1579
1580 template<int size, bool big_endian>
1581 unsigned int
1582 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
1583 {
1584   return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
1585 }
1586
1587 // Return the size of each PLT entry.
1588
1589 template<int size, bool big_endian>
1590 unsigned int
1591 Target_powerpc<size, big_endian>::plt_entry_size() const
1592 {
1593   return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
1594 }
1595
1596 // Create a GOT entry for the TLS module index.
1597
1598 template<int size, bool big_endian>
1599 unsigned int
1600 Target_powerpc<size, big_endian>::got_mod_index_entry(
1601     Symbol_table* symtab,
1602     Layout* layout,
1603     Sized_relobj_file<size, big_endian>* object)
1604 {
1605   if (this->got_mod_index_offset_ == -1U)
1606     {
1607       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1608       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1609       Output_data_got_powerpc<size, big_endian>* got;
1610       unsigned int got_offset;
1611
1612       got = this->got_section(symtab, layout);
1613       got->reserve_ent(2);
1614       got_offset = got->add_constant(0);
1615       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
1616                           got_offset, 0);
1617       got->add_constant(0);
1618       this->got_mod_index_offset_ = got_offset;
1619     }
1620   return this->got_mod_index_offset_;
1621 }
1622
1623 // Optimize the TLS relocation type based on what we know about the
1624 // symbol.  IS_FINAL is true if the final address of this symbol is
1625 // known at link time.
1626
1627 template<int size, bool big_endian>
1628 tls::Tls_optimization
1629 Target_powerpc<size, big_endian>::optimize_tls_reloc(bool, int)
1630 {
1631   // If we are generating a shared library, then we can't do anything
1632   // in the linker.
1633   if (parameters->options().shared())
1634     return tls::TLSOPT_NONE;
1635   // FIXME
1636   return tls::TLSOPT_NONE;
1637 }
1638
1639 // Get the Reference_flags for a particular relocation.
1640
1641 template<int size, bool big_endian>
1642 int
1643 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
1644 {
1645   switch (r_type)
1646     {
1647     case elfcpp::R_POWERPC_NONE:
1648     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1649     case elfcpp::R_POWERPC_GNU_VTENTRY:
1650     case elfcpp::R_PPC64_TOC:
1651       // No symbol reference.
1652       return 0;
1653
1654     case elfcpp::R_POWERPC_ADDR16:
1655     case elfcpp::R_POWERPC_ADDR16_LO:
1656     case elfcpp::R_POWERPC_ADDR16_HI:
1657     case elfcpp::R_POWERPC_ADDR16_HA:
1658     case elfcpp::R_POWERPC_ADDR32:
1659     case elfcpp::R_PPC64_ADDR64:
1660       return Symbol::ABSOLUTE_REF;
1661
1662     case elfcpp::R_POWERPC_REL24:
1663     case elfcpp::R_PPC_LOCAL24PC:
1664     case elfcpp::R_PPC_REL16:
1665     case elfcpp::R_PPC_REL16_LO:
1666     case elfcpp::R_PPC_REL16_HI:
1667     case elfcpp::R_PPC_REL16_HA:
1668       return Symbol::RELATIVE_REF;
1669
1670     case elfcpp::R_PPC_PLTREL24:
1671       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1672
1673     case elfcpp::R_POWERPC_GOT16:
1674     case elfcpp::R_POWERPC_GOT16_LO:
1675     case elfcpp::R_POWERPC_GOT16_HI:
1676     case elfcpp::R_POWERPC_GOT16_HA:
1677     case elfcpp::R_PPC64_TOC16:
1678     case elfcpp::R_PPC64_TOC16_LO:
1679     case elfcpp::R_PPC64_TOC16_HI:
1680     case elfcpp::R_PPC64_TOC16_HA:
1681     case elfcpp::R_PPC64_TOC16_DS:
1682     case elfcpp::R_PPC64_TOC16_LO_DS:
1683       // Absolute in GOT.
1684       return Symbol::ABSOLUTE_REF;
1685
1686     case elfcpp::R_POWERPC_GOT_TPREL16:
1687     case elfcpp::R_POWERPC_TLS:
1688       return Symbol::TLS_REF;
1689
1690     case elfcpp::R_POWERPC_COPY:
1691     case elfcpp::R_POWERPC_GLOB_DAT:
1692     case elfcpp::R_POWERPC_JMP_SLOT:
1693     case elfcpp::R_POWERPC_RELATIVE:
1694     case elfcpp::R_POWERPC_DTPMOD:
1695     default:
1696       // Not expected.  We will give an error later.
1697       return 0;
1698     }
1699 }
1700
1701 // Report an unsupported relocation against a local symbol.
1702
1703 template<int size, bool big_endian>
1704 void
1705 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
1706     Sized_relobj_file<size, big_endian>* object,
1707     unsigned int r_type)
1708 {
1709   gold_error(_("%s: unsupported reloc %u against local symbol"),
1710              object->name().c_str(), r_type);
1711 }
1712
1713 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1714 // dynamic linker does not support it, issue an error.
1715
1716 template<int size, bool big_endian>
1717 void
1718 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
1719                                                       unsigned int r_type)
1720 {
1721   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
1722
1723   // These are the relocation types supported by glibc for both 32-bit
1724   // and 64-bit powerpc.
1725   switch (r_type)
1726     {
1727     case elfcpp::R_POWERPC_RELATIVE:
1728     case elfcpp::R_POWERPC_GLOB_DAT:
1729     case elfcpp::R_POWERPC_DTPMOD:
1730     case elfcpp::R_POWERPC_DTPREL:
1731     case elfcpp::R_POWERPC_TPREL:
1732     case elfcpp::R_POWERPC_JMP_SLOT:
1733     case elfcpp::R_POWERPC_COPY:
1734     case elfcpp::R_POWERPC_ADDR32:
1735     case elfcpp::R_POWERPC_ADDR24:
1736     case elfcpp::R_POWERPC_REL24:
1737       return;
1738
1739     default:
1740       break;
1741     }
1742
1743   if (size == 64)
1744     {
1745       switch (r_type)
1746         {
1747           // These are the relocation types supported only on 64-bit.
1748         case elfcpp::R_PPC64_ADDR64:
1749         case elfcpp::R_PPC64_TPREL16_LO_DS:
1750         case elfcpp::R_PPC64_TPREL16_DS:
1751         case elfcpp::R_POWERPC_TPREL16:
1752         case elfcpp::R_POWERPC_TPREL16_LO:
1753         case elfcpp::R_POWERPC_TPREL16_HI:
1754         case elfcpp::R_POWERPC_TPREL16_HA:
1755         case elfcpp::R_PPC64_TPREL16_HIGHER:
1756         case elfcpp::R_PPC64_TPREL16_HIGHEST:
1757         case elfcpp::R_PPC64_TPREL16_HIGHERA:
1758         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
1759         case elfcpp::R_PPC64_ADDR16_LO_DS:
1760         case elfcpp::R_POWERPC_ADDR16_LO:
1761         case elfcpp::R_POWERPC_ADDR16_HI:
1762         case elfcpp::R_POWERPC_ADDR16_HA:
1763         case elfcpp::R_POWERPC_ADDR30:
1764         case elfcpp::R_PPC64_UADDR64:
1765         case elfcpp::R_POWERPC_UADDR32:
1766         case elfcpp::R_POWERPC_ADDR16:
1767         case elfcpp::R_POWERPC_UADDR16:
1768         case elfcpp::R_PPC64_ADDR16_DS:
1769         case elfcpp::R_PPC64_ADDR16_HIGHER:
1770         case elfcpp::R_PPC64_ADDR16_HIGHEST:
1771         case elfcpp::R_PPC64_ADDR16_HIGHERA:
1772         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
1773         case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
1774         case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
1775         case elfcpp::R_POWERPC_REL32:
1776         case elfcpp::R_PPC64_REL64:
1777           return;
1778
1779         default:
1780           break;
1781         }
1782     }
1783   else
1784     {
1785       switch (r_type)
1786         {
1787           // These are the relocation types supported only on 32-bit.
1788
1789         default:
1790           break;
1791         }
1792     }
1793
1794   // This prevents us from issuing more than one error per reloc
1795   // section.  But we can still wind up issuing more than one
1796   // error per object file.
1797   if (this->issued_non_pic_error_)
1798     return;
1799   gold_assert(parameters->options().output_is_position_independent());
1800   object->error(_("requires unsupported dynamic reloc; "
1801                   "recompile with -fPIC"));
1802   this->issued_non_pic_error_ = true;
1803   return;
1804 }
1805
1806 // Scan a relocation for a local symbol.
1807
1808 template<int size, bool big_endian>
1809 inline void
1810 Target_powerpc<size, big_endian>::Scan::local(
1811     Symbol_table* symtab,
1812     Layout* layout,
1813     Target_powerpc<size, big_endian>* target,
1814     Sized_relobj_file<size, big_endian>* object,
1815     unsigned int data_shndx,
1816     Output_section* output_section,
1817     const elfcpp::Rela<size, big_endian>& reloc,
1818     unsigned int r_type,
1819     const elfcpp::Sym<size, big_endian>& lsym)
1820 {
1821   switch (r_type)
1822     {
1823     case elfcpp::R_POWERPC_NONE:
1824     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1825     case elfcpp::R_POWERPC_GNU_VTENTRY:
1826       break;
1827
1828     case elfcpp::R_PPC64_ADDR64:
1829     case elfcpp::R_POWERPC_ADDR32:
1830     case elfcpp::R_POWERPC_ADDR16_HA:
1831     case elfcpp::R_POWERPC_ADDR16_LO:
1832       // If building a shared library (or a position-independent
1833       // executable), we need to create a dynamic relocation for
1834       // this location.
1835       if (parameters->options().output_is_position_independent())
1836         {
1837           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1838
1839           check_non_pic(object, r_type);
1840           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1841             {
1842               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1843               rela_dyn->add_local(object, r_sym, r_type, output_section,
1844                                   data_shndx, reloc.get_r_offset(),
1845                                   reloc.get_r_addend());
1846             }
1847           else
1848             {
1849               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1850               gold_assert(lsym.get_st_value() == 0);
1851               rela_dyn->add_local_relative(object, r_sym, r_type,
1852                                            output_section, data_shndx,
1853                                            reloc.get_r_offset(),
1854                                            reloc.get_r_addend(), false);
1855             }
1856         }
1857       break;
1858
1859     case elfcpp::R_POWERPC_REL24:
1860     case elfcpp::R_PPC_LOCAL24PC:
1861     case elfcpp::R_POWERPC_REL32:
1862     case elfcpp::R_PPC_REL16_LO:
1863     case elfcpp::R_PPC_REL16_HA:
1864       break;
1865
1866     case elfcpp::R_POWERPC_GOT16:
1867     case elfcpp::R_POWERPC_GOT16_LO:
1868     case elfcpp::R_POWERPC_GOT16_HI:
1869     case elfcpp::R_POWERPC_GOT16_HA:
1870       {
1871         // The symbol requires a GOT entry.
1872         Output_data_got_powerpc<size, big_endian>* got;
1873         unsigned int r_sym;
1874
1875         got = target->got_section(symtab, layout);
1876         r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1877
1878         // If we are generating a shared object, we need to add a
1879         // dynamic relocation for this symbol's GOT entry.
1880         if (parameters->options().output_is_position_independent())
1881           {
1882             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1883               {
1884                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1885                 unsigned int off;
1886
1887                 off = got->add_constant(0);
1888                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1889                 rela_dyn->add_local_relative(object, r_sym,
1890                                              elfcpp::R_POWERPC_RELATIVE,
1891                                              got, off, 0, false);
1892               }
1893           }
1894         else
1895           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1896       }
1897       break;
1898
1899     case elfcpp::R_PPC64_TOC16:
1900     case elfcpp::R_PPC64_TOC16_LO:
1901     case elfcpp::R_PPC64_TOC16_HI:
1902     case elfcpp::R_PPC64_TOC16_HA:
1903     case elfcpp::R_PPC64_TOC16_DS:
1904     case elfcpp::R_PPC64_TOC16_LO_DS:
1905     case elfcpp::R_PPC64_TOC:
1906       // We need a GOT section.
1907       target->got_section(symtab, layout);
1908       break;
1909
1910       // These are relocations which should only be seen by the
1911       // dynamic linker, and should never be seen here.
1912     case elfcpp::R_POWERPC_COPY:
1913     case elfcpp::R_POWERPC_GLOB_DAT:
1914     case elfcpp::R_POWERPC_JMP_SLOT:
1915     case elfcpp::R_POWERPC_RELATIVE:
1916     case elfcpp::R_POWERPC_DTPMOD:
1917       gold_error(_("%s: unexpected reloc %u in object file"),
1918                  object->name().c_str(), r_type);
1919       break;
1920
1921     default:
1922       unsupported_reloc_local(object, r_type);
1923       break;
1924     }
1925 }
1926
1927 // Report an unsupported relocation against a global symbol.
1928
1929 template<int size, bool big_endian>
1930 void
1931 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
1932     Sized_relobj_file<size, big_endian>* object,
1933     unsigned int r_type,
1934     Symbol* gsym)
1935 {
1936   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1937              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1938 }
1939
1940 // Scan a relocation for a global symbol.
1941
1942 template<int size, bool big_endian>
1943 inline void
1944 Target_powerpc<size, big_endian>::Scan::global(
1945     Symbol_table* symtab,
1946     Layout* layout,
1947     Target_powerpc<size, big_endian>* target,
1948     Sized_relobj_file<size, big_endian>* object,
1949     unsigned int data_shndx,
1950     Output_section* output_section,
1951     const elfcpp::Rela<size, big_endian>& reloc,
1952     unsigned int r_type,
1953     Symbol* gsym)
1954 {
1955   switch (r_type)
1956     {
1957     case elfcpp::R_POWERPC_NONE:
1958     case elfcpp::R_POWERPC_GNU_VTINHERIT:
1959     case elfcpp::R_POWERPC_GNU_VTENTRY:
1960     case elfcpp::R_PPC_LOCAL24PC:
1961       break;
1962
1963     case elfcpp::R_POWERPC_ADDR16:
1964     case elfcpp::R_POWERPC_ADDR16_LO:
1965     case elfcpp::R_POWERPC_ADDR16_HI:
1966     case elfcpp::R_POWERPC_ADDR16_HA:
1967     case elfcpp::R_POWERPC_ADDR32:
1968     case elfcpp::R_PPC64_ADDR64:
1969       {
1970         // Make a PLT entry if necessary.
1971         if (gsym->needs_plt_entry())
1972           {
1973             target->make_plt_entry(layout, gsym, reloc, 0);
1974             // Since this is not a PC-relative relocation, we may be
1975             // taking the address of a function. In that case we need to
1976             // set the entry in the dynamic symbol table to the address of
1977             // the PLT entry.
1978             if (size == 32
1979                 && gsym->is_from_dynobj() && !parameters->options().shared())
1980               gsym->set_needs_dynsym_value();
1981           }
1982         // Make a dynamic relocation if necessary.
1983         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1984           {
1985             if (gsym->may_need_copy_reloc())
1986               {
1987                 target->copy_reloc(symtab, layout, object,
1988                                    data_shndx, output_section, gsym, reloc);
1989               }
1990             else if ((r_type == elfcpp::R_POWERPC_ADDR32
1991                       || r_type == elfcpp::R_PPC64_ADDR64)
1992                      && gsym->can_use_relative_reloc(false))
1993               {
1994                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1995                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
1996                                               output_section, object,
1997                                               data_shndx, reloc.get_r_offset(),
1998                                               reloc.get_r_addend(), false);
1999               }
2000             else
2001               {
2002                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2003
2004                 check_non_pic(object, r_type);
2005                 if (gsym->is_from_dynobj()
2006                     || gsym->is_undefined()
2007                     || gsym->is_preemptible())
2008                   rela_dyn->add_global(gsym, r_type, output_section,
2009                                        object, data_shndx,
2010                                        reloc.get_r_offset(),
2011                                        reloc.get_r_addend());
2012                 else
2013                   rela_dyn->add_global_relative(gsym, r_type,
2014                                                 output_section, object,
2015                                                 data_shndx,
2016                                                 reloc.get_r_offset(),
2017                                                 reloc.get_r_addend(), false);
2018               }
2019           }
2020       }
2021       break;
2022
2023     case elfcpp::R_PPC_PLTREL24:
2024     case elfcpp::R_POWERPC_REL24:
2025       {
2026         if (gsym->needs_plt_entry()
2027             || (!gsym->final_value_is_known()
2028                  && !(gsym->is_defined()
2029                       && !gsym->is_from_dynobj()
2030                       && !gsym->is_preemptible())))
2031           target->make_plt_entry(layout, gsym, reloc, object);
2032         // Make a dynamic relocation if necessary.
2033         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2034           {
2035             if (gsym->may_need_copy_reloc())
2036               {
2037                 target->copy_reloc(symtab, layout, object,
2038                                    data_shndx, output_section, gsym,
2039                                    reloc);
2040               }
2041             else
2042               {
2043                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2044                 check_non_pic(object, r_type);
2045                 rela_dyn->add_global(gsym, r_type, output_section, object,
2046                                      data_shndx, reloc.get_r_offset(),
2047                                      reloc.get_r_addend());
2048               }
2049           }
2050       }
2051       break;
2052
2053     case elfcpp::R_PPC_REL16:
2054     case elfcpp::R_PPC_REL16_LO:
2055     case elfcpp::R_PPC_REL16_HI:
2056     case elfcpp::R_PPC_REL16_HA:
2057       break;
2058
2059     case elfcpp::R_POWERPC_GOT16:
2060     case elfcpp::R_POWERPC_GOT16_LO:
2061     case elfcpp::R_POWERPC_GOT16_HI:
2062     case elfcpp::R_POWERPC_GOT16_HA:
2063       {
2064         // The symbol requires a GOT entry.
2065         Output_data_got_powerpc<size, big_endian>* got;
2066
2067         got = target->got_section(symtab, layout);
2068         if (gsym->final_value_is_known())
2069           got->add_global(gsym, GOT_TYPE_STANDARD);
2070         else
2071           {
2072             // If this symbol is not fully resolved, we need to add a
2073             // dynamic relocation for it.
2074             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2075             if (gsym->is_from_dynobj()
2076                 || gsym->is_undefined()
2077                 || gsym->is_preemptible())
2078               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2079                                        elfcpp::R_POWERPC_GLOB_DAT);
2080             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2081               {
2082                 unsigned int off = got->add_constant(0);
2083
2084                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2085                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2086                                               got, off, 0, false);
2087               }
2088           }
2089       }
2090       break;
2091
2092     case elfcpp::R_PPC64_TOC:
2093     case elfcpp::R_PPC64_TOC16:
2094     case elfcpp::R_PPC64_TOC16_LO:
2095     case elfcpp::R_PPC64_TOC16_HI:
2096     case elfcpp::R_PPC64_TOC16_HA:
2097     case elfcpp::R_PPC64_TOC16_DS:
2098     case elfcpp::R_PPC64_TOC16_LO_DS:
2099       // We need a GOT section.
2100       target->got_section(symtab, layout);
2101       break;
2102
2103     case elfcpp::R_POWERPC_GOT_TPREL16:
2104     case elfcpp::R_POWERPC_TLS:
2105       // XXX TLS
2106       break;
2107
2108       // These are relocations which should only be seen by the
2109       // dynamic linker, and should never be seen here.
2110     case elfcpp::R_POWERPC_COPY:
2111     case elfcpp::R_POWERPC_GLOB_DAT:
2112     case elfcpp::R_POWERPC_JMP_SLOT:
2113     case elfcpp::R_POWERPC_RELATIVE:
2114     case elfcpp::R_POWERPC_DTPMOD:
2115       gold_error(_("%s: unexpected reloc %u in object file"),
2116                  object->name().c_str(), r_type);
2117       break;
2118
2119     default:
2120       unsupported_reloc_global(object, r_type, gsym);
2121       break;
2122     }
2123 }
2124
2125 // Process relocations for gc.
2126
2127 template<int size, bool big_endian>
2128 void
2129 Target_powerpc<size, big_endian>::gc_process_relocs(
2130     Symbol_table* symtab,
2131     Layout* layout,
2132     Sized_relobj_file<size, big_endian>* object,
2133     unsigned int data_shndx,
2134     unsigned int,
2135     const unsigned char* prelocs,
2136     size_t reloc_count,
2137     Output_section* output_section,
2138     bool needs_special_offset_handling,
2139     size_t local_symbol_count,
2140     const unsigned char* plocal_symbols)
2141 {
2142   typedef Target_powerpc<size, big_endian> Powerpc;
2143   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2144
2145   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
2146                           typename Target_powerpc::Relocatable_size_for_reloc>(
2147     symtab,
2148     layout,
2149     this,
2150     object,
2151     data_shndx,
2152     prelocs,
2153     reloc_count,
2154     output_section,
2155     needs_special_offset_handling,
2156     local_symbol_count,
2157     plocal_symbols);
2158 }
2159
2160 // Scan relocations for a section.
2161
2162 template<int size, bool big_endian>
2163 void
2164 Target_powerpc<size, big_endian>::scan_relocs(
2165     Symbol_table* symtab,
2166     Layout* layout,
2167     Sized_relobj_file<size, big_endian>* object,
2168     unsigned int data_shndx,
2169     unsigned int sh_type,
2170     const unsigned char* prelocs,
2171     size_t reloc_count,
2172     Output_section* output_section,
2173     bool needs_special_offset_handling,
2174     size_t local_symbol_count,
2175     const unsigned char* plocal_symbols)
2176 {
2177   typedef Target_powerpc<size, big_endian> Powerpc;
2178   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2179
2180   if (sh_type == elfcpp::SHT_REL)
2181     {
2182       gold_error(_("%s: unsupported REL reloc section"),
2183                  object->name().c_str());
2184       return;
2185     }
2186
2187   if (size == 32)
2188     {
2189       static Output_data_space* sdata;
2190
2191       // Define _SDA_BASE_ at the start of the .sdata section.
2192       if (sdata == NULL)
2193         {
2194           // layout->find_output_section(".sdata") == NULL
2195           sdata = new Output_data_space(4, "** sdata");
2196           Output_section* os
2197             = layout->add_output_section_data(".sdata", 0,
2198                                               elfcpp::SHF_ALLOC
2199                                               | elfcpp::SHF_WRITE,
2200                                               sdata, ORDER_SMALL_DATA, false);
2201           symtab->define_in_output_data("_SDA_BASE_", NULL,
2202                                         Symbol_table::PREDEFINED,
2203                                         os, 32768, 0, elfcpp::STT_OBJECT,
2204                                         elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2205                                         0, false, false);
2206         }
2207     }
2208
2209   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
2210     symtab,
2211     layout,
2212     this,
2213     object,
2214     data_shndx,
2215     prelocs,
2216     reloc_count,
2217     output_section,
2218     needs_special_offset_handling,
2219     local_symbol_count,
2220     plocal_symbols);
2221 }
2222
2223 // Finalize the sections.
2224
2225 template<int size, bool big_endian>
2226 void
2227 Target_powerpc<size, big_endian>::do_finalize_sections(
2228     Layout* layout,
2229     const Input_objects*,
2230     Symbol_table*)
2231 {
2232   // Fill in some more dynamic tags.
2233   const Reloc_section* rel_plt = (this->plt_ == NULL
2234                                   ? NULL
2235                                   : this->plt_->rel_plt());
2236   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2237                                   this->rela_dyn_, true, size == 32);
2238
2239   if (size == 32)
2240     {
2241       this->got_->finalize_data_size();
2242       Output_data_dynamic* odyn = layout->dynamic_data();
2243       odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
2244                                     this->got_, this->got_->g_o_t());
2245     }
2246
2247   // Emit any relocs we saved in an attempt to avoid generating COPY
2248   // relocs.
2249   if (this->copy_relocs_.any_saved_relocs())
2250     this->copy_relocs_.emit(this->rela_dyn_section(layout));
2251 }
2252
2253 // Perform a relocation.
2254
2255 template<int size, bool big_endian>
2256 inline bool
2257 Target_powerpc<size, big_endian>::Relocate::relocate(
2258     const Relocate_info<size, big_endian>* relinfo,
2259     Target_powerpc* target,
2260     Output_section* os,
2261     size_t relnum,
2262     const elfcpp::Rela<size, big_endian>& rela,
2263     unsigned int r_type,
2264     const Sized_symbol<size>* gsym,
2265     const Symbol_value<size>* psymval,
2266     unsigned char* view,
2267     typename elfcpp::Elf_types<size>::Elf_Addr address,
2268     section_size_type /* view_size */)
2269 {
2270   const unsigned int toc_base_offset = 0x8000;
2271   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
2272   const Powerpc_relobj<size, big_endian>* const object
2273     = static_cast<const Powerpc_relobj<size, big_endian>*>(relinfo->object);
2274   elfcpp::Elf_Xword value;
2275
2276   if (r_type == elfcpp::R_POWERPC_GOT16
2277       || r_type == elfcpp::R_POWERPC_GOT16_LO
2278       || r_type == elfcpp::R_POWERPC_GOT16_HI
2279       || r_type == elfcpp::R_POWERPC_GOT16_HA
2280       || r_type == elfcpp::R_PPC64_GOT16_DS
2281       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
2282     {
2283       if (gsym != NULL)
2284         {
2285           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2286           value = gsym->got_offset(GOT_TYPE_STANDARD);
2287         }
2288       else
2289         {
2290           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2291           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2292           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2293         }
2294       value -= target->got_section()->g_o_t();
2295     }
2296   else if (r_type == elfcpp::R_PPC64_TOC)
2297     {
2298       value = target->got_section()->address() + toc_base_offset;
2299     }
2300   else if (gsym != NULL
2301            && (r_type == elfcpp::R_POWERPC_REL24
2302                || r_type == elfcpp::R_PPC_PLTREL24)
2303            && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2304     {
2305       const Output_data_glink<size, big_endian>* glink;
2306
2307       glink = target->glink_section();
2308       unsigned int glink_index = glink->find_entry(gsym, rela, object);
2309       value = glink->address() + glink_index * glink->glink_entry_size();
2310     }
2311   else
2312     {
2313       elfcpp::Elf_Xword addend = 0;
2314       if (r_type != elfcpp::R_PPC_PLTREL24)
2315         addend = rela.get_r_addend();
2316       value = psymval->value(object, addend);
2317     }
2318
2319   switch (r_type)
2320     {
2321     case elfcpp::R_PPC64_TOC16:
2322     case elfcpp::R_PPC64_TOC16_LO:
2323     case elfcpp::R_PPC64_TOC16_HI:
2324     case elfcpp::R_PPC64_TOC16_HA:
2325     case elfcpp::R_PPC64_TOC16_DS:
2326     case elfcpp::R_PPC64_TOC16_LO_DS:
2327       // Subtract the TOC base address.
2328       value -= target->got_section()->address() + toc_base_offset;
2329       break;
2330
2331     case elfcpp::R_POWERPC_SECTOFF:
2332     case elfcpp::R_POWERPC_SECTOFF_LO:
2333     case elfcpp::R_POWERPC_SECTOFF_HI:
2334     case elfcpp::R_POWERPC_SECTOFF_HA:
2335     case elfcpp::R_PPC64_SECTOFF_DS:
2336     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2337       if (os != NULL)
2338         value -= os->address();
2339       break;
2340
2341     default:
2342       break;
2343     }
2344
2345   switch (r_type)
2346     {
2347     case elfcpp::R_POWERPC_NONE:
2348     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2349     case elfcpp::R_POWERPC_GNU_VTENTRY:
2350       break;
2351
2352     case elfcpp::R_POWERPC_REL32:
2353       Reloc::rel32(view, value, 0, address);
2354       break;
2355
2356     case elfcpp::R_POWERPC_REL24:
2357     case elfcpp::R_PPC_PLTREL24:
2358     case elfcpp::R_PPC_LOCAL24PC:
2359       Reloc::rel24(view, value, 0, address);
2360       break;
2361
2362     case elfcpp::R_POWERPC_REL14:
2363       Reloc::rel14(view, value, 0, address);
2364       break;
2365
2366     case elfcpp::R_PPC64_ADDR64:
2367     case elfcpp::R_PPC64_TOC:
2368       Relocate_functions<size, big_endian>::rela64(view, value, 0);
2369       break;
2370
2371     case elfcpp::R_POWERPC_ADDR32:
2372       Relocate_functions<size, big_endian>::rela32(view, value, 0);
2373       break;
2374
2375     case elfcpp::R_POWERPC_ADDR16:
2376     case elfcpp::R_PPC64_TOC16:
2377     case elfcpp::R_POWERPC_GOT16:
2378     case elfcpp::R_POWERPC_SECTOFF:
2379       Reloc::addr16(view, value, 0);
2380       break;
2381
2382     case elfcpp::R_POWERPC_ADDR16_LO:
2383     case elfcpp::R_PPC64_TOC16_LO:
2384     case elfcpp::R_POWERPC_GOT16_LO:
2385     case elfcpp::R_POWERPC_SECTOFF_LO:
2386       Reloc::addr16_lo(view, value, 0);
2387       break;
2388
2389     case elfcpp::R_POWERPC_ADDR16_HI:
2390     case elfcpp::R_PPC64_TOC16_HI:
2391     case elfcpp::R_POWERPC_GOT16_HI:
2392     case elfcpp::R_POWERPC_SECTOFF_HI:
2393       Reloc::addr16_hi(view, value, 0);
2394       break;
2395
2396     case elfcpp::R_POWERPC_ADDR16_HA:
2397     case elfcpp::R_PPC64_TOC16_HA:
2398     case elfcpp::R_POWERPC_GOT16_HA:
2399     case elfcpp::R_POWERPC_SECTOFF_HA:
2400       Reloc::addr16_ha(view, value, 0);
2401       break;
2402
2403     case elfcpp::R_PPC_REL16_LO:
2404       Reloc::rel16_lo(view, value, 0, address);
2405       break;
2406
2407     case elfcpp::R_PPC_REL16_HI:
2408       Reloc::rel16_hi(view, value, 0, address);
2409       break;
2410
2411     case elfcpp::R_PPC_REL16_HA:
2412       Reloc::rel16_ha(view, value, 0, address);
2413       break;
2414
2415     case elfcpp::R_PPC64_ADDR16_DS:
2416     case elfcpp::R_PPC64_ADDR16_LO_DS:
2417     case elfcpp::R_PPC64_TOC16_DS:
2418     case elfcpp::R_PPC64_TOC16_LO_DS:
2419     case elfcpp::R_PPC64_GOT16_DS:
2420     case elfcpp::R_PPC64_GOT16_LO_DS:
2421     case elfcpp::R_PPC64_SECTOFF_DS:
2422     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2423       Reloc::addr16_ds(view, value, 0);
2424       break;
2425
2426     case elfcpp::R_POWERPC_COPY:
2427     case elfcpp::R_POWERPC_GLOB_DAT:
2428     case elfcpp::R_POWERPC_JMP_SLOT:
2429     case elfcpp::R_POWERPC_RELATIVE:
2430       // This is an outstanding tls reloc, which is unexpected when
2431       // linking.
2432     case elfcpp::R_POWERPC_DTPMOD:
2433       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2434                              _("unexpected reloc %u in object file"),
2435                              r_type);
2436       break;
2437
2438     default:
2439       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2440                              _("unsupported reloc %u"),
2441                              r_type);
2442       break;
2443     }
2444
2445   return true;
2446 }
2447
2448 // Perform a TLS relocation.
2449
2450 template<int size, bool big_endian>
2451 inline void
2452 Target_powerpc<size, big_endian>::Relocate::relocate_tls(
2453     const Relocate_info<size, big_endian>* relinfo,
2454     Target_powerpc<size, big_endian>* target,
2455     size_t relnum,
2456     const elfcpp::Rela<size, big_endian>& rela,
2457     unsigned int r_type,
2458     const Sized_symbol<size>* gsym,
2459     const Symbol_value<size>* psymval,
2460     unsigned char* view,
2461     typename elfcpp::Elf_types<size>::Elf_Addr address,
2462     section_size_type)
2463 {
2464   Output_segment* tls_segment = relinfo->layout->tls_segment();
2465   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2466
2467   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2468   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2469
2470   const bool is_final =
2471     (gsym == NULL
2472      ? !parameters->options().output_is_position_independent()
2473      : gsym->final_value_is_known());
2474
2475   switch (r_type)
2476     {
2477       // XXX
2478     }
2479 }
2480
2481 // Relocate section data.
2482
2483 template<int size, bool big_endian>
2484 void
2485 Target_powerpc<size, big_endian>::relocate_section(
2486     const Relocate_info<size, big_endian>* relinfo,
2487     unsigned int sh_type,
2488     const unsigned char* prelocs,
2489     size_t reloc_count,
2490     Output_section* output_section,
2491     bool needs_special_offset_handling,
2492     unsigned char* view,
2493     typename elfcpp::Elf_types<size>::Elf_Addr address,
2494     section_size_type view_size,
2495     const Reloc_symbol_changes* reloc_symbol_changes)
2496 {
2497   typedef Target_powerpc<size, big_endian> Powerpc;
2498   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
2499
2500   gold_assert(sh_type == elfcpp::SHT_RELA);
2501
2502   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
2503                          Powerpc_relocate>(
2504     relinfo,
2505     this,
2506     prelocs,
2507     reloc_count,
2508     output_section,
2509     needs_special_offset_handling,
2510     view,
2511     address,
2512     view_size,
2513     reloc_symbol_changes);
2514 }
2515
2516 class Powerpc_scan_relocatable_reloc
2517 {
2518 public:
2519   // Return the strategy to use for a local symbol which is not a
2520   // section symbol, given the relocation type.
2521   inline Relocatable_relocs::Reloc_strategy
2522   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
2523   {
2524     if (r_type == 0 && r_sym == 0)
2525       return Relocatable_relocs::RELOC_DISCARD;
2526     return Relocatable_relocs::RELOC_COPY;
2527   }
2528
2529   // Return the strategy to use for a local symbol which is a section
2530   // symbol, given the relocation type.
2531   inline Relocatable_relocs::Reloc_strategy
2532   local_section_strategy(unsigned int, Relobj*)
2533   {
2534     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
2535   }
2536
2537   // Return the strategy to use for a global symbol, given the
2538   // relocation type, the object, and the symbol index.
2539   inline Relocatable_relocs::Reloc_strategy
2540   global_strategy(unsigned int r_type, Relobj*, unsigned int)
2541   {
2542     if (r_type == elfcpp::R_PPC_PLTREL24)
2543       return Relocatable_relocs::RELOC_SPECIAL;
2544     return Relocatable_relocs::RELOC_COPY;
2545   }
2546 };
2547
2548 // Scan the relocs during a relocatable link.
2549
2550 template<int size, bool big_endian>
2551 void
2552 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
2553     Symbol_table* symtab,
2554     Layout* layout,
2555     Sized_relobj_file<size, big_endian>* object,
2556     unsigned int data_shndx,
2557     unsigned int sh_type,
2558     const unsigned char* prelocs,
2559     size_t reloc_count,
2560     Output_section* output_section,
2561     bool needs_special_offset_handling,
2562     size_t local_symbol_count,
2563     const unsigned char* plocal_symbols,
2564     Relocatable_relocs* rr)
2565 {
2566   gold_assert(sh_type == elfcpp::SHT_RELA);
2567
2568   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
2569                                 Powerpc_scan_relocatable_reloc>(
2570     symtab,
2571     layout,
2572     object,
2573     data_shndx,
2574     prelocs,
2575     reloc_count,
2576     output_section,
2577     needs_special_offset_handling,
2578     local_symbol_count,
2579     plocal_symbols,
2580     rr);
2581 }
2582
2583 // Relocate a section during a relocatable link.
2584
2585 template<int size, bool big_endian>
2586 void
2587 Target_powerpc<size, big_endian>::relocate_for_relocatable(
2588     const Relocate_info<size, big_endian>* relinfo,
2589     unsigned int sh_type,
2590     const unsigned char* prelocs,
2591     size_t reloc_count,
2592     Output_section* output_section,
2593     off_t offset_in_output_section,
2594     const Relocatable_relocs* rr,
2595     unsigned char*,
2596     typename elfcpp::Elf_types<size>::Elf_Addr,
2597     section_size_type,
2598     unsigned char* reloc_view,
2599     section_size_type reloc_view_size)
2600 {
2601   gold_assert(sh_type == elfcpp::SHT_RELA);
2602
2603   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
2604   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
2605     Reltype;
2606   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
2607     Reltype_write;
2608   const int reloc_size
2609     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
2610   const Address invalid_address = static_cast<Address>(0) - 1;
2611
2612   Powerpc_relobj<size, big_endian>* const object
2613     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
2614   const unsigned int local_count = object->local_symbol_count();
2615   unsigned int got2_shndx = object->got2_shndx();
2616   typename elfcpp::Elf_types<size>::Elf_Swxword got2_addend = 0;
2617   if (got2_shndx != 0)
2618     got2_addend = object->get_output_section_offset(got2_shndx);
2619
2620   unsigned char* pwrite = reloc_view;
2621
2622   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2623     {
2624       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
2625       if (strategy == Relocatable_relocs::RELOC_DISCARD)
2626         continue;
2627
2628       Reltype reloc(prelocs);
2629       Reltype_write reloc_write(pwrite);
2630
2631       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
2632       const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2633       const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2634
2635       // Get the new symbol index.
2636
2637       unsigned int new_symndx;
2638       if (r_sym < local_count)
2639         {
2640           switch (strategy)
2641             {
2642             case Relocatable_relocs::RELOC_COPY:
2643             case Relocatable_relocs::RELOC_SPECIAL:
2644               new_symndx = object->symtab_index(r_sym);
2645               gold_assert(new_symndx != -1U);
2646               break;
2647
2648             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
2649               {
2650                 // We are adjusting a section symbol.  We need to find
2651                 // the symbol table index of the section symbol for
2652                 // the output section corresponding to input section
2653                 // in which this symbol is defined.
2654                 gold_assert(r_sym < local_count);
2655                 bool is_ordinary;
2656                 unsigned int shndx =
2657                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
2658                 gold_assert(is_ordinary);
2659                 Output_section* os = object->output_section(shndx);
2660                 gold_assert(os != NULL);
2661                 gold_assert(os->needs_symtab_index());
2662                 new_symndx = os->symtab_index();
2663               }
2664               break;
2665
2666             default:
2667               gold_unreachable();
2668             }
2669         }
2670       else
2671         {
2672           const Symbol* gsym = object->global_symbol(r_sym);
2673           gold_assert(gsym != NULL);
2674           if (gsym->is_forwarder())
2675             gsym = relinfo->symtab->resolve_forwards(gsym);
2676
2677           gold_assert(gsym->has_symtab_index());
2678           new_symndx = gsym->symtab_index();
2679         }
2680
2681       // Get the new offset--the location in the output section where
2682       // this relocation should be applied.
2683
2684       Address offset = reloc.get_r_offset();
2685       Address new_offset;
2686       if (static_cast<Address>(offset_in_output_section) != invalid_address)
2687         new_offset = offset + offset_in_output_section;
2688       else
2689         {
2690           section_offset_type sot_offset =
2691               convert_types<section_offset_type, Address>(offset);
2692           section_offset_type new_sot_offset =
2693               output_section->output_offset(object, relinfo->data_shndx,
2694                                             sot_offset);
2695           gold_assert(new_sot_offset != -1);
2696           new_offset = new_sot_offset;
2697         }
2698
2699       reloc_write.put_r_offset(new_offset);
2700       reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type));
2701
2702       // Handle the reloc addend based on the strategy.
2703       typename elfcpp::Elf_types<size>::Elf_Swxword addend;
2704       addend = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2705         get_reloc_addend(&reloc);
2706
2707       if (strategy == Relocatable_relocs::RELOC_COPY)
2708         ;
2709       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
2710         {
2711           const Symbol_value<size>* psymval = object->local_symbol(r_sym);
2712
2713           addend = psymval->value(object, addend);
2714         }
2715       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
2716         {
2717           if (addend >= 32768)
2718             addend += got2_addend;
2719         }
2720       else
2721         gold_unreachable();
2722
2723       Reloc_types<elfcpp::SHT_RELA, size, big_endian>::
2724         set_reloc_addend(&reloc_write, addend);
2725
2726       pwrite += reloc_size;
2727     }
2728
2729   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
2730               == reloc_view_size);
2731 }
2732
2733 // Return the value to use for a dynamic which requires special
2734 // treatment.  This is how we support equality comparisons of function
2735 // pointers across shared library boundaries, as described in the
2736 // processor specific ABI supplement.
2737
2738 template<int size, bool big_endian>
2739 uint64_t
2740 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
2741 {
2742   if (size == 32)
2743     {
2744       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2745       return this->plt_section()->address() + gsym->plt_offset();
2746     }
2747   else
2748     gold_unreachable();
2749 }
2750
2751 // The selector for powerpc object files.
2752
2753 template<int size, bool big_endian>
2754 class Target_selector_powerpc : public Target_selector
2755 {
2756 public:
2757   Target_selector_powerpc()
2758     : Target_selector(elfcpp::EM_NONE, size, big_endian,
2759                       (size == 64
2760                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
2761                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
2762                       (size == 64
2763                        ? (big_endian ? "elf64ppc" : "elf64lppc")
2764                        : (big_endian ? "elf32ppc" : "elf32lppc")))
2765   { }
2766
2767   virtual Target*
2768   do_recognize(Input_file*, off_t, int machine, int, int)
2769   {
2770     switch (size)
2771       {
2772       case 64:
2773         if (machine != elfcpp::EM_PPC64)
2774           return NULL;
2775         break;
2776
2777       case 32:
2778         if (machine != elfcpp::EM_PPC)
2779           return NULL;
2780         break;
2781
2782       default:
2783         return NULL;
2784       }
2785
2786     return this->instantiate_target();
2787   }
2788
2789   virtual Target*
2790   do_instantiate_target()
2791   { return new Target_powerpc<size, big_endian>(); }
2792 };
2793
2794 Target_selector_powerpc<32, true> target_selector_ppc32;
2795 Target_selector_powerpc<32, false> target_selector_ppc32le;
2796 Target_selector_powerpc<64, true> target_selector_ppc64;
2797 Target_selector_powerpc<64, false> target_selector_ppc64le;
2798
2799 } // End anonymous namespace.