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