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