* output.h (Output_data_got::add_global_tls, add_local_tls,
[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   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
61   typedef typename elfcpp::Elf_types<size>::Elf_Off Offset;
62   typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
63   typedef Unordered_map<Address, Section_refs> Access_from;
64
65   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
66                  const typename elfcpp::Ehdr<size, big_endian>& ehdr)
67     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
68       special_(0), opd_ent_shndx_(), opd_ent_off_(), access_from_map_(),
69       opd_valid_(false)
70   { }
71
72   ~Powerpc_relobj()
73   { }
74
75   // The .got2 section shndx.
76   unsigned int
77   got2_shndx() const
78   {
79     if (size == 32)
80       return this->special_;
81     else
82       return 0;
83   }
84
85   // The .opd section shndx.
86   unsigned int
87   opd_shndx() const
88   {
89     if (size == 32)
90       return 0;
91     else
92       return this->special_;
93   }
94
95   // Init OPD entry arrays.
96   void
97   init_opd(size_t opd_size)
98   {
99     size_t count = this->opd_ent_ndx(opd_size);
100     this->opd_ent_shndx_.resize(count);
101     this->opd_ent_off_.reserve(count);
102   }
103
104   // Return section and offset of function entry for .opd + R_OFF.
105   unsigned int
106   get_opd_ent(Address r_off, Address* value = NULL) const
107   {
108     size_t ndx = this->opd_ent_ndx(r_off);
109     gold_assert(ndx < this->opd_ent_shndx_.size());
110     gold_assert(this->opd_ent_shndx_[ndx] != 0);
111     if (value != NULL)
112       *value = this->opd_ent_off_[ndx];
113     return this->opd_ent_shndx_[ndx];
114   }
115
116   // Set section and offset of function entry for .opd + R_OFF.
117   void
118   set_opd_ent(Address r_off, unsigned int shndx, Address value)
119   {
120     size_t ndx = this->opd_ent_ndx(r_off);
121     gold_assert(ndx < this->opd_ent_shndx_.size());
122     this->opd_ent_shndx_[ndx] = shndx;
123     this->opd_ent_off_[ndx] = value;
124   }
125
126   Access_from*
127   access_from_map()
128   { return &this->access_from_map_; }
129
130   // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
131   // section at DST_OFF.
132   void
133   add_reference(Object* src_obj,
134                 unsigned int src_indx,
135                 typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
136   {
137     Section_id src_id(src_obj, src_indx);
138     this->access_from_map_[dst_off].insert(src_id);
139   }
140
141   bool
142   opd_valid() const
143   { return this->opd_valid_; }
144
145   void
146   set_opd_valid()
147   { this->opd_valid_ = true; }
148
149   // Examine .rela.opd to build info about function entry points.
150   void
151   scan_opd_relocs(size_t reloc_count,
152                   const unsigned char* prelocs,
153                   const unsigned char* plocal_syms);
154
155   void
156   do_read_relocs(Read_relocs_data*);
157
158   bool
159   do_find_special_sections(Read_symbols_data* sd);
160
161   // Return offset in output GOT section that this object will use
162   // as a TOC pointer.  Won't be just a constant with multi-toc support.
163   Address
164   toc_base_offset() const
165   { return 0x8000; }
166
167 private:
168   // Return index into opd_ent_shndx or opd_ent_off array for .opd entry
169   // at OFF.  .opd entries are 24 bytes long, but they can be spaced
170   // 16 bytes apart when the language doesn't use the last 8-byte
171   // word, the environment pointer.  Thus dividing the entry section
172   // offset by 16 will give an index into opd_ent_shndx_ and
173   // opd_ent_off_ that works for either layout of .opd.  (It leaves
174   // some elements of the vectors unused when .opd entries are spaced
175   // 24 bytes apart, but we don't know the spacing until relocations
176   // are processed, and in any case it is possible for an object to
177   // have some entries spaced 16 bytes apart and others 24 bytes apart.)
178   size_t
179   opd_ent_ndx(size_t off) const
180   { return off >> 4;}
181
182   // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
183   unsigned int special_;
184   // The first 8-byte word of an OPD entry gives the address of the
185   // entry point of the function.  Relocatable object files have a
186   // relocation on this word.  The following two vectors record the
187   // section and offset specified by these relocations.
188   std::vector<unsigned int> opd_ent_shndx_;
189   std::vector<Offset> opd_ent_off_;
190   // References made to this object's .opd section when running
191   // gc_process_relocs for another object, before the opd_ent vectors
192   // are valid for this object.
193   Access_from access_from_map_;
194   // Set at the start of gc_process_relocs, when we know opd_ent
195   // vectors are valid.  The flag could be made atomic and set in
196   // do_read_relocs with memory_order_release and then tested with
197   // memory_order_acquire, potentially resulting in fewer entries in
198   // access_from_map_.
199   bool opd_valid_;
200 };
201
202 template<int size, bool big_endian>
203 class Target_powerpc : public Sized_target<size, big_endian>
204 {
205  public:
206   typedef
207     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
208   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
209   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
210   static const Address invalid_address = static_cast<Address>(0) - 1;
211   // Offset of tp and dtp pointers from start of TLS block.
212   static const Address tp_offset = 0x7000;
213   static const Address dtp_offset = 0x8000;
214
215   Target_powerpc()
216     : Sized_target<size, big_endian>(&powerpc_info),
217       got_(NULL), plt_(NULL), glink_(NULL), rela_dyn_(NULL),
218       copy_relocs_(elfcpp::R_POWERPC_COPY),
219       dynbss_(NULL), tlsld_got_offset_(-1U)
220   {
221   }
222
223   // Process the relocations to determine unreferenced sections for
224   // garbage collection.
225   void
226   gc_process_relocs(Symbol_table* symtab,
227                     Layout* layout,
228                     Sized_relobj_file<size, big_endian>* object,
229                     unsigned int data_shndx,
230                     unsigned int sh_type,
231                     const unsigned char* prelocs,
232                     size_t reloc_count,
233                     Output_section* output_section,
234                     bool needs_special_offset_handling,
235                     size_t local_symbol_count,
236                     const unsigned char* plocal_symbols);
237
238   // Scan the relocations to look for symbol adjustments.
239   void
240   scan_relocs(Symbol_table* symtab,
241               Layout* layout,
242               Sized_relobj_file<size, big_endian>* object,
243               unsigned int data_shndx,
244               unsigned int sh_type,
245               const unsigned char* prelocs,
246               size_t reloc_count,
247               Output_section* output_section,
248               bool needs_special_offset_handling,
249               size_t local_symbol_count,
250               const unsigned char* plocal_symbols);
251
252   // Map input .toc section to output .got section.
253   const char*
254   do_output_section_name(const Relobj*, const char* name, size_t* plen) const
255   {
256     if (size == 64 && strcmp(name, ".toc") == 0)
257       {
258         *plen = 4;
259         return ".got";
260       }
261     return NULL;
262   }
263
264   // Finalize the sections.
265   void
266   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
267
268   // Return the value to use for a dynamic which requires special
269   // treatment.
270   uint64_t
271   do_dynsym_value(const Symbol*) const;
272
273   // Return the offset to use for the GOT_INDX'th got entry which is
274   // for a local tls symbol specified by OBJECT, SYMNDX.
275   int64_t
276   do_tls_offset_for_local(const Relobj* object,
277                           unsigned int symndx,
278                           unsigned int got_indx) const;
279
280   // Return the offset to use for the GOT_INDX'th got entry which is
281   // for global tls symbol GSYM.
282   int64_t
283   do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
284
285   // Relocate a section.
286   void
287   relocate_section(const Relocate_info<size, big_endian>*,
288                    unsigned int sh_type,
289                    const unsigned char* prelocs,
290                    size_t reloc_count,
291                    Output_section* output_section,
292                    bool needs_special_offset_handling,
293                    unsigned char* view,
294                    Address view_address,
295                    section_size_type view_size,
296                    const Reloc_symbol_changes*);
297
298   // Scan the relocs during a relocatable link.
299   void
300   scan_relocatable_relocs(Symbol_table* symtab,
301                           Layout* layout,
302                           Sized_relobj_file<size, big_endian>* object,
303                           unsigned int data_shndx,
304                           unsigned int sh_type,
305                           const unsigned char* prelocs,
306                           size_t reloc_count,
307                           Output_section* output_section,
308                           bool needs_special_offset_handling,
309                           size_t local_symbol_count,
310                           const unsigned char* plocal_symbols,
311                           Relocatable_relocs*);
312
313   // Emit relocations for a section.
314   void
315   relocate_relocs(const Relocate_info<size, big_endian>*,
316                   unsigned int sh_type,
317                   const unsigned char* prelocs,
318                   size_t reloc_count,
319                   Output_section* output_section,
320                   off_t offset_in_output_section,
321                   const Relocatable_relocs*,
322                   unsigned char*,
323                   Address view_address,
324                   section_size_type,
325                   unsigned char* reloc_view,
326                   section_size_type reloc_view_size);
327
328   // Return whether SYM is defined by the ABI.
329   bool
330   do_is_defined_by_abi(const Symbol* sym) const
331   {
332     return strcmp(sym->name(), "__tls_get_addr") == 0;
333   }
334
335   // Return the size of the GOT section.
336   section_size_type
337   got_size() const
338   {
339     gold_assert(this->got_ != NULL);
340     return this->got_->data_size();
341   }
342
343   // Get the PLT section.
344   const Output_data_plt_powerpc<size, big_endian>*
345   plt_section() const
346   {
347     gold_assert(this->plt_ != NULL);
348     return this->plt_;
349   }
350
351   // Get the .glink section.
352   const Output_data_glink<size, big_endian>*
353   glink_section() const
354   {
355     gold_assert(this->glink_ != NULL);
356     return this->glink_;
357   }
358
359   // Get the GOT section.
360   const Output_data_got_powerpc<size, big_endian>*
361   got_section() const
362   {
363     gold_assert(this->got_ != NULL);
364     return this->got_;
365   }
366
367   Object*
368   do_make_elf_object(const std::string&, Input_file*, off_t,
369                      const elfcpp::Ehdr<size, big_endian>&);
370
371   // Return the number of entries in the GOT.
372   unsigned int
373   got_entry_count() const
374   {
375     if (this->got_ == NULL)
376       return 0;
377     return this->got_size() / (size / 8);
378   }
379
380   // Return the number of entries in the PLT.
381   unsigned int
382   plt_entry_count() const;
383
384   // Return the offset of the first non-reserved PLT entry.
385   unsigned int
386   first_plt_entry_offset() const;
387
388   // Return the size of each PLT entry.
389   unsigned int
390   plt_entry_size() const;
391
392   // Add any special sections for this symbol to the gc work list.
393   // For powerpc64, this adds the code section of a function
394   // descriptor.
395   void
396   do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
397
398   // Handle target specific gc actions when adding a gc reference from
399   // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
400   // and DST_OFF.  For powerpc64, this adds a referenc to the code
401   // section of a function descriptor.
402   void
403   do_gc_add_reference(Symbol_table* symtab,
404                       Object* src_obj,
405                       unsigned int src_shndx,
406                       Object* dst_obj,
407                       unsigned int dst_shndx,
408                       Address dst_off) const;
409
410  private:
411
412   // The class which scans relocations.
413   class Scan
414   {
415   public:
416     Scan()
417       : issued_non_pic_error_(false)
418     { }
419
420     static inline int
421     get_reference_flags(unsigned int r_type);
422
423     inline void
424     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
425           Sized_relobj_file<size, big_endian>* object,
426           unsigned int data_shndx,
427           Output_section* output_section,
428           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
429           const elfcpp::Sym<size, big_endian>& lsym);
430
431     inline void
432     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
433            Sized_relobj_file<size, big_endian>* object,
434            unsigned int data_shndx,
435            Output_section* output_section,
436            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
437            Symbol* gsym);
438
439     inline bool
440     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
441                                         Target_powerpc* ,
442                                         Sized_relobj_file<size, big_endian>* ,
443                                         unsigned int ,
444                                         Output_section* ,
445                                         const elfcpp::Rela<size, big_endian>& ,
446                                         unsigned int ,
447                                         const elfcpp::Sym<size, big_endian>&)
448     { return false; }
449
450     inline bool
451     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
452                                          Target_powerpc* ,
453                                          Sized_relobj_file<size, big_endian>* ,
454                                          unsigned int ,
455                                          Output_section* ,
456                                          const elfcpp::Rela<size,
457                                                             big_endian>& ,
458                                          unsigned int , Symbol*)
459     { return false; }
460
461   private:
462     static void
463     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
464                             unsigned int r_type);
465
466     static void
467     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
468                              unsigned int r_type, Symbol*);
469
470     static void
471     generate_tls_call(Symbol_table* symtab, Layout* layout,
472                       Target_powerpc* target);
473
474     void
475     check_non_pic(Relobj*, unsigned int r_type);
476
477     // Whether we have issued an error about a non-PIC compilation.
478     bool issued_non_pic_error_;
479   };
480
481   Address
482   symval_for_branch(Address value, const Sized_symbol<size>* gsym,
483                     Powerpc_relobj<size, big_endian>* object,
484                     unsigned int *dest_shndx);
485
486   // The class which implements relocation.
487   class Relocate
488   {
489    public:
490     // Use 'at' branch hints when true, 'y' when false.
491     // FIXME maybe: set this with an option.
492     static const bool is_isa_v2 = true;
493
494     enum skip_tls
495     {
496       CALL_NOT_EXPECTED = 0,
497       CALL_EXPECTED = 1,
498       CALL_SKIP = 2
499     };
500
501     Relocate()
502       : call_tls_get_addr_(CALL_NOT_EXPECTED)
503     { }
504
505     ~Relocate()
506     {
507       if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
508         {
509           // FIXME: This needs to specify the location somehow.
510           gold_error(_("missing expected __tls_get_addr call"));
511         }
512     }
513
514     // Do a relocation.  Return false if the caller should not issue
515     // any warnings about this relocation.
516     inline bool
517     relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
518              Output_section*, size_t relnum,
519              const elfcpp::Rela<size, big_endian>&,
520              unsigned int r_type, const Sized_symbol<size>*,
521              const Symbol_value<size>*,
522              unsigned char*,
523              typename elfcpp::Elf_types<size>::Elf_Addr,
524              section_size_type);
525
526     // This is set if we should skip the next reloc, which should be a
527     // call to __tls_get_addr.
528     enum skip_tls call_tls_get_addr_;
529   };
530
531   // A class which returns the size required for a relocation type,
532   // used while scanning relocs during a relocatable link.
533   class Relocatable_size_for_reloc
534   {
535    public:
536     unsigned int
537     get_size_for_reloc(unsigned int, Relobj*)
538     {
539       gold_unreachable();
540       return 0;
541     }
542   };
543
544   // Optimize the TLS relocation type based on what we know about the
545   // symbol.  IS_FINAL is true if the final address of this symbol is
546   // known at link time.
547
548   tls::Tls_optimization
549   optimize_tls_gd(bool is_final)
550   {
551     // If we are generating a shared library, then we can't do anything
552     // in the linker.
553     if (parameters->options().shared())
554       return tls::TLSOPT_NONE;
555
556     if (!is_final)
557       return tls::TLSOPT_TO_IE;
558     return tls::TLSOPT_TO_LE;
559   }
560
561   tls::Tls_optimization
562   optimize_tls_ld()
563   {
564     if (parameters->options().shared())
565       return tls::TLSOPT_NONE;
566
567     return tls::TLSOPT_TO_LE;
568   }
569
570   tls::Tls_optimization
571   optimize_tls_ie(bool is_final)
572   {
573     if (!is_final || parameters->options().shared())
574       return tls::TLSOPT_NONE;
575
576     return tls::TLSOPT_TO_LE;
577   }
578
579   // Get the GOT section, creating it if necessary.
580   Output_data_got_powerpc<size, big_endian>*
581   got_section(Symbol_table*, Layout*);
582
583   // Create glink.
584   void
585   make_glink_section(Layout*);
586
587   // Create the PLT section.
588   void
589   make_plt_section(Layout*);
590
591   // Create a PLT entry for a global symbol.
592   void
593   make_plt_entry(Layout*, Symbol*,
594                  const elfcpp::Rela<size, big_endian>&,
595                  const Sized_relobj<size, big_endian>* object);
596
597   // Create a GOT entry for local dynamic __tls_get_addr.
598   unsigned int
599   tlsld_got_offset(Symbol_table* symtab, Layout* layout,
600                    Sized_relobj_file<size, big_endian>* object);
601
602   unsigned int
603   tlsld_got_offset() const
604   {
605     return this->tlsld_got_offset_;
606   }
607
608   // Get the dynamic reloc section, creating it if necessary.
609   Reloc_section*
610   rela_dyn_section(Layout*);
611
612   // Copy a relocation against a global symbol.
613   void
614   copy_reloc(Symbol_table* symtab, Layout* layout,
615              Sized_relobj_file<size, big_endian>* object,
616              unsigned int shndx, Output_section* output_section,
617              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
618   {
619     this->copy_relocs_.copy_reloc(symtab, layout,
620                                   symtab->get_sized_symbol<size>(sym),
621                                   object, shndx, output_section,
622                                   reloc, this->rela_dyn_section(layout));
623   }
624
625   // Information about this specific target which we pass to the
626   // general Target structure.
627   static Target::Target_info powerpc_info;
628
629   // The types of GOT entries needed for this platform.
630   // These values are exposed to the ABI in an incremental link.
631   // Do not renumber existing values without changing the version
632   // number of the .gnu_incremental_inputs section.
633   enum Got_type
634   {
635     GOT_TYPE_STANDARD,
636     GOT_TYPE_TLSGD,     // double entry for @got@tlsgd
637     GOT_TYPE_DTPREL,    // entry for @got@dtprel
638     GOT_TYPE_TPREL      // entry for @got@tprel
639   };
640
641   // The GOT output section.
642   Output_data_got_powerpc<size, big_endian>* got_;
643   // The PLT output section.
644   Output_data_plt_powerpc<size, big_endian>* plt_;
645   // The .glink output section.
646   Output_data_glink<size, big_endian>* glink_;
647   // The dynamic reloc output section.
648   Reloc_section* rela_dyn_;
649   // Relocs saved to avoid a COPY reloc.
650   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
651   // Space for variables copied with a COPY reloc.
652   Output_data_space* dynbss_;
653   // Offset of the GOT entry for local dynamic __tls_get_addr calls.
654   unsigned int tlsld_got_offset_;
655 };
656
657 template<>
658 Target::Target_info Target_powerpc<32, true>::powerpc_info =
659 {
660   32,                   // size
661   true,                 // is_big_endian
662   elfcpp::EM_PPC,       // machine_code
663   false,                // has_make_symbol
664   false,                // has_resolve
665   false,                // has_code_fill
666   true,                 // is_default_stack_executable
667   false,                // can_icf_inline_merge_sections
668   '\0',                 // wrap_char
669   "/usr/lib/ld.so.1",   // dynamic_linker
670   0x10000000,           // default_text_segment_address
671   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
672   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
673   false,                // isolate_execinstr
674   0,                    // rosegment_gap
675   elfcpp::SHN_UNDEF,    // small_common_shndx
676   elfcpp::SHN_UNDEF,    // large_common_shndx
677   0,                    // small_common_section_flags
678   0,                    // large_common_section_flags
679   NULL,                 // attributes_section
680   NULL                  // attributes_vendor
681 };
682
683 template<>
684 Target::Target_info Target_powerpc<32, false>::powerpc_info =
685 {
686   32,                   // size
687   false,                // is_big_endian
688   elfcpp::EM_PPC,       // machine_code
689   false,                // has_make_symbol
690   false,                // has_resolve
691   false,                // has_code_fill
692   true,                 // is_default_stack_executable
693   false,                // can_icf_inline_merge_sections
694   '\0',                 // wrap_char
695   "/usr/lib/ld.so.1",   // dynamic_linker
696   0x10000000,           // default_text_segment_address
697   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
698   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
699   false,                // isolate_execinstr
700   0,                    // rosegment_gap
701   elfcpp::SHN_UNDEF,    // small_common_shndx
702   elfcpp::SHN_UNDEF,    // large_common_shndx
703   0,                    // small_common_section_flags
704   0,                    // large_common_section_flags
705   NULL,                 // attributes_section
706   NULL                  // attributes_vendor
707 };
708
709 template<>
710 Target::Target_info Target_powerpc<64, true>::powerpc_info =
711 {
712   64,                   // size
713   true,                 // is_big_endian
714   elfcpp::EM_PPC64,     // machine_code
715   false,                // has_make_symbol
716   false,                // has_resolve
717   false,                // has_code_fill
718   true,                 // is_default_stack_executable
719   false,                // can_icf_inline_merge_sections
720   '\0',                 // wrap_char
721   "/usr/lib/ld.so.1",   // dynamic_linker
722   0x10000000,           // default_text_segment_address
723   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
724   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
725   false,                // isolate_execinstr
726   0,                    // rosegment_gap
727   elfcpp::SHN_UNDEF,    // small_common_shndx
728   elfcpp::SHN_UNDEF,    // large_common_shndx
729   0,                    // small_common_section_flags
730   0,                    // large_common_section_flags
731   NULL,                 // attributes_section
732   NULL                  // attributes_vendor
733 };
734
735 template<>
736 Target::Target_info Target_powerpc<64, false>::powerpc_info =
737 {
738   64,                   // size
739   false,                // is_big_endian
740   elfcpp::EM_PPC64,     // machine_code
741   false,                // has_make_symbol
742   false,                // has_resolve
743   false,                // has_code_fill
744   true,                 // is_default_stack_executable
745   false,                // can_icf_inline_merge_sections
746   '\0',                 // wrap_char
747   "/usr/lib/ld.so.1",   // dynamic_linker
748   0x10000000,           // default_text_segment_address
749   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
750   4 * 1024,             // common_pagesize (overridable by -z common-page-size)
751   false,                // isolate_execinstr
752   0,                    // rosegment_gap
753   elfcpp::SHN_UNDEF,    // small_common_shndx
754   elfcpp::SHN_UNDEF,    // large_common_shndx
755   0,                    // small_common_section_flags
756   0,                    // large_common_section_flags
757   NULL,                 // attributes_section
758   NULL                  // attributes_vendor
759 };
760
761 inline bool
762 is_branch_reloc(unsigned int r_type)
763 {
764   return (r_type == elfcpp::R_POWERPC_REL24
765           || r_type == elfcpp::R_PPC_PLTREL24
766           || r_type == elfcpp::R_PPC_LOCAL24PC
767           || r_type == elfcpp::R_POWERPC_REL14
768           || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
769           || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
770           || r_type == elfcpp::R_POWERPC_ADDR24
771           || r_type == elfcpp::R_POWERPC_ADDR14
772           || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
773           || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
774 }
775
776 // If INSN is an opcode that may be used with an @tls operand, return
777 // the transformed insn for TLS optimisation, otherwise return 0.  If
778 // REG is non-zero only match an insn with RB or RA equal to REG.
779 uint32_t
780 at_tls_transform(uint32_t insn, unsigned int reg)
781 {
782   if ((insn & (0x3f << 26)) != 31 << 26)
783     return 0;
784
785   unsigned int rtra;
786   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
787     rtra = insn & ((1 << 26) - (1 << 16));
788   else if (((insn >> 16) & 0x1f) == reg)
789     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
790   else
791     return 0;
792
793   if ((insn & (0x3ff << 1)) == 266 << 1)
794     // add -> addi
795     insn = 14 << 26;
796   else if ((insn & (0x1f << 1)) == 23 << 1
797            && ((insn & (0x1f << 6)) < 14 << 6
798                || ((insn & (0x1f << 6)) >= 16 << 6
799                    && (insn & (0x1f << 6)) < 24 << 6)))
800     // load and store indexed -> dform
801     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
802   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
803     // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
804     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
805   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
806     // lwax -> lwa
807     insn = (58 << 26) | 2;
808   else
809     return 0;
810   insn |= rtra;
811   return insn;
812 }
813
814 // Modified version of symtab.h class Symbol member
815 // Given a direct absolute or pc-relative static relocation against
816 // the global symbol, this function returns whether a dynamic relocation
817 // is needed.
818
819 template<int size>
820 bool
821 needs_dynamic_reloc(const Symbol* gsym, int flags)
822 {
823   // No dynamic relocations in a static link!
824   if (parameters->doing_static_link())
825     return false;
826
827   // A reference to an undefined symbol from an executable should be
828   // statically resolved to 0, and does not need a dynamic relocation.
829   // This matches gnu ld behavior.
830   if (gsym->is_undefined() && !parameters->options().shared())
831     return false;
832
833   // A reference to an absolute symbol does not need a dynamic relocation.
834   if (gsym->is_absolute())
835     return false;
836
837   // An absolute reference within a position-independent output file
838   // will need a dynamic relocation.
839   if ((flags & Symbol::ABSOLUTE_REF)
840       && parameters->options().output_is_position_independent())
841     return true;
842
843   // A function call that can branch to a local PLT entry does not need
844   // a dynamic relocation.
845   if ((flags & Symbol::FUNCTION_CALL) && gsym->has_plt_offset())
846     return false;
847
848   // A reference to any PLT entry in a non-position-independent executable
849   // does not need a dynamic relocation.
850   // Except due to having function descriptors on powerpc64 we don't define
851   // functions to their plt code in an executable, so this doesn't apply.
852   if (size == 32
853       && !parameters->options().output_is_position_independent()
854       && gsym->has_plt_offset())
855     return false;
856
857   // A reference to a symbol defined in a dynamic object or to a
858   // symbol that is preemptible will need a dynamic relocation.
859   if (gsym->is_from_dynobj()
860       || gsym->is_undefined()
861       || gsym->is_preemptible())
862     return true;
863
864   // For all other cases, return FALSE.
865   return false;
866 }
867
868 // Modified version of symtab.h class Symbol member
869 // Whether we should use the PLT offset associated with a symbol for
870 // a relocation.  FLAGS is a set of Reference_flags.
871
872 template<int size>
873 bool
874 use_plt_offset(const Symbol* gsym, int flags)
875 {
876   // If the symbol doesn't have a PLT offset, then naturally we
877   // don't want to use it.
878   if (!gsym->has_plt_offset())
879     return false;
880
881   // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
882   if (gsym->type() == elfcpp::STT_GNU_IFUNC)
883     return true;
884
885   // If we are going to generate a dynamic relocation, then we will
886   // wind up using that, so no need to use the PLT entry.
887   if (needs_dynamic_reloc<size>(gsym, flags))
888     return false;
889
890   // If the symbol is from a dynamic object, we need to use the PLT
891   // entry.
892   if (gsym->is_from_dynobj())
893     return true;
894
895   // If we are generating a shared object, and gsym symbol is
896   // undefined or preemptible, we need to use the PLT entry.
897   if (parameters->options().shared()
898       && (gsym->is_undefined() || gsym->is_preemptible()))
899     return true;
900
901   // If gsym is a call to a weak undefined symbol, we need to use
902   // the PLT entry; the symbol may be defined by a library loaded
903   // at runtime.
904   if ((flags & Symbol::FUNCTION_CALL) && gsym->is_weak_undefined())
905     return true;
906
907   // Otherwise we can use the regular definition.
908   return false;
909 }
910
911 template<int size, bool big_endian>
912 class Powerpc_relocate_functions
913 {
914 public:
915   enum Overflow_check
916   {
917     CHECK_NONE,
918     CHECK_SIGNED,
919     CHECK_BITFIELD
920   };
921
922   enum Status
923   {
924     STATUS_OK,
925     STATUS_OVERFLOW
926   };
927
928 private:
929   typedef Powerpc_relocate_functions<size, big_endian> This;
930   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
931
932   template<int valsize>
933   static inline bool
934   has_overflow_signed(Address value)
935   {
936     // limit = 1 << (valsize - 1) without shift count exceeding size of type
937     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
938     limit <<= ((valsize - 1) >> 1);
939     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
940     return value + limit > (limit << 1) - 1;
941   }
942
943   template<int valsize>
944   static inline bool
945   has_overflow_bitfield(Address value)
946   {
947     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
948     limit <<= ((valsize - 1) >> 1);
949     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
950     return value > (limit << 1) - 1 && value + limit > (limit << 1) - 1;
951   }
952
953   template<int valsize>
954   static inline Status
955   overflowed(Address value, Overflow_check overflow)
956   {
957     if (overflow == CHECK_SIGNED)
958       {
959         if (has_overflow_signed<valsize>(value))
960           return STATUS_OVERFLOW;
961       }
962     else if (overflow == CHECK_BITFIELD)
963       {
964         if (has_overflow_bitfield<valsize>(value))
965           return STATUS_OVERFLOW;
966       }
967     return STATUS_OK;
968   }
969
970   // Do a simple RELA relocation
971   template<int valsize>
972   static inline Status
973   rela(unsigned char* view, Address value, Overflow_check overflow)
974   {
975     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
976     Valtype* wv = reinterpret_cast<Valtype*>(view);
977     elfcpp::Swap<valsize, big_endian>::writeval(wv, value);
978     return overflowed<valsize>(value, overflow);
979   }
980
981   template<int valsize>
982   static inline Status
983   rela(unsigned char* view,
984        unsigned int right_shift,
985        typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
986        Address value,
987        Overflow_check overflow)
988   {
989     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
990     Valtype* wv = reinterpret_cast<Valtype*>(view);
991     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
992     Valtype reloc = value >> right_shift;
993     val &= ~dst_mask;
994     reloc &= dst_mask;
995     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
996     return overflowed<valsize>(value >> right_shift, overflow);
997   }
998
999   // Do a simple RELA relocation, unaligned.
1000   template<int valsize>
1001   static inline Status
1002   rela_ua(unsigned char* view, Address value, Overflow_check overflow)
1003   {
1004     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, value);
1005     return overflowed<valsize>(value, overflow);
1006   }
1007
1008   template<int valsize>
1009   static inline Status
1010   rela_ua(unsigned char* view,
1011           unsigned int right_shift,
1012           typename elfcpp::Valtype_base<valsize>::Valtype dst_mask,
1013           Address value,
1014           Overflow_check overflow)
1015   {
1016     typedef typename elfcpp::Swap_unaligned<valsize, big_endian>::Valtype
1017       Valtype;
1018     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(view);
1019     Valtype reloc = value >> right_shift;
1020     val &= ~dst_mask;
1021     reloc &= dst_mask;
1022     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(view, val | reloc);
1023     return overflowed<valsize>(value >> right_shift, overflow);
1024   }
1025
1026 public:
1027   // R_PPC64_ADDR64: (Symbol + Addend)
1028   static inline void
1029   addr64(unsigned char* view, Address value)
1030   { This::template rela<64>(view, value, CHECK_NONE); }
1031
1032   // R_PPC64_UADDR64: (Symbol + Addend) unaligned
1033   static inline void
1034   addr64_u(unsigned char* view, Address value)
1035   { This::template rela_ua<64>(view, value, CHECK_NONE); }
1036
1037   // R_POWERPC_ADDR32: (Symbol + Addend)
1038   static inline Status
1039   addr32(unsigned char* view, Address value, Overflow_check overflow)
1040   { return This::template rela<32>(view, value, overflow); }
1041
1042   // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
1043   static inline Status
1044   addr32_u(unsigned char* view, Address value, Overflow_check overflow)
1045   { return This::template rela_ua<32>(view, value, overflow); }
1046
1047   // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
1048   static inline Status
1049   addr24(unsigned char* view, Address value, Overflow_check overflow)
1050   {
1051     Status stat = This::template rela<32>(view, 0, 0x03fffffc, value, overflow);
1052     if (overflow != CHECK_NONE && (value & 3) != 0)
1053       stat = STATUS_OVERFLOW;
1054     return stat;
1055   }
1056
1057   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
1058   static inline Status
1059   addr16(unsigned char* view, Address value, Overflow_check overflow)
1060   { return This::template rela<16>(view, value, overflow); }
1061
1062   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
1063   static inline Status
1064   addr16_u(unsigned char* view, Address value, Overflow_check overflow)
1065   { return This::template rela_ua<16>(view, value, overflow); }
1066
1067   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
1068   static inline Status
1069   addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
1070   {
1071     Status stat = This::template rela<16>(view, 0, 0xfffc, value, overflow);
1072     if (overflow != CHECK_NONE && (value & 3) != 0)
1073       stat = STATUS_OVERFLOW;
1074     return stat;
1075   }
1076
1077   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
1078   static inline void
1079   addr16_hi(unsigned char* view, Address value)
1080   { This::template rela<16>(view, 16, 0xffff, value, CHECK_NONE); }
1081
1082   // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
1083   static inline void
1084   addr16_ha(unsigned char* view, Address value)
1085   { This::addr16_hi(view, value + 0x8000); }
1086
1087   // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
1088   static inline void
1089   addr16_hi2(unsigned char* view, Address value)
1090   { This::template rela<16>(view, 32, 0xffff, value, CHECK_NONE); }
1091
1092   // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
1093   static inline void
1094   addr16_ha2(unsigned char* view, Address value)
1095   { This::addr16_hi2(view, value + 0x8000); }
1096
1097   // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
1098   static inline void
1099   addr16_hi3(unsigned char* view, Address value)
1100   { This::template rela<16>(view, 48, 0xffff, value, CHECK_NONE); }
1101
1102   // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
1103   static inline void
1104   addr16_ha3(unsigned char* view, Address value)
1105   { This::addr16_hi3(view, value + 0x8000); }
1106
1107   // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
1108   static inline Status
1109   addr14(unsigned char* view, Address value, Overflow_check overflow)
1110   {
1111     Status stat = This::template rela<32>(view, 0, 0xfffc, value, overflow);
1112     if (overflow != CHECK_NONE && (value & 3) != 0)
1113       stat = STATUS_OVERFLOW;
1114     return stat;
1115   }
1116 };
1117
1118 // Stash away the index of .got2 or .opd in a relocatable object, if
1119 // such a section exists.
1120
1121 template<int size, bool big_endian>
1122 bool
1123 Powerpc_relobj<size, big_endian>::do_find_special_sections(
1124     Read_symbols_data* sd)
1125 {
1126   const unsigned char* const pshdrs = sd->section_headers->data();
1127   const unsigned char* namesu = sd->section_names->data();
1128   const char* names = reinterpret_cast<const char*>(namesu);
1129   section_size_type names_size = sd->section_names_size;
1130   const unsigned char* s;
1131
1132   s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
1133                       names, names_size, NULL);
1134   if (s != NULL)
1135     {
1136       unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
1137       this->special_ = ndx;
1138     }
1139   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
1140 }
1141
1142 // Examine .rela.opd to build info about function entry points.
1143
1144 template<int size, bool big_endian>
1145 void
1146 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
1147     size_t reloc_count,
1148     const unsigned char* prelocs,
1149     const unsigned char* plocal_syms)
1150 {
1151   if (size == 64)
1152     {
1153       typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
1154         Reltype;
1155       const int reloc_size
1156         = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
1157       const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1158
1159       for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
1160         {
1161           Reltype reloc(prelocs);
1162           typename elfcpp::Elf_types<size>::Elf_WXword r_info
1163             = reloc.get_r_info();
1164           unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
1165           if (r_type == elfcpp::R_PPC64_ADDR64)
1166             {
1167               unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
1168               typename elfcpp::Elf_types<size>::Elf_Addr value;
1169               bool is_ordinary;
1170               unsigned int shndx;
1171               if (r_sym < this->local_symbol_count())
1172                 {
1173                   typename elfcpp::Sym<size, big_endian>
1174                     lsym(plocal_syms + r_sym * sym_size);
1175                   shndx = lsym.get_st_shndx();
1176                   shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1177                   value = lsym.get_st_value();
1178                 }
1179               else
1180                 shndx = this->symbol_section_and_value(r_sym, &value,
1181                                                        &is_ordinary);
1182               this->set_opd_ent(reloc.get_r_offset(), shndx,
1183                                 value + reloc.get_r_addend());
1184             }
1185         }
1186     }
1187 }
1188
1189 template<int size, bool big_endian>
1190 void
1191 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
1192 {
1193   Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
1194   if (size == 64)
1195     {
1196       for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
1197            p != rd->relocs.end();
1198            ++p)
1199         {
1200           if (p->data_shndx == this->opd_shndx())
1201             {
1202               this->init_opd(this->section_size(this->opd_shndx()));
1203               this->scan_opd_relocs(p->reloc_count, p->contents->data(),
1204                                     rd->local_symbols->data());
1205               break;
1206             }
1207         }
1208     }
1209 }
1210
1211 // Set up PowerPC target specific relobj.
1212
1213 template<int size, bool big_endian>
1214 Object*
1215 Target_powerpc<size, big_endian>::do_make_elf_object(
1216     const std::string& name,
1217     Input_file* input_file,
1218     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1219 {
1220   int et = ehdr.get_e_type();
1221   // ET_EXEC files are valid input for --just-symbols/-R,
1222   // and we treat them as relocatable objects.
1223   if (et == elfcpp::ET_REL
1224       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
1225     {
1226       Powerpc_relobj<size, big_endian>* obj =
1227         new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
1228       obj->setup();
1229       return obj;
1230     }
1231   else if (et == elfcpp::ET_DYN)
1232     {
1233       Sized_dynobj<size, big_endian>* obj =
1234         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1235       obj->setup();
1236       return obj;
1237     }
1238   else
1239     {
1240       gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
1241       return NULL;
1242     }
1243 }
1244
1245 template<int size, bool big_endian>
1246 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
1247 {
1248 public:
1249   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
1250   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
1251
1252   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout)
1253     : Output_data_got<size, big_endian>(),
1254       symtab_(symtab), layout_(layout),
1255       header_ent_cnt_(size == 32 ? 3 : 1),
1256       header_index_(size == 32 ? 0x2000 : 0)
1257   {}
1258
1259   class Got_entry;
1260
1261   // Create a new GOT entry and return its offset.
1262   unsigned int
1263   add_got_entry(Got_entry got_entry)
1264   {
1265     this->reserve_ent();
1266     return Output_data_got<size, big_endian>::add_got_entry(got_entry);
1267   }
1268
1269   // Create a pair of new GOT entries and return the offset of the first.
1270   unsigned int
1271   add_got_entry_pair(Got_entry got_entry_1, Got_entry got_entry_2)
1272   {
1273     this->reserve_ent(2);
1274     return Output_data_got<size, big_endian>::add_got_entry_pair(got_entry_1,
1275                                                                  got_entry_2);
1276   }
1277
1278   unsigned int
1279   add_constant_pair(Valtype c1, Valtype c2)
1280   {
1281     this->reserve_ent(2);
1282     unsigned int got_offset = this->add_constant(c1);
1283     this->add_constant(c2);
1284     return got_offset;
1285   }
1286
1287   // Offset of _GLOBAL_OFFSET_TABLE_.
1288   unsigned int
1289   g_o_t() const
1290   {
1291     return this->got_offset(this->header_index_);
1292   }
1293
1294   // Offset of base used to access the GOT/TOC.
1295   // The got/toc pointer reg will be set to this value.
1296   typename elfcpp::Elf_types<size>::Elf_Off
1297   got_base_offset(const Powerpc_relobj<size, big_endian>* object) const
1298   {
1299     if (size == 32)
1300       return this->g_o_t();
1301     else
1302       return (this->output_section()->address()
1303               + object->toc_base_offset()
1304               - this->address());
1305   }
1306
1307   // Ensure our GOT has a header.
1308   void
1309   set_final_data_size()
1310   {
1311     if (this->header_ent_cnt_ != 0)
1312       this->make_header();
1313     Output_data_got<size, big_endian>::set_final_data_size();
1314   }
1315
1316   // First word of GOT header needs some values that are not
1317   // handled by Output_data_got so poke them in here.
1318   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
1319   void
1320   do_write(Output_file* of)
1321   {
1322     this->replace_constant(this->header_index_,
1323                            (size == 32
1324                             ? this->layout_->dynamic_section()->address()
1325                             : this->output_section()->address() + 0x8000));
1326
1327     Output_data_got<size, big_endian>::do_write(of);
1328   }
1329
1330 private:
1331   void
1332   reserve_ent(unsigned int cnt = 1)
1333   {
1334     if (this->header_ent_cnt_ == 0)
1335       return;
1336     if (this->num_entries() + cnt > this->header_index_)
1337       this->make_header();
1338   }
1339
1340   void
1341   make_header()
1342   {
1343     this->header_ent_cnt_ = 0;
1344     this->header_index_ = this->num_entries();
1345     if (size == 32)
1346       {
1347         Output_data_got<size, big_endian>::add_constant(0);
1348         Output_data_got<size, big_endian>::add_constant(0);
1349         Output_data_got<size, big_endian>::add_constant(0);
1350
1351         // Define _GLOBAL_OFFSET_TABLE_ at the header
1352         this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1353                                              Symbol_table::PREDEFINED,
1354                                              this, this->g_o_t(), 0,
1355                                              elfcpp::STT_OBJECT,
1356                                              elfcpp::STB_LOCAL,
1357                                              elfcpp::STV_HIDDEN,
1358                                              0, false, false);
1359       }
1360     else
1361       Output_data_got<size, big_endian>::add_constant(0);
1362   }
1363
1364   // Stashed pointers.
1365   Symbol_table* symtab_;
1366   Layout* layout_;
1367
1368   // GOT header size.
1369   unsigned int header_ent_cnt_;
1370   // GOT header index.
1371   unsigned int header_index_;
1372 };
1373
1374 // Get the GOT section, creating it if necessary.
1375
1376 template<int size, bool big_endian>
1377 Output_data_got_powerpc<size, big_endian>*
1378 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
1379                                               Layout* layout)
1380 {
1381   if (this->got_ == NULL)
1382     {
1383       gold_assert(symtab != NULL && layout != NULL);
1384
1385       this->got_
1386         = new Output_data_got_powerpc<size, big_endian>(symtab, layout);
1387
1388       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1389                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1390                                       this->got_, ORDER_DATA, false);
1391     }
1392
1393   return this->got_;
1394 }
1395
1396 // Get the dynamic reloc section, creating it if necessary.
1397
1398 template<int size, bool big_endian>
1399 typename Target_powerpc<size, big_endian>::Reloc_section*
1400 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
1401 {
1402   if (this->rela_dyn_ == NULL)
1403     {
1404       gold_assert(layout != NULL);
1405       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1406       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1407                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
1408                                       ORDER_DYNAMIC_RELOCS, false);
1409     }
1410   return this->rela_dyn_;
1411 }
1412
1413 // A class to handle the PLT data.
1414
1415 template<int size, bool big_endian>
1416 class Output_data_plt_powerpc : public Output_section_data_build
1417 {
1418  public:
1419   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1420                             size, big_endian> Reloc_section;
1421
1422   Output_data_plt_powerpc(Layout*, Target_powerpc<size, big_endian>*);
1423
1424   // Add an entry to the PLT.
1425   void
1426   add_entry(Symbol*);
1427
1428   // Return the .rela.plt section data.
1429   const Reloc_section*
1430   rel_plt() const
1431   {
1432     return this->rel_;
1433   }
1434
1435   // Return the number of PLT entries.
1436   unsigned int
1437   entry_count() const
1438   {
1439     return ((this->current_data_size() - initial_plt_entry_size)
1440             / plt_entry_size);
1441   }
1442
1443   // Return the offset of the first non-reserved PLT entry.
1444   static unsigned int
1445   first_plt_entry_offset()
1446   { return initial_plt_entry_size; }
1447
1448   // Return the size of a PLT entry.
1449   static unsigned int
1450   get_plt_entry_size()
1451   { return plt_entry_size; }
1452
1453  protected:
1454   void
1455   do_adjust_output_section(Output_section* os)
1456   {
1457     os->set_entsize(0);
1458   }
1459
1460   // Write to a map file.
1461   void
1462   do_print_to_mapfile(Mapfile* mapfile) const
1463   { mapfile->print_output_data(this, _("** PLT")); }
1464
1465  private:
1466   // The size of an entry in the PLT.
1467   static const int plt_entry_size = size == 32 ? 4 : 24;
1468   // The size of the first reserved entry.
1469   static const int initial_plt_entry_size = size == 32 ? 0 : 24;
1470
1471   // Write out the PLT data.
1472   void
1473   do_write(Output_file*);
1474
1475   // The reloc section.
1476   Reloc_section* rel_;
1477   // Allows access to .glink for do_write.
1478   Target_powerpc<size, big_endian>* targ_;
1479 };
1480
1481 // Create the PLT section.
1482
1483 template<int size, bool big_endian>
1484 Output_data_plt_powerpc<size, big_endian>::Output_data_plt_powerpc(
1485     Layout* layout,
1486     Target_powerpc<size, big_endian>* targ)
1487   : Output_section_data_build(size == 32 ? 4 : 8),
1488     targ_(targ)
1489 {
1490   this->rel_ = new Reloc_section(false);
1491   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1492                                   elfcpp::SHF_ALLOC, this->rel_,
1493                                   ORDER_DYNAMIC_PLT_RELOCS, false);
1494 }
1495
1496 // Add an entry to the PLT.
1497
1498 template<int size, bool big_endian>
1499 void
1500 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym)
1501 {
1502   if (!gsym->has_plt_offset())
1503     {
1504       off_t off = this->current_data_size();
1505
1506       if (off == 0)
1507         off += initial_plt_entry_size;
1508       gsym->set_plt_offset(off);
1509       gsym->set_needs_dynsym_entry();
1510       this->rel_->add_global(gsym, elfcpp::R_POWERPC_JMP_SLOT, this, off, 0);
1511       off += plt_entry_size;
1512       this->set_current_data_size(off);
1513     }
1514 }
1515
1516 static const uint32_t add_0_11_11       = 0x7c0b5a14;
1517 static const uint32_t add_3_3_2         = 0x7c631214;
1518 static const uint32_t add_3_3_13        = 0x7c636a14;
1519 static const uint32_t add_11_0_11       = 0x7d605a14;
1520 static const uint32_t add_12_2_11       = 0x7d825a14;
1521 static const uint32_t addi_11_11        = 0x396b0000;
1522 static const uint32_t addi_12_12        = 0x398c0000;
1523 static const uint32_t addi_2_2          = 0x38420000;
1524 static const uint32_t addi_3_2          = 0x38620000;
1525 static const uint32_t addi_3_3          = 0x38630000;
1526 static const uint32_t addis_0_2         = 0x3c020000;
1527 static const uint32_t addis_0_13        = 0x3c0d0000;
1528 static const uint32_t addis_11_11       = 0x3d6b0000;
1529 static const uint32_t addis_11_30       = 0x3d7e0000;
1530 static const uint32_t addis_12_12       = 0x3d8c0000;
1531 static const uint32_t addis_12_2        = 0x3d820000;
1532 static const uint32_t addis_3_2         = 0x3c620000;
1533 static const uint32_t addis_3_13        = 0x3c6d0000;
1534 static const uint32_t b                 = 0x48000000;
1535 static const uint32_t bcl_20_31         = 0x429f0005;
1536 static const uint32_t bctr              = 0x4e800420;
1537 static const uint32_t blrl              = 0x4e800021;
1538 static const uint32_t cror_15_15_15     = 0x4def7b82;
1539 static const uint32_t cror_31_31_31     = 0x4ffffb82;
1540 static const uint32_t ld_11_12          = 0xe96c0000;
1541 static const uint32_t ld_11_2           = 0xe9620000;
1542 static const uint32_t ld_2_1            = 0xe8410000;
1543 static const uint32_t ld_2_11           = 0xe84b0000;
1544 static const uint32_t ld_2_12           = 0xe84c0000;
1545 static const uint32_t ld_2_2            = 0xe8420000;
1546 static const uint32_t li_0_0            = 0x38000000;
1547 static const uint32_t lis_0_0           = 0x3c000000;
1548 static const uint32_t lis_11            = 0x3d600000;
1549 static const uint32_t lis_12            = 0x3d800000;
1550 static const uint32_t lwz_0_12          = 0x800c0000;
1551 static const uint32_t lwz_11_11         = 0x816b0000;
1552 static const uint32_t lwz_11_30         = 0x817e0000;
1553 static const uint32_t lwz_12_12         = 0x818c0000;
1554 static const uint32_t lwzu_0_12         = 0x840c0000;
1555 static const uint32_t mflr_0            = 0x7c0802a6;
1556 static const uint32_t mflr_11           = 0x7d6802a6;
1557 static const uint32_t mflr_12           = 0x7d8802a6;
1558 static const uint32_t mtctr_0           = 0x7c0903a6;
1559 static const uint32_t mtctr_11          = 0x7d6903a6;
1560 static const uint32_t mtlr_0            = 0x7c0803a6;
1561 static const uint32_t mtlr_12           = 0x7d8803a6;
1562 static const uint32_t nop               = 0x60000000;
1563 static const uint32_t ori_0_0_0         = 0x60000000;
1564 static const uint32_t std_2_1           = 0xf8410000;
1565 static const uint32_t sub_11_11_12      = 0x7d6c5850;
1566
1567 // Write out the PLT.
1568
1569 template<int size, bool big_endian>
1570 void
1571 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
1572 {
1573   if (size == 32)
1574     {
1575       const off_t offset = this->offset();
1576       const section_size_type oview_size
1577         = convert_to_section_size_type(this->data_size());
1578       unsigned char* const oview = of->get_output_view(offset, oview_size);
1579       unsigned char* pov = oview;
1580       unsigned char* endpov = oview + oview_size;
1581
1582       // The address the .glink branch table
1583       const Output_data_glink<size, big_endian>* glink
1584         = this->targ_->glink_section();
1585       elfcpp::Elf_types<32>::Elf_Addr branch_tab
1586         = glink->address() + glink->pltresolve();
1587
1588       while (pov < endpov)
1589         {
1590           elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
1591           pov += 4;
1592           branch_tab += 4;
1593         }
1594
1595       of->write_output_view(offset, oview_size, oview);
1596     }
1597 }
1598
1599 // Create the PLT section.
1600
1601 template<int size, bool big_endian>
1602 void
1603 Target_powerpc<size, big_endian>::make_plt_section(Layout* layout)
1604 {
1605   if (this->plt_ == NULL)
1606     {
1607       if (this->glink_ == NULL)
1608         make_glink_section(layout);
1609
1610       // Ensure that .rela.dyn always appears before .rela.plt  This is
1611       // necessary due to how, on PowerPC and some other targets, .rela.dyn
1612       // needs to include .rela.plt in it's range.
1613       this->rela_dyn_section(layout);
1614
1615       this->plt_ = new Output_data_plt_powerpc<size, big_endian>(layout, this);
1616       layout->add_output_section_data(".plt",
1617                                       (size == 32
1618                                        ? elfcpp::SHT_PROGBITS
1619                                        : elfcpp::SHT_NOBITS),
1620                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1621                                       this->plt_,
1622                                       (size == 32
1623                                        ? ORDER_SMALL_DATA
1624                                        : ORDER_SMALL_BSS),
1625                                       false);
1626     }
1627 }
1628
1629 // A class to handle .glink.
1630
1631 template<int size, bool big_endian>
1632 class Output_data_glink : public Output_section_data
1633 {
1634  public:
1635   static const int pltresolve_size = 16*4;
1636
1637   Output_data_glink(Target_powerpc<size, big_endian>*);
1638
1639   // Add an entry
1640   void
1641   add_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1642             const Sized_relobj<size, big_endian>*);
1643
1644   unsigned int
1645   find_entry(const Symbol*, const elfcpp::Rela<size, big_endian>&,
1646              const Sized_relobj<size, big_endian>*) const;
1647
1648   unsigned int
1649   glink_entry_size() const
1650   {
1651     if (size == 32)
1652       return 4 * 4;
1653     else
1654       // FIXME: We should be using multiple glink sections for
1655       // stubs to support > 33M applications.
1656       return 8 * 4;
1657   }
1658
1659   off_t
1660   pltresolve() const
1661   {
1662     return this->pltresolve_;
1663   }
1664
1665  protected:
1666   // Write to a map file.
1667   void
1668   do_print_to_mapfile(Mapfile* mapfile) const
1669   { mapfile->print_output_data(this, _("** glink")); }
1670
1671  private:
1672   void
1673   set_final_data_size();
1674
1675   // Write out .glink
1676   void
1677   do_write(Output_file*);
1678
1679   class Glink_sym_ent
1680   {
1681   public:
1682     Glink_sym_ent(const Symbol* sym,
1683                   const elfcpp::Rela<size, big_endian>& reloc,
1684                   const Sized_relobj<size, big_endian>* object)
1685       : sym_(sym), addend_(0), object_(0)
1686     {
1687       if (size != 32)
1688         this->addend_ = reloc.get_r_addend();
1689       else if (parameters->options().output_is_position_independent()
1690                && (elfcpp::elf_r_type<size>(reloc.get_r_info())
1691                    == elfcpp::R_PPC_PLTREL24))
1692         {
1693           this->addend_ = reloc.get_r_addend();
1694           if (this->addend_ != 0)
1695             this->object_ = object;
1696         }
1697     }
1698
1699     bool operator==(const Glink_sym_ent& that) const
1700     {
1701       return (this->sym_ == that.sym_
1702               && this->object_ == that.object_
1703               && this->addend_ == that.addend_);
1704     }
1705
1706     const Symbol* sym_;
1707     unsigned int addend_;
1708     const Sized_relobj<size, big_endian>* object_;
1709   };
1710
1711   class Glink_sym_ent_hash
1712   {
1713   public:
1714     size_t operator()(const Glink_sym_ent& ent) const
1715     {
1716       return (reinterpret_cast<uintptr_t>(ent.sym_)
1717               ^ reinterpret_cast<uintptr_t>(ent.object_)
1718               ^ ent.addend_);
1719     }
1720   };
1721
1722   // Map sym/object/addend to index.
1723   typedef Unordered_map<Glink_sym_ent, unsigned int,
1724                         Glink_sym_ent_hash> Glink_entries;
1725   Glink_entries glink_entries_;
1726
1727   // Offset of pltresolve stub (actually, branch table for 32-bit)
1728   off_t pltresolve_;
1729
1730   // Allows access to .got and .plt for do_write.
1731   Target_powerpc<size, big_endian>* targ_;
1732 };
1733
1734 // Create the glink section.
1735
1736 template<int size, bool big_endian>
1737 Output_data_glink<size, big_endian>::Output_data_glink(
1738     Target_powerpc<size, big_endian>* targ)
1739   : Output_section_data(16),
1740     pltresolve_(0), targ_(targ)
1741 {
1742 }
1743
1744 // Add an entry to glink, if we do not already have one for this
1745 // sym/object/addend combo.
1746
1747 template<int size, bool big_endian>
1748 void
1749 Output_data_glink<size, big_endian>::add_entry(
1750     const Symbol* gsym,
1751     const elfcpp::Rela<size, big_endian>& reloc,
1752     const Sized_relobj<size, big_endian>* object)
1753 {
1754   Glink_sym_ent ent(gsym, reloc, object);
1755   unsigned int indx = this->glink_entries_.size();
1756   this->glink_entries_.insert(std::make_pair(ent, indx));
1757 }
1758
1759 template<int size, bool big_endian>
1760 unsigned int
1761 Output_data_glink<size, big_endian>::find_entry(
1762     const Symbol* gsym,
1763     const elfcpp::Rela<size, big_endian>& reloc,
1764     const Sized_relobj<size, big_endian>* object) const
1765 {
1766   Glink_sym_ent ent(gsym, reloc, object);
1767   typename Glink_entries::const_iterator p = this->glink_entries_.find(ent);
1768   gold_assert(p != this->glink_entries_.end());
1769   return p->second;
1770 }
1771
1772 template<int size, bool big_endian>
1773 void
1774 Output_data_glink<size, big_endian>::set_final_data_size()
1775 {
1776   unsigned int count = this->glink_entries_.size();
1777   off_t total = count;
1778
1779   if (count != 0)
1780     {
1781       if (size == 32)
1782         {
1783           total *= 16;
1784           this->pltresolve_ = total;
1785
1786           // space for branch table
1787           total += 4 * (count - 1);
1788
1789           total += -total & 15;
1790           total += this->pltresolve_size;
1791         }
1792       else
1793         {
1794           total *= 32;
1795           this->pltresolve_ = total;
1796           total += this->pltresolve_size;
1797
1798           // space for branch table
1799           total += 8 * count;
1800           if (count > 0x8000)
1801             total += 4 * (count - 0x8000);
1802         }
1803     }
1804
1805   this->set_data_size(total);
1806 }
1807
1808 static inline uint32_t
1809 l(uint32_t a)
1810 {
1811   return a & 0xffff;
1812 }
1813
1814 static inline uint32_t
1815 hi(uint32_t a)
1816 {
1817   return l(a >> 16);
1818 }
1819
1820 static inline uint32_t
1821 ha(uint32_t a)
1822 {
1823   return hi(a + 0x8000);
1824 }
1825
1826 template<bool big_endian>
1827 static inline void
1828 write_insn(unsigned char* p, uint32_t v)
1829 {
1830   elfcpp::Swap<32, big_endian>::writeval(p, v);
1831 }
1832
1833 // Write out .glink.
1834
1835 template<int size, bool big_endian>
1836 void
1837 Output_data_glink<size, big_endian>::do_write(Output_file* of)
1838 {
1839   const off_t off = this->offset();
1840   const section_size_type oview_size =
1841     convert_to_section_size_type(this->data_size());
1842   unsigned char* const oview = of->get_output_view(off, oview_size);
1843   unsigned char* p;
1844
1845   // The base address of the .plt section.
1846   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1847   Address plt_base = this->targ_->plt_section()->address();
1848
1849   const Output_data_got_powerpc<size, big_endian>* got
1850     = this->targ_->got_section();
1851
1852   if (size == 64)
1853     {
1854       Address got_os_addr = got->output_section()->address();
1855
1856       // Write out call stubs.
1857       typename Glink_entries::const_iterator g;
1858       for (g = this->glink_entries_.begin();
1859            g != this->glink_entries_.end();
1860            ++g)
1861         {
1862           Address plt_addr = plt_base + g->first.sym_->plt_offset();
1863           const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1864             <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1865           Address got_addr = got_os_addr + ppcobj->toc_base_offset();
1866           Address pltoff = plt_addr - got_addr;
1867
1868           if (pltoff + 0x80008000 > 0xffffffff || (pltoff & 7) != 0)
1869             gold_error(_("%s: linkage table error against `%s'"),
1870                        g->first.object_->name().c_str(),
1871                        g->first.sym_->demangled_name().c_str());
1872
1873           p = oview + g->second * this->glink_entry_size();
1874           if (ha(pltoff) != 0)
1875             {
1876               write_insn<big_endian>(p, addis_12_2 + ha(pltoff)),       p += 4;
1877               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1878               write_insn<big_endian>(p, ld_11_12 + l(pltoff)),          p += 4;
1879               if (ha(pltoff + 16) != ha(pltoff))
1880                 {
1881                   write_insn<big_endian>(p, addi_12_12 + l(pltoff)),    p += 4;
1882                   pltoff = 0;
1883                 }
1884               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1885               write_insn<big_endian>(p, ld_2_12 + l(pltoff + 8)),       p += 4;
1886               write_insn<big_endian>(p, ld_11_12 + l(pltoff + 16)),     p += 4;
1887               write_insn<big_endian>(p, bctr),                          p += 4;
1888             }
1889           else
1890             {
1891               write_insn<big_endian>(p, std_2_1 + 40),                  p += 4;
1892               write_insn<big_endian>(p, ld_11_2 + l(pltoff)),           p += 4;
1893               if (ha(pltoff + 16) != ha(pltoff))
1894                 {
1895                   write_insn<big_endian>(p, addi_2_2 + l(pltoff)),      p += 4;
1896                   pltoff = 0;
1897                 }
1898               write_insn<big_endian>(p, mtctr_11),                      p += 4;
1899               write_insn<big_endian>(p, ld_11_2 + l(pltoff + 16)),      p += 4;
1900               write_insn<big_endian>(p, ld_2_2 + l(pltoff + 8)),        p += 4;
1901               write_insn<big_endian>(p, bctr),                          p += 4;
1902             }
1903         }
1904
1905       // Write pltresolve stub.
1906       p = oview + this->pltresolve_;
1907       Address after_bcl = this->address() + this->pltresolve_ + 16;
1908       Address pltoff = plt_base - after_bcl;
1909
1910       elfcpp::Swap<64, big_endian>::writeval(p, pltoff),        p += 8;
1911
1912       write_insn<big_endian>(p, mflr_12),                       p += 4;
1913       write_insn<big_endian>(p, bcl_20_31),                     p += 4;
1914       write_insn<big_endian>(p, mflr_11),                       p += 4;
1915       write_insn<big_endian>(p, ld_2_11 + l(-16)),              p += 4;
1916       write_insn<big_endian>(p, mtlr_12),                       p += 4;
1917       write_insn<big_endian>(p, add_12_2_11),                   p += 4;
1918       write_insn<big_endian>(p, ld_11_12 + 0),                  p += 4;
1919       write_insn<big_endian>(p, ld_2_12 + 8),                   p += 4;
1920       write_insn<big_endian>(p, mtctr_11),                      p += 4;
1921       write_insn<big_endian>(p, ld_11_12 + 16),                 p += 4;
1922       write_insn<big_endian>(p, bctr),                          p += 4;
1923       while (p < oview + this->pltresolve_ + this->pltresolve_size)
1924         write_insn<big_endian>(p, nop), p += 4;
1925
1926       // Write lazy link call stubs.
1927       uint32_t indx = 0;
1928       while (p < oview + oview_size)
1929         {
1930           if (indx < 0x8000)
1931             {
1932               write_insn<big_endian>(p, li_0_0 + indx),                 p += 4;
1933             }
1934           else
1935             {
1936               write_insn<big_endian>(p, lis_0_0 + hi(indx)),            p += 4;
1937               write_insn<big_endian>(p, ori_0_0_0 + l(indx)),           p += 4;
1938             }
1939           uint32_t branch_off = this->pltresolve_ + 8 - (p - oview);
1940           write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),      p += 4;
1941           indx++;
1942         }
1943     }
1944   else
1945     {
1946       // The address of _GLOBAL_OFFSET_TABLE_.
1947       Address g_o_t = got->address() + got->g_o_t();
1948
1949       // Write out call stubs.
1950       typename Glink_entries::const_iterator g;
1951       for (g = this->glink_entries_.begin();
1952            g != this->glink_entries_.end();
1953            ++g)
1954         {
1955           Address plt_addr = plt_base + g->first.sym_->plt_offset();
1956           Address got_addr;
1957           const Address invalid_address = static_cast<Address>(-1);
1958
1959           p = oview + g->second * this->glink_entry_size();
1960           if (parameters->options().output_is_position_independent())
1961             {
1962               const Powerpc_relobj<size, big_endian>* object = static_cast
1963                 <const Powerpc_relobj<size, big_endian>*>(g->first.object_);
1964               if (object != NULL)
1965                 {
1966                   unsigned int got2 = object->got2_shndx();
1967                   got_addr = g->first.object_->get_output_section_offset(got2);
1968                   gold_assert(got_addr != invalid_address);
1969                   got_addr += (g->first.object_->output_section(got2)->address()
1970                                + g->first.addend_);
1971                 }
1972               else
1973                 got_addr = g_o_t;
1974
1975               Address pltoff = plt_addr - got_addr;
1976               if (ha(pltoff) == 0)
1977                 {
1978                   write_insn<big_endian>(p +  0, lwz_11_30 + l(pltoff));
1979                   write_insn<big_endian>(p +  4, mtctr_11);
1980                   write_insn<big_endian>(p +  8, bctr);
1981                 }
1982               else
1983                 {
1984                   write_insn<big_endian>(p +  0, addis_11_30 + ha(pltoff));
1985                   write_insn<big_endian>(p +  4, lwz_11_11 + l(pltoff));
1986                   write_insn<big_endian>(p +  8, mtctr_11);
1987                   write_insn<big_endian>(p + 12, bctr);
1988                 }
1989             }
1990           else
1991             {
1992               write_insn<big_endian>(p +  0, lis_11 + ha(plt_addr));
1993               write_insn<big_endian>(p +  4, lwz_11_11 + l(plt_addr));
1994               write_insn<big_endian>(p +  8, mtctr_11);
1995               write_insn<big_endian>(p + 12, bctr);
1996             }
1997         }
1998
1999       // Write out pltresolve branch table.
2000       p = oview + this->pltresolve_;
2001       unsigned int the_end = oview_size - this->pltresolve_size;
2002       unsigned char* end_p = oview + the_end;
2003       while (p < end_p - 8 * 4)
2004         write_insn<big_endian>(p, b + end_p - p), p += 4;
2005       while (p < end_p)
2006         write_insn<big_endian>(p, nop), p += 4;
2007
2008       // Write out pltresolve call stub.
2009       if (parameters->options().output_is_position_independent())
2010         {
2011           Address res0_off = this->pltresolve_;
2012           Address after_bcl_off = the_end + 12;
2013           Address bcl_res0 = after_bcl_off - res0_off;
2014
2015           write_insn<big_endian>(p +  0, addis_11_11 + ha(bcl_res0));
2016           write_insn<big_endian>(p +  4, mflr_0);
2017           write_insn<big_endian>(p +  8, bcl_20_31);
2018           write_insn<big_endian>(p + 12, addi_11_11 + l(bcl_res0));
2019           write_insn<big_endian>(p + 16, mflr_12);
2020           write_insn<big_endian>(p + 20, mtlr_0);
2021           write_insn<big_endian>(p + 24, sub_11_11_12);
2022
2023           Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
2024
2025           write_insn<big_endian>(p + 28, addis_12_12 + ha(got_bcl));
2026           if (ha(got_bcl) == ha(got_bcl + 4))
2027             {
2028               write_insn<big_endian>(p + 32, lwz_0_12 + l(got_bcl));
2029               write_insn<big_endian>(p + 36, lwz_12_12 + l(got_bcl + 4));
2030             }
2031           else
2032             {
2033               write_insn<big_endian>(p + 32, lwzu_0_12 + l(got_bcl));
2034               write_insn<big_endian>(p + 36, lwz_12_12 + 4);
2035             }
2036           write_insn<big_endian>(p + 40, mtctr_0);
2037           write_insn<big_endian>(p + 44, add_0_11_11);
2038           write_insn<big_endian>(p + 48, add_11_0_11);
2039           write_insn<big_endian>(p + 52, bctr);
2040           write_insn<big_endian>(p + 56, nop);
2041           write_insn<big_endian>(p + 60, nop);
2042         }
2043       else
2044         {
2045           Address res0 = this->pltresolve_ + this->address();
2046
2047           write_insn<big_endian>(p + 0, lis_12 + ha(g_o_t + 4));
2048           write_insn<big_endian>(p + 4, addis_11_11 + ha(-res0));
2049           if (ha(g_o_t + 4) == ha(g_o_t + 8))
2050             write_insn<big_endian>(p + 8, lwz_0_12 + l(g_o_t + 4));
2051           else
2052             write_insn<big_endian>(p + 8, lwzu_0_12 + l(g_o_t + 4));
2053           write_insn<big_endian>(p + 12, addi_11_11 + l(-res0));
2054           write_insn<big_endian>(p + 16, mtctr_0);
2055           write_insn<big_endian>(p + 20, add_0_11_11);
2056           if (ha(g_o_t + 4) == ha(g_o_t + 8))
2057             write_insn<big_endian>(p + 24, lwz_12_12 + l(g_o_t + 8));
2058           else
2059             write_insn<big_endian>(p + 24, lwz_12_12 + 4);
2060           write_insn<big_endian>(p + 28, add_11_0_11);
2061           write_insn<big_endian>(p + 32, bctr);
2062           write_insn<big_endian>(p + 36, nop);
2063           write_insn<big_endian>(p + 40, nop);
2064           write_insn<big_endian>(p + 44, nop);
2065           write_insn<big_endian>(p + 48, nop);
2066           write_insn<big_endian>(p + 52, nop);
2067           write_insn<big_endian>(p + 56, nop);
2068           write_insn<big_endian>(p + 60, nop);
2069         }
2070       p += 64;
2071     }
2072
2073   of->write_output_view(off, oview_size, oview);
2074 }
2075
2076 // Create the glink section.
2077
2078 template<int size, bool big_endian>
2079 void
2080 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
2081 {
2082   if (this->glink_ == NULL)
2083     {
2084       this->glink_ = new Output_data_glink<size, big_endian>(this);
2085       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
2086                                       elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
2087                                       this->glink_, ORDER_TEXT, false);
2088     }
2089 }
2090
2091 // Create a PLT entry for a global symbol.
2092
2093 template<int size, bool big_endian>
2094 void
2095 Target_powerpc<size, big_endian>::make_plt_entry(
2096     Layout* layout,
2097     Symbol* gsym,
2098     const elfcpp::Rela<size, big_endian>& reloc,
2099     const Sized_relobj<size, big_endian>* object)
2100 {
2101   if (this->plt_ == NULL)
2102     this->make_plt_section(layout);
2103
2104   this->plt_->add_entry(gsym);
2105
2106   this->glink_->add_entry(gsym, reloc, object);
2107 }
2108
2109 // Return the number of entries in the PLT.
2110
2111 template<int size, bool big_endian>
2112 unsigned int
2113 Target_powerpc<size, big_endian>::plt_entry_count() const
2114 {
2115   if (this->plt_ == NULL)
2116     return 0;
2117   return this->plt_->entry_count();
2118 }
2119
2120 // Return the offset of the first non-reserved PLT entry.
2121
2122 template<int size, bool big_endian>
2123 unsigned int
2124 Target_powerpc<size, big_endian>::first_plt_entry_offset() const
2125 {
2126   return Output_data_plt_powerpc<size, big_endian>::first_plt_entry_offset();
2127 }
2128
2129 // Return the size of each PLT entry.
2130
2131 template<int size, bool big_endian>
2132 unsigned int
2133 Target_powerpc<size, big_endian>::plt_entry_size() const
2134 {
2135   return Output_data_plt_powerpc<size, big_endian>::get_plt_entry_size();
2136 }
2137
2138 // Create a GOT entry for local dynamic __tls_get_addr calls.
2139
2140 template<int size, bool big_endian>
2141 unsigned int
2142 Target_powerpc<size, big_endian>::tlsld_got_offset(
2143     Symbol_table* symtab,
2144     Layout* layout,
2145     Sized_relobj_file<size, big_endian>* object)
2146 {
2147   if (this->tlsld_got_offset_ == -1U)
2148     {
2149       gold_assert(symtab != NULL && layout != NULL && object != NULL);
2150       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2151       Output_data_got_powerpc<size, big_endian>* got
2152         = this->got_section(symtab, layout);
2153       unsigned int got_offset = got->add_constant_pair(0, 0);
2154       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
2155                           got_offset, 0);
2156       this->tlsld_got_offset_ = got_offset;
2157     }
2158   return this->tlsld_got_offset_;
2159 }
2160
2161 // Get the Reference_flags for a particular relocation.
2162
2163 template<int size, bool big_endian>
2164 int
2165 Target_powerpc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2166 {
2167   switch (r_type)
2168     {
2169     case elfcpp::R_POWERPC_NONE:
2170     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2171     case elfcpp::R_POWERPC_GNU_VTENTRY:
2172     case elfcpp::R_PPC64_TOC:
2173       // No symbol reference.
2174       return 0;
2175
2176     case elfcpp::R_PPC64_ADDR64:
2177     case elfcpp::R_PPC64_UADDR64:
2178     case elfcpp::R_POWERPC_ADDR32:
2179     case elfcpp::R_POWERPC_UADDR32:
2180     case elfcpp::R_POWERPC_ADDR16:
2181     case elfcpp::R_POWERPC_UADDR16:
2182     case elfcpp::R_POWERPC_ADDR16_LO:
2183     case elfcpp::R_POWERPC_ADDR16_HI:
2184     case elfcpp::R_POWERPC_ADDR16_HA:
2185       return Symbol::ABSOLUTE_REF;
2186
2187     case elfcpp::R_POWERPC_ADDR24:
2188     case elfcpp::R_POWERPC_ADDR14:
2189     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2190     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2191       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2192
2193     case elfcpp::R_POWERPC_REL32:
2194     case elfcpp::R_PPC_LOCAL24PC:
2195     case elfcpp::R_POWERPC_REL16:
2196     case elfcpp::R_POWERPC_REL16_LO:
2197     case elfcpp::R_POWERPC_REL16_HI:
2198     case elfcpp::R_POWERPC_REL16_HA:
2199       return Symbol::RELATIVE_REF;
2200
2201     case elfcpp::R_POWERPC_REL24:
2202     case elfcpp::R_PPC_PLTREL24:
2203     case elfcpp::R_POWERPC_REL14:
2204     case elfcpp::R_POWERPC_REL14_BRTAKEN:
2205     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2206       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2207
2208     case elfcpp::R_POWERPC_GOT16:
2209     case elfcpp::R_POWERPC_GOT16_LO:
2210     case elfcpp::R_POWERPC_GOT16_HI:
2211     case elfcpp::R_POWERPC_GOT16_HA:
2212     case elfcpp::R_PPC64_TOC16:
2213     case elfcpp::R_PPC64_TOC16_LO:
2214     case elfcpp::R_PPC64_TOC16_HI:
2215     case elfcpp::R_PPC64_TOC16_HA:
2216     case elfcpp::R_PPC64_TOC16_DS:
2217     case elfcpp::R_PPC64_TOC16_LO_DS:
2218       // Absolute in GOT.
2219       return Symbol::ABSOLUTE_REF;
2220
2221     case elfcpp::R_POWERPC_GOT_TPREL16:
2222     case elfcpp::R_POWERPC_TLS:
2223       return Symbol::TLS_REF;
2224
2225     case elfcpp::R_POWERPC_COPY:
2226     case elfcpp::R_POWERPC_GLOB_DAT:
2227     case elfcpp::R_POWERPC_JMP_SLOT:
2228     case elfcpp::R_POWERPC_RELATIVE:
2229     case elfcpp::R_POWERPC_DTPMOD:
2230     default:
2231       // Not expected.  We will give an error later.
2232       return 0;
2233     }
2234 }
2235
2236 // Report an unsupported relocation against a local symbol.
2237
2238 template<int size, bool big_endian>
2239 void
2240 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
2241     Sized_relobj_file<size, big_endian>* object,
2242     unsigned int r_type)
2243 {
2244   gold_error(_("%s: unsupported reloc %u against local symbol"),
2245              object->name().c_str(), r_type);
2246 }
2247
2248 // We are about to emit a dynamic relocation of type R_TYPE.  If the
2249 // dynamic linker does not support it, issue an error.
2250
2251 template<int size, bool big_endian>
2252 void
2253 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
2254                                                       unsigned int r_type)
2255 {
2256   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
2257
2258   // These are the relocation types supported by glibc for both 32-bit
2259   // and 64-bit powerpc.
2260   switch (r_type)
2261     {
2262     case elfcpp::R_POWERPC_NONE:
2263     case elfcpp::R_POWERPC_RELATIVE:
2264     case elfcpp::R_POWERPC_GLOB_DAT:
2265     case elfcpp::R_POWERPC_DTPMOD:
2266     case elfcpp::R_POWERPC_DTPREL:
2267     case elfcpp::R_POWERPC_TPREL:
2268     case elfcpp::R_POWERPC_JMP_SLOT:
2269     case elfcpp::R_POWERPC_COPY:
2270     case elfcpp::R_POWERPC_IRELATIVE:
2271     case elfcpp::R_POWERPC_ADDR32:
2272     case elfcpp::R_POWERPC_UADDR32:
2273     case elfcpp::R_POWERPC_ADDR24:
2274     case elfcpp::R_POWERPC_ADDR16:
2275     case elfcpp::R_POWERPC_UADDR16:
2276     case elfcpp::R_POWERPC_ADDR16_LO:
2277     case elfcpp::R_POWERPC_ADDR16_HI:
2278     case elfcpp::R_POWERPC_ADDR16_HA:
2279     case elfcpp::R_POWERPC_ADDR14:
2280     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2281     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2282     case elfcpp::R_POWERPC_REL32:
2283     case elfcpp::R_POWERPC_REL24:
2284     case elfcpp::R_POWERPC_TPREL16:
2285     case elfcpp::R_POWERPC_TPREL16_LO:
2286     case elfcpp::R_POWERPC_TPREL16_HI:
2287     case elfcpp::R_POWERPC_TPREL16_HA:
2288       return;
2289
2290     default:
2291       break;
2292     }
2293
2294   if (size == 64)
2295     {
2296       switch (r_type)
2297         {
2298           // These are the relocation types supported only on 64-bit.
2299         case elfcpp::R_PPC64_ADDR64:
2300         case elfcpp::R_PPC64_UADDR64:
2301         case elfcpp::R_PPC64_JMP_IREL:
2302         case elfcpp::R_PPC64_ADDR16_DS:
2303         case elfcpp::R_PPC64_ADDR16_LO_DS:
2304         case elfcpp::R_PPC64_ADDR16_HIGHER:
2305         case elfcpp::R_PPC64_ADDR16_HIGHEST:
2306         case elfcpp::R_PPC64_ADDR16_HIGHERA:
2307         case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2308         case elfcpp::R_PPC64_REL64:
2309         case elfcpp::R_POWERPC_ADDR30:
2310         case elfcpp::R_PPC64_TPREL16_DS:
2311         case elfcpp::R_PPC64_TPREL16_LO_DS:
2312         case elfcpp::R_PPC64_TPREL16_HIGHER:
2313         case elfcpp::R_PPC64_TPREL16_HIGHEST:
2314         case elfcpp::R_PPC64_TPREL16_HIGHERA:
2315         case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2316           return;
2317
2318         default:
2319           break;
2320         }
2321     }
2322   else
2323     {
2324       switch (r_type)
2325         {
2326           // These are the relocation types supported only on 32-bit.
2327           // ??? glibc ld.so doesn't need to support these.
2328         case elfcpp::R_POWERPC_DTPREL16:
2329         case elfcpp::R_POWERPC_DTPREL16_LO:
2330         case elfcpp::R_POWERPC_DTPREL16_HI:
2331         case elfcpp::R_POWERPC_DTPREL16_HA:
2332           return;
2333
2334         default:
2335           break;
2336         }
2337     }
2338
2339   // This prevents us from issuing more than one error per reloc
2340   // section.  But we can still wind up issuing more than one
2341   // error per object file.
2342   if (this->issued_non_pic_error_)
2343     return;
2344   gold_assert(parameters->options().output_is_position_independent());
2345   object->error(_("requires unsupported dynamic reloc; "
2346                   "recompile with -fPIC"));
2347   this->issued_non_pic_error_ = true;
2348   return;
2349 }
2350
2351 // Scan a relocation for a local symbol.
2352
2353 template<int size, bool big_endian>
2354 inline void
2355 Target_powerpc<size, big_endian>::Scan::local(
2356     Symbol_table* symtab,
2357     Layout* layout,
2358     Target_powerpc<size, big_endian>* target,
2359     Sized_relobj_file<size, big_endian>* object,
2360     unsigned int data_shndx,
2361     Output_section* output_section,
2362     const elfcpp::Rela<size, big_endian>& reloc,
2363     unsigned int r_type,
2364     const elfcpp::Sym<size, big_endian>& /* lsym */)
2365 {
2366   Powerpc_relobj<size, big_endian>* ppc_object
2367     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2368
2369   switch (r_type)
2370     {
2371     case elfcpp::R_POWERPC_NONE:
2372     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2373     case elfcpp::R_POWERPC_GNU_VTENTRY:
2374     case elfcpp::R_PPC64_TOCSAVE:
2375     case elfcpp::R_PPC_EMB_MRKREF:
2376     case elfcpp::R_POWERPC_TLS:
2377       break;
2378
2379     case elfcpp::R_PPC64_TOC:
2380       {
2381         Output_data_got_powerpc<size, big_endian>* got
2382           = target->got_section(symtab, layout);
2383         if (parameters->options().output_is_position_independent())
2384           {
2385             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2386             rela_dyn->add_output_section_relative(got->output_section(),
2387                                                   elfcpp::R_POWERPC_RELATIVE,
2388                                                   output_section,
2389                                                   object, data_shndx,
2390                                                   reloc.get_r_offset(),
2391                                                   ppc_object->toc_base_offset());
2392           }
2393       }
2394       break;
2395
2396     case elfcpp::R_PPC64_ADDR64:
2397     case elfcpp::R_PPC64_UADDR64:
2398     case elfcpp::R_POWERPC_ADDR32:
2399     case elfcpp::R_POWERPC_UADDR32:
2400     case elfcpp::R_POWERPC_ADDR24:
2401     case elfcpp::R_POWERPC_ADDR16:
2402     case elfcpp::R_POWERPC_ADDR16_LO:
2403     case elfcpp::R_POWERPC_ADDR16_HI:
2404     case elfcpp::R_POWERPC_ADDR16_HA:
2405     case elfcpp::R_POWERPC_UADDR16:
2406     case elfcpp::R_PPC64_ADDR16_HIGHER:
2407     case elfcpp::R_PPC64_ADDR16_HIGHERA:
2408     case elfcpp::R_PPC64_ADDR16_HIGHEST:
2409     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2410     case elfcpp::R_PPC64_ADDR16_DS:
2411     case elfcpp::R_PPC64_ADDR16_LO_DS:
2412     case elfcpp::R_POWERPC_ADDR14:
2413     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2414     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2415       // If building a shared library (or a position-independent
2416       // executable), we need to create a dynamic relocation for
2417       // this location.
2418       if (parameters->options().output_is_position_independent())
2419         {
2420           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2421
2422           if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2423               || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2424             {
2425               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2426               rela_dyn->add_local_relative(object, r_sym,
2427                                            elfcpp::R_POWERPC_RELATIVE,
2428                                            output_section, data_shndx,
2429                                            reloc.get_r_offset(),
2430                                            reloc.get_r_addend(), false);
2431             }
2432           else
2433             {
2434               check_non_pic(object, r_type);
2435               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2436               rela_dyn->add_local(object, r_sym, r_type, output_section,
2437                                   data_shndx, reloc.get_r_offset(),
2438                                   reloc.get_r_addend());
2439             }
2440         }
2441       break;
2442
2443     case elfcpp::R_PPC64_REL64:
2444     case elfcpp::R_POWERPC_REL32:
2445     case elfcpp::R_POWERPC_REL24:
2446     case elfcpp::R_PPC_LOCAL24PC:
2447     case elfcpp::R_POWERPC_REL16:
2448     case elfcpp::R_POWERPC_REL16_LO:
2449     case elfcpp::R_POWERPC_REL16_HI:
2450     case elfcpp::R_POWERPC_REL16_HA:
2451     case elfcpp::R_POWERPC_REL14:
2452     case elfcpp::R_POWERPC_REL14_BRTAKEN:
2453     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2454     case elfcpp::R_POWERPC_SECTOFF:
2455     case elfcpp::R_POWERPC_TPREL16:
2456     case elfcpp::R_POWERPC_DTPREL16:
2457     case elfcpp::R_POWERPC_SECTOFF_LO:
2458     case elfcpp::R_POWERPC_TPREL16_LO:
2459     case elfcpp::R_POWERPC_DTPREL16_LO:
2460     case elfcpp::R_POWERPC_SECTOFF_HI:
2461     case elfcpp::R_POWERPC_TPREL16_HI:
2462     case elfcpp::R_POWERPC_DTPREL16_HI:
2463     case elfcpp::R_POWERPC_SECTOFF_HA:
2464     case elfcpp::R_POWERPC_TPREL16_HA:
2465     case elfcpp::R_POWERPC_DTPREL16_HA:
2466     case elfcpp::R_PPC64_DTPREL16_HIGHER:
2467     case elfcpp::R_PPC64_TPREL16_HIGHER:
2468     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2469     case elfcpp::R_PPC64_TPREL16_HIGHERA:
2470     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2471     case elfcpp::R_PPC64_TPREL16_HIGHEST:
2472     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2473     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2474     case elfcpp::R_PPC64_TPREL16_DS:
2475     case elfcpp::R_PPC64_TPREL16_LO_DS:
2476     case elfcpp::R_PPC64_DTPREL16_DS:
2477     case elfcpp::R_PPC64_DTPREL16_LO_DS:
2478     case elfcpp::R_PPC64_SECTOFF_DS:
2479     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2480     case elfcpp::R_PPC64_TLSGD:
2481     case elfcpp::R_PPC64_TLSLD:
2482       break;
2483
2484     case elfcpp::R_POWERPC_GOT16:
2485     case elfcpp::R_POWERPC_GOT16_LO:
2486     case elfcpp::R_POWERPC_GOT16_HI:
2487     case elfcpp::R_POWERPC_GOT16_HA:
2488     case elfcpp::R_PPC64_GOT16_DS:
2489     case elfcpp::R_PPC64_GOT16_LO_DS:
2490       {
2491         // The symbol requires a GOT entry.
2492         Output_data_got_powerpc<size, big_endian>* got
2493           = target->got_section(symtab, layout);
2494         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2495
2496         // If we are generating a shared object, we need to add a
2497         // dynamic relocation for this symbol's GOT entry.
2498         if (parameters->options().output_is_position_independent())
2499           {
2500             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2501               {
2502                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2503                 unsigned int off;
2504
2505                 off = got->add_constant(0);
2506                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2507                 rela_dyn->add_local_relative(object, r_sym,
2508                                              elfcpp::R_POWERPC_RELATIVE,
2509                                              got, off, 0, false);
2510               }
2511           }
2512         else
2513           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2514       }
2515       break;
2516
2517     case elfcpp::R_PPC64_TOC16:
2518     case elfcpp::R_PPC64_TOC16_LO:
2519     case elfcpp::R_PPC64_TOC16_HI:
2520     case elfcpp::R_PPC64_TOC16_HA:
2521     case elfcpp::R_PPC64_TOC16_DS:
2522     case elfcpp::R_PPC64_TOC16_LO_DS:
2523       // We need a GOT section.
2524       target->got_section(symtab, layout);
2525       break;
2526
2527     case elfcpp::R_POWERPC_GOT_TLSGD16:
2528     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2529     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2530     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2531       {
2532         const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
2533         if (tls_type == tls::TLSOPT_NONE)
2534           {
2535             Output_data_got_powerpc<size, big_endian>* got
2536               = target->got_section(symtab, layout);
2537             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2538             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2539             got->add_local_tls_pair(object, r_sym, GOT_TYPE_TLSGD,
2540                                     rela_dyn, elfcpp::R_POWERPC_DTPMOD);
2541           }
2542         else if (tls_type == tls::TLSOPT_TO_LE)
2543           {
2544             // no GOT relocs needed for Local Exec.
2545           }
2546         else
2547           gold_unreachable();
2548       }
2549       break;
2550
2551     case elfcpp::R_POWERPC_GOT_TLSLD16:
2552     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2553     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2554     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2555       {
2556         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2557         if (tls_type == tls::TLSOPT_NONE)
2558           target->tlsld_got_offset(symtab, layout, object);
2559         else if (tls_type == tls::TLSOPT_TO_LE)
2560           {
2561             // no GOT relocs needed for Local Exec.
2562             if (parameters->options().emit_relocs())
2563               {
2564                 Output_section* os = layout->tls_segment()->first_section();
2565                 gold_assert(os != NULL);
2566                 os->set_needs_symtab_index();
2567               }
2568           }
2569         else
2570           gold_unreachable();
2571       }
2572       break;
2573
2574     case elfcpp::R_POWERPC_GOT_DTPREL16:
2575     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2576     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2577     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2578       {
2579         Output_data_got_powerpc<size, big_endian>* got
2580           = target->got_section(symtab, layout);
2581         unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2582         got->add_local_tls(object, r_sym, GOT_TYPE_DTPREL);
2583       }
2584       break;
2585
2586     case elfcpp::R_POWERPC_GOT_TPREL16:
2587     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2588     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2589     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2590       {
2591         const tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
2592         if (tls_type == tls::TLSOPT_NONE)
2593           {
2594             Output_data_got_powerpc<size, big_endian>* got
2595               = target->got_section(symtab, layout);
2596             unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2597             got->add_local_tls(object, r_sym, GOT_TYPE_TPREL);
2598           }
2599         else if (tls_type == tls::TLSOPT_TO_LE)
2600           {
2601             // no GOT relocs needed for Local Exec.
2602           }
2603         else
2604           gold_unreachable();
2605       }
2606       break;
2607
2608     default:
2609       unsupported_reloc_local(object, r_type);
2610       break;
2611     }
2612 }
2613
2614 // Report an unsupported relocation against a global symbol.
2615
2616 template<int size, bool big_endian>
2617 void
2618 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
2619     Sized_relobj_file<size, big_endian>* object,
2620     unsigned int r_type,
2621     Symbol* gsym)
2622 {
2623   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2624              object->name().c_str(), r_type, gsym->demangled_name().c_str());
2625 }
2626
2627 // Scan a relocation for a global symbol.
2628
2629 template<int size, bool big_endian>
2630 inline void
2631 Target_powerpc<size, big_endian>::Scan::global(
2632     Symbol_table* symtab,
2633     Layout* layout,
2634     Target_powerpc<size, big_endian>* target,
2635     Sized_relobj_file<size, big_endian>* object,
2636     unsigned int data_shndx,
2637     Output_section* output_section,
2638     const elfcpp::Rela<size, big_endian>& reloc,
2639     unsigned int r_type,
2640     Symbol* gsym)
2641 {
2642   Powerpc_relobj<size, big_endian>* ppc_object
2643     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2644
2645   switch (r_type)
2646     {
2647     case elfcpp::R_POWERPC_NONE:
2648     case elfcpp::R_POWERPC_GNU_VTINHERIT:
2649     case elfcpp::R_POWERPC_GNU_VTENTRY:
2650     case elfcpp::R_PPC_LOCAL24PC:
2651     case elfcpp::R_PPC_EMB_MRKREF:
2652     case elfcpp::R_POWERPC_TLS:
2653       break;
2654
2655     case elfcpp::R_PPC64_TOC:
2656       {
2657         Output_data_got_powerpc<size, big_endian>* got
2658           = target->got_section(symtab, layout);
2659         if (parameters->options().output_is_position_independent())
2660           {
2661             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2662             Powerpc_relobj<size, big_endian>* symobj = ppc_object;
2663             if (data_shndx != ppc_object->opd_shndx())
2664               symobj = static_cast
2665                 <Powerpc_relobj<size, big_endian>*>(gsym->object());
2666             rela_dyn->add_output_section_relative(got->output_section(),
2667                                                   elfcpp::R_POWERPC_RELATIVE,
2668                                                   output_section,
2669                                                   object, data_shndx,
2670                                                   reloc.get_r_offset(),
2671                                                   symobj->toc_base_offset());
2672           }
2673       }
2674       break;
2675
2676     case elfcpp::R_PPC64_ADDR64:
2677     case elfcpp::R_PPC64_UADDR64:
2678     case elfcpp::R_POWERPC_ADDR32:
2679     case elfcpp::R_POWERPC_UADDR32:
2680     case elfcpp::R_POWERPC_ADDR24:
2681     case elfcpp::R_POWERPC_ADDR16:
2682     case elfcpp::R_POWERPC_ADDR16_LO:
2683     case elfcpp::R_POWERPC_ADDR16_HI:
2684     case elfcpp::R_POWERPC_ADDR16_HA:
2685     case elfcpp::R_POWERPC_UADDR16:
2686     case elfcpp::R_PPC64_ADDR16_HIGHER:
2687     case elfcpp::R_PPC64_ADDR16_HIGHERA:
2688     case elfcpp::R_PPC64_ADDR16_HIGHEST:
2689     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
2690     case elfcpp::R_PPC64_ADDR16_DS:
2691     case elfcpp::R_PPC64_ADDR16_LO_DS:
2692     case elfcpp::R_POWERPC_ADDR14:
2693     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
2694     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
2695       {
2696         // Make a PLT entry if necessary.
2697         if (gsym->needs_plt_entry())
2698           {
2699             target->make_plt_entry(layout, gsym, reloc, 0);
2700             // Since this is not a PC-relative relocation, we may be
2701             // taking the address of a function. In that case we need to
2702             // set the entry in the dynamic symbol table to the address of
2703             // the PLT entry.
2704             if (size == 32
2705                 && gsym->is_from_dynobj() && !parameters->options().shared())
2706               gsym->set_needs_dynsym_value();
2707           }
2708         // Make a dynamic relocation if necessary.
2709         if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2710           {
2711             if (gsym->may_need_copy_reloc())
2712               {
2713                 target->copy_reloc(symtab, layout, object,
2714                                    data_shndx, output_section, gsym, reloc);
2715               }
2716             else if (((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
2717                       || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
2718                      && (gsym->can_use_relative_reloc(false)
2719                          || data_shndx == ppc_object->opd_shndx()))
2720               {
2721                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2722                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2723                                               output_section, object,
2724                                               data_shndx, reloc.get_r_offset(),
2725                                               reloc.get_r_addend(), false);
2726               }
2727             else
2728               {
2729                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2730                 check_non_pic(object, r_type);
2731                 rela_dyn->add_global(gsym, r_type, output_section,
2732                                      object, data_shndx,
2733                                      reloc.get_r_offset(),
2734                                      reloc.get_r_addend());
2735               }
2736           }
2737       }
2738       break;
2739
2740     case elfcpp::R_PPC_PLTREL24:
2741     case elfcpp::R_POWERPC_REL24:
2742       if (gsym->needs_plt_entry()
2743           || (!gsym->final_value_is_known()
2744               && (gsym->is_undefined()
2745                   || gsym->is_from_dynobj()
2746                   || gsym->is_preemptible())))
2747         target->make_plt_entry(layout, gsym, reloc, object);
2748       // Fall thru
2749
2750     case elfcpp::R_PPC64_REL64:
2751     case elfcpp::R_POWERPC_REL32:
2752       // Make a dynamic relocation if necessary.
2753       if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type)))
2754         {
2755           if (gsym->may_need_copy_reloc())
2756             {
2757               target->copy_reloc(symtab, layout, object,
2758                                  data_shndx, output_section, gsym,
2759                                  reloc);
2760             }
2761           else
2762             {
2763               Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2764               check_non_pic(object, r_type);
2765               rela_dyn->add_global(gsym, r_type, output_section, object,
2766                                    data_shndx, reloc.get_r_offset(),
2767                                    reloc.get_r_addend());
2768             }
2769         }
2770       break;
2771
2772     case elfcpp::R_POWERPC_REL16:
2773     case elfcpp::R_POWERPC_REL16_LO:
2774     case elfcpp::R_POWERPC_REL16_HI:
2775     case elfcpp::R_POWERPC_REL16_HA:
2776     case elfcpp::R_POWERPC_REL14:
2777     case elfcpp::R_POWERPC_REL14_BRTAKEN:
2778     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
2779     case elfcpp::R_POWERPC_SECTOFF:
2780     case elfcpp::R_POWERPC_TPREL16:
2781     case elfcpp::R_POWERPC_DTPREL16:
2782     case elfcpp::R_POWERPC_SECTOFF_LO:
2783     case elfcpp::R_POWERPC_TPREL16_LO:
2784     case elfcpp::R_POWERPC_DTPREL16_LO:
2785     case elfcpp::R_POWERPC_SECTOFF_HI:
2786     case elfcpp::R_POWERPC_TPREL16_HI:
2787     case elfcpp::R_POWERPC_DTPREL16_HI:
2788     case elfcpp::R_POWERPC_SECTOFF_HA:
2789     case elfcpp::R_POWERPC_TPREL16_HA:
2790     case elfcpp::R_POWERPC_DTPREL16_HA:
2791     case elfcpp::R_PPC64_DTPREL16_HIGHER:
2792     case elfcpp::R_PPC64_TPREL16_HIGHER:
2793     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
2794     case elfcpp::R_PPC64_TPREL16_HIGHERA:
2795     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
2796     case elfcpp::R_PPC64_TPREL16_HIGHEST:
2797     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
2798     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
2799     case elfcpp::R_PPC64_TPREL16_DS:
2800     case elfcpp::R_PPC64_TPREL16_LO_DS:
2801     case elfcpp::R_PPC64_DTPREL16_DS:
2802     case elfcpp::R_PPC64_DTPREL16_LO_DS:
2803     case elfcpp::R_PPC64_SECTOFF_DS:
2804     case elfcpp::R_PPC64_SECTOFF_LO_DS:
2805     case elfcpp::R_PPC64_TLSGD:
2806     case elfcpp::R_PPC64_TLSLD:
2807       break;
2808
2809     case elfcpp::R_POWERPC_GOT16:
2810     case elfcpp::R_POWERPC_GOT16_LO:
2811     case elfcpp::R_POWERPC_GOT16_HI:
2812     case elfcpp::R_POWERPC_GOT16_HA:
2813     case elfcpp::R_PPC64_GOT16_DS:
2814     case elfcpp::R_PPC64_GOT16_LO_DS:
2815       {
2816         // The symbol requires a GOT entry.
2817         Output_data_got_powerpc<size, big_endian>* got;
2818
2819         got = target->got_section(symtab, layout);
2820         if (gsym->final_value_is_known())
2821           got->add_global(gsym, GOT_TYPE_STANDARD);
2822         else
2823           {
2824             // If this symbol is not fully resolved, we need to add a
2825             // dynamic relocation for it.
2826             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2827             if (gsym->is_from_dynobj()
2828                 || gsym->is_undefined()
2829                 || gsym->is_preemptible())
2830               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2831                                        elfcpp::R_POWERPC_GLOB_DAT);
2832             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2833               {
2834                 unsigned int off = got->add_constant(0);
2835
2836                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2837                 rela_dyn->add_global_relative(gsym, elfcpp::R_POWERPC_RELATIVE,
2838                                               got, off, 0, false);
2839               }
2840           }
2841       }
2842       break;
2843
2844     case elfcpp::R_PPC64_TOC16:
2845     case elfcpp::R_PPC64_TOC16_LO:
2846     case elfcpp::R_PPC64_TOC16_HI:
2847     case elfcpp::R_PPC64_TOC16_HA:
2848     case elfcpp::R_PPC64_TOC16_DS:
2849     case elfcpp::R_PPC64_TOC16_LO_DS:
2850       // We need a GOT section.
2851       target->got_section(symtab, layout);
2852       break;
2853
2854     case elfcpp::R_POWERPC_GOT_TLSGD16:
2855     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
2856     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
2857     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
2858       {
2859         const bool final = gsym->final_value_is_known();
2860         const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
2861         if (tls_type == tls::TLSOPT_NONE)
2862           {
2863             Output_data_got_powerpc<size, big_endian>* got
2864               = target->got_section(symtab, layout);
2865             got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
2866                                           target->rela_dyn_section(layout),
2867                                           elfcpp::R_POWERPC_DTPMOD,
2868                                           elfcpp::R_POWERPC_DTPREL);
2869           }
2870         else if (tls_type == tls::TLSOPT_TO_IE)
2871           {
2872             Output_data_got_powerpc<size, big_endian>* got
2873               = target->got_section(symtab, layout);
2874             got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2875                                      target->rela_dyn_section(layout),
2876                                      elfcpp::R_POWERPC_TPREL);
2877           }
2878         else if (tls_type == tls::TLSOPT_TO_LE)
2879           {
2880             // no GOT relocs needed for Local Exec.
2881           }
2882         else
2883           gold_unreachable();
2884       }
2885       break;
2886
2887     case elfcpp::R_POWERPC_GOT_TLSLD16:
2888     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
2889     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
2890     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
2891       {
2892         const tls::Tls_optimization tls_type = target->optimize_tls_ld();
2893         if (tls_type == tls::TLSOPT_NONE)
2894           target->tlsld_got_offset(symtab, layout, object);
2895         else if (tls_type == tls::TLSOPT_TO_LE)
2896           {
2897             // no GOT relocs needed for Local Exec.
2898             if (parameters->options().emit_relocs())
2899               {
2900                 Output_section* os = layout->tls_segment()->first_section();
2901                 gold_assert(os != NULL);
2902                 os->set_needs_symtab_index();
2903               }
2904           }
2905         else
2906           gold_unreachable();
2907       }
2908       break;
2909
2910     case elfcpp::R_POWERPC_GOT_DTPREL16:
2911     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
2912     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
2913     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
2914       {
2915         Output_data_got_powerpc<size, big_endian>* got
2916           = target->got_section(symtab, layout);
2917         if (!gsym->final_value_is_known()
2918             && (gsym->is_from_dynobj()
2919                 || gsym->is_undefined()
2920                 || gsym->is_preemptible()))
2921           got->add_global_with_rel(gsym, GOT_TYPE_DTPREL,
2922                                    target->rela_dyn_section(layout),
2923                                    elfcpp::R_POWERPC_DTPREL);
2924         else
2925           got->add_global_tls(gsym, GOT_TYPE_DTPREL);
2926       }
2927       break;
2928
2929     case elfcpp::R_POWERPC_GOT_TPREL16:
2930     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
2931     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
2932     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
2933       {
2934         const bool final = gsym->final_value_is_known();
2935         const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
2936         if (tls_type == tls::TLSOPT_NONE)
2937           {
2938             Output_data_got_powerpc<size, big_endian>* got
2939               = target->got_section(symtab, layout);
2940             if (!gsym->final_value_is_known()
2941                 && (gsym->is_from_dynobj()
2942                     || gsym->is_undefined()
2943                     || gsym->is_preemptible()))
2944               got->add_global_with_rel(gsym, GOT_TYPE_TPREL,
2945                                        target->rela_dyn_section(layout),
2946                                        elfcpp::R_POWERPC_TPREL);
2947             else
2948               got->add_global_tls(gsym, GOT_TYPE_TPREL);
2949           }
2950         else if (tls_type == tls::TLSOPT_TO_LE)
2951           {
2952             // no GOT relocs needed for Local Exec.
2953           }
2954         else
2955           gold_unreachable();
2956       }
2957       break;
2958
2959     default:
2960       unsupported_reloc_global(object, r_type, gsym);
2961       break;
2962     }
2963 }
2964
2965 // Process relocations for gc.
2966
2967 template<int size, bool big_endian>
2968 void
2969 Target_powerpc<size, big_endian>::gc_process_relocs(
2970     Symbol_table* symtab,
2971     Layout* layout,
2972     Sized_relobj_file<size, big_endian>* object,
2973     unsigned int data_shndx,
2974     unsigned int,
2975     const unsigned char* prelocs,
2976     size_t reloc_count,
2977     Output_section* output_section,
2978     bool needs_special_offset_handling,
2979     size_t local_symbol_count,
2980     const unsigned char* plocal_symbols)
2981 {
2982   typedef Target_powerpc<size, big_endian> Powerpc;
2983   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
2984   Powerpc_relobj<size, big_endian>* ppc_object
2985     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
2986   if (size == 64)
2987     ppc_object->set_opd_valid();
2988   if (size == 64 && data_shndx == ppc_object->opd_shndx())
2989     {
2990       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
2991       for (p = ppc_object->access_from_map()->begin();
2992            p != ppc_object->access_from_map()->end();
2993            ++p)
2994         {
2995           Address dst_off = p->first;
2996           unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
2997           typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
2998           for (s = p->second.begin(); s != p->second.end(); ++s)
2999             {
3000               Object* src_obj = s->first;
3001               unsigned int src_indx = s->second;
3002               symtab->gc()->add_reference(src_obj, src_indx,
3003                                           ppc_object, dst_indx);
3004             }
3005           p->second.clear();
3006         }
3007       ppc_object->access_from_map()->clear();
3008       // Don't look at .opd relocs as .opd will reference everything.
3009       return;
3010     }
3011
3012   gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
3013                           typename Target_powerpc::Relocatable_size_for_reloc>(
3014     symtab,
3015     layout,
3016     this,
3017     object,
3018     data_shndx,
3019     prelocs,
3020     reloc_count,
3021     output_section,
3022     needs_special_offset_handling,
3023     local_symbol_count,
3024     plocal_symbols);
3025 }
3026
3027 // Handle target specific gc actions when adding a gc reference from
3028 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
3029 // and DST_OFF.  For powerpc64, this adds a referenc to the code
3030 // section of a function descriptor.
3031
3032 template<int size, bool big_endian>
3033 void
3034 Target_powerpc<size, big_endian>::do_gc_add_reference(
3035     Symbol_table* symtab,
3036     Object* src_obj,
3037     unsigned int src_shndx,
3038     Object* dst_obj,
3039     unsigned int dst_shndx,
3040     Address dst_off) const
3041 {
3042   Powerpc_relobj<size, big_endian>* ppc_object
3043     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
3044   if (size == 64 && dst_shndx == ppc_object->opd_shndx())
3045     {
3046       if (ppc_object->opd_valid())
3047         {
3048           dst_shndx = ppc_object->get_opd_ent(dst_off);
3049           symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
3050         }
3051       else
3052         {
3053           // If we haven't run scan_opd_relocs, we must delay
3054           // processing this function descriptor reference.
3055           ppc_object->add_reference(src_obj, src_shndx, dst_off);
3056         }
3057     }
3058 }
3059
3060 // Add any special sections for this symbol to the gc work list.
3061 // For powerpc64, this adds the code section of a function
3062 // descriptor.
3063
3064 template<int size, bool big_endian>
3065 void
3066 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
3067     Symbol_table* symtab,
3068     Symbol* sym) const
3069 {
3070   if (size == 64)
3071     {
3072       Powerpc_relobj<size, big_endian>* ppc_object
3073         = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
3074       bool is_ordinary;
3075       unsigned int shndx = sym->shndx(&is_ordinary);
3076       if (is_ordinary && shndx == ppc_object->opd_shndx())
3077         {
3078           Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
3079           Address dst_off = gsym->value();
3080           unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
3081           symtab->gc()->worklist().push(Section_id(ppc_object, dst_indx));
3082         }
3083     }
3084 }
3085
3086 // Scan relocations for a section.
3087
3088 template<int size, bool big_endian>
3089 void
3090 Target_powerpc<size, big_endian>::scan_relocs(
3091     Symbol_table* symtab,
3092     Layout* layout,
3093     Sized_relobj_file<size, big_endian>* object,
3094     unsigned int data_shndx,
3095     unsigned int sh_type,
3096     const unsigned char* prelocs,
3097     size_t reloc_count,
3098     Output_section* output_section,
3099     bool needs_special_offset_handling,
3100     size_t local_symbol_count,
3101     const unsigned char* plocal_symbols)
3102 {
3103   typedef Target_powerpc<size, big_endian> Powerpc;
3104   typedef typename Target_powerpc<size, big_endian>::Scan Scan;
3105
3106   if (sh_type == elfcpp::SHT_REL)
3107     {
3108       gold_error(_("%s: unsupported REL reloc section"),
3109                  object->name().c_str());
3110       return;
3111     }
3112
3113   if (size == 32)
3114     {
3115       static Output_data_space* sdata;
3116
3117       // Define _SDA_BASE_ at the start of the .sdata section.
3118       if (sdata == NULL)
3119         {
3120           // layout->find_output_section(".sdata") == NULL
3121           sdata = new Output_data_space(4, "** sdata");
3122           Output_section* os
3123             = layout->add_output_section_data(".sdata", 0,
3124                                               elfcpp::SHF_ALLOC
3125                                               | elfcpp::SHF_WRITE,
3126                                               sdata, ORDER_SMALL_DATA, false);
3127           symtab->define_in_output_data("_SDA_BASE_", NULL,
3128                                         Symbol_table::PREDEFINED,
3129                                         os, 32768, 0, elfcpp::STT_OBJECT,
3130                                         elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
3131                                         0, false, false);
3132         }
3133     }
3134
3135   gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
3136     symtab,
3137     layout,
3138     this,
3139     object,
3140     data_shndx,
3141     prelocs,
3142     reloc_count,
3143     output_section,
3144     needs_special_offset_handling,
3145     local_symbol_count,
3146     plocal_symbols);
3147 }
3148
3149 // Finalize the sections.
3150
3151 template<int size, bool big_endian>
3152 void
3153 Target_powerpc<size, big_endian>::do_finalize_sections(
3154     Layout* layout,
3155     const Input_objects*,
3156     Symbol_table*)
3157 {
3158   // Fill in some more dynamic tags.
3159   const Reloc_section* rel_plt = (this->plt_ == NULL
3160                                   ? NULL
3161                                   : this->plt_->rel_plt());
3162   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3163                                   this->rela_dyn_, true, size == 32);
3164
3165   Output_data_dynamic* odyn = layout->dynamic_data();
3166   if (size == 32)
3167     {
3168       if (this->got_ != NULL)
3169         {
3170           this->got_->finalize_data_size();
3171           odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
3172                                         this->got_, this->got_->g_o_t());
3173         }
3174     }
3175   else
3176     {
3177       if (this->glink_ != NULL)
3178         {
3179           this->glink_->finalize_data_size();
3180           odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
3181                                         this->glink_,
3182                                         (this->glink_->pltresolve()
3183                                          + this->glink_->pltresolve_size - 32));
3184         }
3185     }
3186
3187   // Emit any relocs we saved in an attempt to avoid generating COPY
3188   // relocs.
3189   if (this->copy_relocs_.any_saved_relocs())
3190     this->copy_relocs_.emit(this->rela_dyn_section(layout));
3191 }
3192
3193 // Return the value to use for a branch relocation.
3194
3195 template<int size, bool big_endian>
3196 typename elfcpp::Elf_types<size>::Elf_Addr
3197 Target_powerpc<size, big_endian>::symval_for_branch(
3198     Address value,
3199     const Sized_symbol<size>* gsym,
3200     Powerpc_relobj<size, big_endian>* object,
3201     unsigned int *dest_shndx)
3202 {
3203   *dest_shndx = 0;
3204   if (size == 32)
3205     return value;
3206
3207   // If the symbol is defined in an opd section, ie. is a function
3208   // descriptor, use the function descriptor code entry address
3209   Powerpc_relobj<size, big_endian>* symobj = object;
3210   if (gsym != NULL)
3211     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
3212   unsigned int shndx = symobj->opd_shndx();
3213   if (shndx == 0)
3214     return value;
3215   Address opd_addr = symobj->get_output_section_offset(shndx);
3216   gold_assert(opd_addr != invalid_address);
3217   opd_addr += symobj->output_section(shndx)->address();
3218   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
3219     {
3220       Address sec_off;
3221       *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
3222       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
3223       gold_assert(sec_addr != invalid_address);
3224       sec_addr += symobj->output_section(*dest_shndx)->address();
3225       value = sec_addr + sec_off;
3226     }
3227   return value;
3228 }
3229
3230 // Perform a relocation.
3231
3232 template<int size, bool big_endian>
3233 inline bool
3234 Target_powerpc<size, big_endian>::Relocate::relocate(
3235     const Relocate_info<size, big_endian>* relinfo,
3236     Target_powerpc* target,
3237     Output_section* os,
3238     size_t relnum,
3239     const elfcpp::Rela<size, big_endian>& rela,
3240     unsigned int r_type,
3241     const Sized_symbol<size>* gsym,
3242     const Symbol_value<size>* psymval,
3243     unsigned char* view,
3244     Address address,
3245     section_size_type view_size)
3246 {
3247
3248   bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
3249                        || r_type == elfcpp::R_PPC_PLTREL24)
3250                       && gsym != NULL
3251                       && strcmp(gsym->name(), "__tls_get_addr") == 0);
3252   enum skip_tls last_tls = this->call_tls_get_addr_;
3253   this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
3254   if (is_tls_call)
3255     {
3256       if (last_tls == CALL_NOT_EXPECTED)
3257         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3258                                _("__tls_get_addr call lacks marker reloc"));
3259       else if (last_tls == CALL_SKIP)
3260         return false;
3261     }
3262   else if (last_tls != CALL_NOT_EXPECTED)
3263     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3264                            _("missing expected __tls_get_addr call"));
3265
3266   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
3267   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
3268   Powerpc_relobj<size, big_endian>* const object
3269     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
3270   Address value = 0;
3271   bool has_plt_value = false;
3272   if (gsym != NULL
3273       && use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type)))
3274     {
3275       const Output_data_glink<size, big_endian>* glink
3276         = target->glink_section();
3277       unsigned int glink_index = glink->find_entry(gsym, rela, object);
3278       value = glink->address() + glink_index * glink->glink_entry_size();
3279       has_plt_value = true;
3280     }
3281
3282   if (r_type == elfcpp::R_POWERPC_GOT16
3283       || r_type == elfcpp::R_POWERPC_GOT16_LO
3284       || r_type == elfcpp::R_POWERPC_GOT16_HI
3285       || r_type == elfcpp::R_POWERPC_GOT16_HA
3286       || r_type == elfcpp::R_PPC64_GOT16_DS
3287       || r_type == elfcpp::R_PPC64_GOT16_LO_DS)
3288     {
3289       if (gsym != NULL)
3290         {
3291           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3292           value = gsym->got_offset(GOT_TYPE_STANDARD);
3293         }
3294       else
3295         {
3296           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3297           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3298           value = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3299         }
3300       value -= target->got_section()->got_base_offset(object);
3301     }
3302   else if (r_type == elfcpp::R_PPC64_TOC)
3303     {
3304       value = (target->got_section()->output_section()->address()
3305                + object->toc_base_offset());
3306     }
3307   else if (gsym != NULL
3308            && (r_type == elfcpp::R_POWERPC_REL24
3309                || r_type == elfcpp::R_PPC_PLTREL24)
3310            && has_plt_value)
3311     {
3312       if (size == 64)
3313         {
3314           typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
3315           Valtype* wv = reinterpret_cast<Valtype*>(view);
3316           bool can_plt_call = false;
3317           if (rela.get_r_offset() + 8 <= view_size)
3318             {
3319               Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
3320               Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
3321               if ((insn & 1) != 0
3322                   && (insn2 == nop
3323                       || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
3324                 {
3325                   elfcpp::Swap<32, big_endian>::writeval(wv + 1, ld_2_1 + 40);
3326                   can_plt_call = true;
3327                 }
3328             }
3329           if (!can_plt_call)
3330             {
3331               // If we don't have a branch and link followed by a nop,
3332               // we can't go via the plt because there is no place to
3333               // put a toc restoring instruction.
3334               // Unless we know we won't be returning.
3335               if (strcmp(gsym->name(), "__libc_start_main") == 0)
3336                 can_plt_call = true;
3337             }
3338           if (!can_plt_call)
3339             {
3340               // This is not an error in one special case: A self
3341               // call.  It isn't possible to cheaply verify we have
3342               // such a call so just check for a call to the same
3343               // section.
3344               bool ok = false;
3345               if (gsym->source() == Symbol::FROM_OBJECT
3346                   && gsym->object() == object)
3347                 {
3348                   Address addend = rela.get_r_addend();
3349                   unsigned int dest_shndx;
3350                   value = psymval->value(object, addend);
3351                   value = target->symval_for_branch(value, gsym, object,
3352                                                     &dest_shndx);
3353                   bool is_ordinary;
3354                   if (dest_shndx == 0)
3355                     dest_shndx = gsym->shndx(&is_ordinary);
3356                   ok = dest_shndx == relinfo->data_shndx;
3357                 }
3358               if (!ok)
3359                 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3360                                        _("call lacks nop, can't restore toc; "
3361                                          "recompile with -fPIC"));
3362             }
3363         }
3364     }
3365   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3366            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
3367            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
3368            || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
3369     {
3370       // First instruction of a global dynamic sequence, arg setup insn.
3371       const bool final = gsym == NULL || gsym->final_value_is_known();
3372       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3373       enum Got_type got_type = GOT_TYPE_STANDARD;
3374       if (tls_type == tls::TLSOPT_NONE)
3375         got_type = GOT_TYPE_TLSGD;
3376       else if (tls_type == tls::TLSOPT_TO_IE)
3377         got_type = GOT_TYPE_TPREL;
3378       if (got_type != GOT_TYPE_STANDARD)
3379         {
3380           if (gsym != NULL)
3381             {
3382               gold_assert(gsym->has_got_offset(got_type));
3383               value = gsym->got_offset(got_type);
3384             }
3385           else
3386             {
3387               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3388               gold_assert(object->local_has_got_offset(r_sym, got_type));
3389               value = object->local_got_offset(r_sym, got_type);
3390             }
3391           value -= target->got_section()->got_base_offset(object);
3392         }
3393       if (tls_type == tls::TLSOPT_TO_IE)
3394         {
3395           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3396               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3397             {
3398               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3399               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3400               insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
3401               if (size == 32)
3402                 insn |= 32 << 26; // lwz
3403               else
3404                 insn |= 58 << 26; // ld
3405               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3406             }
3407           r_type += (elfcpp::R_POWERPC_GOT_TPREL16
3408                      - elfcpp::R_POWERPC_GOT_TLSGD16);
3409         }
3410       else if (tls_type == tls::TLSOPT_TO_LE)
3411         {
3412           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
3413               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
3414             {
3415               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3416               Insn insn = addis_3_13;
3417               if (size == 32)
3418                 insn = addis_3_2;
3419               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3420               r_type = elfcpp::R_POWERPC_TPREL16_HA;
3421               value = psymval->value(object, rela.get_r_addend());
3422             }
3423           else
3424             {
3425               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3426               Insn insn = nop;
3427               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3428               r_type = elfcpp::R_POWERPC_NONE;
3429             }
3430         }
3431     }
3432   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3433            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
3434            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
3435            || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
3436     {
3437       // First instruction of a local dynamic sequence, arg setup insn.
3438       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3439       if (tls_type == tls::TLSOPT_NONE)
3440         {
3441           value = target->tlsld_got_offset();
3442           value -= target->got_section()->got_base_offset(object);
3443         }
3444       else
3445         {
3446           gold_assert(tls_type == tls::TLSOPT_TO_LE);
3447           if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
3448               || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
3449             {
3450               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3451               Insn insn = addis_3_13;
3452               if (size == 32)
3453                 insn = addis_3_2;
3454               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3455               r_type = elfcpp::R_POWERPC_TPREL16_HA;
3456               value = dtp_offset;
3457             }
3458           else
3459             {
3460               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3461               Insn insn = nop;
3462               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3463               r_type = elfcpp::R_POWERPC_NONE;
3464             }
3465         }
3466     }
3467   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
3468            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
3469            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
3470            || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA)
3471     {
3472       // Accesses relative to a local dynamic sequence address,
3473       // no optimisation here.
3474       if (gsym != NULL)
3475         {
3476           gold_assert(gsym->has_got_offset(GOT_TYPE_DTPREL));
3477           value = gsym->got_offset(GOT_TYPE_DTPREL);
3478         }
3479       else
3480         {
3481           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3482           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_DTPREL));
3483           value = object->local_got_offset(r_sym, GOT_TYPE_DTPREL);
3484         }
3485       value -= target->got_section()->got_base_offset(object);
3486     }
3487   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3488            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
3489            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
3490            || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
3491     {
3492       // First instruction of initial exec sequence.
3493       const bool final = gsym == NULL || gsym->final_value_is_known();
3494       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3495       if (tls_type == tls::TLSOPT_NONE)
3496         {
3497           if (gsym != NULL)
3498             {
3499               gold_assert(gsym->has_got_offset(GOT_TYPE_TPREL));
3500               value = gsym->got_offset(GOT_TYPE_TPREL);
3501             }
3502           else
3503             {
3504               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3505               gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_TPREL));
3506               value = object->local_got_offset(r_sym, GOT_TYPE_TPREL);
3507             }
3508           value -= target->got_section()->got_base_offset(object);
3509         }
3510       else
3511         {
3512           gold_assert(tls_type == tls::TLSOPT_TO_LE);
3513           if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
3514               || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
3515             {
3516               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3517               Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3518               insn &= (1 << 26) - (1 << 21); // extract rt from ld
3519               if (size == 32)
3520                 insn |= addis_0_2;
3521               else
3522                 insn |= addis_0_13;
3523               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3524               r_type = elfcpp::R_POWERPC_TPREL16_HA;
3525               value = psymval->value(object, rela.get_r_addend());
3526             }
3527           else
3528             {
3529               Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
3530               Insn insn = nop;
3531               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3532               r_type = elfcpp::R_POWERPC_NONE;
3533             }
3534         }
3535     }
3536   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
3537            || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
3538     {
3539       // Second instruction of a global dynamic sequence,
3540       // the __tls_get_addr call
3541       this->call_tls_get_addr_ = CALL_EXPECTED;
3542       const bool final = gsym == NULL || gsym->final_value_is_known();
3543       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
3544       if (tls_type != tls::TLSOPT_NONE)
3545         {
3546           if (tls_type == tls::TLSOPT_TO_IE)
3547             {
3548               Insn* iview = reinterpret_cast<Insn*>(view);
3549               Insn insn = add_3_3_13;
3550               if (size == 32)
3551                 insn = add_3_3_2;
3552               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3553               r_type = elfcpp::R_POWERPC_NONE;
3554             }
3555           else
3556             {
3557               Insn* iview = reinterpret_cast<Insn*>(view);
3558               Insn insn = addi_3_3;
3559               elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3560               r_type = elfcpp::R_POWERPC_TPREL16_LO;
3561               view += 2 * big_endian;
3562               value = psymval->value(object, rela.get_r_addend());
3563             }
3564           this->call_tls_get_addr_ = CALL_SKIP;
3565         }
3566     }
3567   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
3568            || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
3569     {
3570       // Second instruction of a local dynamic sequence,
3571       // the __tls_get_addr call
3572       this->call_tls_get_addr_ = CALL_EXPECTED;
3573       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
3574       if (tls_type == tls::TLSOPT_TO_LE)
3575         {
3576           Insn* iview = reinterpret_cast<Insn*>(view);
3577           Insn insn = addi_3_3;
3578           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3579           this->call_tls_get_addr_ = CALL_SKIP;
3580           r_type = elfcpp::R_POWERPC_TPREL16_LO;
3581           view += 2 * big_endian;
3582           value = dtp_offset;
3583         }
3584     }
3585   else if (r_type == elfcpp::R_POWERPC_TLS)
3586     {
3587       // Second instruction of an initial exec sequence
3588       const bool final = gsym == NULL || gsym->final_value_is_known();
3589       const tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
3590       if (tls_type == tls::TLSOPT_TO_LE)
3591         {
3592           Insn* iview = reinterpret_cast<Insn*>(view);
3593           Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3594           unsigned int reg = size == 32 ? 2 : 13;
3595           insn = at_tls_transform(insn, reg);
3596           gold_assert(insn != 0);
3597           elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3598           r_type = elfcpp::R_POWERPC_TPREL16_LO;
3599           view += 2 * big_endian;
3600           value = psymval->value(object, rela.get_r_addend());
3601         }
3602     }
3603   else
3604     {
3605       Address addend = 0;
3606       unsigned int dest_shndx;
3607       if (r_type != elfcpp::R_PPC_PLTREL24)
3608         addend = rela.get_r_addend();
3609       if (size == 64 || !has_plt_value)
3610         value = psymval->value(object, addend);
3611       if (size == 64 && is_branch_reloc(r_type))
3612         value = target->symval_for_branch(value, gsym, object, &dest_shndx);
3613     }
3614
3615   switch (r_type)
3616     {
3617     case elfcpp::R_PPC64_REL64:
3618     case elfcpp::R_POWERPC_REL32:
3619     case elfcpp::R_POWERPC_REL24:
3620     case elfcpp::R_PPC_PLTREL24:
3621     case elfcpp::R_PPC_LOCAL24PC:
3622     case elfcpp::R_POWERPC_REL16:
3623     case elfcpp::R_POWERPC_REL16_LO:
3624     case elfcpp::R_POWERPC_REL16_HI:
3625     case elfcpp::R_POWERPC_REL16_HA:
3626     case elfcpp::R_POWERPC_REL14:
3627     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3628     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3629       value -= address;
3630       break;
3631
3632     case elfcpp::R_PPC64_TOC16:
3633     case elfcpp::R_PPC64_TOC16_LO:
3634     case elfcpp::R_PPC64_TOC16_HI:
3635     case elfcpp::R_PPC64_TOC16_HA:
3636     case elfcpp::R_PPC64_TOC16_DS:
3637     case elfcpp::R_PPC64_TOC16_LO_DS:
3638       // Subtract the TOC base address.
3639       value -= (target->got_section()->output_section()->address()
3640                 + object->toc_base_offset());
3641       break;
3642
3643     case elfcpp::R_POWERPC_SECTOFF:
3644     case elfcpp::R_POWERPC_SECTOFF_LO:
3645     case elfcpp::R_POWERPC_SECTOFF_HI:
3646     case elfcpp::R_POWERPC_SECTOFF_HA:
3647     case elfcpp::R_PPC64_SECTOFF_DS:
3648     case elfcpp::R_PPC64_SECTOFF_LO_DS:
3649       if (os != NULL)
3650         value -= os->address();
3651       break;
3652
3653     case elfcpp::R_PPC64_TPREL16_DS:
3654     case elfcpp::R_PPC64_TPREL16_LO_DS:
3655       if (size != 64)
3656         // R_PPC_TLSGD and R_PPC_TLSLD
3657         break;
3658     case elfcpp::R_POWERPC_TPREL16:
3659     case elfcpp::R_POWERPC_TPREL16_LO:
3660     case elfcpp::R_POWERPC_TPREL16_HI:
3661     case elfcpp::R_POWERPC_TPREL16_HA:
3662     case elfcpp::R_POWERPC_TPREL:
3663     case elfcpp::R_PPC64_TPREL16_HIGHER:
3664     case elfcpp::R_PPC64_TPREL16_HIGHERA:
3665     case elfcpp::R_PPC64_TPREL16_HIGHEST:
3666     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3667       // tls symbol values are relative to tls_segment()->vaddr()
3668       value -= tp_offset;
3669       break;
3670
3671     case elfcpp::R_PPC64_DTPREL16_DS:
3672     case elfcpp::R_PPC64_DTPREL16_LO_DS:
3673     case elfcpp::R_PPC64_DTPREL16_HIGHER:
3674     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3675     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3676     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3677       if (size != 64)
3678         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
3679         // R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
3680         break;
3681     case elfcpp::R_POWERPC_DTPREL16:
3682     case elfcpp::R_POWERPC_DTPREL16_LO:
3683     case elfcpp::R_POWERPC_DTPREL16_HI:
3684     case elfcpp::R_POWERPC_DTPREL16_HA:
3685     case elfcpp::R_POWERPC_DTPREL:
3686       // tls symbol values are relative to tls_segment()->vaddr()
3687       value -= dtp_offset;
3688       break;
3689
3690     default:
3691       break;
3692     }
3693
3694   Insn branch_bit = 0;
3695   switch (r_type)
3696     {
3697     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3698     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3699       branch_bit = 1 << 21;
3700     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3701     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3702       {
3703         Insn* iview = reinterpret_cast<Insn*>(view);
3704         Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
3705         insn &= ~(1 << 21);
3706         insn |= branch_bit;
3707         if (this->is_isa_v2)
3708           {
3709             // Set 'a' bit.  This is 0b00010 in BO field for branch
3710             // on CR(BI) insns (BO == 001at or 011at), and 0b01000
3711             // for branch on CTR insns (BO == 1a00t or 1a01t).
3712             if ((insn & (0x14 << 21)) == (0x04 << 21))
3713               insn |= 0x02 << 21;
3714             else if ((insn & (0x14 << 21)) == (0x10 << 21))
3715               insn |= 0x08 << 21;
3716             else
3717               break;
3718           }
3719         else
3720           {
3721             // Invert 'y' bit if not the default.
3722             if (static_cast<Signed_address>(value) < 0)
3723               insn ^= 1 << 21;
3724           }
3725         elfcpp::Swap<32, big_endian>::writeval(iview, insn);
3726       }
3727       break;
3728
3729     default:
3730       break;
3731     }
3732
3733   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
3734   switch (r_type)
3735     {
3736     case elfcpp::R_POWERPC_ADDR32:
3737     case elfcpp::R_POWERPC_UADDR32:
3738       if (size == 64)
3739         overflow = Reloc::CHECK_BITFIELD;
3740       break;
3741
3742     case elfcpp::R_POWERPC_REL32:
3743       if (size == 64)
3744         overflow = Reloc::CHECK_SIGNED;
3745       break;
3746
3747     case elfcpp::R_POWERPC_ADDR24:
3748     case elfcpp::R_POWERPC_ADDR16:
3749     case elfcpp::R_POWERPC_UADDR16:
3750     case elfcpp::R_PPC64_ADDR16_DS:
3751     case elfcpp::R_POWERPC_ADDR14:
3752     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3753     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3754       overflow = Reloc::CHECK_BITFIELD;
3755       break;
3756
3757     case elfcpp::R_POWERPC_REL24:
3758     case elfcpp::R_PPC_PLTREL24:
3759     case elfcpp::R_PPC_LOCAL24PC:
3760     case elfcpp::R_POWERPC_REL16:
3761     case elfcpp::R_PPC64_TOC16:
3762     case elfcpp::R_POWERPC_GOT16:
3763     case elfcpp::R_POWERPC_SECTOFF:
3764     case elfcpp::R_POWERPC_TPREL16:
3765     case elfcpp::R_POWERPC_DTPREL16:
3766     case elfcpp::R_PPC64_TPREL16_DS:
3767     case elfcpp::R_PPC64_DTPREL16_DS:
3768     case elfcpp::R_PPC64_TOC16_DS:
3769     case elfcpp::R_PPC64_GOT16_DS:
3770     case elfcpp::R_PPC64_SECTOFF_DS:
3771     case elfcpp::R_POWERPC_REL14:
3772     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3773     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3774     case elfcpp::R_POWERPC_GOT_TLSGD16:
3775     case elfcpp::R_POWERPC_GOT_TLSLD16:
3776     case elfcpp::R_POWERPC_GOT_TPREL16:
3777     case elfcpp::R_POWERPC_GOT_DTPREL16:
3778       overflow = Reloc::CHECK_SIGNED;
3779       break;
3780     }
3781
3782   typename Powerpc_relocate_functions<size, big_endian>::Status status
3783     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
3784   switch (r_type)
3785     {
3786     case elfcpp::R_POWERPC_NONE:
3787     case elfcpp::R_POWERPC_TLS:
3788     case elfcpp::R_POWERPC_GNU_VTINHERIT:
3789     case elfcpp::R_POWERPC_GNU_VTENTRY:
3790     case elfcpp::R_PPC_EMB_MRKREF:
3791       break;
3792
3793     case elfcpp::R_PPC64_ADDR64:
3794     case elfcpp::R_PPC64_REL64:
3795     case elfcpp::R_PPC64_TOC:
3796       Reloc::addr64(view, value);
3797       break;
3798
3799     case elfcpp::R_POWERPC_TPREL:
3800     case elfcpp::R_POWERPC_DTPREL:
3801       if (size == 64)
3802         Reloc::addr64(view, value);
3803       else
3804         status = Reloc::addr32(view, value, overflow);
3805       break;
3806
3807     case elfcpp::R_PPC64_UADDR64:
3808       Reloc::addr64_u(view, value);
3809       break;
3810
3811     case elfcpp::R_POWERPC_ADDR32:
3812     case elfcpp::R_POWERPC_REL32:
3813       status = Reloc::addr32(view, value, overflow);
3814       break;
3815
3816     case elfcpp::R_POWERPC_UADDR32:
3817       status = Reloc::addr32_u(view, value, overflow);
3818       break;
3819
3820     case elfcpp::R_POWERPC_ADDR24:
3821     case elfcpp::R_POWERPC_REL24:
3822     case elfcpp::R_PPC_PLTREL24:
3823     case elfcpp::R_PPC_LOCAL24PC:
3824       status = Reloc::addr24(view, value, overflow);
3825       break;
3826
3827     case elfcpp::R_POWERPC_GOT_DTPREL16:
3828     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
3829       if (size == 64)
3830         {
3831           status = Reloc::addr16_ds(view, value, overflow);
3832           break;
3833         }
3834     case elfcpp::R_POWERPC_ADDR16:
3835     case elfcpp::R_POWERPC_REL16:
3836     case elfcpp::R_PPC64_TOC16:
3837     case elfcpp::R_POWERPC_GOT16:
3838     case elfcpp::R_POWERPC_SECTOFF:
3839     case elfcpp::R_POWERPC_TPREL16:
3840     case elfcpp::R_POWERPC_DTPREL16:
3841     case elfcpp::R_POWERPC_GOT_TLSGD16:
3842     case elfcpp::R_POWERPC_GOT_TLSLD16:
3843     case elfcpp::R_POWERPC_GOT_TPREL16:
3844     case elfcpp::R_POWERPC_ADDR16_LO:
3845     case elfcpp::R_POWERPC_REL16_LO:
3846     case elfcpp::R_PPC64_TOC16_LO:
3847     case elfcpp::R_POWERPC_GOT16_LO:
3848     case elfcpp::R_POWERPC_SECTOFF_LO:
3849     case elfcpp::R_POWERPC_TPREL16_LO:
3850     case elfcpp::R_POWERPC_DTPREL16_LO:
3851     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
3852     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
3853     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
3854       status = Reloc::addr16(view, value, overflow);
3855       break;
3856
3857     case elfcpp::R_POWERPC_UADDR16:
3858       status = Reloc::addr16_u(view, value, overflow);
3859       break;
3860
3861     case elfcpp::R_POWERPC_ADDR16_HI:
3862     case elfcpp::R_POWERPC_REL16_HI:
3863     case elfcpp::R_PPC64_TOC16_HI:
3864     case elfcpp::R_POWERPC_GOT16_HI:
3865     case elfcpp::R_POWERPC_SECTOFF_HI:
3866     case elfcpp::R_POWERPC_TPREL16_HI:
3867     case elfcpp::R_POWERPC_DTPREL16_HI:
3868     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
3869     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
3870     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
3871     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
3872       Reloc::addr16_hi(view, value);
3873       break;
3874
3875     case elfcpp::R_POWERPC_ADDR16_HA:
3876     case elfcpp::R_POWERPC_REL16_HA:
3877     case elfcpp::R_PPC64_TOC16_HA:
3878     case elfcpp::R_POWERPC_GOT16_HA:
3879     case elfcpp::R_POWERPC_SECTOFF_HA:
3880     case elfcpp::R_POWERPC_TPREL16_HA:
3881     case elfcpp::R_POWERPC_DTPREL16_HA:
3882     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
3883     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
3884     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
3885     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
3886       Reloc::addr16_ha(view, value);
3887       break;
3888
3889     case elfcpp::R_PPC64_DTPREL16_HIGHER:
3890       if (size == 32)
3891         // R_PPC_EMB_NADDR16_LO
3892         goto unsupp;
3893     case elfcpp::R_PPC64_ADDR16_HIGHER:
3894     case elfcpp::R_PPC64_TPREL16_HIGHER:
3895       Reloc::addr16_hi2(view, value);
3896       break;
3897
3898     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
3899       if (size == 32)
3900         // R_PPC_EMB_NADDR16_HI
3901         goto unsupp;
3902     case elfcpp::R_PPC64_ADDR16_HIGHERA:
3903     case elfcpp::R_PPC64_TPREL16_HIGHERA:
3904       Reloc::addr16_ha2(view, value);
3905       break;
3906
3907     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
3908       if (size == 32)
3909         // R_PPC_EMB_NADDR16_HA
3910         goto unsupp;
3911     case elfcpp::R_PPC64_ADDR16_HIGHEST:
3912     case elfcpp::R_PPC64_TPREL16_HIGHEST:
3913       Reloc::addr16_hi3(view, value);
3914       break;
3915
3916     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
3917       if (size == 32)
3918         // R_PPC_EMB_SDAI16
3919         goto unsupp;
3920     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
3921     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
3922       Reloc::addr16_ha3(view, value);
3923       break;
3924
3925     case elfcpp::R_PPC64_DTPREL16_DS:
3926     case elfcpp::R_PPC64_DTPREL16_LO_DS:
3927       if (size == 32)
3928         // R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
3929         goto unsupp;
3930     case elfcpp::R_PPC64_TPREL16_DS:
3931     case elfcpp::R_PPC64_TPREL16_LO_DS:
3932       if (size == 32)
3933         // R_PPC_TLSGD, R_PPC_TLSLD
3934         break;
3935     case elfcpp::R_PPC64_ADDR16_DS:
3936     case elfcpp::R_PPC64_ADDR16_LO_DS:
3937     case elfcpp::R_PPC64_TOC16_DS:
3938     case elfcpp::R_PPC64_TOC16_LO_DS:
3939     case elfcpp::R_PPC64_GOT16_DS:
3940     case elfcpp::R_PPC64_GOT16_LO_DS:
3941     case elfcpp::R_PPC64_SECTOFF_DS:
3942     case elfcpp::R_PPC64_SECTOFF_LO_DS:
3943       status = Reloc::addr16_ds(view, value, overflow);
3944       break;
3945
3946     case elfcpp::R_POWERPC_ADDR14:
3947     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
3948     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
3949     case elfcpp::R_POWERPC_REL14:
3950     case elfcpp::R_POWERPC_REL14_BRTAKEN:
3951     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
3952       status = Reloc::addr14(view, value, overflow);
3953       break;
3954
3955     case elfcpp::R_POWERPC_COPY:
3956     case elfcpp::R_POWERPC_GLOB_DAT:
3957     case elfcpp::R_POWERPC_JMP_SLOT:
3958     case elfcpp::R_POWERPC_RELATIVE:
3959     case elfcpp::R_POWERPC_DTPMOD:
3960     case elfcpp::R_PPC64_JMP_IREL:
3961     case elfcpp::R_POWERPC_IRELATIVE:
3962       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3963                              _("unexpected reloc %u in object file"),
3964                              r_type);
3965       break;
3966
3967     case elfcpp::R_PPC_EMB_SDA21:
3968       if (size == 32)
3969         goto unsupp;
3970       else
3971         {
3972           // R_PPC64_TOCSAVE.  For the time being this can be ignored.
3973         }
3974       break;
3975
3976     case elfcpp::R_PPC_EMB_SDA2I16:
3977     case elfcpp::R_PPC_EMB_SDA2REL:
3978       if (size == 32)
3979         goto unsupp;
3980       // R_PPC64_TLSGD, R_PPC64_TLSLD
3981       break;
3982
3983     case elfcpp::R_POWERPC_PLT32:
3984     case elfcpp::R_POWERPC_PLTREL32:
3985     case elfcpp::R_POWERPC_PLT16_LO:
3986     case elfcpp::R_POWERPC_PLT16_HI:
3987     case elfcpp::R_POWERPC_PLT16_HA:
3988     case elfcpp::R_PPC_SDAREL16:
3989     case elfcpp::R_POWERPC_ADDR30:
3990     case elfcpp::R_PPC64_PLT64:
3991     case elfcpp::R_PPC64_PLTREL64:
3992     case elfcpp::R_PPC64_PLTGOT16:
3993     case elfcpp::R_PPC64_PLTGOT16_LO:
3994     case elfcpp::R_PPC64_PLTGOT16_HI:
3995     case elfcpp::R_PPC64_PLTGOT16_HA:
3996     case elfcpp::R_PPC64_PLT16_LO_DS:
3997     case elfcpp::R_PPC64_PLTGOT16_DS:
3998     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
3999     case elfcpp::R_PPC_EMB_RELSEC16:
4000     case elfcpp::R_PPC_EMB_RELST_LO:
4001     case elfcpp::R_PPC_EMB_RELST_HI:
4002     case elfcpp::R_PPC_EMB_RELST_HA:
4003     case elfcpp::R_PPC_EMB_BIT_FLD:
4004     case elfcpp::R_PPC_EMB_RELSDA:
4005     case elfcpp::R_PPC_TOC16:
4006     default:
4007     unsupp:
4008       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4009                              _("unsupported reloc %u"),
4010                              r_type);
4011       break;
4012     }
4013   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK)
4014     gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4015                            _("relocation overflow"));
4016
4017   return true;
4018 }
4019
4020 // Relocate section data.
4021
4022 template<int size, bool big_endian>
4023 void
4024 Target_powerpc<size, big_endian>::relocate_section(
4025     const Relocate_info<size, big_endian>* relinfo,
4026     unsigned int sh_type,
4027     const unsigned char* prelocs,
4028     size_t reloc_count,
4029     Output_section* output_section,
4030     bool needs_special_offset_handling,
4031     unsigned char* view,
4032     Address address,
4033     section_size_type view_size,
4034     const Reloc_symbol_changes* reloc_symbol_changes)
4035 {
4036   typedef Target_powerpc<size, big_endian> Powerpc;
4037   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
4038
4039   gold_assert(sh_type == elfcpp::SHT_RELA);
4040
4041   gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
4042                          Powerpc_relocate>(
4043     relinfo,
4044     this,
4045     prelocs,
4046     reloc_count,
4047     output_section,
4048     needs_special_offset_handling,
4049     view,
4050     address,
4051     view_size,
4052     reloc_symbol_changes);
4053 }
4054
4055 class Powerpc_scan_relocatable_reloc
4056 {
4057 public:
4058   // Return the strategy to use for a local symbol which is not a
4059   // section symbol, given the relocation type.
4060   inline Relocatable_relocs::Reloc_strategy
4061   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
4062   {
4063     if (r_type == 0 && r_sym == 0)
4064       return Relocatable_relocs::RELOC_DISCARD;
4065     return Relocatable_relocs::RELOC_COPY;
4066   }
4067
4068   // Return the strategy to use for a local symbol which is a section
4069   // symbol, given the relocation type.
4070   inline Relocatable_relocs::Reloc_strategy
4071   local_section_strategy(unsigned int, Relobj*)
4072   {
4073     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
4074   }
4075
4076   // Return the strategy to use for a global symbol, given the
4077   // relocation type, the object, and the symbol index.
4078   inline Relocatable_relocs::Reloc_strategy
4079   global_strategy(unsigned int r_type, Relobj*, unsigned int)
4080   {
4081     if (r_type == elfcpp::R_PPC_PLTREL24)
4082       return Relocatable_relocs::RELOC_SPECIAL;
4083     return Relocatable_relocs::RELOC_COPY;
4084   }
4085 };
4086
4087 // Scan the relocs during a relocatable link.
4088
4089 template<int size, bool big_endian>
4090 void
4091 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
4092     Symbol_table* symtab,
4093     Layout* layout,
4094     Sized_relobj_file<size, big_endian>* object,
4095     unsigned int data_shndx,
4096     unsigned int sh_type,
4097     const unsigned char* prelocs,
4098     size_t reloc_count,
4099     Output_section* output_section,
4100     bool needs_special_offset_handling,
4101     size_t local_symbol_count,
4102     const unsigned char* plocal_symbols,
4103     Relocatable_relocs* rr)
4104 {
4105   gold_assert(sh_type == elfcpp::SHT_RELA);
4106
4107   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
4108                                 Powerpc_scan_relocatable_reloc>(
4109     symtab,
4110     layout,
4111     object,
4112     data_shndx,
4113     prelocs,
4114     reloc_count,
4115     output_section,
4116     needs_special_offset_handling,
4117     local_symbol_count,
4118     plocal_symbols,
4119     rr);
4120 }
4121
4122 // Emit relocations for a section.
4123 // This is a modified version of the function by the same name in
4124 // target-reloc.h.  Using relocate_special_relocatable for
4125 // R_PPC_PLTREL24 would require duplication of the entire body of the
4126 // loop, so we may as well duplicate the whole thing.
4127
4128 template<int size, bool big_endian>
4129 void
4130 Target_powerpc<size, big_endian>::relocate_relocs(
4131     const Relocate_info<size, big_endian>* relinfo,
4132     unsigned int sh_type,
4133     const unsigned char* prelocs,
4134     size_t reloc_count,
4135     Output_section* output_section,
4136     off_t offset_in_output_section,
4137     const Relocatable_relocs* rr,
4138     unsigned char*,
4139     Address view_address,
4140     section_size_type,
4141     unsigned char* reloc_view,
4142     section_size_type reloc_view_size)
4143 {
4144   gold_assert(sh_type == elfcpp::SHT_RELA);
4145
4146   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
4147     Reltype;
4148   typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc_write
4149     Reltype_write;
4150   const int reloc_size
4151     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
4152
4153   Powerpc_relobj<size, big_endian>* const object
4154     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
4155   const unsigned int local_count = object->local_symbol_count();
4156   unsigned int got2_shndx = object->got2_shndx();
4157   Address got2_addend = 0;
4158   if (got2_shndx != 0)
4159     {
4160       got2_addend = object->get_output_section_offset(got2_shndx);
4161       gold_assert(got2_addend != invalid_address);
4162     }
4163
4164   unsigned char* pwrite = reloc_view;
4165   bool zap_next = false;
4166   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
4167     {
4168       Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
4169       if (strategy == Relocatable_relocs::RELOC_DISCARD)
4170         continue;
4171
4172       Reltype reloc(prelocs);
4173       Reltype_write reloc_write(pwrite);
4174
4175       Address offset = reloc.get_r_offset();
4176       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
4177       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
4178       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
4179       const unsigned int orig_r_sym = r_sym;
4180       typename elfcpp::Elf_types<size>::Elf_Swxword addend
4181         = reloc.get_r_addend();
4182       const Symbol* gsym = NULL;
4183
4184       if (zap_next)
4185         {
4186           // We could arrange to discard these and other relocs for
4187           // tls optimised sequences in the strategy methods, but for
4188           // now do as BFD ld does.
4189           r_type = elfcpp::R_POWERPC_NONE;
4190           zap_next = false;
4191         }
4192
4193       // Get the new symbol index.
4194       if (r_sym < local_count)
4195         {
4196           switch (strategy)
4197             {
4198             case Relocatable_relocs::RELOC_COPY:
4199             case Relocatable_relocs::RELOC_SPECIAL:
4200               if (r_sym != 0)
4201                 {
4202                   r_sym = object->symtab_index(r_sym);
4203                   gold_assert(r_sym != -1U);
4204                 }
4205               break;
4206
4207             case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
4208               {
4209                 // We are adjusting a section symbol.  We need to find
4210                 // the symbol table index of the section symbol for
4211                 // the output section corresponding to input section
4212                 // in which this symbol is defined.
4213                 gold_assert(r_sym < local_count);
4214                 bool is_ordinary;
4215                 unsigned int shndx =
4216                   object->local_symbol_input_shndx(r_sym, &is_ordinary);
4217                 gold_assert(is_ordinary);
4218                 Output_section* os = object->output_section(shndx);
4219                 gold_assert(os != NULL);
4220                 gold_assert(os->needs_symtab_index());
4221                 r_sym = os->symtab_index();
4222               }
4223               break;
4224
4225             default:
4226               gold_unreachable();
4227             }
4228         }
4229       else
4230         {
4231           gsym = object->global_symbol(r_sym);
4232           gold_assert(gsym != NULL);
4233           if (gsym->is_forwarder())
4234             gsym = relinfo->symtab->resolve_forwards(gsym);
4235
4236           gold_assert(gsym->has_symtab_index());
4237           r_sym = gsym->symtab_index();
4238         }
4239
4240       // Get the new offset--the location in the output section where
4241       // this relocation should be applied.
4242       if (static_cast<Address>(offset_in_output_section) != invalid_address)
4243         offset += offset_in_output_section;
4244       else
4245         {
4246           section_offset_type sot_offset =
4247             convert_types<section_offset_type, Address>(offset);
4248           section_offset_type new_sot_offset =
4249             output_section->output_offset(object, relinfo->data_shndx,
4250                                           sot_offset);
4251           gold_assert(new_sot_offset != -1);
4252           offset = new_sot_offset;
4253         }
4254
4255       // In an object file, r_offset is an offset within the section.
4256       // In an executable or dynamic object, generated by
4257       // --emit-relocs, r_offset is an absolute address.
4258       if (!parameters->options().relocatable())
4259         {
4260           offset += view_address;
4261           if (static_cast<Address>(offset_in_output_section) != invalid_address)
4262             offset -= offset_in_output_section;
4263         }
4264
4265       // Handle the reloc addend based on the strategy.
4266       if (strategy == Relocatable_relocs::RELOC_COPY)
4267         ;
4268       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
4269         {
4270           const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
4271           addend = psymval->value(object, addend);
4272         }
4273       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
4274         {
4275           if (addend >= 32768)
4276             addend += got2_addend;
4277         }
4278       else
4279         gold_unreachable();
4280
4281       if (!parameters->options().relocatable())
4282         {
4283           if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4284               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
4285               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
4286               || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
4287             {
4288               // First instruction of a global dynamic sequence,
4289               // arg setup insn.
4290               const bool final = gsym == NULL || gsym->final_value_is_known();
4291               switch (this->optimize_tls_gd(final))
4292                 {
4293                 case tls::TLSOPT_TO_IE:
4294                   r_type += (elfcpp::R_POWERPC_GOT_TPREL16
4295                              - elfcpp::R_POWERPC_GOT_TLSGD16);
4296                   break;
4297                 case tls::TLSOPT_TO_LE:
4298                   if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
4299                       || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
4300                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
4301                   else
4302                     {
4303                       r_type = elfcpp::R_POWERPC_NONE;
4304                       offset -= 2 * big_endian;
4305                     }
4306                   break;
4307                 default:
4308                   break;
4309                 }
4310             }
4311           else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4312                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
4313                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
4314                    || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
4315             {
4316               // First instruction of a local dynamic sequence,
4317               // arg setup insn.
4318               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4319                 {
4320                   if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
4321                       || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
4322                     {
4323                       r_type = elfcpp::R_POWERPC_TPREL16_HA;
4324                       const Output_section* os = relinfo->layout->tls_segment()
4325                         ->first_section();
4326                       gold_assert(os != NULL);
4327                       gold_assert(os->needs_symtab_index());
4328                       r_sym = os->symtab_index();
4329                       addend = dtp_offset;
4330                     }
4331                   else
4332                     {
4333                       r_type = elfcpp::R_POWERPC_NONE;
4334                       offset -= 2 * big_endian;
4335                     }
4336                 }
4337             }
4338           else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4339                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
4340                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
4341                    || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
4342             {
4343               // First instruction of initial exec sequence.
4344               const bool final = gsym == NULL || gsym->final_value_is_known();
4345               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4346                 {
4347                   if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
4348                       || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
4349                     r_type = elfcpp::R_POWERPC_TPREL16_HA;
4350                   else
4351                     {
4352                       r_type = elfcpp::R_POWERPC_NONE;
4353                       offset -= 2 * big_endian;
4354                     }
4355                 }
4356             }
4357           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
4358                    || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
4359             {
4360               // Second instruction of a global dynamic sequence,
4361               // the __tls_get_addr call
4362               const bool final = gsym == NULL || gsym->final_value_is_known();
4363               switch (this->optimize_tls_gd(final))
4364                 {
4365                 case tls::TLSOPT_TO_IE:
4366                   r_type = elfcpp::R_POWERPC_NONE;
4367                   zap_next = true;
4368                   break;
4369                 case tls::TLSOPT_TO_LE:
4370                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
4371                   offset += 2 * big_endian;
4372                   zap_next = true;
4373                   break;
4374                 default:
4375                   break;
4376                 }
4377             }
4378           else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
4379                    || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
4380             {
4381               // Second instruction of a local dynamic sequence,
4382               // the __tls_get_addr call
4383               if (this->optimize_tls_ld() == tls::TLSOPT_TO_LE)
4384                 {
4385                   const Output_section* os = relinfo->layout->tls_segment()
4386                     ->first_section();
4387                   gold_assert(os != NULL);
4388                   gold_assert(os->needs_symtab_index());
4389                   r_sym = os->symtab_index();
4390                   addend = dtp_offset;
4391                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
4392                   offset += 2 * big_endian;
4393                   zap_next = true;
4394                 }
4395             }
4396           else if (r_type == elfcpp::R_POWERPC_TLS)
4397             {
4398               // Second instruction of an initial exec sequence
4399               const bool final = gsym == NULL || gsym->final_value_is_known();
4400               if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
4401                 {
4402                   r_type = elfcpp::R_POWERPC_TPREL16_LO;
4403                   offset += 2 * big_endian;
4404                 }
4405             }
4406         }
4407
4408       reloc_write.put_r_offset(offset);
4409       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
4410       reloc_write.put_r_addend(addend);
4411
4412       pwrite += reloc_size;
4413     }
4414
4415   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
4416               == reloc_view_size);
4417 }
4418
4419 // Return the value to use for a dynamic which requires special
4420 // treatment.  This is how we support equality comparisons of function
4421 // pointers across shared library boundaries, as described in the
4422 // processor specific ABI supplement.
4423
4424 template<int size, bool big_endian>
4425 uint64_t
4426 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4427 {
4428   if (size == 32)
4429     {
4430       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4431       return this->plt_section()->address() + gsym->plt_offset();
4432     }
4433   else
4434     gold_unreachable();
4435 }
4436
4437 // Return the offset to use for the GOT_INDX'th got entry which is
4438 // for a local tls symbol specified by OBJECT, SYMNDX.
4439 template<int size, bool big_endian>
4440 int64_t
4441 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
4442     const Relobj* object,
4443     unsigned int symndx,
4444     unsigned int got_indx) const
4445 {
4446   const Powerpc_relobj<size, big_endian>* ppc_object
4447     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
4448   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
4449     {
4450       for (Got_type got_type = GOT_TYPE_TLSGD;
4451            got_type <= GOT_TYPE_TPREL;
4452            got_type = Got_type(got_type + 1))
4453         if (ppc_object->local_has_got_offset(symndx, got_type))
4454           {
4455             unsigned int off = ppc_object->local_got_offset(symndx, got_type);
4456             if (got_type == GOT_TYPE_TLSGD)
4457               off += size / 8;
4458             if (off == got_indx * (size / 8))
4459               {
4460                 if (got_type == GOT_TYPE_TPREL)
4461                   return -tp_offset;
4462                 else
4463                   return -dtp_offset;
4464               }
4465           }
4466     }
4467   gold_unreachable();
4468 }
4469
4470 // Return the offset to use for the GOT_INDX'th got entry which is
4471 // for global tls symbol GSYM.
4472 template<int size, bool big_endian>
4473 int64_t
4474 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
4475     Symbol* gsym,
4476     unsigned int got_indx) const
4477 {
4478   if (gsym->type() == elfcpp::STT_TLS)
4479     {
4480       for (Got_type got_type = GOT_TYPE_TLSGD;
4481            got_type <= GOT_TYPE_TPREL;
4482            got_type = Got_type(got_type + 1))
4483         if (gsym->has_got_offset(got_type))
4484           {
4485             unsigned int off = gsym->got_offset(got_type);
4486             if (got_type == GOT_TYPE_TLSGD)
4487               off += size / 8;
4488             if (off == got_indx * (size / 8))
4489               {
4490                 if (got_type == GOT_TYPE_TPREL)
4491                   return -tp_offset;
4492                 else
4493                   return -dtp_offset;
4494               }
4495           }
4496     }
4497   gold_unreachable();
4498 }
4499
4500 // The selector for powerpc object files.
4501
4502 template<int size, bool big_endian>
4503 class Target_selector_powerpc : public Target_selector
4504 {
4505 public:
4506   Target_selector_powerpc()
4507     : Target_selector(elfcpp::EM_NONE, size, big_endian,
4508                       (size == 64
4509                        ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
4510                        : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
4511                       (size == 64
4512                        ? (big_endian ? "elf64ppc" : "elf64lppc")
4513                        : (big_endian ? "elf32ppc" : "elf32lppc")))
4514   { }
4515
4516   virtual Target*
4517   do_recognize(Input_file*, off_t, int machine, int, int)
4518   {
4519     switch (size)
4520       {
4521       case 64:
4522         if (machine != elfcpp::EM_PPC64)
4523           return NULL;
4524         break;
4525
4526       case 32:
4527         if (machine != elfcpp::EM_PPC)
4528           return NULL;
4529         break;
4530
4531       default:
4532         return NULL;
4533       }
4534
4535     return this->instantiate_target();
4536   }
4537
4538   virtual Target*
4539   do_instantiate_target()
4540   { return new Target_powerpc<size, big_endian>(); }
4541 };
4542
4543 Target_selector_powerpc<32, true> target_selector_ppc32;
4544 Target_selector_powerpc<32, false> target_selector_ppc32le;
4545 Target_selector_powerpc<64, true> target_selector_ppc64;
4546 Target_selector_powerpc<64, false> target_selector_ppc64le;
4547
4548 } // End anonymous namespace.