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