xref: /netbsd-src/external/gpl3/binutils.old/dist/gold/powerpc.cc (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1 // powerpc.cc -- powerpc target support for gold.
2 
3 // Copyright (C) 2008-2022 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 <set>
27 #include <algorithm>
28 #include "elfcpp.h"
29 #include "dwarf.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "powerpc.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "copy-relocs.h"
38 #include "target.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
41 #include "tls.h"
42 #include "errors.h"
43 #include "gc.h"
44 #include "attributes.h"
45 
46 namespace
47 {
48 
49 using namespace gold;
50 
51 template<int size, bool big_endian>
52 class Output_data_plt_powerpc;
53 
54 template<int size, bool big_endian>
55 class Output_data_brlt_powerpc;
56 
57 template<int size, bool big_endian>
58 class Output_data_got_powerpc;
59 
60 template<int size, bool big_endian>
61 class Output_data_glink;
62 
63 template<int size, bool big_endian>
64 class Stub_table;
65 
66 template<int size, bool big_endian>
67 class Output_data_save_res;
68 
69 template<int size, bool big_endian>
70 class Target_powerpc;
71 
72 struct Stub_table_owner
73 {
Stub_table_owner__anon10389ec10111::Stub_table_owner74   Stub_table_owner()
75     : output_section(NULL), owner(NULL)
76   { }
77 
78   Output_section* output_section;
79   const Output_section::Input_section* owner;
80 };
81 
82 template<int size>
83 inline bool is_branch_reloc(unsigned int);
84 
85 template<int size>
86 inline bool is_plt16_reloc(unsigned int);
87 
88 // Counter incremented on every Powerpc_relobj constructed.
89 static uint32_t object_id = 0;
90 
91 template<int size, bool big_endian>
92 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
93 {
94 public:
95   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
96   typedef Unordered_set<Section_id, Section_id_hash> Section_refs;
97   typedef Unordered_map<Address, Section_refs> Access_from;
98 
Powerpc_relobj(const std::string & name,Input_file * input_file,off_t offset,const typename elfcpp::Ehdr<size,big_endian> & ehdr)99   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
100 		 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
101     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
102       uniq_(object_id++), special_(0), relatoc_(0), toc_(0),
103       has_small_toc_reloc_(false), opd_valid_(false),
104       e_flags_(ehdr.get_e_flags()), no_toc_opt_(), opd_ent_(),
105       access_from_map_(), has14_(), stub_table_index_(), st_other_(),
106       attributes_section_data_(NULL)
107   {
108     this->set_abiversion(0);
109   }
110 
~Powerpc_relobj()111   ~Powerpc_relobj()
112   { delete this->attributes_section_data_; }
113 
114   // Read the symbols then set up st_other vector.
115   void
116   do_read_symbols(Read_symbols_data*);
117 
118   // Arrange to always relocate .toc first.
119   virtual void
120   do_relocate_sections(
121       const Symbol_table* symtab, const Layout* layout,
122       const unsigned char* pshdrs, Output_file* of,
123       typename Sized_relobj_file<size, big_endian>::Views* pviews);
124 
125   // The .toc section index.
126   unsigned int
toc_shndx() const127   toc_shndx() const
128   {
129     return this->toc_;
130   }
131 
132   // Mark .toc entry at OFF as not optimizable.
133   void
set_no_toc_opt(Address off)134   set_no_toc_opt(Address off)
135   {
136     if (this->no_toc_opt_.empty())
137       this->no_toc_opt_.resize(this->section_size(this->toc_shndx())
138 			       / (size / 8));
139     off /= size / 8;
140     if (off < this->no_toc_opt_.size())
141       this->no_toc_opt_[off] = true;
142   }
143 
144   // Mark the entire .toc as not optimizable.
145   void
set_no_toc_opt()146   set_no_toc_opt()
147   {
148     this->no_toc_opt_.resize(1);
149     this->no_toc_opt_[0] = true;
150   }
151 
152   // Return true if code using the .toc entry at OFF should not be edited.
153   bool
no_toc_opt(Address off) const154   no_toc_opt(Address off) const
155   {
156     if (this->no_toc_opt_.empty())
157       return false;
158     off /= size / 8;
159     if (off >= this->no_toc_opt_.size())
160       return true;
161     return this->no_toc_opt_[off];
162   }
163 
164   // The .got2 section shndx.
165   unsigned int
got2_shndx() const166   got2_shndx() const
167   {
168     if (size == 32)
169       return this->special_;
170     else
171       return 0;
172   }
173 
174   // The .opd section shndx.
175   unsigned int
opd_shndx() const176   opd_shndx() const
177   {
178     if (size == 32)
179       return 0;
180     else
181       return this->special_;
182   }
183 
184   // Init OPD entry arrays.
185   void
init_opd(size_t opd_size)186   init_opd(size_t opd_size)
187   {
188     size_t count = this->opd_ent_ndx(opd_size);
189     this->opd_ent_.resize(count);
190   }
191 
192   // Return section and offset of function entry for .opd + R_OFF.
193   unsigned int
get_opd_ent(Address r_off,Address * value=NULL) const194   get_opd_ent(Address r_off, Address* value = NULL) const
195   {
196     size_t ndx = this->opd_ent_ndx(r_off);
197     gold_assert(ndx < this->opd_ent_.size());
198     gold_assert(this->opd_ent_[ndx].shndx != 0);
199     if (value != NULL)
200       *value = this->opd_ent_[ndx].off;
201     return this->opd_ent_[ndx].shndx;
202   }
203 
204   // Set section and offset of function entry for .opd + R_OFF.
205   void
set_opd_ent(Address r_off,unsigned int shndx,Address value)206   set_opd_ent(Address r_off, unsigned int shndx, Address value)
207   {
208     size_t ndx = this->opd_ent_ndx(r_off);
209     gold_assert(ndx < this->opd_ent_.size());
210     this->opd_ent_[ndx].shndx = shndx;
211     this->opd_ent_[ndx].off = value;
212   }
213 
214   // Return discard flag for .opd + R_OFF.
215   bool
get_opd_discard(Address r_off) const216   get_opd_discard(Address r_off) const
217   {
218     size_t ndx = this->opd_ent_ndx(r_off);
219     gold_assert(ndx < this->opd_ent_.size());
220     return this->opd_ent_[ndx].discard;
221   }
222 
223   // Set discard flag for .opd + R_OFF.
224   void
set_opd_discard(Address r_off)225   set_opd_discard(Address r_off)
226   {
227     size_t ndx = this->opd_ent_ndx(r_off);
228     gold_assert(ndx < this->opd_ent_.size());
229     this->opd_ent_[ndx].discard = true;
230   }
231 
232   bool
opd_valid() const233   opd_valid() const
234   { return this->opd_valid_; }
235 
236   void
set_opd_valid()237   set_opd_valid()
238   { this->opd_valid_ = true; }
239 
240   // Examine .rela.opd to build info about function entry points.
241   void
242   scan_opd_relocs(size_t reloc_count,
243 		  const unsigned char* prelocs,
244 		  const unsigned char* plocal_syms);
245 
246   // Returns true if a code sequence loading a TOC entry can be
247   // converted into code calculating a TOC pointer relative offset.
248   bool
249   make_toc_relative(Target_powerpc<size, big_endian>* target,
250 		    Address* value);
251 
252   bool
253   make_got_relative(Target_powerpc<size, big_endian>* target,
254 		    const Symbol_value<size>* psymval,
255 		    Address addend,
256 		    Address* value);
257 
258   // Perform the Sized_relobj_file method, then set up opd info from
259   // .opd relocs.
260   void
261   do_read_relocs(Read_relocs_data*);
262 
263   bool
264   do_find_special_sections(Read_symbols_data* sd);
265 
266   // Adjust this local symbol value.  Return false if the symbol
267   // should be discarded from the output file.
268   bool
do_adjust_local_symbol(Symbol_value<size> * lv) const269   do_adjust_local_symbol(Symbol_value<size>* lv) const
270   {
271     if (size == 64 && this->opd_shndx() != 0)
272       {
273 	bool is_ordinary;
274 	if (lv->input_shndx(&is_ordinary) != this->opd_shndx())
275 	  return true;
276 	if (this->get_opd_discard(lv->input_value()))
277 	  return false;
278       }
279     return true;
280   }
281 
282   Access_from*
access_from_map()283   access_from_map()
284   { return &this->access_from_map_; }
285 
286   // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
287   // section at DST_OFF.
288   void
add_reference(Relobj * src_obj,unsigned int src_indx,typename elfcpp::Elf_types<size>::Elf_Addr dst_off)289   add_reference(Relobj* src_obj,
290 		unsigned int src_indx,
291 		typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
292   {
293     Section_id src_id(src_obj, src_indx);
294     this->access_from_map_[dst_off].insert(src_id);
295   }
296 
297   // Add a reference to the code section specified by the .opd entry
298   // at DST_OFF
299   void
add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)300   add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
301   {
302     size_t ndx = this->opd_ent_ndx(dst_off);
303     if (ndx >= this->opd_ent_.size())
304       this->opd_ent_.resize(ndx + 1);
305     this->opd_ent_[ndx].gc_mark = true;
306   }
307 
308   void
process_gc_mark(Symbol_table * symtab)309   process_gc_mark(Symbol_table* symtab)
310   {
311     for (size_t i = 0; i < this->opd_ent_.size(); i++)
312       if (this->opd_ent_[i].gc_mark)
313 	{
314 	  unsigned int shndx = this->opd_ent_[i].shndx;
315 	  symtab->gc()->worklist().push_back(Section_id(this, shndx));
316 	}
317   }
318 
319   void
set_has_small_toc_reloc()320   set_has_small_toc_reloc()
321   { has_small_toc_reloc_ = true; }
322 
323   bool
has_small_toc_reloc() const324   has_small_toc_reloc() const
325   { return has_small_toc_reloc_; }
326 
327   void
set_has_14bit_branch(unsigned int shndx)328   set_has_14bit_branch(unsigned int shndx)
329   {
330     if (shndx >= this->has14_.size())
331       this->has14_.resize(shndx + 1);
332     this->has14_[shndx] = true;
333   }
334 
335   bool
has_14bit_branch(unsigned int shndx) const336   has_14bit_branch(unsigned int shndx) const
337   { return shndx < this->has14_.size() && this->has14_[shndx];  }
338 
339   void
set_stub_table(unsigned int shndx,unsigned int stub_index)340   set_stub_table(unsigned int shndx, unsigned int stub_index)
341   {
342     if (shndx >= this->stub_table_index_.size())
343       this->stub_table_index_.resize(shndx + 1, -1);
344     this->stub_table_index_[shndx] = stub_index;
345   }
346 
347   Stub_table<size, big_endian>*
stub_table(unsigned int shndx)348   stub_table(unsigned int shndx)
349   {
350     if (shndx < this->stub_table_index_.size())
351       {
352 	Target_powerpc<size, big_endian>* target
353 	  = static_cast<Target_powerpc<size, big_endian>*>(
354 	      parameters->sized_target<size, big_endian>());
355 	unsigned int indx = this->stub_table_index_[shndx];
356 	if (indx < target->stub_tables().size())
357 	  return target->stub_tables()[indx];
358       }
359     return NULL;
360   }
361 
362   void
clear_stub_table()363   clear_stub_table()
364   {
365     this->stub_table_index_.clear();
366   }
367 
368   uint32_t
uniq() const369   uniq() const
370   { return this->uniq_; }
371 
372   int
abiversion() const373   abiversion() const
374   { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
375 
376   // Set ABI version for input and output
377   void
378   set_abiversion(int ver);
379 
380   unsigned int
st_other(unsigned int symndx) const381   st_other (unsigned int symndx) const
382   {
383     return this->st_other_[symndx];
384   }
385 
386   unsigned int
ppc64_local_entry_offset(const Symbol * sym) const387   ppc64_local_entry_offset(const Symbol* sym) const
388   { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); }
389 
390   unsigned int
ppc64_local_entry_offset(unsigned int symndx) const391   ppc64_local_entry_offset(unsigned int symndx) const
392   { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); }
393 
394   bool
ppc64_needs_toc(const Symbol * sym) const395   ppc64_needs_toc(const Symbol* sym) const
396   { return sym->nonvis() > 1 << 3; }
397 
398   bool
ppc64_needs_toc(unsigned int symndx) const399   ppc64_needs_toc(unsigned int symndx) const
400   { return this->st_other_[symndx] > 1 << 5; }
401 
402   // The contents of the .gnu.attributes section if there is one.
403   const Attributes_section_data*
attributes_section_data() const404   attributes_section_data() const
405   { return this->attributes_section_data_; }
406 
407 private:
408   struct Opd_ent
409   {
410     unsigned int shndx;
411     bool discard : 1;
412     bool gc_mark : 1;
413     Address off;
414   };
415 
416   // Return index into opd_ent_ array for .opd entry at OFF.
417   // .opd entries are 24 bytes long, but they can be spaced 16 bytes
418   // apart when the language doesn't use the last 8-byte word, the
419   // environment pointer.  Thus dividing the entry section offset by
420   // 16 will give an index into opd_ent_ that works for either layout
421   // of .opd.  (It leaves some elements of the vector unused when .opd
422   // entries are spaced 24 bytes apart, but we don't know the spacing
423   // until relocations are processed, and in any case it is possible
424   // for an object to have some entries spaced 16 bytes apart and
425   // others 24 bytes apart.)
426   size_t
opd_ent_ndx(size_t off) const427   opd_ent_ndx(size_t off) const
428   { return off >> 4;}
429 
430   // Per object unique identifier
431   uint32_t uniq_;
432 
433   // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
434   unsigned int special_;
435 
436   // For 64-bit the .rela.toc and .toc section shdnx.
437   unsigned int relatoc_;
438   unsigned int toc_;
439 
440   // For 64-bit, whether this object uses small model relocs to access
441   // the toc.
442   bool has_small_toc_reloc_;
443 
444   // Set at the start of gc_process_relocs, when we know opd_ent_
445   // vector is valid.  The flag could be made atomic and set in
446   // do_read_relocs with memory_order_release and then tested with
447   // memory_order_acquire, potentially resulting in fewer entries in
448   // access_from_map_.
449   bool opd_valid_;
450 
451   // Header e_flags
452   elfcpp::Elf_Word e_flags_;
453 
454   // For 64-bit, an array with one entry per 64-bit word in the .toc
455   // section, set if accesses using that word cannot be optimised.
456   std::vector<bool> no_toc_opt_;
457 
458   // The first 8-byte word of an OPD entry gives the address of the
459   // entry point of the function.  Relocatable object files have a
460   // relocation on this word.  The following vector records the
461   // section and offset specified by these relocations.
462   std::vector<Opd_ent> opd_ent_;
463 
464   // References made to this object's .opd section when running
465   // gc_process_relocs for another object, before the opd_ent_ vector
466   // is valid for this object.
467   Access_from access_from_map_;
468 
469   // Whether input section has a 14-bit branch reloc.
470   std::vector<bool> has14_;
471 
472   // The stub table to use for a given input section.
473   std::vector<unsigned int> stub_table_index_;
474 
475   // ELF st_other field for local symbols.
476   std::vector<unsigned char> st_other_;
477 
478   // Object attributes if there is a .gnu.attributes section.
479   Attributes_section_data* attributes_section_data_;
480 };
481 
482 template<int size, bool big_endian>
483 class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
484 {
485 public:
486   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
487 
Powerpc_dynobj(const std::string & name,Input_file * input_file,off_t offset,const typename elfcpp::Ehdr<size,big_endian> & ehdr)488   Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
489 		 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
490     : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
491       opd_shndx_(0), e_flags_(ehdr.get_e_flags()), opd_ent_(),
492       attributes_section_data_(NULL)
493   {
494     this->set_abiversion(0);
495   }
496 
~Powerpc_dynobj()497   ~Powerpc_dynobj()
498   { delete this->attributes_section_data_; }
499 
500   // Call Sized_dynobj::do_read_symbols to read the symbols then
501   // read .opd from a dynamic object, filling in opd_ent_ vector,
502   void
503   do_read_symbols(Read_symbols_data*);
504 
505   // The .opd section shndx.
506   unsigned int
opd_shndx() const507   opd_shndx() const
508   {
509     return this->opd_shndx_;
510   }
511 
512   // The .opd section address.
513   Address
opd_address() const514   opd_address() const
515   {
516     return this->opd_address_;
517   }
518 
519   // Init OPD entry arrays.
520   void
init_opd(size_t opd_size)521   init_opd(size_t opd_size)
522   {
523     size_t count = this->opd_ent_ndx(opd_size);
524     this->opd_ent_.resize(count);
525   }
526 
527   // Return section and offset of function entry for .opd + R_OFF.
528   unsigned int
get_opd_ent(Address r_off,Address * value=NULL) const529   get_opd_ent(Address r_off, Address* value = NULL) const
530   {
531     size_t ndx = this->opd_ent_ndx(r_off);
532     gold_assert(ndx < this->opd_ent_.size());
533     gold_assert(this->opd_ent_[ndx].shndx != 0);
534     if (value != NULL)
535       *value = this->opd_ent_[ndx].off;
536     return this->opd_ent_[ndx].shndx;
537   }
538 
539   // Set section and offset of function entry for .opd + R_OFF.
540   void
set_opd_ent(Address r_off,unsigned int shndx,Address value)541   set_opd_ent(Address r_off, unsigned int shndx, Address value)
542   {
543     size_t ndx = this->opd_ent_ndx(r_off);
544     gold_assert(ndx < this->opd_ent_.size());
545     this->opd_ent_[ndx].shndx = shndx;
546     this->opd_ent_[ndx].off = value;
547   }
548 
549   int
abiversion() const550   abiversion() const
551   { return this->e_flags_ & elfcpp::EF_PPC64_ABI; }
552 
553   // Set ABI version for input and output.
554   void
555   set_abiversion(int ver);
556 
557   // The contents of the .gnu.attributes section if there is one.
558   const Attributes_section_data*
attributes_section_data() const559   attributes_section_data() const
560   { return this->attributes_section_data_; }
561 
562 private:
563   // Used to specify extent of executable sections.
564   struct Sec_info
565   {
Sec_info__anon10389ec10111::Powerpc_dynobj::Sec_info566     Sec_info(Address start_, Address len_, unsigned int shndx_)
567       : start(start_), len(len_), shndx(shndx_)
568     { }
569 
570     bool
operator <__anon10389ec10111::Powerpc_dynobj::Sec_info571     operator<(const Sec_info& that) const
572     { return this->start < that.start; }
573 
574     Address start;
575     Address len;
576     unsigned int shndx;
577   };
578 
579   struct Opd_ent
580   {
581     unsigned int shndx;
582     Address off;
583   };
584 
585   // Return index into opd_ent_ array for .opd entry at OFF.
586   size_t
opd_ent_ndx(size_t off) const587   opd_ent_ndx(size_t off) const
588   { return off >> 4;}
589 
590   // For 64-bit the .opd section shndx and address.
591   unsigned int opd_shndx_;
592   Address opd_address_;
593 
594   // Header e_flags
595   elfcpp::Elf_Word e_flags_;
596 
597   // The first 8-byte word of an OPD entry gives the address of the
598   // entry point of the function.  Records the section and offset
599   // corresponding to the address.  Note that in dynamic objects,
600   // offset is *not* relative to the section.
601   std::vector<Opd_ent> opd_ent_;
602 
603   // Object attributes if there is a .gnu.attributes section.
604   Attributes_section_data* attributes_section_data_;
605 };
606 
607 // Powerpc_copy_relocs class.  Needed to peek at dynamic relocs the
608 // base class will emit.
609 
610 template<int sh_type, int size, bool big_endian>
611 class Powerpc_copy_relocs : public Copy_relocs<sh_type, size, big_endian>
612 {
613  public:
Powerpc_copy_relocs()614   Powerpc_copy_relocs()
615     : Copy_relocs<sh_type, size, big_endian>(elfcpp::R_POWERPC_COPY)
616   { }
617 
618   // Emit any saved relocations which turn out to be needed.  This is
619   // called after all the relocs have been scanned.
620   void
621   emit(Output_data_reloc<sh_type, true, size, big_endian>*);
622 };
623 
624 // The types of GOT entries needed for this platform.
625 // These values are exposed to the ABI in an incremental link, but
626 // powerpc does not support incremental linking as yet.
627 enum Got_type
628   {
629     GOT_TYPE_STANDARD = 0,
630     GOT_TYPE_TLSGD = 1,		// double entry for @got@tlsgd
631     GOT_TYPE_DTPREL = 2,	// entry for @got@dtprel
632     GOT_TYPE_TPREL = 3,		// entry for @got@tprel
633     GOT_TYPE_SMALL = 4,
634     GOT_TYPE_SMALL_TLSGD = 5,
635     GOT_TYPE_SMALL_DTPREL = 6,
636     GOT_TYPE_SMALL_TPREL = 7
637   };
638 
639 template<int size, bool big_endian>
640 class Target_powerpc : public Sized_target<size, big_endian>
641 {
642  public:
643   typedef
644     Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
645   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
646   typedef typename elfcpp::Elf_types<size>::Elf_Swxword Signed_address;
647   typedef Unordered_set<Symbol_location, Symbol_location_hash> Tocsave_loc;
648   static const Address invalid_address = static_cast<Address>(0) - 1;
649   // Offset of tp and dtp pointers from start of TLS block.
650   static const Address tp_offset = 0x7000;
651   static const Address dtp_offset = 0x8000;
652 
Target_powerpc()653   Target_powerpc()
654     : Sized_target<size, big_endian>(&powerpc_info),
655       got_(NULL), biggot_(NULL), plt_(NULL), iplt_(NULL), lplt_(NULL),
656       brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(),
657       tlsld_got_offset_(-1U),
658       stub_tables_(), branch_lookup_table_(), branch_info_(), tocsave_loc_(),
659       power10_relocs_(false), plt_thread_safe_(false), plt_localentry0_(false),
660       plt_localentry0_init_(false), has_localentry0_(false),
661       has_tls_get_addr_opt_(false), no_tprel_opt_(false),
662       relax_failed_(false), relax_fail_count_(0),
663       stub_group_size_(0), savres_section_(0),
664       tls_get_addr_(NULL), tls_get_addr_opt_(NULL),
665       attributes_section_data_(NULL),
666       last_fp_(NULL), last_ld_(NULL), last_vec_(NULL), last_struct_(NULL)
667   {
668   }
669 
670   // Process the relocations to determine unreferenced sections for
671   // garbage collection.
672   void
673   gc_process_relocs(Symbol_table* symtab,
674 		    Layout* layout,
675 		    Sized_relobj_file<size, big_endian>* object,
676 		    unsigned int data_shndx,
677 		    unsigned int sh_type,
678 		    const unsigned char* prelocs,
679 		    size_t reloc_count,
680 		    Output_section* output_section,
681 		    bool needs_special_offset_handling,
682 		    size_t local_symbol_count,
683 		    const unsigned char* plocal_symbols);
684 
685   // Scan the relocations to look for symbol adjustments.
686   void
687   scan_relocs(Symbol_table* symtab,
688 	      Layout* layout,
689 	      Sized_relobj_file<size, big_endian>* object,
690 	      unsigned int data_shndx,
691 	      unsigned int sh_type,
692 	      const unsigned char* prelocs,
693 	      size_t reloc_count,
694 	      Output_section* output_section,
695 	      bool needs_special_offset_handling,
696 	      size_t local_symbol_count,
697 	      const unsigned char* plocal_symbols);
698 
699   // Map input .toc section to output .got section.
700   const char*
do_output_section_name(const Relobj *,const char * name,size_t * plen) const701   do_output_section_name(const Relobj*, const char* name, size_t* plen) const
702   {
703     if (size == 64 && strcmp(name, ".toc") == 0)
704       {
705 	*plen = 4;
706 	return ".got";
707       }
708     return NULL;
709   }
710 
711   // Provide linker defined save/restore functions.
712   void
713   define_save_restore_funcs(Layout*, Symbol_table*);
714 
715   // No stubs unless a final link.
716   bool
do_may_relax() const717   do_may_relax() const
718   { return !parameters->options().relocatable(); }
719 
720   bool
721   do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
722 
723   void
724   do_plt_fde_location(const Output_data*, unsigned char*,
725 		      uint64_t*, off_t*) const;
726 
727   // Stash info about branches, for stub generation.
728   void
push_branch(Powerpc_relobj<size,big_endian> * ppc_object,unsigned int data_shndx,Address r_offset,unsigned int r_type,unsigned int r_sym,Address addend)729   push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
730 	      unsigned int data_shndx, Address r_offset,
731 	      unsigned int r_type, unsigned int r_sym, Address addend)
732   {
733     Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend);
734     this->branch_info_.push_back(info);
735     if (r_type == elfcpp::R_POWERPC_REL14
736 	|| r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
737 	|| r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
738       ppc_object->set_has_14bit_branch(data_shndx);
739   }
740 
741   // Return whether the last branch is a plt call, and if so, mark the
742   // branch as having an R_PPC64_TOCSAVE.
743   bool
mark_pltcall(Powerpc_relobj<size,big_endian> * ppc_object,unsigned int data_shndx,Address r_offset,Symbol_table * symtab)744   mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
745 	       unsigned int data_shndx, Address r_offset, Symbol_table* symtab)
746   {
747     return (size == 64
748 	    && !this->branch_info_.empty()
749 	    && this->branch_info_.back().mark_pltcall(ppc_object, data_shndx,
750 						      r_offset, this, symtab));
751   }
752 
753   // Say the given location, that of a nop in a function prologue with
754   // an R_PPC64_TOCSAVE reloc, will be used to save r2.
755   // R_PPC64_TOCSAVE relocs on nops following calls point at this nop.
756   void
add_tocsave(Powerpc_relobj<size,big_endian> * ppc_object,unsigned int shndx,Address offset)757   add_tocsave(Powerpc_relobj<size, big_endian>* ppc_object,
758 	      unsigned int shndx, Address offset)
759   {
760     Symbol_location loc;
761     loc.object = ppc_object;
762     loc.shndx = shndx;
763     loc.offset = offset;
764     this->tocsave_loc_.insert(loc);
765   }
766 
767   // Accessor
768   const Tocsave_loc*
tocsave_loc() const769   tocsave_loc() const
770   {
771     return &this->tocsave_loc_;
772   }
773 
774   void
775   do_define_standard_symbols(Symbol_table*, Layout*);
776 
777   // Finalize the sections.
778   void
779   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
780 
781   // Return the value to use for a dynamic which requires special
782   // treatment.
783   uint64_t
784   do_dynsym_value(const Symbol*) const;
785 
786   // Return the PLT address to use for a local symbol.
787   uint64_t
788   do_plt_address_for_local(const Relobj*, unsigned int) const;
789 
790   // Return the PLT address to use for a global symbol.
791   uint64_t
792   do_plt_address_for_global(const Symbol*) const;
793 
794   // Return the offset to use for the GOT_INDX'th got entry which is
795   // for a local tls symbol specified by OBJECT, SYMNDX.
796   int64_t
797   do_tls_offset_for_local(const Relobj* object,
798 			  unsigned int symndx,
799 			  Output_data_got_base* got,
800 			  unsigned int got_indx,
801 			  uint64_t addend) const;
802 
803   // Return the offset to use for the GOT_INDX'th got entry which is
804   // for global tls symbol GSYM.
805   int64_t
806   do_tls_offset_for_global(Symbol* gsym,
807 			   Output_data_got_base* got, unsigned int got_indx,
808 			   uint64_t addend) const;
809 
810   void
811   do_function_location(Symbol_location*) const;
812 
813   bool
do_can_check_for_function_pointers() const814   do_can_check_for_function_pointers() const
815   { return true; }
816 
817   // Adjust -fsplit-stack code which calls non-split-stack code.
818   void
819   do_calls_non_split(Relobj* object, unsigned int shndx,
820 		     section_offset_type fnoffset, section_size_type fnsize,
821 		     const unsigned char* prelocs, size_t reloc_count,
822 		     unsigned char* view, section_size_type view_size,
823 		     std::string* from, std::string* to) const;
824 
825   // Relocate a section.
826   void
827   relocate_section(const Relocate_info<size, big_endian>*,
828 		   unsigned int sh_type,
829 		   const unsigned char* prelocs,
830 		   size_t reloc_count,
831 		   Output_section* output_section,
832 		   bool needs_special_offset_handling,
833 		   unsigned char* view,
834 		   Address view_address,
835 		   section_size_type view_size,
836 		   const Reloc_symbol_changes*);
837 
838   // Scan the relocs during a relocatable link.
839   void
840   scan_relocatable_relocs(Symbol_table* symtab,
841 			  Layout* layout,
842 			  Sized_relobj_file<size, big_endian>* object,
843 			  unsigned int data_shndx,
844 			  unsigned int sh_type,
845 			  const unsigned char* prelocs,
846 			  size_t reloc_count,
847 			  Output_section* output_section,
848 			  bool needs_special_offset_handling,
849 			  size_t local_symbol_count,
850 			  const unsigned char* plocal_symbols,
851 			  Relocatable_relocs*);
852 
853   // Scan the relocs for --emit-relocs.
854   void
855   emit_relocs_scan(Symbol_table* symtab,
856 		   Layout* layout,
857 		   Sized_relobj_file<size, big_endian>* object,
858 		   unsigned int data_shndx,
859 		   unsigned int sh_type,
860 		   const unsigned char* prelocs,
861 		   size_t reloc_count,
862 		   Output_section* output_section,
863 		   bool needs_special_offset_handling,
864 		   size_t local_symbol_count,
865 		   const unsigned char* plocal_syms,
866 		   Relocatable_relocs* rr);
867 
868   // Emit relocations for a section.
869   void
870   relocate_relocs(const Relocate_info<size, big_endian>*,
871 		  unsigned int sh_type,
872 		  const unsigned char* prelocs,
873 		  size_t reloc_count,
874 		  Output_section* output_section,
875 		  typename elfcpp::Elf_types<size>::Elf_Off
876                     offset_in_output_section,
877 		  unsigned char*,
878 		  Address view_address,
879 		  section_size_type,
880 		  unsigned char* reloc_view,
881 		  section_size_type reloc_view_size);
882 
883   // Return whether SYM is defined by the ABI.
884   bool
do_is_defined_by_abi(const Symbol * sym) const885   do_is_defined_by_abi(const Symbol* sym) const
886   {
887     return strcmp(sym->name(), "__tls_get_addr") == 0;
888   }
889 
890   // Return the size of the GOT section, for incremental linking
891   section_size_type
got_size() const892   got_size() const
893   {
894     gold_assert(this->got_ != NULL);
895     return this->got_->data_size() + (this->biggot_
896 				      ? this->biggot_->data_size() : 0);
897   }
898 
899   // Get the PLT section.
900   const Output_data_plt_powerpc<size, big_endian>*
plt_section() const901   plt_section() const
902   {
903     gold_assert(this->plt_ != NULL);
904     return this->plt_;
905   }
906 
907   // Get the IPLT section.
908   const Output_data_plt_powerpc<size, big_endian>*
iplt_section() const909   iplt_section() const
910   {
911     gold_assert(this->iplt_ != NULL);
912     return this->iplt_;
913   }
914 
915   // Get the LPLT section.
916   const Output_data_plt_powerpc<size, big_endian>*
lplt_section() const917   lplt_section() const
918   {
919     return this->lplt_;
920   }
921 
922   // Return the plt offset and section for the given global sym.
923   Address
plt_off(const Symbol * gsym,const Output_data_plt_powerpc<size,big_endian> ** sec) const924   plt_off(const Symbol* gsym,
925 	  const Output_data_plt_powerpc<size, big_endian>** sec) const
926   {
927     if (gsym->type() == elfcpp::STT_GNU_IFUNC
928 	&& gsym->can_use_relative_reloc(false))
929       *sec = this->iplt_section();
930     else if (!parameters->doing_static_link())
931       *sec = this->plt_section();
932     else
933       *sec = this->lplt_section();
934     return gsym->plt_offset();
935   }
936 
937   // Return the plt offset and section for the given local sym.
938   Address
plt_off(const Sized_relobj_file<size,big_endian> * relobj,unsigned int local_sym_index,const Output_data_plt_powerpc<size,big_endian> ** sec) const939   plt_off(const Sized_relobj_file<size, big_endian>* relobj,
940 	  unsigned int local_sym_index,
941 	  const Output_data_plt_powerpc<size, big_endian>** sec) const
942   {
943     const Symbol_value<size>* lsym = relobj->local_symbol(local_sym_index);
944     if (lsym->is_ifunc_symbol())
945       *sec = this->iplt_section();
946     else
947       *sec = this->lplt_section();
948     return relobj->local_plt_offset(local_sym_index);
949   }
950 
951   // Get the .glink section.
952   const Output_data_glink<size, big_endian>*
glink_section() const953   glink_section() const
954   {
955     gold_assert(this->glink_ != NULL);
956     return this->glink_;
957   }
958 
959   Output_data_glink<size, big_endian>*
glink_section()960   glink_section()
961   {
962     gold_assert(this->glink_ != NULL);
963     return this->glink_;
964   }
965 
has_glink() const966   bool has_glink() const
967   { return this->glink_ != NULL; }
968 
969   // Get the GOT section.
970   const Output_data_got_powerpc<size, big_endian>*
got_section(Got_type got_type) const971   got_section(Got_type got_type) const
972   {
973     gold_assert(this->got_ != NULL);
974     if (size == 32 || (got_type & GOT_TYPE_SMALL))
975       return this->got_;
976     gold_assert(this->biggot_ != NULL);
977     return this->biggot_;
978   }
979 
980   // Get the GOT section, creating it if necessary.
981   Output_data_got_powerpc<size, big_endian>*
982   got_section(Symbol_table*, Layout*, Got_type);
983 
984   // The toc/got pointer reg will be set to this value.
985   Address
toc_pointer() const986   toc_pointer() const
987   {
988     return this->got_->address() + this->got_->g_o_t();
989   }
990 
991   // Offset of base used to access the GOT/TOC relative to the GOT section.
992   Address
got_base_offset(Got_type got_type) const993   got_base_offset(Got_type got_type) const
994   {
995     if (size == 32 || (got_type & GOT_TYPE_SMALL))
996       return this->got_->g_o_t();
997     return this->toc_pointer() - this->biggot_->address();
998   }
999 
1000   Object*
1001   do_make_elf_object(const std::string&, Input_file*, off_t,
1002 		     const elfcpp::Ehdr<size, big_endian>&);
1003 
1004   // Return the number of entries in the GOT.
1005   unsigned int
got_entry_count() const1006   got_entry_count() const
1007   {
1008     if (this->got_ == NULL)
1009       return 0;
1010     return this->got_size() / (size / 8);
1011   }
1012 
1013   // Return the number of entries in the PLT.
1014   unsigned int
1015   plt_entry_count() const;
1016 
1017   // Return the offset of the first non-reserved PLT entry.
1018   unsigned int
first_plt_entry_offset() const1019   first_plt_entry_offset() const
1020   {
1021     if (size == 32)
1022       return 0;
1023     if (this->abiversion() >= 2)
1024       return 16;
1025     return 24;
1026   }
1027 
1028   // Return the size of each PLT entry.
1029   unsigned int
plt_entry_size() const1030   plt_entry_size() const
1031   {
1032     if (size == 32)
1033       return 4;
1034     if (this->abiversion() >= 2)
1035       return 8;
1036     return 24;
1037   }
1038 
1039   Output_data_save_res<size, big_endian>*
savres_section() const1040   savres_section() const
1041   {
1042     return this->savres_section_;
1043   }
1044 
1045   // Add any special sections for this symbol to the gc work list.
1046   // For powerpc64, this adds the code section of a function
1047   // descriptor.
1048   void
1049   do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const;
1050 
1051   // Handle target specific gc actions when adding a gc reference from
1052   // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
1053   // and DST_OFF.  For powerpc64, this adds a referenc to the code
1054   // section of a function descriptor.
1055   void
1056   do_gc_add_reference(Symbol_table* symtab,
1057 		      Relobj* src_obj,
1058 		      unsigned int src_shndx,
1059 		      Relobj* dst_obj,
1060 		      unsigned int dst_shndx,
1061 		      Address dst_off) const;
1062 
1063   typedef std::vector<Stub_table<size, big_endian>*> Stub_tables;
1064   const Stub_tables&
stub_tables() const1065   stub_tables() const
1066   { return this->stub_tables_; }
1067 
1068   const Output_data_brlt_powerpc<size, big_endian>*
brlt_section() const1069   brlt_section() const
1070   { return this->brlt_section_; }
1071 
1072   void
add_branch_lookup_table(Address to)1073   add_branch_lookup_table(Address to)
1074   {
1075     unsigned int off = this->branch_lookup_table_.size() * (size / 8);
1076     this->branch_lookup_table_.insert(std::make_pair(to, off));
1077   }
1078 
1079   Address
find_branch_lookup_table(Address to)1080   find_branch_lookup_table(Address to)
1081   {
1082     typename Branch_lookup_table::const_iterator p
1083       = this->branch_lookup_table_.find(to);
1084     return p == this->branch_lookup_table_.end() ? invalid_address : p->second;
1085   }
1086 
1087   void
write_branch_lookup_table(unsigned char * oview)1088   write_branch_lookup_table(unsigned char *oview)
1089   {
1090     for (typename Branch_lookup_table::const_iterator p
1091 	   = this->branch_lookup_table_.begin();
1092 	 p != this->branch_lookup_table_.end();
1093 	 ++p)
1094       {
1095 	elfcpp::Swap<size, big_endian>::writeval(oview + p->second, p->first);
1096       }
1097   }
1098 
1099   // Wrapper used after relax to define a local symbol in output data,
1100   // from the end if value < 0.
1101   void
define_local(Symbol_table * symtab,const char * name,Output_data * od,Address value,unsigned int symsize)1102   define_local(Symbol_table* symtab, const char* name,
1103 	       Output_data* od, Address value, unsigned int symsize)
1104   {
1105     Symbol* sym
1106       = symtab->define_in_output_data(name, NULL, Symbol_table::PREDEFINED,
1107 				      od, value, symsize, elfcpp::STT_NOTYPE,
1108 				      elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0,
1109 				      static_cast<Signed_address>(value) < 0,
1110 				      false);
1111     // We are creating this symbol late, so need to fix up things
1112     // done early in Layout::finalize.
1113     sym->set_dynsym_index(-1U);
1114   }
1115 
1116   void
set_power10_relocs()1117   set_power10_relocs()
1118   {
1119       this->power10_relocs_ = true;
1120   }
1121 
1122   bool
power10_stubs() const1123   power10_stubs() const
1124   {
1125     return (this->power10_relocs_
1126 	    && (parameters->options().power10_stubs_enum()
1127 		!= General_options::POWER10_STUBS_NO));
1128   }
1129 
1130   bool
power10_stubs_auto() const1131   power10_stubs_auto() const
1132   {
1133     return (parameters->options().power10_stubs_enum()
1134 	    == General_options::POWER10_STUBS_AUTO);
1135   }
1136 
1137   bool
plt_thread_safe() const1138   plt_thread_safe() const
1139   { return this->plt_thread_safe_; }
1140 
1141   bool
plt_localentry0() const1142   plt_localentry0() const
1143   { return this->plt_localentry0_; }
1144 
1145   bool
has_localentry0() const1146   has_localentry0() const
1147   { return this->has_localentry0_; }
1148 
1149   void
set_has_localentry0()1150   set_has_localentry0()
1151   {
1152     this->has_localentry0_ = true;
1153   }
1154 
1155   bool
is_elfv2_localentry0(const Symbol * gsym) const1156   is_elfv2_localentry0(const Symbol* gsym) const
1157   {
1158     return (size == 64
1159 	    && this->abiversion() >= 2
1160 	    && this->plt_localentry0()
1161 	    && gsym->type() == elfcpp::STT_FUNC
1162 	    && gsym->is_defined()
1163 	    && gsym->nonvis() >> 3 == 0
1164 	    && !gsym->non_zero_localentry());
1165   }
1166 
1167   bool
is_elfv2_localentry0(const Sized_relobj_file<size,big_endian> * object,unsigned int r_sym) const1168   is_elfv2_localentry0(const Sized_relobj_file<size, big_endian>* object,
1169 		       unsigned int r_sym) const
1170   {
1171     const Powerpc_relobj<size, big_endian>* ppc_object
1172       = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
1173 
1174     if (size == 64
1175 	&& this->abiversion() >= 2
1176 	&& this->plt_localentry0()
1177 	&& ppc_object->st_other(r_sym) >> 5 == 0)
1178       {
1179 	const Symbol_value<size>* psymval = object->local_symbol(r_sym);
1180 	bool is_ordinary;
1181 	if (!psymval->is_ifunc_symbol()
1182 	    && psymval->input_shndx(&is_ordinary) != elfcpp::SHN_UNDEF
1183 	    && is_ordinary)
1184 	  return true;
1185       }
1186     return false;
1187   }
1188 
1189   bool
tprel_opt() const1190   tprel_opt() const
1191   { return !this->no_tprel_opt_ && parameters->options().tls_optimize(); }
1192 
1193   void
set_no_tprel_opt()1194   set_no_tprel_opt()
1195   { this->no_tprel_opt_ = true; }
1196 
1197   // Remember any symbols seen with non-zero localentry, even those
1198   // not providing a definition
1199   bool
resolve(Symbol * to,const elfcpp::Sym<size,big_endian> & sym,Object *,const char *)1200   resolve(Symbol* to, const elfcpp::Sym<size, big_endian>& sym, Object*,
1201 	  const char*)
1202   {
1203     if (size == 64)
1204       {
1205 	unsigned char st_other = sym.get_st_other();
1206 	if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
1207 	  to->set_non_zero_localentry();
1208       }
1209     // We haven't resolved anything, continue normal processing.
1210     return false;
1211   }
1212 
1213   int
abiversion() const1214   abiversion() const
1215   { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; }
1216 
1217   void
set_abiversion(int ver)1218   set_abiversion(int ver)
1219   {
1220     elfcpp::Elf_Word flags = this->processor_specific_flags();
1221     flags &= ~elfcpp::EF_PPC64_ABI;
1222     flags |= ver & elfcpp::EF_PPC64_ABI;
1223     this->set_processor_specific_flags(flags);
1224   }
1225 
1226   Symbol*
tls_get_addr_opt() const1227   tls_get_addr_opt() const
1228   { return this->tls_get_addr_opt_; }
1229 
1230   Symbol*
tls_get_addr() const1231   tls_get_addr() const
1232   { return this->tls_get_addr_; }
1233 
1234   // If optimizing __tls_get_addr calls, whether this is the
1235   // "__tls_get_addr" symbol.
1236   bool
is_tls_get_addr_opt(const Symbol * gsym) const1237   is_tls_get_addr_opt(const Symbol* gsym) const
1238   {
1239     return this->tls_get_addr_opt_ && (gsym == this->tls_get_addr_
1240 				       || gsym == this->tls_get_addr_opt_);
1241   }
1242 
1243   bool
replace_tls_get_addr(const Symbol * gsym) const1244   replace_tls_get_addr(const Symbol* gsym) const
1245   { return this->tls_get_addr_opt_ && gsym == this->tls_get_addr_; }
1246 
1247   void
set_has_tls_get_addr_opt()1248   set_has_tls_get_addr_opt()
1249   { this->has_tls_get_addr_opt_ = true; }
1250 
1251   // Offset to toc save stack slot
1252   int
stk_toc() const1253   stk_toc() const
1254   { return this->abiversion() < 2 ? 40 : 24; }
1255 
1256   // Offset to linker save stack slot.  ELFv2 doesn't have a linker word,
1257   // so use the CR save slot.  Used only by __tls_get_addr call stub,
1258   // relying on __tls_get_addr not saving CR itself.
1259   int
stk_linker() const1260   stk_linker() const
1261   { return this->abiversion() < 2 ? 32 : 8; }
1262 
1263   // Merge object attributes from input object with those in the output.
1264   void
1265   merge_object_attributes(const Object*, const Attributes_section_data*);
1266 
1267   bool
1268   symval_for_branch(const Symbol_table* symtab,
1269 		    const Sized_symbol<size>* gsym,
1270 		    Powerpc_relobj<size, big_endian>* object,
1271 		    Address *value, unsigned int *dest_shndx);
1272 
1273  private:
1274 
1275   class Track_tls
1276   {
1277   public:
1278     enum Tls_get_addr
1279     {
1280       NOT_EXPECTED = 0,
1281       EXPECTED = 1,
1282       SKIP = 2,
1283       NORMAL = 3
1284     };
1285 
Track_tls()1286     Track_tls()
1287       : tls_get_addr_state_(NOT_EXPECTED),
1288 	relinfo_(NULL), relnum_(0), r_offset_(0)
1289     { }
1290 
~Track_tls()1291     ~Track_tls()
1292     {
1293       if (this->tls_get_addr_state_ != NOT_EXPECTED)
1294 	this->missing();
1295     }
1296 
1297     void
missing(void)1298     missing(void)
1299     {
1300       if (this->relinfo_ != NULL)
1301 	gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
1302 			       _("missing expected __tls_get_addr call"));
1303     }
1304 
1305     void
expect_tls_get_addr_call(const Relocate_info<size,big_endian> * relinfo,size_t relnum,Address r_offset)1306     expect_tls_get_addr_call(
1307 	const Relocate_info<size, big_endian>* relinfo,
1308 	size_t relnum,
1309 	Address r_offset)
1310     {
1311       this->tls_get_addr_state_ = EXPECTED;
1312       this->relinfo_ = relinfo;
1313       this->relnum_ = relnum;
1314       this->r_offset_ = r_offset;
1315     }
1316 
1317     void
expect_tls_get_addr_call()1318     expect_tls_get_addr_call()
1319     { this->tls_get_addr_state_ = EXPECTED; }
1320 
1321     void
skip_next_tls_get_addr_call()1322     skip_next_tls_get_addr_call()
1323     {this->tls_get_addr_state_ = SKIP; }
1324 
1325     Tls_get_addr
maybe_skip_tls_get_addr_call(Target_powerpc<size,big_endian> * target,unsigned int r_type,const Symbol * gsym)1326     maybe_skip_tls_get_addr_call(Target_powerpc<size, big_endian>* target,
1327 				 unsigned int r_type, const Symbol* gsym)
1328     {
1329       bool is_tls_call
1330 	= ((r_type == elfcpp::R_POWERPC_REL24
1331 	    || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1332 	    || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
1333 	    || r_type == elfcpp::R_PPC_PLTREL24
1334 	    || is_plt16_reloc<size>(r_type)
1335 	    || r_type == elfcpp::R_PPC64_PLT_PCREL34
1336 	    || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
1337 	    || r_type == elfcpp::R_POWERPC_PLTSEQ
1338 	    || r_type == elfcpp::R_POWERPC_PLTCALL
1339 	    || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
1340 	    || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
1341 	   && gsym != NULL
1342 	   && (gsym == target->tls_get_addr()
1343 	       || gsym == target->tls_get_addr_opt()));
1344       Tls_get_addr last_tls = this->tls_get_addr_state_;
1345       this->tls_get_addr_state_ = NOT_EXPECTED;
1346       if (is_tls_call && last_tls != EXPECTED)
1347 	return last_tls;
1348       else if (!is_tls_call && last_tls != NOT_EXPECTED)
1349 	{
1350 	  this->missing();
1351 	  return EXPECTED;
1352 	}
1353       return NORMAL;
1354     }
1355 
1356   private:
1357     // What we're up to regarding calls to __tls_get_addr.
1358     // On powerpc, the branch and link insn making a call to
1359     // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
1360     // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
1361     // usual R_POWERPC_REL24 or R_PPC_PLTREL24 relocation on a call.
1362     // The marker relocation always comes first, and has the same
1363     // symbol as the reloc on the insn setting up the __tls_get_addr
1364     // argument.  This ties the arg setup insn with the call insn,
1365     // allowing ld to safely optimize away the call.  We check that
1366     // every call to __tls_get_addr has a marker relocation, and that
1367     // every marker relocation is on a call to __tls_get_addr.
1368     Tls_get_addr tls_get_addr_state_;
1369     // Info about the last reloc for error message.
1370     const Relocate_info<size, big_endian>* relinfo_;
1371     size_t relnum_;
1372     Address r_offset_;
1373   };
1374 
1375   // The class which scans relocations.
1376   class Scan : protected Track_tls
1377   {
1378   public:
1379     typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1380 
Scan()1381     Scan()
1382       : Track_tls(), issued_non_pic_error_(false)
1383     { }
1384 
1385     static inline int
1386     get_reference_flags(unsigned int r_type, const Target_powerpc* target);
1387 
1388     inline void
1389     local(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1390 	  Sized_relobj_file<size, big_endian>* object,
1391 	  unsigned int data_shndx,
1392 	  Output_section* output_section,
1393 	  const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1394 	  const elfcpp::Sym<size, big_endian>& lsym,
1395 	  bool is_discarded);
1396 
1397     inline void
1398     global(Symbol_table* symtab, Layout* layout, Target_powerpc* target,
1399 	   Sized_relobj_file<size, big_endian>* object,
1400 	   unsigned int data_shndx,
1401 	   Output_section* output_section,
1402 	   const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
1403 	   Symbol* gsym);
1404 
1405     inline bool
local_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_powerpc *,Sized_relobj_file<size,big_endian> * relobj,unsigned int,Output_section *,const elfcpp::Rela<size,big_endian> &,unsigned int r_type,const elfcpp::Sym<size,big_endian> &)1406     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1407 					Target_powerpc* ,
1408 					Sized_relobj_file<size, big_endian>* relobj,
1409 					unsigned int ,
1410 					Output_section* ,
1411 					const elfcpp::Rela<size, big_endian>& ,
1412 					unsigned int r_type,
1413 					const elfcpp::Sym<size, big_endian>&)
1414     {
1415       // PowerPC64 .opd is not folded, so any identical function text
1416       // may be folded and we'll still keep function addresses distinct.
1417       // That means no reloc is of concern here.
1418       if (size == 64)
1419 	{
1420 	  Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1421 	    <Powerpc_relobj<size, big_endian>*>(relobj);
1422 	  if (ppcobj->abiversion() == 1)
1423 	    return false;
1424 	}
1425       // For 32-bit and ELFv2, conservatively assume anything but calls to
1426       // function code might be taking the address of the function.
1427       return !is_branch_reloc<size>(r_type);
1428     }
1429 
1430     inline bool
global_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_powerpc *,Sized_relobj_file<size,big_endian> * relobj,unsigned int,Output_section *,const elfcpp::Rela<size,big_endian> &,unsigned int r_type,Symbol *)1431     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
1432 					 Target_powerpc* ,
1433 					 Sized_relobj_file<size, big_endian>* relobj,
1434 					 unsigned int ,
1435 					 Output_section* ,
1436 					 const elfcpp::Rela<size, big_endian>& ,
1437 					 unsigned int r_type,
1438 					 Symbol*)
1439     {
1440       // As above.
1441       if (size == 64)
1442 	{
1443 	  Powerpc_relobj<size, big_endian>* ppcobj = static_cast
1444 	    <Powerpc_relobj<size, big_endian>*>(relobj);
1445 	  if (ppcobj->abiversion() == 1)
1446 	    return false;
1447 	}
1448       return !is_branch_reloc<size>(r_type);
1449     }
1450 
1451     static bool
1452     reloc_needs_plt_for_ifunc(Target_powerpc<size, big_endian>* target,
1453 			      Sized_relobj_file<size, big_endian>* object,
1454 			      unsigned int r_type, bool report_err);
1455 
1456   private:
1457     static void
1458     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
1459 			    unsigned int r_type);
1460 
1461     static void
1462     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
1463 			     unsigned int r_type, Symbol*);
1464 
1465     static void
1466     generate_tls_call(Symbol_table* symtab, Layout* layout,
1467 		      Target_powerpc* target);
1468 
1469     void
1470     check_non_pic(Relobj*, unsigned int r_type);
1471 
1472     // Whether we have issued an error about a non-PIC compilation.
1473     bool issued_non_pic_error_;
1474   };
1475 
1476   // The class which implements relocation.
1477   class Relocate : protected Track_tls
1478   {
1479    public:
1480     // Use 'at' branch hints when true, 'y' when false.
1481     // FIXME maybe: set this with an option.
1482     static const bool is_isa_v2 = true;
1483 
Relocate()1484     Relocate()
1485       : Track_tls()
1486     { }
1487 
1488     // Do a relocation.  Return false if the caller should not issue
1489     // any warnings about this relocation.
1490     inline bool
1491     relocate(const Relocate_info<size, big_endian>*, unsigned int,
1492 	     Target_powerpc*, Output_section*, size_t, const unsigned char*,
1493 	     const Sized_symbol<size>*, const Symbol_value<size>*,
1494 	     unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
1495 	     section_size_type);
1496   };
1497 
1498   class Relocate_comdat_behavior
1499   {
1500    public:
1501     // Decide what the linker should do for relocations that refer to
1502     // discarded comdat sections.
1503     inline Comdat_behavior
get(const char * name)1504     get(const char* name)
1505     {
1506       gold::Default_comdat_behavior default_behavior;
1507       Comdat_behavior ret = default_behavior.get(name);
1508       if (ret == CB_ERROR)
1509 	{
1510 	  if (size == 32
1511 	      && (strcmp(name, ".fixup") == 0
1512 		  || strcmp(name, ".got2") == 0))
1513 	    ret = CB_IGNORE;
1514 	  if (size == 64
1515 	      && (strcmp(name, ".opd") == 0
1516 		  || strcmp(name, ".toc") == 0
1517 		  || strcmp(name, ".toc1") == 0))
1518 	    ret = CB_IGNORE;
1519 	}
1520       return ret;
1521     }
1522   };
1523 
1524   // Optimize the TLS relocation type based on what we know about the
1525   // symbol.  IS_FINAL is true if the final address of this symbol is
1526   // known at link time.
1527 
1528   tls::Tls_optimization
optimize_tls_gd(bool is_final)1529   optimize_tls_gd(bool is_final)
1530   {
1531     // If we are generating a shared library, then we can't do anything
1532     // in the linker.
1533     if (parameters->options().shared()
1534 	|| !parameters->options().tls_optimize())
1535       return tls::TLSOPT_NONE;
1536 
1537     if (!is_final)
1538       return tls::TLSOPT_TO_IE;
1539     return tls::TLSOPT_TO_LE;
1540   }
1541 
1542   tls::Tls_optimization
optimize_tls_ld()1543   optimize_tls_ld()
1544   {
1545     if (parameters->options().shared()
1546 	|| !parameters->options().tls_optimize())
1547       return tls::TLSOPT_NONE;
1548 
1549     return tls::TLSOPT_TO_LE;
1550   }
1551 
1552   tls::Tls_optimization
optimize_tls_ie(bool is_final)1553   optimize_tls_ie(bool is_final)
1554   {
1555     if (!is_final
1556 	|| parameters->options().shared()
1557 	|| !parameters->options().tls_optimize())
1558       return tls::TLSOPT_NONE;
1559 
1560     return tls::TLSOPT_TO_LE;
1561   }
1562 
1563   // Create glink.
1564   void
1565   make_glink_section(Layout*);
1566 
1567   // Create the PLT section.
1568   void
1569   make_plt_section(Symbol_table*, Layout*);
1570 
1571   void
1572   make_iplt_section(Symbol_table*, Layout*);
1573 
1574   void
1575   make_lplt_section(Symbol_table*, Layout*);
1576 
1577   void
1578   make_brlt_section(Layout*);
1579 
1580   // Create a PLT entry for a global symbol.
1581   void
1582   make_plt_entry(Symbol_table*, Layout*, Symbol*);
1583 
1584   // Create a PLT entry for a local IFUNC symbol.
1585   void
1586   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
1587 			     Sized_relobj_file<size, big_endian>*,
1588 			     unsigned int);
1589 
1590   // Create a PLT entry for a local non-IFUNC symbol.
1591   void
1592   make_local_plt_entry(Symbol_table*, Layout*,
1593 		       Sized_relobj_file<size, big_endian>*,
1594 		       unsigned int);
1595 
1596   void
1597   make_local_plt_entry(Symbol_table*, Layout*, Symbol*);
1598 
1599   // Create a GOT entry for local dynamic __tls_get_addr.
1600   unsigned int
1601   tlsld_got_offset(Symbol_table* symtab, Layout* layout,
1602 		   Sized_relobj_file<size, big_endian>* object);
1603 
1604   unsigned int
tlsld_got_offset() const1605   tlsld_got_offset() const
1606   {
1607     return this->tlsld_got_offset_;
1608   }
1609 
1610   // Get the dynamic reloc section, creating it if necessary.
1611   Reloc_section*
1612   rela_dyn_section(Layout*);
1613 
1614   // Similarly, but for ifunc symbols get the one for ifunc.
1615   Reloc_section*
1616   rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
1617 
1618   // Copy a relocation against a global symbol.
1619   void
copy_reloc(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int shndx,Output_section * output_section,Symbol * sym,const elfcpp::Rela<size,big_endian> & reloc)1620   copy_reloc(Symbol_table* symtab, Layout* layout,
1621 	     Sized_relobj_file<size, big_endian>* object,
1622 	     unsigned int shndx, Output_section* output_section,
1623 	     Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
1624   {
1625     unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
1626     this->copy_relocs_.copy_reloc(symtab, layout,
1627 				  symtab->get_sized_symbol<size>(sym),
1628 				  object, shndx, output_section,
1629 				  r_type, reloc.get_r_offset(),
1630 				  reloc.get_r_addend(),
1631 				  this->rela_dyn_section(layout));
1632   }
1633 
1634   // Look over all the input sections, deciding where to place stubs.
1635   void
1636   group_sections(Layout*, const Task*, bool);
1637 
1638   // Sort output sections by address.
1639   struct Sort_sections
1640   {
1641     bool
operator ()__anon10389ec10111::Target_powerpc::Sort_sections1642     operator()(const Output_section* sec1, const Output_section* sec2)
1643     { return sec1->address() < sec2->address(); }
1644   };
1645 
1646   class Branch_info
1647   {
1648    public:
Branch_info(Powerpc_relobj<size,big_endian> * ppc_object,unsigned int data_shndx,Address r_offset,unsigned int r_type,unsigned int r_sym,Address addend)1649     Branch_info(Powerpc_relobj<size, big_endian>* ppc_object,
1650 		unsigned int data_shndx,
1651 		Address r_offset,
1652 		unsigned int r_type,
1653 		unsigned int r_sym,
1654 		Address addend)
1655       : object_(ppc_object), shndx_(data_shndx), offset_(r_offset),
1656 	r_type_(r_type), tocsave_ (0), r_sym_(r_sym), addend_(addend)
1657     { }
1658 
~Branch_info()1659     ~Branch_info()
1660     { }
1661 
1662     // Return whether this branch is going via a plt call stub, and if
1663     // so, mark it as having an R_PPC64_TOCSAVE.
1664     bool
1665     mark_pltcall(Powerpc_relobj<size, big_endian>* ppc_object,
1666 		 unsigned int shndx, Address offset,
1667 		 Target_powerpc* target, Symbol_table* symtab);
1668 
1669     // If this branch needs a plt call stub, or a long branch stub, make one.
1670     bool
1671     make_stub(Stub_table<size, big_endian>*,
1672 	      Stub_table<size, big_endian>*,
1673 	      Symbol_table*) const;
1674 
1675    private:
1676     // The branch location..
1677     Powerpc_relobj<size, big_endian>* object_;
1678     unsigned int shndx_;
1679     Address offset_;
1680     // ..and the branch type and destination.
1681     unsigned int r_type_ : 31;
1682     unsigned int tocsave_ : 1;
1683     unsigned int r_sym_;
1684     Address addend_;
1685   };
1686 
1687   // Information about this specific target which we pass to the
1688   // general Target structure.
1689   static Target::Target_info powerpc_info;
1690 
1691   // The small GOT section used by ppc32, and by ppc64 for entries that
1692   // must be addresseed +/-32k from the got pointer.
1693   Output_data_got_powerpc<size, big_endian>* got_;
1694   // Another GOT section used for entries that can be addressed +/- 2G
1695   // from the got pointer.
1696   Output_data_got_powerpc<size, big_endian>* biggot_;
1697 
1698   // The PLT section.  This is a container for a table of addresses,
1699   // and their relocations.  Each address in the PLT has a dynamic
1700   // relocation (R_*_JMP_SLOT) and each address will have a
1701   // corresponding entry in .glink for lazy resolution of the PLT.
1702   // ppc32 initialises the PLT to point at the .glink entry, while
1703   // ppc64 leaves this to ld.so.  To make a call via the PLT, the
1704   // linker adds a stub that loads the PLT entry into ctr then
1705   // branches to ctr.  There may be more than one stub for each PLT
1706   // entry.  DT_JMPREL points at the first PLT dynamic relocation and
1707   // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
1708   Output_data_plt_powerpc<size, big_endian>* plt_;
1709   // The IPLT section.  Like plt_, this is a container for a table of
1710   // addresses and their relocations, specifically for STT_GNU_IFUNC
1711   // functions that resolve locally (STT_GNU_IFUNC functions that
1712   // don't resolve locally go in PLT).  Unlike plt_, these have no
1713   // entry in .glink for lazy resolution, and the relocation section
1714   // does not have a 1-1 correspondence with IPLT addresses.  In fact,
1715   // the relocation section may contain relocations against
1716   // STT_GNU_IFUNC symbols at locations outside of IPLT.  The
1717   // relocation section will appear at the end of other dynamic
1718   // relocations, so that ld.so applies these relocations after other
1719   // dynamic relocations.  In a static executable, the relocation
1720   // section is emitted and marked with __rela_iplt_start and
1721   // __rela_iplt_end symbols.
1722   Output_data_plt_powerpc<size, big_endian>* iplt_;
1723   // A PLT style section for local, non-ifunc symbols
1724   Output_data_plt_powerpc<size, big_endian>* lplt_;
1725   // Section holding long branch destinations.
1726   Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
1727   // The .glink section.
1728   Output_data_glink<size, big_endian>* glink_;
1729   // The dynamic reloc section.
1730   Reloc_section* rela_dyn_;
1731   // Relocs saved to avoid a COPY reloc.
1732   Powerpc_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
1733   // Offset of the GOT entry for local dynamic __tls_get_addr calls.
1734   unsigned int tlsld_got_offset_;
1735 
1736   Stub_tables stub_tables_;
1737   typedef Unordered_map<Address, unsigned int> Branch_lookup_table;
1738   Branch_lookup_table branch_lookup_table_;
1739 
1740   typedef std::vector<Branch_info> Branches;
1741   Branches branch_info_;
1742   Tocsave_loc tocsave_loc_;
1743 
1744   bool power10_relocs_;
1745   bool plt_thread_safe_;
1746   bool plt_localentry0_;
1747   bool plt_localentry0_init_;
1748   bool has_localentry0_;
1749   bool has_tls_get_addr_opt_;
1750   bool no_tprel_opt_;
1751 
1752   bool relax_failed_;
1753   int relax_fail_count_;
1754   int32_t stub_group_size_;
1755 
1756   Output_data_save_res<size, big_endian> *savres_section_;
1757 
1758   // The "__tls_get_addr" symbol, if present
1759   Symbol* tls_get_addr_;
1760   // If optimizing __tls_get_addr calls, the "__tls_get_addr_opt" symbol.
1761   Symbol* tls_get_addr_opt_;
1762 
1763   // Attributes in output.
1764   Attributes_section_data* attributes_section_data_;
1765 
1766   // Last input file to change various attribute tags
1767   const char* last_fp_;
1768   const char* last_ld_;
1769   const char* last_vec_;
1770   const char* last_struct_;
1771 };
1772 
1773 template<>
1774 Target::Target_info Target_powerpc<32, true>::powerpc_info =
1775 {
1776   32,			// size
1777   true,			// is_big_endian
1778   elfcpp::EM_PPC,	// machine_code
1779   false,		// has_make_symbol
1780   false,		// has_resolve
1781   false,		// has_code_fill
1782   true,			// is_default_stack_executable
1783   false,		// can_icf_inline_merge_sections
1784   '\0',			// wrap_char
1785   "/usr/lib/ld.so.1",	// dynamic_linker
1786   0x10000000,		// default_text_segment_address
1787   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1788   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1789   false,		// isolate_execinstr
1790   0,			// rosegment_gap
1791   elfcpp::SHN_UNDEF,	// small_common_shndx
1792   elfcpp::SHN_UNDEF,	// large_common_shndx
1793   0,			// small_common_section_flags
1794   0,			// large_common_section_flags
1795   NULL,			// attributes_section
1796   NULL,			// attributes_vendor
1797   "_start",		// entry_symbol_name
1798   32,			// hash_entry_size
1799   elfcpp::SHT_PROGBITS,	// unwind_section_type
1800 };
1801 
1802 template<>
1803 Target::Target_info Target_powerpc<32, false>::powerpc_info =
1804 {
1805   32,			// size
1806   false,		// is_big_endian
1807   elfcpp::EM_PPC,	// machine_code
1808   false,		// has_make_symbol
1809   false,		// has_resolve
1810   false,		// has_code_fill
1811   true,			// is_default_stack_executable
1812   false,		// can_icf_inline_merge_sections
1813   '\0',			// wrap_char
1814   "/usr/lib/ld.so.1",	// dynamic_linker
1815   0x10000000,		// default_text_segment_address
1816   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1817   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1818   false,		// isolate_execinstr
1819   0,			// rosegment_gap
1820   elfcpp::SHN_UNDEF,	// small_common_shndx
1821   elfcpp::SHN_UNDEF,	// large_common_shndx
1822   0,			// small_common_section_flags
1823   0,			// large_common_section_flags
1824   NULL,			// attributes_section
1825   NULL,			// attributes_vendor
1826   "_start",		// entry_symbol_name
1827   32,			// hash_entry_size
1828   elfcpp::SHT_PROGBITS,	// unwind_section_type
1829 };
1830 
1831 template<>
1832 Target::Target_info Target_powerpc<64, true>::powerpc_info =
1833 {
1834   64,			// size
1835   true,			// is_big_endian
1836   elfcpp::EM_PPC64,	// machine_code
1837   false,		// has_make_symbol
1838   true,			// has_resolve
1839   false,		// has_code_fill
1840   false,		// is_default_stack_executable
1841   false,		// can_icf_inline_merge_sections
1842   '\0',			// wrap_char
1843   "/usr/lib/ld.so.1",	// dynamic_linker
1844   0x10000000,		// default_text_segment_address
1845   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1846   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1847   false,		// isolate_execinstr
1848   0,			// rosegment_gap
1849   elfcpp::SHN_UNDEF,	// small_common_shndx
1850   elfcpp::SHN_UNDEF,	// large_common_shndx
1851   0,			// small_common_section_flags
1852   0,			// large_common_section_flags
1853   NULL,			// attributes_section
1854   NULL,			// attributes_vendor
1855   "_start",		// entry_symbol_name
1856   32,			// hash_entry_size
1857   elfcpp::SHT_PROGBITS,	// unwind_section_type
1858 };
1859 
1860 template<>
1861 Target::Target_info Target_powerpc<64, false>::powerpc_info =
1862 {
1863   64,			// size
1864   false,		// is_big_endian
1865   elfcpp::EM_PPC64,	// machine_code
1866   false,		// has_make_symbol
1867   true,			// has_resolve
1868   false,		// has_code_fill
1869   false,		// is_default_stack_executable
1870   false,		// can_icf_inline_merge_sections
1871   '\0',			// wrap_char
1872   "/usr/lib/ld.so.1",	// dynamic_linker
1873   0x10000000,		// default_text_segment_address
1874   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
1875   4 * 1024,		// common_pagesize (overridable by -z common-page-size)
1876   false,		// isolate_execinstr
1877   0,			// rosegment_gap
1878   elfcpp::SHN_UNDEF,	// small_common_shndx
1879   elfcpp::SHN_UNDEF,	// large_common_shndx
1880   0,			// small_common_section_flags
1881   0,			// large_common_section_flags
1882   NULL,			// attributes_section
1883   NULL,			// attributes_vendor
1884   "_start",		// entry_symbol_name
1885   32,			// hash_entry_size
1886   elfcpp::SHT_PROGBITS,	// unwind_section_type
1887 };
1888 
1889 template<int size>
1890 inline bool
is_branch_reloc(unsigned int r_type)1891 is_branch_reloc(unsigned int r_type)
1892 {
1893   return (r_type == elfcpp::R_POWERPC_REL24
1894 	  || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
1895 	  || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
1896 	  || r_type == elfcpp::R_PPC_PLTREL24
1897 	  || r_type == elfcpp::R_PPC_LOCAL24PC
1898 	  || r_type == elfcpp::R_POWERPC_REL14
1899 	  || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
1900 	  || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN
1901 	  || r_type == elfcpp::R_POWERPC_ADDR24
1902 	  || r_type == elfcpp::R_POWERPC_ADDR14
1903 	  || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN
1904 	  || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN);
1905 }
1906 
1907 // Reloc resolves to plt entry.
1908 template<int size>
1909 inline bool
is_plt16_reloc(unsigned int r_type)1910 is_plt16_reloc(unsigned int r_type)
1911 {
1912   return (r_type == elfcpp::R_POWERPC_PLT16_LO
1913 	  || r_type == elfcpp::R_POWERPC_PLT16_HI
1914 	  || r_type == elfcpp::R_POWERPC_PLT16_HA
1915 	  || (size == 64 && r_type == elfcpp::R_PPC64_PLT16_LO_DS));
1916 }
1917 
1918 // GOT_TYPE_STANDARD or GOT_TYPE_SMALL (ie. not TLS) GOT relocs
1919 inline bool
is_got_reloc(unsigned int r_type)1920 is_got_reloc(unsigned int r_type)
1921 {
1922   return (r_type == elfcpp::R_POWERPC_GOT16
1923 	  || r_type == elfcpp::R_POWERPC_GOT16_LO
1924 	  || r_type == elfcpp::R_POWERPC_GOT16_HI
1925 	  || r_type == elfcpp::R_POWERPC_GOT16_HA
1926 	  || r_type == elfcpp::R_PPC64_GOT16_DS
1927 	  || r_type == elfcpp::R_PPC64_GOT16_LO_DS
1928 	  || r_type == elfcpp::R_PPC64_GOT_PCREL34);
1929 }
1930 
1931 // If INSN is an opcode that may be used with an @tls operand, return
1932 // the transformed insn for TLS optimisation, otherwise return 0.  If
1933 // REG is non-zero only match an insn with RB or RA equal to REG.
1934 uint32_t
at_tls_transform(uint32_t insn,unsigned int reg)1935 at_tls_transform(uint32_t insn, unsigned int reg)
1936 {
1937   if ((insn & (0x3f << 26)) != 31 << 26)
1938     return 0;
1939 
1940   unsigned int rtra;
1941   if (reg == 0 || ((insn >> 11) & 0x1f) == reg)
1942     rtra = insn & ((1 << 26) - (1 << 16));
1943   else if (((insn >> 16) & 0x1f) == reg)
1944     rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5);
1945   else
1946     return 0;
1947 
1948   if ((insn & (0x3ff << 1)) == 266 << 1)
1949     // add -> addi
1950     insn = 14 << 26;
1951   else if ((insn & (0x1f << 1)) == 23 << 1
1952 	   && ((insn & (0x1f << 6)) < 14 << 6
1953 	       || ((insn & (0x1f << 6)) >= 16 << 6
1954 		   && (insn & (0x1f << 6)) < 24 << 6)))
1955     // load and store indexed -> dform
1956     insn = (32 | ((insn >> 6) & 0x1f)) << 26;
1957   else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1)
1958     // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu
1959     insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1);
1960   else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1)
1961     // lwax -> lwa
1962     insn = (58 << 26) | 2;
1963   else
1964     return 0;
1965   insn |= rtra;
1966   return insn;
1967 }
1968 
1969 
1970 template<int size, bool big_endian>
1971 class Powerpc_relocate_functions
1972 {
1973 public:
1974   enum Overflow_check
1975   {
1976     CHECK_NONE,
1977     CHECK_SIGNED,
1978     CHECK_UNSIGNED,
1979     CHECK_BITFIELD,
1980     CHECK_LOW_INSN,
1981     CHECK_HIGH_INSN
1982   };
1983 
1984   enum Status
1985   {
1986     STATUS_OK,
1987     STATUS_OVERFLOW
1988   };
1989 
1990 private:
1991   typedef Powerpc_relocate_functions<size, big_endian> This;
1992   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
1993   typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
1994 
1995   template<int valsize>
1996   static inline bool
has_overflow_signed(Address value)1997   has_overflow_signed(Address value)
1998   {
1999     // limit = 1 << (valsize - 1) without shift count exceeding size of type
2000     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2001     limit <<= ((valsize - 1) >> 1);
2002     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2003     return value + limit > (limit << 1) - 1;
2004   }
2005 
2006   template<int valsize>
2007   static inline bool
has_overflow_unsigned(Address value)2008   has_overflow_unsigned(Address value)
2009   {
2010     Address limit = static_cast<Address>(1) << ((valsize - 1) >> 1);
2011     limit <<= ((valsize - 1) >> 1);
2012     limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1));
2013     return value > (limit << 1) - 1;
2014   }
2015 
2016   template<int valsize>
2017   static inline bool
has_overflow_bitfield(Address value)2018   has_overflow_bitfield(Address value)
2019   {
2020     return (has_overflow_unsigned<valsize>(value)
2021 	    && has_overflow_signed<valsize>(value));
2022   }
2023 
2024   template<int valsize>
2025   static inline Status
overflowed(Address value,Overflow_check overflow)2026   overflowed(Address value, Overflow_check overflow)
2027   {
2028     if (overflow == CHECK_SIGNED)
2029       {
2030 	if (has_overflow_signed<valsize>(value))
2031 	  return STATUS_OVERFLOW;
2032       }
2033     else if (overflow == CHECK_UNSIGNED)
2034       {
2035 	if (has_overflow_unsigned<valsize>(value))
2036 	  return STATUS_OVERFLOW;
2037       }
2038     else if (overflow == CHECK_BITFIELD)
2039       {
2040 	if (has_overflow_bitfield<valsize>(value))
2041 	  return STATUS_OVERFLOW;
2042       }
2043     return STATUS_OK;
2044   }
2045 
2046   // Do a simple RELA relocation
2047   template<int fieldsize, int valsize>
2048   static inline Status
rela(unsigned char * view,Address value,Overflow_check overflow)2049   rela(unsigned char* view, Address value, Overflow_check overflow)
2050   {
2051     typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2052     Valtype* wv = reinterpret_cast<Valtype*>(view);
2053     elfcpp::Swap<fieldsize, big_endian>::writeval(wv, value);
2054     return overflowed<valsize>(value, overflow);
2055   }
2056 
2057   template<int fieldsize, int valsize>
2058   static inline Status
rela(unsigned char * view,unsigned int right_shift,typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,Address value,Overflow_check overflow)2059   rela(unsigned char* view,
2060        unsigned int right_shift,
2061        typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2062        Address value,
2063        Overflow_check overflow)
2064   {
2065     typedef typename elfcpp::Swap<fieldsize, big_endian>::Valtype Valtype;
2066     Valtype* wv = reinterpret_cast<Valtype*>(view);
2067     Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(wv);
2068     if (overflow == CHECK_SIGNED)
2069       value = static_cast<SignedAddress>(value) >> right_shift;
2070     else
2071       value = value >> right_shift;
2072     Valtype reloc = value;
2073     val &= ~dst_mask;
2074     reloc &= dst_mask;
2075     elfcpp::Swap<fieldsize, big_endian>::writeval(wv, val | reloc);
2076     return overflowed<valsize>(value, overflow);
2077   }
2078 
2079   // Do a simple RELA relocation, unaligned.
2080   template<int fieldsize, int valsize>
2081   static inline Status
rela_ua(unsigned char * view,Address value,Overflow_check overflow)2082   rela_ua(unsigned char* view, Address value, Overflow_check overflow)
2083   {
2084     elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, value);
2085     return overflowed<valsize>(value, overflow);
2086   }
2087 
2088   template<int fieldsize, int valsize>
2089   static inline Status
rela_ua(unsigned char * view,unsigned int right_shift,typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,Address value,Overflow_check overflow)2090   rela_ua(unsigned char* view,
2091 	  unsigned int right_shift,
2092 	  typename elfcpp::Valtype_base<fieldsize>::Valtype dst_mask,
2093 	  Address value,
2094 	  Overflow_check overflow)
2095   {
2096     typedef typename elfcpp::Swap_unaligned<fieldsize, big_endian>::Valtype
2097       Valtype;
2098     Valtype val = elfcpp::Swap<fieldsize, big_endian>::readval(view);
2099     if (overflow == CHECK_SIGNED)
2100       value = static_cast<SignedAddress>(value) >> right_shift;
2101     else
2102       value = value >> right_shift;
2103     Valtype reloc = value;
2104     val &= ~dst_mask;
2105     reloc &= dst_mask;
2106     elfcpp::Swap_unaligned<fieldsize, big_endian>::writeval(view, val | reloc);
2107     return overflowed<valsize>(value, overflow);
2108   }
2109 
2110 public:
2111   // R_PPC64_ADDR64: (Symbol + Addend)
2112   static inline void
addr64(unsigned char * view,Address value)2113   addr64(unsigned char* view, Address value)
2114   { This::template rela<64,64>(view, value, CHECK_NONE); }
2115 
2116   // R_PPC64_UADDR64: (Symbol + Addend) unaligned
2117   static inline void
addr64_u(unsigned char * view,Address value)2118   addr64_u(unsigned char* view, Address value)
2119   { This::template rela_ua<64,64>(view, value, CHECK_NONE); }
2120 
2121   // R_POWERPC_ADDR32: (Symbol + Addend)
2122   static inline Status
addr32(unsigned char * view,Address value,Overflow_check overflow)2123   addr32(unsigned char* view, Address value, Overflow_check overflow)
2124   { return This::template rela<32,32>(view, value, overflow); }
2125 
2126   // R_POWERPC_UADDR32: (Symbol + Addend) unaligned
2127   static inline Status
addr32_u(unsigned char * view,Address value,Overflow_check overflow)2128   addr32_u(unsigned char* view, Address value, Overflow_check overflow)
2129   { return This::template rela_ua<32,32>(view, value, overflow); }
2130 
2131   // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc
2132   static inline Status
addr24(unsigned char * view,Address value,Overflow_check overflow)2133   addr24(unsigned char* view, Address value, Overflow_check overflow)
2134   {
2135     Status stat = This::template rela<32,26>(view, 0, 0x03fffffc,
2136 					     value, overflow);
2137     if (overflow != CHECK_NONE && (value & 3) != 0)
2138       stat = STATUS_OVERFLOW;
2139     return stat;
2140   }
2141 
2142   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff
2143   static inline Status
addr16(unsigned char * view,Address value,Overflow_check overflow)2144   addr16(unsigned char* view, Address value, Overflow_check overflow)
2145   { return This::template rela<16,16>(view, value, overflow); }
2146 
2147   // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned
2148   static inline Status
addr16_u(unsigned char * view,Address value,Overflow_check overflow)2149   addr16_u(unsigned char* view, Address value, Overflow_check overflow)
2150   { return This::template rela_ua<16,16>(view, value, overflow); }
2151 
2152   // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc
2153   static inline Status
addr16_ds(unsigned char * view,Address value,Overflow_check overflow)2154   addr16_ds(unsigned char* view, Address value, Overflow_check overflow)
2155   {
2156     Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow);
2157     if ((value & 3) != 0)
2158       stat = STATUS_OVERFLOW;
2159     return stat;
2160   }
2161 
2162   // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
2163   static inline Status
addr16_dq(unsigned char * view,Address value,Overflow_check overflow)2164   addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
2165   {
2166     Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
2167     if ((value & 15) != 0)
2168       stat = STATUS_OVERFLOW;
2169     return stat;
2170   }
2171 
2172   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
2173   static inline void
addr16_hi(unsigned char * view,Address value)2174   addr16_hi(unsigned char* view, Address value)
2175   { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); }
2176 
2177   // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff
2178   static inline void
addr16_ha(unsigned char * view,Address value)2179   addr16_ha(unsigned char* view, Address value)
2180   { This::addr16_hi(view, value + 0x8000); }
2181 
2182   // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff
2183   static inline void
addr16_hi2(unsigned char * view,Address value)2184   addr16_hi2(unsigned char* view, Address value)
2185   { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); }
2186 
2187   // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff
2188   static inline void
addr16_ha2(unsigned char * view,Address value)2189   addr16_ha2(unsigned char* view, Address value)
2190   { This::addr16_hi2(view, value + 0x8000); }
2191 
2192   // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff
2193   static inline void
addr16_hi3(unsigned char * view,Address value)2194   addr16_hi3(unsigned char* view, Address value)
2195   { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); }
2196 
2197   // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff
2198   static inline void
addr16_ha3(unsigned char * view,Address value)2199   addr16_ha3(unsigned char* view, Address value)
2200   { This::addr16_hi3(view, value + 0x8000); }
2201 
2202   // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc
2203   static inline Status
addr14(unsigned char * view,Address value,Overflow_check overflow)2204   addr14(unsigned char* view, Address value, Overflow_check overflow)
2205   {
2206     Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow);
2207     if (overflow != CHECK_NONE && (value & 3) != 0)
2208       stat = STATUS_OVERFLOW;
2209     return stat;
2210   }
2211 
2212   // R_POWERPC_REL16DX_HA
2213   static inline Status
addr16dx_ha(unsigned char * view,Address value,Overflow_check overflow)2214   addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
2215   {
2216     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
2217     Valtype* wv = reinterpret_cast<Valtype*>(view);
2218     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
2219     value += 0x8000;
2220     value = static_cast<SignedAddress>(value) >> 16;
2221     val |= (value & 0xffc1) | ((value & 0x3e) << 15);
2222     elfcpp::Swap<32, big_endian>::writeval(wv, val);
2223     return overflowed<16>(value, overflow);
2224   }
2225 
2226   // R_PPC64_D34
2227   static inline Status
addr34(unsigned char * view,uint64_t value,Overflow_check overflow)2228   addr34(unsigned char *view, uint64_t value, Overflow_check overflow)
2229   {
2230     Status stat = This::template rela<32,18>(view, 16, 0x3ffff,
2231 					     value, overflow);
2232     This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2233     return stat;
2234   }
2235 
2236   // R_PPC64_D34_HI30
2237   static inline void
addr34_hi(unsigned char * view,uint64_t value)2238   addr34_hi(unsigned char *view, uint64_t value)
2239   { This::addr34(view, value >> 34, CHECK_NONE);}
2240 
2241   // R_PPC64_D34_HA30
2242   static inline void
addr34_ha(unsigned char * view,uint64_t value)2243   addr34_ha(unsigned char *view, uint64_t value)
2244   { This::addr34_hi(view, value + (1ULL << 33));}
2245 
2246   // R_PPC64_D28
2247   static inline Status
addr28(unsigned char * view,uint64_t value,Overflow_check overflow)2248   addr28(unsigned char *view, uint64_t value, Overflow_check overflow)
2249   {
2250     Status stat = This::template rela<32,12>(view, 16, 0xfff,
2251 					     value, overflow);
2252     This::rela<32,16>(view + 4, 0, 0xffff, value, CHECK_NONE);
2253     return stat;
2254   }
2255 
2256   // R_PPC64_ADDR16_HIGHER34
2257   static inline void
addr16_higher34(unsigned char * view,uint64_t value)2258   addr16_higher34(unsigned char* view, uint64_t value)
2259   { This::addr16(view, value >> 34, CHECK_NONE); }
2260 
2261   // R_PPC64_ADDR16_HIGHERA34
2262   static inline void
addr16_highera34(unsigned char * view,uint64_t value)2263   addr16_highera34(unsigned char* view, uint64_t value)
2264   { This::addr16_higher34(view, value + (1ULL << 33)); }
2265 
2266   // R_PPC64_ADDR16_HIGHEST34
2267   static inline void
addr16_highest34(unsigned char * view,uint64_t value)2268   addr16_highest34(unsigned char* view, uint64_t value)
2269   { This::addr16(view, value >> 50, CHECK_NONE); }
2270 
2271   // R_PPC64_ADDR16_HIGHESTA34
2272   static inline void
addr16_highesta34(unsigned char * view,uint64_t value)2273   addr16_highesta34(unsigned char* view, uint64_t value)
2274   { This::addr16_highest34(view, value + (1ULL << 33)); }
2275 };
2276 
2277 // Set ABI version for input and output.
2278 
2279 template<int size, bool big_endian>
2280 void
set_abiversion(int ver)2281 Powerpc_relobj<size, big_endian>::set_abiversion(int ver)
2282 {
2283   this->e_flags_ |= ver;
2284   if (this->abiversion() != 0)
2285     {
2286       Target_powerpc<size, big_endian>* target =
2287 	static_cast<Target_powerpc<size, big_endian>*>(
2288 	   parameters->sized_target<size, big_endian>());
2289       if (target->abiversion() == 0)
2290 	target->set_abiversion(this->abiversion());
2291       else if (target->abiversion() != this->abiversion())
2292 	gold_error(_("%s: ABI version %d is not compatible "
2293 		     "with ABI version %d output"),
2294 		   this->name().c_str(),
2295 		   this->abiversion(), target->abiversion());
2296 
2297     }
2298 }
2299 
2300 // Stash away the index of .got2, .opd, .rela.toc, and .toc in a
2301 // relocatable object, if such sections exists.
2302 
2303 template<int size, bool big_endian>
2304 bool
do_find_special_sections(Read_symbols_data * sd)2305 Powerpc_relobj<size, big_endian>::do_find_special_sections(
2306     Read_symbols_data* sd)
2307 {
2308   const unsigned char* const pshdrs = sd->section_headers->data();
2309   const unsigned char* namesu = sd->section_names->data();
2310   const char* names = reinterpret_cast<const char*>(namesu);
2311   section_size_type names_size = sd->section_names_size;
2312   const unsigned char* s;
2313 
2314   s = this->template find_shdr<size, big_endian>(pshdrs,
2315 						 size == 32 ? ".got2" : ".opd",
2316 						 names, names_size, NULL);
2317   if (s != NULL)
2318     {
2319       unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2320       this->special_ = ndx;
2321       if (size == 64)
2322 	{
2323 	  if (this->abiversion() == 0)
2324 	    this->set_abiversion(1);
2325 	  else if (this->abiversion() > 1)
2326 	    gold_error(_("%s: .opd invalid in abiv%d"),
2327 		       this->name().c_str(), this->abiversion());
2328 	}
2329     }
2330   if (size == 64)
2331     {
2332       s = this->template find_shdr<size, big_endian>(pshdrs, ".rela.toc",
2333 						     names, names_size, NULL);
2334       if (s != NULL)
2335 	{
2336 	  unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
2337 	  this->relatoc_ = ndx;
2338 	  typename elfcpp::Shdr<size, big_endian> shdr(s);
2339 	  this->toc_ = this->adjust_shndx(shdr.get_sh_info());
2340 	}
2341     }
2342   return Sized_relobj_file<size, big_endian>::do_find_special_sections(sd);
2343 }
2344 
2345 // Examine .rela.opd to build info about function entry points.
2346 
2347 template<int size, bool big_endian>
2348 void
scan_opd_relocs(size_t reloc_count,const unsigned char * prelocs,const unsigned char * plocal_syms)2349 Powerpc_relobj<size, big_endian>::scan_opd_relocs(
2350     size_t reloc_count,
2351     const unsigned char* prelocs,
2352     const unsigned char* plocal_syms)
2353 {
2354   if (size == 64)
2355     {
2356       typedef typename elfcpp::Rela<size, big_endian> Reltype;
2357       const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
2358       const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2359       Address expected_off = 0;
2360       bool regular = true;
2361       unsigned int opd_ent_size = 0;
2362 
2363       for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
2364 	{
2365 	  Reltype reloc(prelocs);
2366 	  typename elfcpp::Elf_types<size>::Elf_WXword r_info
2367 	    = reloc.get_r_info();
2368 	  unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
2369 	  if (r_type == elfcpp::R_PPC64_ADDR64)
2370 	    {
2371 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
2372 	      typename elfcpp::Elf_types<size>::Elf_Addr value;
2373 	      bool is_ordinary;
2374 	      unsigned int shndx;
2375 	      if (r_sym < this->local_symbol_count())
2376 		{
2377 		  typename elfcpp::Sym<size, big_endian>
2378 		    lsym(plocal_syms + r_sym * sym_size);
2379 		  shndx = lsym.get_st_shndx();
2380 		  shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2381 		  value = lsym.get_st_value();
2382 		}
2383 	      else
2384 		shndx = this->symbol_section_and_value(r_sym, &value,
2385 						       &is_ordinary);
2386 	      this->set_opd_ent(reloc.get_r_offset(), shndx,
2387 				value + reloc.get_r_addend());
2388 	      if (i == 2)
2389 		{
2390 		  expected_off = reloc.get_r_offset();
2391 		  opd_ent_size = expected_off;
2392 		}
2393 	      else if (expected_off != reloc.get_r_offset())
2394 		regular = false;
2395 	      expected_off += opd_ent_size;
2396 	    }
2397 	  else if (r_type == elfcpp::R_PPC64_TOC)
2398 	    {
2399 	      if (expected_off - opd_ent_size + 8 != reloc.get_r_offset())
2400 		regular = false;
2401 	    }
2402 	  else
2403 	    {
2404 	      gold_warning(_("%s: unexpected reloc type %u in .opd section"),
2405 			   this->name().c_str(), r_type);
2406 	      regular = false;
2407 	    }
2408 	}
2409       if (reloc_count <= 2)
2410 	opd_ent_size = this->section_size(this->opd_shndx());
2411       if (opd_ent_size != 24 && opd_ent_size != 16)
2412 	regular = false;
2413       if (!regular)
2414 	{
2415 	  gold_warning(_("%s: .opd is not a regular array of opd entries"),
2416 		       this->name().c_str());
2417 	  opd_ent_size = 0;
2418 	}
2419     }
2420 }
2421 
2422 // Returns true if a code sequence loading the TOC entry at VALUE
2423 // relative to the TOC pointer can be converted into code calculating
2424 // a TOC pointer relative offset.
2425 // If so, the TOC pointer relative offset is stored to VALUE.
2426 
2427 template<int size, bool big_endian>
2428 bool
make_toc_relative(Target_powerpc<size,big_endian> * target,Address * value)2429 Powerpc_relobj<size, big_endian>::make_toc_relative(
2430     Target_powerpc<size, big_endian>* target,
2431     Address* value)
2432 {
2433   if (size != 64)
2434     return false;
2435 
2436   // With -mcmodel=medium code it is quite possible to have
2437   // toc-relative relocs referring to objects outside the TOC.
2438   // Don't try to look at a non-existent TOC.
2439   if (this->toc_shndx() == 0
2440       || this->output_section(this->toc_shndx()) == 0)
2441     return false;
2442 
2443   // Convert VALUE back to an address by adding got_base (see below),
2444   // then to an offset in the TOC by subtracting the TOC output
2445   // section address and the TOC output offset.
2446   Address off = (*value + target->toc_pointer()
2447 		 - this->output_section(this->toc_shndx())->address()
2448 		 - this->output_section_offset(this->toc_shndx()));
2449   // Is this offset in the TOC?  -mcmodel=medium code may be using
2450   // TOC relative access to variables outside the TOC.  Those of
2451   // course can't be optimized.  We also don't try to optimize code
2452   // that is using a different object's TOC.
2453   if (off >= this->section_size(this->toc_shndx()))
2454     return false;
2455 
2456   if (this->no_toc_opt(off))
2457     return false;
2458 
2459   section_size_type vlen;
2460   unsigned char* view = this->get_output_view(this->toc_shndx(), &vlen);
2461   Address addr = elfcpp::Swap<size, big_endian>::readval(view + off);
2462   // The TOC pointer
2463   Address got_base = target->toc_pointer();
2464   addr -= got_base;
2465   if (addr + (uint64_t) 0x80008000 >= (uint64_t) 1 << 32)
2466     return false;
2467 
2468   *value = addr;
2469   return true;
2470 }
2471 
2472 template<int size, bool big_endian>
2473 bool
make_got_relative(Target_powerpc<size,big_endian> * target,const Symbol_value<size> * psymval,Address addend,Address * value)2474 Powerpc_relobj<size, big_endian>::make_got_relative(
2475     Target_powerpc<size, big_endian>* target,
2476     const Symbol_value<size>* psymval,
2477     Address addend,
2478     Address* value)
2479 {
2480   Address addr = psymval->value(this, addend);
2481   Address got_base = target->toc_pointer();
2482   addr -= got_base;
2483   if (addr + 0x80008000 > 0xffffffff)
2484     return false;
2485 
2486   *value = addr;
2487   return true;
2488 }
2489 
2490 // Perform the Sized_relobj_file method, then set up opd info from
2491 // .opd relocs.
2492 
2493 template<int size, bool big_endian>
2494 void
do_read_relocs(Read_relocs_data * rd)2495 Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
2496 {
2497   Sized_relobj_file<size, big_endian>::do_read_relocs(rd);
2498   if (size == 64)
2499     {
2500       for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin();
2501 	   p != rd->relocs.end();
2502 	   ++p)
2503 	{
2504 	  if (p->data_shndx == this->opd_shndx())
2505 	    {
2506 	      uint64_t opd_size = this->section_size(this->opd_shndx());
2507 	      gold_assert(opd_size == static_cast<size_t>(opd_size));
2508 	      if (opd_size != 0)
2509 		{
2510 		  this->init_opd(opd_size);
2511 		  this->scan_opd_relocs(p->reloc_count, p->contents->data(),
2512 					rd->local_symbols->data());
2513 		}
2514 	      break;
2515 	    }
2516 	}
2517     }
2518 }
2519 
2520 // Read the symbols then set up st_other vector.
2521 
2522 template<int size, bool big_endian>
2523 void
do_read_symbols(Read_symbols_data * sd)2524 Powerpc_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2525 {
2526   this->base_read_symbols(sd);
2527   if (this->input_file()->format() != Input_file::FORMAT_ELF)
2528     return;
2529   if (size == 64)
2530     {
2531       const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2532       const unsigned char* const pshdrs = sd->section_headers->data();
2533       const unsigned int loccount = this->do_local_symbol_count();
2534       if (loccount != 0)
2535 	{
2536 	  this->st_other_.resize(loccount);
2537 	  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2538 	  off_t locsize = loccount * sym_size;
2539 	  const unsigned int symtab_shndx = this->symtab_shndx();
2540 	  const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size;
2541 	  typename elfcpp::Shdr<size, big_endian> shdr(psymtab);
2542 	  const unsigned char* psyms = this->get_view(shdr.get_sh_offset(),
2543 						      locsize, true, false);
2544 	  psyms += sym_size;
2545 	  for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
2546 	    {
2547 	      elfcpp::Sym<size, big_endian> sym(psyms);
2548 	      unsigned char st_other = sym.get_st_other();
2549 	      this->st_other_[i] = st_other;
2550 	      if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0)
2551 		{
2552 		  if (this->abiversion() == 0)
2553 		    this->set_abiversion(2);
2554 		  else if (this->abiversion() < 2)
2555 		    gold_error(_("%s: local symbol %d has invalid st_other"
2556 				 " for ABI version 1"),
2557 			       this->name().c_str(), i);
2558 		}
2559 	    }
2560 	}
2561     }
2562 
2563   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2564   const unsigned char* ps = sd->section_headers->data() + shdr_size;
2565   bool merge_attributes = false;
2566   for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
2567     {
2568       elfcpp::Shdr<size, big_endian> shdr(ps);
2569       switch (shdr.get_sh_type())
2570 	{
2571 	case elfcpp::SHT_GNU_ATTRIBUTES:
2572 	  {
2573 	    gold_assert(this->attributes_section_data_ == NULL);
2574 	    section_offset_type section_offset = shdr.get_sh_offset();
2575 	    section_size_type section_size =
2576 	      convert_to_section_size_type(shdr.get_sh_size());
2577 	    const unsigned char* view =
2578 	      this->get_view(section_offset, section_size, true, false);
2579 	    this->attributes_section_data_ =
2580 	      new Attributes_section_data(view, section_size);
2581 	  }
2582 	  break;
2583 
2584 	case elfcpp::SHT_SYMTAB:
2585 	  {
2586 	    // Sometimes an object has no contents except the section
2587 	    // name string table and an empty symbol table with the
2588 	    // undefined symbol.  We don't want to merge
2589 	    // processor-specific flags from such an object.
2590 	    const typename elfcpp::Elf_types<size>::Elf_WXword sym_size =
2591 	      elfcpp::Elf_sizes<size>::sym_size;
2592 	    if (shdr.get_sh_size() > sym_size)
2593 	      merge_attributes = true;
2594 	  }
2595 	  break;
2596 
2597 	case elfcpp::SHT_STRTAB:
2598 	  break;
2599 
2600 	default:
2601 	  merge_attributes = true;
2602 	  break;
2603 	}
2604     }
2605 
2606   if (!merge_attributes)
2607     {
2608       // Should rarely happen.
2609       delete this->attributes_section_data_;
2610       this->attributes_section_data_ = NULL;
2611     }
2612 }
2613 
2614 template<int size, bool big_endian>
2615 void
set_abiversion(int ver)2616 Powerpc_dynobj<size, big_endian>::set_abiversion(int ver)
2617 {
2618   this->e_flags_ |= ver;
2619   if (this->abiversion() != 0)
2620     {
2621       Target_powerpc<size, big_endian>* target =
2622 	static_cast<Target_powerpc<size, big_endian>*>(
2623 	  parameters->sized_target<size, big_endian>());
2624       if (target->abiversion() == 0)
2625 	target->set_abiversion(this->abiversion());
2626       else if (target->abiversion() != this->abiversion())
2627 	gold_error(_("%s: ABI version %d is not compatible "
2628 		     "with ABI version %d output"),
2629 		   this->name().c_str(),
2630 		   this->abiversion(), target->abiversion());
2631 
2632     }
2633 }
2634 
2635 // Call Sized_dynobj::base_read_symbols to read the symbols then
2636 // read .opd from a dynamic object, filling in opd_ent_ vector,
2637 
2638 template<int size, bool big_endian>
2639 void
do_read_symbols(Read_symbols_data * sd)2640 Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
2641 {
2642   this->base_read_symbols(sd);
2643   const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
2644   const unsigned char* ps =
2645     sd->section_headers->data() + shdr_size * (this->shnum() - 1);
2646   for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
2647     {
2648       elfcpp::Shdr<size, big_endian> shdr(ps);
2649       if (shdr.get_sh_type() == elfcpp::SHT_GNU_ATTRIBUTES)
2650 	{
2651 	  section_offset_type section_offset = shdr.get_sh_offset();
2652 	  section_size_type section_size =
2653 	    convert_to_section_size_type(shdr.get_sh_size());
2654 	  const unsigned char* view =
2655 	    this->get_view(section_offset, section_size, true, false);
2656 	  this->attributes_section_data_ =
2657 	    new Attributes_section_data(view, section_size);
2658 	  break;
2659 	}
2660     }
2661   if (size == 64)
2662     {
2663       const unsigned char* const pshdrs = sd->section_headers->data();
2664       const unsigned char* namesu = sd->section_names->data();
2665       const char* names = reinterpret_cast<const char*>(namesu);
2666       const unsigned char* s = NULL;
2667       const unsigned char* opd;
2668       section_size_type opd_size;
2669 
2670       // Find and read .opd section.
2671       while (1)
2672 	{
2673 	  s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
2674 							 sd->section_names_size,
2675 							 s);
2676 	  if (s == NULL)
2677 	    return;
2678 
2679 	  typename elfcpp::Shdr<size, big_endian> shdr(s);
2680 	  if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2681 	      && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
2682 	    {
2683 	      if (this->abiversion() == 0)
2684 		this->set_abiversion(1);
2685 	      else if (this->abiversion() > 1)
2686 		gold_error(_("%s: .opd invalid in abiv%d"),
2687 			   this->name().c_str(), this->abiversion());
2688 
2689 	      this->opd_shndx_ = (s - pshdrs) / shdr_size;
2690 	      this->opd_address_ = shdr.get_sh_addr();
2691 	      opd_size = convert_to_section_size_type(shdr.get_sh_size());
2692 	      opd = this->get_view(shdr.get_sh_offset(), opd_size,
2693 				   true, false);
2694 	      break;
2695 	    }
2696 	}
2697 
2698       // Build set of executable sections.
2699       // Using a set is probably overkill.  There is likely to be only
2700       // a few executable sections, typically .init, .text and .fini,
2701       // and they are generally grouped together.
2702       typedef std::set<Sec_info> Exec_sections;
2703       Exec_sections exec_sections;
2704       s = pshdrs;
2705       for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
2706 	{
2707 	  typename elfcpp::Shdr<size, big_endian> shdr(s);
2708 	  if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
2709 	      && ((shdr.get_sh_flags()
2710 		   & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2711 		  == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
2712 	      && shdr.get_sh_size() != 0)
2713 	    {
2714 	      exec_sections.insert(Sec_info(shdr.get_sh_addr(),
2715 					    shdr.get_sh_size(), i));
2716 	    }
2717 	}
2718       if (exec_sections.empty())
2719 	return;
2720 
2721       // Look over the OPD entries.  This is complicated by the fact
2722       // that some binaries will use two-word entries while others
2723       // will use the standard three-word entries.  In most cases
2724       // the third word (the environment pointer for languages like
2725       // Pascal) is unused and will be zero.  If the third word is
2726       // used it should not be pointing into executable sections,
2727       // I think.
2728       this->init_opd(opd_size);
2729       for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
2730 	{
2731 	  typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
2732 	  const Valtype* valp = reinterpret_cast<const Valtype*>(p);
2733 	  Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
2734 	  if (val == 0)
2735 	    // Chances are that this is the third word of an OPD entry.
2736 	    continue;
2737 	  typename Exec_sections::const_iterator e
2738 	    = exec_sections.upper_bound(Sec_info(val, 0, 0));
2739 	  if (e != exec_sections.begin())
2740 	    {
2741 	      --e;
2742 	      if (e->start <= val && val < e->start + e->len)
2743 		{
2744 		  // We have an address in an executable section.
2745 		  // VAL ought to be the function entry, set it up.
2746 		  this->set_opd_ent(p - opd, e->shndx, val);
2747 		  // Skip second word of OPD entry, the TOC pointer.
2748 		  p += 8;
2749 		}
2750 	    }
2751 	  // If we didn't match any executable sections, we likely
2752 	  // have a non-zero third word in the OPD entry.
2753 	}
2754     }
2755 }
2756 
2757 // Relocate sections.
2758 
2759 template<int size, bool big_endian>
2760 void
do_relocate_sections(const Symbol_table * symtab,const Layout * layout,const unsigned char * pshdrs,Output_file * of,typename Sized_relobj_file<size,big_endian>::Views * pviews)2761 Powerpc_relobj<size, big_endian>::do_relocate_sections(
2762     const Symbol_table* symtab, const Layout* layout,
2763     const unsigned char* pshdrs, Output_file* of,
2764     typename Sized_relobj_file<size, big_endian>::Views* pviews)
2765 {
2766   unsigned int start = 1;
2767   if (size == 64
2768       && this->relatoc_ != 0
2769       && !parameters->options().relocatable())
2770     {
2771       // Relocate .toc first.
2772       this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2773 				   this->relatoc_, this->relatoc_);
2774       this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2775 				   1, this->relatoc_ - 1);
2776       start = this->relatoc_ + 1;
2777     }
2778   this->relocate_section_range(symtab, layout, pshdrs, of, pviews,
2779 			       start, this->shnum() - 1);
2780 }
2781 
2782 // Set up some symbols.
2783 
2784 template<int size, bool big_endian>
2785 void
do_define_standard_symbols(Symbol_table * symtab,Layout * layout)2786 Target_powerpc<size, big_endian>::do_define_standard_symbols(
2787     Symbol_table* symtab,
2788     Layout* layout)
2789 {
2790   if (size == 32)
2791     {
2792       // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as
2793       // undefined when scanning relocs (and thus requires
2794       // non-relative dynamic relocs).  The proper value will be
2795       // updated later.
2796       Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
2797       if (gotsym != NULL && gotsym->is_undefined())
2798 	{
2799 	  Target_powerpc<size, big_endian>* target =
2800 	    static_cast<Target_powerpc<size, big_endian>*>(
2801 		parameters->sized_target<size, big_endian>());
2802 	  Output_data_got_powerpc<size, big_endian>* got
2803 	    = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2804 	  symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
2805 					Symbol_table::PREDEFINED,
2806 					got, 0, 0,
2807 					elfcpp::STT_OBJECT,
2808 					elfcpp::STB_LOCAL,
2809 					elfcpp::STV_HIDDEN, 0,
2810 					false, false);
2811 	}
2812 
2813       // Define _SDA_BASE_ at the start of the .sdata section + 32768.
2814       Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL);
2815       if (sdasym != NULL && sdasym->is_undefined())
2816 	{
2817 	  Output_data_space* sdata = new Output_data_space(4, "** sdata");
2818 	  Output_section* os
2819 	    = layout->add_output_section_data(".sdata", 0,
2820 					      elfcpp::SHF_ALLOC
2821 					      | elfcpp::SHF_WRITE,
2822 					      sdata, ORDER_SMALL_DATA, false);
2823 	  symtab->define_in_output_data("_SDA_BASE_", NULL,
2824 					Symbol_table::PREDEFINED,
2825 					os, 32768, 0, elfcpp::STT_OBJECT,
2826 					elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN,
2827 					0, false, false);
2828 	}
2829     }
2830   else
2831     {
2832       // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_
2833       Symbol *gotsym = symtab->lookup(".TOC.", NULL);
2834       if (gotsym != NULL && gotsym->is_undefined())
2835 	{
2836 	  Target_powerpc<size, big_endian>* target =
2837 	    static_cast<Target_powerpc<size, big_endian>*>(
2838 		parameters->sized_target<size, big_endian>());
2839 	  Output_data_got_powerpc<size, big_endian>* got
2840 	    = target->got_section(symtab, layout, GOT_TYPE_SMALL);
2841 	  symtab->define_in_output_data(".TOC.", NULL,
2842 					Symbol_table::PREDEFINED,
2843 					got, 0x8000, 0,
2844 					elfcpp::STT_OBJECT,
2845 					elfcpp::STB_LOCAL,
2846 					elfcpp::STV_HIDDEN, 0,
2847 					false, false);
2848 	}
2849     }
2850 
2851   this->tls_get_addr_ = symtab->lookup("__tls_get_addr");
2852   if (parameters->options().tls_get_addr_optimize()
2853       && this->tls_get_addr_ != NULL
2854       && this->tls_get_addr_->in_reg())
2855     this->tls_get_addr_opt_ = symtab->lookup("__tls_get_addr_opt");
2856   if (this->tls_get_addr_opt_ != NULL)
2857     {
2858       if (this->tls_get_addr_->is_undefined()
2859 	  || this->tls_get_addr_->is_from_dynobj())
2860 	{
2861 	  // Make it seem as if references to __tls_get_addr are
2862 	  // really to __tls_get_addr_opt, so the latter symbol is
2863 	  // made dynamic, not the former.
2864 	  this->tls_get_addr_->clear_in_reg();
2865 	  this->tls_get_addr_opt_->set_in_reg();
2866 	}
2867       // We have a non-dynamic definition for __tls_get_addr.
2868       // Make __tls_get_addr_opt the same, if it does not already have
2869       // a non-dynamic definition.
2870       else if (this->tls_get_addr_opt_->is_undefined()
2871 	       || this->tls_get_addr_opt_->is_from_dynobj())
2872 	{
2873 	  Sized_symbol<size>* from
2874 	    = static_cast<Sized_symbol<size>*>(this->tls_get_addr_);
2875 	  Sized_symbol<size>* to
2876 	    = static_cast<Sized_symbol<size>*>(this->tls_get_addr_opt_);
2877 	  symtab->clone<size>(to, from);
2878 	}
2879     }
2880 }
2881 
2882 // Set up PowerPC target specific relobj.
2883 
2884 template<int size, bool big_endian>
2885 Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<size,big_endian> & ehdr)2886 Target_powerpc<size, big_endian>::do_make_elf_object(
2887     const std::string& name,
2888     Input_file* input_file,
2889     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2890 {
2891   int et = ehdr.get_e_type();
2892   // ET_EXEC files are valid input for --just-symbols/-R,
2893   // and we treat them as relocatable objects.
2894   if (et == elfcpp::ET_REL
2895       || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
2896     {
2897       Powerpc_relobj<size, big_endian>* obj =
2898 	new Powerpc_relobj<size, big_endian>(name, input_file, offset, ehdr);
2899       obj->setup();
2900       return obj;
2901     }
2902   else if (et == elfcpp::ET_DYN)
2903     {
2904       Powerpc_dynobj<size, big_endian>* obj =
2905 	new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2906       obj->setup();
2907       return obj;
2908     }
2909   else
2910     {
2911       gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et);
2912       return NULL;
2913     }
2914 }
2915 
2916 template<int size, bool big_endian>
2917 class Output_data_got_powerpc : public Output_data_got<size, big_endian>
2918 {
2919 public:
2920   typedef typename elfcpp::Elf_types<size>::Elf_Addr Valtype;
2921   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Rela_dyn;
2922 
Output_data_got_powerpc(Symbol_table * symtab,Layout * layout,Got_type got_type)2923   Output_data_got_powerpc(Symbol_table* symtab, Layout* layout,
2924 			  Got_type got_type)
2925     : Output_data_got<size, big_endian>(),
2926       symtab_(symtab), layout_(layout),
2927       header_ent_cnt_(size == 32 ? 3 : 1),
2928       header_index_(size == 32 ? 0x2000 : -1u)
2929   {
2930     if (size == 64)
2931       this->set_addralign(256);
2932     if (size == 64 && (got_type & GOT_TYPE_SMALL))
2933       this->make_header();
2934   }
2935 
2936   // Override all the Output_data_got methods we use so as to first call
2937   // reserve_ent().
2938   bool
add_global(Symbol * gsym,unsigned int got_type,uint64_t addend)2939   add_global(Symbol* gsym, unsigned int got_type, uint64_t addend)
2940   {
2941     this->reserve_ent();
2942     return Output_data_got<size, big_endian>::add_global(gsym, got_type,
2943 							 addend);
2944   }
2945 
2946   bool
add_global_plt(Symbol * gsym,unsigned int got_type,uint64_t addend)2947   add_global_plt(Symbol* gsym, unsigned int got_type, uint64_t addend)
2948   {
2949     this->reserve_ent();
2950     return Output_data_got<size, big_endian>::add_global_plt(gsym, got_type,
2951 							     addend);
2952   }
2953 
2954   bool
add_global_tls(Symbol * gsym,unsigned int got_type,uint64_t addend)2955   add_global_tls(Symbol* gsym, unsigned int got_type, uint64_t addend)
2956   { return this->add_global_plt(gsym, got_type, addend); }
2957 
2958   void
add_global_with_rel(Symbol * gsym,unsigned int got_type,Output_data_reloc_generic * rel_dyn,unsigned int r_type,uint64_t addend)2959   add_global_with_rel(Symbol* gsym, unsigned int got_type,
2960 		      Output_data_reloc_generic* rel_dyn,
2961 		      unsigned int r_type, uint64_t addend)
2962   {
2963     this->reserve_ent();
2964     Output_data_got<size, big_endian>::
2965       add_global_with_rel(gsym, got_type, rel_dyn, r_type, addend);
2966   }
2967 
2968   void
add_global_pair_with_rel(Symbol * gsym,unsigned int got_type,Output_data_reloc_generic * rel_dyn,unsigned int r_type_1,unsigned int r_type_2,uint64_t addend)2969   add_global_pair_with_rel(Symbol* gsym, unsigned int got_type,
2970 			   Output_data_reloc_generic* rel_dyn,
2971 			   unsigned int r_type_1, unsigned int r_type_2,
2972 			   uint64_t addend)
2973   {
2974     if (gsym->has_got_offset(got_type))
2975       return;
2976 
2977     this->reserve_ent(2);
2978     Output_data_got<size, big_endian>::
2979       add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2,
2980 			       addend);
2981   }
2982 
2983   bool
add_local(Relobj * object,unsigned int sym_index,unsigned int got_type,uint64_t addend)2984   add_local(Relobj* object, unsigned int sym_index, unsigned int got_type,
2985 	    uint64_t addend)
2986   {
2987     this->reserve_ent();
2988     return Output_data_got<size, big_endian>::add_local(object, sym_index,
2989 							got_type, addend);
2990   }
2991 
2992   bool
add_local_plt(Relobj * object,unsigned int sym_index,unsigned int got_type,uint64_t addend)2993   add_local_plt(Relobj* object, unsigned int sym_index,
2994 		unsigned int got_type, uint64_t addend)
2995   {
2996     this->reserve_ent();
2997     return Output_data_got<size, big_endian>::add_local_plt(object, sym_index,
2998 							    got_type, addend);
2999   }
3000 
3001   bool
add_local_tls(Relobj * object,unsigned int sym_index,unsigned int got_type,uint64_t addend)3002   add_local_tls(Relobj* object, unsigned int sym_index,
3003 		unsigned int got_type, uint64_t addend)
3004   { return this->add_local_plt(object, sym_index, got_type, addend); }
3005 
3006   void
add_local_tls_pair(Relobj * object,unsigned int sym_index,unsigned int got_type,Output_data_reloc_generic * rel_dyn,unsigned int r_type,uint64_t addend)3007   add_local_tls_pair(Relobj* object, unsigned int sym_index,
3008 		     unsigned int got_type,
3009 		     Output_data_reloc_generic* rel_dyn,
3010 		     unsigned int r_type, uint64_t addend)
3011   {
3012     if (object->local_has_got_offset(sym_index, got_type, addend))
3013       return;
3014 
3015     this->reserve_ent(2);
3016     Output_data_got<size, big_endian>::
3017       add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type, addend);
3018   }
3019 
3020   unsigned int
add_constant(Valtype constant)3021   add_constant(Valtype constant)
3022   {
3023     this->reserve_ent();
3024     return Output_data_got<size, big_endian>::add_constant(constant);
3025   }
3026 
3027   unsigned int
add_constant_pair(Valtype c1,Valtype c2)3028   add_constant_pair(Valtype c1, Valtype c2)
3029   {
3030     this->reserve_ent(2);
3031     return Output_data_got<size, big_endian>::add_constant_pair(c1, c2);
3032   }
3033 
3034   // Offset of _GLOBAL_OFFSET_TABLE_ and .TOC. in this section.
3035   unsigned int
g_o_t() const3036   g_o_t() const
3037   {
3038     if (size == 32)
3039       return this->got_offset(this->header_index_);
3040     else if (this->header_index_ != -1u)
3041       return this->got_offset(this->header_index_) + 0x8000;
3042     else
3043       gold_unreachable();
3044   }
3045 
3046   // Ensure our GOT has a header.
3047   void
set_final_data_size()3048   set_final_data_size()
3049   {
3050     if (size == 32 && this->header_ent_cnt_ != 0)
3051       this->make_header();
3052     Output_data_got<size, big_endian>::set_final_data_size();
3053   }
3054 
3055   // First word of GOT header needs some values that are not
3056   // handled by Output_data_got so poke them in here.
3057   // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase.
3058   void
do_write(Output_file * of)3059   do_write(Output_file* of)
3060   {
3061     if (this->header_index_ != -1u)
3062       {
3063 	Valtype val = 0;
3064 	if (size == 32 && this->layout_->dynamic_data() != NULL)
3065 	  val = this->layout_->dynamic_section()->address();
3066 	if (size == 64)
3067 	  val = this->address() + this->g_o_t();
3068 	this->replace_constant(this->header_index_, val);
3069       }
3070     Output_data_got<size, big_endian>::do_write(of);
3071   }
3072 
3073 private:
3074   void
reserve_ent(unsigned int cnt=1)3075   reserve_ent(unsigned int cnt = 1)
3076   {
3077     if (size != 32 || this->header_ent_cnt_ == 0)
3078       return;
3079     if (this->num_entries() + cnt > this->header_index_)
3080       this->make_header();
3081   }
3082 
3083   void
make_header()3084   make_header()
3085   {
3086     this->header_ent_cnt_ = 0;
3087     this->header_index_ = this->num_entries();
3088     if (size == 32)
3089       {
3090 	Output_data_got<size, big_endian>::add_constant(0);
3091 	Output_data_got<size, big_endian>::add_constant(0);
3092 	Output_data_got<size, big_endian>::add_constant(0);
3093 
3094 	// Define _GLOBAL_OFFSET_TABLE_ at the header
3095 	Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL);
3096 	if (gotsym != NULL)
3097 	  {
3098 	    Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(gotsym);
3099 	    sym->set_value(this->g_o_t());
3100 	  }
3101 	else
3102 	  this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
3103 					       Symbol_table::PREDEFINED,
3104 					       this, this->g_o_t(), 0,
3105 					       elfcpp::STT_OBJECT,
3106 					       elfcpp::STB_LOCAL,
3107 					       elfcpp::STV_HIDDEN, 0,
3108 					       false, false);
3109       }
3110     else
3111       Output_data_got<size, big_endian>::add_constant(0);
3112   }
3113 
3114   // Stashed pointers.
3115   Symbol_table* symtab_;
3116   Layout* layout_;
3117 
3118   // GOT header size.
3119   unsigned int header_ent_cnt_;
3120   // GOT header index.
3121   unsigned int header_index_;
3122 };
3123 
3124 // Get the GOT section, creating it if necessary.
3125 
3126 template<int size, bool big_endian>
3127 Output_data_got_powerpc<size, big_endian>*
got_section(Symbol_table * symtab,Layout * layout,Got_type got_type)3128 Target_powerpc<size, big_endian>::got_section(Symbol_table* symtab,
3129 					      Layout* layout,
3130 					      Got_type got_type)
3131 {
3132   if (this->got_ == NULL)
3133     {
3134       gold_assert(symtab != NULL && layout != NULL);
3135 
3136       this->got_
3137 	= new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3138 							GOT_TYPE_SMALL);
3139 
3140       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3141 				      elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3142 				      this->got_, ORDER_DATA, false);
3143     }
3144 
3145   if (size == 32 || (got_type & GOT_TYPE_SMALL))
3146     return this->got_;
3147 
3148   if (this->biggot_ == NULL)
3149     {
3150       this->biggot_
3151 	= new Output_data_got_powerpc<size, big_endian>(symtab, layout,
3152 							GOT_TYPE_STANDARD);
3153 
3154       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
3155 				      elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
3156 				      this->biggot_, ORDER_DATA, false);
3157     }
3158 
3159   return this->biggot_;
3160 }
3161 
3162 // Get the dynamic reloc section, creating it if necessary.
3163 
3164 template<int size, bool big_endian>
3165 typename Target_powerpc<size, big_endian>::Reloc_section*
rela_dyn_section(Layout * layout)3166 Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
3167 {
3168   if (this->rela_dyn_ == NULL)
3169     {
3170       gold_assert(layout != NULL);
3171       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
3172       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
3173 				      elfcpp::SHF_ALLOC, this->rela_dyn_,
3174 				      ORDER_DYNAMIC_RELOCS, false);
3175     }
3176   return this->rela_dyn_;
3177 }
3178 
3179 // Similarly, but for ifunc symbols get the one for ifunc.
3180 
3181 template<int size, bool big_endian>
3182 typename Target_powerpc<size, big_endian>::Reloc_section*
rela_dyn_section(Symbol_table * symtab,Layout * layout,bool for_ifunc)3183 Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
3184 						   Layout* layout,
3185 						   bool for_ifunc)
3186 {
3187   if (!for_ifunc)
3188     return this->rela_dyn_section(layout);
3189 
3190   if (this->iplt_ == NULL)
3191     this->make_iplt_section(symtab, layout);
3192   return this->iplt_->rel_plt();
3193 }
3194 
3195 class Stub_control
3196 {
3197  public:
3198   // Determine the stub group size.  The group size is the absolute
3199   // value of the parameter --stub-group-size.  If --stub-group-size
3200   // is passed a negative value, we restrict stubs to be always after
3201   // the stubbed branches.
Stub_control(int32_t size,bool no_size_errors,bool multi_os)3202   Stub_control(int32_t size, bool no_size_errors, bool multi_os)
3203     : stub_group_size_(abs(size)), stubs_always_after_branch_(size < 0),
3204       suppress_size_errors_(no_size_errors), multi_os_(multi_os),
3205       state_(NO_GROUP), group_size_(0), group_start_addr_(0),
3206       owner_(NULL), output_section_(NULL)
3207   {
3208   }
3209 
3210   // Return true iff input section can be handled by current stub
3211   // group.
3212   bool
3213   can_add_to_stub_group(Output_section* o,
3214 			const Output_section::Input_section* i,
3215 			bool has14);
3216 
3217   const Output_section::Input_section*
owner()3218   owner()
3219   { return owner_; }
3220 
3221   Output_section*
output_section()3222   output_section()
3223   { return output_section_; }
3224 
3225   void
set_output_and_owner(Output_section * o,const Output_section::Input_section * i)3226   set_output_and_owner(Output_section* o,
3227 		       const Output_section::Input_section* i)
3228   {
3229     this->output_section_ = o;
3230     this->owner_ = i;
3231   }
3232 
3233  private:
3234   typedef enum
3235   {
3236     // Initial state.
3237     NO_GROUP,
3238     // Adding group sections before the stubs.
3239     FINDING_STUB_SECTION,
3240     // Adding group sections after the stubs.
3241     HAS_STUB_SECTION
3242   } State;
3243 
3244   uint32_t stub_group_size_;
3245   bool stubs_always_after_branch_;
3246   bool suppress_size_errors_;
3247   // True if a stub group can serve multiple output sections.
3248   bool multi_os_;
3249   State state_;
3250   // Current max size of group.  Starts at stub_group_size_ but is
3251   // reduced to stub_group_size_/1024 on seeing a section with
3252   // external conditional branches.
3253   uint32_t group_size_;
3254   uint64_t group_start_addr_;
3255   // owner_ and output_section_ specify the section to which stubs are
3256   // attached.  The stubs are placed at the end of this section.
3257   const Output_section::Input_section* owner_;
3258   Output_section* output_section_;
3259 };
3260 
3261 // Return true iff input section can be handled by current stub
3262 // group.  Sections are presented to this function in order,
3263 // so the first section is the head of the group.
3264 
3265 bool
can_add_to_stub_group(Output_section * o,const Output_section::Input_section * i,bool has14)3266 Stub_control::can_add_to_stub_group(Output_section* o,
3267 				    const Output_section::Input_section* i,
3268 				    bool has14)
3269 {
3270   bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI;
3271   uint64_t this_size;
3272   uint64_t start_addr = o->address();
3273 
3274   if (whole_sec)
3275     // .init and .fini sections are pasted together to form a single
3276     // function.  We can't be adding stubs in the middle of the function.
3277     this_size = o->data_size();
3278   else
3279     {
3280       start_addr += i->relobj()->output_section_offset(i->shndx());
3281       this_size = i->data_size();
3282     }
3283 
3284   uint64_t end_addr = start_addr + this_size;
3285   uint32_t group_size = this->stub_group_size_;
3286   if (has14)
3287     this->group_size_ = group_size = group_size >> 10;
3288 
3289   if (this_size > group_size && !this->suppress_size_errors_)
3290     gold_warning(_("%s:%s exceeds group size"),
3291 		 i->relobj()->name().c_str(),
3292 		 i->relobj()->section_name(i->shndx()).c_str());
3293 
3294   gold_debug(DEBUG_TARGET, "maybe add%s %s:%s size=%#llx total=%#llx",
3295 	     has14 ? " 14bit" : "",
3296 	     i->relobj()->name().c_str(),
3297 	     i->relobj()->section_name(i->shndx()).c_str(),
3298 	     (long long) this_size,
3299 	     (this->state_ == NO_GROUP
3300 	      ? this_size
3301 	      : (long long) end_addr - this->group_start_addr_));
3302 
3303   if (this->state_ == NO_GROUP)
3304     {
3305       // Only here on very first use of Stub_control
3306       this->owner_ = i;
3307       this->output_section_ = o;
3308       this->state_ = FINDING_STUB_SECTION;
3309       this->group_size_ = group_size;
3310       this->group_start_addr_ = start_addr;
3311       return true;
3312     }
3313   else if (!this->multi_os_ && this->output_section_ != o)
3314     ;
3315   else if (this->state_ == HAS_STUB_SECTION)
3316     {
3317       // Can we add this section, which is after the stubs, to the
3318       // group?
3319       if (end_addr - this->group_start_addr_ <= this->group_size_)
3320 	return true;
3321     }
3322   else if (this->state_ == FINDING_STUB_SECTION)
3323     {
3324       if ((whole_sec && this->output_section_ == o)
3325 	  || end_addr - this->group_start_addr_ <= this->group_size_)
3326 	{
3327 	  // Stubs are added at the end of "owner_".
3328 	  this->owner_ = i;
3329 	  this->output_section_ = o;
3330 	  return true;
3331 	}
3332       // The group before the stubs has reached maximum size.
3333       // Now see about adding sections after the stubs to the
3334       // group.  If the current section has a 14-bit branch and
3335       // the group before the stubs exceeds group_size_ (because
3336       // they didn't have 14-bit branches), don't add sections
3337       // after the stubs:  The size of stubs for such a large
3338       // group may exceed the reach of a 14-bit branch.
3339       if (!this->stubs_always_after_branch_
3340 	  && this_size <= this->group_size_
3341 	  && start_addr - this->group_start_addr_ <= this->group_size_)
3342 	{
3343 	  gold_debug(DEBUG_TARGET, "adding after stubs");
3344 	  this->state_ = HAS_STUB_SECTION;
3345 	  this->group_start_addr_ = start_addr;
3346 	  return true;
3347 	}
3348     }
3349   else
3350     gold_unreachable();
3351 
3352   gold_debug(DEBUG_TARGET,
3353 	     !this->multi_os_ && this->output_section_ != o
3354 	     ? "nope, new output section\n"
3355 	     : "nope, didn't fit\n");
3356 
3357   // The section fails to fit in the current group.  Set up a few
3358   // things for the next group.  owner_ and output_section_ will be
3359   // set later after we've retrieved those values for the current
3360   // group.
3361   this->state_ = FINDING_STUB_SECTION;
3362   this->group_size_ = group_size;
3363   this->group_start_addr_ = start_addr;
3364   return false;
3365 }
3366 
3367 // Look over all the input sections, deciding where to place stubs.
3368 
3369 template<int size, bool big_endian>
3370 void
group_sections(Layout * layout,const Task *,bool no_size_errors)3371 Target_powerpc<size, big_endian>::group_sections(Layout* layout,
3372 						 const Task*,
3373 						 bool no_size_errors)
3374 {
3375   Stub_control stub_control(this->stub_group_size_, no_size_errors,
3376 			    parameters->options().stub_group_multi());
3377 
3378   // Group input sections and insert stub table
3379   Stub_table_owner* table_owner = NULL;
3380   std::vector<Stub_table_owner*> tables;
3381   Layout::Section_list section_list;
3382   layout->get_executable_sections(&section_list);
3383   std::stable_sort(section_list.begin(), section_list.end(), Sort_sections());
3384   for (Layout::Section_list::iterator o = section_list.begin();
3385        o != section_list.end();
3386        ++o)
3387     {
3388       typedef Output_section::Input_section_list Input_section_list;
3389       for (Input_section_list::const_iterator i
3390 	     = (*o)->input_sections().begin();
3391 	   i != (*o)->input_sections().end();
3392 	   ++i)
3393 	{
3394 	  if (i->is_input_section()
3395 	      || i->is_relaxed_input_section())
3396 	    {
3397 	      Powerpc_relobj<size, big_endian>* ppcobj = static_cast
3398 		<Powerpc_relobj<size, big_endian>*>(i->relobj());
3399 	      bool has14 = ppcobj->has_14bit_branch(i->shndx());
3400 	      if (!stub_control.can_add_to_stub_group(*o, &*i, has14))
3401 		{
3402 		  table_owner->output_section = stub_control.output_section();
3403 		  table_owner->owner = stub_control.owner();
3404 		  stub_control.set_output_and_owner(*o, &*i);
3405 		  table_owner = NULL;
3406 		}
3407 	      if (table_owner == NULL)
3408 		{
3409 		  table_owner = new Stub_table_owner;
3410 		  tables.push_back(table_owner);
3411 		}
3412 	      ppcobj->set_stub_table(i->shndx(), tables.size() - 1);
3413 	    }
3414 	}
3415     }
3416   if (table_owner != NULL)
3417     {
3418       table_owner->output_section = stub_control.output_section();
3419       table_owner->owner = stub_control.owner();;
3420     }
3421   for (typename std::vector<Stub_table_owner*>::iterator t = tables.begin();
3422        t != tables.end();
3423        ++t)
3424     {
3425       Stub_table<size, big_endian>* stub_table;
3426 
3427       if ((*t)->owner->is_input_section())
3428 	stub_table = new Stub_table<size, big_endian>(this,
3429 						      (*t)->output_section,
3430 						      (*t)->owner,
3431 						      this->stub_tables_.size());
3432       else if ((*t)->owner->is_relaxed_input_section())
3433 	stub_table = static_cast<Stub_table<size, big_endian>*>(
3434 			(*t)->owner->relaxed_input_section());
3435       else
3436 	gold_unreachable();
3437       this->stub_tables_.push_back(stub_table);
3438       delete *t;
3439     }
3440 }
3441 
3442 template<int size>
3443 static unsigned long
max_branch_delta(unsigned int r_type)3444 max_branch_delta (unsigned int r_type)
3445 {
3446   if (r_type == elfcpp::R_POWERPC_REL14
3447       || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN
3448       || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN)
3449     return 1L << 15;
3450   if (r_type == elfcpp::R_POWERPC_REL24
3451       || (size == 64 && r_type == elfcpp::R_PPC64_REL24_NOTOC)
3452       || r_type == elfcpp::R_PPC64_REL24_P9NOTOC
3453       || r_type == elfcpp::R_PPC_PLTREL24
3454       || r_type == elfcpp::R_PPC_LOCAL24PC)
3455     return 1L << 25;
3456   return 0;
3457 }
3458 
3459 // Return whether this branch is going via a plt call stub.
3460 
3461 template<int size, bool big_endian>
3462 bool
mark_pltcall(Powerpc_relobj<size,big_endian> * ppc_object,unsigned int shndx,Address offset,Target_powerpc * target,Symbol_table * symtab)3463 Target_powerpc<size, big_endian>::Branch_info::mark_pltcall(
3464     Powerpc_relobj<size, big_endian>* ppc_object,
3465     unsigned int shndx,
3466     Address offset,
3467     Target_powerpc* target,
3468     Symbol_table* symtab)
3469 {
3470   if (this->object_ != ppc_object
3471       || this->shndx_ != shndx
3472       || this->offset_ != offset)
3473     return false;
3474 
3475   Symbol* sym = this->object_->global_symbol(this->r_sym_);
3476   if (sym != NULL && sym->is_forwarder())
3477     sym = symtab->resolve_forwards(sym);
3478   if (target->replace_tls_get_addr(sym))
3479     sym = target->tls_get_addr_opt();
3480   const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3481   if (gsym != NULL
3482       ? (gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3483 	 && !target->is_elfv2_localentry0(gsym))
3484       : (this->object_->local_has_plt_offset(this->r_sym_)
3485 	 && !target->is_elfv2_localentry0(this->object_, this->r_sym_)))
3486     {
3487       this->tocsave_ = 1;
3488       return true;
3489     }
3490   return false;
3491 }
3492 
3493 // If this branch needs a plt call stub, or a long branch stub, make one.
3494 
3495 template<int size, bool big_endian>
3496 bool
make_stub(Stub_table<size,big_endian> * stub_table,Stub_table<size,big_endian> * ifunc_stub_table,Symbol_table * symtab) const3497 Target_powerpc<size, big_endian>::Branch_info::make_stub(
3498     Stub_table<size, big_endian>* stub_table,
3499     Stub_table<size, big_endian>* ifunc_stub_table,
3500     Symbol_table* symtab) const
3501 {
3502   Symbol* sym = this->object_->global_symbol(this->r_sym_);
3503   Target_powerpc<size, big_endian>* target =
3504     static_cast<Target_powerpc<size, big_endian>*>(
3505       parameters->sized_target<size, big_endian>());
3506   if (sym != NULL && sym->is_forwarder())
3507     sym = symtab->resolve_forwards(sym);
3508   if (target->replace_tls_get_addr(sym))
3509     sym = target->tls_get_addr_opt();
3510   const Sized_symbol<size>* gsym = static_cast<const Sized_symbol<size>*>(sym);
3511   bool ok = true;
3512 
3513   if (gsym != NULL
3514       ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target))
3515       : this->object_->local_has_plt_offset(this->r_sym_))
3516     {
3517       if (size == 64
3518 	  && gsym != NULL
3519 	  && target->abiversion() >= 2
3520 	  && !parameters->options().output_is_position_independent()
3521 	  && !is_branch_reloc<size>(this->r_type_))
3522 	target->glink_section()->add_global_entry(gsym);
3523       else
3524 	{
3525 	  if (stub_table == NULL
3526 	      && !(size == 32
3527 		   && gsym != NULL
3528 		   && !parameters->options().output_is_position_independent()
3529 		   && !is_branch_reloc<size>(this->r_type_)))
3530 	    stub_table = this->object_->stub_table(this->shndx_);
3531 	  if (stub_table == NULL)
3532 	    {
3533 	      // This is a ref from a data section to an ifunc symbol,
3534 	      // or a non-branch reloc for which we always want to use
3535 	      // one set of stubs for resolving function addresses.
3536 	      stub_table = ifunc_stub_table;
3537 	    }
3538 	  gold_assert(stub_table != NULL);
3539 	  Address from = this->object_->get_output_section_offset(this->shndx_);
3540 	  if (from != invalid_address)
3541 	    from += (this->object_->output_section(this->shndx_)->address()
3542 		     + this->offset_);
3543 	  if (gsym != NULL)
3544 	    ok = stub_table->add_plt_call_entry(from,
3545 						this->object_, gsym,
3546 						this->r_type_, this->addend_,
3547 						this->tocsave_);
3548 	  else
3549 	    ok = stub_table->add_plt_call_entry(from,
3550 						this->object_, this->r_sym_,
3551 						this->r_type_, this->addend_,
3552 						this->tocsave_);
3553 	}
3554     }
3555   else
3556     {
3557       Address max_branch_offset = max_branch_delta<size>(this->r_type_);
3558       if (max_branch_offset == 0)
3559 	return true;
3560       Address from = this->object_->get_output_section_offset(this->shndx_);
3561       gold_assert(from != invalid_address);
3562       from += (this->object_->output_section(this->shndx_)->address()
3563 	       + this->offset_);
3564       Address to;
3565       unsigned int other = 0;
3566       if (gsym != NULL)
3567 	{
3568 	  switch (gsym->source())
3569 	    {
3570 	    case Symbol::FROM_OBJECT:
3571 	      {
3572 		Object* symobj = gsym->object();
3573 		if (symobj->is_dynamic()
3574 		    || symobj->pluginobj() != NULL)
3575 		  return true;
3576 		bool is_ordinary;
3577 		unsigned int shndx = gsym->shndx(&is_ordinary);
3578 		if (shndx == elfcpp::SHN_UNDEF)
3579 		  return true;
3580 	      }
3581 	      break;
3582 
3583 	    case Symbol::IS_UNDEFINED:
3584 	      return true;
3585 
3586 	    default:
3587 	      break;
3588 	    }
3589 	  Symbol_table::Compute_final_value_status status;
3590 	  to = symtab->compute_final_value<size>(gsym, &status);
3591 	  if (status != Symbol_table::CFVS_OK)
3592 	    return true;
3593 	  if (size == 64)
3594 	    other = gsym->nonvis() >> 3;
3595 	}
3596       else
3597 	{
3598 	  const Symbol_value<size>* psymval
3599 	    = this->object_->local_symbol(this->r_sym_);
3600 	  Symbol_value<size> symval;
3601 	  if (psymval->is_section_symbol())
3602 	    symval.set_is_section_symbol();
3603 	  typedef Sized_relobj_file<size, big_endian> ObjType;
3604 	  typename ObjType::Compute_final_local_value_status status
3605 	    = this->object_->compute_final_local_value(this->r_sym_, psymval,
3606 						       &symval, symtab);
3607 	  if (status != ObjType::CFLV_OK
3608 	      || !symval.has_output_value())
3609 	    return true;
3610 	  to = symval.value(this->object_, 0);
3611 	  if (size == 64)
3612 	    other = this->object_->st_other(this->r_sym_) >> 5;
3613 	}
3614       if (!(size == 32 && this->r_type_ == elfcpp::R_PPC_PLTREL24))
3615 	to += this->addend_;
3616       if (stub_table == NULL)
3617 	stub_table = this->object_->stub_table(this->shndx_);
3618       if (size == 64 && target->abiversion() < 2)
3619 	{
3620 	  unsigned int dest_shndx;
3621 	  if (!target->symval_for_branch(symtab, gsym, this->object_,
3622 					 &to, &dest_shndx))
3623 	    return true;
3624 	}
3625       unsigned int local_ent = 0;
3626       if (size == 64
3627 	  && this->r_type_ != elfcpp::R_PPC64_REL24_NOTOC
3628 	  && this->r_type_ != elfcpp::R_PPC64_REL24_P9NOTOC)
3629 	local_ent = elfcpp::ppc64_decode_local_entry(other);
3630       Address delta = to + local_ent - from;
3631       if (delta + max_branch_offset >= 2 * max_branch_offset
3632 	  || (size == 64
3633 	      && (this->r_type_ == elfcpp::R_PPC64_REL24_NOTOC
3634 		  || this->r_type_ == elfcpp::R_PPC64_REL24_P9NOTOC)
3635 	      && (gsym != NULL
3636 		  ? this->object_->ppc64_needs_toc(gsym)
3637 		  : this->object_->ppc64_needs_toc(this->r_sym_))))
3638 	{
3639 	  if (stub_table == NULL)
3640 	    {
3641 	      gold_warning(_("%s:%s: branch in non-executable section,"
3642 			     " no long branch stub for you"),
3643 			   this->object_->name().c_str(),
3644 			   this->object_->section_name(this->shndx_).c_str());
3645 	      return true;
3646 	    }
3647 	  bool save_res = (size == 64
3648 			   && gsym != NULL
3649 			   && gsym->source() == Symbol::IN_OUTPUT_DATA
3650 			   && gsym->output_data() == target->savres_section());
3651 	  ok = stub_table->add_long_branch_entry(this->r_type_,
3652 						 from, to, other, save_res);
3653 	}
3654     }
3655   if (!ok)
3656     gold_debug(DEBUG_TARGET,
3657 	       "branch at %s:%s+%#lx\n"
3658 	       "can't reach stub attached to %s:%s",
3659 	       this->object_->name().c_str(),
3660 	       this->object_->section_name(this->shndx_).c_str(),
3661 	       (unsigned long) this->offset_,
3662 	       stub_table->relobj()->name().c_str(),
3663 	       stub_table->relobj()->section_name(stub_table->shndx()).c_str());
3664 
3665   return ok;
3666 }
3667 
3668 // Relaxation hook.  This is where we do stub generation.
3669 
3670 template<int size, bool big_endian>
3671 bool
do_relax(int pass,const Input_objects *,Symbol_table * symtab,Layout * layout,const Task * task)3672 Target_powerpc<size, big_endian>::do_relax(int pass,
3673 					   const Input_objects*,
3674 					   Symbol_table* symtab,
3675 					   Layout* layout,
3676 					   const Task* task)
3677 {
3678   unsigned int prev_brlt_size = 0;
3679   if (pass == 1)
3680     {
3681       bool thread_safe
3682 	= this->abiversion() < 2 && parameters->options().plt_thread_safe();
3683       if (size == 64
3684 	  && this->abiversion() < 2
3685 	  && !thread_safe
3686 	  && !parameters->options().user_set_plt_thread_safe())
3687 	{
3688 	  static const char* const thread_starter[] =
3689 	    {
3690 	      "pthread_create",
3691 	      /* libstdc++ */
3692 	      "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
3693 	      /* librt */
3694 	      "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
3695 	      "mq_notify", "create_timer",
3696 	      /* libanl */
3697 	      "getaddrinfo_a",
3698 	      /* libgomp */
3699 	      "GOMP_parallel",
3700 	      "GOMP_parallel_start",
3701 	      "GOMP_parallel_loop_static",
3702 	      "GOMP_parallel_loop_static_start",
3703 	      "GOMP_parallel_loop_dynamic",
3704 	      "GOMP_parallel_loop_dynamic_start",
3705 	      "GOMP_parallel_loop_guided",
3706 	      "GOMP_parallel_loop_guided_start",
3707 	      "GOMP_parallel_loop_runtime",
3708 	      "GOMP_parallel_loop_runtime_start",
3709 	      "GOMP_parallel_sections",
3710 	      "GOMP_parallel_sections_start",
3711 	      /* libgo */
3712 	      "__go_go",
3713 	    };
3714 
3715 	  if (parameters->options().shared())
3716 	    thread_safe = true;
3717 	  else
3718 	    {
3719 	      for (unsigned int i = 0;
3720 		   i < sizeof(thread_starter) / sizeof(thread_starter[0]);
3721 		   i++)
3722 		{
3723 		  Symbol* sym = symtab->lookup(thread_starter[i], NULL);
3724 		  thread_safe = (sym != NULL
3725 				 && sym->in_reg()
3726 				 && sym->in_real_elf());
3727 		  if (thread_safe)
3728 		    break;
3729 		}
3730 	    }
3731 	}
3732       this->plt_thread_safe_ = thread_safe;
3733     }
3734 
3735   if (pass == 1)
3736     {
3737       this->stub_group_size_ = parameters->options().stub_group_size();
3738       bool no_size_errors = true;
3739       if (this->stub_group_size_ == 1)
3740 	this->stub_group_size_ = 0x1c00000;
3741       else if (this->stub_group_size_ == -1)
3742 	this->stub_group_size_ = -0x1e00000;
3743       else
3744 	no_size_errors = false;
3745       this->group_sections(layout, task, no_size_errors);
3746     }
3747   else if (this->relax_failed_ && this->relax_fail_count_ < 3)
3748     {
3749       this->branch_lookup_table_.clear();
3750       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3751 	   p != this->stub_tables_.end();
3752 	   ++p)
3753 	{
3754 	  (*p)->clear_stubs(true);
3755 	}
3756       this->stub_tables_.clear();
3757       this->stub_group_size_ = this->stub_group_size_ / 4 * 3;
3758       gold_info(_("%s: stub group size is too large; retrying with %#x"),
3759 		program_name, this->stub_group_size_);
3760       this->group_sections(layout, task, true);
3761     }
3762 
3763   // We need address of stub tables valid for make_stub.
3764   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3765        p != this->stub_tables_.end();
3766        ++p)
3767     {
3768       const Powerpc_relobj<size, big_endian>* object
3769 	= static_cast<const Powerpc_relobj<size, big_endian>*>((*p)->relobj());
3770       Address off = object->get_output_section_offset((*p)->shndx());
3771       gold_assert(off != invalid_address);
3772       Output_section* os = (*p)->output_section();
3773       (*p)->set_address_and_size(os, off);
3774     }
3775 
3776   if (pass != 1)
3777     {
3778       // Clear plt call stubs, long branch stubs and branch lookup table.
3779       prev_brlt_size = this->branch_lookup_table_.size();
3780       this->branch_lookup_table_.clear();
3781       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3782 	   p != this->stub_tables_.end();
3783 	   ++p)
3784 	{
3785 	  (*p)->clear_stubs(false);
3786 	}
3787     }
3788 
3789   // Build all the stubs.
3790   this->relax_failed_ = false;
3791   Stub_table<size, big_endian>* ifunc_stub_table
3792     = this->stub_tables_.size() == 0 ? NULL : this->stub_tables_[0];
3793   Stub_table<size, big_endian>* one_stub_table
3794     = this->stub_tables_.size() != 1 ? NULL : ifunc_stub_table;
3795   for (typename Branches::const_iterator b = this->branch_info_.begin();
3796        b != this->branch_info_.end();
3797        b++)
3798     {
3799       if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3800 	  && !this->relax_failed_)
3801 	{
3802 	  this->relax_failed_ = true;
3803 	  this->relax_fail_count_++;
3804 	  if (this->relax_fail_count_ < 3)
3805 	    return true;
3806 	}
3807     }
3808   bool do_resize = false;
3809   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3810        p != this->stub_tables_.end();
3811        ++p)
3812     if ((*p)->need_resize())
3813       {
3814 	do_resize = true;
3815 	break;
3816       }
3817   if (do_resize)
3818     {
3819       this->branch_lookup_table_.clear();
3820       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3821 	   p != this->stub_tables_.end();
3822 	   ++p)
3823 	(*p)->set_resizing(true);
3824       for (typename Branches::const_iterator b = this->branch_info_.begin();
3825 	   b != this->branch_info_.end();
3826 	   b++)
3827 	{
3828 	  if (!b->make_stub(one_stub_table, ifunc_stub_table, symtab)
3829 	      && !this->relax_failed_)
3830 	    {
3831 	      this->relax_failed_ = true;
3832 	      this->relax_fail_count_++;
3833 	      if (this->relax_fail_count_ < 3)
3834 		return true;
3835 	    }
3836 	}
3837       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3838 	   p != this->stub_tables_.end();
3839 	   ++p)
3840 	(*p)->set_resizing(false);
3841     }
3842 
3843   // Did anything change size?
3844   unsigned int num_huge_branches = this->branch_lookup_table_.size();
3845   bool again = num_huge_branches != prev_brlt_size;
3846   if (size == 64 && num_huge_branches != 0)
3847     this->make_brlt_section(layout);
3848   if (size == 64 && again)
3849     this->brlt_section_->set_current_size(num_huge_branches);
3850 
3851   for (typename Stub_tables::reverse_iterator p = this->stub_tables_.rbegin();
3852        p != this->stub_tables_.rend();
3853        ++p)
3854     (*p)->remove_eh_frame(layout);
3855 
3856   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3857        p != this->stub_tables_.end();
3858        ++p)
3859     (*p)->add_eh_frame(layout);
3860 
3861   typedef Unordered_set<Output_section*> Output_sections;
3862   Output_sections os_need_update;
3863   for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3864        p != this->stub_tables_.end();
3865        ++p)
3866     {
3867       if ((*p)->size_update())
3868 	{
3869 	  again = true;
3870 	  os_need_update.insert((*p)->output_section());
3871 	}
3872     }
3873 
3874   // Set output section offsets for all input sections in an output
3875   // section that just changed size.  Anything past the stubs will
3876   // need updating.
3877   for (typename Output_sections::iterator p = os_need_update.begin();
3878        p != os_need_update.end();
3879        p++)
3880     {
3881       Output_section* os = *p;
3882       Address off = 0;
3883       typedef Output_section::Input_section_list Input_section_list;
3884       for (Input_section_list::const_iterator i = os->input_sections().begin();
3885 	   i != os->input_sections().end();
3886 	   ++i)
3887 	{
3888 	  off = align_address(off, i->addralign());
3889 	  if (i->is_input_section() || i->is_relaxed_input_section())
3890 	    i->relobj()->set_section_offset(i->shndx(), off);
3891 	  if (i->is_relaxed_input_section())
3892 	    {
3893 	      Stub_table<size, big_endian>* stub_table
3894 		= static_cast<Stub_table<size, big_endian>*>(
3895 		    i->relaxed_input_section());
3896 	      Address stub_table_size = stub_table->set_address_and_size(os, off);
3897 	      off += stub_table_size;
3898 	      // After a few iterations, set current stub table size
3899 	      // as min size threshold, so later stub tables can only
3900 	      // grow in size.
3901 	      if (pass >= 4)
3902 		stub_table->set_min_size_threshold(stub_table_size);
3903 	    }
3904 	  else
3905 	    off += i->data_size();
3906 	}
3907       // If .branch_lt is part of this output section, then we have
3908       // just done the offset adjustment.
3909       os->clear_section_offsets_need_adjustment();
3910     }
3911 
3912   if (size == 64
3913       && !again
3914       && num_huge_branches != 0
3915       && parameters->options().output_is_position_independent())
3916     {
3917       // Fill in the BRLT relocs.
3918       this->brlt_section_->reset_brlt_sizes();
3919       for (typename Branch_lookup_table::const_iterator p
3920 	     = this->branch_lookup_table_.begin();
3921 	   p != this->branch_lookup_table_.end();
3922 	   ++p)
3923 	{
3924 	  this->brlt_section_->add_reloc(p->first, p->second);
3925 	}
3926       this->brlt_section_->finalize_brlt_sizes();
3927     }
3928 
3929   if (!again
3930       && (parameters->options().user_set_emit_stub_syms()
3931 	  ? parameters->options().emit_stub_syms()
3932 	  : (size == 64
3933 	     || parameters->options().output_is_position_independent()
3934 	     || parameters->options().emit_relocs())))
3935     {
3936       for (typename Stub_tables::iterator p = this->stub_tables_.begin();
3937 	   p != this->stub_tables_.end();
3938 	   ++p)
3939 	(*p)->define_stub_syms(symtab);
3940 
3941       if (this->glink_ != NULL)
3942 	{
3943 	  int stub_size = this->glink_->pltresolve_size();
3944 	  Address value = -stub_size;
3945 	  if (size == 64)
3946 	    {
3947 	      value = 8;
3948 	      stub_size -= 8;
3949 	    }
3950 	  this->define_local(symtab, "__glink_PLTresolve",
3951 			     this->glink_, value, stub_size);
3952 
3953 	  if (size != 64)
3954 	    this->define_local(symtab, "__glink", this->glink_, 0, 0);
3955 	}
3956     }
3957 
3958   return again;
3959 }
3960 
3961 template<int size, bool big_endian>
3962 void
do_plt_fde_location(const Output_data * plt,unsigned char * oview,uint64_t * paddress,off_t * plen) const3963 Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
3964 						      unsigned char* oview,
3965 						      uint64_t* paddress,
3966 						      off_t* plen) const
3967 {
3968   uint64_t address = plt->address();
3969   off_t len = plt->data_size();
3970 
3971   if (plt == this->glink_)
3972     {
3973       // See Output_data_glink::do_write() for glink contents.
3974       if (len == 0)
3975 	{
3976 	  // Static linking may need stubs, to support ifunc and long
3977 	  // branches.  We need to create an output section for
3978 	  // .eh_frame early in the link process, to have a place to
3979 	  // attach stub .eh_frame info.  We also need to have
3980 	  // registered a CIE that matches the stub CIE.  Both of
3981 	  // these requirements are satisfied by creating an FDE and
3982 	  // CIE for .glink, even though static linking will leave
3983 	  // .glink zero length.
3984 	  // ??? Hopefully generating an FDE with a zero address range
3985 	  // won't confuse anything that consumes .eh_frame info.
3986 	}
3987       else if (size == 64)
3988 	{
3989 	  // There is one word before __glink_PLTresolve
3990 	  address += 8;
3991 	  len -= 8;
3992 	}
3993       else if (parameters->options().output_is_position_independent())
3994 	{
3995 	  // There are two FDEs for a position independent glink.
3996 	  // The first covers the branch table, the second
3997 	  // __glink_PLTresolve at the end of glink.
3998 	  off_t resolve_size = this->glink_->pltresolve_size();
3999 	  if (oview[9] == elfcpp::DW_CFA_nop)
4000 	    len -= resolve_size;
4001 	  else
4002 	    {
4003 	      address += len - resolve_size;
4004 	      len = resolve_size;
4005 	    }
4006 	}
4007     }
4008   else
4009     {
4010       // Must be a stub table.
4011       const Stub_table<size, big_endian>* stub_table
4012 	= static_cast<const Stub_table<size, big_endian>*>(plt);
4013       uint64_t stub_address = stub_table->stub_address();
4014       len -= stub_address - address;
4015       address = stub_address;
4016     }
4017 
4018   *paddress = address;
4019   *plen = len;
4020 }
4021 
4022 // A class to handle the PLT data.
4023 
4024 template<int size, bool big_endian>
4025 class Output_data_plt_powerpc : public Output_section_data_build
4026 {
4027  public:
4028   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4029 			    size, big_endian> Reloc_section;
4030 
Output_data_plt_powerpc(Target_powerpc<size,big_endian> * targ,Symbol_table * symtab,Reloc_section * plt_rel,const char * name)4031   Output_data_plt_powerpc(Target_powerpc<size, big_endian>* targ,
4032 			  Symbol_table* symtab,
4033 			  Reloc_section* plt_rel,
4034 			  const char* name)
4035     : Output_section_data_build(size == 32 ? 4 : 8),
4036       rel_(plt_rel), targ_(targ), symtab_(symtab), name_(name), sym_ents_()
4037   { }
4038 
4039   // Add an entry to the PLT.
4040   void
4041   add_entry(Symbol*, bool = false);
4042 
4043   void
4044   add_ifunc_entry(Symbol*);
4045 
4046   void
4047   add_local_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4048 
4049   void
4050   add_local_ifunc_entry(Sized_relobj_file<size, big_endian>*, unsigned int);
4051 
4052   // Return the .rela.plt section data.
4053   Reloc_section*
rel_plt() const4054   rel_plt() const
4055   {
4056     return this->rel_;
4057   }
4058 
4059   // Return the number of PLT entries.
4060   unsigned int
entry_count() const4061   entry_count() const
4062   {
4063     if (this->current_data_size() == 0)
4064       return 0;
4065     return ((this->current_data_size() - this->first_plt_entry_offset())
4066 	    / this->plt_entry_size());
4067   }
4068 
4069  protected:
4070   void
do_adjust_output_section(Output_section * os)4071   do_adjust_output_section(Output_section* os)
4072   {
4073     os->set_entsize(0);
4074   }
4075 
4076   // Write to a map file.
4077   void
do_print_to_mapfile(Mapfile * mapfile) const4078   do_print_to_mapfile(Mapfile* mapfile) const
4079   { mapfile->print_output_data(this, this->name_); }
4080 
4081  private:
4082   struct Local_plt_ent
4083   {
Local_plt_ent__anon10389ec10111::Output_data_plt_powerpc::Local_plt_ent4084     Local_plt_ent(Sized_relobj_file<size, big_endian>* obj, unsigned int rsym)
4085     { rsym_ = rsym; u.obj_ = obj; }
Local_plt_ent__anon10389ec10111::Output_data_plt_powerpc::Local_plt_ent4086     Local_plt_ent(Symbol* sym)
4087     { rsym_ = -1u; u.gsym_ = sym; }
~Local_plt_ent__anon10389ec10111::Output_data_plt_powerpc::Local_plt_ent4088     ~Local_plt_ent()
4089     { }
4090 
4091     unsigned int rsym_;
4092     union
4093     {
4094       Sized_relobj_file<size, big_endian>* obj_;
4095       Symbol* gsym_;
4096     } u;
4097   };
4098 
4099   // Return the offset of the first non-reserved PLT entry.
4100   unsigned int
first_plt_entry_offset() const4101   first_plt_entry_offset() const
4102   {
4103     // IPLT and LPLT have no reserved entry.
4104     if (this->name_[3] == 'I' || this->name_[3] == 'L')
4105       return 0;
4106     return this->targ_->first_plt_entry_offset();
4107   }
4108 
4109   // Return the size of each PLT entry.
4110   unsigned int
plt_entry_size() const4111   plt_entry_size() const
4112   {
4113     return this->targ_->plt_entry_size();
4114   }
4115 
4116   // Write out the PLT data.
4117   void
4118   do_write(Output_file*);
4119 
4120   // The reloc section.
4121   Reloc_section* rel_;
4122   // Allows access to .glink for do_write.
4123   Target_powerpc<size, big_endian>* targ_;
4124   Symbol_table* symtab_;
4125   // What to report in map file.
4126   const char *name_;
4127 
4128   std::vector<Local_plt_ent> sym_ents_;
4129 };
4130 
4131 // Add an entry to the PLT.
4132 
4133 template<int size, bool big_endian>
4134 void
add_entry(Symbol * gsym,bool stash)4135 Output_data_plt_powerpc<size, big_endian>::add_entry(Symbol* gsym,
4136 						     bool stash)
4137 {
4138   if (!gsym->has_plt_offset())
4139     {
4140       section_size_type off = this->current_data_size();
4141       if (off == 0)
4142 	off += this->first_plt_entry_offset();
4143       gsym->set_plt_offset(off);
4144       if (this->rel_)
4145 	{
4146 	  gsym->set_needs_dynsym_entry();
4147 	  unsigned int dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4148 	  this->rel_->add_global(gsym, dynrel, this, off, 0);
4149 	}
4150       off += this->plt_entry_size();
4151       this->set_current_data_size(off);
4152       if (stash)
4153 	{
4154 	  Local_plt_ent sym(gsym);
4155 	  this->sym_ents_.push_back(sym);
4156 	}
4157     }
4158 }
4159 
4160 // Add an entry for a global ifunc symbol that resolves locally, to the IPLT.
4161 
4162 template<int size, bool big_endian>
4163 void
add_ifunc_entry(Symbol * gsym)4164 Output_data_plt_powerpc<size, big_endian>::add_ifunc_entry(Symbol* gsym)
4165 {
4166   if (!gsym->has_plt_offset())
4167     {
4168       section_size_type off = this->current_data_size();
4169       gsym->set_plt_offset(off);
4170       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4171       if (size == 64 && this->targ_->abiversion() < 2)
4172 	dynrel = elfcpp::R_PPC64_JMP_IREL;
4173       this->rel_->add_symbolless_global_addend(gsym, dynrel, this, off, 0);
4174       off += this->plt_entry_size();
4175       this->set_current_data_size(off);
4176     }
4177 }
4178 
4179 // Add an entry for a local symbol to the PLT.
4180 
4181 template<int size, bool big_endian>
4182 void
add_local_entry(Sized_relobj_file<size,big_endian> * relobj,unsigned int local_sym_index)4183 Output_data_plt_powerpc<size, big_endian>::add_local_entry(
4184     Sized_relobj_file<size, big_endian>* relobj,
4185     unsigned int local_sym_index)
4186 {
4187   if (!relobj->local_has_plt_offset(local_sym_index))
4188     {
4189       section_size_type off = this->current_data_size();
4190       relobj->set_local_plt_offset(local_sym_index, off);
4191       if (this->rel_)
4192 	{
4193 	  unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
4194 	  if (size == 64 && this->targ_->abiversion() < 2)
4195 	    dynrel = elfcpp::R_POWERPC_JMP_SLOT;
4196 	  this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
4197 						  dynrel, this, off, 0);
4198 	}
4199       off += this->plt_entry_size();
4200       this->set_current_data_size(off);
4201       Local_plt_ent sym(relobj, local_sym_index);
4202       this->sym_ents_.push_back(sym);
4203     }
4204 }
4205 
4206 // Add an entry for a local ifunc symbol to the IPLT.
4207 
4208 template<int size, bool big_endian>
4209 void
add_local_ifunc_entry(Sized_relobj_file<size,big_endian> * relobj,unsigned int local_sym_index)4210 Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
4211     Sized_relobj_file<size, big_endian>* relobj,
4212     unsigned int local_sym_index)
4213 {
4214   if (!relobj->local_has_plt_offset(local_sym_index))
4215     {
4216       section_size_type off = this->current_data_size();
4217       relobj->set_local_plt_offset(local_sym_index, off);
4218       unsigned int dynrel = elfcpp::R_POWERPC_IRELATIVE;
4219       if (size == 64 && this->targ_->abiversion() < 2)
4220 	dynrel = elfcpp::R_PPC64_JMP_IREL;
4221       this->rel_->add_symbolless_local_addend(relobj, local_sym_index, dynrel,
4222 					      this, off, 0);
4223       off += this->plt_entry_size();
4224       this->set_current_data_size(off);
4225     }
4226 }
4227 
4228 static const uint32_t add_0_11_11	= 0x7c0b5a14;
4229 static const uint32_t add_2_2_11	= 0x7c425a14;
4230 static const uint32_t add_2_2_12	= 0x7c426214;
4231 static const uint32_t add_3_3_2		= 0x7c631214;
4232 static const uint32_t add_3_3_13	= 0x7c636a14;
4233 static const uint32_t add_3_12_2	= 0x7c6c1214;
4234 static const uint32_t add_3_12_13	= 0x7c6c6a14;
4235 static const uint32_t add_11_0_11	= 0x7d605a14;
4236 static const uint32_t add_11_2_11	= 0x7d625a14;
4237 static const uint32_t add_11_11_2	= 0x7d6b1214;
4238 static const uint32_t add_12_11_12	= 0x7d8b6214;
4239 static const uint32_t addi_0_12		= 0x380c0000;
4240 static const uint32_t addi_2_2		= 0x38420000;
4241 static const uint32_t addi_3_3		= 0x38630000;
4242 static const uint32_t addi_11_11	= 0x396b0000;
4243 static const uint32_t addi_12_1		= 0x39810000;
4244 static const uint32_t addi_12_11	= 0x398b0000;
4245 static const uint32_t addi_12_12	= 0x398c0000;
4246 static const uint32_t addis_0_2		= 0x3c020000;
4247 static const uint32_t addis_0_13	= 0x3c0d0000;
4248 static const uint32_t addis_2_12	= 0x3c4c0000;
4249 static const uint32_t addis_11_2	= 0x3d620000;
4250 static const uint32_t addis_11_11	= 0x3d6b0000;
4251 static const uint32_t addis_11_30	= 0x3d7e0000;
4252 static const uint32_t addis_12_1	= 0x3d810000;
4253 static const uint32_t addis_12_2	= 0x3d820000;
4254 static const uint32_t addis_12_11	= 0x3d8b0000;
4255 static const uint32_t addis_12_12	= 0x3d8c0000;
4256 static const uint32_t b			= 0x48000000;
4257 static const uint32_t bcl_20_31		= 0x429f0005;
4258 static const uint32_t bctr		= 0x4e800420;
4259 static const uint32_t bctrl		= 0x4e800421;
4260 static const uint32_t beqlr		= 0x4d820020;
4261 static const uint32_t blr		= 0x4e800020;
4262 static const uint32_t bnectr_p4		= 0x4ce20420;
4263 static const uint32_t cmpld_7_12_0	= 0x7fac0040;
4264 static const uint32_t cmpldi_2_0	= 0x28220000;
4265 static const uint32_t cmpdi_11_0	= 0x2c2b0000;
4266 static const uint32_t cmpwi_11_0	= 0x2c0b0000;
4267 static const uint32_t cror_15_15_15	= 0x4def7b82;
4268 static const uint32_t cror_31_31_31	= 0x4ffffb82;
4269 static const uint32_t ld_0_1		= 0xe8010000;
4270 static const uint32_t ld_0_11		= 0xe80b0000;
4271 static const uint32_t ld_0_12		= 0xe80c0000;
4272 static const uint32_t ld_2_1		= 0xe8410000;
4273 static const uint32_t ld_2_2		= 0xe8420000;
4274 static const uint32_t ld_2_11		= 0xe84b0000;
4275 static const uint32_t ld_2_12		= 0xe84c0000;
4276 static const uint32_t ld_11_1		= 0xe9610000;
4277 static const uint32_t ld_11_2		= 0xe9620000;
4278 static const uint32_t ld_11_3		= 0xe9630000;
4279 static const uint32_t ld_11_11		= 0xe96b0000;
4280 static const uint32_t ld_12_2		= 0xe9820000;
4281 static const uint32_t ld_12_3		= 0xe9830000;
4282 static const uint32_t ld_12_11		= 0xe98b0000;
4283 static const uint32_t ld_12_12		= 0xe98c0000;
4284 static const uint32_t ldx_12_11_12	= 0x7d8b602a;
4285 static const uint32_t lfd_0_1		= 0xc8010000;
4286 static const uint32_t li_0_0		= 0x38000000;
4287 static const uint32_t li_11_0		= 0x39600000;
4288 static const uint32_t li_12_0		= 0x39800000;
4289 static const uint32_t lis_0		= 0x3c000000;
4290 static const uint32_t lis_2		= 0x3c400000;
4291 static const uint32_t lis_11		= 0x3d600000;
4292 static const uint32_t lis_12		= 0x3d800000;
4293 static const uint32_t lvx_0_12_0	= 0x7c0c00ce;
4294 static const uint32_t lwz_0_12		= 0x800c0000;
4295 static const uint32_t lwz_11_3		= 0x81630000;
4296 static const uint32_t lwz_11_11		= 0x816b0000;
4297 static const uint32_t lwz_11_30		= 0x817e0000;
4298 static const uint32_t lwz_12_3		= 0x81830000;
4299 static const uint32_t lwz_12_12		= 0x818c0000;
4300 static const uint32_t lwzu_0_12		= 0x840c0000;
4301 static const uint32_t mflr_0		= 0x7c0802a6;
4302 static const uint32_t mflr_11		= 0x7d6802a6;
4303 static const uint32_t mflr_12		= 0x7d8802a6;
4304 static const uint32_t mr_0_3		= 0x7c601b78;
4305 static const uint32_t mr_3_0		= 0x7c030378;
4306 static const uint32_t mtctr_0		= 0x7c0903a6;
4307 static const uint32_t mtctr_11		= 0x7d6903a6;
4308 static const uint32_t mtctr_12		= 0x7d8903a6;
4309 static const uint32_t mtlr_0		= 0x7c0803a6;
4310 static const uint32_t mtlr_11		= 0x7d6803a6;
4311 static const uint32_t mtlr_12		= 0x7d8803a6;
4312 static const uint32_t nop		= 0x60000000;
4313 static const uint32_t ori_0_0_0		= 0x60000000;
4314 static const uint32_t ori_11_11_0	= 0x616b0000;
4315 static const uint32_t ori_12_12_0	= 0x618c0000;
4316 static const uint32_t oris_12_12_0	= 0x658c0000;
4317 static const uint32_t sldi_11_11_34	= 0x796b1746;
4318 static const uint32_t sldi_12_12_32	= 0x799c07c6;
4319 static const uint32_t srdi_0_0_2	= 0x7800f082;
4320 static const uint32_t std_0_1		= 0xf8010000;
4321 static const uint32_t std_0_12		= 0xf80c0000;
4322 static const uint32_t std_2_1		= 0xf8410000;
4323 static const uint32_t std_11_1		= 0xf9610000;
4324 static const uint32_t stfd_0_1		= 0xd8010000;
4325 static const uint32_t stvx_0_12_0	= 0x7c0c01ce;
4326 static const uint32_t sub_11_11_12	= 0x7d6c5850;
4327 static const uint32_t sub_12_12_11	= 0x7d8b6050;
4328 static const uint32_t xor_2_12_12	= 0x7d826278;
4329 static const uint32_t xor_11_12_12	= 0x7d8b6278;
4330 
4331 static const uint64_t paddi_12_pc	= 0x0610000039800000ULL;
4332 static const uint64_t pld_12_pc		= 0x04100000e5800000ULL;
4333 static const uint64_t pnop		= 0x0700000000000000ULL;
4334 
4335 // Write out the PLT.
4336 
4337 template<int size, bool big_endian>
4338 void
do_write(Output_file * of)4339 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
4340 {
4341   if (!this->sym_ents_.empty())
4342     {
4343       const section_size_type offset = this->offset();
4344       const section_size_type oview_size
4345 	= convert_to_section_size_type(this->data_size());
4346       unsigned char* const oview = of->get_output_view(offset, oview_size);
4347       unsigned char* pov = oview;
4348       unsigned char* endpov = oview + oview_size;
4349 
4350       for (typename std::vector<Local_plt_ent>::iterator e
4351 	     = this->sym_ents_.begin();
4352 	   e != this->sym_ents_.end();
4353 	   e++)
4354 	{
4355 	  typename elfcpp::Elf_types<size>::Elf_Addr val;
4356 	  Sized_symbol<size>* gsym = NULL;
4357 	  Powerpc_relobj<size, big_endian>* obj = NULL;
4358 	  if (e->rsym_ == -1u)
4359 	    {
4360 	      gsym = static_cast<Sized_symbol<size>*>(e->u.gsym_);
4361 	      val = gsym->value();
4362 	    }
4363 	  else
4364 	    {
4365 	      obj = static_cast<Powerpc_relobj<size, big_endian>*>(e->u.obj_);
4366 	      val = obj->local_symbol(e->rsym_)->value(obj, 0);
4367 	    }
4368 	  if (this->targ_->abiversion() >= 2)
4369 	    {
4370 	      elfcpp::Swap<size, big_endian>::writeval(pov, val);
4371 	      pov += size / 8;
4372 	    }
4373 	  else
4374 	    {
4375 	      unsigned int shndx;
4376 	      this->targ_->symval_for_branch(this->symtab_, gsym, obj,
4377 					     &val, &shndx);
4378 	      elfcpp::Swap<size, big_endian>::writeval(pov, val);
4379 	      pov += size / 8;
4380 	      val = this->targ_->toc_pointer();
4381 	      elfcpp::Swap<size, big_endian>::writeval(pov, val);
4382 	      pov += size / 8;
4383 	      if (this->plt_entry_size() > 16)
4384 		{
4385 		  elfcpp::Swap<size, big_endian>::writeval(pov, 0);
4386 		  pov += size / 8;
4387 		}
4388 	    }
4389 	}
4390       gold_assert(pov == endpov);
4391     }
4392 
4393   if (size == 32 && (this->name_[3] != 'I' && this->name_[3] != 'L'))
4394     {
4395       const section_size_type offset = this->offset();
4396       const section_size_type oview_size
4397 	= convert_to_section_size_type(this->data_size());
4398       unsigned char* const oview = of->get_output_view(offset, oview_size);
4399       unsigned char* pov = oview;
4400       unsigned char* endpov = oview + oview_size;
4401 
4402       // The address of the .glink branch table
4403       const Output_data_glink<size, big_endian>* glink
4404 	= this->targ_->glink_section();
4405       elfcpp::Elf_types<32>::Elf_Addr branch_tab = glink->address();
4406 
4407       while (pov < endpov)
4408 	{
4409 	  elfcpp::Swap<32, big_endian>::writeval(pov, branch_tab);
4410 	  pov += 4;
4411 	  branch_tab += 4;
4412 	}
4413 
4414       of->write_output_view(offset, oview_size, oview);
4415     }
4416 }
4417 
4418 // Create the PLT section.
4419 
4420 template<int size, bool big_endian>
4421 void
make_plt_section(Symbol_table * symtab,Layout * layout)4422 Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
4423 						   Layout* layout)
4424 {
4425   if (this->plt_ == NULL)
4426     {
4427       if (this->got_ == NULL)
4428 	this->got_section(symtab, layout, GOT_TYPE_SMALL);
4429 
4430       if (this->glink_ == NULL)
4431 	make_glink_section(layout);
4432 
4433       // Ensure that .rela.dyn always appears before .rela.plt  This is
4434       // necessary due to how, on PowerPC and some other targets, .rela.dyn
4435       // needs to include .rela.plt in its range.
4436       this->rela_dyn_section(layout);
4437 
4438       Reloc_section* plt_rel = new Reloc_section(false);
4439       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
4440 				      elfcpp::SHF_ALLOC, plt_rel,
4441 				      ORDER_DYNAMIC_PLT_RELOCS, false);
4442       this->plt_
4443 	= new Output_data_plt_powerpc<size, big_endian>(this, symtab, plt_rel,
4444 							"** PLT");
4445       layout->add_output_section_data(".plt",
4446 				      (size == 32
4447 				       ? elfcpp::SHT_PROGBITS
4448 				       : elfcpp::SHT_NOBITS),
4449 				      elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4450 				      this->plt_,
4451 				      (size == 32
4452 				       ? ORDER_SMALL_DATA
4453 				       : ORDER_SMALL_BSS),
4454 				      false);
4455 
4456       Output_section* rela_plt_os = plt_rel->output_section();
4457       rela_plt_os->set_info_section(this->plt_->output_section());
4458     }
4459 }
4460 
4461 // Create the IPLT section.
4462 
4463 template<int size, bool big_endian>
4464 void
make_iplt_section(Symbol_table * symtab,Layout * layout)4465 Target_powerpc<size, big_endian>::make_iplt_section(Symbol_table* symtab,
4466 						    Layout* layout)
4467 {
4468   if (this->iplt_ == NULL)
4469     {
4470       this->make_plt_section(symtab, layout);
4471       this->make_lplt_section(symtab, layout);
4472 
4473       Reloc_section* iplt_rel = new Reloc_section(false);
4474       if (this->rela_dyn_->output_section())
4475 	this->rela_dyn_->output_section()->add_output_section_data(iplt_rel);
4476       this->iplt_
4477 	= new Output_data_plt_powerpc<size, big_endian>(this, symtab, iplt_rel,
4478 							"** IPLT");
4479       if (this->plt_->output_section())
4480 	this->plt_->output_section()->add_output_section_data(this->iplt_);
4481     }
4482 }
4483 
4484 // Create the LPLT section.
4485 
4486 template<int size, bool big_endian>
4487 void
make_lplt_section(Symbol_table * symtab,Layout * layout)4488 Target_powerpc<size, big_endian>::make_lplt_section(Symbol_table* symtab,
4489 						    Layout* layout)
4490 {
4491   if (this->lplt_ == NULL)
4492     {
4493       Reloc_section* lplt_rel = NULL;
4494       if (parameters->options().output_is_position_independent())
4495 	{
4496 	  lplt_rel = new Reloc_section(false);
4497 	  this->rela_dyn_section(layout);
4498 	  if (this->rela_dyn_->output_section())
4499 	    this->rela_dyn_->output_section()
4500 	      ->add_output_section_data(lplt_rel);
4501 	}
4502       this->lplt_
4503 	= new Output_data_plt_powerpc<size, big_endian>(this, symtab, lplt_rel,
4504 							"** LPLT");
4505       this->make_brlt_section(layout);
4506       if (this->brlt_section_ && this->brlt_section_->output_section())
4507 	this->brlt_section_->output_section()
4508 	  ->add_output_section_data(this->lplt_);
4509       else
4510 	layout->add_output_section_data(".branch_lt",
4511 					elfcpp::SHT_PROGBITS,
4512 					elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4513 					this->lplt_,
4514 					ORDER_RELRO,
4515 					true);
4516     }
4517 }
4518 
4519 // A section for huge long branch addresses, similar to plt section.
4520 
4521 template<int size, bool big_endian>
4522 class Output_data_brlt_powerpc : public Output_section_data_build
4523 {
4524  public:
4525   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4526   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
4527 			    size, big_endian> Reloc_section;
4528 
Output_data_brlt_powerpc(Target_powerpc<size,big_endian> * targ,Reloc_section * brlt_rel)4529   Output_data_brlt_powerpc(Target_powerpc<size, big_endian>* targ,
4530 			   Reloc_section* brlt_rel)
4531     : Output_section_data_build(size == 32 ? 4 : 8),
4532       rel_(brlt_rel),
4533       targ_(targ)
4534   { }
4535 
4536   void
reset_brlt_sizes()4537   reset_brlt_sizes()
4538   {
4539     this->reset_data_size();
4540     this->rel_->reset_data_size();
4541   }
4542 
4543   void
finalize_brlt_sizes()4544   finalize_brlt_sizes()
4545   {
4546     this->finalize_data_size();
4547     this->rel_->finalize_data_size();
4548   }
4549 
4550   // Add a reloc for an entry in the BRLT.
4551   void
add_reloc(Address to,unsigned int off)4552   add_reloc(Address to, unsigned int off)
4553   { this->rel_->add_relative(elfcpp::R_POWERPC_RELATIVE, this, off, to); }
4554 
4555   // Update section and reloc section size.
4556   void
set_current_size(unsigned int num_branches)4557   set_current_size(unsigned int num_branches)
4558   {
4559     this->reset_address_and_file_offset();
4560     this->set_current_data_size(num_branches * 16);
4561     this->finalize_data_size();
4562     Output_section* os = this->output_section();
4563     os->set_section_offsets_need_adjustment();
4564     if (this->rel_ != NULL)
4565       {
4566 	const unsigned int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
4567 	this->rel_->reset_address_and_file_offset();
4568 	this->rel_->set_current_data_size(num_branches * reloc_size);
4569 	this->rel_->finalize_data_size();
4570 	Output_section* os = this->rel_->output_section();
4571 	os->set_section_offsets_need_adjustment();
4572       }
4573   }
4574 
4575  protected:
4576   void
do_adjust_output_section(Output_section * os)4577   do_adjust_output_section(Output_section* os)
4578   {
4579     os->set_entsize(0);
4580   }
4581 
4582   // Write to a map file.
4583   void
do_print_to_mapfile(Mapfile * mapfile) const4584   do_print_to_mapfile(Mapfile* mapfile) const
4585   { mapfile->print_output_data(this, "** BRLT"); }
4586 
4587  private:
4588   // Write out the BRLT data.
4589   void
4590   do_write(Output_file*);
4591 
4592   // The reloc section.
4593   Reloc_section* rel_;
4594   Target_powerpc<size, big_endian>* targ_;
4595 };
4596 
4597 // Make the branch lookup table section.
4598 
4599 template<int size, bool big_endian>
4600 void
make_brlt_section(Layout * layout)4601 Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
4602 {
4603   if (size == 64 && this->brlt_section_ == NULL)
4604     {
4605       Reloc_section* brlt_rel = NULL;
4606       bool is_pic = parameters->options().output_is_position_independent();
4607       if (is_pic)
4608 	{
4609 	  // When PIC we can't fill in .branch_lt but must initialise at
4610 	  // runtime via dynamic relocations.
4611 	  this->rela_dyn_section(layout);
4612 	  brlt_rel = new Reloc_section(false);
4613 	  if (this->rela_dyn_->output_section())
4614 	    this->rela_dyn_->output_section()
4615 	      ->add_output_section_data(brlt_rel);
4616 	}
4617       this->brlt_section_
4618 	= new Output_data_brlt_powerpc<size, big_endian>(this, brlt_rel);
4619       if (this->plt_ && is_pic && this->plt_->output_section())
4620 	this->plt_->output_section()
4621 	  ->add_output_section_data(this->brlt_section_);
4622       else
4623 	layout->add_output_section_data(".branch_lt",
4624 					elfcpp::SHT_PROGBITS,
4625 					elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
4626 					this->brlt_section_,
4627 					ORDER_RELRO,
4628 					true);
4629     }
4630 }
4631 
4632 // Write out .branch_lt when non-PIC.
4633 
4634 template<int size, bool big_endian>
4635 void
do_write(Output_file * of)4636 Output_data_brlt_powerpc<size, big_endian>::do_write(Output_file* of)
4637 {
4638   if (size == 64 && !parameters->options().output_is_position_independent())
4639     {
4640       const section_size_type offset = this->offset();
4641       const section_size_type oview_size
4642 	= convert_to_section_size_type(this->data_size());
4643       unsigned char* const oview = of->get_output_view(offset, oview_size);
4644 
4645       this->targ_->write_branch_lookup_table(oview);
4646       of->write_output_view(offset, oview_size, oview);
4647     }
4648 }
4649 
4650 static inline uint32_t
l(uint32_t a)4651 l(uint32_t a)
4652 {
4653   return a & 0xffff;
4654 }
4655 
4656 static inline uint32_t
hi(uint32_t a)4657 hi(uint32_t a)
4658 {
4659   return l(a >> 16);
4660 }
4661 
4662 static inline uint32_t
ha(uint32_t a)4663 ha(uint32_t a)
4664 {
4665   return hi(a + 0x8000);
4666 }
4667 
4668 static inline uint64_t
d34(uint64_t v)4669 d34(uint64_t v)
4670 {
4671   return ((v & 0x3ffff0000ULL) << 16) | (v & 0xffff);
4672 }
4673 
4674 static inline uint64_t
ha34(uint64_t v)4675 ha34(uint64_t v)
4676 {
4677   return (v + (1ULL << 33)) >> 34;
4678 }
4679 
4680 template<int size>
4681 struct Eh_cie
4682 {
4683   static const unsigned char eh_frame_cie[12];
4684 };
4685 
4686 template<int size>
4687 const unsigned char Eh_cie<size>::eh_frame_cie[] =
4688 {
4689   1,					// CIE version.
4690   'z', 'R', 0,				// Augmentation string.
4691   4,					// Code alignment.
4692   0x80 - size / 8 ,			// Data alignment.
4693   65,					// RA reg.
4694   1,					// Augmentation size.
4695   (elfcpp::DW_EH_PE_pcrel
4696    | elfcpp::DW_EH_PE_sdata4),		// FDE encoding.
4697   elfcpp::DW_CFA_def_cfa, 1, 0		// def_cfa: r1 offset 0.
4698 };
4699 
4700 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv1.
4701 static const unsigned char glink_eh_frame_fde_64v1[] =
4702 {
4703   0, 0, 0, 0,				// Replaced with offset to .glink.
4704   0, 0, 0, 0,				// Replaced with size of .glink.
4705   0,					// Augmentation size.
4706   elfcpp::DW_CFA_advance_loc + 2,
4707   elfcpp::DW_CFA_register, 65, 12,
4708   elfcpp::DW_CFA_advance_loc + 4,
4709   elfcpp::DW_CFA_restore_extended, 65
4710 };
4711 
4712 // Describe __glink_PLTresolve use of LR, 64-bit version ABIv2.
4713 static const unsigned char glink_eh_frame_fde_64v2[] =
4714 {
4715   0, 0, 0, 0,				// Replaced with offset to .glink.
4716   0, 0, 0, 0,				// Replaced with size of .glink.
4717   0,					// Augmentation size.
4718   elfcpp::DW_CFA_advance_loc + 2,
4719   elfcpp::DW_CFA_register, 65, 0,
4720   elfcpp::DW_CFA_advance_loc + 2,
4721   elfcpp::DW_CFA_restore_extended, 65
4722 };
4723 
4724 static const unsigned char glink_eh_frame_fde_64v2_localentry0[] =
4725 {
4726   0, 0, 0, 0,				// Replaced with offset to .glink.
4727   0, 0, 0, 0,				// Replaced with size of .glink.
4728   0,					// Augmentation size.
4729   elfcpp::DW_CFA_advance_loc + 3,
4730   elfcpp::DW_CFA_register, 65, 0,
4731   elfcpp::DW_CFA_advance_loc + 2,
4732   elfcpp::DW_CFA_restore_extended, 65
4733 };
4734 
4735 // Describe __glink_PLTresolve use of LR, 32-bit version.
4736 static const unsigned char glink_eh_frame_fde_32[] =
4737 {
4738   0, 0, 0, 0,				// Replaced with offset to .glink.
4739   0, 0, 0, 0,				// Replaced with size of .glink.
4740   0,					// Augmentation size.
4741   elfcpp::DW_CFA_advance_loc + 2,
4742   elfcpp::DW_CFA_register, 65, 0,
4743   elfcpp::DW_CFA_advance_loc + 4,
4744   elfcpp::DW_CFA_restore_extended, 65
4745 };
4746 
4747 static const unsigned char default_fde[] =
4748 {
4749   0, 0, 0, 0,				// Replaced with offset to stubs.
4750   0, 0, 0, 0,				// Replaced with size of stubs.
4751   0,					// Augmentation size.
4752   elfcpp::DW_CFA_nop,			// Pad.
4753   elfcpp::DW_CFA_nop,
4754   elfcpp::DW_CFA_nop
4755 };
4756 
4757 template<bool big_endian>
4758 static inline void
write_insn(unsigned char * p,uint32_t v)4759 write_insn(unsigned char* p, uint32_t v)
4760 {
4761   elfcpp::Swap<32, big_endian>::writeval(p, v);
4762 }
4763 
4764 template<int size>
4765 static inline unsigned int
param_plt_align()4766 param_plt_align()
4767 {
4768   if (!parameters->options().user_set_plt_align())
4769     return size == 64 ? 32 : 8;
4770   return 1 << parameters->options().plt_align();
4771 }
4772 
4773 // Stub_table holds information about plt and long branch stubs.
4774 // Stubs are built in an area following some input section determined
4775 // by group_sections().  This input section is converted to a relaxed
4776 // input section allowing it to be resized to accommodate the stubs
4777 
4778 template<int size, bool big_endian>
4779 class Stub_table : public Output_relaxed_input_section
4780 {
4781  public:
4782   struct Plt_stub_ent
4783   {
Plt_stub_ent__anon10389ec10111::Stub_table::Plt_stub_ent4784     Plt_stub_ent(unsigned int off, unsigned int indx)
4785       : off_(off), indx_(indx), tocoff_(0), p9off_(0), tsize_ (0), iter_(0),
4786 	toc_(0), notoc_(0), p9notoc_(0), r2save_(0), localentry0_(0)
4787     { }
4788 
4789     unsigned int off_;
4790     unsigned int indx_;
4791     // off_ points at p10 notoc stub, tocoff_ is offset from there to
4792     // toc stub, p9off_ is offset to p9notoc stub
4793     unsigned int tocoff_ : 8;
4794     unsigned int p9off_ : 8;
4795     // The size of the toc stub, used to locate blr on tls_get_addr stub.
4796     unsigned int tsize_ : 8;
4797     // Stub revision management
4798     unsigned int iter_ : 1;
4799     // The three types of stubs.
4800     unsigned int toc_ : 1;
4801     unsigned int notoc_ : 1;
4802     unsigned int p9notoc_ : 1;
4803     // Each with a possible variant saving r2 first
4804     unsigned int r2save_ : 1;
4805     // Handy cached info from symbol
4806     unsigned int localentry0_ : 1;
4807   };
4808   struct Branch_stub_ent
4809   {
Branch_stub_ent__anon10389ec10111::Stub_table::Branch_stub_ent4810     Branch_stub_ent(unsigned int off)
4811       : off_(off), tocoff_(0), p9off_(0), iter_(0), toc_(0), notoc_(0),
4812 	p9notoc_(0), save_res_(0), other_(0)
4813     { }
4814 
4815     unsigned int off_;
4816     // off_ points at p10 notoc stub, tocoff_ is offset from there to
4817     // toc stub, p9off_ is offset to p9notoc stub
4818     unsigned int tocoff_ : 8;
4819     unsigned int p9off_ : 8;
4820     // Stub revision management
4821     unsigned int iter_ : 1;
4822     // Four types of stubs.
4823     unsigned int toc_ : 1;
4824     unsigned int notoc_ : 1;
4825     unsigned int p9notoc_ : 1;
4826     unsigned int save_res_ : 1;
4827     // Handy cached info from symbol
4828     unsigned int other_ : 3;
4829   };
4830   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
4831   static const Address invalid_address = static_cast<Address>(0) - 1;
4832 
Stub_table(Target_powerpc<size,big_endian> * targ,Output_section * output_section,const Output_section::Input_section * owner,uint32_t id)4833   Stub_table(Target_powerpc<size, big_endian>* targ,
4834 	     Output_section* output_section,
4835 	     const Output_section::Input_section* owner,
4836 	     uint32_t id)
4837     : Output_relaxed_input_section(owner->relobj(), owner->shndx(),
4838 				   owner->relobj()
4839 				   ->section_addralign(owner->shndx())),
4840       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
4841       orig_data_size_(owner->current_data_size()),
4842       plt_size_(0), last_plt_size_(0),
4843       branch_size_(0), last_branch_size_(0), min_size_threshold_(0),
4844       need_save_res_(false), need_resize_(false), resizing_(false),
4845       uniq_(id)
4846   {
4847     this->set_output_section(output_section);
4848 
4849     std::vector<Output_relaxed_input_section*> new_relaxed;
4850     new_relaxed.push_back(this);
4851     output_section->convert_input_sections_to_relaxed_sections(new_relaxed);
4852   }
4853 
4854   // Add a plt call stub.
4855   bool
4856   add_plt_call_entry(Address,
4857 		     const Sized_relobj_file<size, big_endian>*,
4858 		     const Symbol*,
4859 		     unsigned int,
4860 		     Address,
4861 		     bool);
4862 
4863   bool
4864   add_plt_call_entry(Address,
4865 		     const Sized_relobj_file<size, big_endian>*,
4866 		     unsigned int,
4867 		     unsigned int,
4868 		     Address,
4869 		     bool);
4870 
4871   // Find a given plt call stub.
4872   const Plt_stub_ent*
4873   find_plt_call_entry(const Symbol*) const;
4874 
4875   const Plt_stub_ent*
4876   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4877 		      unsigned int) const;
4878 
4879   const Plt_stub_ent*
4880   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4881 		      const Symbol*,
4882 		      unsigned int,
4883 		      Address) const;
4884 
4885   const Plt_stub_ent*
4886   find_plt_call_entry(const Sized_relobj_file<size, big_endian>*,
4887 		      unsigned int,
4888 		      unsigned int,
4889 		      Address) const;
4890 
4891   // Add a long branch stub.
4892   bool
4893   add_long_branch_entry(unsigned int, Address, Address, unsigned int, bool);
4894 
4895   const Branch_stub_ent*
4896   find_long_branch_entry(Address) const;
4897 
4898   bool
can_reach_stub(Address from,unsigned int off,unsigned int r_type)4899   can_reach_stub(Address from, unsigned int off, unsigned int r_type)
4900   {
4901     Address max_branch_offset = max_branch_delta<size>(r_type);
4902     if (max_branch_offset == 0)
4903       return true;
4904     gold_assert(from != invalid_address);
4905     Address loc = off + this->stub_address();
4906     return loc - from + max_branch_offset < 2 * max_branch_offset;
4907   }
4908 
4909   void
clear_stubs(bool all)4910   clear_stubs(bool all)
4911   {
4912     this->plt_call_stubs_.clear();
4913     this->plt_size_ = 0;
4914     this->long_branch_stubs_.clear();
4915     this->branch_size_ = 0;
4916     this->need_save_res_ = false;
4917     if (all)
4918       {
4919 	this->last_plt_size_ = 0;
4920 	this->last_branch_size_ = 0;
4921       }
4922   }
4923 
4924   bool
need_resize() const4925   need_resize() const
4926   { return need_resize_; }
4927 
4928   void
set_resizing(bool val)4929   set_resizing(bool val)
4930   {
4931     this->resizing_ = val;
4932     if (val)
4933       {
4934 	this->need_resize_ = false;
4935 	this->plt_size_ = 0;
4936 	this->branch_size_ = 0;
4937 	this->need_save_res_ = false;
4938       }
4939   }
4940 
4941   Address
set_address_and_size(const Output_section * os,Address off)4942   set_address_and_size(const Output_section* os, Address off)
4943   {
4944     Address start_off = off;
4945     off += this->orig_data_size_;
4946     Address my_size = this->plt_size_ + this->branch_size_;
4947     if (this->need_save_res_)
4948       my_size += this->targ_->savres_section()->data_size();
4949     if (my_size != 0)
4950       off = align_address(off, this->stub_align());
4951     // Include original section size and alignment padding in size
4952     my_size += off - start_off;
4953     // Ensure new size is always larger than min size
4954     // threshold. Alignment requirement is included in "my_size", so
4955     // increase "my_size" does not invalidate alignment.
4956     if (my_size < this->min_size_threshold_)
4957       my_size = this->min_size_threshold_;
4958     this->reset_address_and_file_offset();
4959     this->set_current_data_size(my_size);
4960     this->set_address_and_file_offset(os->address() + start_off,
4961 				      os->offset() + start_off);
4962     return my_size;
4963   }
4964 
4965   Address
stub_address() const4966   stub_address() const
4967   {
4968     return align_address(this->address() + this->orig_data_size_,
4969 			 this->stub_align());
4970   }
4971 
4972   Address
stub_offset() const4973   stub_offset() const
4974   {
4975     return align_address(this->offset() + this->orig_data_size_,
4976 			 this->stub_align());
4977   }
4978 
4979   section_size_type
plt_size() const4980   plt_size() const
4981   { return this->plt_size_; }
4982 
4983   section_size_type
branch_size() const4984   branch_size() const
4985   { return this->branch_size_; }
4986 
4987   void
set_min_size_threshold(Address min_size)4988   set_min_size_threshold(Address min_size)
4989   { this->min_size_threshold_ = min_size; }
4990 
4991   void
4992   define_stub_syms(Symbol_table*);
4993 
4994   bool
size_update()4995   size_update()
4996   {
4997     Output_section* os = this->output_section();
4998     if (os->addralign() < this->stub_align())
4999       {
5000 	os->set_addralign(this->stub_align());
5001 	// FIXME: get rid of the insane checkpointing.
5002 	// We can't increase alignment of the input section to which
5003 	// stubs are attached;  The input section may be .init which
5004 	// is pasted together with other .init sections to form a
5005 	// function.  Aligning might insert zero padding resulting in
5006 	// sigill.  However we do need to increase alignment of the
5007 	// output section so that the align_address() on offset in
5008 	// set_address_and_size() adds the same padding as the
5009 	// align_address() on address in stub_address().
5010 	// What's more, we need this alignment for the layout done in
5011 	// relaxation_loop_body() so that the output section starts at
5012 	// a suitably aligned address.
5013 	os->checkpoint_set_addralign(this->stub_align());
5014       }
5015     if (this->last_plt_size_ != this->plt_size_
5016 	|| this->last_branch_size_ != this->branch_size_)
5017       {
5018 	this->last_plt_size_ = this->plt_size_;
5019 	this->last_branch_size_ = this->branch_size_;
5020 	return true;
5021       }
5022     return false;
5023   }
5024 
5025   // Add .eh_frame info for this stub section.
5026   void
5027   add_eh_frame(Layout* layout);
5028 
5029   // Remove .eh_frame info for this stub section.
5030   void
5031   remove_eh_frame(Layout* layout);
5032 
5033   Target_powerpc<size, big_endian>*
targ() const5034   targ() const
5035   { return targ_; }
5036 
5037  private:
5038   class Plt_stub_key;
5039   class Plt_stub_key_hash;
5040   typedef Unordered_map<Plt_stub_key, Plt_stub_ent,
5041 			Plt_stub_key_hash> Plt_stub_entries;
5042   class Branch_stub_key;
5043   class Branch_stub_key_hash;
5044   typedef Unordered_map<Branch_stub_key, Branch_stub_ent,
5045 			Branch_stub_key_hash> Branch_stub_entries;
5046 
5047   // Alignment of stub section.
5048   unsigned int
stub_align() const5049   stub_align() const
5050   {
5051     unsigned int min_align = size == 64 ? 32 : 16;
5052     unsigned int user_align = 1 << parameters->options().plt_align();
5053     return std::max(user_align, min_align);
5054   }
5055 
5056   // Return the plt offset for the given call stub.
5057   Address
plt_off(typename Plt_stub_entries::const_iterator p,const Output_data_plt_powerpc<size,big_endian> ** sec) const5058   plt_off(typename Plt_stub_entries::const_iterator p,
5059 	  const Output_data_plt_powerpc<size, big_endian>** sec) const
5060   {
5061     const Symbol* gsym = p->first.sym_;
5062     if (gsym != NULL)
5063       return this->targ_->plt_off(gsym, sec);
5064     else
5065       {
5066 	const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
5067 	unsigned int local_sym_index = p->first.locsym_;
5068 	return this->targ_->plt_off(relobj, local_sym_index, sec);
5069       }
5070   }
5071 
5072   // Size of a given plt call stub.
5073   unsigned int
5074   plt_call_size(typename Plt_stub_entries::iterator p) const;
5075 
5076   unsigned int
plt_call_align(unsigned int bytes) const5077   plt_call_align(unsigned int bytes) const
5078   {
5079     unsigned int align = param_plt_align<size>();
5080     return (bytes + align - 1) & -align;
5081   }
5082 
5083   // Return long branch stub size.
5084   unsigned int
5085   branch_stub_size(typename Branch_stub_entries::iterator p,
5086 		   bool* need_lt);
5087 
5088   void
5089   build_tls_opt_head(unsigned char** pp,  bool save_lr);
5090 
5091   void
5092   build_tls_opt_tail(unsigned char* p);
5093 
5094   void
5095   plt_error(const Plt_stub_key& p);
5096 
5097   // Write out stubs.
5098   void
5099   do_write(Output_file*);
5100 
5101   // Plt call stub keys.
5102   class Plt_stub_key
5103   {
5104   public:
Plt_stub_key(const Symbol * sym)5105     Plt_stub_key(const Symbol* sym)
5106       : sym_(sym), object_(0), addend_(0), locsym_(0)
5107     { }
5108 
Plt_stub_key(const Sized_relobj_file<size,big_endian> * object,unsigned int locsym_index)5109     Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5110 		 unsigned int locsym_index)
5111       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5112     { }
5113 
Plt_stub_key(const Sized_relobj_file<size,big_endian> * object,const Symbol * sym,unsigned int r_type,Address addend)5114     Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5115 		 const Symbol* sym,
5116 		 unsigned int r_type,
5117 		 Address addend)
5118       : sym_(sym), object_(0), addend_(0), locsym_(0)
5119     {
5120       if (size != 32)
5121 	this->addend_ = addend;
5122       else if (parameters->options().output_is_position_independent()
5123 	       && (r_type == elfcpp::R_PPC_PLTREL24
5124 		   || r_type == elfcpp::R_POWERPC_PLTCALL))
5125 	{
5126 	  this->addend_ = addend;
5127 	  if (this->addend_ >= 32768)
5128 	    this->object_ = object;
5129 	}
5130     }
5131 
Plt_stub_key(const Sized_relobj_file<size,big_endian> * object,unsigned int locsym_index,unsigned int r_type,Address addend)5132     Plt_stub_key(const Sized_relobj_file<size, big_endian>* object,
5133 		 unsigned int locsym_index,
5134 		 unsigned int r_type,
5135 		 Address addend)
5136       : sym_(NULL), object_(object), addend_(0), locsym_(locsym_index)
5137     {
5138       if (size != 32)
5139 	this->addend_ = addend;
5140       else if (parameters->options().output_is_position_independent()
5141 	       && (r_type == elfcpp::R_PPC_PLTREL24
5142 		   || r_type == elfcpp::R_POWERPC_PLTCALL))
5143 	this->addend_ = addend;
5144     }
5145 
operator ==(const Plt_stub_key & that) const5146     bool operator==(const Plt_stub_key& that) const
5147     {
5148       return (this->sym_ == that.sym_
5149 	      && this->object_ == that.object_
5150 	      && this->addend_ == that.addend_
5151 	      && this->locsym_ == that.locsym_);
5152     }
5153 
5154     const Symbol* sym_;
5155     const Sized_relobj_file<size, big_endian>* object_;
5156     typename elfcpp::Elf_types<size>::Elf_Addr addend_;
5157     unsigned int locsym_;
5158   };
5159 
5160   class Plt_stub_key_hash
5161   {
5162   public:
operator ()(const Plt_stub_key & ent) const5163     size_t operator()(const Plt_stub_key& ent) const
5164     {
5165       return (reinterpret_cast<uintptr_t>(ent.sym_)
5166 	      ^ reinterpret_cast<uintptr_t>(ent.object_)
5167 	      ^ ent.addend_
5168 	      ^ ent.locsym_);
5169     }
5170   };
5171 
5172   // Long branch stub keys.
5173   class Branch_stub_key
5174   {
5175   public:
Branch_stub_key(Address to)5176     Branch_stub_key(Address to)
5177       : dest_(to)
5178     { }
5179 
operator ==(const Branch_stub_key & that) const5180     bool operator==(const Branch_stub_key& that) const
5181     {
5182       return this->dest_ == that.dest_;
5183     }
5184 
5185     Address dest_;
5186   };
5187 
5188   class Branch_stub_key_hash
5189   {
5190   public:
operator ()(const Branch_stub_key & key) const5191     size_t operator()(const Branch_stub_key& key) const
5192     { return key.dest_; }
5193   };
5194 
5195   // In a sane world this would be a global.
5196   Target_powerpc<size, big_endian>* targ_;
5197   // Map sym/object/addend to stub offset.
5198   Plt_stub_entries plt_call_stubs_;
5199   // Map destination address to stub offset.
5200   Branch_stub_entries long_branch_stubs_;
5201   // size of input section
5202   section_size_type orig_data_size_;
5203   // size of stubs
5204   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
5205   // Some rare cases cause (PR/20529) fluctuation in stub table
5206   // size, which leads to an endless relax loop. This is to be fixed
5207   // by, after the first few iterations, allowing only increase of
5208   // stub table size. This variable sets the minimal possible size of
5209   // a stub table, it is zero for the first few iterations, then
5210   // increases monotonically.
5211   Address min_size_threshold_;
5212   // Set if this stub group needs a copy of out-of-line register
5213   // save/restore functions.
5214   bool need_save_res_;
5215   // Set when notoc_/r2save_ changes after sizing a stub
5216   bool need_resize_;
5217   // Set when resizing stubs
5218   bool resizing_;
5219   // Per stub table unique identifier.
5220   uint32_t uniq_;
5221 };
5222 
5223 // Add a plt call stub, if we do not already have one for this
5224 // sym/object/addend combo.
5225 
5226 template<int size, bool big_endian>
5227 bool
add_plt_call_entry(Address from,const Sized_relobj_file<size,big_endian> * object,const Symbol * gsym,unsigned int r_type,Address addend,bool tocsave)5228 Stub_table<size, big_endian>::add_plt_call_entry(
5229     Address from,
5230     const Sized_relobj_file<size, big_endian>* object,
5231     const Symbol* gsym,
5232     unsigned int r_type,
5233     Address addend,
5234     bool tocsave)
5235 {
5236   Plt_stub_key key(object, gsym, r_type, addend);
5237   Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5238   std::pair<typename Plt_stub_entries::iterator, bool> p
5239     = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5240   if (size == 64)
5241     {
5242       if (p.second
5243 	  && this->targ_->is_elfv2_localentry0(gsym))
5244 	{
5245 	  p.first->second.localentry0_ = 1;
5246 	  this->targ_->set_has_localentry0();
5247 	}
5248       if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5249 	  || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5250 	{
5251 	  if (this->targ_->power10_stubs()
5252 	      && (!this->targ_->power10_stubs_auto()
5253 		  || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5254 	    {
5255 	      if (!p.second && !p.first->second.notoc_)
5256 		this->need_resize_ = true;
5257 	      p.first->second.notoc_ = 1;
5258 	    }
5259 	  else
5260 	    {
5261 	      if (!p.second && !p.first->second.p9notoc_)
5262 		this->need_resize_ = true;
5263 	      p.first->second.p9notoc_ = 1;
5264 	    }
5265 	}
5266       else
5267 	{
5268 	  if (!p.second && !p.first->second.toc_)
5269 	    this->need_resize_ = true;
5270 	  p.first->second.toc_ = 1;
5271 	  if (!tocsave && !p.first->second.localentry0_)
5272 	    {
5273 	      if (!p.second && !p.first->second.r2save_)
5274 		this->need_resize_ = true;
5275 	      p.first->second.r2save_ = 1;
5276 	    }
5277 	}
5278     }
5279   if (p.second || (this->resizing_ && !p.first->second.iter_))
5280     {
5281       if (this->resizing_)
5282 	{
5283 	  p.first->second.iter_ = 1;
5284 	  p.first->second.off_ = this->plt_size_;
5285 	}
5286       this->plt_size_ += this->plt_call_size(p.first);
5287       if (this->targ_->is_tls_get_addr_opt(gsym))
5288 	this->targ_->set_has_tls_get_addr_opt();
5289     }
5290   return this->can_reach_stub(from, p.first->second.off_, r_type);
5291 }
5292 
5293 template<int size, bool big_endian>
5294 bool
add_plt_call_entry(Address from,const Sized_relobj_file<size,big_endian> * object,unsigned int locsym_index,unsigned int r_type,Address addend,bool tocsave)5295 Stub_table<size, big_endian>::add_plt_call_entry(
5296     Address from,
5297     const Sized_relobj_file<size, big_endian>* object,
5298     unsigned int locsym_index,
5299     unsigned int r_type,
5300     Address addend,
5301     bool tocsave)
5302 {
5303   Plt_stub_key key(object, locsym_index, r_type, addend);
5304   Plt_stub_ent ent(this->plt_size_, this->plt_call_stubs_.size());
5305   std::pair<typename Plt_stub_entries::iterator, bool> p
5306     = this->plt_call_stubs_.insert(std::make_pair(key, ent));
5307   if (size == 64)
5308     {
5309       if (p.second
5310 	  && this->targ_->is_elfv2_localentry0(object, locsym_index))
5311 	{
5312 	  p.first->second.localentry0_ = 1;
5313 	  this->targ_->set_has_localentry0();
5314 	}
5315       if (r_type == elfcpp::R_PPC64_REL24_NOTOC
5316 	  || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
5317 	{
5318 	  if (this->targ_->power10_stubs()
5319 	      && (!this->targ_->power10_stubs_auto()
5320 		  || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5321 	    {
5322 	      if (!p.second && !p.first->second.notoc_)
5323 		this->need_resize_ = true;
5324 	      p.first->second.notoc_ = 1;
5325 	    }
5326 	  else
5327 	    {
5328 	      if (!p.second && !p.first->second.p9notoc_)
5329 		this->need_resize_ = true;
5330 	      p.first->second.p9notoc_ = 1;
5331 	    }
5332 	}
5333       else
5334 	{
5335 	  if (!p.second && !p.first->second.toc_)
5336 	    this->need_resize_ = true;
5337 	  p.first->second.toc_ = 1;
5338 	  if (!tocsave && !p.first->second.localentry0_)
5339 	    {
5340 	      if (!p.second && !p.first->second.r2save_)
5341 		this->need_resize_ = true;
5342 	      p.first->second.r2save_ = 1;
5343 	    }
5344 	}
5345     }
5346   if (p.second || (this->resizing_ && !p.first->second.iter_))
5347     {
5348       if (this->resizing_)
5349 	{
5350 	  p.first->second.iter_ = 1;
5351 	  p.first->second.off_ = this->plt_size_;
5352 	}
5353       this->plt_size_ += this->plt_call_size(p.first);
5354     }
5355   return this->can_reach_stub(from, p.first->second.off_, r_type);
5356 }
5357 
5358 // Find a plt call stub.
5359 
5360 template<int size, bool big_endian>
5361 const typename Stub_table<size, big_endian>::Plt_stub_ent*
find_plt_call_entry(const Sized_relobj_file<size,big_endian> * object,const Symbol * gsym,unsigned int r_type,Address addend) const5362 Stub_table<size, big_endian>::find_plt_call_entry(
5363     const Sized_relobj_file<size, big_endian>* object,
5364     const Symbol* gsym,
5365     unsigned int r_type,
5366     Address addend) const
5367 {
5368   Plt_stub_key key(object, gsym, r_type, addend);
5369   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5370   if (p == this->plt_call_stubs_.end())
5371     return NULL;
5372   return &p->second;
5373 }
5374 
5375 template<int size, bool big_endian>
5376 const typename Stub_table<size, big_endian>::Plt_stub_ent*
find_plt_call_entry(const Symbol * gsym) const5377 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
5378 {
5379   Plt_stub_key key(gsym);
5380   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5381   if (p == this->plt_call_stubs_.end())
5382     return NULL;
5383   return &p->second;
5384 }
5385 
5386 template<int size, bool big_endian>
5387 const typename Stub_table<size, big_endian>::Plt_stub_ent*
find_plt_call_entry(const Sized_relobj_file<size,big_endian> * object,unsigned int locsym_index,unsigned int r_type,Address addend) const5388 Stub_table<size, big_endian>::find_plt_call_entry(
5389     const Sized_relobj_file<size, big_endian>* object,
5390     unsigned int locsym_index,
5391     unsigned int r_type,
5392     Address addend) const
5393 {
5394   Plt_stub_key key(object, locsym_index, r_type, addend);
5395   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5396   if (p == this->plt_call_stubs_.end())
5397     return NULL;
5398   return &p->second;
5399 }
5400 
5401 template<int size, bool big_endian>
5402 const typename Stub_table<size, big_endian>::Plt_stub_ent*
find_plt_call_entry(const Sized_relobj_file<size,big_endian> * object,unsigned int locsym_index) const5403 Stub_table<size, big_endian>::find_plt_call_entry(
5404     const Sized_relobj_file<size, big_endian>* object,
5405     unsigned int locsym_index) const
5406 {
5407   Plt_stub_key key(object, locsym_index);
5408   typename Plt_stub_entries::const_iterator p = this->plt_call_stubs_.find(key);
5409   if (p == this->plt_call_stubs_.end())
5410     return NULL;
5411   return &p->second;
5412 }
5413 
5414 // Add a long branch stub if we don't already have one to given
5415 // destination.
5416 
5417 template<int size, bool big_endian>
5418 bool
add_long_branch_entry(unsigned int r_type,Address from,Address to,unsigned int other,bool save_res)5419 Stub_table<size, big_endian>::add_long_branch_entry(
5420     unsigned int r_type,
5421     Address from,
5422     Address to,
5423     unsigned int other,
5424     bool save_res)
5425 {
5426   Branch_stub_key key(to);
5427   Branch_stub_ent ent(this->branch_size_);
5428   std::pair<typename Branch_stub_entries::iterator, bool> p
5429     = this->long_branch_stubs_.insert(std::make_pair(key, ent));
5430   if (save_res)
5431     {
5432       if (!p.second && !p.first->second.save_res_)
5433 	this->need_resize_ = true;
5434       p.first->second.save_res_ = true;
5435     }
5436   else if (size == 64
5437 	   && (r_type == elfcpp::R_PPC64_REL24_NOTOC
5438 	       || r_type == elfcpp::R_PPC64_REL24_P9NOTOC))
5439     {
5440       if (this->targ_->power10_stubs()
5441 	  && (!this->targ_->power10_stubs_auto()
5442 	      || r_type == elfcpp::R_PPC64_REL24_NOTOC))
5443 	{
5444 	  if (!p.second && !p.first->second.notoc_)
5445 	    this->need_resize_ = true;
5446 	  p.first->second.notoc_ = true;
5447 	}
5448       else
5449 	{
5450 	  if (!p.second && !p.first->second.p9notoc_)
5451 	    this->need_resize_ = true;
5452 	  p.first->second.p9notoc_ = true;
5453 	}
5454     }
5455   else
5456     {
5457       if (!p.second && !p.first->second.toc_)
5458 	this->need_resize_ = true;
5459       p.first->second.toc_ = true;
5460     }
5461   if (size == 64 && p.first->second.other_ == 0)
5462     p.first->second.other_ = other;
5463   if (p.second || (this->resizing_ && !p.first->second.iter_))
5464     {
5465       if (this->resizing_)
5466 	{
5467 	  p.first->second.iter_ = 1;
5468 	  p.first->second.off_ = this->branch_size_;
5469 	}
5470       if (save_res)
5471 	this->need_save_res_ = true;
5472       else
5473 	{
5474 	  bool need_lt = false;
5475 	  unsigned int stub_size = this->branch_stub_size(p.first, &need_lt);
5476 	  this->branch_size_ += stub_size;
5477 	  if (size == 64 && need_lt)
5478 	    this->targ_->add_branch_lookup_table(to);
5479 	}
5480     }
5481   return this->can_reach_stub(from, p.first->second.off_, r_type);
5482 }
5483 
5484 // Find long branch stub offset.
5485 
5486 template<int size, bool big_endian>
5487 const typename Stub_table<size, big_endian>::Branch_stub_ent*
find_long_branch_entry(Address to) const5488 Stub_table<size, big_endian>::find_long_branch_entry(Address to) const
5489 {
5490   Branch_stub_key key(to);
5491   typename Branch_stub_entries::const_iterator p
5492     = this->long_branch_stubs_.find(key);
5493   if (p == this->long_branch_stubs_.end())
5494     return NULL;
5495   return &p->second;
5496 }
5497 
5498 template<bool big_endian>
5499 static void
eh_advance(std::vector<unsigned char> & fde,unsigned int delta)5500 eh_advance (std::vector<unsigned char>& fde, unsigned int delta)
5501 {
5502   delta /= 4;
5503   if (delta < 64)
5504     fde.push_back(elfcpp::DW_CFA_advance_loc + delta);
5505   else if (delta < 256)
5506     {
5507       fde.push_back(elfcpp::DW_CFA_advance_loc1);
5508       fde.push_back(delta);
5509     }
5510   else if (delta < 65536)
5511     {
5512       fde.resize(fde.size() + 3);
5513       unsigned char *p = &*fde.end() - 3;
5514       *p++ = elfcpp::DW_CFA_advance_loc2;
5515       elfcpp::Swap<16, big_endian>::writeval(p, delta);
5516     }
5517   else
5518     {
5519       fde.resize(fde.size() + 5);
5520       unsigned char *p = &*fde.end() - 5;
5521       *p++ = elfcpp::DW_CFA_advance_loc4;
5522       elfcpp::Swap<32, big_endian>::writeval(p, delta);
5523     }
5524 }
5525 
5526 template<typename T>
5527 static bool
stub_sort(T s1,T s2)5528 stub_sort(T s1, T s2)
5529 {
5530   return s1->second.off_ < s2->second.off_;
5531 }
5532 
5533 // Add .eh_frame info for this stub section.  Unlike other linker
5534 // generated .eh_frame this is added late in the link, because we
5535 // only want the .eh_frame info if this particular stub section is
5536 // non-empty.
5537 
5538 template<int size, bool big_endian>
5539 void
add_eh_frame(Layout * layout)5540 Stub_table<size, big_endian>::add_eh_frame(Layout* layout)
5541 {
5542   if (size != 64
5543       || !parameters->options().ld_generated_unwind_info())
5544     return;
5545 
5546   // Since we add stub .eh_frame info late, it must be placed
5547   // after all other linker generated .eh_frame info so that
5548   // merge mapping need not be updated for input sections.
5549   // There is no provision to use a different CIE to that used
5550   // by .glink.
5551   if (!this->targ_->has_glink())
5552     return;
5553 
5554   typedef typename Plt_stub_entries::iterator plt_iter;
5555   std::vector<plt_iter> calls;
5556   if (!this->plt_call_stubs_.empty())
5557     for (plt_iter cs = this->plt_call_stubs_.begin();
5558 	 cs != this->plt_call_stubs_.end();
5559 	 ++cs)
5560       if (cs->second.p9notoc_
5561 	  || (cs->second.toc_
5562 	      && cs->second.r2save_
5563 	      && !cs->second.localentry0_
5564 	      && this->targ_->is_tls_get_addr_opt(cs->first.sym_)))
5565 	calls.push_back(cs);
5566   if (calls.size() > 1)
5567     std::stable_sort(calls.begin(), calls.end(),
5568 		     stub_sort<plt_iter>);
5569 
5570   typedef typename Branch_stub_entries::const_iterator branch_iter;
5571   std::vector<branch_iter> branches;
5572   if (!this->long_branch_stubs_.empty()
5573       && !this->targ_->power10_stubs())
5574     for (branch_iter bs = this->long_branch_stubs_.begin();
5575 	 bs != this->long_branch_stubs_.end();
5576 	 ++bs)
5577       if (bs->second.notoc_)
5578 	branches.push_back(bs);
5579   if (branches.size() > 1)
5580     std::stable_sort(branches.begin(), branches.end(),
5581 		     stub_sort<branch_iter>);
5582 
5583   if (calls.empty() && branches.empty())
5584     return;
5585 
5586   unsigned int last_eh_loc = 0;
5587   // offset pcrel sdata4, size udata4, and augmentation size byte.
5588   std::vector<unsigned char> fde(9, 0);
5589 
5590   for (unsigned int i = 0; i < calls.size(); i++)
5591     {
5592       plt_iter cs = calls[i];
5593       unsigned int off = cs->second.off_;
5594       // The __tls_get_addr_opt call stub needs to describe where
5595       // it saves LR, to support exceptions that might be thrown
5596       // from __tls_get_addr, and to support asynchronous exceptions.
5597       if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5598 	{
5599 	  off += 7 * 4;
5600 	  if (cs->second.toc_
5601 	      && cs->second.r2save_
5602 	      && !cs->second.localentry0_)
5603 	    {
5604 	      off += cs->second.tocoff_ + 2 * 4;
5605 	      eh_advance<big_endian>(fde, off - last_eh_loc);
5606 	      fde.resize(fde.size() + 6);
5607 	      unsigned char* p = &*fde.end() - 6;
5608 	      *p++ = elfcpp::DW_CFA_offset_extended_sf;
5609 	      *p++ = 65;
5610 	      *p++ = -(this->targ_->stk_linker() / 8) & 0x7f;
5611 	      unsigned int delta = cs->second.tsize_ - 9 * 4 - 4;
5612 	      *p++ = elfcpp::DW_CFA_advance_loc + delta / 4;
5613 	      *p++ = elfcpp::DW_CFA_restore_extended;
5614 	      *p++ = 65;
5615 	      last_eh_loc = off + delta;
5616 	      off = cs->second.off_ + 7 * 4;
5617 	    }
5618 	}
5619       // notoc stubs also should describe LR changes, to support
5620       // asynchronous exceptions.
5621       if (cs->second.p9notoc_)
5622 	{
5623 	  off += cs->second.p9off_;
5624 	  off += (cs->second.r2save_ ? 4 : 0) + 8;
5625 	  eh_advance<big_endian>(fde, off - last_eh_loc);
5626 	  fde.resize(fde.size() + 6);
5627 	  unsigned char* p = &*fde.end() - 6;
5628 	  *p++ = elfcpp::DW_CFA_register;
5629 	  *p++ = 65;
5630 	  *p++ = 12;
5631 	  *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5632 	  *p++ = elfcpp::DW_CFA_restore_extended;
5633 	  *p++ = 65;
5634 	  last_eh_loc = off + 8;
5635 	}
5636     }
5637 
5638   for (unsigned int i = 0; i < branches.size(); i++)
5639     {
5640       branch_iter bs = branches[i];
5641       unsigned int off = bs->second.off_ + 8;
5642       eh_advance<big_endian>(fde, off - last_eh_loc);
5643       fde.resize(fde.size() + 6);
5644       unsigned char* p = &*fde.end() - 6;
5645       *p++ = elfcpp::DW_CFA_register;
5646       *p++ = 65;
5647       *p++ = 12;
5648       *p++ = elfcpp::DW_CFA_advance_loc + 8 / 4;
5649       *p++ = elfcpp::DW_CFA_restore_extended;
5650       *p++ = 65;
5651       last_eh_loc = off + 8;
5652     }
5653 
5654   layout->add_eh_frame_for_plt(this,
5655 			       Eh_cie<size>::eh_frame_cie,
5656 			       sizeof (Eh_cie<size>::eh_frame_cie),
5657 			       &*fde.begin(), fde.size());
5658 }
5659 
5660 template<int size, bool big_endian>
5661 void
remove_eh_frame(Layout * layout)5662 Stub_table<size, big_endian>::remove_eh_frame(Layout* layout)
5663 {
5664   if (size == 64
5665       && parameters->options().ld_generated_unwind_info()
5666       && this->targ_->has_glink())
5667     layout->remove_eh_frame_for_plt(this,
5668 				    Eh_cie<size>::eh_frame_cie,
5669 				    sizeof (Eh_cie<size>::eh_frame_cie));
5670 }
5671 
5672 // A class to handle .glink.
5673 
5674 template<int size, bool big_endian>
5675 class Output_data_glink : public Output_section_data
5676 {
5677  public:
5678   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
5679   static const Address invalid_address = static_cast<Address>(0) - 1;
5680 
Output_data_glink(Target_powerpc<size,big_endian> * targ)5681   Output_data_glink(Target_powerpc<size, big_endian>* targ)
5682     : Output_section_data(16), targ_(targ), global_entry_stubs_(),
5683       end_branch_table_(), ge_size_(0)
5684   { }
5685 
5686   void
5687   add_eh_frame(Layout* layout);
5688 
5689   void
5690   add_global_entry(const Symbol*);
5691 
5692   Address
5693   find_global_entry(const Symbol*) const;
5694 
5695   unsigned int
global_entry_align(unsigned int off) const5696   global_entry_align(unsigned int off) const
5697   {
5698     unsigned int align = param_plt_align<size>();
5699     return (off + align - 1) & -align;
5700   }
5701 
5702   unsigned int
global_entry_off() const5703   global_entry_off() const
5704   {
5705     return this->global_entry_align(this->end_branch_table_);
5706   }
5707 
5708   Address
global_entry_address() const5709   global_entry_address() const
5710   {
5711     gold_assert(this->is_data_size_valid());
5712     return this->address() + this->global_entry_off();
5713   }
5714 
5715   int
pltresolve_size() const5716   pltresolve_size() const
5717   {
5718     if (size == 64)
5719       return (8
5720 	      + (this->targ_->abiversion() < 2 ? 11 * 4
5721 		 : this->targ_->has_localentry0() ? 14 * 4 : 13 * 4));
5722     return 16 * 4;
5723   }
5724 
5725  protected:
5726   // Write to a map file.
5727   void
do_print_to_mapfile(Mapfile * mapfile) const5728   do_print_to_mapfile(Mapfile* mapfile) const
5729   { mapfile->print_output_data(this, _("** glink")); }
5730 
5731  private:
5732   void
5733   set_final_data_size();
5734 
5735   // Write out .glink
5736   void
5737   do_write(Output_file*);
5738 
5739   // Allows access to .got and .plt for do_write.
5740   Target_powerpc<size, big_endian>* targ_;
5741 
5742   // Map sym to stub offset.
5743   typedef Unordered_map<const Symbol*, unsigned int> Global_entry_stub_entries;
5744   Global_entry_stub_entries global_entry_stubs_;
5745 
5746   unsigned int end_branch_table_, ge_size_;
5747 };
5748 
5749 template<int size, bool big_endian>
5750 void
add_eh_frame(Layout * layout)5751 Output_data_glink<size, big_endian>::add_eh_frame(Layout* layout)
5752 {
5753   if (!parameters->options().ld_generated_unwind_info())
5754     return;
5755 
5756   if (size == 64)
5757     {
5758       if (this->targ_->abiversion() < 2)
5759 	layout->add_eh_frame_for_plt(this,
5760 				     Eh_cie<64>::eh_frame_cie,
5761 				     sizeof (Eh_cie<64>::eh_frame_cie),
5762 				     glink_eh_frame_fde_64v1,
5763 				     sizeof (glink_eh_frame_fde_64v1));
5764       else if (this->targ_->has_localentry0())
5765 	layout->add_eh_frame_for_plt(this,
5766 				     Eh_cie<64>::eh_frame_cie,
5767 				     sizeof (Eh_cie<64>::eh_frame_cie),
5768 				     glink_eh_frame_fde_64v2_localentry0,
5769 				     sizeof (glink_eh_frame_fde_64v2));
5770       else
5771 	layout->add_eh_frame_for_plt(this,
5772 				     Eh_cie<64>::eh_frame_cie,
5773 				     sizeof (Eh_cie<64>::eh_frame_cie),
5774 				     glink_eh_frame_fde_64v2,
5775 				     sizeof (glink_eh_frame_fde_64v2));
5776     }
5777   else
5778     {
5779       // 32-bit .glink can use the default since the CIE return
5780       // address reg, LR, is valid.
5781       layout->add_eh_frame_for_plt(this,
5782 				   Eh_cie<32>::eh_frame_cie,
5783 				   sizeof (Eh_cie<32>::eh_frame_cie),
5784 				   default_fde,
5785 				   sizeof (default_fde));
5786       // Except where LR is used in a PIC __glink_PLTresolve.
5787       if (parameters->options().output_is_position_independent())
5788 	layout->add_eh_frame_for_plt(this,
5789 				     Eh_cie<32>::eh_frame_cie,
5790 				     sizeof (Eh_cie<32>::eh_frame_cie),
5791 				     glink_eh_frame_fde_32,
5792 				     sizeof (glink_eh_frame_fde_32));
5793     }
5794 }
5795 
5796 template<int size, bool big_endian>
5797 void
add_global_entry(const Symbol * gsym)5798 Output_data_glink<size, big_endian>::add_global_entry(const Symbol* gsym)
5799 {
5800   unsigned int off = this->global_entry_align(this->ge_size_);
5801   std::pair<typename Global_entry_stub_entries::iterator, bool> p
5802     = this->global_entry_stubs_.insert(std::make_pair(gsym, off));
5803   if (p.second)
5804     this->ge_size_ = off + 16;
5805 }
5806 
5807 template<int size, bool big_endian>
5808 typename Output_data_glink<size, big_endian>::Address
find_global_entry(const Symbol * gsym) const5809 Output_data_glink<size, big_endian>::find_global_entry(const Symbol* gsym) const
5810 {
5811   typename Global_entry_stub_entries::const_iterator p
5812     = this->global_entry_stubs_.find(gsym);
5813   return p == this->global_entry_stubs_.end() ? invalid_address : p->second;
5814 }
5815 
5816 template<int size, bool big_endian>
5817 void
set_final_data_size()5818 Output_data_glink<size, big_endian>::set_final_data_size()
5819 {
5820   unsigned int count = this->targ_->plt_entry_count();
5821   section_size_type total = 0;
5822 
5823   if (count != 0)
5824     {
5825       if (size == 32)
5826 	{
5827 	  // space for branch table
5828 	  total += 4 * (count - 1);
5829 
5830 	  total += -total & 15;
5831 	  total += this->pltresolve_size();
5832 	}
5833       else
5834 	{
5835 	  total += this->pltresolve_size();
5836 
5837 	  // space for branch table
5838 	  total += 4 * count;
5839 	  if (this->targ_->abiversion() < 2)
5840 	    {
5841 	      total += 4 * count;
5842 	      if (count > 0x8000)
5843 		total += 4 * (count - 0x8000);
5844 	    }
5845 	}
5846     }
5847   this->end_branch_table_ = total;
5848   total = this->global_entry_align(total);
5849   total += this->ge_size_;
5850 
5851   this->set_data_size(total);
5852 }
5853 
5854 // Define symbols on stubs, identifying the stub.
5855 
5856 template<int size, bool big_endian>
5857 void
define_stub_syms(Symbol_table * symtab)5858 Stub_table<size, big_endian>::define_stub_syms(Symbol_table* symtab)
5859 {
5860   if (!this->plt_call_stubs_.empty())
5861     {
5862       // The key for the plt call stub hash table includes addresses,
5863       // therefore traversal order depends on those addresses, which
5864       // can change between runs if gold is a PIE.  Unfortunately the
5865       // output .symtab ordering depends on the order in which symbols
5866       // are added to the linker symtab.  We want reproducible output
5867       // so must sort the call stub symbols.
5868       typedef typename Plt_stub_entries::iterator plt_iter;
5869       std::vector<plt_iter> sorted;
5870       sorted.resize(this->plt_call_stubs_.size());
5871 
5872       for (plt_iter cs = this->plt_call_stubs_.begin();
5873 	   cs != this->plt_call_stubs_.end();
5874 	   ++cs)
5875 	sorted[cs->second.indx_] = cs;
5876 
5877       for (unsigned int i = 0; i < this->plt_call_stubs_.size(); ++i)
5878 	{
5879 	  plt_iter cs = sorted[i];
5880 	  char add[10];
5881 	  add[0] = 0;
5882 	  if (cs->first.addend_ != 0)
5883 	    sprintf(add, "+%x", static_cast<uint32_t>(cs->first.addend_));
5884 	  char obj[10];
5885 	  obj[0] = 0;
5886 	  if (cs->first.object_)
5887 	    {
5888 	      const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
5889 		<const Powerpc_relobj<size, big_endian>*>(cs->first.object_);
5890 	      sprintf(obj, "%x:", ppcobj->uniq());
5891 	    }
5892 	  char localname[9];
5893 	  const char *symname;
5894 	  if (cs->first.sym_ == NULL)
5895 	    {
5896 	      sprintf(localname, "%x", cs->first.locsym_);
5897 	      symname = localname;
5898 	    }
5899 	  else if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
5900 	    symname = this->targ_->tls_get_addr_opt()->name();
5901 	  else
5902 	    symname = cs->first.sym_->name();
5903 	  char* name = new char[8 + 10 + strlen(obj) + strlen(symname) + strlen(add) + 1];
5904 	  sprintf(name, "%08x.plt_call.%s%s%s", this->uniq_, obj, symname, add);
5905 	  Address value
5906 	    = this->stub_address() - this->address() + cs->second.off_;
5907 	  unsigned int stub_size = this->plt_call_size(cs);
5908 	  this->targ_->define_local(symtab, name, this, value, stub_size);
5909 	}
5910     }
5911 
5912   typedef typename Branch_stub_entries::iterator branch_iter;
5913   for (branch_iter bs = this->long_branch_stubs_.begin();
5914        bs != this->long_branch_stubs_.end();
5915        ++bs)
5916     {
5917       if (bs->second.save_res_)
5918 	continue;
5919 
5920       char* name = new char[8 + 13 + 16 + 1];
5921       sprintf(name, "%08x.long_branch.%llx", this->uniq_,
5922 	      static_cast<unsigned long long>(bs->first.dest_));
5923       Address value = (this->stub_address() - this->address()
5924 		       + this->plt_size_ + bs->second.off_);
5925       bool need_lt = false;
5926       unsigned int stub_size = this->branch_stub_size(bs, &need_lt);
5927       this->targ_->define_local(symtab, name, this, value, stub_size);
5928     }
5929 }
5930 
5931 // Emit the start of a __tls_get_addr_opt plt call stub.
5932 
5933 template<int size, bool big_endian>
5934 void
build_tls_opt_head(unsigned char ** pp,bool save_lr)5935 Stub_table<size, big_endian>::build_tls_opt_head(unsigned char** pp,
5936 						 bool save_lr)
5937 {
5938   unsigned char* p = *pp;
5939   if (size == 64)
5940     {
5941       write_insn<big_endian>(p, ld_11_3 + 0);
5942       p += 4;
5943       write_insn<big_endian>(p, ld_12_3 + 8);
5944       p += 4;
5945       write_insn<big_endian>(p, mr_0_3);
5946       p += 4;
5947       write_insn<big_endian>(p, cmpdi_11_0);
5948       p += 4;
5949       write_insn<big_endian>(p, add_3_12_13);
5950       p += 4;
5951       write_insn<big_endian>(p, beqlr);
5952       p += 4;
5953       write_insn<big_endian>(p, mr_3_0);
5954       p += 4;
5955       if (save_lr)
5956 	{
5957 	  write_insn<big_endian>(p, mflr_11);
5958 	  p += 4;
5959 	  write_insn<big_endian>(p, (std_11_1 + this->targ_->stk_linker()));
5960 	  p += 4;
5961 	}
5962     }
5963   else
5964     {
5965       write_insn<big_endian>(p, lwz_11_3 + 0);
5966       p += 4;
5967       write_insn<big_endian>(p, lwz_12_3 + 4);
5968       p += 4;
5969       write_insn<big_endian>(p, mr_0_3);
5970       p += 4;
5971       write_insn<big_endian>(p, cmpwi_11_0);
5972       p += 4;
5973       write_insn<big_endian>(p, add_3_12_2);
5974       p += 4;
5975       write_insn<big_endian>(p, beqlr);
5976       p += 4;
5977       write_insn<big_endian>(p, mr_3_0);
5978       p += 4;
5979       write_insn<big_endian>(p, nop);
5980       p += 4;
5981     }
5982   *pp = p;
5983 }
5984 
5985 // Emit the tail of a __tls_get_addr_opt plt call stub.
5986 
5987 template<int size, bool big_endian>
5988 void
build_tls_opt_tail(unsigned char * p)5989 Stub_table<size, big_endian>::build_tls_opt_tail(unsigned char* p)
5990 {
5991   write_insn<big_endian>(p, bctrl);
5992   p += 4;
5993   write_insn<big_endian>(p, ld_2_1 + this->targ_->stk_toc());
5994   p += 4;
5995   write_insn<big_endian>(p, ld_11_1 + this->targ_->stk_linker());
5996   p += 4;
5997   write_insn<big_endian>(p, mtlr_11);
5998   p += 4;
5999   write_insn<big_endian>(p, blr);
6000 }
6001 
6002 // Emit pc-relative plt call stub code.
6003 
6004 template<bool big_endian>
6005 static unsigned char*
build_power10_offset(unsigned char * p,uint64_t off,uint64_t odd,bool load)6006 build_power10_offset(unsigned char* p, uint64_t off, uint64_t odd, bool load)
6007 {
6008   uint64_t insn;
6009   if (off - odd + (1ULL << 33) < 1ULL << 34)
6010     {
6011       off -= odd;
6012       if (odd)
6013 	{
6014 	  write_insn<big_endian>(p, nop);
6015 	  p += 4;
6016 	}
6017       if (load)
6018 	insn = pld_12_pc;
6019       else
6020 	insn = paddi_12_pc;
6021       insn |= d34(off);
6022       write_insn<big_endian>(p, insn >> 32);
6023       p += 4;
6024       write_insn<big_endian>(p, insn & 0xffffffff);
6025     }
6026   else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6027     {
6028       off -= 8 - odd;
6029       write_insn<big_endian>(p, li_11_0 | (ha34(off) & 0xffff));
6030       p += 4;
6031       if (!odd)
6032 	{
6033 	  write_insn<big_endian>(p, sldi_11_11_34);
6034 	  p += 4;
6035 	}
6036       insn = paddi_12_pc | d34(off);
6037       write_insn<big_endian>(p, insn >> 32);
6038       p += 4;
6039       write_insn<big_endian>(p, insn & 0xffffffff);
6040       p += 4;
6041       if (odd)
6042 	{
6043 	  write_insn<big_endian>(p, sldi_11_11_34);
6044 	  p += 4;
6045 	}
6046       if (load)
6047 	write_insn<big_endian>(p, ldx_12_11_12);
6048       else
6049 	write_insn<big_endian>(p, add_12_11_12);
6050     }
6051   else
6052     {
6053       off -= odd + 8;
6054       write_insn<big_endian>(p, lis_11 | ((ha34(off) >> 16) & 0x3fff));
6055       p += 4;
6056       write_insn<big_endian>(p, ori_11_11_0 | (ha34(off) & 0xffff));
6057       p += 4;
6058       if (odd)
6059 	{
6060 	  write_insn<big_endian>(p, sldi_11_11_34);
6061 	  p += 4;
6062 	}
6063       insn = paddi_12_pc | d34(off);
6064       write_insn<big_endian>(p, insn >> 32);
6065       p += 4;
6066       write_insn<big_endian>(p, insn & 0xffffffff);
6067       p += 4;
6068       if (!odd)
6069 	{
6070 	  write_insn<big_endian>(p, sldi_11_11_34);
6071 	  p += 4;
6072 	}
6073       if (load)
6074 	write_insn<big_endian>(p, ldx_12_11_12);
6075       else
6076 	write_insn<big_endian>(p, add_12_11_12);
6077     }
6078   p += 4;
6079   return p;
6080 }
6081 
6082 // Gets the address of a label (1:) in r11 and builds an offset in r12,
6083 // then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
6084 //	mflr	%r12
6085 //	bcl	20,31,1f
6086 // 1:	mflr	%r11
6087 //	mtlr	%r12
6088 //	lis	%r12,xxx-1b@highest
6089 //	ori	%r12,%r12,xxx-1b@higher
6090 //	sldi	%r12,%r12,32
6091 //	oris	%r12,%r12,xxx-1b@high
6092 //	ori	%r12,%r12,xxx-1b@l
6093 //	add/ldx	%r12,%r11,%r12
6094 
6095 template<bool big_endian>
6096 static unsigned char*
build_notoc_offset(unsigned char * p,uint64_t off,bool load)6097 build_notoc_offset(unsigned char* p, uint64_t off, bool load)
6098 {
6099   write_insn<big_endian>(p, mflr_12);
6100   p += 4;
6101   write_insn<big_endian>(p, bcl_20_31);
6102   p += 4;
6103   write_insn<big_endian>(p, mflr_11);
6104   p += 4;
6105   write_insn<big_endian>(p, mtlr_12);
6106   p += 4;
6107   if (off + 0x8000 < 0x10000)
6108     {
6109       if (load)
6110 	write_insn<big_endian>(p, ld_12_11 + l(off));
6111       else
6112 	write_insn<big_endian>(p, addi_12_11 + l(off));
6113     }
6114   else if (off + 0x80008000ULL < 0x100000000ULL)
6115     {
6116       write_insn<big_endian>(p, addis_12_11 + ha(off));
6117       p += 4;
6118       if (load)
6119 	write_insn<big_endian>(p, ld_12_12 + l(off));
6120       else
6121 	write_insn<big_endian>(p, addi_12_12 + l(off));
6122     }
6123   else
6124     {
6125       if (off + 0x800000000000ULL < 0x1000000000000ULL)
6126 	{
6127 	  write_insn<big_endian>(p, li_12_0 + ((off >> 32) & 0xffff));
6128 	  p += 4;
6129 	}
6130       else
6131 	{
6132 	  write_insn<big_endian>(p, lis_12 + ((off >> 48) & 0xffff));
6133 	  p += 4;
6134 	  if (((off >> 32) & 0xffff) != 0)
6135 	    {
6136 	      write_insn<big_endian>(p, ori_12_12_0 + ((off >> 32) & 0xffff));
6137 	      p += 4;
6138 	    }
6139 	}
6140       if (((off >> 32) & 0xffffffffULL) != 0)
6141 	{
6142 	  write_insn<big_endian>(p, sldi_12_12_32);
6143 	  p += 4;
6144 	}
6145       if (hi(off) != 0)
6146 	{
6147 	  write_insn<big_endian>(p, oris_12_12_0 + hi(off));
6148 	  p += 4;
6149 	}
6150       if (l(off) != 0)
6151 	{
6152 	  write_insn<big_endian>(p, ori_12_12_0 + l(off));
6153 	  p += 4;
6154 	}
6155       if (load)
6156 	write_insn<big_endian>(p, ldx_12_11_12);
6157       else
6158 	write_insn<big_endian>(p, add_12_11_12);
6159     }
6160   p += 4;
6161   return p;
6162 }
6163 
6164 // Size of a given plt call stub.
6165 
6166 template<int size, bool big_endian>
6167 unsigned int
plt_call_size(typename Plt_stub_entries::iterator p) const6168 Stub_table<size, big_endian>::plt_call_size(
6169     typename Plt_stub_entries::iterator p) const
6170 {
6171   if (size == 32)
6172     {
6173       unsigned int bytes = 4 * 4;
6174       if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6175 	bytes = 12 * 4;
6176       return this->plt_call_align(bytes);
6177     }
6178 
6179   const Output_data_plt_powerpc<size, big_endian>* plt;
6180   uint64_t plt_addr = this->plt_off(p, &plt);
6181   plt_addr += plt->address();
6182   if (this->targ_->power10_stubs()
6183       && this->targ_->power10_stubs_auto())
6184     {
6185       unsigned int bytes = 0;
6186       if (p->second.notoc_)
6187 	{
6188 	  if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6189 	    bytes = 7 * 4;
6190 	  uint64_t from = this->stub_address() + p->second.off_ + bytes;
6191 	  uint64_t odd = from & 4;
6192 	  uint64_t off = plt_addr - from;
6193 	  if (off - odd + (1ULL << 33) < 1ULL << 34)
6194 	    bytes += odd + 4 * 4;
6195 	  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6196 	    bytes += 7 * 4;
6197 	  else
6198 	    bytes += 8 * 4;
6199 	  bytes = this->plt_call_align(bytes);
6200 	}
6201       if (p->second.toc_)
6202 	{
6203 	  p->second.tocoff_ = bytes;
6204 	  if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6205 	    {
6206 	      bytes += 7 * 4;
6207 	      if (p->second.r2save_ && !p->second.localentry0_)
6208 		bytes += 2 * 4 + 4 * 4;
6209 	    }
6210 	  if (p->second.r2save_)
6211 	    bytes += 4;
6212 	  uint64_t got_addr = this->targ_->toc_pointer();
6213 	  uint64_t off = plt_addr - got_addr;
6214 	  bytes += 3 * 4 + 4 * (ha(off) != 0);
6215 	  p->second.tsize_ = bytes - p->second.tocoff_;
6216 	  bytes = this->plt_call_align(bytes);
6217 	}
6218       if (p->second.p9notoc_)
6219 	{
6220 	  p->second.p9off_ = bytes;
6221 	  if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6222 	    bytes += 7 * 4;
6223 	  uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6224 	  uint64_t off = plt_addr - from;
6225 	  if (off + 0x8000 < 0x10000)
6226 	    bytes += 7 * 4;
6227 	  else if (off + 0x80008000ULL < 0x100000000ULL)
6228 	    bytes += 8 * 4;
6229 	  else
6230 	    {
6231 	      bytes += 8 * 4;
6232 	      if (off + 0x800000000000ULL >= 0x1000000000000ULL
6233 		  && ((off >> 32) & 0xffff) != 0)
6234 		bytes += 4;
6235 	      if (((off >> 32) & 0xffffffffULL) != 0)
6236 		bytes += 4;
6237 	      if (hi(off) != 0)
6238 		bytes += 4;
6239 	      if (l(off) != 0)
6240 		bytes += 4;
6241 	    }
6242 	  bytes = this->plt_call_align(bytes);
6243 	}
6244       return bytes;
6245     }
6246   else
6247     {
6248       unsigned int bytes = 0;
6249       unsigned int tail = 0;
6250       if (this->targ_->is_tls_get_addr_opt(p->first.sym_))
6251 	{
6252 	  bytes = 7 * 4;
6253 	  if (p->second.r2save_ && !p->second.localentry0_)
6254 	    {
6255 	      bytes = 9 * 4;
6256 	      tail = 4 * 4;
6257 	    }
6258 	}
6259 
6260       if (p->second.r2save_)
6261 	bytes += 4;
6262 
6263       if (this->targ_->power10_stubs())
6264 	{
6265 	  uint64_t from = this->stub_address() + p->second.off_ + bytes;
6266 	  uint64_t odd = from & 4;
6267 	  uint64_t off = plt_addr - from;
6268 	  if (off - odd + (1ULL << 33) < 1ULL << 34)
6269 	    bytes += odd + 4 * 4;
6270 	  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6271 	    bytes += 7 * 4;
6272 	  else
6273 	    bytes += 8 * 4;
6274 	  return this->plt_call_align(bytes + tail);
6275 	}
6276 
6277       if (p->second.p9notoc_)
6278 	{
6279 	  uint64_t from = this->stub_address() + p->second.off_ + bytes + 2 * 4;
6280 	  uint64_t off = plt_addr - from;
6281 	  if (off + 0x8000 < 0x10000)
6282 	    bytes += 7 * 4;
6283 	  else if (off + 0x80008000ULL < 0x100000000ULL)
6284 	    bytes += 8 * 4;
6285 	  else
6286 	    {
6287 	      bytes += 8 * 4;
6288 	      if (off + 0x800000000000ULL >= 0x1000000000000ULL
6289 		  && ((off >> 32) & 0xffff) != 0)
6290 		bytes += 4;
6291 	      if (((off >> 32) & 0xffffffffULL) != 0)
6292 		bytes += 4;
6293 	      if (hi(off) != 0)
6294 		bytes += 4;
6295 	      if (l(off) != 0)
6296 		bytes += 4;
6297 	    }
6298 	  return this->plt_call_align(bytes + tail);
6299 	}
6300 
6301       uint64_t got_addr = this->targ_->toc_pointer();
6302       uint64_t off = plt_addr - got_addr;
6303       bytes += 3 * 4 + 4 * (ha(off) != 0);
6304       if (this->targ_->abiversion() < 2)
6305 	{
6306 	  bool static_chain = parameters->options().plt_static_chain();
6307 	  bool thread_safe = this->targ_->plt_thread_safe();
6308 	  bytes += (4
6309 		    + 4 * static_chain
6310 		    + 8 * thread_safe
6311 		    + 4 * (ha(off + 8 + 8 * static_chain) != ha(off)));
6312 	}
6313       return this->plt_call_align(bytes + tail);
6314     }
6315 }
6316 
6317 // Return long branch stub size.
6318 
6319 template<int size, bool big_endian>
6320 unsigned int
branch_stub_size(typename Branch_stub_entries::iterator p,bool * need_lt)6321 Stub_table<size, big_endian>::branch_stub_size(
6322      typename Branch_stub_entries::iterator p,
6323      bool* need_lt)
6324 {
6325   Address loc = this->stub_address() + this->last_plt_size_ + p->second.off_;
6326   if (size == 32)
6327     {
6328       if (p->first.dest_ - loc + (1 << 25) < 2 << 25)
6329 	return 4;
6330       if (parameters->options().output_is_position_independent())
6331 	return 32;
6332       return 16;
6333     }
6334 
6335   uint64_t off = p->first.dest_ - loc;
6336   unsigned int bytes = 0;
6337   if (p->second.notoc_)
6338     {
6339       if (this->targ_->power10_stubs())
6340 	{
6341 	  Address odd = loc & 4;
6342 	  if (off + (1 << 25) < 2 << 25)
6343 	    bytes = odd + 12;
6344 	  else if (off - odd + (1ULL << 33) < 1ULL << 34)
6345 	    bytes = odd + 16;
6346 	  else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
6347 	    bytes = 28;
6348 	  else
6349 	    bytes = 32;
6350 	  if (!(p->second.toc_ && this->targ_->power10_stubs_auto()))
6351 	    return bytes;
6352 	  p->second.tocoff_ = bytes;
6353 	}
6354       else
6355 	{
6356 	  off -= 8;
6357 	  if (off + 0x8000 < 0x10000)
6358 	    return 24;
6359 	  if (off + 0x80008000ULL < 0x100000000ULL)
6360 	    {
6361 	      if (off + 24 + (1 << 25) < 2 << 25)
6362 		return 28;
6363 	      return 32;
6364 	    }
6365 
6366 	  bytes = 32;
6367 	  if (off + 0x800000000000ULL >= 0x1000000000000ULL
6368 	      && ((off >> 32) & 0xffff) != 0)
6369 	    bytes += 4;
6370 	  if (((off >> 32) & 0xffffffffULL) != 0)
6371 	    bytes += 4;
6372 	  if (hi(off) != 0)
6373 	    bytes += 4;
6374 	  if (l(off) != 0)
6375 	    bytes += 4;
6376 	  return bytes;
6377 	}
6378     }
6379 
6380   off += elfcpp::ppc64_decode_local_entry(p->second.other_);
6381   if (off + (1 << 25) < 2 << 25)
6382     return bytes + 4;
6383   if (!this->targ_->power10_stubs()
6384       || (p->second.toc_ && this->targ_->power10_stubs_auto()))
6385     *need_lt = true;
6386   return bytes + 16;
6387 }
6388 
6389 template<int size, bool big_endian>
6390 void
plt_error(const Plt_stub_key & p)6391 Stub_table<size, big_endian>::plt_error(const Plt_stub_key& p)
6392 {
6393   if (p.sym_)
6394     gold_error(_("linkage table error against `%s'"),
6395 	       p.sym_->demangled_name().c_str());
6396   else
6397     gold_error(_("linkage table error against `%s:[local %u]'"),
6398 	       p.object_->name().c_str(),
6399 	       p.locsym_);
6400 }
6401 
6402 // Write out plt and long branch stub code.
6403 
6404 template<int size, bool big_endian>
6405 void
do_write(Output_file * of)6406 Stub_table<size, big_endian>::do_write(Output_file* of)
6407 {
6408   if (this->plt_call_stubs_.empty()
6409       && this->long_branch_stubs_.empty())
6410     return;
6411 
6412   const section_size_type start_off = this->offset();
6413   const section_size_type off = this->stub_offset();
6414   const section_size_type oview_size =
6415     convert_to_section_size_type(this->data_size() - (off - start_off));
6416   unsigned char* const oview = of->get_output_view(off, oview_size);
6417   unsigned char* p;
6418 
6419   if (size == 64
6420       && this->targ_->power10_stubs())
6421     {
6422       if (!this->plt_call_stubs_.empty())
6423 	{
6424 	  // Write out plt call stubs.
6425 	  typename Plt_stub_entries::const_iterator cs;
6426 	  for (cs = this->plt_call_stubs_.begin();
6427 	       cs != this->plt_call_stubs_.end();
6428 	       ++cs)
6429 	    {
6430 	      p = oview + cs->second.off_;
6431 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6432 	      Address pltoff = this->plt_off(cs, &plt);
6433 	      Address plt_addr = pltoff + plt->address();
6434 	      if (this->targ_->power10_stubs_auto())
6435 		{
6436 		  if (cs->second.notoc_)
6437 		    {
6438 		      if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6439 			this->build_tls_opt_head(&p, false);
6440 		      Address from = this->stub_address() + (p - oview);
6441 		      Address delta = plt_addr - from;
6442 		      p = build_power10_offset<big_endian>(p, delta, from & 4,
6443 							   true);
6444 		      write_insn<big_endian>(p, mtctr_12);
6445 		      p += 4;
6446 		      write_insn<big_endian>(p, bctr);
6447 		      p += 4;
6448 		      p = oview + this->plt_call_align(p - oview);
6449 		    }
6450 		  if (cs->second.toc_)
6451 		    {
6452 		      if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6453 			{
6454 			  bool save_lr
6455 			    = cs->second.r2save_ && !cs->second.localentry0_;
6456 			  this->build_tls_opt_head(&p, save_lr);
6457 			}
6458 		      Address got_addr = this->targ_->toc_pointer();
6459 		      Address off = plt_addr - got_addr;
6460 
6461 		      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6462 			this->plt_error(cs->first);
6463 
6464 		      if (cs->second.r2save_)
6465 			{
6466 			  write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6467 			  p += 4;
6468 			}
6469 		      if (ha(off) != 0)
6470 			{
6471 			  write_insn<big_endian>(p, addis_12_2 + ha(off));
6472 			  p += 4;
6473 			  write_insn<big_endian>(p, ld_12_12 + l(off));
6474 			  p += 4;
6475 			}
6476 		      else
6477 			{
6478 			  write_insn<big_endian>(p, ld_12_2 + l(off));
6479 			  p += 4;
6480 			}
6481 		      write_insn<big_endian>(p, mtctr_12);
6482 		      p += 4;
6483 		      if (cs->second.r2save_
6484 			  && !cs->second.localentry0_
6485 			  && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6486 			this->build_tls_opt_tail(p);
6487 		      else
6488 			write_insn<big_endian>(p, bctr);
6489 		    }
6490 		  if (cs->second.p9notoc_)
6491 		    {
6492 		      if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6493 			this->build_tls_opt_head(&p, false);
6494 		      Address from = this->stub_address() + (p - oview);
6495 		      Address delta = plt_addr - from;
6496 		      p = build_notoc_offset<big_endian>(p, delta, true);
6497 		      write_insn<big_endian>(p, mtctr_12);
6498 		      p += 4;
6499 		      write_insn<big_endian>(p, bctr);
6500 		      p += 4;
6501 		      p = oview + this->plt_call_align(p - oview);
6502 		    }
6503 		}
6504 	      else
6505 		{
6506 		  if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6507 		    {
6508 		      bool save_lr
6509 			= cs->second.r2save_ && !cs->second.localentry0_;
6510 		      this->build_tls_opt_head(&p, save_lr);
6511 		    }
6512 		  if (cs->second.r2save_)
6513 		    {
6514 		      write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6515 		      p += 4;
6516 		    }
6517 		  Address from = this->stub_address() + (p - oview);
6518 		  Address delta = plt_addr - from;
6519 		  p = build_power10_offset<big_endian>(p, delta, from & 4, true);
6520 		  write_insn<big_endian>(p, mtctr_12);
6521 		  p += 4;
6522 		  if (cs->second.r2save_
6523 		      && !cs->second.localentry0_
6524 		      && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6525 		    this->build_tls_opt_tail(p);
6526 		  else
6527 		    write_insn<big_endian>(p, bctr);
6528 		}
6529 	    }
6530 	}
6531 
6532       // Write out long branch stubs.
6533       typename Branch_stub_entries::const_iterator bs;
6534       for (bs = this->long_branch_stubs_.begin();
6535 	   bs != this->long_branch_stubs_.end();
6536 	   ++bs)
6537 	{
6538 	  if (bs->second.save_res_)
6539 	    continue;
6540 	  Address off = this->plt_size_ + bs->second.off_;
6541 	  p = oview + off;
6542 	  Address loc = this->stub_address() + off;
6543 	  Address delta = bs->first.dest_ - loc;
6544 	  if (this->targ_->power10_stubs_auto())
6545 	    {
6546 	      if (bs->second.notoc_)
6547 		{
6548 		  unsigned char* startp = p;
6549 		  p = build_power10_offset<big_endian>(p, delta,
6550 						       loc & 4, false);
6551 		  delta -= p - startp;
6552 		  startp = p;
6553 		  if (delta + (1 << 25) < 2 << 25)
6554 		    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6555 		  else
6556 		    {
6557 		      write_insn<big_endian>(p, mtctr_12);
6558 		      p += 4;
6559 		      write_insn<big_endian>(p, bctr);
6560 		    }
6561 		  p += 4;
6562 		  delta -= p - startp;
6563 		}
6564 	      if (bs->second.toc_)
6565 		{
6566 		  delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6567 		  if (delta + (1 << 25) >= 2 << 25)
6568 		    {
6569 		      Address brlt_addr
6570 			= this->targ_->find_branch_lookup_table(bs->first.dest_);
6571 		      gold_assert(brlt_addr != invalid_address);
6572 		      brlt_addr += this->targ_->brlt_section()->address();
6573 		      Address got_addr = this->targ_->toc_pointer();
6574 		      Address brltoff = brlt_addr - got_addr;
6575 		      if (ha(brltoff) == 0)
6576 			{
6577 			  write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6578 			  p += 4;
6579 			}
6580 		      else
6581 			{
6582 			  write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6583 			  p += 4;
6584 			  write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6585 			  p += 4;
6586 			}
6587 		    }
6588 		  if (delta + (1 << 25) < 2 << 25)
6589 		    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6590 		  else
6591 		    {
6592 		      write_insn<big_endian>(p, mtctr_12);
6593 		      p += 4;
6594 		      write_insn<big_endian>(p, bctr);
6595 		    }
6596 		}
6597 	      if (bs->second.p9notoc_)
6598 		{
6599 		  unsigned char* startp = p;
6600 		  p = build_notoc_offset<big_endian>(p, delta, false);
6601 		  delta -= p - startp;
6602 		  startp = p;
6603 		  if (delta + (1 << 25) < 2 << 25)
6604 		    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6605 		  else
6606 		    {
6607 		      write_insn<big_endian>(p, mtctr_12);
6608 		      p += 4;
6609 		      write_insn<big_endian>(p, bctr);
6610 		    }
6611 		  p += 4;
6612 		  delta -= p - startp;
6613 		}
6614 	    }
6615 	  else
6616 	    {
6617 	      if (!bs->second.notoc_)
6618 		delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6619 	      if (bs->second.notoc_ || delta + (1 << 25) >= 2 << 25)
6620 		{
6621 		  unsigned char* startp = p;
6622 		  p = build_power10_offset<big_endian>(p, delta,
6623 						       loc & 4, false);
6624 		  delta -= p - startp;
6625 		}
6626 	      if (delta + (1 << 25) < 2 << 25)
6627 		write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6628 	      else
6629 		{
6630 		  write_insn<big_endian>(p, mtctr_12);
6631 		  p += 4;
6632 		  write_insn<big_endian>(p, bctr);
6633 		}
6634 	    }
6635 	}
6636     }
6637   else if (size == 64)
6638     {
6639 
6640       if (!this->plt_call_stubs_.empty()
6641 	  && this->targ_->abiversion() >= 2)
6642 	{
6643 	  // Write out plt call stubs for ELFv2.
6644 	  typename Plt_stub_entries::const_iterator cs;
6645 	  for (cs = this->plt_call_stubs_.begin();
6646 	       cs != this->plt_call_stubs_.end();
6647 	       ++cs)
6648 	    {
6649 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6650 	      Address pltoff = this->plt_off(cs, &plt);
6651 	      Address plt_addr = pltoff + plt->address();
6652 
6653 	      p = oview + cs->second.off_;
6654 	      if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6655 		{
6656 		  bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6657 		  this->build_tls_opt_head(&p, save_lr);
6658 		}
6659 	      if (cs->second.r2save_)
6660 		{
6661 		  write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6662 		  p += 4;
6663 		}
6664 	      if (cs->second.p9notoc_)
6665 		{
6666 		  Address from = this->stub_address() + (p - oview) + 8;
6667 		  Address off = plt_addr - from;
6668 		  p = build_notoc_offset<big_endian>(p, off, true);
6669 		}
6670 	      else
6671 		{
6672 		  Address got_addr = this->targ_->toc_pointer();
6673 		  Address off = plt_addr - got_addr;
6674 
6675 		  if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6676 		    this->plt_error(cs->first);
6677 
6678 		  if (ha(off) != 0)
6679 		    {
6680 		      write_insn<big_endian>(p, addis_12_2 + ha(off));
6681 		      p += 4;
6682 		      write_insn<big_endian>(p, ld_12_12 + l(off));
6683 		      p += 4;
6684 		    }
6685 		  else
6686 		    {
6687 		      write_insn<big_endian>(p, ld_12_2 + l(off));
6688 		      p += 4;
6689 		    }
6690 		}
6691 	      write_insn<big_endian>(p, mtctr_12);
6692 	      p += 4;
6693 	      if (cs->second.r2save_
6694 		  && !cs->second.localentry0_
6695 		  && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6696 		this->build_tls_opt_tail(p);
6697 	      else
6698 		write_insn<big_endian>(p, bctr);
6699 	    }
6700 	}
6701       else if (!this->plt_call_stubs_.empty())
6702 	{
6703 	  // Write out plt call stubs for ELFv1.
6704 	  typename Plt_stub_entries::const_iterator cs;
6705 	  for (cs = this->plt_call_stubs_.begin();
6706 	       cs != this->plt_call_stubs_.end();
6707 	       ++cs)
6708 	    {
6709 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6710 	      Address pltoff = this->plt_off(cs, &plt);
6711 	      Address plt_addr = pltoff + plt->address();
6712 	      Address got_addr = this->targ_->toc_pointer();
6713 	      Address off = plt_addr - got_addr;
6714 
6715 	      if (off + 0x80008000 > 0xffffffff || (off & 7) != 0
6716 		  || cs->second.notoc_)
6717 		this->plt_error(cs->first);
6718 
6719 	      bool static_chain = parameters->options().plt_static_chain();
6720 	      bool thread_safe = this->targ_->plt_thread_safe();
6721 	      bool use_fake_dep = false;
6722 	      Address cmp_branch_off = 0;
6723 	      if (thread_safe)
6724 		{
6725 		  unsigned int pltindex
6726 		    = ((pltoff - this->targ_->first_plt_entry_offset())
6727 		       / this->targ_->plt_entry_size());
6728 		  Address glinkoff
6729 		    = (this->targ_->glink_section()->pltresolve_size()
6730 		       + pltindex * 8);
6731 		  if (pltindex > 32768)
6732 		    glinkoff += (pltindex - 32768) * 4;
6733 		  Address to
6734 		    = this->targ_->glink_section()->address() + glinkoff;
6735 		  Address from
6736 		    = (this->stub_address() + cs->second.off_ + 20
6737 		       + 4 * cs->second.r2save_
6738 		       + 4 * (ha(off) != 0)
6739 		       + 4 * (ha(off + 8 + 8 * static_chain) != ha(off))
6740 		       + 4 * static_chain);
6741 		  cmp_branch_off = to - from;
6742 		  use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
6743 		}
6744 
6745 	      p = oview + cs->second.off_;
6746 	      if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6747 		{
6748 		  bool save_lr = cs->second.r2save_ && !cs->second.localentry0_;
6749 		  this->build_tls_opt_head(&p, save_lr);
6750 		  use_fake_dep = thread_safe;
6751 		}
6752 	      if (cs->second.r2save_)
6753 		{
6754 		  write_insn<big_endian>(p, std_2_1 + this->targ_->stk_toc());
6755 		  p += 4;
6756 		}
6757 	      if (ha(off) != 0)
6758 		{
6759 		  write_insn<big_endian>(p, addis_11_2 + ha(off));
6760 		  p += 4;
6761 		  write_insn<big_endian>(p, ld_12_11 + l(off));
6762 		  p += 4;
6763 		  if (ha(off + 8 + 8 * static_chain) != ha(off))
6764 		    {
6765 		      write_insn<big_endian>(p, addi_11_11 + l(off));
6766 		      p += 4;
6767 		      off = 0;
6768 		    }
6769 		  write_insn<big_endian>(p, mtctr_12);
6770 		  p += 4;
6771 		  if (use_fake_dep)
6772 		    {
6773 		      write_insn<big_endian>(p, xor_2_12_12);
6774 		      p += 4;
6775 		      write_insn<big_endian>(p, add_11_11_2);
6776 		      p += 4;
6777 		    }
6778 		  write_insn<big_endian>(p, ld_2_11 + l(off + 8));
6779 		  p += 4;
6780 		  if (static_chain)
6781 		    {
6782 		      write_insn<big_endian>(p, ld_11_11 + l(off + 16));
6783 		      p += 4;
6784 		    }
6785 		}
6786 	      else
6787 		{
6788 		  write_insn<big_endian>(p, ld_12_2 + l(off));
6789 		  p += 4;
6790 		  if (ha(off + 8 + 8 * static_chain) != ha(off))
6791 		    {
6792 		      write_insn<big_endian>(p, addi_2_2 + l(off));
6793 		      p += 4;
6794 		      off = 0;
6795 		    }
6796 		  write_insn<big_endian>(p, mtctr_12);
6797 		  p += 4;
6798 		  if (use_fake_dep)
6799 		    {
6800 		      write_insn<big_endian>(p, xor_11_12_12);
6801 		      p += 4;
6802 		      write_insn<big_endian>(p, add_2_2_11);
6803 		      p += 4;
6804 		    }
6805 		  if (static_chain)
6806 		    {
6807 		      write_insn<big_endian>(p, ld_11_2 + l(off + 16));
6808 		      p += 4;
6809 		    }
6810 		  write_insn<big_endian>(p, ld_2_2 + l(off + 8));
6811 		  p += 4;
6812 		}
6813 	      if (cs->second.r2save_
6814 		  && !cs->second.localentry0_
6815 		  && this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6816 		this->build_tls_opt_tail(p);
6817 	      else if (thread_safe && !use_fake_dep)
6818 		{
6819 		  write_insn<big_endian>(p, cmpldi_2_0);
6820 		  p += 4;
6821 		  write_insn<big_endian>(p, bnectr_p4);
6822 		  p += 4;
6823 		  write_insn<big_endian>(p, b | (cmp_branch_off & 0x3fffffc));
6824 		}
6825 	      else
6826 		write_insn<big_endian>(p, bctr);
6827 	    }
6828 	}
6829 
6830       // Write out long branch stubs.
6831       typename Branch_stub_entries::const_iterator bs;
6832       for (bs = this->long_branch_stubs_.begin();
6833 	   bs != this->long_branch_stubs_.end();
6834 	   ++bs)
6835 	{
6836 	  if (bs->second.save_res_)
6837 	    continue;
6838 	  Address off = this->plt_size_ + bs->second.off_;
6839 	  p = oview + off;
6840 	  Address loc = this->stub_address() + off;
6841 	  Address delta = bs->first.dest_ - loc;
6842 	  if (!bs->second.p9notoc_)
6843 	    delta += elfcpp::ppc64_decode_local_entry(bs->second.other_);
6844 	  if (bs->second.p9notoc_)
6845 	    {
6846 	      unsigned char* startp = p;
6847 	      p = build_notoc_offset<big_endian>(p, off, false);
6848 	      delta -= p - startp;
6849 	    }
6850 	  else if (delta + (1 << 25) >= 2 << 25)
6851 	    {
6852 	      Address brlt_addr
6853 		= this->targ_->find_branch_lookup_table(bs->first.dest_);
6854 	      gold_assert(brlt_addr != invalid_address);
6855 	      brlt_addr += this->targ_->brlt_section()->address();
6856 	      Address got_addr = this->targ_->toc_pointer();
6857 	      Address brltoff = brlt_addr - got_addr;
6858 	      if (ha(brltoff) == 0)
6859 		{
6860 		  write_insn<big_endian>(p, ld_12_2 + l(brltoff));
6861 		  p += 4;
6862 		}
6863 	      else
6864 		{
6865 		  write_insn<big_endian>(p, addis_12_2 + ha(brltoff));
6866 		  p += 4;
6867 		  write_insn<big_endian>(p, ld_12_12 + l(brltoff));
6868 		  p += 4;
6869 		}
6870 	    }
6871 	  if (delta + (1 << 25) < 2 << 25)
6872 	    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6873 	  else
6874 	    {
6875 	      write_insn<big_endian>(p, mtctr_12);
6876 	      p += 4;
6877 	      write_insn<big_endian>(p, bctr);
6878 	    }
6879 	}
6880     }
6881   else // size == 32
6882     {
6883       if (!this->plt_call_stubs_.empty())
6884 	{
6885 	  // The address of _GLOBAL_OFFSET_TABLE_.
6886 	  Address g_o_t = invalid_address;
6887 
6888 	  // Write out plt call stubs.
6889 	  typename Plt_stub_entries::const_iterator cs;
6890 	  for (cs = this->plt_call_stubs_.begin();
6891 	       cs != this->plt_call_stubs_.end();
6892 	       ++cs)
6893 	    {
6894 	      const Output_data_plt_powerpc<size, big_endian>* plt;
6895 	      Address plt_addr = this->plt_off(cs, &plt);
6896 	      plt_addr += plt->address();
6897 
6898 	      p = oview + cs->second.off_;
6899 	      if (this->targ_->is_tls_get_addr_opt(cs->first.sym_))
6900 		this->build_tls_opt_head(&p, false);
6901 	      if (parameters->options().output_is_position_independent())
6902 		{
6903 		  Address got_addr;
6904 		  const Powerpc_relobj<size, big_endian>* ppcobj
6905 		    = (static_cast<const Powerpc_relobj<size, big_endian>*>
6906 		       (cs->first.object_));
6907 		  if (ppcobj != NULL && cs->first.addend_ >= 32768)
6908 		    {
6909 		      unsigned int got2 = ppcobj->got2_shndx();
6910 		      got_addr = ppcobj->get_output_section_offset(got2);
6911 		      gold_assert(got_addr != invalid_address);
6912 		      got_addr += (ppcobj->output_section(got2)->address()
6913 				   + cs->first.addend_);
6914 		    }
6915 		  else
6916 		    {
6917 		      if (g_o_t == invalid_address)
6918 			g_o_t = this->targ_->toc_pointer();
6919 		      got_addr = g_o_t;
6920 		    }
6921 
6922 		  Address off = plt_addr - got_addr;
6923 		  if (ha(off) == 0)
6924 		    write_insn<big_endian>(p, lwz_11_30 + l(off));
6925 		  else
6926 		    {
6927 		      write_insn<big_endian>(p, addis_11_30 + ha(off));
6928 		      p += 4;
6929 		      write_insn<big_endian>(p, lwz_11_11 + l(off));
6930 		    }
6931 		}
6932 	      else
6933 		{
6934 		  write_insn<big_endian>(p, lis_11 + ha(plt_addr));
6935 		  p += 4;
6936 		  write_insn<big_endian>(p, lwz_11_11 + l(plt_addr));
6937 		}
6938 	      p += 4;
6939 	      write_insn<big_endian>(p, mtctr_11);
6940 	      p += 4;
6941 	      write_insn<big_endian>(p, bctr);
6942 	    }
6943 	}
6944 
6945       // Write out long branch stubs.
6946       typename Branch_stub_entries::const_iterator bs;
6947       for (bs = this->long_branch_stubs_.begin();
6948 	   bs != this->long_branch_stubs_.end();
6949 	   ++bs)
6950 	{
6951 	  if (bs->second.save_res_)
6952 	    continue;
6953 	  Address off = this->plt_size_ + bs->second.off_;
6954 	  p = oview + off;
6955 	  Address loc = this->stub_address() + off;
6956 	  Address delta = bs->first.dest_ - loc;
6957 	  if (delta + (1 << 25) < 2 << 25)
6958 	    write_insn<big_endian>(p, b | (delta & 0x3fffffc));
6959 	  else if (!parameters->options().output_is_position_independent())
6960 	    {
6961 	      write_insn<big_endian>(p, lis_12 + ha(bs->first.dest_));
6962 	      p += 4;
6963 	      write_insn<big_endian>(p, addi_12_12 + l(bs->first.dest_));
6964 	    }
6965 	  else
6966 	    {
6967 	      delta -= 8;
6968 	      write_insn<big_endian>(p, mflr_0);
6969 	      p += 4;
6970 	      write_insn<big_endian>(p, bcl_20_31);
6971 	      p += 4;
6972 	      write_insn<big_endian>(p, mflr_12);
6973 	      p += 4;
6974 	      write_insn<big_endian>(p, addis_12_12 + ha(delta));
6975 	      p += 4;
6976 	      write_insn<big_endian>(p, addi_12_12 + l(delta));
6977 	      p += 4;
6978 	      write_insn<big_endian>(p, mtlr_0);
6979 	    }
6980 	  p += 4;
6981 	  write_insn<big_endian>(p, mtctr_12);
6982 	  p += 4;
6983 	  write_insn<big_endian>(p, bctr);
6984 	}
6985     }
6986   if (this->need_save_res_)
6987     {
6988       p = oview + this->plt_size_ + this->branch_size_;
6989       memcpy (p, this->targ_->savres_section()->contents(),
6990 	      this->targ_->savres_section()->data_size());
6991     }
6992 }
6993 
6994 // Write out .glink.
6995 
6996 template<int size, bool big_endian>
6997 void
do_write(Output_file * of)6998 Output_data_glink<size, big_endian>::do_write(Output_file* of)
6999 {
7000   const section_size_type off = this->offset();
7001   const section_size_type oview_size =
7002     convert_to_section_size_type(this->data_size());
7003   unsigned char* const oview = of->get_output_view(off, oview_size);
7004   unsigned char* p;
7005 
7006   // The base address of the .plt section.
7007   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
7008   Address plt_base = this->targ_->plt_section()->address();
7009 
7010   if (size == 64)
7011     {
7012       if (this->end_branch_table_ != 0)
7013 	{
7014 	  // Write pltresolve stub.
7015 	  p = oview;
7016 	  Address after_bcl = this->address() + 16;
7017 	  Address pltoff = plt_base - after_bcl;
7018 
7019 	  elfcpp::Swap<64, big_endian>::writeval(p, pltoff),	p += 8;
7020 
7021 	  if (this->targ_->abiversion() < 2)
7022 	    {
7023 	      write_insn<big_endian>(p, mflr_12),		p += 4;
7024 	      write_insn<big_endian>(p, bcl_20_31),		p += 4;
7025 	      write_insn<big_endian>(p, mflr_11),		p += 4;
7026 	      write_insn<big_endian>(p, ld_2_11 + l(-16)),	p += 4;
7027 	      write_insn<big_endian>(p, mtlr_12),		p += 4;
7028 	      write_insn<big_endian>(p, add_11_2_11),		p += 4;
7029 	      write_insn<big_endian>(p, ld_12_11 + 0),		p += 4;
7030 	      write_insn<big_endian>(p, ld_2_11 + 8),		p += 4;
7031 	      write_insn<big_endian>(p, mtctr_12),		p += 4;
7032 	      write_insn<big_endian>(p, ld_11_11 + 16),		p += 4;
7033 	    }
7034 	  else
7035 	    {
7036 	      if (this->targ_->has_localentry0())
7037 		{
7038 		  write_insn<big_endian>(p, std_2_1 + 24),	p += 4;
7039 		}
7040 	      write_insn<big_endian>(p, mflr_0),		p += 4;
7041 	      write_insn<big_endian>(p, bcl_20_31),		p += 4;
7042 	      write_insn<big_endian>(p, mflr_11),		p += 4;
7043 	      write_insn<big_endian>(p, mtlr_0),		p += 4;
7044 	      if (this->targ_->has_localentry0())
7045 		{
7046 		  write_insn<big_endian>(p, ld_0_11 + l(-20)),	p += 4;
7047 		}
7048 	      else
7049 		{
7050 		  write_insn<big_endian>(p, ld_0_11 + l(-16)),	p += 4;
7051 		}
7052 	      write_insn<big_endian>(p, sub_12_12_11),		p += 4;
7053 	      write_insn<big_endian>(p, add_11_0_11),		p += 4;
7054 	      write_insn<big_endian>(p, addi_0_12 + l(-44)),	p += 4;
7055 	      write_insn<big_endian>(p, ld_12_11 + 0),		p += 4;
7056 	      write_insn<big_endian>(p, srdi_0_0_2),		p += 4;
7057 	      write_insn<big_endian>(p, mtctr_12),		p += 4;
7058 	      write_insn<big_endian>(p, ld_11_11 + 8),		p += 4;
7059 	    }
7060 	  write_insn<big_endian>(p, bctr),			p += 4;
7061 	  gold_assert(p == oview + this->pltresolve_size());
7062 
7063 	  // Write lazy link call stubs.
7064 	  uint32_t indx = 0;
7065 	  while (p < oview + this->end_branch_table_)
7066 	    {
7067 	      if (this->targ_->abiversion() < 2)
7068 		{
7069 		  if (indx < 0x8000)
7070 		    {
7071 		      write_insn<big_endian>(p, li_0_0 + indx),		p += 4;
7072 		    }
7073 		  else
7074 		    {
7075 		      write_insn<big_endian>(p, lis_0 + hi(indx)),	p += 4;
7076 		      write_insn<big_endian>(p, ori_0_0_0 + l(indx)),	p += 4;
7077 		    }
7078 		}
7079 	      uint32_t branch_off = 8 - (p - oview);
7080 	      write_insn<big_endian>(p, b + (branch_off & 0x3fffffc)),	p += 4;
7081 	      indx++;
7082 	    }
7083 	}
7084 
7085       Address plt_base = this->targ_->plt_section()->address();
7086       Address iplt_base = invalid_address;
7087       unsigned int global_entry_off = this->global_entry_off();
7088       Address global_entry_base = this->address() + global_entry_off;
7089       typename Global_entry_stub_entries::const_iterator ge;
7090       for (ge = this->global_entry_stubs_.begin();
7091 	   ge != this->global_entry_stubs_.end();
7092 	   ++ge)
7093 	{
7094 	  p = oview + global_entry_off + ge->second;
7095 	  Address plt_addr = ge->first->plt_offset();
7096 	  if (ge->first->type() == elfcpp::STT_GNU_IFUNC
7097 	      && ge->first->can_use_relative_reloc(false))
7098 	    {
7099 	      if (iplt_base == invalid_address)
7100 		iplt_base = this->targ_->iplt_section()->address();
7101 	      plt_addr += iplt_base;
7102 	    }
7103 	  else
7104 	    plt_addr += plt_base;
7105 	  Address my_addr = global_entry_base + ge->second;
7106 	  Address off = plt_addr - my_addr;
7107 
7108 	  if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
7109 	    gold_error(_("linkage table error against `%s'"),
7110 		       ge->first->demangled_name().c_str());
7111 
7112 	  write_insn<big_endian>(p, addis_12_12 + ha(off)),	p += 4;
7113 	  write_insn<big_endian>(p, ld_12_12 + l(off)),		p += 4;
7114 	  write_insn<big_endian>(p, mtctr_12),			p += 4;
7115 	  write_insn<big_endian>(p, bctr);
7116 	}
7117     }
7118   else
7119     {
7120       // The address of _GLOBAL_OFFSET_TABLE_.
7121       Address g_o_t = this->targ_->toc_pointer();
7122 
7123       // Write out pltresolve branch table.
7124       p = oview;
7125       unsigned int the_end = oview_size - this->pltresolve_size();
7126       unsigned char* end_p = oview + the_end;
7127       while (p < end_p - 8 * 4)
7128 	write_insn<big_endian>(p, b + end_p - p), p += 4;
7129       while (p < end_p)
7130 	write_insn<big_endian>(p, nop), p += 4;
7131 
7132       // Write out pltresolve call stub.
7133       end_p = oview + oview_size;
7134       if (parameters->options().output_is_position_independent())
7135 	{
7136 	  Address res0_off = 0;
7137 	  Address after_bcl_off = the_end + 12;
7138 	  Address bcl_res0 = after_bcl_off - res0_off;
7139 
7140 	  write_insn<big_endian>(p, addis_11_11 + ha(bcl_res0));
7141 	  p += 4;
7142 	  write_insn<big_endian>(p, mflr_0);
7143 	  p += 4;
7144 	  write_insn<big_endian>(p, bcl_20_31);
7145 	  p += 4;
7146 	  write_insn<big_endian>(p, addi_11_11 + l(bcl_res0));
7147 	  p += 4;
7148 	  write_insn<big_endian>(p, mflr_12);
7149 	  p += 4;
7150 	  write_insn<big_endian>(p, mtlr_0);
7151 	  p += 4;
7152 	  write_insn<big_endian>(p, sub_11_11_12);
7153 	  p += 4;
7154 
7155 	  Address got_bcl = g_o_t + 4 - (after_bcl_off + this->address());
7156 
7157 	  write_insn<big_endian>(p, addis_12_12 + ha(got_bcl));
7158 	  p += 4;
7159 	  if (ha(got_bcl) == ha(got_bcl + 4))
7160 	    {
7161 	      write_insn<big_endian>(p, lwz_0_12 + l(got_bcl));
7162 	      p += 4;
7163 	      write_insn<big_endian>(p, lwz_12_12 + l(got_bcl + 4));
7164 	    }
7165 	  else
7166 	    {
7167 	      write_insn<big_endian>(p, lwzu_0_12 + l(got_bcl));
7168 	      p += 4;
7169 	      write_insn<big_endian>(p, lwz_12_12 + 4);
7170 	    }
7171 	  p += 4;
7172 	  write_insn<big_endian>(p, mtctr_0);
7173 	  p += 4;
7174 	  write_insn<big_endian>(p, add_0_11_11);
7175 	  p += 4;
7176 	  write_insn<big_endian>(p, add_11_0_11);
7177 	}
7178       else
7179 	{
7180 	  Address res0 = this->address();
7181 
7182 	  write_insn<big_endian>(p, lis_12 + ha(g_o_t + 4));
7183 	  p += 4;
7184 	  write_insn<big_endian>(p, addis_11_11 + ha(-res0));
7185 	  p += 4;
7186 	  if (ha(g_o_t + 4) == ha(g_o_t + 8))
7187 	    write_insn<big_endian>(p, lwz_0_12 + l(g_o_t + 4));
7188 	  else
7189 	    write_insn<big_endian>(p, lwzu_0_12 + l(g_o_t + 4));
7190 	  p += 4;
7191 	  write_insn<big_endian>(p, addi_11_11 + l(-res0));
7192 	  p += 4;
7193 	  write_insn<big_endian>(p, mtctr_0);
7194 	  p += 4;
7195 	  write_insn<big_endian>(p, add_0_11_11);
7196 	  p += 4;
7197 	  if (ha(g_o_t + 4) == ha(g_o_t + 8))
7198 	    write_insn<big_endian>(p, lwz_12_12 + l(g_o_t + 8));
7199 	  else
7200 	    write_insn<big_endian>(p, lwz_12_12 + 4);
7201 	  p += 4;
7202 	  write_insn<big_endian>(p, add_11_0_11);
7203 	}
7204       p += 4;
7205       write_insn<big_endian>(p, bctr);
7206       p += 4;
7207       while (p < end_p)
7208 	{
7209 	  write_insn<big_endian>(p, nop);
7210 	  p += 4;
7211 	}
7212     }
7213 
7214   of->write_output_view(off, oview_size, oview);
7215 }
7216 
7217 
7218 // A class to handle linker generated save/restore functions.
7219 
7220 template<int size, bool big_endian>
7221 class Output_data_save_res : public Output_section_data_build
7222 {
7223  public:
7224   Output_data_save_res(Symbol_table* symtab);
7225 
7226   const unsigned char*
contents() const7227   contents() const
7228   {
7229     return contents_;
7230   }
7231 
7232  protected:
7233   // Write to a map file.
7234   void
do_print_to_mapfile(Mapfile * mapfile) const7235   do_print_to_mapfile(Mapfile* mapfile) const
7236   { mapfile->print_output_data(this, _("** save/restore")); }
7237 
7238   void
7239   do_write(Output_file*);
7240 
7241  private:
7242   // The maximum size of save/restore contents.
7243   static const unsigned int savres_max = 218*4;
7244 
7245   void
7246   savres_define(Symbol_table* symtab,
7247 		const char *name,
7248 		unsigned int lo, unsigned int hi,
7249 		unsigned char* write_ent(unsigned char*, int),
7250 		unsigned char* write_tail(unsigned char*, int));
7251 
7252   unsigned char *contents_;
7253 };
7254 
7255 template<bool big_endian>
7256 static unsigned char*
savegpr0(unsigned char * p,int r)7257 savegpr0(unsigned char* p, int r)
7258 {
7259   uint32_t insn = std_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7260   write_insn<big_endian>(p, insn);
7261   return p + 4;
7262 }
7263 
7264 template<bool big_endian>
7265 static unsigned char*
savegpr0_tail(unsigned char * p,int r)7266 savegpr0_tail(unsigned char* p, int r)
7267 {
7268   p = savegpr0<big_endian>(p, r);
7269   uint32_t insn = std_0_1 + 16;
7270   write_insn<big_endian>(p, insn);
7271   p = p + 4;
7272   write_insn<big_endian>(p, blr);
7273   return p + 4;
7274 }
7275 
7276 template<bool big_endian>
7277 static unsigned char*
restgpr0(unsigned char * p,int r)7278 restgpr0(unsigned char* p, int r)
7279 {
7280   uint32_t insn = ld_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7281   write_insn<big_endian>(p, insn);
7282   return p + 4;
7283 }
7284 
7285 template<bool big_endian>
7286 static unsigned char*
restgpr0_tail(unsigned char * p,int r)7287 restgpr0_tail(unsigned char* p, int r)
7288 {
7289   uint32_t insn = ld_0_1 + 16;
7290   write_insn<big_endian>(p, insn);
7291   p = p + 4;
7292   p = restgpr0<big_endian>(p, r);
7293   write_insn<big_endian>(p, mtlr_0);
7294   p = p + 4;
7295   if (r == 29)
7296     {
7297       p = restgpr0<big_endian>(p, 30);
7298       p = restgpr0<big_endian>(p, 31);
7299     }
7300   write_insn<big_endian>(p, blr);
7301   return p + 4;
7302 }
7303 
7304 template<bool big_endian>
7305 static unsigned char*
savegpr1(unsigned char * p,int r)7306 savegpr1(unsigned char* p, int r)
7307 {
7308   uint32_t insn = std_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7309   write_insn<big_endian>(p, insn);
7310   return p + 4;
7311 }
7312 
7313 template<bool big_endian>
7314 static unsigned char*
savegpr1_tail(unsigned char * p,int r)7315 savegpr1_tail(unsigned char* p, int r)
7316 {
7317   p = savegpr1<big_endian>(p, r);
7318   write_insn<big_endian>(p, blr);
7319   return p + 4;
7320 }
7321 
7322 template<bool big_endian>
7323 static unsigned char*
restgpr1(unsigned char * p,int r)7324 restgpr1(unsigned char* p, int r)
7325 {
7326   uint32_t insn = ld_0_12 + (r << 21) + (1 << 16) - (32 - r) * 8;
7327   write_insn<big_endian>(p, insn);
7328   return p + 4;
7329 }
7330 
7331 template<bool big_endian>
7332 static unsigned char*
restgpr1_tail(unsigned char * p,int r)7333 restgpr1_tail(unsigned char* p, int r)
7334 {
7335   p = restgpr1<big_endian>(p, r);
7336   write_insn<big_endian>(p, blr);
7337   return p + 4;
7338 }
7339 
7340 template<bool big_endian>
7341 static unsigned char*
savefpr(unsigned char * p,int r)7342 savefpr(unsigned char* p, int r)
7343 {
7344   uint32_t insn = stfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7345   write_insn<big_endian>(p, insn);
7346   return p + 4;
7347 }
7348 
7349 template<bool big_endian>
7350 static unsigned char*
savefpr0_tail(unsigned char * p,int r)7351 savefpr0_tail(unsigned char* p, int r)
7352 {
7353   p = savefpr<big_endian>(p, r);
7354   write_insn<big_endian>(p, std_0_1 + 16);
7355   p = p + 4;
7356   write_insn<big_endian>(p, blr);
7357   return p + 4;
7358 }
7359 
7360 template<bool big_endian>
7361 static unsigned char*
restfpr(unsigned char * p,int r)7362 restfpr(unsigned char* p, int r)
7363 {
7364   uint32_t insn = lfd_0_1 + (r << 21) + (1 << 16) - (32 - r) * 8;
7365   write_insn<big_endian>(p, insn);
7366   return p + 4;
7367 }
7368 
7369 template<bool big_endian>
7370 static unsigned char*
restfpr0_tail(unsigned char * p,int r)7371 restfpr0_tail(unsigned char* p, int r)
7372 {
7373   write_insn<big_endian>(p, ld_0_1 + 16);
7374   p = p + 4;
7375   p = restfpr<big_endian>(p, r);
7376   write_insn<big_endian>(p, mtlr_0);
7377   p = p + 4;
7378   if (r == 29)
7379     {
7380       p = restfpr<big_endian>(p, 30);
7381       p = restfpr<big_endian>(p, 31);
7382     }
7383   write_insn<big_endian>(p, blr);
7384   return p + 4;
7385 }
7386 
7387 template<bool big_endian>
7388 static unsigned char*
savefpr1_tail(unsigned char * p,int r)7389 savefpr1_tail(unsigned char* p, int r)
7390 {
7391   p = savefpr<big_endian>(p, r);
7392   write_insn<big_endian>(p, blr);
7393   return p + 4;
7394 }
7395 
7396 template<bool big_endian>
7397 static unsigned char*
restfpr1_tail(unsigned char * p,int r)7398 restfpr1_tail(unsigned char* p, int r)
7399 {
7400   p = restfpr<big_endian>(p, r);
7401   write_insn<big_endian>(p, blr);
7402   return p + 4;
7403 }
7404 
7405 template<bool big_endian>
7406 static unsigned char*
savevr(unsigned char * p,int r)7407 savevr(unsigned char* p, int r)
7408 {
7409   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7410   write_insn<big_endian>(p, insn);
7411   p = p + 4;
7412   insn = stvx_0_12_0 + (r << 21);
7413   write_insn<big_endian>(p, insn);
7414   return p + 4;
7415 }
7416 
7417 template<bool big_endian>
7418 static unsigned char*
savevr_tail(unsigned char * p,int r)7419 savevr_tail(unsigned char* p, int r)
7420 {
7421   p = savevr<big_endian>(p, r);
7422   write_insn<big_endian>(p, blr);
7423   return p + 4;
7424 }
7425 
7426 template<bool big_endian>
7427 static unsigned char*
restvr(unsigned char * p,int r)7428 restvr(unsigned char* p, int r)
7429 {
7430   uint32_t insn = li_12_0 + (1 << 16) - (32 - r) * 16;
7431   write_insn<big_endian>(p, insn);
7432   p = p + 4;
7433   insn = lvx_0_12_0 + (r << 21);
7434   write_insn<big_endian>(p, insn);
7435   return p + 4;
7436 }
7437 
7438 template<bool big_endian>
7439 static unsigned char*
restvr_tail(unsigned char * p,int r)7440 restvr_tail(unsigned char* p, int r)
7441 {
7442   p = restvr<big_endian>(p, r);
7443   write_insn<big_endian>(p, blr);
7444   return p + 4;
7445 }
7446 
7447 
7448 template<int size, bool big_endian>
Output_data_save_res(Symbol_table * symtab)7449 Output_data_save_res<size, big_endian>::Output_data_save_res(
7450     Symbol_table* symtab)
7451   : Output_section_data_build(4),
7452     contents_(NULL)
7453 {
7454   this->savres_define(symtab,
7455 		      "_savegpr0_", 14, 31,
7456 		      savegpr0<big_endian>, savegpr0_tail<big_endian>);
7457   this->savres_define(symtab,
7458 		      "_restgpr0_", 14, 29,
7459 		      restgpr0<big_endian>, restgpr0_tail<big_endian>);
7460   this->savres_define(symtab,
7461 		      "_restgpr0_", 30, 31,
7462 		      restgpr0<big_endian>, restgpr0_tail<big_endian>);
7463   this->savres_define(symtab,
7464 		      "_savegpr1_", 14, 31,
7465 		      savegpr1<big_endian>, savegpr1_tail<big_endian>);
7466   this->savres_define(symtab,
7467 		      "_restgpr1_", 14, 31,
7468 		      restgpr1<big_endian>, restgpr1_tail<big_endian>);
7469   this->savres_define(symtab,
7470 		      "_savefpr_", 14, 31,
7471 		      savefpr<big_endian>, savefpr0_tail<big_endian>);
7472   this->savres_define(symtab,
7473 		      "_restfpr_", 14, 29,
7474 		      restfpr<big_endian>, restfpr0_tail<big_endian>);
7475   this->savres_define(symtab,
7476 		      "_restfpr_", 30, 31,
7477 		      restfpr<big_endian>, restfpr0_tail<big_endian>);
7478   this->savres_define(symtab,
7479 		      "._savef", 14, 31,
7480 		      savefpr<big_endian>, savefpr1_tail<big_endian>);
7481   this->savres_define(symtab,
7482 		      "._restf", 14, 31,
7483 		      restfpr<big_endian>, restfpr1_tail<big_endian>);
7484   this->savres_define(symtab,
7485 		      "_savevr_", 20, 31,
7486 		      savevr<big_endian>, savevr_tail<big_endian>);
7487   this->savres_define(symtab,
7488 		      "_restvr_", 20, 31,
7489 		      restvr<big_endian>, restvr_tail<big_endian>);
7490 }
7491 
7492 template<int size, bool big_endian>
7493 void
savres_define(Symbol_table * symtab,const char * name,unsigned int lo,unsigned int hi,unsigned char * write_ent (unsigned char *,int),unsigned char * write_tail (unsigned char *,int))7494 Output_data_save_res<size, big_endian>::savres_define(
7495     Symbol_table* symtab,
7496     const char *name,
7497     unsigned int lo, unsigned int hi,
7498     unsigned char* write_ent(unsigned char*, int),
7499     unsigned char* write_tail(unsigned char*, int))
7500 {
7501   size_t len = strlen(name);
7502   bool writing = false;
7503   char sym[16];
7504 
7505   memcpy(sym, name, len);
7506   sym[len + 2] = 0;
7507 
7508   for (unsigned int i = lo; i <= hi; i++)
7509     {
7510       sym[len + 0] = i / 10 + '0';
7511       sym[len + 1] = i % 10 + '0';
7512       Symbol* gsym = symtab->lookup(sym);
7513       bool refd = gsym != NULL && gsym->is_undefined();
7514       writing = writing || refd;
7515       if (writing)
7516 	{
7517 	  if (this->contents_ == NULL)
7518 	    this->contents_ = new unsigned char[this->savres_max];
7519 
7520 	  section_size_type value = this->current_data_size();
7521 	  unsigned char* p = this->contents_ + value;
7522 	  if (i != hi)
7523 	    p = write_ent(p, i);
7524 	  else
7525 	    p = write_tail(p, i);
7526 	  section_size_type cur_size = p - this->contents_;
7527 	  this->set_current_data_size(cur_size);
7528 	  if (refd)
7529 	    symtab->define_in_output_data(sym, NULL, Symbol_table::PREDEFINED,
7530 					  this, value, cur_size - value,
7531 					  elfcpp::STT_FUNC, elfcpp::STB_GLOBAL,
7532 					  elfcpp::STV_HIDDEN, 0, false, false);
7533 	}
7534     }
7535 }
7536 
7537 // Write out save/restore.
7538 
7539 template<int size, bool big_endian>
7540 void
do_write(Output_file * of)7541 Output_data_save_res<size, big_endian>::do_write(Output_file* of)
7542 {
7543   const section_size_type off = this->offset();
7544   const section_size_type oview_size =
7545     convert_to_section_size_type(this->data_size());
7546   unsigned char* const oview = of->get_output_view(off, oview_size);
7547   memcpy(oview, this->contents_, oview_size);
7548   of->write_output_view(off, oview_size, oview);
7549 }
7550 
7551 
7552 // Create the glink section.
7553 
7554 template<int size, bool big_endian>
7555 void
make_glink_section(Layout * layout)7556 Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
7557 {
7558   if (this->glink_ == NULL)
7559     {
7560       this->glink_ = new Output_data_glink<size, big_endian>(this);
7561       this->glink_->add_eh_frame(layout);
7562       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
7563 				      elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
7564 				      this->glink_, ORDER_TEXT, false);
7565     }
7566 }
7567 
7568 // Create a PLT entry for a global symbol.
7569 
7570 template<int size, bool big_endian>
7571 void
make_plt_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)7572 Target_powerpc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
7573 						 Layout* layout,
7574 						 Symbol* gsym)
7575 {
7576   if (gsym->type() == elfcpp::STT_GNU_IFUNC
7577       && gsym->can_use_relative_reloc(false))
7578     {
7579       if (this->iplt_ == NULL)
7580 	this->make_iplt_section(symtab, layout);
7581       this->iplt_->add_ifunc_entry(gsym);
7582     }
7583   else
7584     {
7585       if (this->plt_ == NULL)
7586 	this->make_plt_section(symtab, layout);
7587       this->plt_->add_entry(gsym);
7588     }
7589 }
7590 
7591 // Make a PLT entry for a local symbol.
7592 
7593 template<int size, bool big_endian>
7594 void
make_local_plt_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * relobj,unsigned int r_sym)7595 Target_powerpc<size, big_endian>::make_local_plt_entry(
7596     Symbol_table* symtab,
7597     Layout* layout,
7598     Sized_relobj_file<size, big_endian>* relobj,
7599     unsigned int r_sym)
7600 {
7601   if (this->lplt_ == NULL)
7602     this->make_lplt_section(symtab, layout);
7603   this->lplt_->add_local_entry(relobj, r_sym);
7604 }
7605 
7606 template<int size, bool big_endian>
7607 void
make_local_plt_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)7608 Target_powerpc<size, big_endian>::make_local_plt_entry(Symbol_table* symtab,
7609 						       Layout* layout,
7610 						       Symbol* gsym)
7611 {
7612   if (this->lplt_ == NULL)
7613     this->make_lplt_section(symtab, layout);
7614   this->lplt_->add_entry(gsym, true);
7615 }
7616 
7617 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
7618 
7619 template<int size, bool big_endian>
7620 void
make_local_ifunc_plt_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * relobj,unsigned int r_sym)7621 Target_powerpc<size, big_endian>::make_local_ifunc_plt_entry(
7622     Symbol_table* symtab,
7623     Layout* layout,
7624     Sized_relobj_file<size, big_endian>* relobj,
7625     unsigned int r_sym)
7626 {
7627   if (this->iplt_ == NULL)
7628     this->make_iplt_section(symtab, layout);
7629   this->iplt_->add_local_ifunc_entry(relobj, r_sym);
7630 }
7631 
7632 // Return the number of entries in the PLT.
7633 
7634 template<int size, bool big_endian>
7635 unsigned int
plt_entry_count() const7636 Target_powerpc<size, big_endian>::plt_entry_count() const
7637 {
7638   if (this->plt_ == NULL)
7639     return 0;
7640   return this->plt_->entry_count();
7641 }
7642 
7643 // Create a GOT entry for local dynamic __tls_get_addr calls.
7644 
7645 template<int size, bool big_endian>
7646 unsigned int
tlsld_got_offset(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object)7647 Target_powerpc<size, big_endian>::tlsld_got_offset(
7648     Symbol_table* symtab,
7649     Layout* layout,
7650     Sized_relobj_file<size, big_endian>* object)
7651 {
7652   if (this->tlsld_got_offset_ == -1U)
7653     {
7654       gold_assert(symtab != NULL && layout != NULL && object != NULL);
7655       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
7656       Output_data_got_powerpc<size, big_endian>* got
7657 	= this->got_section(symtab, layout, GOT_TYPE_SMALL);
7658       unsigned int got_offset = got->add_constant_pair(0, 0);
7659       rela_dyn->add_local(object, 0, elfcpp::R_POWERPC_DTPMOD, got,
7660 			  got_offset, 0);
7661       this->tlsld_got_offset_ = got_offset;
7662     }
7663   return this->tlsld_got_offset_;
7664 }
7665 
7666 // Get the Reference_flags for a particular relocation.
7667 
7668 template<int size, bool big_endian>
7669 int
get_reference_flags(unsigned int r_type,const Target_powerpc * target)7670 Target_powerpc<size, big_endian>::Scan::get_reference_flags(
7671     unsigned int r_type,
7672     const Target_powerpc* target)
7673 {
7674   int ref = 0;
7675 
7676   switch (r_type)
7677     {
7678     case elfcpp::R_POWERPC_NONE:
7679     case elfcpp::R_POWERPC_GNU_VTINHERIT:
7680     case elfcpp::R_POWERPC_GNU_VTENTRY:
7681     case elfcpp::R_PPC64_TOC:
7682       // No symbol reference.
7683       break;
7684 
7685     case elfcpp::R_PPC64_ADDR64:
7686     case elfcpp::R_PPC64_UADDR64:
7687     case elfcpp::R_POWERPC_ADDR32:
7688     case elfcpp::R_POWERPC_UADDR32:
7689     case elfcpp::R_POWERPC_ADDR16:
7690     case elfcpp::R_POWERPC_UADDR16:
7691     case elfcpp::R_POWERPC_ADDR16_LO:
7692     case elfcpp::R_POWERPC_ADDR16_HI:
7693     case elfcpp::R_POWERPC_ADDR16_HA:
7694     case elfcpp::R_PPC64_ADDR16_HIGHER34:
7695     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
7696     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
7697     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
7698     case elfcpp::R_PPC64_D34:
7699     case elfcpp::R_PPC64_D34_LO:
7700     case elfcpp::R_PPC64_D34_HI30:
7701     case elfcpp::R_PPC64_D34_HA30:
7702     case elfcpp::R_PPC64_D28:
7703       ref = Symbol::ABSOLUTE_REF;
7704       break;
7705 
7706     case elfcpp::R_POWERPC_ADDR24:
7707     case elfcpp::R_POWERPC_ADDR14:
7708     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7709     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7710       ref = Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
7711       break;
7712 
7713     case elfcpp::R_PPC64_REL64:
7714     case elfcpp::R_POWERPC_REL32:
7715     case elfcpp::R_PPC_LOCAL24PC:
7716     case elfcpp::R_POWERPC_REL16:
7717     case elfcpp::R_POWERPC_REL16_LO:
7718     case elfcpp::R_POWERPC_REL16_HI:
7719     case elfcpp::R_POWERPC_REL16_HA:
7720     case elfcpp::R_PPC64_REL16_HIGH:
7721     case elfcpp::R_PPC64_REL16_HIGHA:
7722     case elfcpp::R_PPC64_REL16_HIGHER:
7723     case elfcpp::R_PPC64_REL16_HIGHERA:
7724     case elfcpp::R_PPC64_REL16_HIGHEST:
7725     case elfcpp::R_PPC64_REL16_HIGHESTA:
7726     case elfcpp::R_PPC64_PCREL34:
7727     case elfcpp::R_PPC64_REL16_HIGHER34:
7728     case elfcpp::R_PPC64_REL16_HIGHERA34:
7729     case elfcpp::R_PPC64_REL16_HIGHEST34:
7730     case elfcpp::R_PPC64_REL16_HIGHESTA34:
7731     case elfcpp::R_PPC64_PCREL28:
7732       ref = Symbol::RELATIVE_REF;
7733       break;
7734 
7735     case elfcpp::R_PPC64_REL24_NOTOC:
7736       if (size == 32)
7737 	break;
7738       // Fall through.
7739     case elfcpp::R_PPC64_REL24_P9NOTOC:
7740     case elfcpp::R_POWERPC_REL24:
7741     case elfcpp::R_PPC_PLTREL24:
7742     case elfcpp::R_POWERPC_REL14:
7743     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7744     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7745     case elfcpp::R_POWERPC_PLT16_LO:
7746     case elfcpp::R_POWERPC_PLT16_HI:
7747     case elfcpp::R_POWERPC_PLT16_HA:
7748     case elfcpp::R_PPC64_PLT16_LO_DS:
7749     case elfcpp::R_POWERPC_PLTSEQ:
7750     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7751     case elfcpp::R_POWERPC_PLTCALL:
7752     case elfcpp::R_PPC64_PLTCALL_NOTOC:
7753     case elfcpp::R_PPC64_PLT_PCREL34:
7754     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7755       ref = Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
7756       break;
7757 
7758     case elfcpp::R_POWERPC_GOT16:
7759     case elfcpp::R_POWERPC_GOT16_LO:
7760     case elfcpp::R_POWERPC_GOT16_HI:
7761     case elfcpp::R_POWERPC_GOT16_HA:
7762     case elfcpp::R_PPC64_GOT16_DS:
7763     case elfcpp::R_PPC64_GOT16_LO_DS:
7764     case elfcpp::R_PPC64_GOT_PCREL34:
7765     case elfcpp::R_PPC64_TOC16:
7766     case elfcpp::R_PPC64_TOC16_LO:
7767     case elfcpp::R_PPC64_TOC16_HI:
7768     case elfcpp::R_PPC64_TOC16_HA:
7769     case elfcpp::R_PPC64_TOC16_DS:
7770     case elfcpp::R_PPC64_TOC16_LO_DS:
7771       ref = Symbol::RELATIVE_REF;
7772       break;
7773 
7774     case elfcpp::R_POWERPC_GOT_TPREL16:
7775     case elfcpp::R_POWERPC_TLS:
7776     case elfcpp::R_PPC64_TLSGD:
7777     case elfcpp::R_PPC64_TLSLD:
7778     case elfcpp::R_PPC64_TPREL34:
7779     case elfcpp::R_PPC64_DTPREL34:
7780     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
7781     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
7782     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
7783     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
7784       ref = Symbol::TLS_REF;
7785       break;
7786 
7787     case elfcpp::R_POWERPC_COPY:
7788     case elfcpp::R_POWERPC_GLOB_DAT:
7789     case elfcpp::R_POWERPC_JMP_SLOT:
7790     case elfcpp::R_POWERPC_RELATIVE:
7791     case elfcpp::R_POWERPC_DTPMOD:
7792     default:
7793       // Not expected.  We will give an error later.
7794       break;
7795     }
7796 
7797   if (size == 64 && target->abiversion() < 2)
7798     ref |= Symbol::FUNC_DESC_ABI;
7799   return ref;
7800 }
7801 
7802 // Report an unsupported relocation against a local symbol.
7803 
7804 template<int size, bool big_endian>
7805 void
unsupported_reloc_local(Sized_relobj_file<size,big_endian> * object,unsigned int r_type)7806 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_local(
7807     Sized_relobj_file<size, big_endian>* object,
7808     unsigned int r_type)
7809 {
7810   gold_error(_("%s: unsupported reloc %u against local symbol"),
7811 	     object->name().c_str(), r_type);
7812 }
7813 
7814 // We are about to emit a dynamic relocation of type R_TYPE.  If the
7815 // dynamic linker does not support it, issue an error.
7816 
7817 template<int size, bool big_endian>
7818 void
check_non_pic(Relobj * object,unsigned int r_type)7819 Target_powerpc<size, big_endian>::Scan::check_non_pic(Relobj* object,
7820 						      unsigned int r_type)
7821 {
7822   gold_assert(r_type != elfcpp::R_POWERPC_NONE);
7823 
7824   // These are the relocation types supported by glibc for both 32-bit
7825   // and 64-bit powerpc.
7826   switch (r_type)
7827     {
7828     case elfcpp::R_POWERPC_NONE:
7829     case elfcpp::R_POWERPC_RELATIVE:
7830     case elfcpp::R_POWERPC_GLOB_DAT:
7831     case elfcpp::R_POWERPC_DTPMOD:
7832     case elfcpp::R_POWERPC_DTPREL:
7833     case elfcpp::R_POWERPC_TPREL:
7834     case elfcpp::R_POWERPC_JMP_SLOT:
7835     case elfcpp::R_POWERPC_COPY:
7836     case elfcpp::R_POWERPC_IRELATIVE:
7837     case elfcpp::R_POWERPC_ADDR32:
7838     case elfcpp::R_POWERPC_UADDR32:
7839     case elfcpp::R_POWERPC_ADDR24:
7840     case elfcpp::R_POWERPC_ADDR16:
7841     case elfcpp::R_POWERPC_UADDR16:
7842     case elfcpp::R_POWERPC_ADDR16_LO:
7843     case elfcpp::R_POWERPC_ADDR16_HI:
7844     case elfcpp::R_POWERPC_ADDR16_HA:
7845     case elfcpp::R_POWERPC_ADDR14:
7846     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7847     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7848     case elfcpp::R_POWERPC_REL32:
7849     case elfcpp::R_POWERPC_TPREL16:
7850     case elfcpp::R_POWERPC_TPREL16_LO:
7851     case elfcpp::R_POWERPC_TPREL16_HI:
7852     case elfcpp::R_POWERPC_TPREL16_HA:
7853       return;
7854 
7855     default:
7856       break;
7857     }
7858 
7859   if (size == 64)
7860     {
7861       switch (r_type)
7862 	{
7863 	  // These are the relocation types supported only on 64-bit.
7864 	case elfcpp::R_PPC64_ADDR64:
7865 	case elfcpp::R_PPC64_UADDR64:
7866 	case elfcpp::R_PPC64_JMP_IREL:
7867 	case elfcpp::R_PPC64_ADDR16_DS:
7868 	case elfcpp::R_PPC64_ADDR16_LO_DS:
7869 	case elfcpp::R_PPC64_ADDR16_HIGH:
7870 	case elfcpp::R_PPC64_ADDR16_HIGHA:
7871 	case elfcpp::R_PPC64_ADDR16_HIGHER:
7872 	case elfcpp::R_PPC64_ADDR16_HIGHEST:
7873 	case elfcpp::R_PPC64_ADDR16_HIGHERA:
7874 	case elfcpp::R_PPC64_ADDR16_HIGHESTA:
7875 	case elfcpp::R_PPC64_REL64:
7876 	case elfcpp::R_POWERPC_ADDR30:
7877 	case elfcpp::R_PPC64_TPREL16_DS:
7878 	case elfcpp::R_PPC64_TPREL16_LO_DS:
7879 	case elfcpp::R_PPC64_TPREL16_HIGH:
7880 	case elfcpp::R_PPC64_TPREL16_HIGHA:
7881 	case elfcpp::R_PPC64_TPREL16_HIGHER:
7882 	case elfcpp::R_PPC64_TPREL16_HIGHEST:
7883 	case elfcpp::R_PPC64_TPREL16_HIGHERA:
7884 	case elfcpp::R_PPC64_TPREL16_HIGHESTA:
7885 	  return;
7886 
7887 	default:
7888 	  break;
7889 	}
7890     }
7891   else
7892     {
7893       switch (r_type)
7894 	{
7895 	  // These are the relocation types supported only on 32-bit.
7896 	  // ??? glibc ld.so doesn't need to support these.
7897 	case elfcpp::R_POWERPC_REL24:
7898 	case elfcpp::R_POWERPC_DTPREL16:
7899 	case elfcpp::R_POWERPC_DTPREL16_LO:
7900 	case elfcpp::R_POWERPC_DTPREL16_HI:
7901 	case elfcpp::R_POWERPC_DTPREL16_HA:
7902 	  return;
7903 
7904 	default:
7905 	  break;
7906 	}
7907     }
7908 
7909   // This prevents us from issuing more than one error per reloc
7910   // section.  But we can still wind up issuing more than one
7911   // error per object file.
7912   if (this->issued_non_pic_error_)
7913     return;
7914   gold_assert(parameters->options().output_is_position_independent());
7915   object->error(_("requires unsupported dynamic reloc; "
7916 		  "recompile with -fPIC"));
7917   this->issued_non_pic_error_ = true;
7918   return;
7919 }
7920 
7921 // Return whether we need to make a PLT entry for a relocation of the
7922 // given type against a STT_GNU_IFUNC symbol.
7923 
7924 template<int size, bool big_endian>
7925 bool
reloc_needs_plt_for_ifunc(Target_powerpc<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int r_type,bool report_err)7926 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
7927      Target_powerpc<size, big_endian>* target,
7928      Sized_relobj_file<size, big_endian>* object,
7929      unsigned int r_type,
7930      bool report_err)
7931 {
7932   // In non-pic code any reference will resolve to the plt call stub
7933   // for the ifunc symbol.
7934   if ((size == 32 || target->abiversion() >= 2)
7935       && !parameters->options().output_is_position_independent())
7936     return true;
7937 
7938   switch (r_type)
7939     {
7940     // Word size refs from data sections are OK, but don't need a PLT entry.
7941     case elfcpp::R_POWERPC_ADDR32:
7942     case elfcpp::R_POWERPC_UADDR32:
7943       if (size == 32)
7944 	return false;
7945       break;
7946 
7947     case elfcpp::R_PPC64_ADDR64:
7948     case elfcpp::R_PPC64_UADDR64:
7949       if (size == 64)
7950 	return false;
7951       break;
7952 
7953     // GOT refs are good, but also don't need a PLT entry.
7954     case elfcpp::R_POWERPC_GOT16:
7955     case elfcpp::R_POWERPC_GOT16_LO:
7956     case elfcpp::R_POWERPC_GOT16_HI:
7957     case elfcpp::R_POWERPC_GOT16_HA:
7958     case elfcpp::R_PPC64_GOT16_DS:
7959     case elfcpp::R_PPC64_GOT16_LO_DS:
7960     case elfcpp::R_PPC64_GOT_PCREL34:
7961       return false;
7962 
7963     // PLT relocs are OK and need a PLT entry.
7964     case elfcpp::R_POWERPC_PLT16_LO:
7965     case elfcpp::R_POWERPC_PLT16_HI:
7966     case elfcpp::R_POWERPC_PLT16_HA:
7967     case elfcpp::R_PPC64_PLT16_LO_DS:
7968     case elfcpp::R_POWERPC_PLTSEQ:
7969     case elfcpp::R_POWERPC_PLTCALL:
7970     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
7971     case elfcpp::R_PPC64_PLTCALL_NOTOC:
7972     case elfcpp::R_PPC64_PLT_PCREL34:
7973     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
7974       return true;
7975       break;
7976 
7977     // Function calls are good, and these do need a PLT entry.
7978     case elfcpp::R_PPC64_REL24_NOTOC:
7979       if (size == 32)
7980 	break;
7981       // Fall through.
7982     case elfcpp::R_PPC64_REL24_P9NOTOC:
7983     case elfcpp::R_POWERPC_ADDR24:
7984     case elfcpp::R_POWERPC_ADDR14:
7985     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
7986     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
7987     case elfcpp::R_POWERPC_REL24:
7988     case elfcpp::R_PPC_PLTREL24:
7989     case elfcpp::R_POWERPC_REL14:
7990     case elfcpp::R_POWERPC_REL14_BRTAKEN:
7991     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
7992       return true;
7993 
7994     default:
7995       break;
7996     }
7997 
7998   // Anything else is a problem.
7999   // If we are building a static executable, the libc startup function
8000   // responsible for applying indirect function relocations is going
8001   // to complain about the reloc type.
8002   // If we are building a dynamic executable, we will have a text
8003   // relocation.  The dynamic loader will set the text segment
8004   // writable and non-executable to apply text relocations.  So we'll
8005   // segfault when trying to run the indirection function to resolve
8006   // the reloc.
8007   if (report_err)
8008     gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
8009 	       object->name().c_str(), r_type);
8010   return false;
8011 }
8012 
8013 // Return TRUE iff INSN is one we expect on a _LO variety toc/got
8014 // reloc.
8015 
8016 static bool
ok_lo_toc_insn(uint32_t insn,unsigned int r_type)8017 ok_lo_toc_insn(uint32_t insn, unsigned int r_type)
8018 {
8019   return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8020 	  || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8021 	  || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8022 	  || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8023 	  || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8024 	  || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8025 	  || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8026 	  || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8027 	  || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8028 	  || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8029 	  || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8030 	  || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8031 	  || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8032 	  || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8033 	  || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8034 	  || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8035 	  || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8036 	      /* Exclude lfqu by testing reloc.  If relocs are ever
8037 		 defined for the reduced D field in psq_lu then those
8038 		 will need testing too.  */
8039 	      && r_type != elfcpp::R_PPC64_TOC16_LO
8040 	      && r_type != elfcpp::R_POWERPC_GOT16_LO)
8041 	  || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8042 	      && (insn & 1) == 0)
8043 	  || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8044 	  || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8045 	      /* Exclude stfqu.  psq_stu as above for psq_lu.  */
8046 	      && r_type != elfcpp::R_PPC64_TOC16_LO
8047 	      && r_type != elfcpp::R_POWERPC_GOT16_LO)
8048 	  || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8049 	      && (insn & 1) == 0));
8050 }
8051 
8052 // Scan a relocation for a local symbol.
8053 
8054 template<int size, bool big_endian>
8055 inline void
local(Symbol_table * symtab,Layout * layout,Target_powerpc<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const elfcpp::Rela<size,big_endian> & reloc,unsigned int r_type,const elfcpp::Sym<size,big_endian> & lsym,bool is_discarded)8056 Target_powerpc<size, big_endian>::Scan::local(
8057     Symbol_table* symtab,
8058     Layout* layout,
8059     Target_powerpc<size, big_endian>* target,
8060     Sized_relobj_file<size, big_endian>* object,
8061     unsigned int data_shndx,
8062     Output_section* output_section,
8063     const elfcpp::Rela<size, big_endian>& reloc,
8064     unsigned int r_type,
8065     const elfcpp::Sym<size, big_endian>& lsym,
8066     bool is_discarded)
8067 {
8068   Powerpc_relobj<size, big_endian>* ppc_object
8069     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8070 
8071   this->maybe_skip_tls_get_addr_call(target, r_type, NULL);
8072 
8073   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8074       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8075     {
8076       this->expect_tls_get_addr_call();
8077       tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8078       if (tls_type != tls::TLSOPT_NONE)
8079 	this->skip_next_tls_get_addr_call();
8080     }
8081   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8082 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8083     {
8084       this->expect_tls_get_addr_call();
8085       tls::Tls_optimization tls_type = target->optimize_tls_ld();
8086       if (tls_type != tls::TLSOPT_NONE)
8087 	this->skip_next_tls_get_addr_call();
8088     }
8089 
8090   if (is_discarded)
8091     {
8092       if (size == 64
8093 	  && data_shndx == ppc_object->opd_shndx()
8094 	  && r_type == elfcpp::R_PPC64_ADDR64)
8095 	ppc_object->set_opd_discard(reloc.get_r_offset());
8096       return;
8097     }
8098 
8099   // A local STT_GNU_IFUNC symbol may require a PLT entry.
8100   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
8101   if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8102     {
8103       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8104       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8105 			  r_type, r_sym, reloc.get_r_addend());
8106       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
8107     }
8108 
8109   switch (r_type)
8110     {
8111     case elfcpp::R_POWERPC_NONE:
8112     case elfcpp::R_POWERPC_GNU_VTINHERIT:
8113     case elfcpp::R_POWERPC_GNU_VTENTRY:
8114     case elfcpp::R_POWERPC_TLS:
8115     case elfcpp::R_PPC64_ENTRY:
8116     case elfcpp::R_POWERPC_PLTSEQ:
8117     case elfcpp::R_POWERPC_PLTCALL:
8118     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8119     case elfcpp::R_PPC64_PLTCALL_NOTOC:
8120     case elfcpp::R_PPC64_PCREL_OPT:
8121     case elfcpp::R_PPC64_ADDR16_HIGHER34:
8122     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8123     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8124     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8125     case elfcpp::R_PPC64_REL16_HIGHER34:
8126     case elfcpp::R_PPC64_REL16_HIGHERA34:
8127     case elfcpp::R_PPC64_REL16_HIGHEST34:
8128     case elfcpp::R_PPC64_REL16_HIGHESTA34:
8129     case elfcpp::R_PPC64_D34:
8130     case elfcpp::R_PPC64_D34_LO:
8131     case elfcpp::R_PPC64_D34_HI30:
8132     case elfcpp::R_PPC64_D34_HA30:
8133     case elfcpp::R_PPC64_D28:
8134     case elfcpp::R_PPC64_PCREL34:
8135     case elfcpp::R_PPC64_PCREL28:
8136     case elfcpp::R_PPC64_TPREL34:
8137     case elfcpp::R_PPC64_DTPREL34:
8138       break;
8139 
8140     case elfcpp::R_PPC64_TOC:
8141       {
8142 	Output_data_got_powerpc<size, big_endian>* got
8143 	  = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8144 	if (parameters->options().output_is_position_independent())
8145 	  {
8146 	    Address off = reloc.get_r_offset();
8147 	    if (size == 64
8148 		&& target->abiversion() < 2
8149 		&& data_shndx == ppc_object->opd_shndx()
8150 		&& ppc_object->get_opd_discard(off - 8))
8151 	      break;
8152 
8153 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8154 	    Address got_off = got->g_o_t();
8155 	    rela_dyn->add_output_section_relative(got->output_section(),
8156 						  elfcpp::R_POWERPC_RELATIVE,
8157 						  output_section,
8158 						  object, data_shndx, off,
8159 						  got_off);
8160 	  }
8161       }
8162       break;
8163 
8164     case elfcpp::R_PPC64_ADDR64:
8165     case elfcpp::R_PPC64_UADDR64:
8166     case elfcpp::R_POWERPC_ADDR32:
8167     case elfcpp::R_POWERPC_UADDR32:
8168     case elfcpp::R_POWERPC_ADDR24:
8169     case elfcpp::R_POWERPC_ADDR16:
8170     case elfcpp::R_POWERPC_ADDR16_LO:
8171     case elfcpp::R_POWERPC_ADDR16_HI:
8172     case elfcpp::R_POWERPC_ADDR16_HA:
8173     case elfcpp::R_POWERPC_UADDR16:
8174     case elfcpp::R_PPC64_ADDR16_HIGH:
8175     case elfcpp::R_PPC64_ADDR16_HIGHA:
8176     case elfcpp::R_PPC64_ADDR16_HIGHER:
8177     case elfcpp::R_PPC64_ADDR16_HIGHERA:
8178     case elfcpp::R_PPC64_ADDR16_HIGHEST:
8179     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8180     case elfcpp::R_PPC64_ADDR16_DS:
8181     case elfcpp::R_PPC64_ADDR16_LO_DS:
8182     case elfcpp::R_POWERPC_ADDR14:
8183     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8184     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8185       // If building a shared library (or a position-independent
8186       // executable), we need to create a dynamic relocation for
8187       // this location.
8188       if (parameters->options().output_is_position_independent()
8189 	  || (size == 64 && is_ifunc && target->abiversion() < 2))
8190 	{
8191 	  Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8192 							     is_ifunc);
8193 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8194 	  if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
8195 	      || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
8196 	    {
8197 	      unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8198 				     : elfcpp::R_POWERPC_RELATIVE);
8199 	      rela_dyn->add_local_relative(object, r_sym, dynrel,
8200 					   output_section, data_shndx,
8201 					   reloc.get_r_offset(),
8202 					   reloc.get_r_addend(), false);
8203 	    }
8204 	  else if (lsym.get_st_type() != elfcpp::STT_SECTION)
8205 	    {
8206 	      check_non_pic(object, r_type);
8207 	      rela_dyn->add_local(object, r_sym, r_type, output_section,
8208 				  data_shndx, reloc.get_r_offset(),
8209 				  reloc.get_r_addend());
8210 	    }
8211 	  else
8212 	    {
8213 	      gold_assert(lsym.get_st_value() == 0);
8214 	      unsigned int shndx = lsym.get_st_shndx();
8215 	      bool is_ordinary;
8216 	      shndx = object->adjust_sym_shndx(r_sym, shndx,
8217 					       &is_ordinary);
8218 	      if (!is_ordinary)
8219 		object->error(_("section symbol %u has bad shndx %u"),
8220 			      r_sym, shndx);
8221 	      else
8222 		rela_dyn->add_local_section(object, shndx, r_type,
8223 					    output_section, data_shndx,
8224 					    reloc.get_r_offset());
8225 	    }
8226 	}
8227       break;
8228 
8229     case elfcpp::R_PPC64_PLT_PCREL34:
8230     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8231     case elfcpp::R_POWERPC_PLT16_LO:
8232     case elfcpp::R_POWERPC_PLT16_HI:
8233     case elfcpp::R_POWERPC_PLT16_HA:
8234     case elfcpp::R_PPC64_PLT16_LO_DS:
8235       if (!is_ifunc)
8236 	{
8237 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8238 	  target->make_local_plt_entry(symtab, layout, object, r_sym);
8239 	}
8240       break;
8241 
8242     case elfcpp::R_PPC64_REL24_NOTOC:
8243       if (size == 32)
8244 	break;
8245       // Fall through.
8246     case elfcpp::R_PPC64_REL24_P9NOTOC:
8247     case elfcpp::R_POWERPC_REL24:
8248     case elfcpp::R_PPC_PLTREL24:
8249     case elfcpp::R_PPC_LOCAL24PC:
8250     case elfcpp::R_POWERPC_REL14:
8251     case elfcpp::R_POWERPC_REL14_BRTAKEN:
8252     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
8253       if (!is_ifunc)
8254 	{
8255 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8256 	  target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8257 			      r_type, r_sym, reloc.get_r_addend());
8258 	}
8259       break;
8260 
8261     case elfcpp::R_PPC64_TOCSAVE:
8262       // R_PPC64_TOCSAVE follows a call instruction to indicate the
8263       // caller has already saved r2 and thus a plt call stub need not
8264       // save r2.
8265       if (size == 64
8266 	  && target->mark_pltcall(ppc_object, data_shndx,
8267 				  reloc.get_r_offset() - 4, symtab))
8268 	{
8269 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8270 	  unsigned int shndx = lsym.get_st_shndx();
8271 	  bool is_ordinary;
8272 	  shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8273 	  if (!is_ordinary)
8274 	    object->error(_("tocsave symbol %u has bad shndx %u"),
8275 			  r_sym, shndx);
8276 	  else
8277 	    target->add_tocsave(ppc_object, shndx,
8278 				lsym.get_st_value() + reloc.get_r_addend());
8279 	}
8280       break;
8281 
8282     case elfcpp::R_PPC64_REL64:
8283     case elfcpp::R_POWERPC_REL32:
8284     case elfcpp::R_POWERPC_REL16:
8285     case elfcpp::R_POWERPC_REL16_LO:
8286     case elfcpp::R_POWERPC_REL16_HI:
8287     case elfcpp::R_POWERPC_REL16_HA:
8288     case elfcpp::R_POWERPC_REL16DX_HA:
8289     case elfcpp::R_PPC64_REL16_HIGH:
8290     case elfcpp::R_PPC64_REL16_HIGHA:
8291     case elfcpp::R_PPC64_REL16_HIGHER:
8292     case elfcpp::R_PPC64_REL16_HIGHERA:
8293     case elfcpp::R_PPC64_REL16_HIGHEST:
8294     case elfcpp::R_PPC64_REL16_HIGHESTA:
8295     case elfcpp::R_POWERPC_SECTOFF:
8296     case elfcpp::R_POWERPC_SECTOFF_LO:
8297     case elfcpp::R_POWERPC_SECTOFF_HI:
8298     case elfcpp::R_POWERPC_SECTOFF_HA:
8299     case elfcpp::R_PPC64_SECTOFF_DS:
8300     case elfcpp::R_PPC64_SECTOFF_LO_DS:
8301     case elfcpp::R_POWERPC_TPREL16:
8302     case elfcpp::R_POWERPC_TPREL16_LO:
8303     case elfcpp::R_POWERPC_TPREL16_HI:
8304     case elfcpp::R_POWERPC_TPREL16_HA:
8305     case elfcpp::R_PPC64_TPREL16_DS:
8306     case elfcpp::R_PPC64_TPREL16_LO_DS:
8307     case elfcpp::R_PPC64_TPREL16_HIGH:
8308     case elfcpp::R_PPC64_TPREL16_HIGHA:
8309     case elfcpp::R_PPC64_TPREL16_HIGHER:
8310     case elfcpp::R_PPC64_TPREL16_HIGHERA:
8311     case elfcpp::R_PPC64_TPREL16_HIGHEST:
8312     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8313     case elfcpp::R_POWERPC_DTPREL16:
8314     case elfcpp::R_POWERPC_DTPREL16_LO:
8315     case elfcpp::R_POWERPC_DTPREL16_HI:
8316     case elfcpp::R_POWERPC_DTPREL16_HA:
8317     case elfcpp::R_PPC64_DTPREL16_DS:
8318     case elfcpp::R_PPC64_DTPREL16_LO_DS:
8319     case elfcpp::R_PPC64_DTPREL16_HIGH:
8320     case elfcpp::R_PPC64_DTPREL16_HIGHA:
8321     case elfcpp::R_PPC64_DTPREL16_HIGHER:
8322     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
8323     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
8324     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
8325     case elfcpp::R_PPC64_TLSGD:
8326     case elfcpp::R_PPC64_TLSLD:
8327     case elfcpp::R_PPC64_ADDR64_LOCAL:
8328       break;
8329 
8330     case elfcpp::R_PPC64_GOT_PCREL34:
8331     case elfcpp::R_POWERPC_GOT16:
8332     case elfcpp::R_POWERPC_GOT16_LO:
8333     case elfcpp::R_POWERPC_GOT16_HI:
8334     case elfcpp::R_POWERPC_GOT16_HA:
8335     case elfcpp::R_PPC64_GOT16_DS:
8336     case elfcpp::R_PPC64_GOT16_LO_DS:
8337       {
8338 	// The symbol requires a GOT entry.
8339 	Got_type got_type = ((size == 32
8340 			      || r_type == elfcpp::R_POWERPC_GOT16
8341 			      || r_type == elfcpp::R_PPC64_GOT16_DS)
8342 			     ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
8343 	Output_data_got_powerpc<size, big_endian>* got
8344 	  = target->got_section(symtab, layout, got_type);
8345 	unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8346 	uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8347 
8348 	if (!parameters->options().output_is_position_independent())
8349 	  {
8350 	    if (is_ifunc
8351 		&& (size == 32 || target->abiversion() >= 2))
8352 	      got->add_local_plt(object, r_sym, got_type, addend);
8353 	    else
8354 	      got->add_local(object, r_sym, got_type, addend);
8355 	  }
8356 	else if (!object->local_has_got_offset(r_sym, got_type, addend))
8357 	  {
8358 	    // If we are generating a shared object or a pie, this
8359 	    // symbol's GOT entry will be set by a dynamic relocation.
8360 	    unsigned int off;
8361 	    off = got->add_constant(0);
8362 	    object->set_local_got_offset(r_sym, got_type, off, addend);
8363 
8364 	    Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
8365 							       is_ifunc);
8366 	    unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8367 				   : elfcpp::R_POWERPC_RELATIVE);
8368 	    rela_dyn->add_local_relative(object, r_sym, dynrel,
8369 					 got, off, addend, false);
8370 	  }
8371       }
8372       break;
8373 
8374     case elfcpp::R_PPC64_TOC16:
8375     case elfcpp::R_PPC64_TOC16_LO:
8376     case elfcpp::R_PPC64_TOC16_HI:
8377     case elfcpp::R_PPC64_TOC16_HA:
8378     case elfcpp::R_PPC64_TOC16_DS:
8379     case elfcpp::R_PPC64_TOC16_LO_DS:
8380       // We need a GOT section.
8381       target->got_section(symtab, layout, GOT_TYPE_SMALL);
8382       break;
8383 
8384     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8385     case elfcpp::R_POWERPC_GOT_TLSGD16:
8386     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8387     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
8388     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8389       {
8390 	tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
8391 	if (tls_type == tls::TLSOPT_NONE)
8392 	  {
8393 	    Got_type got_type = ((size == 32
8394 				  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
8395 				 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
8396 	    Output_data_got_powerpc<size, big_endian>* got
8397 	      = target->got_section(symtab, layout, got_type);
8398 	    unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8399 	    uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8400 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8401 	    got->add_local_tls_pair(object, r_sym, got_type,
8402 				    rela_dyn, elfcpp::R_POWERPC_DTPMOD,
8403 				    addend);
8404 	  }
8405 	else if (tls_type == tls::TLSOPT_TO_LE)
8406 	  {
8407 	    // no GOT relocs needed for Local Exec.
8408 	  }
8409 	else
8410 	  gold_unreachable();
8411       }
8412       break;
8413 
8414     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8415     case elfcpp::R_POWERPC_GOT_TLSLD16:
8416     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8417     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
8418     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8419       {
8420 	tls::Tls_optimization tls_type = target->optimize_tls_ld();
8421 	if (tls_type == tls::TLSOPT_NONE)
8422 	  target->tlsld_got_offset(symtab, layout, object);
8423 	else if (tls_type == tls::TLSOPT_TO_LE)
8424 	  {
8425 	    // no GOT relocs needed for Local Exec.
8426 	    if (parameters->options().emit_relocs())
8427 	      {
8428 		Output_section* os = layout->tls_segment()->first_section();
8429 		gold_assert(os != NULL);
8430 		os->set_needs_symtab_index();
8431 	      }
8432 	  }
8433 	else
8434 	  gold_unreachable();
8435       }
8436       break;
8437 
8438     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8439     case elfcpp::R_POWERPC_GOT_DTPREL16:
8440     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8441     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
8442     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8443       {
8444 	Got_type got_type = ((size == 32
8445 			      || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
8446 			     ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
8447 	Output_data_got_powerpc<size, big_endian>* got
8448 	  = target->got_section(symtab, layout, got_type);
8449 	unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8450 	uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8451 	got->add_local_tls(object, r_sym, got_type, addend);
8452       }
8453       break;
8454 
8455     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8456     case elfcpp::R_POWERPC_GOT_TPREL16:
8457     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8458     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
8459     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8460       {
8461 	tls::Tls_optimization tls_type = target->optimize_tls_ie(true);
8462 	if (tls_type == tls::TLSOPT_NONE)
8463 	  {
8464 	    unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8465 	    uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
8466 	    Got_type got_type = ((size == 32
8467 				  || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
8468 				 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
8469 	    if (!object->local_has_got_offset(r_sym, got_type, addend))
8470 	      {
8471 		Output_data_got_powerpc<size, big_endian>* got
8472 		  = target->got_section(symtab, layout, got_type);
8473 		unsigned int off = got->add_constant(0);
8474 		object->set_local_got_offset(r_sym, got_type, off, addend);
8475 
8476 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8477 		rela_dyn->add_symbolless_local_addend(object, r_sym,
8478 						      elfcpp::R_POWERPC_TPREL,
8479 						      got, off, addend);
8480 	      }
8481 	  }
8482 	else if (tls_type == tls::TLSOPT_TO_LE)
8483 	  {
8484 	    // no GOT relocs needed for Local Exec.
8485 	  }
8486 	else
8487 	  gold_unreachable();
8488       }
8489       break;
8490 
8491     default:
8492       unsupported_reloc_local(object, r_type);
8493       break;
8494     }
8495 
8496   if (size == 64
8497       && parameters->options().toc_optimize())
8498     {
8499       if (data_shndx == ppc_object->toc_shndx())
8500 	{
8501 	  bool ok = true;
8502 	  if (r_type != elfcpp::R_PPC64_ADDR64
8503 	      || (is_ifunc && target->abiversion() < 2))
8504 	    ok = false;
8505 	  else if (parameters->options().output_is_position_independent())
8506 	    {
8507 	      if (is_ifunc)
8508 		ok = false;
8509 	      else
8510 		{
8511 		  unsigned int shndx = lsym.get_st_shndx();
8512 		  if (shndx >= elfcpp::SHN_LORESERVE
8513 		      && shndx != elfcpp::SHN_XINDEX)
8514 		    ok = false;
8515 		}
8516 	    }
8517 	  if (!ok)
8518 	    ppc_object->set_no_toc_opt(reloc.get_r_offset());
8519 	}
8520 
8521       enum {no_check, check_lo, check_ha} insn_check;
8522       switch (r_type)
8523 	{
8524 	default:
8525 	  insn_check = no_check;
8526 	  break;
8527 
8528 	case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
8529 	case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
8530 	case elfcpp::R_POWERPC_GOT_TPREL16_HA:
8531 	case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
8532 	case elfcpp::R_POWERPC_GOT16_HA:
8533 	case elfcpp::R_PPC64_TOC16_HA:
8534 	  insn_check = check_ha;
8535 	  break;
8536 
8537 	case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
8538 	case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
8539 	case elfcpp::R_POWERPC_GOT_TPREL16_LO:
8540 	case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
8541 	case elfcpp::R_POWERPC_GOT16_LO:
8542 	case elfcpp::R_PPC64_GOT16_LO_DS:
8543 	case elfcpp::R_PPC64_TOC16_LO:
8544 	case elfcpp::R_PPC64_TOC16_LO_DS:
8545 	  insn_check = check_lo;
8546 	  break;
8547 	}
8548 
8549       section_size_type slen;
8550       const unsigned char* view = NULL;
8551       if (insn_check != no_check)
8552 	{
8553 	  view = ppc_object->section_contents(data_shndx, &slen, false);
8554 	  section_size_type off =
8555 	    convert_to_section_size_type(reloc.get_r_offset()) & -4;
8556 	  if (off < slen)
8557 	    {
8558 	      uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8559 	      if (insn_check == check_lo
8560 		  ? !ok_lo_toc_insn(insn, r_type)
8561 		  : ((insn & ((0x3f << 26) | 0x1f << 16))
8562 		     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8563 		{
8564 		  ppc_object->set_no_toc_opt();
8565 		  gold_warning(_("%s: toc optimization is not supported "
8566 				 "for %#08x instruction"),
8567 			       ppc_object->name().c_str(), insn);
8568 		}
8569 	    }
8570 	}
8571 
8572       switch (r_type)
8573 	{
8574 	default:
8575 	  break;
8576 	case elfcpp::R_PPC64_TOC16:
8577 	case elfcpp::R_PPC64_TOC16_LO:
8578 	case elfcpp::R_PPC64_TOC16_HI:
8579 	case elfcpp::R_PPC64_TOC16_HA:
8580 	case elfcpp::R_PPC64_TOC16_DS:
8581 	case elfcpp::R_PPC64_TOC16_LO_DS:
8582 	  unsigned int shndx = lsym.get_st_shndx();
8583 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8584 	  bool is_ordinary;
8585 	  shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8586 	  if (is_ordinary && shndx == ppc_object->toc_shndx())
8587 	    {
8588 	      Address dst_off = lsym.get_st_value() + reloc.get_r_addend();
8589 	      if (dst_off < ppc_object->section_size(shndx))
8590 		{
8591 		  bool ok = false;
8592 		  if (r_type == elfcpp::R_PPC64_TOC16_HA)
8593 		    ok = true;
8594 		  else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
8595 		    {
8596 		      // Need to check that the insn is a ld
8597 		      if (!view)
8598 			view = ppc_object->section_contents(data_shndx,
8599 							    &slen,
8600 							    false);
8601 		      section_size_type off =
8602 			(convert_to_section_size_type(reloc.get_r_offset())
8603 			 + (big_endian ? -2 : 3));
8604 		      if (off < slen
8605 			  && (view[off] & (0x3f << 2)) == 58u << 2)
8606 			ok = true;
8607 		    }
8608 		  if (!ok)
8609 		    ppc_object->set_no_toc_opt(dst_off);
8610 		}
8611 	    }
8612 	  break;
8613 	}
8614     }
8615 
8616   if (size == 32)
8617     {
8618       switch (r_type)
8619 	{
8620 	case elfcpp::R_POWERPC_REL32:
8621 	  if (ppc_object->got2_shndx() != 0
8622 	      && parameters->options().output_is_position_independent())
8623 	    {
8624 	      unsigned int shndx = lsym.get_st_shndx();
8625 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8626 	      bool is_ordinary;
8627 	      shndx = ppc_object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
8628 	      if (is_ordinary && shndx == ppc_object->got2_shndx()
8629 		  && (ppc_object->section_flags(data_shndx)
8630 		      & elfcpp::SHF_EXECINSTR) != 0)
8631 		gold_error(_("%s: unsupported -mbss-plt code"),
8632 			   ppc_object->name().c_str());
8633 	    }
8634 	  break;
8635 	default:
8636 	  break;
8637 	}
8638     }
8639 
8640   switch (r_type)
8641     {
8642     case elfcpp::R_POWERPC_GOT_TLSLD16:
8643     case elfcpp::R_POWERPC_GOT_TLSGD16:
8644     case elfcpp::R_POWERPC_GOT_TPREL16:
8645     case elfcpp::R_POWERPC_GOT_DTPREL16:
8646     case elfcpp::R_POWERPC_GOT16:
8647     case elfcpp::R_PPC64_GOT16_DS:
8648     case elfcpp::R_PPC64_TOC16:
8649     case elfcpp::R_PPC64_TOC16_DS:
8650       ppc_object->set_has_small_toc_reloc();
8651       break;
8652     default:
8653       break;
8654     }
8655 
8656   switch (r_type)
8657     {
8658     case elfcpp::R_PPC64_TPREL16_DS:
8659     case elfcpp::R_PPC64_TPREL16_LO_DS:
8660     case elfcpp::R_PPC64_TPREL16_HIGH:
8661     case elfcpp::R_PPC64_TPREL16_HIGHA:
8662     case elfcpp::R_PPC64_TPREL16_HIGHER:
8663     case elfcpp::R_PPC64_TPREL16_HIGHERA:
8664     case elfcpp::R_PPC64_TPREL16_HIGHEST:
8665     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8666     case elfcpp::R_PPC64_TPREL34:
8667       if (size != 64)
8668 	break;
8669       // Fall through.
8670     case elfcpp::R_POWERPC_TPREL16:
8671     case elfcpp::R_POWERPC_TPREL16_LO:
8672     case elfcpp::R_POWERPC_TPREL16_HI:
8673     case elfcpp::R_POWERPC_TPREL16_HA:
8674       layout->set_has_static_tls();
8675       break;
8676     default:
8677       break;
8678     }
8679 
8680   switch (r_type)
8681     {
8682     case elfcpp::R_POWERPC_TPREL16_HA:
8683       if (target->tprel_opt())
8684 	{
8685 	  section_size_type slen;
8686 	  const unsigned char* view = NULL;
8687 	  view = ppc_object->section_contents(data_shndx, &slen, false);
8688 	  section_size_type off
8689 	    = convert_to_section_size_type(reloc.get_r_offset()) & -4;
8690 	  if (off < slen)
8691 	    {
8692 	      uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
8693 	      if ((insn & ((0x3fu << 26) | 0x1f << 16))
8694 		  != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
8695 		target->set_no_tprel_opt();
8696 	    }
8697 	}
8698       break;
8699 
8700     case elfcpp::R_PPC64_TPREL16_HIGH:
8701     case elfcpp::R_PPC64_TPREL16_HIGHA:
8702     case elfcpp::R_PPC64_TPREL16_HIGHER:
8703     case elfcpp::R_PPC64_TPREL16_HIGHERA:
8704     case elfcpp::R_PPC64_TPREL16_HIGHEST:
8705     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
8706       if (size != 64)
8707 	break;
8708       // Fall through.
8709     case elfcpp::R_POWERPC_TPREL16_HI:
8710       target->set_no_tprel_opt();
8711       break;
8712     default:
8713       break;
8714     }
8715 
8716   switch (r_type)
8717     {
8718     case elfcpp::R_PPC64_D34:
8719     case elfcpp::R_PPC64_D34_LO:
8720     case elfcpp::R_PPC64_D34_HI30:
8721     case elfcpp::R_PPC64_D34_HA30:
8722     case elfcpp::R_PPC64_D28:
8723     case elfcpp::R_PPC64_PCREL34:
8724     case elfcpp::R_PPC64_PCREL28:
8725     case elfcpp::R_PPC64_TPREL34:
8726     case elfcpp::R_PPC64_DTPREL34:
8727     case elfcpp::R_PPC64_PLT_PCREL34:
8728     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8729     case elfcpp::R_PPC64_GOT_PCREL34:
8730     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
8731     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
8732     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
8733     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
8734       target->set_power10_relocs();
8735       break;
8736     default:
8737       break;
8738     }
8739 }
8740 
8741 // Report an unsupported relocation against a global symbol.
8742 
8743 template<int size, bool big_endian>
8744 void
unsupported_reloc_global(Sized_relobj_file<size,big_endian> * object,unsigned int r_type,Symbol * gsym)8745 Target_powerpc<size, big_endian>::Scan::unsupported_reloc_global(
8746     Sized_relobj_file<size, big_endian>* object,
8747     unsigned int r_type,
8748     Symbol* gsym)
8749 {
8750   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
8751 	     object->name().c_str(), r_type, gsym->demangled_name().c_str());
8752 }
8753 
8754 // Scan a relocation for a global symbol.
8755 
8756 template<int size, bool big_endian>
8757 inline void
global(Symbol_table * symtab,Layout * layout,Target_powerpc<size,big_endian> * target,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,Output_section * output_section,const elfcpp::Rela<size,big_endian> & reloc,unsigned int r_type,Symbol * gsym)8758 Target_powerpc<size, big_endian>::Scan::global(
8759     Symbol_table* symtab,
8760     Layout* layout,
8761     Target_powerpc<size, big_endian>* target,
8762     Sized_relobj_file<size, big_endian>* object,
8763     unsigned int data_shndx,
8764     Output_section* output_section,
8765     const elfcpp::Rela<size, big_endian>& reloc,
8766     unsigned int r_type,
8767     Symbol* gsym)
8768 {
8769   Powerpc_relobj<size, big_endian>* ppc_object
8770     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
8771 
8772   switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
8773     {
8774     case Track_tls::SKIP:
8775       return;
8776     default:
8777       break;
8778     }
8779 
8780   if (target->replace_tls_get_addr(gsym))
8781     // Change a __tls_get_addr reference to __tls_get_addr_opt
8782     // so dynamic relocs are emitted against the latter symbol.
8783     gsym = target->tls_get_addr_opt();
8784 
8785   if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
8786       || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
8787     {
8788       this->expect_tls_get_addr_call();
8789       bool final = gsym->final_value_is_known();
8790       tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
8791       if (tls_type != tls::TLSOPT_NONE)
8792 	this->skip_next_tls_get_addr_call();
8793     }
8794   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
8795 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
8796     {
8797       this->expect_tls_get_addr_call();
8798       tls::Tls_optimization tls_type = target->optimize_tls_ld();
8799       if (tls_type != tls::TLSOPT_NONE)
8800 	this->skip_next_tls_get_addr_call();
8801     }
8802 
8803   // A STT_GNU_IFUNC symbol may require a PLT entry.
8804   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
8805   bool pushed_ifunc = false;
8806   if (is_ifunc && this->reloc_needs_plt_for_ifunc(target, object, r_type, true))
8807     {
8808       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8809       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
8810 			  r_type, r_sym, reloc.get_r_addend());
8811       target->make_plt_entry(symtab, layout, gsym);
8812       pushed_ifunc = true;
8813     }
8814 
8815   switch (r_type)
8816     {
8817     case elfcpp::R_POWERPC_NONE:
8818     case elfcpp::R_POWERPC_GNU_VTINHERIT:
8819     case elfcpp::R_POWERPC_GNU_VTENTRY:
8820     case elfcpp::R_PPC_LOCAL24PC:
8821     case elfcpp::R_POWERPC_TLS:
8822     case elfcpp::R_PPC64_ENTRY:
8823     case elfcpp::R_POWERPC_PLTSEQ:
8824     case elfcpp::R_POWERPC_PLTCALL:
8825     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
8826     case elfcpp::R_PPC64_PLTCALL_NOTOC:
8827     case elfcpp::R_PPC64_PCREL_OPT:
8828     case elfcpp::R_PPC64_ADDR16_HIGHER34:
8829     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
8830     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
8831     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
8832     case elfcpp::R_PPC64_REL16_HIGHER34:
8833     case elfcpp::R_PPC64_REL16_HIGHERA34:
8834     case elfcpp::R_PPC64_REL16_HIGHEST34:
8835     case elfcpp::R_PPC64_REL16_HIGHESTA34:
8836     case elfcpp::R_PPC64_D34:
8837     case elfcpp::R_PPC64_D34_LO:
8838     case elfcpp::R_PPC64_D34_HI30:
8839     case elfcpp::R_PPC64_D34_HA30:
8840     case elfcpp::R_PPC64_D28:
8841     case elfcpp::R_PPC64_PCREL34:
8842     case elfcpp::R_PPC64_PCREL28:
8843     case elfcpp::R_PPC64_TPREL34:
8844     case elfcpp::R_PPC64_DTPREL34:
8845       break;
8846 
8847     case elfcpp::R_PPC64_TOC:
8848       {
8849 	Output_data_got_powerpc<size, big_endian>* got
8850 	  = target->got_section(symtab, layout, GOT_TYPE_SMALL);
8851 	if (parameters->options().output_is_position_independent())
8852 	  {
8853 	    Address off = reloc.get_r_offset();
8854 	    if (size == 64
8855 		&& data_shndx == ppc_object->opd_shndx()
8856 		&& ppc_object->get_opd_discard(off - 8))
8857 	      break;
8858 
8859 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8860 	    Address got_off = got->g_o_t();
8861 	    rela_dyn->add_output_section_relative(got->output_section(),
8862 						  elfcpp::R_POWERPC_RELATIVE,
8863 						  output_section,
8864 						  object, data_shndx, off,
8865 						  got_off);
8866 	  }
8867       }
8868       break;
8869 
8870     case elfcpp::R_PPC64_ADDR64:
8871       if (size == 64
8872 	  && target->abiversion() < 2
8873 	  && data_shndx == ppc_object->opd_shndx()
8874 	  && (gsym->is_defined_in_discarded_section()
8875 	      || gsym->object() != object))
8876 	{
8877 	  ppc_object->set_opd_discard(reloc.get_r_offset());
8878 	  break;
8879 	}
8880       // Fall through.
8881     case elfcpp::R_PPC64_UADDR64:
8882     case elfcpp::R_POWERPC_ADDR32:
8883     case elfcpp::R_POWERPC_UADDR32:
8884     case elfcpp::R_POWERPC_ADDR24:
8885     case elfcpp::R_POWERPC_ADDR16:
8886     case elfcpp::R_POWERPC_ADDR16_LO:
8887     case elfcpp::R_POWERPC_ADDR16_HI:
8888     case elfcpp::R_POWERPC_ADDR16_HA:
8889     case elfcpp::R_POWERPC_UADDR16:
8890     case elfcpp::R_PPC64_ADDR16_HIGH:
8891     case elfcpp::R_PPC64_ADDR16_HIGHA:
8892     case elfcpp::R_PPC64_ADDR16_HIGHER:
8893     case elfcpp::R_PPC64_ADDR16_HIGHERA:
8894     case elfcpp::R_PPC64_ADDR16_HIGHEST:
8895     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
8896     case elfcpp::R_PPC64_ADDR16_DS:
8897     case elfcpp::R_PPC64_ADDR16_LO_DS:
8898     case elfcpp::R_POWERPC_ADDR14:
8899     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
8900     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
8901       {
8902 	// Make a PLT entry if necessary.
8903 	if (gsym->needs_plt_entry())
8904 	  {
8905 	    // Since this is not a PC-relative relocation, we may be
8906 	    // taking the address of a function. In that case we need to
8907 	    // set the entry in the dynamic symbol table to the address of
8908 	    // the PLT call stub.
8909 	    bool need_ifunc_plt = false;
8910 	    if ((size == 32 || target->abiversion() >= 2)
8911 		&& gsym->is_from_dynobj()
8912 		&& !parameters->options().output_is_position_independent())
8913 	      {
8914 		gsym->set_needs_dynsym_value();
8915 		need_ifunc_plt = true;
8916 	      }
8917 	    if (!is_ifunc || (!pushed_ifunc && need_ifunc_plt))
8918 	      {
8919 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
8920 		target->push_branch(ppc_object, data_shndx,
8921 				    reloc.get_r_offset(), r_type, r_sym,
8922 				    reloc.get_r_addend());
8923 		target->make_plt_entry(symtab, layout, gsym);
8924 	      }
8925 	  }
8926 	// Make a dynamic relocation if necessary.
8927 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target))
8928 	    || (size == 64 && is_ifunc && target->abiversion() < 2))
8929 	  {
8930 	    if (!parameters->options().output_is_position_independent()
8931 		&& gsym->may_need_copy_reloc())
8932 	      {
8933 		target->copy_reloc(symtab, layout, object,
8934 				   data_shndx, output_section, gsym, reloc);
8935 	      }
8936 	    else if ((((size == 32
8937 			&& r_type == elfcpp::R_POWERPC_ADDR32)
8938 		       || (size == 64
8939 			   && r_type == elfcpp::R_PPC64_ADDR64
8940 			   && target->abiversion() >= 2))
8941 		      && gsym->can_use_relative_reloc(false)
8942 		      && !(gsym->visibility() == elfcpp::STV_PROTECTED
8943 			   && parameters->options().shared()))
8944 		     || (size == 64
8945 			 && r_type == elfcpp::R_PPC64_ADDR64
8946 			 && target->abiversion() < 2
8947 			 && (gsym->can_use_relative_reloc(false)
8948 			     || data_shndx == ppc_object->opd_shndx())))
8949 	      {
8950 		Reloc_section* rela_dyn
8951 		  = target->rela_dyn_section(symtab, layout, is_ifunc);
8952 		unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
8953 				       : elfcpp::R_POWERPC_RELATIVE);
8954 		rela_dyn->add_symbolless_global_addend(
8955 		    gsym, dynrel, output_section, object, data_shndx,
8956 		    reloc.get_r_offset(), reloc.get_r_addend());
8957 	      }
8958 	    else
8959 	      {
8960 		Reloc_section* rela_dyn
8961 		  = target->rela_dyn_section(symtab, layout, is_ifunc);
8962 		check_non_pic(object, r_type);
8963 		rela_dyn->add_global(gsym, r_type, output_section,
8964 				     object, data_shndx,
8965 				     reloc.get_r_offset(),
8966 				     reloc.get_r_addend());
8967 
8968 		if (size == 64
8969 		    && parameters->options().toc_optimize()
8970 		    && data_shndx == ppc_object->toc_shndx())
8971 		  ppc_object->set_no_toc_opt(reloc.get_r_offset());
8972 	      }
8973 	  }
8974       }
8975       break;
8976 
8977     case elfcpp::R_PPC64_PLT_PCREL34:
8978     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
8979     case elfcpp::R_POWERPC_PLT16_LO:
8980     case elfcpp::R_POWERPC_PLT16_HI:
8981     case elfcpp::R_POWERPC_PLT16_HA:
8982     case elfcpp::R_PPC64_PLT16_LO_DS:
8983       if (!pushed_ifunc)
8984 	{
8985 	  if (!parameters->doing_static_link())
8986 	    target->make_plt_entry(symtab, layout, gsym);
8987 	  else
8988 	    target->make_local_plt_entry(symtab, layout, gsym);
8989 	}
8990       break;
8991 
8992     case elfcpp::R_PPC64_REL24_NOTOC:
8993       if (size == 32)
8994 	break;
8995       // Fall through.
8996     case elfcpp::R_PPC64_REL24_P9NOTOC:
8997     case elfcpp::R_PPC_PLTREL24:
8998     case elfcpp::R_POWERPC_REL24:
8999       if (!is_ifunc)
9000 	{
9001 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9002 	  target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9003 			      r_type, r_sym, reloc.get_r_addend());
9004 	  if (gsym->needs_plt_entry()
9005 	      || (!gsym->final_value_is_known()
9006 		  && (gsym->is_undefined()
9007 		      || gsym->is_from_dynobj()
9008 		      || gsym->is_preemptible())))
9009 	    target->make_plt_entry(symtab, layout, gsym);
9010 	}
9011       // Fall through.
9012 
9013     case elfcpp::R_PPC64_REL64:
9014     case elfcpp::R_POWERPC_REL32:
9015       // Make a dynamic relocation if necessary.
9016       if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type, target)))
9017 	{
9018 	  if (!parameters->options().output_is_position_independent()
9019 	      && gsym->may_need_copy_reloc())
9020 	    {
9021 	      target->copy_reloc(symtab, layout, object,
9022 				 data_shndx, output_section, gsym,
9023 				 reloc);
9024 	    }
9025 	  else
9026 	    {
9027 	      Reloc_section* rela_dyn
9028 		= target->rela_dyn_section(symtab, layout, is_ifunc);
9029 	      check_non_pic(object, r_type);
9030 	      rela_dyn->add_global(gsym, r_type, output_section, object,
9031 				   data_shndx, reloc.get_r_offset(),
9032 				   reloc.get_r_addend());
9033 	    }
9034 	}
9035       break;
9036 
9037     case elfcpp::R_POWERPC_REL14:
9038     case elfcpp::R_POWERPC_REL14_BRTAKEN:
9039     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
9040       if (!is_ifunc)
9041 	{
9042 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9043 	  target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
9044 			      r_type, r_sym, reloc.get_r_addend());
9045 	}
9046       break;
9047 
9048     case elfcpp::R_PPC64_TOCSAVE:
9049       // R_PPC64_TOCSAVE follows a call instruction to indicate the
9050       // caller has already saved r2 and thus a plt call stub need not
9051       // save r2.
9052       if (size == 64
9053 	  && target->mark_pltcall(ppc_object, data_shndx,
9054 				  reloc.get_r_offset() - 4, symtab))
9055 	{
9056 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
9057 	  bool is_ordinary;
9058 	  unsigned int shndx = gsym->shndx(&is_ordinary);
9059 	  if (!is_ordinary)
9060 	    object->error(_("tocsave symbol %u has bad shndx %u"),
9061 			  r_sym, shndx);
9062 	  else
9063 	    {
9064 	      Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9065 	      target->add_tocsave(ppc_object, shndx,
9066 				  sym->value() + reloc.get_r_addend());
9067 	    }
9068 	}
9069       break;
9070 
9071     case elfcpp::R_POWERPC_REL16:
9072     case elfcpp::R_POWERPC_REL16_LO:
9073     case elfcpp::R_POWERPC_REL16_HI:
9074     case elfcpp::R_POWERPC_REL16_HA:
9075     case elfcpp::R_POWERPC_REL16DX_HA:
9076     case elfcpp::R_PPC64_REL16_HIGH:
9077     case elfcpp::R_PPC64_REL16_HIGHA:
9078     case elfcpp::R_PPC64_REL16_HIGHER:
9079     case elfcpp::R_PPC64_REL16_HIGHERA:
9080     case elfcpp::R_PPC64_REL16_HIGHEST:
9081     case elfcpp::R_PPC64_REL16_HIGHESTA:
9082     case elfcpp::R_POWERPC_SECTOFF:
9083     case elfcpp::R_POWERPC_SECTOFF_LO:
9084     case elfcpp::R_POWERPC_SECTOFF_HI:
9085     case elfcpp::R_POWERPC_SECTOFF_HA:
9086     case elfcpp::R_PPC64_SECTOFF_DS:
9087     case elfcpp::R_PPC64_SECTOFF_LO_DS:
9088     case elfcpp::R_POWERPC_TPREL16:
9089     case elfcpp::R_POWERPC_TPREL16_LO:
9090     case elfcpp::R_POWERPC_TPREL16_HI:
9091     case elfcpp::R_POWERPC_TPREL16_HA:
9092     case elfcpp::R_PPC64_TPREL16_DS:
9093     case elfcpp::R_PPC64_TPREL16_LO_DS:
9094     case elfcpp::R_PPC64_TPREL16_HIGH:
9095     case elfcpp::R_PPC64_TPREL16_HIGHA:
9096     case elfcpp::R_PPC64_TPREL16_HIGHER:
9097     case elfcpp::R_PPC64_TPREL16_HIGHERA:
9098     case elfcpp::R_PPC64_TPREL16_HIGHEST:
9099     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9100     case elfcpp::R_POWERPC_DTPREL16:
9101     case elfcpp::R_POWERPC_DTPREL16_LO:
9102     case elfcpp::R_POWERPC_DTPREL16_HI:
9103     case elfcpp::R_POWERPC_DTPREL16_HA:
9104     case elfcpp::R_PPC64_DTPREL16_DS:
9105     case elfcpp::R_PPC64_DTPREL16_LO_DS:
9106     case elfcpp::R_PPC64_DTPREL16_HIGH:
9107     case elfcpp::R_PPC64_DTPREL16_HIGHA:
9108     case elfcpp::R_PPC64_DTPREL16_HIGHER:
9109     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
9110     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
9111     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
9112     case elfcpp::R_PPC64_TLSGD:
9113     case elfcpp::R_PPC64_TLSLD:
9114     case elfcpp::R_PPC64_ADDR64_LOCAL:
9115       break;
9116 
9117     case elfcpp::R_PPC64_GOT_PCREL34:
9118     case elfcpp::R_POWERPC_GOT16:
9119     case elfcpp::R_POWERPC_GOT16_LO:
9120     case elfcpp::R_POWERPC_GOT16_HI:
9121     case elfcpp::R_POWERPC_GOT16_HA:
9122     case elfcpp::R_PPC64_GOT16_DS:
9123     case elfcpp::R_PPC64_GOT16_LO_DS:
9124       {
9125 	// The symbol requires a GOT entry.
9126 	Output_data_got_powerpc<size, big_endian>* got;
9127 	uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9128 	Got_type got_type = ((size == 32
9129 			      || r_type == elfcpp::R_POWERPC_GOT16
9130 			      || r_type == elfcpp::R_PPC64_GOT16_DS)
9131 			     ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
9132 
9133 	got = target->got_section(symtab, layout, got_type);
9134 	if (gsym->final_value_is_known())
9135 	  {
9136 	    if (is_ifunc
9137 		&& (size == 32 || target->abiversion() >= 2))
9138 	      got->add_global_plt(gsym, got_type, addend);
9139 	    else
9140 	      got->add_global(gsym, got_type, addend);
9141 	  }
9142 	else if (!gsym->has_got_offset(got_type, addend))
9143 	  {
9144 	    // If we are generating a shared object or a pie, this
9145 	    // symbol's GOT entry will be set by a dynamic relocation.
9146 	    unsigned int off = got->add_constant(0);
9147 	    gsym->set_got_offset(got_type, off, addend);
9148 
9149 	    Reloc_section* rela_dyn
9150 	      = target->rela_dyn_section(symtab, layout, is_ifunc);
9151 
9152 	    if (gsym->can_use_relative_reloc(false)
9153 		&& !((size == 32
9154 		      || target->abiversion() >= 2)
9155 		     && gsym->visibility() == elfcpp::STV_PROTECTED
9156 		     && parameters->options().shared()))
9157 	      {
9158 		unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
9159 				       : elfcpp::R_POWERPC_RELATIVE);
9160 		rela_dyn->add_global_relative(gsym, dynrel, got, off,
9161 					      addend, false);
9162 	      }
9163 	    else
9164 	      {
9165 		unsigned int dynrel = elfcpp::R_POWERPC_GLOB_DAT;
9166 		rela_dyn->add_global(gsym, dynrel, got, off, addend);
9167 	      }
9168 	  }
9169       }
9170       break;
9171 
9172     case elfcpp::R_PPC64_TOC16:
9173     case elfcpp::R_PPC64_TOC16_LO:
9174     case elfcpp::R_PPC64_TOC16_HI:
9175     case elfcpp::R_PPC64_TOC16_HA:
9176     case elfcpp::R_PPC64_TOC16_DS:
9177     case elfcpp::R_PPC64_TOC16_LO_DS:
9178       // We need a GOT section.
9179       target->got_section(symtab, layout, GOT_TYPE_SMALL);
9180       break;
9181 
9182     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9183     case elfcpp::R_POWERPC_GOT_TLSGD16:
9184     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9185     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
9186     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9187       {
9188 	bool final = gsym->final_value_is_known();
9189 	tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
9190 	if (tls_type == tls::TLSOPT_NONE)
9191 	  {
9192 	    Got_type got_type = ((size == 32
9193 				  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9194 				 ? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
9195 	    Output_data_got_powerpc<size, big_endian>* got
9196 	      = target->got_section(symtab, layout, got_type);
9197 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9198 	    uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9199 	    got->add_global_pair_with_rel(gsym, got_type, rela_dyn,
9200 					  elfcpp::R_POWERPC_DTPMOD,
9201 					  elfcpp::R_POWERPC_DTPREL,
9202 					  addend);
9203 	  }
9204 	else if (tls_type == tls::TLSOPT_TO_IE)
9205 	  {
9206 	    Got_type got_type = ((size == 32
9207 				  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
9208 				 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9209 	    if (!gsym->has_got_offset(got_type))
9210 	      {
9211 		Output_data_got_powerpc<size, big_endian>* got
9212 		  = target->got_section(symtab, layout, got_type);
9213 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9214 		uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9215 		if (gsym->is_undefined()
9216 		    || gsym->is_from_dynobj())
9217 		  {
9218 		    got->add_global_with_rel(gsym, got_type, rela_dyn,
9219 					     elfcpp::R_POWERPC_TPREL, addend);
9220 		  }
9221 		else
9222 		  {
9223 		    unsigned int off = got->add_constant(0);
9224 		    gsym->set_got_offset(got_type, off);
9225 		    unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9226 		    rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9227 							   got, off, addend);
9228 		  }
9229 	      }
9230 	  }
9231 	else if (tls_type == tls::TLSOPT_TO_LE)
9232 	  {
9233 	    // no GOT relocs needed for Local Exec.
9234 	  }
9235 	else
9236 	  gold_unreachable();
9237       }
9238       break;
9239 
9240     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9241     case elfcpp::R_POWERPC_GOT_TLSLD16:
9242     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9243     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
9244     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9245       {
9246 	tls::Tls_optimization tls_type = target->optimize_tls_ld();
9247 	if (tls_type == tls::TLSOPT_NONE)
9248 	  target->tlsld_got_offset(symtab, layout, object);
9249 	else if (tls_type == tls::TLSOPT_TO_LE)
9250 	  {
9251 	    // no GOT relocs needed for Local Exec.
9252 	    if (parameters->options().emit_relocs())
9253 	      {
9254 		Output_section* os = layout->tls_segment()->first_section();
9255 		gold_assert(os != NULL);
9256 		os->set_needs_symtab_index();
9257 	      }
9258 	  }
9259 	else
9260 	  gold_unreachable();
9261       }
9262       break;
9263 
9264     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9265     case elfcpp::R_POWERPC_GOT_DTPREL16:
9266     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9267     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
9268     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9269       {
9270 	Got_type got_type = ((size == 32
9271 			      || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
9272 			     ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
9273 	Output_data_got_powerpc<size, big_endian>* got
9274 	  = target->got_section(symtab, layout, got_type);
9275 	uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9276 	if (!gsym->final_value_is_known()
9277 	    && (gsym->is_from_dynobj()
9278 		|| gsym->is_undefined()
9279 		|| gsym->is_preemptible()))
9280 	  got->add_global_with_rel(gsym, got_type,
9281 				   target->rela_dyn_section(layout),
9282 				   elfcpp::R_POWERPC_DTPREL, addend);
9283 	else
9284 	  got->add_global_tls(gsym, got_type, addend);
9285       }
9286       break;
9287 
9288     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9289     case elfcpp::R_POWERPC_GOT_TPREL16:
9290     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9291     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
9292     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9293       {
9294 	bool final = gsym->final_value_is_known();
9295 	tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
9296 	if (tls_type == tls::TLSOPT_NONE)
9297 	  {
9298 	    Got_type got_type = ((size == 32
9299 				  || r_type == elfcpp::R_POWERPC_GOT_TPREL16)
9300 				 ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
9301 	    if (!gsym->has_got_offset(got_type))
9302 	      {
9303 		Output_data_got_powerpc<size, big_endian>* got
9304 		  = target->got_section(symtab, layout, got_type);
9305 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
9306 		uint64_t addend = size == 32 ? 0 : reloc.get_r_addend();
9307 		if (gsym->is_undefined()
9308 		    || gsym->is_from_dynobj())
9309 		  {
9310 		    got->add_global_with_rel(gsym, got_type, rela_dyn,
9311 					     elfcpp::R_POWERPC_TPREL, addend);
9312 		  }
9313 		else
9314 		  {
9315 		    unsigned int off = got->add_constant(0);
9316 		    gsym->set_got_offset(got_type, off);
9317 		    unsigned int dynrel = elfcpp::R_POWERPC_TPREL;
9318 		    rela_dyn->add_symbolless_global_addend(gsym, dynrel,
9319 							   got, off, addend);
9320 		  }
9321 	      }
9322 	  }
9323 	else if (tls_type == tls::TLSOPT_TO_LE)
9324 	  {
9325 	    // no GOT relocs needed for Local Exec.
9326 	  }
9327 	else
9328 	  gold_unreachable();
9329       }
9330       break;
9331 
9332     default:
9333       unsupported_reloc_global(object, r_type, gsym);
9334       break;
9335     }
9336 
9337   if (size == 64
9338       && parameters->options().toc_optimize())
9339     {
9340       if (data_shndx == ppc_object->toc_shndx())
9341 	{
9342 	  bool ok = true;
9343 	  if (r_type != elfcpp::R_PPC64_ADDR64
9344 	      || (is_ifunc && target->abiversion() < 2))
9345 	    ok = false;
9346 	  else if (parameters->options().output_is_position_independent()
9347 		   && (is_ifunc || gsym->is_absolute() || gsym->is_undefined()))
9348 	    ok = false;
9349 	  if (!ok)
9350 	    ppc_object->set_no_toc_opt(reloc.get_r_offset());
9351 	}
9352 
9353       enum {no_check, check_lo, check_ha} insn_check;
9354       switch (r_type)
9355 	{
9356 	default:
9357 	  insn_check = no_check;
9358 	  break;
9359 
9360 	case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
9361 	case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
9362 	case elfcpp::R_POWERPC_GOT_TPREL16_HA:
9363 	case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
9364 	case elfcpp::R_POWERPC_GOT16_HA:
9365 	case elfcpp::R_PPC64_TOC16_HA:
9366 	  insn_check = check_ha;
9367 	  break;
9368 
9369 	case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
9370 	case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
9371 	case elfcpp::R_POWERPC_GOT_TPREL16_LO:
9372 	case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
9373 	case elfcpp::R_POWERPC_GOT16_LO:
9374 	case elfcpp::R_PPC64_GOT16_LO_DS:
9375 	case elfcpp::R_PPC64_TOC16_LO:
9376 	case elfcpp::R_PPC64_TOC16_LO_DS:
9377 	  insn_check = check_lo;
9378 	  break;
9379 	}
9380 
9381       section_size_type slen;
9382       const unsigned char* view = NULL;
9383       if (insn_check != no_check)
9384 	{
9385 	  view = ppc_object->section_contents(data_shndx, &slen, false);
9386 	  section_size_type off =
9387 	    convert_to_section_size_type(reloc.get_r_offset()) & -4;
9388 	  if (off < slen)
9389 	    {
9390 	      uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9391 	      if (insn_check == check_lo
9392 		  ? !ok_lo_toc_insn(insn, r_type)
9393 		  : ((insn & ((0x3f << 26) | 0x1f << 16))
9394 		     != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9395 		{
9396 		  ppc_object->set_no_toc_opt();
9397 		  gold_warning(_("%s: toc optimization is not supported "
9398 				 "for %#08x instruction"),
9399 			       ppc_object->name().c_str(), insn);
9400 		}
9401 	    }
9402 	}
9403 
9404       switch (r_type)
9405 	{
9406 	default:
9407 	  break;
9408 	case elfcpp::R_PPC64_TOC16:
9409 	case elfcpp::R_PPC64_TOC16_LO:
9410 	case elfcpp::R_PPC64_TOC16_HI:
9411 	case elfcpp::R_PPC64_TOC16_HA:
9412 	case elfcpp::R_PPC64_TOC16_DS:
9413 	case elfcpp::R_PPC64_TOC16_LO_DS:
9414 	  if (gsym->source() == Symbol::FROM_OBJECT
9415 	      && !gsym->object()->is_dynamic())
9416 	    {
9417 	      Powerpc_relobj<size, big_endian>* sym_object
9418 		= static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
9419 	      bool is_ordinary;
9420 	      unsigned int shndx = gsym->shndx(&is_ordinary);
9421 	      if (shndx == sym_object->toc_shndx())
9422 		{
9423 		  Sized_symbol<size>* sym = symtab->get_sized_symbol<size>(gsym);
9424 		  Address dst_off = sym->value() + reloc.get_r_addend();
9425 		  if (dst_off < sym_object->section_size(shndx))
9426 		    {
9427 		      bool ok = false;
9428 		      if (r_type == elfcpp::R_PPC64_TOC16_HA)
9429 			ok = true;
9430 		      else if (r_type == elfcpp::R_PPC64_TOC16_LO_DS)
9431 			{
9432 			  // Need to check that the insn is a ld
9433 			  if (!view)
9434 			    view = ppc_object->section_contents(data_shndx,
9435 								&slen,
9436 								false);
9437 			  section_size_type off =
9438 			    (convert_to_section_size_type(reloc.get_r_offset())
9439 			     + (big_endian ? -2 : 3));
9440 			  if (off < slen
9441 			      && (view[off] & (0x3f << 2)) == (58u << 2))
9442 			    ok = true;
9443 			}
9444 		      if (!ok)
9445 			sym_object->set_no_toc_opt(dst_off);
9446 		    }
9447 		}
9448 	    }
9449 	  break;
9450 	}
9451     }
9452 
9453   if (size == 32)
9454     {
9455       switch (r_type)
9456 	{
9457 	case elfcpp::R_PPC_LOCAL24PC:
9458 	  if (strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
9459 	    gold_error(_("%s: unsupported -mbss-plt code"),
9460 		       ppc_object->name().c_str());
9461 	  break;
9462 	default:
9463 	  break;
9464 	}
9465     }
9466 
9467   switch (r_type)
9468     {
9469     case elfcpp::R_POWERPC_GOT_TLSLD16:
9470     case elfcpp::R_POWERPC_GOT_TLSGD16:
9471     case elfcpp::R_POWERPC_GOT_TPREL16:
9472     case elfcpp::R_POWERPC_GOT_DTPREL16:
9473     case elfcpp::R_POWERPC_GOT16:
9474     case elfcpp::R_PPC64_GOT16_DS:
9475     case elfcpp::R_PPC64_TOC16:
9476     case elfcpp::R_PPC64_TOC16_DS:
9477       ppc_object->set_has_small_toc_reloc();
9478       break;
9479     default:
9480       break;
9481     }
9482 
9483   switch (r_type)
9484     {
9485     case elfcpp::R_PPC64_TPREL16_DS:
9486     case elfcpp::R_PPC64_TPREL16_LO_DS:
9487     case elfcpp::R_PPC64_TPREL16_HIGH:
9488     case elfcpp::R_PPC64_TPREL16_HIGHA:
9489     case elfcpp::R_PPC64_TPREL16_HIGHER:
9490     case elfcpp::R_PPC64_TPREL16_HIGHERA:
9491     case elfcpp::R_PPC64_TPREL16_HIGHEST:
9492     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9493     case elfcpp::R_PPC64_TPREL34:
9494       if (size != 64)
9495 	break;
9496       // Fall through.
9497     case elfcpp::R_POWERPC_TPREL16:
9498     case elfcpp::R_POWERPC_TPREL16_LO:
9499     case elfcpp::R_POWERPC_TPREL16_HI:
9500     case elfcpp::R_POWERPC_TPREL16_HA:
9501       layout->set_has_static_tls();
9502       break;
9503     default:
9504       break;
9505     }
9506 
9507   switch (r_type)
9508     {
9509     case elfcpp::R_POWERPC_TPREL16_HA:
9510       if (target->tprel_opt())
9511 	{
9512 	  section_size_type slen;
9513 	  const unsigned char* view = NULL;
9514 	  view = ppc_object->section_contents(data_shndx, &slen, false);
9515 	  section_size_type off
9516 	    = convert_to_section_size_type(reloc.get_r_offset()) & -4;
9517 	  if (off < slen)
9518 	    {
9519 	      uint32_t insn = elfcpp::Swap<32, big_endian>::readval(view + off);
9520 	      if ((insn & ((0x3fu << 26) | 0x1f << 16))
9521 		  != ((15u << 26) | ((size == 32 ? 2 : 13) << 16)))
9522 		target->set_no_tprel_opt();
9523 	    }
9524 	}
9525       break;
9526 
9527     case elfcpp::R_PPC64_TPREL16_HIGH:
9528     case elfcpp::R_PPC64_TPREL16_HIGHA:
9529     case elfcpp::R_PPC64_TPREL16_HIGHER:
9530     case elfcpp::R_PPC64_TPREL16_HIGHERA:
9531     case elfcpp::R_PPC64_TPREL16_HIGHEST:
9532     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
9533       if (size != 64)
9534 	break;
9535       // Fall through.
9536     case elfcpp::R_POWERPC_TPREL16_HI:
9537       target->set_no_tprel_opt();
9538       break;
9539     default:
9540       break;
9541     }
9542 
9543   switch (r_type)
9544     {
9545     case elfcpp::R_PPC64_D34:
9546     case elfcpp::R_PPC64_D34_LO:
9547     case elfcpp::R_PPC64_D34_HI30:
9548     case elfcpp::R_PPC64_D34_HA30:
9549     case elfcpp::R_PPC64_D28:
9550     case elfcpp::R_PPC64_PCREL34:
9551     case elfcpp::R_PPC64_PCREL28:
9552     case elfcpp::R_PPC64_TPREL34:
9553     case elfcpp::R_PPC64_DTPREL34:
9554     case elfcpp::R_PPC64_PLT_PCREL34:
9555     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
9556     case elfcpp::R_PPC64_GOT_PCREL34:
9557     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
9558     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
9559     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
9560     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
9561       target->set_power10_relocs();
9562       break;
9563     default:
9564       break;
9565     }
9566 }
9567 
9568 // Process relocations for gc.
9569 
9570 template<int size, bool big_endian>
9571 void
gc_process_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)9572 Target_powerpc<size, big_endian>::gc_process_relocs(
9573     Symbol_table* symtab,
9574     Layout* layout,
9575     Sized_relobj_file<size, big_endian>* object,
9576     unsigned int data_shndx,
9577     unsigned int,
9578     const unsigned char* prelocs,
9579     size_t reloc_count,
9580     Output_section* output_section,
9581     bool needs_special_offset_handling,
9582     size_t local_symbol_count,
9583     const unsigned char* plocal_symbols)
9584 {
9585   typedef Target_powerpc<size, big_endian> Powerpc;
9586   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9587       Classify_reloc;
9588 
9589   Powerpc_relobj<size, big_endian>* ppc_object
9590     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
9591   if (size == 64)
9592     ppc_object->set_opd_valid();
9593   if (size == 64 && data_shndx == ppc_object->opd_shndx())
9594     {
9595       typename Powerpc_relobj<size, big_endian>::Access_from::iterator p;
9596       for (p = ppc_object->access_from_map()->begin();
9597 	   p != ppc_object->access_from_map()->end();
9598 	   ++p)
9599 	{
9600 	  Address dst_off = p->first;
9601 	  unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9602 	  typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
9603 	  for (s = p->second.begin(); s != p->second.end(); ++s)
9604 	    {
9605 	      Relobj* src_obj = s->first;
9606 	      unsigned int src_indx = s->second;
9607 	      symtab->gc()->add_reference(src_obj, src_indx,
9608 					  ppc_object, dst_indx);
9609 	    }
9610 	  p->second.clear();
9611 	}
9612       ppc_object->access_from_map()->clear();
9613       ppc_object->process_gc_mark(symtab);
9614       // Don't look at .opd relocs as .opd will reference everything.
9615       return;
9616     }
9617 
9618   gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9619     symtab,
9620     layout,
9621     this,
9622     object,
9623     data_shndx,
9624     prelocs,
9625     reloc_count,
9626     output_section,
9627     needs_special_offset_handling,
9628     local_symbol_count,
9629     plocal_symbols);
9630 }
9631 
9632 // Handle target specific gc actions when adding a gc reference from
9633 // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX
9634 // and DST_OFF.  For powerpc64, this adds a referenc to the code
9635 // section of a function descriptor.
9636 
9637 template<int size, bool big_endian>
9638 void
do_gc_add_reference(Symbol_table * symtab,Relobj * src_obj,unsigned int src_shndx,Relobj * dst_obj,unsigned int dst_shndx,Address dst_off) const9639 Target_powerpc<size, big_endian>::do_gc_add_reference(
9640     Symbol_table* symtab,
9641     Relobj* src_obj,
9642     unsigned int src_shndx,
9643     Relobj* dst_obj,
9644     unsigned int dst_shndx,
9645     Address dst_off) const
9646 {
9647   if (size != 64 || dst_obj->is_dynamic())
9648     return;
9649 
9650   Powerpc_relobj<size, big_endian>* ppc_object
9651     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
9652   if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
9653     {
9654       if (ppc_object->opd_valid())
9655 	{
9656 	  dst_shndx = ppc_object->get_opd_ent(dst_off);
9657 	  symtab->gc()->add_reference(src_obj, src_shndx, dst_obj, dst_shndx);
9658 	}
9659       else
9660 	{
9661 	  // If we haven't run scan_opd_relocs, we must delay
9662 	  // processing this function descriptor reference.
9663 	  ppc_object->add_reference(src_obj, src_shndx, dst_off);
9664 	}
9665     }
9666 }
9667 
9668 // Add any special sections for this symbol to the gc work list.
9669 // For powerpc64, this adds the code section of a function
9670 // descriptor.
9671 
9672 template<int size, bool big_endian>
9673 void
do_gc_mark_symbol(Symbol_table * symtab,Symbol * sym) const9674 Target_powerpc<size, big_endian>::do_gc_mark_symbol(
9675     Symbol_table* symtab,
9676     Symbol* sym) const
9677 {
9678   if (size == 64 && sym->object()->pluginobj() == NULL)
9679     {
9680       Powerpc_relobj<size, big_endian>* ppc_object
9681 	= static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
9682       bool is_ordinary;
9683       unsigned int shndx = sym->shndx(&is_ordinary);
9684       if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
9685 	{
9686 	  Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
9687 	  Address dst_off = gsym->value();
9688 	  if (ppc_object->opd_valid())
9689 	    {
9690 	      unsigned int dst_indx = ppc_object->get_opd_ent(dst_off);
9691 	      symtab->gc()->worklist().push_back(Section_id(ppc_object,
9692                                                             dst_indx));
9693 	    }
9694 	  else
9695 	    ppc_object->add_gc_mark(dst_off);
9696 	}
9697     }
9698 }
9699 
9700 // For a symbol location in .opd, set LOC to the location of the
9701 // function entry.
9702 
9703 template<int size, bool big_endian>
9704 void
do_function_location(Symbol_location * loc) const9705 Target_powerpc<size, big_endian>::do_function_location(
9706     Symbol_location* loc) const
9707 {
9708   if (size == 64 && loc->shndx != 0)
9709     {
9710       if (loc->object->is_dynamic())
9711 	{
9712 	  Powerpc_dynobj<size, big_endian>* ppc_object
9713 	    = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
9714 	  if (loc->shndx == ppc_object->opd_shndx())
9715 	    {
9716 	      Address dest_off;
9717 	      Address off = loc->offset - ppc_object->opd_address();
9718 	      loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
9719 	      loc->offset = dest_off;
9720 	    }
9721 	}
9722       else
9723 	{
9724 	  const Powerpc_relobj<size, big_endian>* ppc_object
9725 	    = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
9726 	  if (loc->shndx == ppc_object->opd_shndx())
9727 	    {
9728 	      Address dest_off;
9729 	      loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
9730 	      loc->offset = dest_off;
9731 	    }
9732 	}
9733     }
9734 }
9735 
9736 // FNOFFSET in section SHNDX in OBJECT is the start of a function
9737 // compiled with -fsplit-stack.  The function calls non-split-stack
9738 // code.  Change the function to ensure it has enough stack space to
9739 // call some random function.
9740 
9741 template<int size, bool big_endian>
9742 void
do_calls_non_split(Relobj * object,unsigned int shndx,section_offset_type fnoffset,section_size_type fnsize,const unsigned char * prelocs,size_t reloc_count,unsigned char * view,section_size_type view_size,std::string * from,std::string * to) const9743 Target_powerpc<size, big_endian>::do_calls_non_split(
9744     Relobj* object,
9745     unsigned int shndx,
9746     section_offset_type fnoffset,
9747     section_size_type fnsize,
9748     const unsigned char* prelocs,
9749     size_t reloc_count,
9750     unsigned char* view,
9751     section_size_type view_size,
9752     std::string* from,
9753     std::string* to) const
9754 {
9755   // 32-bit not supported.
9756   if (size == 32)
9757     {
9758       // warn
9759       Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
9760 				 prelocs, reloc_count, view, view_size,
9761 				 from, to);
9762       return;
9763     }
9764 
9765   // The function always starts with
9766   //	ld %r0,-0x7000-64(%r13)  # tcbhead_t.__private_ss
9767   //	addis %r12,%r1,-allocate@ha
9768   //	addi %r12,%r12,-allocate@l
9769   //	cmpld %r12,%r0
9770   // but note that the addis or addi may be replaced with a nop
9771 
9772   unsigned char *entry = view + fnoffset;
9773   uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
9774 
9775   if ((insn & 0xffff0000) == addis_2_12)
9776     {
9777       /* Skip ELFv2 global entry code.  */
9778       entry += 8;
9779       insn = elfcpp::Swap<32, big_endian>::readval(entry);
9780     }
9781 
9782   unsigned char *pinsn = entry;
9783   bool ok = false;
9784   const uint32_t ld_private_ss = 0xe80d8fc0;
9785   if (insn == ld_private_ss)
9786     {
9787       int32_t allocate = 0;
9788       while (1)
9789 	{
9790 	  pinsn += 4;
9791 	  insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
9792 	  if ((insn & 0xffff0000) == addis_12_1)
9793 	    allocate += (insn & 0xffff) << 16;
9794 	  else if ((insn & 0xffff0000) == addi_12_1
9795 		   || (insn & 0xffff0000) == addi_12_12)
9796 	    allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
9797 	  else if (insn != nop)
9798 	    break;
9799 	}
9800       if (insn == cmpld_7_12_0 && pinsn == entry + 12)
9801 	{
9802 	  int extra = parameters->options().split_stack_adjust_size();
9803 	  allocate -= extra;
9804 	  if (allocate >= 0 || extra < 0)
9805 	    {
9806 	      object->error(_("split-stack stack size overflow at "
9807 			      "section %u offset %0zx"),
9808 			    shndx, static_cast<size_t>(fnoffset));
9809 	      return;
9810 	    }
9811 	  pinsn = entry + 4;
9812 	  insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
9813 	  if (insn != addis_12_1)
9814 	    {
9815 	      elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9816 	      pinsn += 4;
9817 	      insn = addi_12_12 | (allocate & 0xffff);
9818 	      if (insn != addi_12_12)
9819 		{
9820 		  elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9821 		  pinsn += 4;
9822 		}
9823 	    }
9824 	  else
9825 	    {
9826 	      insn = addi_12_1 | (allocate & 0xffff);
9827 	      elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
9828 	      pinsn += 4;
9829 	    }
9830 	  if (pinsn != entry + 12)
9831 	    elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
9832 
9833 	  ok = true;
9834 	}
9835     }
9836 
9837   if (!ok)
9838     {
9839       if (!object->has_no_split_stack())
9840 	object->error(_("failed to match split-stack sequence at "
9841 			"section %u offset %0zx"),
9842 		      shndx, static_cast<size_t>(fnoffset));
9843     }
9844 }
9845 
9846 // Scan relocations for a section.
9847 
9848 template<int size, bool big_endian>
9849 void
scan_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols)9850 Target_powerpc<size, big_endian>::scan_relocs(
9851     Symbol_table* symtab,
9852     Layout* layout,
9853     Sized_relobj_file<size, big_endian>* object,
9854     unsigned int data_shndx,
9855     unsigned int sh_type,
9856     const unsigned char* prelocs,
9857     size_t reloc_count,
9858     Output_section* output_section,
9859     bool needs_special_offset_handling,
9860     size_t local_symbol_count,
9861     const unsigned char* plocal_symbols)
9862 {
9863   typedef Target_powerpc<size, big_endian> Powerpc;
9864   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
9865       Classify_reloc;
9866 
9867   if (!this->plt_localentry0_init_)
9868     {
9869       bool plt_localentry0 = false;
9870       if (size == 64
9871 	  && this->abiversion() >= 2)
9872 	{
9873 	  if (parameters->options().user_set_plt_localentry())
9874 	    plt_localentry0 = parameters->options().plt_localentry();
9875 	  if (plt_localentry0
9876 	      && symtab->lookup("GLIBC_2.26", NULL) == NULL)
9877 	    gold_warning(_("--plt-localentry is especially dangerous without "
9878 			   "ld.so support to detect ABI violations"));
9879 	}
9880       this->plt_localentry0_ = plt_localentry0;
9881       this->plt_localentry0_init_ = true;
9882     }
9883 
9884   if (sh_type == elfcpp::SHT_REL)
9885     {
9886       gold_error(_("%s: unsupported REL reloc section"),
9887 		 object->name().c_str());
9888       return;
9889     }
9890 
9891   gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
9892     symtab,
9893     layout,
9894     this,
9895     object,
9896     data_shndx,
9897     prelocs,
9898     reloc_count,
9899     output_section,
9900     needs_special_offset_handling,
9901     local_symbol_count,
9902     plocal_symbols);
9903 
9904   if (this->plt_localentry0_ && this->power10_relocs_)
9905     {
9906       gold_warning(_("--plt-localentry is incompatible with "
9907 		     "power10 pc-relative code"));
9908       this->plt_localentry0_ = false;
9909     }
9910 }
9911 
9912 // Functor class for processing the global symbol table.
9913 // Removes symbols defined on discarded opd entries.
9914 
9915 template<bool big_endian>
9916 class Global_symbol_visitor_opd
9917 {
9918  public:
Global_symbol_visitor_opd()9919   Global_symbol_visitor_opd()
9920   { }
9921 
9922   void
operator ()(Sized_symbol<64> * sym)9923   operator()(Sized_symbol<64>* sym)
9924   {
9925     if (sym->has_symtab_index()
9926 	|| sym->source() != Symbol::FROM_OBJECT
9927 	|| !sym->in_real_elf())
9928       return;
9929 
9930     if (sym->object()->is_dynamic())
9931       return;
9932 
9933     Powerpc_relobj<64, big_endian>* symobj
9934       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
9935     if (symobj->opd_shndx() == 0)
9936       return;
9937 
9938     bool is_ordinary;
9939     unsigned int shndx = sym->shndx(&is_ordinary);
9940     if (shndx == symobj->opd_shndx()
9941 	&& symobj->get_opd_discard(sym->value()))
9942       {
9943 	sym->set_undefined();
9944 	sym->set_visibility(elfcpp::STV_DEFAULT);
9945 	sym->set_is_defined_in_discarded_section();
9946 	sym->set_symtab_index(-1U);
9947       }
9948   }
9949 };
9950 
9951 template<int size, bool big_endian>
9952 void
define_save_restore_funcs(Layout * layout,Symbol_table * symtab)9953 Target_powerpc<size, big_endian>::define_save_restore_funcs(
9954     Layout* layout,
9955     Symbol_table* symtab)
9956 {
9957   if (size == 64)
9958     {
9959       Output_data_save_res<size, big_endian>* savres
9960 	= new Output_data_save_res<size, big_endian>(symtab);
9961       this->savres_section_ = savres;
9962       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
9963 				      elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
9964 				      savres, ORDER_TEXT, false);
9965     }
9966 }
9967 
9968 // Sort linker created .got section first (for the header), then input
9969 // sections belonging to files using small model code.
9970 
9971 template<bool big_endian>
9972 class Sort_toc_sections
9973 {
9974   const Output_section_data*
small_got_section() const9975   small_got_section() const
9976   {
9977     return (static_cast<Target_powerpc<64, big_endian>*>(
9978 		parameters->sized_target<64, big_endian>())
9979 	    ->got_section(GOT_TYPE_SMALL));
9980   }
9981 
9982   int
rank(const Output_section::Input_section & isec) const9983   rank(const Output_section::Input_section& isec) const
9984   {
9985     if (!isec.is_input_section())
9986       {
9987 	if (isec.output_section_data() == this->small_got_section())
9988 	  return 0;
9989 	return 2;
9990       }
9991     if (static_cast<const Powerpc_relobj<64, big_endian>*>(isec.relobj())
9992 	->has_small_toc_reloc())
9993       return 1;
9994     return 3;
9995   }
9996 
9997  public:
9998   bool
operator ()(const Output_section::Input_section & is1,const Output_section::Input_section & is2) const9999   operator()(const Output_section::Input_section& is1,
10000 	     const Output_section::Input_section& is2) const
10001   {
10002     return rank(is1) < rank(is2);
10003   }
10004 };
10005 
10006 // Finalize the sections.
10007 
10008 template<int size, bool big_endian>
10009 void
do_finalize_sections(Layout * layout,const Input_objects * input_objects,Symbol_table * symtab)10010 Target_powerpc<size, big_endian>::do_finalize_sections(
10011     Layout* layout,
10012     const Input_objects* input_objects,
10013     Symbol_table* symtab)
10014 {
10015   if (parameters->doing_static_link())
10016     {
10017       // At least some versions of glibc elf-init.o have a strong
10018       // reference to __rela_iplt marker syms.  A weak ref would be
10019       // better..
10020       if (this->iplt_ != NULL)
10021 	{
10022 	  Reloc_section* rel = this->iplt_->rel_plt();
10023 	  symtab->define_in_output_data("__rela_iplt_start", NULL,
10024 					Symbol_table::PREDEFINED, rel, 0, 0,
10025 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10026 					elfcpp::STV_HIDDEN, 0, false, true);
10027 	  symtab->define_in_output_data("__rela_iplt_end", NULL,
10028 					Symbol_table::PREDEFINED, rel, 0, 0,
10029 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10030 					elfcpp::STV_HIDDEN, 0, true, true);
10031 	}
10032       else
10033 	{
10034 	  symtab->define_as_constant("__rela_iplt_start", NULL,
10035 				     Symbol_table::PREDEFINED, 0, 0,
10036 				     elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10037 				     elfcpp::STV_HIDDEN, 0, true, false);
10038 	  symtab->define_as_constant("__rela_iplt_end", NULL,
10039 				     Symbol_table::PREDEFINED, 0, 0,
10040 				     elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
10041 				     elfcpp::STV_HIDDEN, 0, true, false);
10042 	}
10043     }
10044 
10045   if (size == 64)
10046     {
10047       typedef Global_symbol_visitor_opd<big_endian> Symbol_visitor;
10048       symtab->for_all_symbols<64, Symbol_visitor>(Symbol_visitor());
10049 
10050       if (!parameters->options().relocatable())
10051 	{
10052 	  this->define_save_restore_funcs(layout, symtab);
10053 
10054 	  // Annoyingly, we need to make these sections now whether or
10055 	  // not we need them.  If we delay until do_relax then we
10056 	  // need to mess with the relaxation machinery checkpointing.
10057 	  this->got_section(symtab, layout, GOT_TYPE_STANDARD);
10058 	  this->make_brlt_section(layout);
10059 
10060 	  // FIXME, maybe.  Here we could run through all the got
10061 	  // entries in the small got section, removing any duplicates
10062 	  // found in the big got section and renumbering offsets.
10063 
10064 	  if (parameters->options().toc_sort())
10065 	    {
10066 	      Output_section* os = this->got_->output_section();
10067 	      if (os != NULL && os->input_sections().size() > 1)
10068 		std::stable_sort(os->input_sections().begin(),
10069 				 os->input_sections().end(),
10070 				 Sort_toc_sections<big_endian>());
10071 	    }
10072 	}
10073     }
10074 
10075   // Fill in some more dynamic tags.
10076   Output_data_dynamic* odyn = layout->dynamic_data();
10077   if (odyn != NULL)
10078     {
10079       const Reloc_section* rel_plt = (this->plt_ == NULL
10080 				      ? NULL
10081 				      : this->plt_->rel_plt());
10082       layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
10083 				      this->rela_dyn_, true, size == 32);
10084 
10085       if (size == 32)
10086 	{
10087 	  if (this->got_ != NULL)
10088 	    {
10089 	      this->got_->finalize_data_size();
10090 	      odyn->add_section_plus_offset(elfcpp::DT_PPC_GOT,
10091 					    this->got_, this->got_->g_o_t());
10092 	    }
10093 	  if (this->has_tls_get_addr_opt_)
10094 	    odyn->add_constant(elfcpp::DT_PPC_OPT, elfcpp::PPC_OPT_TLS);
10095 	}
10096       else
10097 	{
10098 	  if (this->glink_ != NULL)
10099 	    {
10100 	      this->glink_->finalize_data_size();
10101 	      odyn->add_section_plus_offset(elfcpp::DT_PPC64_GLINK,
10102 					    this->glink_,
10103 					    (this->glink_->pltresolve_size()
10104 					     - 32));
10105 	    }
10106 	  if (this->has_localentry0_ || this->has_tls_get_addr_opt_)
10107 	    odyn->add_constant(elfcpp::DT_PPC64_OPT,
10108 			       ((this->has_localentry0_
10109 				 ? elfcpp::PPC64_OPT_LOCALENTRY : 0)
10110 				| (this->has_tls_get_addr_opt_
10111 				   ? elfcpp::PPC64_OPT_TLS : 0)));
10112 	}
10113     }
10114 
10115   // Emit any relocs we saved in an attempt to avoid generating COPY
10116   // relocs.
10117   if (this->copy_relocs_.any_saved_relocs())
10118     this->copy_relocs_.emit(this->rela_dyn_section(layout));
10119 
10120   for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
10121        p != input_objects->relobj_end();
10122        ++p)
10123     {
10124       Powerpc_relobj<size, big_endian>* ppc_relobj
10125 	= static_cast<Powerpc_relobj<size, big_endian>*>(*p);
10126       if (ppc_relobj->attributes_section_data())
10127 	this->merge_object_attributes(ppc_relobj,
10128 				      ppc_relobj->attributes_section_data());
10129     }
10130   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
10131        p != input_objects->dynobj_end();
10132        ++p)
10133     {
10134       Powerpc_dynobj<size, big_endian>* ppc_dynobj
10135 	= static_cast<Powerpc_dynobj<size, big_endian>*>(*p);
10136       if (ppc_dynobj->attributes_section_data())
10137 	this->merge_object_attributes(ppc_dynobj,
10138 				      ppc_dynobj->attributes_section_data());
10139     }
10140 
10141   // Create a .gnu.attributes section if we have merged any attributes
10142   // from inputs.
10143   if (this->attributes_section_data_ != NULL
10144       && this->attributes_section_data_->size() != 0)
10145     {
10146       Output_attributes_section_data* attributes_section
10147 	= new Output_attributes_section_data(*this->attributes_section_data_);
10148       layout->add_output_section_data(".gnu.attributes",
10149 				      elfcpp::SHT_GNU_ATTRIBUTES, 0,
10150 				      attributes_section, ORDER_INVALID, false);
10151     }
10152 }
10153 
10154 // Merge object attributes from input file called NAME with those of the
10155 // output.  The input object attributes are in the object pointed by PASD.
10156 
10157 template<int size, bool big_endian>
10158 void
merge_object_attributes(const Object * obj,const Attributes_section_data * pasd)10159 Target_powerpc<size, big_endian>::merge_object_attributes(
10160     const Object* obj,
10161     const Attributes_section_data* pasd)
10162 {
10163   // Return if there is no attributes section data.
10164   if (pasd == NULL)
10165     return;
10166 
10167   // Create output object attributes.
10168   if (this->attributes_section_data_ == NULL)
10169     this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
10170 
10171   const int vendor = Object_attribute::OBJ_ATTR_GNU;
10172   const Object_attribute* in_attr = pasd->known_attributes(vendor);
10173   Object_attribute* out_attr
10174     = this->attributes_section_data_->known_attributes(vendor);
10175 
10176   const char* name = obj->name().c_str();
10177   const char* err;
10178   const char* first;
10179   const char* second;
10180   int tag = elfcpp::Tag_GNU_Power_ABI_FP;
10181   int in_fp = in_attr[tag].int_value() & 0xf;
10182   int out_fp = out_attr[tag].int_value() & 0xf;
10183   bool warn_only = obj->is_dynamic();
10184   if (in_fp != out_fp)
10185     {
10186       err = NULL;
10187       if ((in_fp & 3) == 0)
10188 	;
10189       else if ((out_fp & 3) == 0)
10190 	{
10191 	  if (!warn_only)
10192 	    {
10193 	      out_fp |= in_fp & 3;
10194 	      out_attr[tag].set_int_value(out_fp);
10195 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10196 	      this->last_fp_ = name;
10197 	    }
10198 	}
10199       else if ((out_fp & 3) != 2 && (in_fp & 3) == 2)
10200 	{
10201 	  err = N_("%s uses hard float, %s uses soft float");
10202 	  first = this->last_fp_;
10203 	  second = name;
10204 	}
10205       else if ((out_fp & 3) == 2 && (in_fp & 3) != 2)
10206 	{
10207 	  err = N_("%s uses hard float, %s uses soft float");
10208 	  first = name;
10209 	  second = this->last_fp_;
10210 	}
10211       else if ((out_fp & 3) == 1 && (in_fp & 3) == 3)
10212 	{
10213 	  err = N_("%s uses double-precision hard float, "
10214 		   "%s uses single-precision hard float");
10215 	  first = this->last_fp_;
10216 	  second = name;
10217 	}
10218       else if ((out_fp & 3) == 3 && (in_fp & 3) == 1)
10219 	{
10220 	  err = N_("%s uses double-precision hard float, "
10221 		   "%s uses single-precision hard float");
10222 	  first = name;
10223 	  second = this->last_fp_;
10224 	}
10225 
10226       if (err || (in_fp & 0xc) == 0)
10227 	;
10228       else if ((out_fp & 0xc) == 0)
10229 	{
10230 	  if (!warn_only)
10231 	    {
10232 	      out_fp |= in_fp & 0xc;
10233 	      out_attr[tag].set_int_value(out_fp);
10234 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10235 	      this->last_ld_ = name;
10236 	    }
10237 	}
10238       else if ((out_fp & 0xc) != 2 * 4 && (in_fp & 0xc) == 2 * 4)
10239 	{
10240 	  err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10241 	  first = name;
10242 	  second = this->last_ld_;
10243 	}
10244       else if ((in_fp & 0xc) != 2 * 4 && (out_fp & 0xc) == 2 * 4)
10245 	{
10246 	  err = N_("%s uses 64-bit long double, %s uses 128-bit long double");
10247 	  first = this->last_ld_;
10248 	  second = name;
10249 	}
10250       else if ((out_fp & 0xc) == 1 * 4 && (in_fp & 0xc) == 3 * 4)
10251 	{
10252 	  err = N_("%s uses IBM long double, %s uses IEEE long double");
10253 	  first = this->last_ld_;
10254 	  second = name;
10255 	}
10256       else if ((out_fp & 0xc) == 3 * 4 && (in_fp & 0xc) == 1 * 4)
10257 	{
10258 	  err = N_("%s uses IBM long double, %s uses IEEE long double");
10259 	  first = name;
10260 	  second = this->last_ld_;
10261 	}
10262 
10263       if (err)
10264 	{
10265 	  if (parameters->options().warn_mismatch())
10266 	    {
10267 	      if (warn_only)
10268 		gold_warning(_(err), first, second);
10269 	      else
10270 		gold_error(_(err), first, second);
10271 	    }
10272 	  // Arrange for this attribute to be deleted.  It's better to
10273 	  // say "don't know" about a file than to wrongly claim compliance.
10274 	  if (!warn_only)
10275 	    out_attr[tag].set_type(0);
10276 	}
10277     }
10278 
10279   if (size == 32)
10280     {
10281       tag = elfcpp::Tag_GNU_Power_ABI_Vector;
10282       int in_vec = in_attr[tag].int_value() & 3;
10283       int out_vec = out_attr[tag].int_value() & 3;
10284       if (in_vec != out_vec)
10285 	{
10286 	  err = NULL;
10287 	  if (in_vec == 0)
10288 	    ;
10289 	  else if (out_vec == 0)
10290 	    {
10291 	      out_vec = in_vec;
10292 	      out_attr[tag].set_int_value(out_vec);
10293 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10294 	      this->last_vec_ = name;
10295 	    }
10296 	  // For now, allow generic to transition to AltiVec or SPE
10297 	  // without a warning.  If GCC marked files with their stack
10298 	  // alignment and used don't-care markings for files which are
10299 	  // not affected by the vector ABI, we could warn about this
10300 	  // case too.  */
10301 	  else if (in_vec == 1)
10302 	    ;
10303 	  else if (out_vec == 1)
10304 	    {
10305 	      out_vec = in_vec;
10306 	      out_attr[tag].set_int_value(out_vec);
10307 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10308 	      this->last_vec_ = name;
10309 	    }
10310 	  else if (out_vec < in_vec)
10311 	    {
10312 	      err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10313 	      first = this->last_vec_;
10314 	      second = name;
10315 	    }
10316 	  else if (out_vec > in_vec)
10317 	    {
10318 	      err = N_("%s uses AltiVec vector ABI, %s uses SPE vector ABI");
10319 	      first = name;
10320 	      second = this->last_vec_;
10321 	    }
10322 	  if (err)
10323 	    {
10324 	      if (parameters->options().warn_mismatch())
10325 		gold_error(_(err), first, second);
10326 	      out_attr[tag].set_type(0);
10327 	    }
10328 	}
10329 
10330       tag = elfcpp::Tag_GNU_Power_ABI_Struct_Return;
10331       int in_struct = in_attr[tag].int_value() & 3;
10332       int out_struct = out_attr[tag].int_value() & 3;
10333       if (in_struct != out_struct)
10334 	{
10335 	  err = NULL;
10336 	  if (in_struct == 0 || in_struct == 3)
10337 	    ;
10338 	  else if (out_struct == 0)
10339 	    {
10340 	      out_struct = in_struct;
10341 	      out_attr[tag].set_int_value(out_struct);
10342 	      out_attr[tag].set_type(Object_attribute::ATTR_TYPE_FLAG_INT_VAL);
10343 	      this->last_struct_ = name;
10344 	    }
10345 	  else if (out_struct < in_struct)
10346 	    {
10347 	      err = N_("%s uses r3/r4 for small structure returns, "
10348 		       "%s uses memory");
10349 	      first = this->last_struct_;
10350 	      second = name;
10351 	    }
10352 	  else if (out_struct > in_struct)
10353 	    {
10354 	      err = N_("%s uses r3/r4 for small structure returns, "
10355 		       "%s uses memory");
10356 	      first = name;
10357 	      second = this->last_struct_;
10358 	    }
10359 	  if (err)
10360 	    {
10361 	      if (parameters->options().warn_mismatch())
10362 		gold_error(_(err), first, second);
10363 	      out_attr[tag].set_type(0);
10364 	    }
10365 	}
10366     }
10367 
10368   // Merge Tag_compatibility attributes and any common GNU ones.
10369   this->attributes_section_data_->merge(name, pasd);
10370 }
10371 
10372 // Emit any saved relocs, and mark toc entries using any of these
10373 // relocs as not optimizable.
10374 
10375 template<int sh_type, int size, bool big_endian>
10376 void
emit(Output_data_reloc<sh_type,true,size,big_endian> * reloc_section)10377 Powerpc_copy_relocs<sh_type, size, big_endian>::emit(
10378     Output_data_reloc<sh_type, true, size, big_endian>* reloc_section)
10379 {
10380   if (size == 64
10381       && parameters->options().toc_optimize())
10382     {
10383       for (typename Copy_relocs<sh_type, size, big_endian>::
10384 	     Copy_reloc_entries::iterator p = this->entries_.begin();
10385 	   p != this->entries_.end();
10386 	   ++p)
10387 	{
10388 	  typename Copy_relocs<sh_type, size, big_endian>::Copy_reloc_entry&
10389 	    entry = *p;
10390 
10391 	  // If the symbol is no longer defined in a dynamic object,
10392 	  // then we emitted a COPY relocation.  If it is still
10393 	  // dynamic then we'll need dynamic relocations and thus
10394 	  // can't optimize toc entries.
10395 	  if (entry.sym_->is_from_dynobj())
10396 	    {
10397 	      Powerpc_relobj<size, big_endian>* ppc_object
10398 		= static_cast<Powerpc_relobj<size, big_endian>*>(entry.relobj_);
10399 	      if (entry.shndx_ == ppc_object->toc_shndx())
10400 		ppc_object->set_no_toc_opt(entry.address_);
10401 	    }
10402 	}
10403     }
10404 
10405   Copy_relocs<sh_type, size, big_endian>::emit(reloc_section);
10406 }
10407 
10408 // Return the value to use for a branch relocation.
10409 
10410 template<int size, bool big_endian>
10411 bool
symval_for_branch(const Symbol_table * symtab,const Sized_symbol<size> * gsym,Powerpc_relobj<size,big_endian> * object,Address * value,unsigned int * dest_shndx)10412 Target_powerpc<size, big_endian>::symval_for_branch(
10413     const Symbol_table* symtab,
10414     const Sized_symbol<size>* gsym,
10415     Powerpc_relobj<size, big_endian>* object,
10416     Address *value,
10417     unsigned int *dest_shndx)
10418 {
10419   if (size == 32 || this->abiversion() >= 2)
10420     gold_unreachable();
10421   *dest_shndx = 0;
10422 
10423   // If the symbol is defined in an opd section, ie. is a function
10424   // descriptor, use the function descriptor code entry address
10425   Powerpc_relobj<size, big_endian>* symobj = object;
10426   if (gsym != NULL
10427       && (gsym->source() != Symbol::FROM_OBJECT
10428 	  || gsym->object()->is_dynamic()))
10429     return true;
10430   if (gsym != NULL)
10431     symobj = static_cast<Powerpc_relobj<size, big_endian>*>(gsym->object());
10432   unsigned int shndx = symobj->opd_shndx();
10433   if (shndx == 0)
10434     return true;
10435   Address opd_addr = symobj->get_output_section_offset(shndx);
10436   if (opd_addr == invalid_address)
10437     return true;
10438   opd_addr += symobj->output_section_address(shndx);
10439   if (*value >= opd_addr && *value < opd_addr + symobj->section_size(shndx))
10440     {
10441       Address sec_off;
10442       *dest_shndx = symobj->get_opd_ent(*value - opd_addr, &sec_off);
10443       if (symtab->is_section_folded(symobj, *dest_shndx))
10444 	{
10445 	  Section_id folded
10446 	    = symtab->icf()->get_folded_section(symobj, *dest_shndx);
10447 	  symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
10448 	  *dest_shndx = folded.second;
10449 	}
10450       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
10451       if (sec_addr == invalid_address)
10452 	return false;
10453 
10454       sec_addr += symobj->output_section(*dest_shndx)->address();
10455       *value = sec_addr + sec_off;
10456     }
10457   return true;
10458 }
10459 
10460 template<int size>
10461 static bool
relative_value_is_known(const Sized_symbol<size> * gsym)10462 relative_value_is_known(const Sized_symbol<size>* gsym)
10463 {
10464   if (gsym->type() == elfcpp::STT_GNU_IFUNC)
10465     return false;
10466 
10467   if (gsym->is_from_dynobj()
10468       || gsym->is_undefined()
10469       || gsym->is_preemptible())
10470     return false;
10471 
10472   if (gsym->is_absolute())
10473     return !parameters->options().output_is_position_independent();
10474 
10475   return true;
10476 }
10477 
10478 template<int size>
10479 static bool
relative_value_is_known(const Symbol_value<size> * psymval)10480 relative_value_is_known(const Symbol_value<size>* psymval)
10481 {
10482   if (psymval->is_ifunc_symbol())
10483     return false;
10484 
10485   bool is_ordinary;
10486   unsigned int shndx = psymval->input_shndx(&is_ordinary);
10487 
10488   return is_ordinary && shndx != elfcpp::SHN_UNDEF;
10489 }
10490 
10491 // PCREL_OPT in one instance flags to the linker that a pair of insns:
10492 //   pld ra,symbol@got@pcrel
10493 //   load/store rt,0(ra)
10494 // or
10495 //   pla ra,symbol@pcrel
10496 //   load/store rt,0(ra)
10497 // may be translated to
10498 //   pload/pstore rt,symbol@pcrel
10499 //   nop.
10500 // This function returns true if the optimization is possible, placing
10501 // the prefix insn in *PINSN1 and a NOP in *PINSN2.
10502 //
10503 // On entry to this function, the linker has already determined that
10504 // the pld can be replaced with pla: *PINSN1 is that pla insn,
10505 // while *PINSN2 is the second instruction.
10506 
10507 inline bool
xlate_pcrel_opt(uint64_t * pinsn1,uint64_t * pinsn2)10508 xlate_pcrel_opt(uint64_t *pinsn1, uint64_t *pinsn2)
10509 {
10510   uint32_t insn2 = *pinsn2 >> 32;
10511   uint64_t i1new;
10512 
10513   // Check that regs match.
10514   if (((insn2 >> 16) & 31) != ((*pinsn1 >> 21) & 31))
10515     return false;
10516 
10517   switch ((insn2 >> 26) & 63)
10518     {
10519     default:
10520       return false;
10521 
10522     case 32: // lwz
10523     case 34: // lbz
10524     case 36: // stw
10525     case 38: // stb
10526     case 40: // lhz
10527     case 42: // lha
10528     case 44: // sth
10529     case 48: // lfs
10530     case 50: // lfd
10531     case 52: // stfs
10532     case 54: // stfd
10533       // These are the PMLS cases, where we just need to tack a prefix
10534       // on the insn.  Check that the D field is zero.
10535       if ((insn2 & 0xffff) != 0)
10536 	return false;
10537       i1new = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
10538 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10539       break;
10540 
10541     case 58: // lwa, ld
10542       if ((insn2 & 0xfffd) != 0)
10543 	return false;
10544       i1new = ((1ULL << 58) | (1ULL << 52)
10545 	       | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
10546 	       | (insn2 & (31ULL << 21)));
10547       break;
10548 
10549     case 57: // lxsd, lxssp
10550       if ((insn2 & 0xfffc) != 0 || (insn2 & 3) < 2)
10551 	return false;
10552       i1new = ((1ULL << 58) | (1ULL << 52)
10553 	       | ((40ULL | (insn2 & 3)) << 26)
10554 	       | (insn2 & (31ULL << 21)));
10555       break;
10556 
10557     case 61: // stxsd, stxssp, lxv, stxv
10558       if ((insn2 & 3) == 0)
10559 	return false;
10560       else if ((insn2 & 3) >= 2)
10561 	{
10562 	  if ((insn2 & 0xfffc) != 0)
10563 	    return false;
10564 	  i1new = ((1ULL << 58) | (1ULL << 52)
10565 		   | ((44ULL | (insn2 & 3)) << 26)
10566 		   | (insn2 & (31ULL << 21)));
10567 	}
10568       else
10569 	{
10570 	  if ((insn2 & 0xfff0) != 0)
10571 	    return false;
10572 	  i1new = ((1ULL << 58) | (1ULL << 52)
10573 		   | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
10574 		   | (insn2 & (31ULL << 21)));
10575 	}
10576       break;
10577 
10578     case 56: // lq
10579       if ((insn2 & 0xffff) != 0)
10580 	return false;
10581       i1new = ((1ULL << 58) | (1ULL << 52)
10582 	       | (insn2 & ((63ULL << 26) | (31ULL << 21))));
10583       break;
10584 
10585     case 62: // std, stq
10586       if ((insn2 & 0xfffd) != 0)
10587 	return false;
10588       i1new = ((1ULL << 58) | (1ULL << 52)
10589 	       | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
10590 	       | (insn2 & (31ULL << 21)));
10591       break;
10592     }
10593 
10594   *pinsn1 = i1new;
10595   *pinsn2 = (uint64_t) nop << 32;
10596   return true;
10597 }
10598 
10599 // Perform a relocation.
10600 
10601 template<int size, bool big_endian>
10602 inline bool
relocate(const Relocate_info<size,big_endian> * relinfo,unsigned int,Target_powerpc * target,Output_section * os,size_t relnum,const unsigned char * preloc,const Sized_symbol<size> * gsym,const Symbol_value<size> * psymval,unsigned char * view,Address address,section_size_type view_size)10603 Target_powerpc<size, big_endian>::Relocate::relocate(
10604     const Relocate_info<size, big_endian>* relinfo,
10605     unsigned int,
10606     Target_powerpc* target,
10607     Output_section* os,
10608     size_t relnum,
10609     const unsigned char* preloc,
10610     const Sized_symbol<size>* gsym,
10611     const Symbol_value<size>* psymval,
10612     unsigned char* view,
10613     Address address,
10614     section_size_type view_size)
10615 {
10616   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
10617   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
10618   typedef typename elfcpp::Rela<size, big_endian> Reltype;
10619 
10620   if (view == NULL)
10621     return true;
10622 
10623   if (target->replace_tls_get_addr(gsym))
10624     gsym = static_cast<const Sized_symbol<size>*>(target->tls_get_addr_opt());
10625 
10626   const elfcpp::Rela<size, big_endian> rela(preloc);
10627   unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
10628   Powerpc_relobj<size, big_endian>* const object
10629     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
10630   switch (this->maybe_skip_tls_get_addr_call(target, r_type, gsym))
10631     {
10632     case Track_tls::NOT_EXPECTED:
10633       // No warning.  This will result in really old code without tls
10634       // marker relocs being mis-optimised, but there shouldn't be too
10635       // much of that code around.  The problem with warning is that
10636       // glibc and libphobos both construct direct calls to
10637       // __tls_get_addr in a way that is harmless.
10638       break;
10639     case Track_tls::EXPECTED:
10640       // We have already complained.
10641       break;
10642     case Track_tls::SKIP:
10643       if (is_plt16_reloc<size>(r_type)
10644 	  || r_type == elfcpp::R_POWERPC_PLTSEQ
10645 	  || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC)
10646 	{
10647 	  Insn* iview = reinterpret_cast<Insn*>(view);
10648 	  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10649 	}
10650       else if (size == 64 && r_type == elfcpp::R_POWERPC_PLTCALL)
10651 	{
10652 	  Insn* iview = reinterpret_cast<Insn*>(view);
10653 	  elfcpp::Swap<32, big_endian>::writeval(iview + 1, nop);
10654 	}
10655       else if (size == 64 && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10656 			      || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10657 	{
10658 	  Insn* iview = reinterpret_cast<Insn*>(view);
10659 	  elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10660 	  elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10661 	}
10662       return true;
10663     case Track_tls::NORMAL:
10664       break;
10665     }
10666 
10667   // Offset from start of insn to d-field reloc.
10668   const int d_offset = big_endian ? 2 : 0;
10669 
10670   Address value = 0;
10671   bool has_stub_value = false;
10672   bool localentry0 = false;
10673   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
10674   bool use_plt_offset
10675     = (gsym != NULL
10676        ? gsym->use_plt_offset(Scan::get_reference_flags(r_type, target))
10677        : object->local_has_plt_offset(r_sym));
10678   if (is_plt16_reloc<size>(r_type)
10679       || r_type == elfcpp::R_PPC64_PLT_PCREL34
10680       || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10681       || r_type == elfcpp::R_POWERPC_PLTSEQ
10682       || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC
10683       || r_type == elfcpp::R_POWERPC_PLTCALL
10684       || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
10685     {
10686       // It would be possible to replace inline plt calls with direct
10687       // calls if the PLTCALL is in range.  The only difficulty is
10688       // that the decision depends on the PLTCALL reloc, and we don't
10689       // know the address of that instruction when processing others
10690       // in the sequence.  So the decision needs to be made in
10691       // do_relax().  For now, don't optimise inline plt calls.
10692       if (gsym)
10693 	use_plt_offset = gsym->has_plt_offset();
10694     }
10695   if (use_plt_offset
10696       && !is_got_reloc(r_type)
10697       && !is_plt16_reloc<size>(r_type)
10698       && r_type != elfcpp::R_PPC64_PLT_PCREL34
10699       && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC
10700       && r_type != elfcpp::R_POWERPC_PLTSEQ
10701       && r_type != elfcpp::R_POWERPC_PLTCALL
10702       && r_type != elfcpp::R_PPC64_PLTSEQ_NOTOC
10703       && r_type != elfcpp::R_PPC64_PLTCALL_NOTOC
10704       && (!psymval->is_ifunc_symbol()
10705 	  || Scan::reloc_needs_plt_for_ifunc(target, object, r_type, false)))
10706     {
10707       if (size == 64
10708 	  && gsym != NULL
10709 	  && target->abiversion() >= 2
10710 	  && !parameters->options().output_is_position_independent()
10711 	  && !is_branch_reloc<size>(r_type))
10712 	{
10713 	  Address off = target->glink_section()->find_global_entry(gsym);
10714 	  if (off != invalid_address)
10715 	    {
10716 	      value = target->glink_section()->global_entry_address() + off;
10717 	      has_stub_value = true;
10718 	    }
10719 	}
10720       else
10721 	{
10722 	  Stub_table<size, big_endian>* stub_table = NULL;
10723 	  if (target->stub_tables().size() == 1)
10724 	    stub_table = target->stub_tables()[0];
10725 	  if (stub_table == NULL
10726 	      && !(size == 32
10727 		   && gsym != NULL
10728 		   && !parameters->options().output_is_position_independent()
10729 		   && !is_branch_reloc<size>(r_type)))
10730 	    stub_table = object->stub_table(relinfo->data_shndx);
10731 	  if (stub_table == NULL)
10732 	    {
10733 	      // This is a ref from a data section to an ifunc symbol,
10734 	      // or a non-branch reloc for which we always want to use
10735 	      // one set of stubs for resolving function addresses.
10736 	      if (target->stub_tables().size() != 0)
10737 		stub_table = target->stub_tables()[0];
10738 	    }
10739 	  if (stub_table != NULL)
10740 	    {
10741 	      const typename Stub_table<size, big_endian>::Plt_stub_ent* ent;
10742 	      if (gsym != NULL)
10743 		ent = stub_table->find_plt_call_entry(object, gsym, r_type,
10744 						      rela.get_r_addend());
10745 	      else
10746 		ent = stub_table->find_plt_call_entry(object, r_sym, r_type,
10747 						      rela.get_r_addend());
10748 	      if (ent != NULL)
10749 		{
10750 		  value = stub_table->stub_address() + ent->off_;
10751 		  const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
10752 		  elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
10753 		  size_t reloc_count = shdr.get_sh_size() / reloc_size;
10754 		  if (size == 64)
10755 		    {
10756 		      if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
10757 			{
10758 			  if (!ent->notoc_)
10759 			    value += ent->p9off_;
10760 			}
10761 		      else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10762 			value += ent->p9off_;
10763 		      else
10764 			value += ent->tocoff_;
10765 		    }
10766 		  if (size == 64
10767 		      && ent->r2save_
10768 		      && !(gsym != NULL
10769 			   && target->is_tls_get_addr_opt(gsym)))
10770 		    {
10771 		      if (r_type == elfcpp::R_PPC64_REL24_NOTOC
10772 			  || r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
10773 			{
10774 			  if (!(target->power10_stubs()
10775 				&& target->power10_stubs_auto()))
10776 			    value += 4;
10777 			}
10778 		      else if (relnum < reloc_count - 1)
10779 			{
10780 			  Reltype next_rela(preloc + reloc_size);
10781 			  if (elfcpp::elf_r_type<size>(next_rela.get_r_info())
10782 			      == elfcpp::R_PPC64_TOCSAVE
10783 			      && (next_rela.get_r_offset()
10784 				  == rela.get_r_offset() + 4))
10785 			    value += 4;
10786 			}
10787 		    }
10788 		  localentry0 = ent->localentry0_;
10789 		  has_stub_value = true;
10790 		}
10791 	    }
10792 	}
10793       // We don't care too much about bogus debug references to
10794       // non-local functions, but otherwise there had better be a plt
10795       // call stub or global entry stub as appropriate.
10796       gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
10797     }
10798 
10799   if (use_plt_offset && (is_plt16_reloc<size>(r_type)
10800 			 || r_type == elfcpp::R_PPC64_PLT_PCREL34
10801 			 || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10802     {
10803       const Output_data_plt_powerpc<size, big_endian>* plt;
10804       if (gsym)
10805 	value = target->plt_off(gsym, &plt);
10806       else
10807 	value = target->plt_off(object, r_sym, &plt);
10808       value += plt->address();
10809 
10810       if (size == 64)
10811 	{
10812 	  if (r_type != elfcpp::R_PPC64_PLT_PCREL34
10813 	      && r_type != elfcpp::R_PPC64_PLT_PCREL34_NOTOC)
10814 	    value -= target->toc_pointer();
10815 	}
10816       else if (parameters->options().output_is_position_independent())
10817 	{
10818 	  if (rela.get_r_addend() >= 32768)
10819 	    {
10820 	      unsigned int got2 = object->got2_shndx();
10821 	      value -= (object->get_output_section_offset(got2)
10822 			+ object->output_section(got2)->address()
10823 			+ rela.get_r_addend());
10824 	    }
10825 	  else
10826 	    value -= target->toc_pointer();
10827 	}
10828     }
10829   else if (!use_plt_offset
10830 	   && (is_plt16_reloc<size>(r_type)
10831 	       || r_type == elfcpp::R_POWERPC_PLTSEQ
10832 	       || r_type == elfcpp::R_PPC64_PLTSEQ_NOTOC))
10833     {
10834       Insn* iview = reinterpret_cast<Insn*>(view);
10835       elfcpp::Swap<32, big_endian>::writeval(iview, nop);
10836       r_type = elfcpp::R_POWERPC_NONE;
10837     }
10838   else if (!use_plt_offset
10839 	   && (r_type == elfcpp::R_PPC64_PLT_PCREL34
10840 	       || r_type == elfcpp::R_PPC64_PLT_PCREL34_NOTOC))
10841     {
10842       Insn* iview = reinterpret_cast<Insn*>(view);
10843       elfcpp::Swap<32, big_endian>::writeval(iview, pnop >> 32);
10844       elfcpp::Swap<32, big_endian>::writeval(iview + 1, pnop & 0xffffffff);
10845       r_type = elfcpp::R_POWERPC_NONE;
10846     }
10847   else if (is_got_reloc(r_type))
10848     {
10849       uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10850       Got_type got_type = ((size == 32
10851 			    || r_type == elfcpp::R_POWERPC_GOT16
10852 			    || r_type == elfcpp::R_PPC64_GOT16_DS)
10853 			   ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10854       if (gsym != NULL)
10855 	value = gsym->got_offset(got_type, addend);
10856       else
10857 	value = object->local_got_offset(r_sym, got_type, addend);
10858       if (r_type == elfcpp::R_PPC64_GOT_PCREL34)
10859 	value += target->got_section(got_type)->address();
10860       else
10861 	value -= target->got_base_offset(got_type);
10862     }
10863   else if (r_type == elfcpp::R_PPC64_TOC)
10864     {
10865       value = target->toc_pointer();
10866     }
10867   else if (gsym != NULL
10868 	   && (r_type == elfcpp::R_POWERPC_REL24
10869 	       || r_type == elfcpp::R_PPC_PLTREL24)
10870 	   && has_stub_value)
10871     {
10872       if (size == 64)
10873 	{
10874 	  typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
10875 	  Valtype* wv = reinterpret_cast<Valtype*>(view);
10876 	  bool can_plt_call = localentry0 || target->is_tls_get_addr_opt(gsym);
10877 	  if (!can_plt_call && rela.get_r_offset() + 8 <= view_size)
10878 	    {
10879 	      Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
10880 	      Valtype insn2 = elfcpp::Swap<32, big_endian>::readval(wv + 1);
10881 	      if ((insn & 1) != 0
10882 		  && (insn2 == nop
10883 		      || insn2 == cror_15_15_15 || insn2 == cror_31_31_31))
10884 		{
10885 		  elfcpp::Swap<32, big_endian>::
10886 		    writeval(wv + 1, ld_2_1 + target->stk_toc());
10887 		  can_plt_call = true;
10888 		}
10889 	    }
10890 	  if (!can_plt_call)
10891 	    {
10892 	      // If we don't have a branch and link followed by a nop,
10893 	      // we can't go via the plt because there is no place to
10894 	      // put a toc restoring instruction.
10895 	      // Unless we know we won't be returning.
10896 	      if (strcmp(gsym->name(), "__libc_start_main") == 0)
10897 		can_plt_call = true;
10898 	    }
10899 	  if (!can_plt_call)
10900 	    {
10901 	      // g++ as of 20130507 emits self-calls without a
10902 	      // following nop.  This is arguably wrong since we have
10903 	      // conflicting information.  On the one hand a global
10904 	      // symbol and on the other a local call sequence, but
10905 	      // don't error for this special case.
10906 	      // It isn't possible to cheaply verify we have exactly
10907 	      // such a call.  Allow all calls to the same section.
10908 	      bool ok = false;
10909 	      Address code = value;
10910 	      if (gsym->source() == Symbol::FROM_OBJECT
10911 		  && gsym->object() == object)
10912 		{
10913 		  unsigned int dest_shndx = 0;
10914 		  if (target->abiversion() < 2)
10915 		    {
10916 		      Address addend = rela.get_r_addend();
10917 		      code = psymval->value(object, addend);
10918 		      target->symval_for_branch(relinfo->symtab, gsym, object,
10919 						&code, &dest_shndx);
10920 		    }
10921 		  bool is_ordinary;
10922 		  if (dest_shndx == 0)
10923 		    dest_shndx = gsym->shndx(&is_ordinary);
10924 		  ok = dest_shndx == relinfo->data_shndx;
10925 		}
10926 	      if (!ok)
10927 		{
10928 		  gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
10929 					 _("call lacks nop, can't restore toc; "
10930 					   "recompile with -fPIC"));
10931 		  value = code;
10932 		}
10933 	    }
10934 	}
10935     }
10936   else if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10937 	   || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
10938 	   || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
10939 	   || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA
10940 	   || r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10941     {
10942       // First instruction of a global dynamic sequence, arg setup insn.
10943       bool final = gsym == NULL || gsym->final_value_is_known();
10944       tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
10945       Got_type got_type = ((size == 32
10946 			    || r_type == elfcpp::R_POWERPC_GOT_TLSGD16)
10947 			   ? GOT_TYPE_SMALL : GOT_TYPE_STANDARD);
10948       if (tls_type == tls::TLSOPT_NONE)
10949 	got_type = Got_type(got_type | GOT_TYPE_TLSGD);
10950       else if (tls_type == tls::TLSOPT_TO_IE)
10951 	got_type = Got_type(got_type | GOT_TYPE_TPREL);
10952       if ((got_type & ~GOT_TYPE_SMALL) != GOT_TYPE_STANDARD)
10953 	{
10954 	  uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
10955 	  if (gsym != NULL)
10956 	    value = gsym->got_offset(got_type, addend);
10957 	  else
10958 	    value = object->local_got_offset(r_sym, got_type, addend);
10959 	  if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10960 	    value += target->got_section(got_type)->address();
10961 	  else
10962 	    value -= target->got_base_offset(got_type);
10963 	}
10964       if (tls_type == tls::TLSOPT_TO_IE)
10965 	{
10966 	  if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
10967 	    {
10968 	      Insn* iview = reinterpret_cast<Insn*>(view);
10969 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
10970 	      pinsn <<= 32;
10971 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
10972 	      // pla -> pld
10973 	      pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
10974 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
10975 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
10976 						     pinsn & 0xffffffff);
10977 	      r_type = elfcpp::R_PPC64_GOT_TPREL_PCREL34;
10978 	    }
10979 	  else
10980 	    {
10981 	      if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
10982 		  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
10983 		{
10984 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
10985 		  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
10986 		  insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
10987 		  if (size == 32)
10988 		    insn |= 32 << 26; // lwz
10989 		  else
10990 		    insn |= 58 << 26; // ld
10991 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
10992 		}
10993 	      r_type += (elfcpp::R_POWERPC_GOT_TPREL16
10994 			 - elfcpp::R_POWERPC_GOT_TLSGD16);
10995 	    }
10996 	}
10997       else if (tls_type == tls::TLSOPT_TO_LE)
10998 	{
10999 	  if (r_type == elfcpp::R_PPC64_GOT_TLSGD_PCREL34)
11000 	    {
11001 	      Insn* iview = reinterpret_cast<Insn*>(view);
11002 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11003 	      pinsn <<= 32;
11004 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11005 	      // pla pcrel -> paddi r13
11006 	      pinsn += (-1ULL << 52) + (13ULL << 16);
11007 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11008 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11009 						     pinsn & 0xffffffff);
11010 	      r_type = elfcpp::R_PPC64_TPREL34;
11011 	      value = psymval->value(object, rela.get_r_addend());
11012 	    }
11013 	  else
11014 	    {
11015 	      if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
11016 		  || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
11017 		{
11018 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11019 		  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11020 		  insn &= (1 << 26) - (1 << 21); // extract rt
11021 		  if (size == 32)
11022 		    insn |= addis_0_2;
11023 		  else
11024 		    insn |= addis_0_13;
11025 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11026 		  r_type = elfcpp::R_POWERPC_TPREL16_HA;
11027 		  value = psymval->value(object, rela.get_r_addend());
11028 		}
11029 	      else
11030 		{
11031 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11032 		  Insn insn = nop;
11033 		  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11034 		  r_type = elfcpp::R_POWERPC_NONE;
11035 		}
11036 	    }
11037 	}
11038     }
11039   else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11040 	   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
11041 	   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
11042 	   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA
11043 	   || r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11044     {
11045       // First instruction of a local dynamic sequence, arg setup insn.
11046       tls::Tls_optimization tls_type = target->optimize_tls_ld();
11047       if (tls_type == tls::TLSOPT_NONE)
11048 	{
11049 	  value = target->tlsld_got_offset();
11050 	  if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11051 	    value += target->got_section(GOT_TYPE_SMALL)->address();
11052 	  else
11053 	    value -= target->got_base_offset(GOT_TYPE_SMALL);
11054 	}
11055       else
11056 	{
11057 	  gold_assert(tls_type == tls::TLSOPT_TO_LE);
11058 	  if (r_type == elfcpp::R_PPC64_GOT_TLSLD_PCREL34)
11059 	    {
11060 	      Insn* iview = reinterpret_cast<Insn*>(view);
11061 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11062 	      pinsn <<= 32;
11063 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11064 	      // pla pcrel -> paddi r13
11065 	      pinsn += (-1ULL << 52) + (13ULL << 16);
11066 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11067 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11068 						     pinsn & 0xffffffff);
11069 	      r_type = elfcpp::R_PPC64_TPREL34;
11070 	      value = dtp_offset;
11071 	    }
11072 	  else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
11073 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
11074 	    {
11075 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11076 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11077 	      insn &= (1 << 26) - (1 << 21); // extract rt
11078 	      if (size == 32)
11079 		insn |= addis_0_2;
11080 	      else
11081 		insn |= addis_0_13;
11082 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11083 	      r_type = elfcpp::R_POWERPC_TPREL16_HA;
11084 	      value = dtp_offset;
11085 	    }
11086 	  else
11087 	    {
11088 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11089 	      Insn insn = nop;
11090 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11091 	      r_type = elfcpp::R_POWERPC_NONE;
11092 	    }
11093 	}
11094     }
11095   else if (r_type == elfcpp::R_POWERPC_GOT_DTPREL16
11096 	   || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_LO
11097 	   || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HI
11098 	   || r_type == elfcpp::R_POWERPC_GOT_DTPREL16_HA
11099 	   || r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11100     {
11101       // Accesses relative to a local dynamic sequence address,
11102       // no optimisation here.
11103       uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11104       Got_type got_type = ((size == 32
11105 			    || r_type == elfcpp::R_POWERPC_GOT_DTPREL16)
11106 			   ? GOT_TYPE_SMALL_DTPREL : GOT_TYPE_DTPREL);
11107       if (gsym != NULL)
11108 	value = gsym->got_offset(got_type, addend);
11109       else
11110 	value = object->local_got_offset(r_sym, got_type, addend);
11111       if (r_type == elfcpp::R_PPC64_GOT_DTPREL_PCREL34)
11112 	value += target->got_section(got_type)->address();
11113       else
11114 	value -= target->got_base_offset(got_type);
11115     }
11116   else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11117 	   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
11118 	   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
11119 	   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA
11120 	   || r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11121     {
11122       // First instruction of initial exec sequence.
11123       bool final = gsym == NULL || gsym->final_value_is_known();
11124       tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11125       if (tls_type == tls::TLSOPT_NONE)
11126 	{
11127 	  uint64_t addend = size == 32 ? 0 : rela.get_r_addend();
11128 	  Got_type got_type = ((size == 32
11129 				|| r_type == elfcpp::R_POWERPC_GOT_TPREL16)
11130 			       ? GOT_TYPE_SMALL_TPREL : GOT_TYPE_TPREL);
11131 	  if (gsym != NULL)
11132 	    value = gsym->got_offset(got_type, addend);
11133 	  else
11134 	    value = object->local_got_offset(r_sym, got_type, addend);
11135 	  if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11136 	    value += target->got_section(got_type)->address();
11137 	  else
11138 	    value -= target->got_base_offset(got_type);
11139 	}
11140       else
11141 	{
11142 	  gold_assert(tls_type == tls::TLSOPT_TO_LE);
11143 	  if (r_type == elfcpp::R_PPC64_GOT_TPREL_PCREL34)
11144 	    {
11145 	      Insn* iview = reinterpret_cast<Insn*>(view);
11146 	      uint64_t pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11147 	      pinsn <<= 32;
11148 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11149 	      // pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel
11150 	      pinsn += ((2ULL << 56) + (-1ULL << 52)
11151 			+ (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
11152 	      elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11153 	      elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11154 						     pinsn & 0xffffffff);
11155 	      r_type = elfcpp::R_PPC64_TPREL34;
11156 	      value = psymval->value(object, rela.get_r_addend());
11157 	    }
11158 	  else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
11159 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
11160 	    {
11161 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11162 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11163 	      insn &= (1 << 26) - (1 << 21); // extract rt from ld
11164 	      if (size == 32)
11165 		insn |= addis_0_2;
11166 	      else
11167 		insn |= addis_0_13;
11168 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11169 	      r_type = elfcpp::R_POWERPC_TPREL16_HA;
11170 	      value = psymval->value(object, rela.get_r_addend());
11171 	    }
11172 	  else
11173 	    {
11174 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11175 	      Insn insn = nop;
11176 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11177 	      r_type = elfcpp::R_POWERPC_NONE;
11178 	    }
11179 	}
11180     }
11181   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
11182 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
11183     {
11184       // Second instruction of a global dynamic sequence,
11185       // the __tls_get_addr call
11186       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11187       bool final = gsym == NULL || gsym->final_value_is_known();
11188       tls::Tls_optimization tls_type =  target->optimize_tls_gd(final);
11189       if (tls_type != tls::TLSOPT_NONE)
11190 	{
11191 	  if (tls_type == tls::TLSOPT_TO_IE)
11192 	    {
11193 	      Insn* iview = reinterpret_cast<Insn*>(view);
11194 	      Insn insn = add_3_3_13;
11195 	      if (size == 32)
11196 		insn = add_3_3_2;
11197 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11198 	      r_type = elfcpp::R_POWERPC_NONE;
11199 	    }
11200 	  else
11201 	    {
11202 	      bool is_pcrel = false;
11203 	      const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11204 	      elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11205 	      size_t reloc_count = shdr.get_sh_size() / reloc_size;
11206 	      if (relnum < reloc_count - 1)
11207 		{
11208 		  Reltype next_rela(preloc + reloc_size);
11209 		  unsigned int r_type2
11210 		    = elfcpp::elf_r_type<size>(next_rela.get_r_info());
11211 		  if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11212 		       || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11213 		       || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11214 		      && next_rela.get_r_offset() == rela.get_r_offset())
11215 		    is_pcrel = true;
11216 		}
11217 	      Insn* iview = reinterpret_cast<Insn*>(view);
11218 	      if (is_pcrel)
11219 		{
11220 		  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11221 		  r_type = elfcpp::R_POWERPC_NONE;
11222 		}
11223 	      else
11224 		{
11225 		  elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11226 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
11227 		  view += d_offset;
11228 		  value = psymval->value(object, rela.get_r_addend());
11229 		}
11230 	    }
11231 	  this->skip_next_tls_get_addr_call();
11232 	}
11233     }
11234   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
11235 	   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
11236     {
11237       // Second instruction of a local dynamic sequence,
11238       // the __tls_get_addr call
11239       this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
11240       tls::Tls_optimization tls_type = target->optimize_tls_ld();
11241       if (tls_type == tls::TLSOPT_TO_LE)
11242 	{
11243 	  bool is_pcrel = false;
11244 	  const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11245 	  elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11246 	  size_t reloc_count = shdr.get_sh_size() / reloc_size;
11247 	  if (relnum < reloc_count - 1)
11248 	    {
11249 	      Reltype next_rela(preloc + reloc_size);
11250 	      unsigned int r_type2
11251 		= elfcpp::elf_r_type<size>(next_rela.get_r_info());
11252 	      if ((r_type2 == elfcpp::R_PPC64_REL24_NOTOC
11253 		   || r_type2 == elfcpp::R_PPC64_REL24_P9NOTOC
11254 		   || r_type2 == elfcpp::R_PPC64_PLTCALL_NOTOC)
11255 		  && next_rela.get_r_offset() == rela.get_r_offset())
11256 		is_pcrel = true;
11257 	    }
11258 	  Insn* iview = reinterpret_cast<Insn*>(view);
11259 	  if (is_pcrel)
11260 	    {
11261 	      elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11262 	      r_type = elfcpp::R_POWERPC_NONE;
11263 	    }
11264 	  else
11265 	    {
11266 	      elfcpp::Swap<32, big_endian>::writeval(iview, addi_3_3);
11267 	      r_type = elfcpp::R_POWERPC_TPREL16_LO;
11268 	      view += d_offset;
11269 	      value = dtp_offset;
11270 	    }
11271 	  this->skip_next_tls_get_addr_call();
11272 	}
11273     }
11274   else if (r_type == elfcpp::R_POWERPC_TLS)
11275     {
11276       // Second instruction of an initial exec sequence
11277       bool final = gsym == NULL || gsym->final_value_is_known();
11278       tls::Tls_optimization tls_type = target->optimize_tls_ie(final);
11279       if (tls_type == tls::TLSOPT_TO_LE)
11280 	{
11281 	  Address roff = rela.get_r_offset() & 3;
11282 	  Insn* iview = reinterpret_cast<Insn*>(view - roff);
11283 	  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11284 	  unsigned int reg = size == 32 ? 2 : 13;
11285 	  insn = at_tls_transform(insn, reg);
11286 	  gold_assert(insn != 0);
11287 	  if (roff == 0)
11288 	    {
11289 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11290 	      r_type = elfcpp::R_POWERPC_TPREL16_LO;
11291 	      view += d_offset;
11292 	      value = psymval->value(object, rela.get_r_addend());
11293 	    }
11294 	  else if (roff == 1)
11295 	    {
11296 	      // For pcrel IE to LE we already have the full offset
11297 	      // and thus don't need an addi here.  A nop or mr will do.
11298 	      if ((insn & (0x3f << 26)) == 14 << 26)
11299 		{
11300 		  // Extract regs from addi rt,ra,si.
11301 		  unsigned int rt = (insn >> 21) & 0x1f;
11302 		  unsigned int ra = (insn >> 16) & 0x1f;
11303 		  if (rt == ra)
11304 		    insn = nop;
11305 		  else
11306 		    {
11307 		      // Build or ra,rs,rb with rb==rs, ie. mr ra,rs.
11308 		      insn = (rt << 16) | (ra << 21) | (ra << 11);
11309 		      insn |= (31u << 26) | (444u << 1);
11310 		    }
11311 		}
11312 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11313 	      r_type = elfcpp::R_POWERPC_NONE;
11314 	    }
11315 	}
11316     }
11317   else if (!has_stub_value)
11318     {
11319       if (!use_plt_offset && (r_type == elfcpp::R_POWERPC_PLTCALL
11320 			      || r_type == elfcpp::R_PPC64_PLTCALL_NOTOC))
11321 	{
11322 	  // PLTCALL without plt entry => convert to direct call
11323 	  Insn* iview = reinterpret_cast<Insn*>(view);
11324 	  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11325 	  insn = (insn & 1) | b;
11326 	  elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11327 	  if (size == 32)
11328 	    r_type = elfcpp::R_PPC_PLTREL24;
11329 	  else if (r_type == elfcpp::R_PPC64_PLTCALL_NOTOC)
11330 	    r_type = elfcpp::R_PPC64_REL24_NOTOC;
11331 	  else
11332 	    r_type = elfcpp::R_POWERPC_REL24;
11333 	}
11334       Address addend = 0;
11335       if (!(size == 32
11336 	    && (r_type == elfcpp::R_PPC_PLTREL24
11337 		|| r_type == elfcpp::R_POWERPC_PLT16_LO
11338 		|| r_type == elfcpp::R_POWERPC_PLT16_HI
11339 		|| r_type == elfcpp::R_POWERPC_PLT16_HA)))
11340 	addend = rela.get_r_addend();
11341       value = psymval->value(object, addend);
11342       unsigned int local_ent = 0;
11343       if (size == 64 && is_branch_reloc<size>(r_type))
11344 	{
11345 	  if (target->abiversion() >= 2)
11346 	    {
11347 	      if (gsym != NULL)
11348 		local_ent = object->ppc64_local_entry_offset(gsym);
11349 	      else
11350 		local_ent = object->ppc64_local_entry_offset(r_sym);
11351 	    }
11352 	  else
11353 	    {
11354 	      unsigned int dest_shndx;
11355 	      target->symval_for_branch(relinfo->symtab, gsym, object,
11356 					&value, &dest_shndx);
11357 	    }
11358 	}
11359       Address max_branch = max_branch_delta<size>(r_type);
11360       if (max_branch != 0
11361 	  && (value + local_ent - address + max_branch >= 2 * max_branch
11362 	      || (size == 64
11363 		  && (r_type == elfcpp::R_PPC64_REL24_NOTOC
11364 		      || r_type == elfcpp::R_PPC64_REL24_NOTOC)
11365 		  && (gsym != NULL
11366 		      ? object->ppc64_needs_toc(gsym)
11367 		      : object->ppc64_needs_toc(r_sym)))))
11368 	{
11369 	  Stub_table<size, big_endian>* stub_table
11370 	    = object->stub_table(relinfo->data_shndx);
11371 	  if (stub_table != NULL)
11372 	    {
11373 	      const typename Stub_table<size, big_endian>::Branch_stub_ent* ent
11374 		= stub_table->find_long_branch_entry(value);
11375 	      if (ent != NULL)
11376 		{
11377 		  if (ent->save_res_)
11378 		    value = (value - target->savres_section()->address()
11379 			     + stub_table->stub_address()
11380 			     + stub_table->plt_size()
11381 			     + stub_table->branch_size());
11382 		  else
11383 		    {
11384 		      value = (stub_table->stub_address()
11385 			       + stub_table->plt_size()
11386 			       + ent->off_);
11387 		      if (size == 64)
11388 			{
11389 			  if (r_type == elfcpp::R_PPC64_REL24_NOTOC)
11390 			    {
11391 			      if (!ent->notoc_)
11392 				value += ent->p9off_;
11393 			    }
11394 			  else if (r_type == elfcpp::R_PPC64_REL24_P9NOTOC)
11395 			    value += ent->p9off_;
11396 			  else
11397 			    value += ent->tocoff_;
11398 			}
11399 		    }
11400 		  has_stub_value = true;
11401 		}
11402 	    }
11403 	}
11404       if (!has_stub_value)
11405 	value += local_ent;
11406     }
11407 
11408   switch (r_type)
11409     {
11410     case elfcpp::R_PPC64_REL24_NOTOC:
11411       if (size == 32)
11412 	break;
11413       // Fall through.
11414     case elfcpp::R_PPC64_REL24_P9NOTOC:
11415     case elfcpp::R_PPC64_REL64:
11416     case elfcpp::R_POWERPC_REL32:
11417     case elfcpp::R_POWERPC_REL24:
11418     case elfcpp::R_PPC_PLTREL24:
11419     case elfcpp::R_PPC_LOCAL24PC:
11420     case elfcpp::R_POWERPC_REL16:
11421     case elfcpp::R_POWERPC_REL16_LO:
11422     case elfcpp::R_POWERPC_REL16_HI:
11423     case elfcpp::R_POWERPC_REL16_HA:
11424     case elfcpp::R_POWERPC_REL16DX_HA:
11425     case elfcpp::R_PPC64_REL16_HIGH:
11426     case elfcpp::R_PPC64_REL16_HIGHA:
11427     case elfcpp::R_PPC64_REL16_HIGHER:
11428     case elfcpp::R_PPC64_REL16_HIGHERA:
11429     case elfcpp::R_PPC64_REL16_HIGHEST:
11430     case elfcpp::R_PPC64_REL16_HIGHESTA:
11431     case elfcpp::R_POWERPC_REL14:
11432     case elfcpp::R_POWERPC_REL14_BRTAKEN:
11433     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11434     case elfcpp::R_PPC64_PCREL34:
11435     case elfcpp::R_PPC64_GOT_PCREL34:
11436     case elfcpp::R_PPC64_PLT_PCREL34:
11437     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11438     case elfcpp::R_PPC64_PCREL28:
11439     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11440     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11441     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11442     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11443     case elfcpp::R_PPC64_REL16_HIGHER34:
11444     case elfcpp::R_PPC64_REL16_HIGHERA34:
11445     case elfcpp::R_PPC64_REL16_HIGHEST34:
11446     case elfcpp::R_PPC64_REL16_HIGHESTA34:
11447       value -= address;
11448       break;
11449 
11450     case elfcpp::R_PPC64_TOC16:
11451     case elfcpp::R_PPC64_TOC16_LO:
11452     case elfcpp::R_PPC64_TOC16_HI:
11453     case elfcpp::R_PPC64_TOC16_HA:
11454     case elfcpp::R_PPC64_TOC16_DS:
11455     case elfcpp::R_PPC64_TOC16_LO_DS:
11456       // Subtract the TOC base address.
11457       value -= target->toc_pointer();
11458       break;
11459 
11460     case elfcpp::R_POWERPC_SECTOFF:
11461     case elfcpp::R_POWERPC_SECTOFF_LO:
11462     case elfcpp::R_POWERPC_SECTOFF_HI:
11463     case elfcpp::R_POWERPC_SECTOFF_HA:
11464     case elfcpp::R_PPC64_SECTOFF_DS:
11465     case elfcpp::R_PPC64_SECTOFF_LO_DS:
11466       if (os != NULL)
11467 	value -= os->address();
11468       break;
11469 
11470     case elfcpp::R_PPC64_TPREL16_DS:
11471     case elfcpp::R_PPC64_TPREL16_LO_DS:
11472     case elfcpp::R_PPC64_TPREL16_HIGH:
11473     case elfcpp::R_PPC64_TPREL16_HIGHA:
11474       if (size != 64)
11475 	// R_PPC_TLSGD, R_PPC_TLSLD, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HI
11476 	break;
11477       // Fall through.
11478     case elfcpp::R_POWERPC_TPREL16:
11479     case elfcpp::R_POWERPC_TPREL16_LO:
11480     case elfcpp::R_POWERPC_TPREL16_HI:
11481     case elfcpp::R_POWERPC_TPREL16_HA:
11482     case elfcpp::R_POWERPC_TPREL:
11483     case elfcpp::R_PPC64_TPREL16_HIGHER:
11484     case elfcpp::R_PPC64_TPREL16_HIGHERA:
11485     case elfcpp::R_PPC64_TPREL16_HIGHEST:
11486     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
11487     case elfcpp::R_PPC64_TPREL34:
11488       // tls symbol values are relative to tls_segment()->vaddr()
11489       value -= tp_offset;
11490       break;
11491 
11492     case elfcpp::R_PPC64_DTPREL16_DS:
11493     case elfcpp::R_PPC64_DTPREL16_LO_DS:
11494     case elfcpp::R_PPC64_DTPREL16_HIGHER:
11495     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
11496     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
11497     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
11498       if (size != 64)
11499 	// R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16, R_PPC_EMB_NADDR16_LO
11500 	// R_PPC_EMB_NADDR16_HI, R_PPC_EMB_NADDR16_HA, R_PPC_EMB_SDAI16
11501 	break;
11502       // Fall through.
11503     case elfcpp::R_POWERPC_DTPREL16:
11504     case elfcpp::R_POWERPC_DTPREL16_LO:
11505     case elfcpp::R_POWERPC_DTPREL16_HI:
11506     case elfcpp::R_POWERPC_DTPREL16_HA:
11507     case elfcpp::R_POWERPC_DTPREL:
11508     case elfcpp::R_PPC64_DTPREL16_HIGH:
11509     case elfcpp::R_PPC64_DTPREL16_HIGHA:
11510     case elfcpp::R_PPC64_DTPREL34:
11511       // tls symbol values are relative to tls_segment()->vaddr()
11512       value -= dtp_offset;
11513       break;
11514 
11515     case elfcpp::R_PPC64_ADDR64_LOCAL:
11516       if (gsym != NULL)
11517 	value += object->ppc64_local_entry_offset(gsym);
11518       else
11519 	value += object->ppc64_local_entry_offset(r_sym);
11520       break;
11521 
11522     default:
11523       break;
11524     }
11525 
11526   Insn branch_bit = 0;
11527   switch (r_type)
11528     {
11529     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11530     case elfcpp::R_POWERPC_REL14_BRTAKEN:
11531       branch_bit = 1 << 21;
11532       // Fall through.
11533     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11534     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11535       {
11536 	Insn* iview = reinterpret_cast<Insn*>(view);
11537 	Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11538 	insn &= ~(1 << 21);
11539 	insn |= branch_bit;
11540 	if (this->is_isa_v2)
11541 	  {
11542 	    // Set 'a' bit.  This is 0b00010 in BO field for branch
11543 	    // on CR(BI) insns (BO == 001at or 011at), and 0b01000
11544 	    // for branch on CTR insns (BO == 1a00t or 1a01t).
11545 	    if ((insn & (0x14 << 21)) == (0x04 << 21))
11546 	      insn |= 0x02 << 21;
11547 	    else if ((insn & (0x14 << 21)) == (0x10 << 21))
11548 	      insn |= 0x08 << 21;
11549 	    else
11550 	      break;
11551 	  }
11552 	else
11553 	  {
11554 	    // Invert 'y' bit if not the default.
11555 	    if (static_cast<Signed_address>(value) < 0)
11556 	      insn ^= 1 << 21;
11557 	  }
11558 	elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11559       }
11560       break;
11561 
11562     case elfcpp::R_POWERPC_PLT16_HA:
11563       if (size == 32
11564 	  && !parameters->options().output_is_position_independent())
11565 	{
11566 	  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11567 	  Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11568 
11569 	  // Convert addis to lis.
11570 	  if ((insn & (0x3f << 26)) == 15u << 26
11571 	      && (insn & (0x1f << 16)) != 0)
11572 	    {
11573 	      insn &= ~(0x1f << 16);
11574 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11575 	    }
11576 	}
11577       break;
11578 
11579     default:
11580       break;
11581     }
11582 
11583   if (gsym
11584       ? relative_value_is_known(gsym)
11585       : relative_value_is_known(psymval))
11586     {
11587       Insn* iview;
11588       Insn* iview2;
11589       Insn insn;
11590       uint64_t pinsn, pinsn2;
11591 
11592       switch (r_type)
11593 	{
11594 	default:
11595 	  break;
11596 
11597 	  // Multi-instruction sequences that access the GOT/TOC can
11598 	  // be optimized, eg.
11599 	  //     addis ra,r2,x@got@ha; ld rb,x@got@l(ra);
11600 	  // to  addis ra,r2,x@toc@ha; addi rb,ra,x@toc@l;
11601 	  // and
11602 	  //     addis ra,r2,0; addi rb,ra,x@toc@l;
11603 	  // to  nop;           addi rb,r2,x@toc;
11604 	case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11605 	case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11606 	case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11607 	case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11608 	case elfcpp::R_POWERPC_GOT16_HA:
11609 	case elfcpp::R_PPC64_TOC16_HA:
11610 	  if (size == 64 && parameters->options().toc_optimize())
11611 	    {
11612 	      iview = reinterpret_cast<Insn*>(view - d_offset);
11613 	      insn = elfcpp::Swap<32, big_endian>::readval(iview);
11614 	      if ((r_type == elfcpp::R_PPC64_TOC16_HA
11615 		   && object->make_toc_relative(target, &value))
11616 		  || (r_type == elfcpp::R_POWERPC_GOT16_HA
11617 		      && object->make_got_relative(target, psymval,
11618 						   rela.get_r_addend(),
11619 						   &value)))
11620 		{
11621 		  gold_assert((insn & ((0x3f << 26) | 0x1f << 16))
11622 			      == ((15u << 26) | (2 << 16)));
11623 		}
11624 	      if (((insn & ((0x3f << 26) | 0x1f << 16))
11625 		   == ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
11626 		  && value + 0x8000 < 0x10000)
11627 		{
11628 		  elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11629 		  return true;
11630 		}
11631 	    }
11632 	  break;
11633 
11634 	case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
11635 	case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
11636 	case elfcpp::R_POWERPC_GOT_TPREL16_LO:
11637 	case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
11638 	case elfcpp::R_POWERPC_GOT16_LO:
11639 	case elfcpp::R_PPC64_GOT16_LO_DS:
11640 	case elfcpp::R_PPC64_TOC16_LO:
11641 	case elfcpp::R_PPC64_TOC16_LO_DS:
11642 	  if (size == 64 && parameters->options().toc_optimize())
11643 	    {
11644 	      iview = reinterpret_cast<Insn*>(view - d_offset);
11645 	      insn = elfcpp::Swap<32, big_endian>::readval(iview);
11646 	      bool changed = false;
11647 	      if ((r_type == elfcpp::R_PPC64_TOC16_LO_DS
11648 		   && object->make_toc_relative(target, &value))
11649 		  || (r_type == elfcpp::R_PPC64_GOT16_LO_DS
11650 		      && object->make_got_relative(target, psymval,
11651 						   rela.get_r_addend(),
11652 						   &value)))
11653 		{
11654 		  gold_assert ((insn & (0x3f << 26)) == 58u << 26 /* ld */);
11655 		  insn ^= (14u << 26) ^ (58u << 26);
11656 		  r_type = elfcpp::R_PPC64_TOC16_LO;
11657 		  changed = true;
11658 		}
11659 	      if (ok_lo_toc_insn(insn, r_type)
11660 		  && value + 0x8000 < 0x10000)
11661 		{
11662 		  if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
11663 		    {
11664 		      // Transform addic to addi when we change reg.
11665 		      insn &= ~((0x3f << 26) | (0x1f << 16));
11666 		      insn |= (14u << 26) | (2 << 16);
11667 		    }
11668 		  else
11669 		    {
11670 		      insn &= ~(0x1f << 16);
11671 		      insn |= 2 << 16;
11672 		    }
11673 		  changed = true;
11674 		}
11675 	      if (changed)
11676 		elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11677 	    }
11678 	  break;
11679 
11680 	case elfcpp::R_PPC64_GOT_PCREL34:
11681 	  if (size == 64 && parameters->options().toc_optimize())
11682 	    {
11683 	      iview = reinterpret_cast<Insn*>(view);
11684 	      pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11685 	      pinsn <<= 32;
11686 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11687 	      if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11688 		   != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
11689 		break;
11690 
11691 	      Address relval = psymval->value(object, rela.get_r_addend());
11692 	      relval -= address;
11693 	      if (relval + (1ULL << 33) < 1ULL << 34)
11694 		{
11695 		  value = relval;
11696 		  // Replace with paddi
11697 		  pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
11698 		  elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11699 		  elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11700 							 pinsn & 0xffffffff);
11701 		  goto pcrelopt;
11702 		}
11703 	    }
11704 	  break;
11705 
11706 	case elfcpp::R_PPC64_PCREL34:
11707 	  if (size == 64)
11708 	    {
11709 	      iview = reinterpret_cast<Insn*>(view);
11710 	      pinsn = elfcpp::Swap<32, big_endian>::readval(iview);
11711 	      pinsn <<= 32;
11712 	      pinsn |= elfcpp::Swap<32, big_endian>::readval(iview + 1);
11713 	      if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
11714 		  != ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
11715 		      | (14ULL << 26) /* paddi */))
11716 		break;
11717 
11718 	    pcrelopt:
11719 	      const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11720 	      elfcpp::Shdr<size, big_endian> shdr(relinfo->reloc_shdr);
11721 	      size_t reloc_count = shdr.get_sh_size() / reloc_size;
11722 	      if (relnum >= reloc_count - 1)
11723 		break;
11724 
11725 	      Reltype next_rela(preloc + reloc_size);
11726 	      if ((elfcpp::elf_r_type<size>(next_rela.get_r_info())
11727 		   != elfcpp::R_PPC64_PCREL_OPT)
11728 		  || next_rela.get_r_offset() != rela.get_r_offset())
11729 		break;
11730 
11731 	      Address off = next_rela.get_r_addend();
11732 	      if (off == 0)
11733 		off = 8; // zero means next insn.
11734 	      if (off + rela.get_r_offset() + 4 > view_size)
11735 		break;
11736 
11737 	      iview2 = reinterpret_cast<Insn*>(view + off);
11738 	      pinsn2 = elfcpp::Swap<32, big_endian>::readval(iview2);
11739 	      pinsn2 <<= 32;
11740 	      if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
11741 		break;
11742 	      if (xlate_pcrel_opt(&pinsn, &pinsn2))
11743 		{
11744 		  elfcpp::Swap<32, big_endian>::writeval(iview, pinsn >> 32);
11745 		  elfcpp::Swap<32, big_endian>::writeval(iview + 1,
11746 							 pinsn & 0xffffffff);
11747 		  elfcpp::Swap<32, big_endian>::writeval(iview2, pinsn2 >> 32);
11748 		}
11749 	    }
11750 	  break;
11751 
11752 	case elfcpp::R_POWERPC_TPREL16_HA:
11753 	  if (target->tprel_opt() && value + 0x8000 < 0x10000)
11754 	    {
11755 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11756 	      elfcpp::Swap<32, big_endian>::writeval(iview, nop);
11757 	      return true;
11758 	    }
11759 	  break;
11760 
11761 	case elfcpp::R_PPC64_TPREL16_LO_DS:
11762 	  if (size == 32)
11763 	    // R_PPC_TLSGD, R_PPC_TLSLD
11764 	    break;
11765 	  // Fall through.
11766 	case elfcpp::R_POWERPC_TPREL16_LO:
11767 	  if (target->tprel_opt() && value + 0x8000 < 0x10000)
11768 	    {
11769 	      Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11770 	      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
11771 	      insn &= ~(0x1f << 16);
11772 	      insn |= (size == 32 ? 2 : 13) << 16;
11773 	      elfcpp::Swap<32, big_endian>::writeval(iview, insn);
11774 	    }
11775 	  break;
11776 
11777 	case elfcpp::R_PPC64_ENTRY:
11778 	  if (size == 64)
11779 	    {
11780 	      value = target->toc_pointer();
11781 	      if (value + 0x80008000 <= 0xffffffff
11782 		  && !parameters->options().output_is_position_independent())
11783 		{
11784 		  Insn* iview = reinterpret_cast<Insn*>(view);
11785 		  Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11786 		  Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11787 
11788 		  if ((insn1 & ~0xfffc) == ld_2_12
11789 		      && insn2 == add_2_2_12)
11790 		    {
11791 		      insn1 = lis_2 + ha(value);
11792 		      elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11793 		      insn2 = addi_2_2 + l(value);
11794 		      elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11795 		      return true;
11796 		    }
11797 		}
11798 	      else
11799 		{
11800 		  value -= address;
11801 		  if (value + 0x80008000 <= 0xffffffff)
11802 		    {
11803 		      Insn* iview = reinterpret_cast<Insn*>(view);
11804 		      Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
11805 		      Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
11806 
11807 		      if ((insn1 & ~0xfffc) == ld_2_12
11808 			  && insn2 == add_2_2_12)
11809 			{
11810 			  insn1 = addis_2_12 + ha(value);
11811 			  elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
11812 			  insn2 = addi_2_2 + l(value);
11813 			  elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
11814 			  return true;
11815 			}
11816 		    }
11817 		}
11818 	    }
11819 	  break;
11820 
11821 	case elfcpp::R_POWERPC_REL16_LO:
11822 	  // If we are generating a non-PIC executable, edit
11823 	  // 	0:	addis 2,12,.TOC.-0b@ha
11824 	  //		addi 2,2,.TOC.-0b@l
11825 	  // used by ELFv2 global entry points to set up r2, to
11826 	  //		lis 2,.TOC.@ha
11827 	  //		addi 2,2,.TOC.@l
11828 	  // if .TOC. is in range.  */
11829 	  if (size == 64
11830 	      && value + address - 4 + 0x80008000 <= 0xffffffff
11831 	      && relnum + 1 > 1
11832 	      && preloc != NULL
11833 	      && target->abiversion() >= 2
11834 	      && !parameters->options().output_is_position_independent()
11835 	      && rela.get_r_addend() == d_offset + 4
11836 	      && gsym != NULL
11837 	      && strcmp(gsym->name(), ".TOC.") == 0)
11838 	    {
11839 	      const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
11840 	      Reltype prev_rela(preloc - reloc_size);
11841 	      if ((prev_rela.get_r_info()
11842 		   == elfcpp::elf_r_info<size>(r_sym,
11843 					       elfcpp::R_POWERPC_REL16_HA))
11844 		  && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
11845 		  && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
11846 		{
11847 		  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11848 		  Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
11849 		  Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
11850 
11851 		  if ((insn1 & 0xffff0000) == addis_2_12
11852 		      && (insn2 & 0xffff0000) == addi_2_2)
11853 		    {
11854 		      insn1 = lis_2 + ha(value + address - 4);
11855 		      elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
11856 		      insn2 = addi_2_2 + l(value + address - 4);
11857 		      elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
11858 		      if (relinfo->rr)
11859 			{
11860 			  relinfo->rr->set_strategy(relnum - 1,
11861 						    Relocatable_relocs::RELOC_SPECIAL);
11862 			  relinfo->rr->set_strategy(relnum,
11863 						    Relocatable_relocs::RELOC_SPECIAL);
11864 			}
11865 		      return true;
11866 		    }
11867 		}
11868 	    }
11869 	  break;
11870 	}
11871     }
11872 
11873   typename Reloc::Overflow_check overflow = Reloc::CHECK_NONE;
11874   elfcpp::Shdr<size, big_endian> shdr(relinfo->data_shdr);
11875   switch (r_type)
11876     {
11877     case elfcpp::R_POWERPC_ADDR32:
11878     case elfcpp::R_POWERPC_UADDR32:
11879       if (size == 64)
11880 	overflow = Reloc::CHECK_BITFIELD;
11881       break;
11882 
11883     case elfcpp::R_POWERPC_REL32:
11884     case elfcpp::R_POWERPC_REL16DX_HA:
11885       if (size == 64)
11886 	overflow = Reloc::CHECK_SIGNED;
11887       break;
11888 
11889     case elfcpp::R_POWERPC_UADDR16:
11890       overflow = Reloc::CHECK_BITFIELD;
11891       break;
11892 
11893     case elfcpp::R_POWERPC_ADDR16:
11894       // We really should have three separate relocations,
11895       // one for 16-bit data, one for insns with 16-bit signed fields,
11896       // and one for insns with 16-bit unsigned fields.
11897       overflow = Reloc::CHECK_BITFIELD;
11898       if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
11899 	overflow = Reloc::CHECK_LOW_INSN;
11900       break;
11901 
11902     case elfcpp::R_POWERPC_ADDR16_HI:
11903     case elfcpp::R_POWERPC_ADDR16_HA:
11904     case elfcpp::R_POWERPC_GOT16_HI:
11905     case elfcpp::R_POWERPC_GOT16_HA:
11906     case elfcpp::R_POWERPC_PLT16_HI:
11907     case elfcpp::R_POWERPC_PLT16_HA:
11908     case elfcpp::R_POWERPC_SECTOFF_HI:
11909     case elfcpp::R_POWERPC_SECTOFF_HA:
11910     case elfcpp::R_PPC64_TOC16_HI:
11911     case elfcpp::R_PPC64_TOC16_HA:
11912     case elfcpp::R_PPC64_PLTGOT16_HI:
11913     case elfcpp::R_PPC64_PLTGOT16_HA:
11914     case elfcpp::R_POWERPC_TPREL16_HI:
11915     case elfcpp::R_POWERPC_TPREL16_HA:
11916     case elfcpp::R_POWERPC_DTPREL16_HI:
11917     case elfcpp::R_POWERPC_DTPREL16_HA:
11918     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
11919     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
11920     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
11921     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
11922     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
11923     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
11924     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
11925     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
11926     case elfcpp::R_POWERPC_REL16_HI:
11927     case elfcpp::R_POWERPC_REL16_HA:
11928       if (size != 32)
11929 	overflow = Reloc::CHECK_HIGH_INSN;
11930       break;
11931 
11932     case elfcpp::R_POWERPC_REL16:
11933     case elfcpp::R_PPC64_TOC16:
11934     case elfcpp::R_POWERPC_GOT16:
11935     case elfcpp::R_POWERPC_SECTOFF:
11936     case elfcpp::R_POWERPC_TPREL16:
11937     case elfcpp::R_POWERPC_DTPREL16:
11938     case elfcpp::R_POWERPC_GOT_TLSGD16:
11939     case elfcpp::R_POWERPC_GOT_TLSLD16:
11940     case elfcpp::R_POWERPC_GOT_TPREL16:
11941     case elfcpp::R_POWERPC_GOT_DTPREL16:
11942       overflow = Reloc::CHECK_LOW_INSN;
11943       break;
11944 
11945     case elfcpp::R_PPC64_REL24_NOTOC:
11946       if (size == 32)
11947 	break;
11948       // Fall through.
11949     case elfcpp::R_PPC64_REL24_P9NOTOC:
11950     case elfcpp::R_POWERPC_ADDR24:
11951     case elfcpp::R_POWERPC_ADDR14:
11952     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
11953     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
11954     case elfcpp::R_PPC64_ADDR16_DS:
11955     case elfcpp::R_POWERPC_REL24:
11956     case elfcpp::R_PPC_PLTREL24:
11957     case elfcpp::R_PPC_LOCAL24PC:
11958     case elfcpp::R_PPC64_TPREL16_DS:
11959     case elfcpp::R_PPC64_DTPREL16_DS:
11960     case elfcpp::R_PPC64_TOC16_DS:
11961     case elfcpp::R_PPC64_GOT16_DS:
11962     case elfcpp::R_PPC64_SECTOFF_DS:
11963     case elfcpp::R_POWERPC_REL14:
11964     case elfcpp::R_POWERPC_REL14_BRTAKEN:
11965     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
11966     case elfcpp::R_PPC64_D34:
11967     case elfcpp::R_PPC64_PCREL34:
11968     case elfcpp::R_PPC64_GOT_PCREL34:
11969     case elfcpp::R_PPC64_PLT_PCREL34:
11970     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
11971     case elfcpp::R_PPC64_D28:
11972     case elfcpp::R_PPC64_PCREL28:
11973     case elfcpp::R_PPC64_TPREL34:
11974     case elfcpp::R_PPC64_DTPREL34:
11975     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
11976     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
11977     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
11978     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
11979       overflow = Reloc::CHECK_SIGNED;
11980       break;
11981     }
11982 
11983   Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
11984   Insn insn = 0;
11985 
11986   if (overflow == Reloc::CHECK_LOW_INSN
11987       || overflow == Reloc::CHECK_HIGH_INSN)
11988     {
11989       insn = elfcpp::Swap<32, big_endian>::readval(iview);
11990 
11991       if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
11992 	overflow = Reloc::CHECK_BITFIELD;
11993       else if (overflow == Reloc::CHECK_LOW_INSN
11994 	       ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
11995 		  || (insn & (0x3f << 26)) == 24u << 26 /* ori */
11996 		  || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
11997 	       : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
11998 		  || (insn & (0x3f << 26)) == 25u << 26 /* oris */
11999 		  || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
12000 	overflow = Reloc::CHECK_UNSIGNED;
12001       else
12002 	overflow = Reloc::CHECK_SIGNED;
12003     }
12004 
12005   bool maybe_dq_reloc = false;
12006   typename Powerpc_relocate_functions<size, big_endian>::Status status
12007     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
12008   switch (r_type)
12009     {
12010     case elfcpp::R_POWERPC_NONE:
12011     case elfcpp::R_POWERPC_TLS:
12012     case elfcpp::R_POWERPC_GNU_VTINHERIT:
12013     case elfcpp::R_POWERPC_GNU_VTENTRY:
12014     case elfcpp::R_POWERPC_PLTSEQ:
12015     case elfcpp::R_POWERPC_PLTCALL:
12016     case elfcpp::R_PPC64_PLTSEQ_NOTOC:
12017     case elfcpp::R_PPC64_PLTCALL_NOTOC:
12018     case elfcpp::R_PPC64_PCREL_OPT:
12019       break;
12020 
12021     case elfcpp::R_PPC64_ADDR64:
12022     case elfcpp::R_PPC64_REL64:
12023     case elfcpp::R_PPC64_TOC:
12024     case elfcpp::R_PPC64_ADDR64_LOCAL:
12025       Reloc::addr64(view, value);
12026       break;
12027 
12028     case elfcpp::R_POWERPC_TPREL:
12029     case elfcpp::R_POWERPC_DTPREL:
12030       if (size == 64)
12031 	Reloc::addr64(view, value);
12032       else
12033 	status = Reloc::addr32(view, value, overflow);
12034       break;
12035 
12036     case elfcpp::R_PPC64_UADDR64:
12037       Reloc::addr64_u(view, value);
12038       break;
12039 
12040     case elfcpp::R_POWERPC_ADDR32:
12041       status = Reloc::addr32(view, value, overflow);
12042       break;
12043 
12044     case elfcpp::R_POWERPC_REL32:
12045     case elfcpp::R_POWERPC_UADDR32:
12046       status = Reloc::addr32_u(view, value, overflow);
12047       break;
12048 
12049     case elfcpp::R_PPC64_REL24_NOTOC:
12050       if (size == 32)
12051 	goto unsupp; // R_PPC_EMB_RELSDA
12052       // Fall through.
12053     case elfcpp::R_PPC64_REL24_P9NOTOC:
12054     case elfcpp::R_POWERPC_ADDR24:
12055     case elfcpp::R_POWERPC_REL24:
12056     case elfcpp::R_PPC_PLTREL24:
12057     case elfcpp::R_PPC_LOCAL24PC:
12058       status = Reloc::addr24(view, value, overflow);
12059       break;
12060 
12061     case elfcpp::R_POWERPC_GOT_DTPREL16:
12062     case elfcpp::R_POWERPC_GOT_DTPREL16_LO:
12063     case elfcpp::R_POWERPC_GOT_TPREL16:
12064     case elfcpp::R_POWERPC_GOT_TPREL16_LO:
12065       if (size == 64)
12066 	{
12067 	  // On ppc64 these are all ds form
12068 	  maybe_dq_reloc = true;
12069 	  break;
12070 	}
12071       // Fall through.
12072     case elfcpp::R_POWERPC_ADDR16:
12073     case elfcpp::R_POWERPC_REL16:
12074     case elfcpp::R_PPC64_TOC16:
12075     case elfcpp::R_POWERPC_GOT16:
12076     case elfcpp::R_POWERPC_SECTOFF:
12077     case elfcpp::R_POWERPC_TPREL16:
12078     case elfcpp::R_POWERPC_DTPREL16:
12079     case elfcpp::R_POWERPC_GOT_TLSGD16:
12080     case elfcpp::R_POWERPC_GOT_TLSLD16:
12081     case elfcpp::R_POWERPC_ADDR16_LO:
12082     case elfcpp::R_POWERPC_REL16_LO:
12083     case elfcpp::R_PPC64_TOC16_LO:
12084     case elfcpp::R_POWERPC_GOT16_LO:
12085     case elfcpp::R_POWERPC_PLT16_LO:
12086     case elfcpp::R_POWERPC_SECTOFF_LO:
12087     case elfcpp::R_POWERPC_TPREL16_LO:
12088     case elfcpp::R_POWERPC_DTPREL16_LO:
12089     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
12090     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
12091       if (size == 64)
12092 	status = Reloc::addr16(view, value, overflow);
12093       else
12094 	maybe_dq_reloc = true;
12095       break;
12096 
12097     case elfcpp::R_POWERPC_UADDR16:
12098       status = Reloc::addr16_u(view, value, overflow);
12099       break;
12100 
12101     case elfcpp::R_PPC64_ADDR16_HIGH:
12102     case elfcpp::R_PPC64_TPREL16_HIGH:
12103     case elfcpp::R_PPC64_DTPREL16_HIGH:
12104       if (size == 32)
12105 	// R_PPC_EMB_MRKREF, R_PPC_EMB_RELST_LO, R_PPC_EMB_RELST_HA
12106 	goto unsupp;
12107       // Fall through.
12108     case elfcpp::R_POWERPC_ADDR16_HI:
12109     case elfcpp::R_POWERPC_REL16_HI:
12110     case elfcpp::R_PPC64_REL16_HIGH:
12111     case elfcpp::R_PPC64_TOC16_HI:
12112     case elfcpp::R_POWERPC_GOT16_HI:
12113     case elfcpp::R_POWERPC_PLT16_HI:
12114     case elfcpp::R_POWERPC_SECTOFF_HI:
12115     case elfcpp::R_POWERPC_TPREL16_HI:
12116     case elfcpp::R_POWERPC_DTPREL16_HI:
12117     case elfcpp::R_POWERPC_GOT_TLSGD16_HI:
12118     case elfcpp::R_POWERPC_GOT_TLSLD16_HI:
12119     case elfcpp::R_POWERPC_GOT_TPREL16_HI:
12120     case elfcpp::R_POWERPC_GOT_DTPREL16_HI:
12121       Reloc::addr16_hi(view, value);
12122       break;
12123 
12124     case elfcpp::R_PPC64_ADDR16_HIGHA:
12125     case elfcpp::R_PPC64_TPREL16_HIGHA:
12126     case elfcpp::R_PPC64_DTPREL16_HIGHA:
12127       if (size == 32)
12128 	// R_PPC_EMB_RELSEC16, R_PPC_EMB_RELST_HI, R_PPC_EMB_BIT_FLD
12129 	goto unsupp;
12130       // Fall through.
12131     case elfcpp::R_POWERPC_ADDR16_HA:
12132     case elfcpp::R_POWERPC_REL16_HA:
12133     case elfcpp::R_PPC64_REL16_HIGHA:
12134     case elfcpp::R_PPC64_TOC16_HA:
12135     case elfcpp::R_POWERPC_GOT16_HA:
12136     case elfcpp::R_POWERPC_PLT16_HA:
12137     case elfcpp::R_POWERPC_SECTOFF_HA:
12138     case elfcpp::R_POWERPC_TPREL16_HA:
12139     case elfcpp::R_POWERPC_DTPREL16_HA:
12140     case elfcpp::R_POWERPC_GOT_TLSGD16_HA:
12141     case elfcpp::R_POWERPC_GOT_TLSLD16_HA:
12142     case elfcpp::R_POWERPC_GOT_TPREL16_HA:
12143     case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
12144       Reloc::addr16_ha(view, value);
12145       break;
12146 
12147     case elfcpp::R_POWERPC_REL16DX_HA:
12148       status = Reloc::addr16dx_ha(view, value, overflow);
12149       break;
12150 
12151     case elfcpp::R_PPC64_DTPREL16_HIGHER:
12152       if (size == 32)
12153 	// R_PPC_EMB_NADDR16_LO
12154 	goto unsupp;
12155       // Fall through.
12156     case elfcpp::R_PPC64_ADDR16_HIGHER:
12157     case elfcpp::R_PPC64_REL16_HIGHER:
12158     case elfcpp::R_PPC64_TPREL16_HIGHER:
12159       Reloc::addr16_hi2(view, value);
12160       break;
12161 
12162     case elfcpp::R_PPC64_DTPREL16_HIGHERA:
12163       if (size == 32)
12164 	// R_PPC_EMB_NADDR16_HI
12165 	goto unsupp;
12166       // Fall through.
12167     case elfcpp::R_PPC64_ADDR16_HIGHERA:
12168     case elfcpp::R_PPC64_REL16_HIGHERA:
12169     case elfcpp::R_PPC64_TPREL16_HIGHERA:
12170       Reloc::addr16_ha2(view, value);
12171       break;
12172 
12173     case elfcpp::R_PPC64_DTPREL16_HIGHEST:
12174       if (size == 32)
12175 	// R_PPC_EMB_NADDR16_HA
12176 	goto unsupp;
12177       // Fall through.
12178     case elfcpp::R_PPC64_ADDR16_HIGHEST:
12179     case elfcpp::R_PPC64_REL16_HIGHEST:
12180     case elfcpp::R_PPC64_TPREL16_HIGHEST:
12181       Reloc::addr16_hi3(view, value);
12182       break;
12183 
12184     case elfcpp::R_PPC64_DTPREL16_HIGHESTA:
12185       if (size == 32)
12186 	// R_PPC_EMB_SDAI16
12187 	goto unsupp;
12188       // Fall through.
12189     case elfcpp::R_PPC64_ADDR16_HIGHESTA:
12190     case elfcpp::R_PPC64_REL16_HIGHESTA:
12191     case elfcpp::R_PPC64_TPREL16_HIGHESTA:
12192       Reloc::addr16_ha3(view, value);
12193       break;
12194 
12195     case elfcpp::R_PPC64_DTPREL16_DS:
12196     case elfcpp::R_PPC64_DTPREL16_LO_DS:
12197       if (size == 32)
12198 	// R_PPC_EMB_NADDR32, R_PPC_EMB_NADDR16
12199 	goto unsupp;
12200       // Fall through.
12201     case elfcpp::R_PPC64_TPREL16_DS:
12202     case elfcpp::R_PPC64_TPREL16_LO_DS:
12203       if (size == 32)
12204 	// R_PPC_TLSGD, R_PPC_TLSLD
12205 	break;
12206       // Fall through.
12207     case elfcpp::R_PPC64_ADDR16_DS:
12208     case elfcpp::R_PPC64_ADDR16_LO_DS:
12209     case elfcpp::R_PPC64_TOC16_DS:
12210     case elfcpp::R_PPC64_TOC16_LO_DS:
12211     case elfcpp::R_PPC64_GOT16_DS:
12212     case elfcpp::R_PPC64_GOT16_LO_DS:
12213     case elfcpp::R_PPC64_PLT16_LO_DS:
12214     case elfcpp::R_PPC64_SECTOFF_DS:
12215     case elfcpp::R_PPC64_SECTOFF_LO_DS:
12216       maybe_dq_reloc = true;
12217       break;
12218 
12219     case elfcpp::R_POWERPC_ADDR14:
12220     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
12221     case elfcpp::R_POWERPC_ADDR14_BRNTAKEN:
12222     case elfcpp::R_POWERPC_REL14:
12223     case elfcpp::R_POWERPC_REL14_BRTAKEN:
12224     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
12225       status = Reloc::addr14(view, value, overflow);
12226       break;
12227 
12228     case elfcpp::R_POWERPC_COPY:
12229     case elfcpp::R_POWERPC_GLOB_DAT:
12230     case elfcpp::R_POWERPC_JMP_SLOT:
12231     case elfcpp::R_POWERPC_RELATIVE:
12232     case elfcpp::R_POWERPC_DTPMOD:
12233     case elfcpp::R_PPC64_JMP_IREL:
12234     case elfcpp::R_POWERPC_IRELATIVE:
12235       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12236 			     _("unexpected reloc %u in object file"),
12237 			     r_type);
12238       break;
12239 
12240     case elfcpp::R_PPC64_TOCSAVE:
12241       if (size == 32)
12242 	// R_PPC_EMB_SDA21
12243 	goto unsupp;
12244       else
12245 	{
12246 	  Symbol_location loc;
12247 	  loc.object = relinfo->object;
12248 	  loc.shndx = relinfo->data_shndx;
12249 	  loc.offset = rela.get_r_offset();
12250 	  const Tocsave_loc *tocsave = target->tocsave_loc();
12251 	  if (tocsave->find(loc) != tocsave->end())
12252 	    {
12253 	      // If we've generated plt calls using this tocsave, then
12254 	      // the nop needs to be changed to save r2.
12255 	      Insn* iview = reinterpret_cast<Insn*>(view);
12256 	      if (elfcpp::Swap<32, big_endian>::readval(iview) == nop)
12257 		elfcpp::Swap<32, big_endian>::
12258 		  writeval(iview, std_2_1 + target->stk_toc());
12259 	    }
12260 	}
12261       break;
12262 
12263     case elfcpp::R_PPC_EMB_SDA2I16:
12264     case elfcpp::R_PPC_EMB_SDA2REL:
12265       if (size == 32)
12266 	goto unsupp;
12267       // R_PPC64_TLSGD, R_PPC64_TLSLD
12268       break;
12269 
12270     case elfcpp::R_PPC64_D34:
12271     case elfcpp::R_PPC64_D34_LO:
12272     case elfcpp::R_PPC64_PCREL34:
12273     case elfcpp::R_PPC64_GOT_PCREL34:
12274     case elfcpp::R_PPC64_PLT_PCREL34:
12275     case elfcpp::R_PPC64_PLT_PCREL34_NOTOC:
12276     case elfcpp::R_PPC64_TPREL34:
12277     case elfcpp::R_PPC64_DTPREL34:
12278     case elfcpp::R_PPC64_GOT_TLSGD_PCREL34:
12279     case elfcpp::R_PPC64_GOT_TLSLD_PCREL34:
12280     case elfcpp::R_PPC64_GOT_TPREL_PCREL34:
12281     case elfcpp::R_PPC64_GOT_DTPREL_PCREL34:
12282       if (size == 32)
12283 	goto unsupp;
12284       status = Reloc::addr34(view, value, overflow);
12285       break;
12286 
12287     case elfcpp::R_PPC64_D34_HI30:
12288       if (size == 32)
12289 	goto unsupp;
12290       Reloc::addr34_hi(view, value);
12291       break;
12292 
12293     case elfcpp::R_PPC64_D34_HA30:
12294       if (size == 32)
12295 	goto unsupp;
12296       Reloc::addr34_ha(view, value);
12297       break;
12298 
12299     case elfcpp::R_PPC64_D28:
12300     case elfcpp::R_PPC64_PCREL28:
12301       if (size == 32)
12302 	goto unsupp;
12303       status = Reloc::addr28(view, value, overflow);
12304       break;
12305 
12306     case elfcpp::R_PPC64_ADDR16_HIGHER34:
12307     case elfcpp::R_PPC64_REL16_HIGHER34:
12308       if (size == 32)
12309 	goto unsupp;
12310       Reloc::addr16_higher34(view, value);
12311       break;
12312 
12313     case elfcpp::R_PPC64_ADDR16_HIGHERA34:
12314     case elfcpp::R_PPC64_REL16_HIGHERA34:
12315       if (size == 32)
12316 	goto unsupp;
12317       Reloc::addr16_highera34(view, value);
12318       break;
12319 
12320     case elfcpp::R_PPC64_ADDR16_HIGHEST34:
12321     case elfcpp::R_PPC64_REL16_HIGHEST34:
12322       if (size == 32)
12323 	goto unsupp;
12324       Reloc::addr16_highest34(view, value);
12325       break;
12326 
12327     case elfcpp::R_PPC64_ADDR16_HIGHESTA34:
12328     case elfcpp::R_PPC64_REL16_HIGHESTA34:
12329       if (size == 32)
12330 	goto unsupp;
12331       Reloc::addr16_highesta34(view, value);
12332       break;
12333 
12334     case elfcpp::R_POWERPC_PLT32:
12335     case elfcpp::R_POWERPC_PLTREL32:
12336     case elfcpp::R_PPC_SDAREL16:
12337     case elfcpp::R_POWERPC_ADDR30:
12338     case elfcpp::R_PPC64_PLT64:
12339     case elfcpp::R_PPC64_PLTREL64:
12340     case elfcpp::R_PPC64_PLTGOT16:
12341     case elfcpp::R_PPC64_PLTGOT16_LO:
12342     case elfcpp::R_PPC64_PLTGOT16_HI:
12343     case elfcpp::R_PPC64_PLTGOT16_HA:
12344     case elfcpp::R_PPC64_PLTGOT16_DS:
12345     case elfcpp::R_PPC64_PLTGOT16_LO_DS:
12346     case elfcpp::R_PPC_TOC16:
12347     default:
12348     unsupp:
12349       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12350 			     _("unsupported reloc %u"),
12351 			     r_type);
12352       break;
12353     }
12354 
12355   if (maybe_dq_reloc)
12356     {
12357       if (insn == 0)
12358 	insn = elfcpp::Swap<32, big_endian>::readval(iview);
12359 
12360       if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
12361 	  || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
12362 	      && (insn & 3) == 1))
12363 	status = Reloc::addr16_dq(view, value, overflow);
12364       else if (size == 64
12365 	       || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
12366 	       || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
12367 	       || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
12368 	       || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
12369 	status = Reloc::addr16_ds(view, value, overflow);
12370       else
12371 	status = Reloc::addr16(view, value, overflow);
12372     }
12373 
12374   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
12375       && (has_stub_value
12376 	  || !(gsym != NULL
12377 	       && gsym->is_undefined()
12378 	       && is_branch_reloc<size>(r_type))))
12379     {
12380       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
12381 			     _("relocation overflow"));
12382       if (has_stub_value)
12383 	gold_info(_("try relinking with a smaller --stub-group-size"));
12384     }
12385 
12386   return true;
12387 }
12388 
12389 // Relocate section data.
12390 
12391 template<int size, bool big_endian>
12392 void
relocate_section(const Relocate_info<size,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,unsigned char * view,Address address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)12393 Target_powerpc<size, big_endian>::relocate_section(
12394     const Relocate_info<size, big_endian>* relinfo,
12395     unsigned int sh_type,
12396     const unsigned char* prelocs,
12397     size_t reloc_count,
12398     Output_section* output_section,
12399     bool needs_special_offset_handling,
12400     unsigned char* view,
12401     Address address,
12402     section_size_type view_size,
12403     const Reloc_symbol_changes* reloc_symbol_changes)
12404 {
12405   typedef Target_powerpc<size, big_endian> Powerpc;
12406   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
12407   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
12408     Powerpc_comdat_behavior;
12409   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12410       Classify_reloc;
12411 
12412   gold_assert(sh_type == elfcpp::SHT_RELA);
12413 
12414   gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
12415 			 Powerpc_comdat_behavior, Classify_reloc>(
12416     relinfo,
12417     this,
12418     prelocs,
12419     reloc_count,
12420     output_section,
12421     needs_special_offset_handling,
12422     view,
12423     address,
12424     view_size,
12425     reloc_symbol_changes);
12426 }
12427 
12428 template<int size, bool big_endian>
12429 class Powerpc_scan_relocatable_reloc
12430 {
12431 public:
12432   typedef typename elfcpp::Rela<size, big_endian> Reltype;
12433   static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12434   static const int sh_type = elfcpp::SHT_RELA;
12435 
12436   // Return the symbol referred to by the relocation.
12437   static inline unsigned int
get_r_sym(const Reltype * reloc)12438   get_r_sym(const Reltype* reloc)
12439   { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
12440 
12441   // Return the type of the relocation.
12442   static inline unsigned int
get_r_type(const Reltype * reloc)12443   get_r_type(const Reltype* reloc)
12444   { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
12445 
12446   // Return the strategy to use for a local symbol which is not a
12447   // section symbol, given the relocation type.
12448   inline Relocatable_relocs::Reloc_strategy
local_non_section_strategy(unsigned int r_type,Relobj *,unsigned int r_sym)12449   local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym)
12450   {
12451     if (r_type == 0 && r_sym == 0)
12452       return Relocatable_relocs::RELOC_DISCARD;
12453     return Relocatable_relocs::RELOC_COPY;
12454   }
12455 
12456   // Return the strategy to use for a local symbol which is a section
12457   // symbol, given the relocation type.
12458   inline Relocatable_relocs::Reloc_strategy
local_section_strategy(unsigned int,Relobj *)12459   local_section_strategy(unsigned int, Relobj*)
12460   {
12461     return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
12462   }
12463 
12464   // Return the strategy to use for a global symbol, given the
12465   // relocation type, the object, and the symbol index.
12466   inline Relocatable_relocs::Reloc_strategy
global_strategy(unsigned int r_type,Relobj *,unsigned int)12467   global_strategy(unsigned int r_type, Relobj*, unsigned int)
12468   {
12469     if (size == 32
12470 	&& (r_type == elfcpp::R_PPC_PLTREL24
12471 	    || r_type == elfcpp::R_POWERPC_PLT16_LO
12472 	    || r_type == elfcpp::R_POWERPC_PLT16_HI
12473 	    || r_type == elfcpp::R_POWERPC_PLT16_HA))
12474       return Relocatable_relocs::RELOC_SPECIAL;
12475     return Relocatable_relocs::RELOC_COPY;
12476   }
12477 };
12478 
12479 // Scan the relocs during a relocatable link.
12480 
12481 template<int size, bool big_endian>
12482 void
scan_relocatable_relocs(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_symbols,Relocatable_relocs * rr)12483 Target_powerpc<size, big_endian>::scan_relocatable_relocs(
12484     Symbol_table* symtab,
12485     Layout* layout,
12486     Sized_relobj_file<size, big_endian>* object,
12487     unsigned int data_shndx,
12488     unsigned int sh_type,
12489     const unsigned char* prelocs,
12490     size_t reloc_count,
12491     Output_section* output_section,
12492     bool needs_special_offset_handling,
12493     size_t local_symbol_count,
12494     const unsigned char* plocal_symbols,
12495     Relocatable_relocs* rr)
12496 {
12497   typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
12498 
12499   gold_assert(sh_type == elfcpp::SHT_RELA);
12500 
12501   gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
12502     symtab,
12503     layout,
12504     object,
12505     data_shndx,
12506     prelocs,
12507     reloc_count,
12508     output_section,
12509     needs_special_offset_handling,
12510     local_symbol_count,
12511     plocal_symbols,
12512     rr);
12513 }
12514 
12515 // Scan the relocs for --emit-relocs.
12516 
12517 template<int size, bool big_endian>
12518 void
emit_relocs_scan(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object,unsigned int data_shndx,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,bool needs_special_offset_handling,size_t local_symbol_count,const unsigned char * plocal_syms,Relocatable_relocs * rr)12519 Target_powerpc<size, big_endian>::emit_relocs_scan(
12520     Symbol_table* symtab,
12521     Layout* layout,
12522     Sized_relobj_file<size, big_endian>* object,
12523     unsigned int data_shndx,
12524     unsigned int sh_type,
12525     const unsigned char* prelocs,
12526     size_t reloc_count,
12527     Output_section* output_section,
12528     bool needs_special_offset_handling,
12529     size_t local_symbol_count,
12530     const unsigned char* plocal_syms,
12531     Relocatable_relocs* rr)
12532 {
12533   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
12534       Classify_reloc;
12535   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
12536       Emit_relocs_strategy;
12537 
12538   gold_assert(sh_type == elfcpp::SHT_RELA);
12539 
12540   gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
12541     symtab,
12542     layout,
12543     object,
12544     data_shndx,
12545     prelocs,
12546     reloc_count,
12547     output_section,
12548     needs_special_offset_handling,
12549     local_symbol_count,
12550     plocal_syms,
12551     rr);
12552 }
12553 
12554 // Emit relocations for a section.
12555 // This is a modified version of the function by the same name in
12556 // target-reloc.h.  Using relocate_special_relocatable for
12557 // R_PPC_PLTREL24 would require duplication of the entire body of the
12558 // loop, so we may as well duplicate the whole thing.
12559 
12560 template<int size, bool big_endian>
12561 void
relocate_relocs(const Relocate_info<size,big_endian> * relinfo,unsigned int sh_type,const unsigned char * prelocs,size_t reloc_count,Output_section * output_section,typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,unsigned char *,Address view_address,section_size_type,unsigned char * reloc_view,section_size_type reloc_view_size)12562 Target_powerpc<size, big_endian>::relocate_relocs(
12563     const Relocate_info<size, big_endian>* relinfo,
12564     unsigned int sh_type,
12565     const unsigned char* prelocs,
12566     size_t reloc_count,
12567     Output_section* output_section,
12568     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
12569     unsigned char*,
12570     Address view_address,
12571     section_size_type,
12572     unsigned char* reloc_view,
12573     section_size_type reloc_view_size)
12574 {
12575   gold_assert(sh_type == elfcpp::SHT_RELA);
12576 
12577   typedef typename elfcpp::Rela<size, big_endian> Reltype;
12578   typedef typename elfcpp::Rela_write<size, big_endian> Reltype_write;
12579   const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
12580   // Offset from start of insn to d-field reloc.
12581   const int d_offset = big_endian ? 2 : 0;
12582 
12583   Powerpc_relobj<size, big_endian>* const object
12584     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
12585   const unsigned int local_count = object->local_symbol_count();
12586   unsigned int got2_shndx = object->got2_shndx();
12587   Address got2_addend = 0;
12588   if (got2_shndx != 0)
12589     {
12590       got2_addend = object->get_output_section_offset(got2_shndx);
12591       gold_assert(got2_addend != invalid_address);
12592     }
12593 
12594   const bool relocatable = parameters->options().relocatable();
12595 
12596   unsigned char* pwrite = reloc_view;
12597   bool zap_next = false;
12598   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
12599     {
12600       Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
12601       if (strategy == Relocatable_relocs::RELOC_DISCARD)
12602 	continue;
12603 
12604       Reltype reloc(prelocs);
12605       Reltype_write reloc_write(pwrite);
12606 
12607       Address offset = reloc.get_r_offset();
12608       typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
12609       unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
12610       unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
12611       const unsigned int orig_r_sym = r_sym;
12612       typename elfcpp::Elf_types<size>::Elf_Swxword addend
12613 	= reloc.get_r_addend();
12614       const Symbol* gsym = NULL;
12615 
12616       if (zap_next)
12617 	{
12618 	  // We could arrange to discard these and other relocs for
12619 	  // tls optimised sequences in the strategy methods, but for
12620 	  // now do as BFD ld does.
12621 	  r_type = elfcpp::R_POWERPC_NONE;
12622 	  zap_next = false;
12623 	}
12624 
12625       // Get the new symbol index.
12626       Output_section* os = NULL;
12627       if (r_sym < local_count)
12628 	{
12629 	  switch (strategy)
12630 	    {
12631 	    case Relocatable_relocs::RELOC_COPY:
12632 	    case Relocatable_relocs::RELOC_SPECIAL:
12633 	      if (r_sym != 0)
12634 		{
12635 		  r_sym = object->symtab_index(r_sym);
12636 		  gold_assert(r_sym != -1U);
12637 		}
12638 	      break;
12639 
12640 	    case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA:
12641 	      {
12642 		// We are adjusting a section symbol.  We need to find
12643 		// the symbol table index of the section symbol for
12644 		// the output section corresponding to input section
12645 		// in which this symbol is defined.
12646 		gold_assert(r_sym < local_count);
12647 		bool is_ordinary;
12648 		unsigned int shndx =
12649 		  object->local_symbol_input_shndx(r_sym, &is_ordinary);
12650 		gold_assert(is_ordinary);
12651 		os = object->output_section(shndx);
12652 		gold_assert(os != NULL);
12653 		gold_assert(os->needs_symtab_index());
12654 		r_sym = os->symtab_index();
12655 	      }
12656 	      break;
12657 
12658 	    default:
12659 	      gold_unreachable();
12660 	    }
12661 	}
12662       else
12663 	{
12664 	  gsym = object->global_symbol(r_sym);
12665 	  gold_assert(gsym != NULL);
12666 	  if (gsym->is_forwarder())
12667 	    gsym = relinfo->symtab->resolve_forwards(gsym);
12668 
12669 	  gold_assert(gsym->has_symtab_index());
12670 	  r_sym = gsym->symtab_index();
12671 	}
12672 
12673       // Get the new offset--the location in the output section where
12674       // this relocation should be applied.
12675       if (static_cast<Address>(offset_in_output_section) != invalid_address)
12676 	offset += offset_in_output_section;
12677       else
12678 	{
12679 	  section_offset_type sot_offset =
12680 	    convert_types<section_offset_type, Address>(offset);
12681 	  section_offset_type new_sot_offset =
12682 	    output_section->output_offset(object, relinfo->data_shndx,
12683 					  sot_offset);
12684 	  gold_assert(new_sot_offset != -1);
12685 	  offset = new_sot_offset;
12686 	}
12687 
12688       // In an object file, r_offset is an offset within the section.
12689       // In an executable or dynamic object, generated by
12690       // --emit-relocs, r_offset is an absolute address.
12691       if (!relocatable)
12692 	{
12693 	  offset += view_address;
12694 	  if (static_cast<Address>(offset_in_output_section) != invalid_address)
12695 	    offset -= offset_in_output_section;
12696 	}
12697 
12698       // Handle the reloc addend based on the strategy.
12699       if (strategy == Relocatable_relocs::RELOC_COPY)
12700 	;
12701       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
12702 	{
12703 	  const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
12704 	  addend = psymval->value(object, addend);
12705 	  // In a relocatable link, the symbol value is relative to
12706 	  // the start of the output section. For a non-relocatable
12707 	  // link, we need to adjust the addend.
12708 	  if (!relocatable)
12709 	    {
12710 	      gold_assert(os != NULL);
12711 	      addend -= os->address();
12712 	    }
12713 	}
12714       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
12715 	{
12716 	  if (size == 32)
12717 	    {
12718 	      if (addend >= 32768)
12719 		addend += got2_addend;
12720 	    }
12721 	  else if (r_type == elfcpp::R_POWERPC_REL16_HA)
12722 	    {
12723 	      r_type = elfcpp::R_POWERPC_ADDR16_HA;
12724 	      addend -= d_offset;
12725 	    }
12726 	  else if (r_type == elfcpp::R_POWERPC_REL16_LO)
12727 	    {
12728 	      r_type = elfcpp::R_POWERPC_ADDR16_LO;
12729 	      addend -= d_offset + 4;
12730 	    }
12731 	}
12732       else
12733 	gold_unreachable();
12734 
12735       if (!relocatable)
12736 	{
12737 	  if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12738 	      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO
12739 	      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HI
12740 	      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_HA)
12741 	    {
12742 	      // First instruction of a global dynamic sequence,
12743 	      // arg setup insn.
12744 	      bool final = gsym == NULL || gsym->final_value_is_known();
12745 	      tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12746 	      switch (tls_type)
12747 		{
12748 		case tls::TLSOPT_TO_IE:
12749 		  r_type += (elfcpp::R_POWERPC_GOT_TPREL16
12750 			     - elfcpp::R_POWERPC_GOT_TLSGD16);
12751 		  break;
12752 		case tls::TLSOPT_TO_LE:
12753 		  if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
12754 		      || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
12755 		    r_type = elfcpp::R_POWERPC_TPREL16_HA;
12756 		  else
12757 		    {
12758 		      r_type = elfcpp::R_POWERPC_NONE;
12759 		      offset -= d_offset;
12760 		    }
12761 		  break;
12762 		default:
12763 		  break;
12764 		}
12765 	    }
12766 	  else if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12767 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO
12768 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HI
12769 		   || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_HA)
12770 	    {
12771 	      // First instruction of a local dynamic sequence,
12772 	      // arg setup insn.
12773 	      tls::Tls_optimization tls_type = this->optimize_tls_ld();
12774 	      if (tls_type == tls::TLSOPT_TO_LE)
12775 		{
12776 		  if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
12777 		      || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
12778 		    {
12779 		      r_type = elfcpp::R_POWERPC_TPREL16_HA;
12780 		      const Output_section* os = relinfo->layout->tls_segment()
12781 			->first_section();
12782 		      gold_assert(os != NULL);
12783 		      gold_assert(os->needs_symtab_index());
12784 		      r_sym = os->symtab_index();
12785 		      addend = dtp_offset;
12786 		    }
12787 		  else
12788 		    {
12789 		      r_type = elfcpp::R_POWERPC_NONE;
12790 		      offset -= d_offset;
12791 		    }
12792 		}
12793 	    }
12794 	  else if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12795 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO
12796 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HI
12797 		   || r_type == elfcpp::R_POWERPC_GOT_TPREL16_HA)
12798 	    {
12799 	      // First instruction of initial exec sequence.
12800 	      bool final = gsym == NULL || gsym->final_value_is_known();
12801 	      if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12802 		{
12803 		  if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
12804 		      || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
12805 		    r_type = elfcpp::R_POWERPC_TPREL16_HA;
12806 		  else
12807 		    {
12808 		      r_type = elfcpp::R_POWERPC_NONE;
12809 		      offset -= d_offset;
12810 		    }
12811 		}
12812 	    }
12813 	  else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
12814 		   || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
12815 	    {
12816 	      // Second instruction of a global dynamic sequence,
12817 	      // the __tls_get_addr call
12818 	      bool final = gsym == NULL || gsym->final_value_is_known();
12819 	      tls::Tls_optimization tls_type = this->optimize_tls_gd(final);
12820 	      switch (tls_type)
12821 		{
12822 		case tls::TLSOPT_TO_IE:
12823 		  r_type = elfcpp::R_POWERPC_NONE;
12824 		  zap_next = true;
12825 		  break;
12826 		case tls::TLSOPT_TO_LE:
12827 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
12828 		  offset += d_offset;
12829 		  zap_next = true;
12830 		  break;
12831 		default:
12832 		  break;
12833 		}
12834 	    }
12835 	  else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
12836 		   || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
12837 	    {
12838 	      // Second instruction of a local dynamic sequence,
12839 	      // the __tls_get_addr call
12840 	      tls::Tls_optimization tls_type = this->optimize_tls_ld();
12841 	      if (tls_type == tls::TLSOPT_TO_LE)
12842 		{
12843 		  const Output_section* os = relinfo->layout->tls_segment()
12844 		    ->first_section();
12845 		  gold_assert(os != NULL);
12846 		  gold_assert(os->needs_symtab_index());
12847 		  r_sym = os->symtab_index();
12848 		  addend = dtp_offset;
12849 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
12850 		  offset += d_offset;
12851 		  zap_next = true;
12852 		}
12853 	    }
12854 	  else if (r_type == elfcpp::R_POWERPC_TLS)
12855 	    {
12856 	      // Second instruction of an initial exec sequence
12857 	      bool final = gsym == NULL || gsym->final_value_is_known();
12858 	      if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
12859 		{
12860 		  r_type = elfcpp::R_POWERPC_TPREL16_LO;
12861 		  offset += d_offset;
12862 		}
12863 	    }
12864 	}
12865 
12866       reloc_write.put_r_offset(offset);
12867       reloc_write.put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type));
12868       reloc_write.put_r_addend(addend);
12869 
12870       pwrite += reloc_size;
12871     }
12872 
12873   gold_assert(static_cast<section_size_type>(pwrite - reloc_view)
12874 	      == reloc_view_size);
12875 }
12876 
12877 // Return the value to use for a dynamic symbol which requires special
12878 // treatment.  This is how we support equality comparisons of function
12879 // pointers across shared library boundaries, as described in the
12880 // processor specific ABI supplement.
12881 
12882 template<int size, bool big_endian>
12883 uint64_t
do_dynsym_value(const Symbol * gsym) const12884 Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
12885 {
12886   if (size == 32)
12887     {
12888       gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
12889       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12890 	   p != this->stub_tables_.end();
12891 	   ++p)
12892 	{
12893 	  const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12894 	    = (*p)->find_plt_call_entry(gsym);
12895 	  if (ent != NULL)
12896 	    return (*p)->stub_address() + ent->off_;
12897 	}
12898     }
12899   else if (this->abiversion() >= 2)
12900     {
12901       Address off = this->glink_section()->find_global_entry(gsym);
12902       if (off != invalid_address)
12903 	return this->glink_section()->global_entry_address() + off;
12904     }
12905   gold_unreachable();
12906 }
12907 
12908 // Return the PLT address to use for a local symbol.
12909 template<int size, bool big_endian>
12910 uint64_t
do_plt_address_for_local(const Relobj * object,unsigned int symndx) const12911 Target_powerpc<size, big_endian>::do_plt_address_for_local(
12912     const Relobj* object,
12913     unsigned int symndx) const
12914 {
12915   if (size == 32)
12916     {
12917       const Sized_relobj<size, big_endian>* relobj
12918 	= static_cast<const Sized_relobj<size, big_endian>*>(object);
12919       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12920 	   p != this->stub_tables_.end();
12921 	   ++p)
12922 	{
12923 	  const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12924 	    = (*p)->find_plt_call_entry(relobj->sized_relobj(), symndx);
12925 	  if (ent != NULL)
12926 	    return (*p)->stub_address() + ent->off_;
12927 	}
12928     }
12929   gold_unreachable();
12930 }
12931 
12932 // Return the PLT address to use for a global symbol.
12933 template<int size, bool big_endian>
12934 uint64_t
do_plt_address_for_global(const Symbol * gsym) const12935 Target_powerpc<size, big_endian>::do_plt_address_for_global(
12936     const Symbol* gsym) const
12937 {
12938   if (size == 32)
12939     {
12940       for (typename Stub_tables::const_iterator p = this->stub_tables_.begin();
12941 	   p != this->stub_tables_.end();
12942 	   ++p)
12943 	{
12944 	  const typename Stub_table<size, big_endian>::Plt_stub_ent* ent
12945 	    = (*p)->find_plt_call_entry(gsym);
12946 	  if (ent != NULL)
12947 	    return (*p)->stub_address() + ent->off_;
12948 	}
12949     }
12950   else if (this->abiversion() >= 2)
12951     {
12952       Address off = this->glink_section()->find_global_entry(gsym);
12953       if (off != invalid_address)
12954 	return this->glink_section()->global_entry_address() + off;
12955     }
12956   gold_unreachable();
12957 }
12958 
12959 // Return the offset to use for the GOT_INDX'th got entry which is
12960 // for a local tls symbol specified by OBJECT, SYMNDX.
12961 template<int size, bool big_endian>
12962 int64_t
do_tls_offset_for_local(const Relobj * object,unsigned int symndx,Output_data_got_base * got,unsigned int got_indx,uint64_t addend) const12963 Target_powerpc<size, big_endian>::do_tls_offset_for_local(
12964     const Relobj* object,
12965     unsigned int symndx,
12966     Output_data_got_base* got,
12967     unsigned int got_indx,
12968     uint64_t addend) const
12969 {
12970   const Powerpc_relobj<size, big_endian>* ppc_object
12971     = static_cast<const Powerpc_relobj<size, big_endian>*>(object);
12972   if (ppc_object->local_symbol(symndx)->is_tls_symbol())
12973     {
12974       for (Got_type got_type = (size == 32
12975 				? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
12976 	   got_type <= GOT_TYPE_SMALL_TPREL;
12977 	   got_type = Got_type(got_type + 1))
12978 	if (got_type != GOT_TYPE_SMALL
12979 	    && ppc_object->local_has_got_offset(symndx, got_type, addend))
12980 	  {
12981 	    unsigned int off
12982 	      = ppc_object->local_got_offset(symndx, got_type, addend);
12983 	    if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
12984 	      off += size / 8;
12985 	    if (off == got_indx * (size / 8)
12986 		&& (size == 32 || got == this->got_section(got_type)))
12987 	      {
12988 		if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
12989 		  return -tp_offset;
12990 		else
12991 		  return -dtp_offset;
12992 	      }
12993 	  }
12994     }
12995   gold_unreachable();
12996 }
12997 
12998 // Return the offset to use for the GOT_INDX'th got entry which is
12999 // for global tls symbol GSYM.
13000 template<int size, bool big_endian>
13001 int64_t
do_tls_offset_for_global(Symbol * gsym,Output_data_got_base * got,unsigned int got_indx,uint64_t addend) const13002 Target_powerpc<size, big_endian>::do_tls_offset_for_global(
13003     Symbol* gsym,
13004     Output_data_got_base* got,
13005     unsigned int got_indx,
13006     uint64_t addend) const
13007 {
13008   if (gsym->type() == elfcpp::STT_TLS)
13009     {
13010       for (Got_type got_type = (size == 32
13011 				? GOT_TYPE_SMALL_TLSGD : GOT_TYPE_TLSGD);
13012 	   got_type <= GOT_TYPE_SMALL_TPREL;
13013 	   got_type = Got_type(got_type + 1))
13014 	if (got_type != GOT_TYPE_SMALL
13015 	    && gsym->has_got_offset(got_type, addend))
13016 	  {
13017 	    unsigned int off = gsym->got_offset(got_type, addend);
13018 	    if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TLSGD)
13019 	      off += size / 8;
13020 	    if (off == got_indx * (size / 8)
13021 		&& (size == 32 || got == this->got_section(got_type)))
13022 	      {
13023 		if ((got_type & ~GOT_TYPE_SMALL) == GOT_TYPE_TPREL)
13024 		  return -tp_offset;
13025 		else
13026 		  return -dtp_offset;
13027 	      }
13028 	  }
13029     }
13030   gold_unreachable();
13031 }
13032 
13033 // The selector for powerpc object files.
13034 
13035 template<int size, bool big_endian>
13036 class Target_selector_powerpc : public Target_selector
13037 {
13038 public:
Target_selector_powerpc()13039   Target_selector_powerpc()
13040     : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
13041 		      size, big_endian,
13042 		      (size == 64
13043 		       ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
13044 		       : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
13045 		      (size == 64
13046 		       ? (big_endian ? "elf64ppc" : "elf64lppc")
13047 		       : (big_endian ? "elf32ppc" : "elf32lppc")))
13048   { }
13049 
13050   virtual Target*
do_instantiate_target()13051   do_instantiate_target()
13052   { return new Target_powerpc<size, big_endian>(); }
13053 };
13054 
13055 Target_selector_powerpc<32, true> target_selector_ppc32;
13056 Target_selector_powerpc<32, false> target_selector_ppc32le;
13057 Target_selector_powerpc<64, true> target_selector_ppc64;
13058 Target_selector_powerpc<64, false> target_selector_ppc64le;
13059 
13060 // Instantiate these constants for -O0
13061 template<int size, bool big_endian>
13062 const typename Output_data_glink<size, big_endian>::Address
13063   Output_data_glink<size, big_endian>::invalid_address;
13064 template<int size, bool big_endian>
13065 const typename Stub_table<size, big_endian>::Address
13066   Stub_table<size, big_endian>::invalid_address;
13067 template<int size, bool big_endian>
13068 const typename Target_powerpc<size, big_endian>::Address
13069   Target_powerpc<size, big_endian>::invalid_address;
13070 
13071 } // End anonymous namespace.
13072