1 // sparc.cc -- sparc target support for gold.
2 
3 // Copyright (C) 2008-2021 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>.
5 
6 // This file is part of gold.
7 
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12 
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22 
23 #include "gold.h"
24 
25 #include <cstdlib>
26 #include <cstdio>
27 #include <cstring>
28 
29 #include "elfcpp.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "sparc.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 
45 namespace
46 {
47 
48 using namespace gold;
49 
50 template<int size, bool big_endian>
51 class Output_data_plt_sparc;
52 
53 template<int size, bool big_endian>
54 class Target_sparc : public Sized_target<size, big_endian>
55 {
56  public:
57   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
58 
Target_sparc()59   Target_sparc()
60     : Sized_target<size, big_endian>(&sparc_info),
61       got_(NULL), plt_(NULL), rela_dyn_(NULL), rela_ifunc_(NULL),
62       copy_relocs_(elfcpp::R_SPARC_COPY),
63       got_mod_index_offset_(-1U), tls_get_addr_sym_(NULL),
64       elf_machine_(sparc_info.machine_code), elf_flags_(0),
65       elf_flags_set_(false), register_syms_()
66   {
67   }
68 
69   // Make a new symbol table entry.
70   Sized_symbol<size>*
71   make_symbol(const char*, elfcpp::STT, Object*, unsigned int, uint64_t);
72 
73   // Process the relocations to determine unreferenced sections for
74   // garbage collection.
75   void
76   gc_process_relocs(Symbol_table* symtab,
77 		    Layout* layout,
78 		    Sized_relobj_file<size, big_endian>* object,
79 		    unsigned int data_shndx,
80 		    unsigned int sh_type,
81 		    const unsigned char* prelocs,
82 		    size_t reloc_count,
83 		    Output_section* output_section,
84 		    bool needs_special_offset_handling,
85 		    size_t local_symbol_count,
86 		    const unsigned char* plocal_symbols);
87 
88   // Scan the relocations to look for symbol adjustments.
89   void
90   scan_relocs(Symbol_table* symtab,
91 	      Layout* layout,
92 	      Sized_relobj_file<size, big_endian>* object,
93 	      unsigned int data_shndx,
94 	      unsigned int sh_type,
95 	      const unsigned char* prelocs,
96 	      size_t reloc_count,
97 	      Output_section* output_section,
98 	      bool needs_special_offset_handling,
99 	      size_t local_symbol_count,
100 	      const unsigned char* plocal_symbols);
101   // Finalize the sections.
102   void
103   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
104 
105   // Return the value to use for a dynamic which requires special
106   // treatment.
107   uint64_t
108   do_dynsym_value(const Symbol*) const;
109 
110   // Relocate a section.
111   void
112   relocate_section(const Relocate_info<size, big_endian>*,
113 		   unsigned int sh_type,
114 		   const unsigned char* prelocs,
115 		   size_t reloc_count,
116 		   Output_section* output_section,
117 		   bool needs_special_offset_handling,
118 		   unsigned char* view,
119 		   typename elfcpp::Elf_types<size>::Elf_Addr view_address,
120 		   section_size_type view_size,
121 		   const Reloc_symbol_changes*);
122 
123   // Scan the relocs during a relocatable link.
124   void
125   scan_relocatable_relocs(Symbol_table* symtab,
126 			  Layout* layout,
127 			  Sized_relobj_file<size, big_endian>* object,
128 			  unsigned int data_shndx,
129 			  unsigned int sh_type,
130 			  const unsigned char* prelocs,
131 			  size_t reloc_count,
132 			  Output_section* output_section,
133 			  bool needs_special_offset_handling,
134 			  size_t local_symbol_count,
135 			  const unsigned char* plocal_symbols,
136 			  Relocatable_relocs*);
137 
138   // Scan the relocs for --emit-relocs.
139   void
140   emit_relocs_scan(Symbol_table* symtab,
141 		   Layout* layout,
142 		   Sized_relobj_file<size, big_endian>* object,
143 		   unsigned int data_shndx,
144 		   unsigned int sh_type,
145 		   const unsigned char* prelocs,
146 		   size_t reloc_count,
147 		   Output_section* output_section,
148 		   bool needs_special_offset_handling,
149 		   size_t local_symbol_count,
150 		   const unsigned char* plocal_syms,
151 		   Relocatable_relocs* rr);
152 
153   // Emit relocations for a section.
154   void
155   relocate_relocs(const Relocate_info<size, big_endian>*,
156 		  unsigned int sh_type,
157 		  const unsigned char* prelocs,
158 		  size_t reloc_count,
159 		  Output_section* output_section,
160 		  typename elfcpp::Elf_types<size>::Elf_Off
161                     offset_in_output_section,
162 		  unsigned char* view,
163 		  typename elfcpp::Elf_types<size>::Elf_Addr view_address,
164 		  section_size_type view_size,
165 		  unsigned char* reloc_view,
166 		  section_size_type reloc_view_size);
167 
168   // Return whether SYM is defined by the ABI.
169   bool
do_is_defined_by_abi(const Symbol * sym) const170   do_is_defined_by_abi(const Symbol* sym) const
171   { return strcmp(sym->name(), "___tls_get_addr") == 0; }
172 
173   // Return the PLT address to use for a global symbol.
174   uint64_t
do_plt_address_for_global(const Symbol * gsym) const175   do_plt_address_for_global(const Symbol* gsym) const
176   { return this->plt_section()->address_for_global(gsym); }
177 
178   uint64_t
do_plt_address_for_local(const Relobj * relobj,unsigned int symndx) const179   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
180   { return this->plt_section()->address_for_local(relobj, symndx); }
181 
182   // Return whether there is a GOT section.
183   bool
has_got_section() const184   has_got_section() const
185   { return this->got_ != NULL; }
186 
187   // Return the size of the GOT section.
188   section_size_type
got_size() const189   got_size() const
190   {
191     gold_assert(this->got_ != NULL);
192     return this->got_->data_size();
193   }
194 
195   // Return the number of entries in the GOT.
196   unsigned int
got_entry_count() const197   got_entry_count() const
198   {
199     if (this->got_ == NULL)
200       return 0;
201     return this->got_size() / (size / 8);
202   }
203 
204   // Return the address of the GOT.
205   uint64_t
got_address() const206   got_address() const
207   {
208     if (this->got_ == NULL)
209       return 0;
210     return this->got_->address();
211   }
212 
213   // Return the number of entries in the PLT.
214   unsigned int
215   plt_entry_count() const;
216 
217   // Return the offset of the first non-reserved PLT entry.
218   unsigned int
219   first_plt_entry_offset() const;
220 
221   // Return the size of each PLT entry.
222   unsigned int
223   plt_entry_size() const;
224 
225  protected:
226   // Make an ELF object.
227   Object*
228   do_make_elf_object(const std::string&, Input_file*, off_t,
229 		     const elfcpp::Ehdr<size, big_endian>& ehdr);
230 
231   void
232   do_adjust_elf_header(unsigned char* view, int len);
233 
234  private:
235 
236   // The class which scans relocations.
237   class Scan
238   {
239   public:
Scan()240     Scan()
241       : issued_non_pic_error_(false)
242     { }
243 
244     static inline int
245     get_reference_flags(unsigned int r_type);
246 
247     inline void
248     local(Symbol_table* symtab, Layout* layout, Target_sparc* target,
249 	  Sized_relobj_file<size, big_endian>* object,
250 	  unsigned int data_shndx,
251 	  Output_section* output_section,
252 	  const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
253 	  const elfcpp::Sym<size, big_endian>& lsym,
254 	  bool is_discarded);
255 
256     inline void
257     global(Symbol_table* symtab, Layout* layout, Target_sparc* target,
258 	   Sized_relobj_file<size, big_endian>* object,
259 	   unsigned int data_shndx,
260 	   Output_section* output_section,
261 	   const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
262 	   Symbol* gsym);
263 
264     inline bool
local_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_sparc *,Sized_relobj_file<size,big_endian> *,unsigned int,Output_section *,const elfcpp::Rela<size,big_endian> &,unsigned int,const elfcpp::Sym<size,big_endian> &)265     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
266 					Target_sparc* ,
267 					Sized_relobj_file<size, big_endian>* ,
268 					unsigned int ,
269 					Output_section* ,
270 					const elfcpp::Rela<size, big_endian>& ,
271 					unsigned int ,
272 					const elfcpp::Sym<size, big_endian>&)
273     { return false; }
274 
275     inline bool
global_reloc_may_be_function_pointer(Symbol_table *,Layout *,Target_sparc *,Sized_relobj_file<size,big_endian> *,unsigned int,Output_section *,const elfcpp::Rela<size,big_endian> &,unsigned int,Symbol *)276     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
277 					 Target_sparc* ,
278 					 Sized_relobj_file<size, big_endian>* ,
279 					 unsigned int ,
280 					 Output_section* ,
281 					 const elfcpp::Rela<size,
282 							    big_endian>& ,
283 					 unsigned int , Symbol*)
284     { return false; }
285 
286 
287   private:
288     static void
289     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
290 			    unsigned int r_type);
291 
292     static void
293     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
294 			     unsigned int r_type, Symbol*);
295 
296     static void
297     generate_tls_call(Symbol_table* symtab, Layout* layout,
298 		      Target_sparc* target);
299 
300     void
301     check_non_pic(Relobj*, unsigned int r_type);
302 
303     bool
304     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
305 			      unsigned int r_type);
306 
307     // Whether we have issued an error about a non-PIC compilation.
308     bool issued_non_pic_error_;
309   };
310 
311   // The class which implements relocation.
312   class Relocate
313   {
314    public:
Relocate()315     Relocate()
316       : ignore_gd_add_(false), reloc_adjust_addr_(NULL)
317     { }
318 
~Relocate()319     ~Relocate()
320     {
321       if (this->ignore_gd_add_)
322 	{
323 	  // FIXME: This needs to specify the location somehow.
324 	  gold_error(_("missing expected TLS relocation"));
325 	}
326     }
327 
328     // Do a relocation.  Return false if the caller should not issue
329     // any warnings about this relocation.
330     inline bool
331     relocate(const Relocate_info<size, big_endian>*, unsigned int,
332 	     Target_sparc*, Output_section*, size_t, const unsigned char*,
333 	     const Sized_symbol<size>*, const Symbol_value<size>*,
334 	     unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
335 	     section_size_type);
336 
337    private:
338     // Do a TLS relocation.
339     inline void
340     relocate_tls(const Relocate_info<size, big_endian>*, Target_sparc* target,
341 		 size_t relnum, const elfcpp::Rela<size, big_endian>&,
342 		 unsigned int r_type, const Sized_symbol<size>*,
343 		 const Symbol_value<size>*,
344 		 unsigned char*,
345 		 typename elfcpp::Elf_types<size>::Elf_Addr,
346 		 section_size_type);
347 
348     inline void
349     relax_call(Target_sparc<size, big_endian>* target,
350 	       unsigned char* view,
351 	       const elfcpp::Rela<size, big_endian>& rela,
352 	       section_size_type view_size);
353 
354     // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
355     bool ignore_gd_add_;
356 
357     // If we hit a reloc at this view address, adjust it back by 4 bytes.
358     unsigned char *reloc_adjust_addr_;
359   };
360 
361   // Get the GOT section, creating it if necessary.
362   Output_data_got<size, big_endian>*
363   got_section(Symbol_table*, Layout*);
364 
365   // Create the PLT section.
366   void
367   make_plt_section(Symbol_table* symtab, Layout* layout);
368 
369   // Create a PLT entry for a global symbol.
370   void
371   make_plt_entry(Symbol_table*, Layout*, Symbol*);
372 
373   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
374   void
375   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
376 			     Sized_relobj_file<size, big_endian>* relobj,
377 			     unsigned int local_sym_index);
378 
379   // Create a GOT entry for the TLS module index.
380   unsigned int
381   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
382 		      Sized_relobj_file<size, big_endian>* object);
383 
384   // Return the gsym for "__tls_get_addr".  Cache if not already
385   // cached.
386   Symbol*
tls_get_addr_sym(Symbol_table * symtab)387   tls_get_addr_sym(Symbol_table* symtab)
388   {
389     if (!this->tls_get_addr_sym_)
390       this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
391     gold_assert(this->tls_get_addr_sym_);
392     return this->tls_get_addr_sym_;
393   }
394 
395   // Get the PLT section.
396   Output_data_plt_sparc<size, big_endian>*
plt_section() const397   plt_section() const
398   {
399     gold_assert(this->plt_ != NULL);
400     return this->plt_;
401   }
402 
403   // Get the dynamic reloc section, creating it if necessary.
404   Reloc_section*
405   rela_dyn_section(Layout*);
406 
407   // Get the section to use for IFUNC relocations.
408   Reloc_section*
409   rela_ifunc_section(Layout*);
410 
411   // Copy a relocation against a global symbol.
412   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)413   copy_reloc(Symbol_table* symtab, Layout* layout,
414 	     Sized_relobj_file<size, big_endian>* object,
415 	     unsigned int shndx, Output_section* output_section,
416 	     Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
417   {
418     unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
419     this->copy_relocs_.copy_reloc(symtab, layout,
420 				  symtab->get_sized_symbol<size>(sym),
421 				  object, shndx, output_section,
422 				  r_type, reloc.get_r_offset(),
423 				  reloc.get_r_addend(),
424 				  this->rela_dyn_section(layout));
425   }
426 
427   // Information about this specific target which we pass to the
428   // general Target structure.
429   static Target::Target_info sparc_info;
430 
431   // The types of GOT entries needed for this platform.
432   // These values are exposed to the ABI in an incremental link.
433   // Do not renumber existing values without changing the version
434   // number of the .gnu_incremental_inputs section.
435   enum Got_type
436   {
437     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
438     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
439     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
440   };
441 
442   struct Register_symbol
443   {
Register_symbol__anoncceca1050111::Target_sparc::Register_symbol444     Register_symbol()
445       : name(NULL), shndx(0), obj(NULL)
446     { }
447     const char* name;
448     unsigned int shndx;
449     Object* obj;
450   };
451 
452   // The GOT section.
453   Output_data_got<size, big_endian>* got_;
454   // The PLT section.
455   Output_data_plt_sparc<size, big_endian>* plt_;
456   // The dynamic reloc section.
457   Reloc_section* rela_dyn_;
458   // The section to use for IFUNC relocs.
459   Reloc_section* rela_ifunc_;
460   // Relocs saved to avoid a COPY reloc.
461   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
462   // Offset of the GOT entry for the TLS module index;
463   unsigned int got_mod_index_offset_;
464   // Cached pointer to __tls_get_addr symbol
465   Symbol* tls_get_addr_sym_;
466   // Accumulated elf machine type
467   elfcpp::Elf_Half elf_machine_;
468   // Accumulated elf header flags
469   elfcpp::Elf_Word elf_flags_;
470   // Whether elf_flags_ has been set for the first time yet
471   bool elf_flags_set_;
472   // STT_SPARC_REGISTER symbols (%g2, %g3, %g6, %g7).
473   Register_symbol register_syms_[4];
474 };
475 
476 template<>
477 Target::Target_info Target_sparc<32, true>::sparc_info =
478 {
479   32,			// size
480   true,			// is_big_endian
481   elfcpp::EM_SPARC,	// machine_code
482   false,		// has_make_symbol
483   false,		// has_resolve
484   false,		// has_code_fill
485   true,			// is_default_stack_executable
486   false,		// can_icf_inline_merge_sections
487   '\0',			// wrap_char
488   "/usr/lib/ld.so.1",	// dynamic_linker
489   0x00010000,		// default_text_segment_address
490   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
491   8 * 1024,		// common_pagesize (overridable by -z common-page-size)
492   false,                // isolate_execinstr
493   0,                    // rosegment_gap
494   elfcpp::SHN_UNDEF,	// small_common_shndx
495   elfcpp::SHN_UNDEF,	// large_common_shndx
496   0,			// small_common_section_flags
497   0,			// large_common_section_flags
498   NULL,			// attributes_section
499   NULL,			// attributes_vendor
500   "_start",		// entry_symbol_name
501   32,			// hash_entry_size
502   elfcpp::SHT_PROGBITS,	// unwind_section_type
503 };
504 
505 template<>
506 Target::Target_info Target_sparc<64, true>::sparc_info =
507 {
508   64,			// size
509   true,			// is_big_endian
510   elfcpp::EM_SPARCV9,	// machine_code
511   true,			// has_make_symbol
512   false,		// has_resolve
513   false,		// has_code_fill
514   true,			// is_default_stack_executable
515   false,		// can_icf_inline_merge_sections
516   '\0',			// wrap_char
517   "/usr/lib/sparcv9/ld.so.1",	// dynamic_linker
518   0x100000,		// default_text_segment_address
519   64 * 1024,		// abi_pagesize (overridable by -z max-page-size)
520   8 * 1024,		// common_pagesize (overridable by -z common-page-size)
521   false,                // isolate_execinstr
522   0,                    // rosegment_gap
523   elfcpp::SHN_UNDEF,	// small_common_shndx
524   elfcpp::SHN_UNDEF,	// large_common_shndx
525   0,			// small_common_section_flags
526   0,			// large_common_section_flags
527   NULL,			// attributes_section
528   NULL,			// attributes_vendor
529   "_start",		// entry_symbol_name
530   32,			// hash_entry_size
531   elfcpp::SHT_PROGBITS,	// unwind_section_type
532 };
533 
534 // We have to take care here, even when operating in little-endian
535 // mode, sparc instructions are still big endian.
536 template<int size, bool big_endian>
537 class Sparc_relocate_functions
538 {
539 private:
540   // Do a simple relocation with the addend in the relocation.
541   template<int valsize>
542   static inline void
rela(unsigned char * view,unsigned int right_shift,typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,typename elfcpp::Swap<size,big_endian>::Valtype value,typename elfcpp::Swap<size,big_endian>::Valtype addend)543   rela(unsigned char* view,
544        unsigned int right_shift,
545        typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
546        typename elfcpp::Swap<size, big_endian>::Valtype value,
547        typename elfcpp::Swap<size, big_endian>::Valtype addend)
548   {
549     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
550     Valtype* wv = reinterpret_cast<Valtype*>(view);
551     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
552     Valtype reloc = ((value + addend) >> right_shift);
553 
554     val &= ~dst_mask;
555     reloc &= dst_mask;
556 
557     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
558   }
559 
560   // Do a simple relocation using a symbol value with the addend in
561   // the relocation.
562   template<int valsize>
563   static inline void
rela(unsigned char * view,unsigned int right_shift,typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Swap<valsize,big_endian>::Valtype addend)564   rela(unsigned char* view,
565        unsigned int right_shift,
566        typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
567        const Sized_relobj_file<size, big_endian>* object,
568        const Symbol_value<size>* psymval,
569        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
570   {
571     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
572     Valtype* wv = reinterpret_cast<Valtype*>(view);
573     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
574     Valtype reloc = (psymval->value(object, addend) >> right_shift);
575 
576     val &= ~dst_mask;
577     reloc &= dst_mask;
578 
579     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
580   }
581 
582   // Do a simple relocation using a symbol value with the addend in
583   // the relocation, unaligned.
584   template<int valsize>
585   static inline void
rela_ua(unsigned char * view,unsigned int right_shift,elfcpp::Elf_Xword dst_mask,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Swap<size,big_endian>::Valtype addend)586   rela_ua(unsigned char* view,
587 	  unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
588 	  const Sized_relobj_file<size, big_endian>* object,
589 	  const Symbol_value<size>* psymval,
590 	  typename elfcpp::Swap<size, big_endian>::Valtype addend)
591   {
592     typedef typename elfcpp::Swap_unaligned<valsize,
593 	    big_endian>::Valtype Valtype;
594     unsigned char* wv = view;
595     Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
596     Valtype reloc = (psymval->value(object, addend) >> right_shift);
597 
598     val &= ~dst_mask;
599     reloc &= dst_mask;
600 
601     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
602   }
603 
604   // Do a simple PC relative relocation with a Symbol_value with the
605   // addend in the relocation.
606   template<int valsize>
607   static inline void
pcrela(unsigned char * view,unsigned int right_shift,typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Swap<size,big_endian>::Valtype addend,typename elfcpp::Elf_types<size>::Elf_Addr address)608   pcrela(unsigned char* view,
609 	 unsigned int right_shift,
610 	 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
611 	 const Sized_relobj_file<size, big_endian>* object,
612 	 const Symbol_value<size>* psymval,
613 	 typename elfcpp::Swap<size, big_endian>::Valtype addend,
614 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
615   {
616     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
617     Valtype* wv = reinterpret_cast<Valtype*>(view);
618     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
619     Valtype reloc = ((psymval->value(object, addend) - address)
620 		     >> right_shift);
621 
622     val &= ~dst_mask;
623     reloc &= dst_mask;
624 
625     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
626   }
627 
628   template<int valsize>
629   static inline void
pcrela_unaligned(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Swap<size,big_endian>::Valtype addend,typename elfcpp::Elf_types<size>::Elf_Addr address)630   pcrela_unaligned(unsigned char* view,
631 		   const Sized_relobj_file<size, big_endian>* object,
632 		   const Symbol_value<size>* psymval,
633 		   typename elfcpp::Swap<size, big_endian>::Valtype addend,
634 		   typename elfcpp::Elf_types<size>::Elf_Addr address)
635   {
636     typedef typename elfcpp::Swap_unaligned<valsize,
637 	    big_endian>::Valtype Valtype;
638     unsigned char* wv = view;
639     Valtype reloc = (psymval->value(object, addend) - address);
640 
641     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
642   }
643 
644   typedef Sparc_relocate_functions<size, big_endian> This;
645   typedef Sparc_relocate_functions<size, true> This_insn;
646 
647 public:
648   // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
649   static inline void
wdisp30(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)650   wdisp30(unsigned char* view,
651 	   const Sized_relobj_file<size, big_endian>* object,
652 	   const Symbol_value<size>* psymval,
653 	   typename elfcpp::Elf_types<size>::Elf_Addr addend,
654 	   typename elfcpp::Elf_types<size>::Elf_Addr address)
655   {
656     This_insn::template pcrela<32>(view, 2, 0x3fffffff, object,
657 				   psymval, addend, address);
658   }
659 
660   // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
661   static inline void
wdisp22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)662   wdisp22(unsigned char* view,
663 	   const Sized_relobj_file<size, big_endian>* object,
664 	   const Symbol_value<size>* psymval,
665 	   typename elfcpp::Elf_types<size>::Elf_Addr addend,
666 	   typename elfcpp::Elf_types<size>::Elf_Addr address)
667   {
668     This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
669 				   psymval, addend, address);
670   }
671 
672   // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
673   static inline void
wdisp19(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)674   wdisp19(unsigned char* view,
675 	  const Sized_relobj_file<size, big_endian>* object,
676 	  const Symbol_value<size>* psymval,
677 	  typename elfcpp::Elf_types<size>::Elf_Addr addend,
678 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
679   {
680     This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
681 				   psymval, addend, address);
682   }
683 
684   // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
685   static inline void
wdisp16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)686   wdisp16(unsigned char* view,
687 	  const Sized_relobj_file<size, big_endian>* object,
688 	  const Symbol_value<size>* psymval,
689 	  typename elfcpp::Elf_types<size>::Elf_Addr addend,
690 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
691   {
692     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
693     Valtype* wv = reinterpret_cast<Valtype*>(view);
694     Valtype val = elfcpp::Swap<32, true>::readval(wv);
695     Valtype reloc = ((psymval->value(object, addend) - address)
696 		     >> 2);
697 
698     // The relocation value is split between the low 14 bits,
699     // and bits 20-21.
700     val &= ~((0x3 << 20) | 0x3fff);
701     reloc = (((reloc & 0xc000) << (20 - 14))
702 	     | (reloc & 0x3ffff));
703 
704     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
705   }
706 
707   // R_SPARC_WDISP10: (Symbol + Addend - Address) >> 2
708   static inline void
wdisp10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)709   wdisp10(unsigned char* view,
710 	  const Sized_relobj_file<size, big_endian>* object,
711 	  const Symbol_value<size>* psymval,
712 	  typename elfcpp::Elf_types<size>::Elf_Addr addend,
713 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
714   {
715     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
716     Valtype* wv = reinterpret_cast<Valtype*>(view);
717     Valtype val = elfcpp::Swap<32, true>::readval(wv);
718     Valtype reloc = ((psymval->value(object, addend) - address)
719 		     >> 2);
720 
721     // The relocation value is split between the low bits 5-12,
722     // and high bits 19-20.
723     val &= ~((0x3 << 19) | (0xff << 5));
724     reloc = (((reloc & 0x300) << (19 - 8))
725 	     | ((reloc & 0xff) << (5 - 0)));
726 
727     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
728   }
729 
730   // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
731   static inline void
pc22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)732   pc22(unsigned char* view,
733        const Sized_relobj_file<size, big_endian>* object,
734        const Symbol_value<size>* psymval,
735        typename elfcpp::Elf_types<size>::Elf_Addr addend,
736        typename elfcpp::Elf_types<size>::Elf_Addr address)
737   {
738     This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
739 				   psymval, addend, address);
740   }
741 
742   // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
743   static inline void
pc10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)744   pc10(unsigned char* view,
745        const Sized_relobj_file<size, big_endian>* object,
746        const Symbol_value<size>* psymval,
747        typename elfcpp::Elf_types<size>::Elf_Addr addend,
748        typename elfcpp::Elf_types<size>::Elf_Addr address)
749   {
750     This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
751 				   psymval, addend, address);
752   }
753 
754   // R_SPARC_HI22: (Symbol + Addend) >> 10
755   static inline void
hi22(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)756   hi22(unsigned char* view,
757        typename elfcpp::Elf_types<size>::Elf_Addr value,
758        typename elfcpp::Elf_types<size>::Elf_Addr addend)
759   {
760     This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
761   }
762 
763   // R_SPARC_HI22: (Symbol + Addend) >> 10
764   static inline void
hi22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)765   hi22(unsigned char* view,
766        const Sized_relobj_file<size, big_endian>* object,
767        const Symbol_value<size>* psymval,
768        typename elfcpp::Elf_types<size>::Elf_Addr addend)
769   {
770     This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
771   }
772 
773   // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
774   static inline void
pcplt22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)775   pcplt22(unsigned char* view,
776 	  const Sized_relobj_file<size, big_endian>* object,
777 	  const Symbol_value<size>* psymval,
778 	  typename elfcpp::Elf_types<size>::Elf_Addr addend,
779 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
780   {
781     This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
782 				   psymval, addend, address);
783   }
784 
785   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
786   static inline void
lo10(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)787   lo10(unsigned char* view,
788        typename elfcpp::Elf_types<size>::Elf_Addr value,
789        typename elfcpp::Elf_types<size>::Elf_Addr addend)
790   {
791     This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
792   }
793 
794   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
795   static inline void
lo10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)796   lo10(unsigned char* view,
797        const Sized_relobj_file<size, big_endian>* object,
798        const Symbol_value<size>* psymval,
799        typename elfcpp::Elf_types<size>::Elf_Addr addend)
800   {
801     This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
802   }
803 
804   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
805   static inline void
lo10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)806   lo10(unsigned char* view,
807        const Sized_relobj_file<size, big_endian>* object,
808        const Symbol_value<size>* psymval,
809        typename elfcpp::Elf_types<size>::Elf_Addr addend,
810        typename elfcpp::Elf_types<size>::Elf_Addr address)
811   {
812     This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
813 				   psymval, addend, address);
814   }
815 
816   // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
817   static inline void
olo10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr addend2)818   olo10(unsigned char* view,
819 	const Sized_relobj_file<size, big_endian>* object,
820 	const Symbol_value<size>* psymval,
821 	typename elfcpp::Elf_types<size>::Elf_Addr addend,
822 	typename elfcpp::Elf_types<size>::Elf_Addr addend2)
823   {
824     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
825     Valtype* wv = reinterpret_cast<Valtype*>(view);
826     Valtype val = elfcpp::Swap<32, true>::readval(wv);
827     Valtype reloc = psymval->value(object, addend);
828 
829     val &= ~0x1fff;
830     reloc &= 0x3ff;
831     reloc += addend2;
832     reloc &= 0x1fff;
833 
834     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
835   }
836 
837   // R_SPARC_22: (Symbol + Addend)
838   static inline void
rela32_22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)839   rela32_22(unsigned char* view,
840 	    const Sized_relobj_file<size, big_endian>* object,
841 	    const Symbol_value<size>* psymval,
842 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
843   {
844     This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
845   }
846 
847   // R_SPARC_13: (Symbol + Addend)
848   static inline void
rela32_13(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)849   rela32_13(unsigned char* view,
850 	    typename elfcpp::Elf_types<size>::Elf_Addr value,
851 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
852   {
853     This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
854   }
855 
856   // R_SPARC_13: (Symbol + Addend)
857   static inline void
rela32_13(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)858   rela32_13(unsigned char* view,
859 	    const Sized_relobj_file<size, big_endian>* object,
860 	    const Symbol_value<size>* psymval,
861 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
862   {
863     This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
864   }
865 
866   // R_SPARC_UA16: (Symbol + Addend)
867   static inline void
ua16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)868   ua16(unsigned char* view,
869        const Sized_relobj_file<size, big_endian>* object,
870        const Symbol_value<size>* psymval,
871        typename elfcpp::Elf_types<size>::Elf_Addr addend)
872   {
873     This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
874   }
875 
876   // R_SPARC_UA32: (Symbol + Addend)
877   static inline void
ua32(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)878   ua32(unsigned char* view,
879        const Sized_relobj_file<size, big_endian>* object,
880        const Symbol_value<size>* psymval,
881        typename elfcpp::Elf_types<size>::Elf_Addr addend)
882   {
883     This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
884   }
885 
886   // R_SPARC_UA64: (Symbol + Addend)
887   static inline void
ua64(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)888   ua64(unsigned char* view,
889        const Sized_relobj_file<size, big_endian>* object,
890        const Symbol_value<size>* psymval,
891        typename elfcpp::Elf_types<size>::Elf_Addr addend)
892   {
893     This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
894 			       object, psymval, addend);
895   }
896 
897   // R_SPARC_DISP8: (Symbol + Addend - Address)
898   static inline void
disp8(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)899   disp8(unsigned char* view,
900 	const Sized_relobj_file<size, big_endian>* object,
901 	const Symbol_value<size>* psymval,
902 	typename elfcpp::Elf_types<size>::Elf_Addr addend,
903 	typename elfcpp::Elf_types<size>::Elf_Addr address)
904   {
905     This::template pcrela_unaligned<8>(view, object, psymval,
906 				       addend, address);
907   }
908 
909   // R_SPARC_DISP16: (Symbol + Addend - Address)
910   static inline void
disp16(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)911   disp16(unsigned char* view,
912 	 const Sized_relobj_file<size, big_endian>* object,
913 	 const Symbol_value<size>* psymval,
914 	 typename elfcpp::Elf_types<size>::Elf_Addr addend,
915 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
916   {
917     This::template pcrela_unaligned<16>(view, object, psymval,
918 					addend, address);
919   }
920 
921   // R_SPARC_DISP32: (Symbol + Addend - Address)
922   static inline void
disp32(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)923   disp32(unsigned char* view,
924 	 const Sized_relobj_file<size, big_endian>* object,
925 	 const Symbol_value<size>* psymval,
926 	 typename elfcpp::Elf_types<size>::Elf_Addr addend,
927 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
928   {
929     This::template pcrela_unaligned<32>(view, object, psymval,
930 					addend, address);
931   }
932 
933   // R_SPARC_DISP64: (Symbol + Addend - Address)
934   static inline void
disp64(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,elfcpp::Elf_Xword addend,typename elfcpp::Elf_types<size>::Elf_Addr address)935   disp64(unsigned char* view,
936 	 const Sized_relobj_file<size, big_endian>* object,
937 	 const Symbol_value<size>* psymval,
938 	 elfcpp::Elf_Xword addend,
939 	 typename elfcpp::Elf_types<size>::Elf_Addr address)
940   {
941     This::template pcrela_unaligned<64>(view, object, psymval,
942 					addend, address);
943   }
944 
945   // R_SPARC_H34: (Symbol + Addend) >> 12
946   static inline void
h34(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)947   h34(unsigned char* view,
948       const Sized_relobj_file<size, big_endian>* object,
949       const Symbol_value<size>* psymval,
950       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
951   {
952     This_insn::template rela<32>(view, 12, 0x003fffff, object, psymval, addend);
953   }
954 
955   // R_SPARC_H44: (Symbol + Addend) >> 22
956   static inline void
h44(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)957   h44(unsigned char* view,
958       const Sized_relobj_file<size, big_endian>* object,
959       const Symbol_value<size>* psymval,
960       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
961   {
962     This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
963   }
964 
965   // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
966   static inline void
m44(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)967   m44(unsigned char* view,
968       const Sized_relobj_file<size, big_endian>* object,
969       const Symbol_value<size>* psymval,
970       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
971   {
972     This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
973   }
974 
975   // R_SPARC_L44: (Symbol + Addend) & 0xfff
976   static inline void
l44(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)977   l44(unsigned char* view,
978       const Sized_relobj_file<size, big_endian>* object,
979       const Symbol_value<size>* psymval,
980       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
981   {
982     This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
983   }
984 
985   // R_SPARC_HH22: (Symbol + Addend) >> 42
986   static inline void
hh22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)987   hh22(unsigned char* view,
988        const Sized_relobj_file<size, big_endian>* object,
989        const Symbol_value<size>* psymval,
990        typename elfcpp::Elf_types<size>::Elf_Addr addend)
991   {
992     This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
993   }
994 
995   // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
996   static inline void
pc_hh22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)997   pc_hh22(unsigned char* view,
998 	  const Sized_relobj_file<size, big_endian>* object,
999 	  const Symbol_value<size>* psymval,
1000 	  typename elfcpp::Elf_types<size>::Elf_Addr addend,
1001 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
1002   {
1003     This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
1004 				   psymval, addend, address);
1005   }
1006 
1007   // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
1008   static inline void
hm10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1009   hm10(unsigned char* view,
1010        const Sized_relobj_file<size, big_endian>* object,
1011        const Symbol_value<size>* psymval,
1012        typename elfcpp::Elf_types<size>::Elf_Addr addend)
1013   {
1014     This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
1015   }
1016 
1017   // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
1018   static inline void
pc_hm10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend,typename elfcpp::Elf_types<size>::Elf_Addr address)1019   pc_hm10(unsigned char* view,
1020 	  const Sized_relobj_file<size, big_endian>* object,
1021 	  const Symbol_value<size>* psymval,
1022 	  typename elfcpp::Elf_types<size>::Elf_Addr addend,
1023 	  typename elfcpp::Elf_types<size>::Elf_Addr address)
1024   {
1025     This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
1026 				   psymval, addend, address);
1027   }
1028 
1029   // R_SPARC_11: (Symbol + Addend)
1030   static inline void
rela32_11(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1031   rela32_11(unsigned char* view,
1032 	    const Sized_relobj_file<size, big_endian>* object,
1033 	    const Symbol_value<size>* psymval,
1034 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
1035   {
1036     This_insn::template rela<32>(view, 0, 0x000007ff, object, psymval, addend);
1037   }
1038 
1039   // R_SPARC_10: (Symbol + Addend)
1040   static inline void
rela32_10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1041   rela32_10(unsigned char* view,
1042 	    const Sized_relobj_file<size, big_endian>* object,
1043 	    const Symbol_value<size>* psymval,
1044 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
1045   {
1046     This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
1047   }
1048 
1049   // R_SPARC_7: (Symbol + Addend)
1050   static inline void
rela32_7(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1051   rela32_7(unsigned char* view,
1052 	   const Sized_relobj_file<size, big_endian>* object,
1053 	   const Symbol_value<size>* psymval,
1054 	   typename elfcpp::Elf_types<size>::Elf_Addr addend)
1055   {
1056     This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
1057   }
1058 
1059   // R_SPARC_6: (Symbol + Addend)
1060   static inline void
rela32_6(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1061   rela32_6(unsigned char* view,
1062 	   const Sized_relobj_file<size, big_endian>* object,
1063 	   const Symbol_value<size>* psymval,
1064 	   typename elfcpp::Elf_types<size>::Elf_Addr addend)
1065   {
1066     This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
1067   }
1068 
1069   // R_SPARC_5: (Symbol + Addend)
1070   static inline void
rela32_5(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1071   rela32_5(unsigned char* view,
1072 	   const Sized_relobj_file<size, big_endian>* object,
1073 	   const Symbol_value<size>* psymval,
1074 	   typename elfcpp::Elf_types<size>::Elf_Addr addend)
1075   {
1076     This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
1077   }
1078 
1079   // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
1080   static inline void
ldo_hix22(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)1081   ldo_hix22(unsigned char* view,
1082 	    typename elfcpp::Elf_types<size>::Elf_Addr value,
1083 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
1084   {
1085     This_insn::hi22(view, value, addend);
1086   }
1087 
1088   // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
1089   static inline void
ldo_lox10(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)1090   ldo_lox10(unsigned char* view,
1091 	    typename elfcpp::Elf_types<size>::Elf_Addr value,
1092 	    typename elfcpp::Elf_types<size>::Elf_Addr addend)
1093   {
1094     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1095     Valtype* wv = reinterpret_cast<Valtype*>(view);
1096     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1097     Valtype reloc = (value + addend);
1098 
1099     val &= ~0x1fff;
1100     reloc &= 0x3ff;
1101 
1102     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1103   }
1104 
1105   // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
1106   static inline void
hix22(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)1107   hix22(unsigned char* view,
1108 	typename elfcpp::Elf_types<size>::Elf_Addr value,
1109 	typename elfcpp::Elf_types<size>::Elf_Addr addend)
1110   {
1111     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1112     Valtype* wv = reinterpret_cast<Valtype*>(view);
1113     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1114     Valtype reloc = (value + addend);
1115 
1116     val &= ~0x3fffff;
1117 
1118     reloc ^= ~(Valtype)0;
1119     reloc >>= 10;
1120 
1121     reloc &= 0x3fffff;
1122 
1123     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1124   }
1125 
1126   // R_SPARC_GOTDATA_OP_HIX22: @gdopoff(Symbol + Addend) >> 10
1127   static inline void
gdop_hix22(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value)1128   gdop_hix22(unsigned char* view,
1129 	     typename elfcpp::Elf_types<size>::Elf_Addr value)
1130   {
1131     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1132     Valtype* wv = reinterpret_cast<Valtype*>(view);
1133     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1134     int32_t reloc = static_cast<int32_t>(value);
1135 
1136     val &= ~0x3fffff;
1137 
1138     if (reloc < 0)
1139       reloc ^= ~static_cast<int32_t>(0);
1140     reloc >>= 10;
1141 
1142     reloc &= 0x3fffff;
1143 
1144     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1145   }
1146 
1147   // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
1148   static inline void
hix22(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1149   hix22(unsigned char* view,
1150 	const Sized_relobj_file<size, big_endian>* object,
1151 	const Symbol_value<size>* psymval,
1152 	typename elfcpp::Elf_types<size>::Elf_Addr addend)
1153   {
1154     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1155     Valtype* wv = reinterpret_cast<Valtype*>(view);
1156     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1157     Valtype reloc = psymval->value(object, addend);
1158 
1159     val &= ~0x3fffff;
1160 
1161     reloc ^= ~(Valtype)0;
1162     reloc >>= 10;
1163 
1164     reloc &= 0x3fffff;
1165 
1166     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1167   }
1168 
1169 
1170   // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
1171   static inline void
lox10(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value,typename elfcpp::Elf_types<size>::Elf_Addr addend)1172   lox10(unsigned char* view,
1173 	typename elfcpp::Elf_types<size>::Elf_Addr value,
1174 	typename elfcpp::Elf_types<size>::Elf_Addr addend)
1175   {
1176     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1177     Valtype* wv = reinterpret_cast<Valtype*>(view);
1178     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1179     Valtype reloc = (value + addend);
1180 
1181     val &= ~0x1fff;
1182     reloc &= 0x3ff;
1183     reloc |= 0x1c00;
1184 
1185     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1186   }
1187 
1188   // R_SPARC_GOTDATA_OP_LOX10: (@gdopoff(Symbol + Addend) & 0x3ff) | 0x1c00
1189   static inline void
gdop_lox10(unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr value)1190   gdop_lox10(unsigned char* view,
1191 	     typename elfcpp::Elf_types<size>::Elf_Addr value)
1192   {
1193     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1194     Valtype* wv = reinterpret_cast<Valtype*>(view);
1195     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1196     int32_t reloc = static_cast<int32_t>(value);
1197 
1198     if (reloc < 0)
1199       reloc = (reloc & 0x3ff) | 0x1c00;
1200     else
1201       reloc = (reloc & 0x3ff);
1202 
1203     val &= ~0x1fff;
1204     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1205   }
1206 
1207   // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
1208   static inline void
lox10(unsigned char * view,const Sized_relobj_file<size,big_endian> * object,const Symbol_value<size> * psymval,typename elfcpp::Elf_types<size>::Elf_Addr addend)1209   lox10(unsigned char* view,
1210 	const Sized_relobj_file<size, big_endian>* object,
1211 	const Symbol_value<size>* psymval,
1212 	typename elfcpp::Elf_types<size>::Elf_Addr addend)
1213   {
1214     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1215     Valtype* wv = reinterpret_cast<Valtype*>(view);
1216     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1217     Valtype reloc = psymval->value(object, addend);
1218 
1219     val &= ~0x1fff;
1220     reloc &= 0x3ff;
1221     reloc |= 0x1c00;
1222 
1223     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1224   }
1225 };
1226 
1227 // Get the GOT section, creating it if necessary.
1228 
1229 template<int size, bool big_endian>
1230 Output_data_got<size, big_endian>*
got_section(Symbol_table * symtab,Layout * layout)1231 Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
1232 					    Layout* layout)
1233 {
1234   if (this->got_ == NULL)
1235     {
1236       gold_assert(symtab != NULL && layout != NULL);
1237 
1238       this->got_ = new Output_data_got<size, big_endian>();
1239 
1240       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1241 				      (elfcpp::SHF_ALLOC
1242 				       | elfcpp::SHF_WRITE),
1243 				      this->got_, ORDER_RELRO, true);
1244 
1245       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1246       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1247 				    Symbol_table::PREDEFINED,
1248 				    this->got_,
1249 				    0, 0, elfcpp::STT_OBJECT,
1250 				    elfcpp::STB_LOCAL,
1251 				    elfcpp::STV_HIDDEN, 0,
1252 				    false, false);
1253     }
1254 
1255   return this->got_;
1256 }
1257 
1258 // Get the dynamic reloc section, creating it if necessary.
1259 
1260 template<int size, bool big_endian>
1261 typename Target_sparc<size, big_endian>::Reloc_section*
rela_dyn_section(Layout * layout)1262 Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
1263 {
1264   if (this->rela_dyn_ == NULL)
1265     {
1266       gold_assert(layout != NULL);
1267       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1268       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1269 				      elfcpp::SHF_ALLOC, this->rela_dyn_,
1270 				      ORDER_DYNAMIC_RELOCS, false);
1271     }
1272   return this->rela_dyn_;
1273 }
1274 
1275 // Get the section to use for IFUNC relocs, creating it if
1276 // necessary.  These go in .rela.dyn, but only after all other dynamic
1277 // relocations.  They need to follow the other dynamic relocations so
1278 // that they can refer to global variables initialized by those
1279 // relocs.
1280 
1281 template<int size, bool big_endian>
1282 typename Target_sparc<size, big_endian>::Reloc_section*
rela_ifunc_section(Layout * layout)1283 Target_sparc<size, big_endian>::rela_ifunc_section(Layout* layout)
1284 {
1285   if (this->rela_ifunc_ == NULL)
1286     {
1287       // Make sure we have already created the dynamic reloc section.
1288       this->rela_dyn_section(layout);
1289       this->rela_ifunc_ = new Reloc_section(false);
1290       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1291 				      elfcpp::SHF_ALLOC, this->rela_ifunc_,
1292 				      ORDER_DYNAMIC_RELOCS, false);
1293       gold_assert(this->rela_dyn_->output_section()
1294 		  == this->rela_ifunc_->output_section());
1295     }
1296   return this->rela_ifunc_;
1297 }
1298 
1299 // A class to handle the PLT data.
1300 
1301 template<int size, bool big_endian>
1302 class Output_data_plt_sparc : public Output_section_data
1303 {
1304  public:
1305   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1306 			    size, big_endian> Reloc_section;
1307 
1308   Output_data_plt_sparc(Layout*);
1309 
1310   // Add an entry to the PLT.
1311   void add_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym);
1312 
1313   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
1314   unsigned int
1315   add_local_ifunc_entry(Symbol_table*, Layout*,
1316 			Sized_relobj_file<size, big_endian>* relobj,
1317 			unsigned int local_sym_index);
1318 
1319   // Return the .rela.plt section data.
rel_plt() const1320   const Reloc_section* rel_plt() const
1321   {
1322     return this->rel_;
1323   }
1324 
1325   // Return where the IFUNC relocations should go.
1326   Reloc_section*
1327   rela_ifunc(Symbol_table*, Layout*);
1328 
1329   void
1330   emit_pending_ifunc_relocs();
1331 
1332   // Return whether we created a section for IFUNC relocations.
1333   bool
has_ifunc_section() const1334   has_ifunc_section() const
1335   { return this->ifunc_rel_ != NULL; }
1336 
1337   // Return the number of PLT entries.
1338   unsigned int
entry_count() const1339   entry_count() const
1340   { return this->count_ + this->ifunc_count_; }
1341 
1342   // Return the offset of the first non-reserved PLT entry.
1343   static unsigned int
first_plt_entry_offset()1344   first_plt_entry_offset()
1345   { return 4 * base_plt_entry_size; }
1346 
1347   // Return the size of a PLT entry.
1348   static unsigned int
get_plt_entry_size()1349   get_plt_entry_size()
1350   { return base_plt_entry_size; }
1351 
1352   // Return the PLT address to use for a global symbol.
1353   uint64_t
1354   address_for_global(const Symbol*);
1355 
1356   // Return the PLT address to use for a local symbol.
1357   uint64_t
1358   address_for_local(const Relobj*, unsigned int symndx);
1359 
1360  protected:
1361   void do_adjust_output_section(Output_section* os);
1362 
1363   // Write to a map file.
1364   void
do_print_to_mapfile(Mapfile * mapfile) const1365   do_print_to_mapfile(Mapfile* mapfile) const
1366   { mapfile->print_output_data(this, _("** PLT")); }
1367 
1368  private:
1369   // The size of an entry in the PLT.
1370   static const int base_plt_entry_size = (size == 32 ? 12 : 32);
1371 
1372   static const unsigned int plt_entries_per_block = 160;
1373   static const unsigned int plt_insn_chunk_size = 24;
1374   static const unsigned int plt_pointer_chunk_size = 8;
1375   static const unsigned int plt_block_size =
1376     (plt_entries_per_block
1377      * (plt_insn_chunk_size + plt_pointer_chunk_size));
1378 
1379   section_offset_type
plt_index_to_offset(unsigned int index)1380   plt_index_to_offset(unsigned int index)
1381   {
1382     section_offset_type offset;
1383 
1384     if (size == 32 || index < 32768)
1385       offset = index * base_plt_entry_size;
1386     else
1387       {
1388 	unsigned int ext_index = index - 32768;
1389 
1390 	offset = (32768 * base_plt_entry_size)
1391 	  + ((ext_index / plt_entries_per_block)
1392 	     * plt_block_size)
1393 	  + ((ext_index % plt_entries_per_block)
1394 	     * plt_insn_chunk_size);
1395       }
1396     return offset;
1397   }
1398 
1399   // Set the final size.
1400   void
set_final_data_size()1401   set_final_data_size()
1402   {
1403     unsigned int full_count = this->entry_count() + 4;
1404     unsigned int extra = (size == 32 ? 4 : 0);
1405     section_offset_type sz = plt_index_to_offset(full_count) + extra;
1406 
1407     return this->set_data_size(sz);
1408   }
1409 
1410   // Write out the PLT data.
1411   void
1412   do_write(Output_file*);
1413 
1414   struct Global_ifunc
1415   {
1416     Reloc_section* rel;
1417     Symbol* gsym;
1418     unsigned int plt_index;
1419   };
1420 
1421   struct Local_ifunc
1422   {
1423     Reloc_section* rel;
1424     Sized_relobj_file<size, big_endian>* object;
1425     unsigned int local_sym_index;
1426     unsigned int plt_index;
1427   };
1428 
1429   // The reloc section.
1430   Reloc_section* rel_;
1431   // The IFUNC relocations, if necessary.  These must follow the
1432   // regular relocations.
1433   Reloc_section* ifunc_rel_;
1434   // The number of PLT entries.
1435   unsigned int count_;
1436   // The number of PLT entries for IFUNC symbols.
1437   unsigned int ifunc_count_;
1438   // Global STT_GNU_IFUNC symbols.
1439   std::vector<Global_ifunc> global_ifuncs_;
1440   // Local STT_GNU_IFUNC symbols.
1441   std::vector<Local_ifunc> local_ifuncs_;
1442 };
1443 
1444 // Define the constants as required by C++ standard.
1445 
1446 template<int size, bool big_endian>
1447 const int Output_data_plt_sparc<size, big_endian>::base_plt_entry_size;
1448 
1449 template<int size, bool big_endian>
1450 const unsigned int
1451 Output_data_plt_sparc<size, big_endian>::plt_entries_per_block;
1452 
1453 template<int size, bool big_endian>
1454 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_insn_chunk_size;
1455 
1456 template<int size, bool big_endian>
1457 const unsigned int
1458 Output_data_plt_sparc<size, big_endian>::plt_pointer_chunk_size;
1459 
1460 template<int size, bool big_endian>
1461 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_block_size;
1462 
1463 // Create the PLT section.  The ordinary .got section is an argument,
1464 // since we need to refer to the start.
1465 
1466 template<int size, bool big_endian>
Output_data_plt_sparc(Layout * layout)1467 Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
1468   : Output_section_data(size == 32 ? 4 : 8), ifunc_rel_(NULL),
1469     count_(0), ifunc_count_(0), global_ifuncs_(), local_ifuncs_()
1470 {
1471   this->rel_ = new Reloc_section(false);
1472   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1473 				  elfcpp::SHF_ALLOC, this->rel_,
1474 				  ORDER_DYNAMIC_PLT_RELOCS, false);
1475 }
1476 
1477 template<int size, bool big_endian>
1478 void
do_adjust_output_section(Output_section * os)1479 Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
1480 {
1481   os->set_entsize(0);
1482 }
1483 
1484 // Add an entry to the PLT.
1485 
1486 template<int size, bool big_endian>
1487 void
add_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)1488 Output_data_plt_sparc<size, big_endian>::add_entry(Symbol_table* symtab,
1489 						   Layout* layout,
1490 						   Symbol* gsym)
1491 {
1492   gold_assert(!gsym->has_plt_offset());
1493 
1494   section_offset_type plt_offset;
1495   unsigned int index;
1496 
1497   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1498       && gsym->can_use_relative_reloc(false))
1499     {
1500       index = this->ifunc_count_;
1501       plt_offset = plt_index_to_offset(index);
1502       gsym->set_plt_offset(plt_offset);
1503       ++this->ifunc_count_;
1504       Reloc_section* rel = this->rela_ifunc(symtab, layout);
1505 
1506       struct Global_ifunc gi;
1507       gi.rel = rel;
1508       gi.gsym = gsym;
1509       gi.plt_index = index;
1510       this->global_ifuncs_.push_back(gi);
1511     }
1512   else
1513     {
1514       plt_offset = plt_index_to_offset(this->count_ + 4);
1515       gsym->set_plt_offset(plt_offset);
1516       ++this->count_;
1517       gsym->set_needs_dynsym_entry();
1518       this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
1519 			     plt_offset, 0);
1520     }
1521 
1522   // Note that we don't need to save the symbol.  The contents of the
1523   // PLT are independent of which symbols are used.  The symbols only
1524   // appear in the relocations.
1525 }
1526 
1527 template<int size, bool big_endian>
1528 unsigned int
add_local_ifunc_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * relobj,unsigned int local_sym_index)1529 Output_data_plt_sparc<size, big_endian>::add_local_ifunc_entry(
1530     Symbol_table* symtab,
1531     Layout* layout,
1532     Sized_relobj_file<size, big_endian>* relobj,
1533     unsigned int local_sym_index)
1534 {
1535   unsigned int index = this->ifunc_count_;
1536   section_offset_type plt_offset;
1537 
1538   plt_offset = plt_index_to_offset(index);
1539   ++this->ifunc_count_;
1540 
1541   Reloc_section* rel = this->rela_ifunc(symtab, layout);
1542 
1543   struct Local_ifunc li;
1544   li.rel = rel;
1545   li.object = relobj;
1546   li.local_sym_index = local_sym_index;
1547   li.plt_index = index;
1548   this->local_ifuncs_.push_back(li);
1549 
1550   return plt_offset;
1551 }
1552 
1553 // Emit any pending IFUNC plt relocations.
1554 
1555 template<int size, bool big_endian>
1556 void
emit_pending_ifunc_relocs()1557 Output_data_plt_sparc<size, big_endian>::emit_pending_ifunc_relocs()
1558 {
1559   // Emit any pending IFUNC relocs.
1560   for (typename std::vector<Global_ifunc>::const_iterator p =
1561 	 this->global_ifuncs_.begin();
1562        p != this->global_ifuncs_.end();
1563        ++p)
1564     {
1565       section_offset_type plt_offset;
1566       unsigned int index;
1567 
1568       index = this->count_ + p->plt_index + 4;
1569       plt_offset = this->plt_index_to_offset(index);
1570       p->rel->add_symbolless_global_addend(p->gsym, elfcpp::R_SPARC_JMP_IREL,
1571 					   this, plt_offset, 0);
1572     }
1573 
1574   for (typename std::vector<Local_ifunc>::const_iterator p =
1575 	 this->local_ifuncs_.begin();
1576        p != this->local_ifuncs_.end();
1577        ++p)
1578     {
1579       section_offset_type plt_offset;
1580       unsigned int index;
1581 
1582       index = this->count_ + p->plt_index + 4;
1583       plt_offset = this->plt_index_to_offset(index);
1584       p->rel->add_symbolless_local_addend(p->object, p->local_sym_index,
1585 					  elfcpp::R_SPARC_JMP_IREL,
1586 					  this, plt_offset, 0);
1587     }
1588 }
1589 
1590 // Return where the IFUNC relocations should go in the PLT.  These
1591 // follow the non-IFUNC relocations.
1592 
1593 template<int size, bool big_endian>
1594 typename Output_data_plt_sparc<size, big_endian>::Reloc_section*
rela_ifunc(Symbol_table * symtab,Layout * layout)1595 Output_data_plt_sparc<size, big_endian>::rela_ifunc(
1596 	Symbol_table* symtab,
1597 	Layout* layout)
1598 {
1599   if (this->ifunc_rel_ == NULL)
1600     {
1601       this->ifunc_rel_ = new Reloc_section(false);
1602       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1603 				      elfcpp::SHF_ALLOC, this->ifunc_rel_,
1604 				      ORDER_DYNAMIC_PLT_RELOCS, false);
1605       gold_assert(this->ifunc_rel_->output_section()
1606 		  == this->rel_->output_section());
1607 
1608       if (parameters->doing_static_link())
1609 	{
1610 	  // A statically linked executable will only have a .rel.plt
1611 	  // section to hold R_SPARC_IRELATIVE and R_SPARC_JMP_IREL
1612 	  // relocs for STT_GNU_IFUNC symbols.  The library will use
1613 	  // these symbols to locate the IRELATIVE and JMP_IREL relocs
1614 	  // at program startup time.
1615 	  symtab->define_in_output_data("__rela_iplt_start", NULL,
1616 					Symbol_table::PREDEFINED,
1617 					this->ifunc_rel_, 0, 0,
1618 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1619 					elfcpp::STV_HIDDEN, 0, false, true);
1620 	  symtab->define_in_output_data("__rela_iplt_end", NULL,
1621 					Symbol_table::PREDEFINED,
1622 					this->ifunc_rel_, 0, 0,
1623 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1624 					elfcpp::STV_HIDDEN, 0, true, true);
1625 	}
1626     }
1627   return this->ifunc_rel_;
1628 }
1629 
1630 // Return the PLT address to use for a global symbol.
1631 
1632 template<int size, bool big_endian>
1633 uint64_t
address_for_global(const Symbol * gsym)1634 Output_data_plt_sparc<size, big_endian>::address_for_global(const Symbol* gsym)
1635 {
1636   uint64_t offset = 0;
1637   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1638       && gsym->can_use_relative_reloc(false))
1639     offset = plt_index_to_offset(this->count_ + 4);
1640   return this->address() + offset + gsym->plt_offset();
1641 }
1642 
1643 // Return the PLT address to use for a local symbol.  These are always
1644 // IRELATIVE relocs.
1645 
1646 template<int size, bool big_endian>
1647 uint64_t
address_for_local(const Relobj * object,unsigned int r_sym)1648 Output_data_plt_sparc<size, big_endian>::address_for_local(
1649 	const Relobj* object,
1650 	unsigned int r_sym)
1651 {
1652   return (this->address()
1653 	  + plt_index_to_offset(this->count_ + 4)
1654 	  + object->local_plt_offset(r_sym));
1655 }
1656 
1657 static const unsigned int sparc_nop = 0x01000000;
1658 static const unsigned int sparc_sethi_g1 = 0x03000000;
1659 static const unsigned int sparc_branch_always = 0x30800000;
1660 static const unsigned int sparc_branch_always_pt = 0x30680000;
1661 static const unsigned int sparc_mov = 0x80100000;
1662 static const unsigned int sparc_mov_g0_o0 = 0x90100000;
1663 static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
1664 static const unsigned int sparc_call_plus_8 = 0x40000002;
1665 static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
1666 static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
1667 static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
1668 
1669 // Write out the PLT.
1670 
1671 template<int size, bool big_endian>
1672 void
do_write(Output_file * of)1673 Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
1674 {
1675   const off_t offset = this->offset();
1676   const section_size_type oview_size =
1677     convert_to_section_size_type(this->data_size());
1678   unsigned char* const oview = of->get_output_view(offset, oview_size);
1679   unsigned char* pov = oview;
1680 
1681   memset(pov, 0, base_plt_entry_size * 4);
1682   pov += this->first_plt_entry_offset();
1683 
1684   unsigned int plt_offset = base_plt_entry_size * 4;
1685   const unsigned int count = this->entry_count();
1686 
1687   if (size == 64)
1688     {
1689       unsigned int limit;
1690 
1691       limit = (count > 32768 ? 32768 : count);
1692 
1693       for (unsigned int i = 0; i < limit; ++i)
1694 	{
1695 	  elfcpp::Swap<32, true>::writeval(pov + 0x00,
1696 					   sparc_sethi_g1 + plt_offset);
1697 	  elfcpp::Swap<32, true>::writeval(pov + 0x04,
1698 					   sparc_branch_always_pt +
1699 					   (((base_plt_entry_size -
1700 					      (plt_offset + 4)) >> 2) &
1701 					    0x7ffff));
1702 	  elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1703 	  elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
1704 	  elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
1705 	  elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
1706 	  elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
1707 	  elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
1708 
1709 	  pov += base_plt_entry_size;
1710 	  plt_offset += base_plt_entry_size;
1711 	}
1712 
1713       if (count > 32768)
1714 	{
1715 	  unsigned int ext_cnt = count - 32768;
1716 	  unsigned int blks = ext_cnt / plt_entries_per_block;
1717 
1718 	  for (unsigned int i = 0; i < blks; ++i)
1719 	    {
1720 	      unsigned int data_off = (plt_entries_per_block
1721 				       * plt_insn_chunk_size) - 4;
1722 
1723 	      for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1724 		{
1725 		  elfcpp::Swap<32, true>::writeval(pov + 0x00,
1726 						   sparc_mov_o7_g5);
1727 		  elfcpp::Swap<32, true>::writeval(pov + 0x04,
1728 						   sparc_call_plus_8);
1729 		  elfcpp::Swap<32, true>::writeval(pov + 0x08,
1730 						   sparc_nop);
1731 		  elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1732 						   sparc_ldx_o7_imm_g1 +
1733 						   (data_off & 0x1fff));
1734 		  elfcpp::Swap<32, true>::writeval(pov + 0x10,
1735 						   sparc_jmpl_o7_g1_g1);
1736 		  elfcpp::Swap<32, true>::writeval(pov + 0x14,
1737 						   sparc_mov_g5_o7);
1738 
1739 		  elfcpp::Swap<64, big_endian>::writeval(
1740 				pov + 0x4 + data_off,
1741 				(elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1742 
1743 		  pov += plt_insn_chunk_size;
1744 		  data_off -= 16;
1745 		}
1746 	    }
1747 
1748 	  unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
1749 	  for (unsigned int i = 0; i < sub_blk_cnt; ++i)
1750 	    {
1751 	      unsigned int data_off = (sub_blk_cnt
1752 				       * plt_insn_chunk_size) - 4;
1753 
1754 	      for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1755 		{
1756 		  elfcpp::Swap<32, true>::writeval(pov + 0x00,
1757 						   sparc_mov_o7_g5);
1758 		  elfcpp::Swap<32, true>::writeval(pov + 0x04,
1759 						   sparc_call_plus_8);
1760 		  elfcpp::Swap<32, true>::writeval(pov + 0x08,
1761 						   sparc_nop);
1762 		  elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1763 						   sparc_ldx_o7_imm_g1 +
1764 						   (data_off & 0x1fff));
1765 		  elfcpp::Swap<32, true>::writeval(pov + 0x10,
1766 						   sparc_jmpl_o7_g1_g1);
1767 		  elfcpp::Swap<32, true>::writeval(pov + 0x14,
1768 						   sparc_mov_g5_o7);
1769 
1770 		  elfcpp::Swap<64, big_endian>::writeval(
1771 				pov + 0x4 + data_off,
1772 				(elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1773 
1774 		  pov += plt_insn_chunk_size;
1775 		  data_off -= 16;
1776 		}
1777 	    }
1778 	}
1779     }
1780   else
1781     {
1782       for (unsigned int i = 0; i < count; ++i)
1783 	{
1784 	  elfcpp::Swap<32, true>::writeval(pov + 0x00,
1785 					   sparc_sethi_g1 + plt_offset);
1786 	  elfcpp::Swap<32, true>::writeval(pov + 0x04,
1787 					   sparc_branch_always +
1788 					   (((- (plt_offset + 4)) >> 2) &
1789 					    0x003fffff));
1790 	  elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1791 
1792 	  pov += base_plt_entry_size;
1793 	  plt_offset += base_plt_entry_size;
1794 	}
1795 
1796       elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
1797       pov += 4;
1798     }
1799 
1800   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1801 
1802   of->write_output_view(offset, oview_size, oview);
1803 }
1804 
1805 // Create the PLT section.
1806 
1807 template<int size, bool big_endian>
1808 void
make_plt_section(Symbol_table * symtab,Layout * layout)1809 Target_sparc<size, big_endian>::make_plt_section(Symbol_table* symtab,
1810 						 Layout* layout)
1811 {
1812   // Create the GOT sections first.
1813   this->got_section(symtab, layout);
1814 
1815   // Ensure that .rela.dyn always appears before .rela.plt  This is
1816   // necessary due to how, on Sparc and some other targets, .rela.dyn
1817   // needs to include .rela.plt in it's range.
1818   this->rela_dyn_section(layout);
1819 
1820   this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
1821   layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1822 				  (elfcpp::SHF_ALLOC
1823 				   | elfcpp::SHF_EXECINSTR
1824 				   | elfcpp::SHF_WRITE),
1825 				  this->plt_, ORDER_NON_RELRO_FIRST, false);
1826 
1827   // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1828   symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1829 				Symbol_table::PREDEFINED,
1830 				this->plt_,
1831 				0, 0, elfcpp::STT_OBJECT,
1832 				elfcpp::STB_LOCAL,
1833 				elfcpp::STV_HIDDEN, 0,
1834 				false, false);
1835 }
1836 
1837 // Create a PLT entry for a global symbol.
1838 
1839 template<int size, bool big_endian>
1840 void
make_plt_entry(Symbol_table * symtab,Layout * layout,Symbol * gsym)1841 Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1842 					       Layout* layout,
1843 					       Symbol* gsym)
1844 {
1845   if (gsym->has_plt_offset())
1846     return;
1847 
1848   if (this->plt_ == NULL)
1849     this->make_plt_section(symtab, layout);
1850 
1851   this->plt_->add_entry(symtab, layout, gsym);
1852 }
1853 
1854 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1855 
1856 template<int size, bool big_endian>
1857 void
make_local_ifunc_plt_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * relobj,unsigned int local_sym_index)1858 Target_sparc<size, big_endian>::make_local_ifunc_plt_entry(
1859 	Symbol_table* symtab,
1860 	Layout* layout,
1861 	Sized_relobj_file<size, big_endian>* relobj,
1862 	unsigned int local_sym_index)
1863 {
1864   if (relobj->local_has_plt_offset(local_sym_index))
1865     return;
1866   if (this->plt_ == NULL)
1867     this->make_plt_section(symtab, layout);
1868   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1869 							      relobj,
1870 							      local_sym_index);
1871   relobj->set_local_plt_offset(local_sym_index, plt_offset);
1872 }
1873 
1874 // Return the number of entries in the PLT.
1875 
1876 template<int size, bool big_endian>
1877 unsigned int
plt_entry_count() const1878 Target_sparc<size, big_endian>::plt_entry_count() const
1879 {
1880   if (this->plt_ == NULL)
1881     return 0;
1882   return this->plt_->entry_count();
1883 }
1884 
1885 // Return the offset of the first non-reserved PLT entry.
1886 
1887 template<int size, bool big_endian>
1888 unsigned int
first_plt_entry_offset() const1889 Target_sparc<size, big_endian>::first_plt_entry_offset() const
1890 {
1891   return Output_data_plt_sparc<size, big_endian>::first_plt_entry_offset();
1892 }
1893 
1894 // Return the size of each PLT entry.
1895 
1896 template<int size, bool big_endian>
1897 unsigned int
plt_entry_size() const1898 Target_sparc<size, big_endian>::plt_entry_size() const
1899 {
1900   return Output_data_plt_sparc<size, big_endian>::get_plt_entry_size();
1901 }
1902 
1903 // Create a GOT entry for the TLS module index.
1904 
1905 template<int size, bool big_endian>
1906 unsigned int
got_mod_index_entry(Symbol_table * symtab,Layout * layout,Sized_relobj_file<size,big_endian> * object)1907 Target_sparc<size, big_endian>::got_mod_index_entry(
1908      Symbol_table* symtab,
1909      Layout* layout,
1910      Sized_relobj_file<size, big_endian>* object)
1911 {
1912   if (this->got_mod_index_offset_ == -1U)
1913     {
1914       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1915       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1916       Output_data_got<size, big_endian>* got;
1917       unsigned int got_offset;
1918 
1919       got = this->got_section(symtab, layout);
1920       got_offset = got->add_constant(0);
1921       rela_dyn->add_local(object, 0,
1922 			  (size == 64 ?
1923 			   elfcpp::R_SPARC_TLS_DTPMOD64 :
1924 			   elfcpp::R_SPARC_TLS_DTPMOD32), got,
1925 			  got_offset, 0);
1926       got->add_constant(0);
1927       this->got_mod_index_offset_ = got_offset;
1928     }
1929   return this->got_mod_index_offset_;
1930 }
1931 
1932 // Optimize the TLS relocation type based on what we know about the
1933 // symbol.  IS_FINAL is true if the final address of this symbol is
1934 // known at link time.
1935 
1936 static tls::Tls_optimization
optimize_tls_reloc(bool is_final,int r_type)1937 optimize_tls_reloc(bool is_final, int r_type)
1938 {
1939   // If we are generating a shared library, then we can't do anything
1940   // in the linker.
1941   if (parameters->options().shared())
1942     return tls::TLSOPT_NONE;
1943 
1944   switch (r_type)
1945     {
1946     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1947     case elfcpp::R_SPARC_TLS_GD_LO10:
1948     case elfcpp::R_SPARC_TLS_GD_ADD:
1949     case elfcpp::R_SPARC_TLS_GD_CALL:
1950       // These are General-Dynamic which permits fully general TLS
1951       // access.  Since we know that we are generating an executable,
1952       // we can convert this to Initial-Exec.  If we also know that
1953       // this is a local symbol, we can further switch to Local-Exec.
1954       if (is_final)
1955 	return tls::TLSOPT_TO_LE;
1956       return tls::TLSOPT_TO_IE;
1957 
1958     case elfcpp::R_SPARC_TLS_LDM_HI22:	// Local-dynamic
1959     case elfcpp::R_SPARC_TLS_LDM_LO10:
1960     case elfcpp::R_SPARC_TLS_LDM_ADD:
1961     case elfcpp::R_SPARC_TLS_LDM_CALL:
1962       // This is Local-Dynamic, which refers to a local symbol in the
1963       // dynamic TLS block.  Since we know that we generating an
1964       // executable, we can switch to Local-Exec.
1965       return tls::TLSOPT_TO_LE;
1966 
1967     case elfcpp::R_SPARC_TLS_LDO_HIX22:	// Alternate local-dynamic
1968     case elfcpp::R_SPARC_TLS_LDO_LOX10:
1969     case elfcpp::R_SPARC_TLS_LDO_ADD:
1970       // Another type of Local-Dynamic relocation.
1971       return tls::TLSOPT_TO_LE;
1972 
1973     case elfcpp::R_SPARC_TLS_IE_HI22:	// Initial-exec
1974     case elfcpp::R_SPARC_TLS_IE_LO10:
1975     case elfcpp::R_SPARC_TLS_IE_LD:
1976     case elfcpp::R_SPARC_TLS_IE_LDX:
1977     case elfcpp::R_SPARC_TLS_IE_ADD:
1978       // These are Initial-Exec relocs which get the thread offset
1979       // from the GOT.  If we know that we are linking against the
1980       // local symbol, we can switch to Local-Exec, which links the
1981       // thread offset into the instruction.
1982       if (is_final)
1983 	return tls::TLSOPT_TO_LE;
1984       return tls::TLSOPT_NONE;
1985 
1986     case elfcpp::R_SPARC_TLS_LE_HIX22:	// Local-exec
1987     case elfcpp::R_SPARC_TLS_LE_LOX10:
1988       // When we already have Local-Exec, there is nothing further we
1989       // can do.
1990       return tls::TLSOPT_NONE;
1991 
1992     default:
1993       gold_unreachable();
1994     }
1995 }
1996 
1997 // Get the Reference_flags for a particular relocation.
1998 
1999 template<int size, bool big_endian>
2000 int
get_reference_flags(unsigned int r_type)2001 Target_sparc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
2002 {
2003   r_type &= 0xff;
2004   switch (r_type)
2005     {
2006     case elfcpp::R_SPARC_NONE:
2007     case elfcpp::R_SPARC_REGISTER:
2008     case elfcpp::R_SPARC_GNU_VTINHERIT:
2009     case elfcpp::R_SPARC_GNU_VTENTRY:
2010       // No symbol reference.
2011       return 0;
2012 
2013     case elfcpp::R_SPARC_UA64:
2014     case elfcpp::R_SPARC_64:
2015     case elfcpp::R_SPARC_HIX22:
2016     case elfcpp::R_SPARC_LOX10:
2017     case elfcpp::R_SPARC_H34:
2018     case elfcpp::R_SPARC_H44:
2019     case elfcpp::R_SPARC_M44:
2020     case elfcpp::R_SPARC_L44:
2021     case elfcpp::R_SPARC_HH22:
2022     case elfcpp::R_SPARC_HM10:
2023     case elfcpp::R_SPARC_LM22:
2024     case elfcpp::R_SPARC_HI22:
2025     case elfcpp::R_SPARC_LO10:
2026     case elfcpp::R_SPARC_OLO10:
2027     case elfcpp::R_SPARC_UA32:
2028     case elfcpp::R_SPARC_32:
2029     case elfcpp::R_SPARC_UA16:
2030     case elfcpp::R_SPARC_16:
2031     case elfcpp::R_SPARC_11:
2032     case elfcpp::R_SPARC_10:
2033     case elfcpp::R_SPARC_8:
2034     case elfcpp::R_SPARC_7:
2035     case elfcpp::R_SPARC_6:
2036     case elfcpp::R_SPARC_5:
2037       return Symbol::ABSOLUTE_REF;
2038 
2039     case elfcpp::R_SPARC_DISP8:
2040     case elfcpp::R_SPARC_DISP16:
2041     case elfcpp::R_SPARC_DISP32:
2042     case elfcpp::R_SPARC_DISP64:
2043     case elfcpp::R_SPARC_PC_HH22:
2044     case elfcpp::R_SPARC_PC_HM10:
2045     case elfcpp::R_SPARC_PC_LM22:
2046     case elfcpp::R_SPARC_PC10:
2047     case elfcpp::R_SPARC_PC22:
2048     case elfcpp::R_SPARC_WDISP30:
2049     case elfcpp::R_SPARC_WDISP22:
2050     case elfcpp::R_SPARC_WDISP19:
2051     case elfcpp::R_SPARC_WDISP16:
2052     case elfcpp::R_SPARC_WDISP10:
2053       return Symbol::RELATIVE_REF;
2054 
2055     case elfcpp::R_SPARC_PLT64:
2056     case elfcpp::R_SPARC_PLT32:
2057     case elfcpp::R_SPARC_HIPLT22:
2058     case elfcpp::R_SPARC_LOPLT10:
2059     case elfcpp::R_SPARC_PCPLT10:
2060       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2061 
2062     case elfcpp::R_SPARC_PCPLT32:
2063     case elfcpp::R_SPARC_PCPLT22:
2064     case elfcpp::R_SPARC_WPLT30:
2065       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2066 
2067     case elfcpp::R_SPARC_GOTDATA_OP:
2068     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2069     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2070     case elfcpp::R_SPARC_GOT10:
2071     case elfcpp::R_SPARC_GOT13:
2072     case elfcpp::R_SPARC_GOT22:
2073       // Absolute in GOT.
2074       return Symbol::ABSOLUTE_REF;
2075 
2076     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2077     case elfcpp::R_SPARC_TLS_GD_LO10:
2078     case elfcpp::R_SPARC_TLS_GD_ADD:
2079     case elfcpp::R_SPARC_TLS_GD_CALL:
2080     case elfcpp::R_SPARC_TLS_LDM_HI22:	// Local-dynamic
2081     case elfcpp::R_SPARC_TLS_LDM_LO10:
2082     case elfcpp::R_SPARC_TLS_LDM_ADD:
2083     case elfcpp::R_SPARC_TLS_LDM_CALL:
2084     case elfcpp::R_SPARC_TLS_LDO_HIX22:	// Alternate local-dynamic
2085     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2086     case elfcpp::R_SPARC_TLS_LDO_ADD:
2087     case elfcpp::R_SPARC_TLS_LE_HIX22:
2088     case elfcpp::R_SPARC_TLS_LE_LOX10:
2089     case elfcpp::R_SPARC_TLS_IE_HI22:	// Initial-exec
2090     case elfcpp::R_SPARC_TLS_IE_LO10:
2091     case elfcpp::R_SPARC_TLS_IE_LD:
2092     case elfcpp::R_SPARC_TLS_IE_LDX:
2093     case elfcpp::R_SPARC_TLS_IE_ADD:
2094       return Symbol::TLS_REF;
2095 
2096     case elfcpp::R_SPARC_COPY:
2097     case elfcpp::R_SPARC_GLOB_DAT:
2098     case elfcpp::R_SPARC_JMP_SLOT:
2099     case elfcpp::R_SPARC_JMP_IREL:
2100     case elfcpp::R_SPARC_RELATIVE:
2101     case elfcpp::R_SPARC_IRELATIVE:
2102     case elfcpp::R_SPARC_TLS_DTPMOD64:
2103     case elfcpp::R_SPARC_TLS_DTPMOD32:
2104     case elfcpp::R_SPARC_TLS_DTPOFF64:
2105     case elfcpp::R_SPARC_TLS_DTPOFF32:
2106     case elfcpp::R_SPARC_TLS_TPOFF64:
2107     case elfcpp::R_SPARC_TLS_TPOFF32:
2108     default:
2109       // Not expected.  We will give an error later.
2110       return 0;
2111     }
2112 }
2113 
2114 // Generate a PLT entry slot for a call to __tls_get_addr
2115 template<int size, bool big_endian>
2116 void
generate_tls_call(Symbol_table * symtab,Layout * layout,Target_sparc<size,big_endian> * target)2117 Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
2118 							Layout* layout,
2119 							Target_sparc<size, big_endian>* target)
2120 {
2121   Symbol* gsym = target->tls_get_addr_sym(symtab);
2122 
2123   target->make_plt_entry(symtab, layout, gsym);
2124 }
2125 
2126 // Report an unsupported relocation against a local symbol.
2127 
2128 template<int size, bool big_endian>
2129 void
unsupported_reloc_local(Sized_relobj_file<size,big_endian> * object,unsigned int r_type)2130 Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
2131 			Sized_relobj_file<size, big_endian>* object,
2132 			unsigned int r_type)
2133 {
2134   gold_error(_("%s: unsupported reloc %u against local symbol"),
2135 	     object->name().c_str(), r_type);
2136 }
2137 
2138 // We are about to emit a dynamic relocation of type R_TYPE.  If the
2139 // dynamic linker does not support it, issue an error.
2140 
2141 template<int size, bool big_endian>
2142 void
check_non_pic(Relobj * object,unsigned int r_type)2143 Target_sparc<size, big_endian>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
2144 {
2145   gold_assert(r_type != elfcpp::R_SPARC_NONE);
2146 
2147   if (size == 64)
2148     {
2149       switch (r_type)
2150 	{
2151 	  // These are the relocation types supported by glibc for sparc 64-bit.
2152 	case elfcpp::R_SPARC_RELATIVE:
2153 	case elfcpp::R_SPARC_IRELATIVE:
2154 	case elfcpp::R_SPARC_COPY:
2155 	case elfcpp::R_SPARC_32:
2156 	case elfcpp::R_SPARC_64:
2157 	case elfcpp::R_SPARC_GLOB_DAT:
2158 	case elfcpp::R_SPARC_JMP_SLOT:
2159 	case elfcpp::R_SPARC_JMP_IREL:
2160 	case elfcpp::R_SPARC_TLS_DTPMOD64:
2161 	case elfcpp::R_SPARC_TLS_DTPOFF64:
2162 	case elfcpp::R_SPARC_TLS_TPOFF64:
2163 	case elfcpp::R_SPARC_TLS_LE_HIX22:
2164 	case elfcpp::R_SPARC_TLS_LE_LOX10:
2165 	case elfcpp::R_SPARC_8:
2166 	case elfcpp::R_SPARC_16:
2167 	case elfcpp::R_SPARC_DISP8:
2168 	case elfcpp::R_SPARC_DISP16:
2169 	case elfcpp::R_SPARC_DISP32:
2170 	case elfcpp::R_SPARC_WDISP30:
2171 	case elfcpp::R_SPARC_LO10:
2172 	case elfcpp::R_SPARC_HI22:
2173 	case elfcpp::R_SPARC_OLO10:
2174 	case elfcpp::R_SPARC_H34:
2175 	case elfcpp::R_SPARC_H44:
2176 	case elfcpp::R_SPARC_M44:
2177 	case elfcpp::R_SPARC_L44:
2178 	case elfcpp::R_SPARC_HH22:
2179 	case elfcpp::R_SPARC_HM10:
2180 	case elfcpp::R_SPARC_LM22:
2181 	case elfcpp::R_SPARC_UA16:
2182 	case elfcpp::R_SPARC_UA32:
2183 	case elfcpp::R_SPARC_UA64:
2184 	  return;
2185 
2186 	default:
2187 	  break;
2188 	}
2189     }
2190   else
2191     {
2192       switch (r_type)
2193 	{
2194 	  // These are the relocation types supported by glibc for sparc 32-bit.
2195 	case elfcpp::R_SPARC_RELATIVE:
2196 	case elfcpp::R_SPARC_IRELATIVE:
2197 	case elfcpp::R_SPARC_COPY:
2198 	case elfcpp::R_SPARC_GLOB_DAT:
2199 	case elfcpp::R_SPARC_32:
2200 	case elfcpp::R_SPARC_JMP_SLOT:
2201 	case elfcpp::R_SPARC_JMP_IREL:
2202 	case elfcpp::R_SPARC_TLS_DTPMOD32:
2203 	case elfcpp::R_SPARC_TLS_DTPOFF32:
2204 	case elfcpp::R_SPARC_TLS_TPOFF32:
2205 	case elfcpp::R_SPARC_TLS_LE_HIX22:
2206 	case elfcpp::R_SPARC_TLS_LE_LOX10:
2207 	case elfcpp::R_SPARC_8:
2208 	case elfcpp::R_SPARC_16:
2209 	case elfcpp::R_SPARC_DISP8:
2210 	case elfcpp::R_SPARC_DISP16:
2211 	case elfcpp::R_SPARC_DISP32:
2212 	case elfcpp::R_SPARC_LO10:
2213 	case elfcpp::R_SPARC_WDISP30:
2214 	case elfcpp::R_SPARC_HI22:
2215 	case elfcpp::R_SPARC_UA16:
2216 	case elfcpp::R_SPARC_UA32:
2217 	  return;
2218 
2219 	default:
2220 	  break;
2221 	}
2222     }
2223 
2224   // This prevents us from issuing more than one error per reloc
2225   // section.  But we can still wind up issuing more than one
2226   // error per object file.
2227   if (this->issued_non_pic_error_)
2228     return;
2229   gold_assert(parameters->options().output_is_position_independent());
2230   object->error(_("requires unsupported dynamic reloc; "
2231 		  "recompile with -fPIC"));
2232   this->issued_non_pic_error_ = true;
2233   return;
2234 }
2235 
2236 // Return whether we need to make a PLT entry for a relocation of the
2237 // given type against a STT_GNU_IFUNC symbol.
2238 
2239 template<int size, bool big_endian>
2240 bool
reloc_needs_plt_for_ifunc(Sized_relobj_file<size,big_endian> * object,unsigned int r_type)2241 Target_sparc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
2242      Sized_relobj_file<size, big_endian>* object,
2243      unsigned int r_type)
2244 {
2245   int flags = Scan::get_reference_flags(r_type);
2246   if (flags & Symbol::TLS_REF)
2247     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2248 	       object->name().c_str(), r_type);
2249   return flags != 0;
2250 }
2251 
2252 // Scan a relocation for a local symbol.
2253 
2254 template<int size, bool big_endian>
2255 inline void
local(Symbol_table * symtab,Layout * layout,Target_sparc<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)2256 Target_sparc<size, big_endian>::Scan::local(
2257 			Symbol_table* symtab,
2258 			Layout* layout,
2259 			Target_sparc<size, big_endian>* target,
2260 			Sized_relobj_file<size, big_endian>* object,
2261 			unsigned int data_shndx,
2262 			Output_section* output_section,
2263 			const elfcpp::Rela<size, big_endian>& reloc,
2264 			unsigned int r_type,
2265 			const elfcpp::Sym<size, big_endian>& lsym,
2266 			bool is_discarded)
2267 {
2268   if (is_discarded)
2269     return;
2270 
2271   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2272   unsigned int orig_r_type = r_type;
2273   r_type &= 0xff;
2274 
2275   if (is_ifunc
2276       && this->reloc_needs_plt_for_ifunc(object, r_type))
2277     {
2278       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2279       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2280     }
2281 
2282   switch (r_type)
2283     {
2284     case elfcpp::R_SPARC_NONE:
2285     case elfcpp::R_SPARC_REGISTER:
2286     case elfcpp::R_SPARC_GNU_VTINHERIT:
2287     case elfcpp::R_SPARC_GNU_VTENTRY:
2288       break;
2289 
2290     case elfcpp::R_SPARC_64:
2291     case elfcpp::R_SPARC_32:
2292       // If building a shared library (or a position-independent
2293       // executable), we need to create a dynamic relocation for
2294       // this location. The relocation applied at link time will
2295       // apply the link-time value, so we flag the location with
2296       // an R_SPARC_RELATIVE relocation so the dynamic loader can
2297       // relocate it easily.
2298       if (parameters->options().output_is_position_independent()
2299 	  && ((size == 64 && r_type == elfcpp::R_SPARC_64)
2300 	      || (size == 32 && r_type == elfcpp::R_SPARC_32)))
2301 	{
2302 	  Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2303 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2304 	  rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
2305 				       output_section, data_shndx,
2306 				       reloc.get_r_offset(),
2307 				       reloc.get_r_addend(), is_ifunc);
2308 	  break;
2309 	}
2310       // Fall through.
2311 
2312     case elfcpp::R_SPARC_HIX22:
2313     case elfcpp::R_SPARC_LOX10:
2314     case elfcpp::R_SPARC_H34:
2315     case elfcpp::R_SPARC_H44:
2316     case elfcpp::R_SPARC_M44:
2317     case elfcpp::R_SPARC_L44:
2318     case elfcpp::R_SPARC_HH22:
2319     case elfcpp::R_SPARC_HM10:
2320     case elfcpp::R_SPARC_LM22:
2321     case elfcpp::R_SPARC_UA64:
2322     case elfcpp::R_SPARC_UA32:
2323     case elfcpp::R_SPARC_UA16:
2324     case elfcpp::R_SPARC_HI22:
2325     case elfcpp::R_SPARC_LO10:
2326     case elfcpp::R_SPARC_OLO10:
2327     case elfcpp::R_SPARC_16:
2328     case elfcpp::R_SPARC_11:
2329     case elfcpp::R_SPARC_10:
2330     case elfcpp::R_SPARC_8:
2331     case elfcpp::R_SPARC_7:
2332     case elfcpp::R_SPARC_6:
2333     case elfcpp::R_SPARC_5:
2334       // If building a shared library (or a position-independent
2335       // executable), we need to create a dynamic relocation for
2336       // this location.
2337       if (parameters->options().output_is_position_independent())
2338 	{
2339 	  Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2340 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2341 
2342 	  check_non_pic(object, r_type);
2343 	  if (lsym.get_st_type() != elfcpp::STT_SECTION)
2344 	    {
2345 	      rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
2346 				  data_shndx, reloc.get_r_offset(),
2347 				  reloc.get_r_addend());
2348 	    }
2349 	  else
2350 	    {
2351 	      gold_assert(lsym.get_st_value() == 0);
2352 	      rela_dyn->add_symbolless_local_addend(object, r_sym, orig_r_type,
2353 						    output_section, data_shndx,
2354 						    reloc.get_r_offset(),
2355 						    reloc.get_r_addend());
2356 	    }
2357 	}
2358       break;
2359 
2360     case elfcpp::R_SPARC_WDISP30:
2361     case elfcpp::R_SPARC_WPLT30:
2362     case elfcpp::R_SPARC_WDISP22:
2363     case elfcpp::R_SPARC_WDISP19:
2364     case elfcpp::R_SPARC_WDISP16:
2365     case elfcpp::R_SPARC_WDISP10:
2366     case elfcpp::R_SPARC_DISP8:
2367     case elfcpp::R_SPARC_DISP16:
2368     case elfcpp::R_SPARC_DISP32:
2369     case elfcpp::R_SPARC_DISP64:
2370     case elfcpp::R_SPARC_PC10:
2371     case elfcpp::R_SPARC_PC22:
2372       break;
2373 
2374     case elfcpp::R_SPARC_GOTDATA_OP:
2375     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2376     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2377       // We will optimize this into a GOT relative relocation
2378       // and code transform the GOT load into an addition.
2379       break;
2380 
2381     case elfcpp::R_SPARC_GOT10:
2382     case elfcpp::R_SPARC_GOT13:
2383     case elfcpp::R_SPARC_GOT22:
2384       {
2385 	// The symbol requires a GOT entry.
2386 	Output_data_got<size, big_endian>* got;
2387 	unsigned int r_sym;
2388 
2389 	got = target->got_section(symtab, layout);
2390 	r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2391 
2392 	// If we are generating a shared object, we need to add a
2393 	// dynamic relocation for this symbol's GOT entry.
2394 	if (parameters->options().output_is_position_independent())
2395 	  {
2396 	    if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2397 	      {
2398 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2399 		unsigned int off = got->add_constant(0);
2400 		object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2401 		rela_dyn->add_local_relative(object, r_sym,
2402 					     elfcpp::R_SPARC_RELATIVE,
2403 					     got, off, 0, is_ifunc);
2404 	      }
2405 	  }
2406 	else
2407 	  got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2408       }
2409       break;
2410 
2411       // These are initial TLS relocs, which are expected when
2412       // linking.
2413     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2414     case elfcpp::R_SPARC_TLS_GD_LO10:
2415     case elfcpp::R_SPARC_TLS_GD_ADD:
2416     case elfcpp::R_SPARC_TLS_GD_CALL:
2417     case elfcpp::R_SPARC_TLS_LDM_HI22 :	// Local-dynamic
2418     case elfcpp::R_SPARC_TLS_LDM_LO10:
2419     case elfcpp::R_SPARC_TLS_LDM_ADD:
2420     case elfcpp::R_SPARC_TLS_LDM_CALL:
2421     case elfcpp::R_SPARC_TLS_LDO_HIX22:	// Alternate local-dynamic
2422     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2423     case elfcpp::R_SPARC_TLS_LDO_ADD:
2424     case elfcpp::R_SPARC_TLS_IE_HI22:	// Initial-exec
2425     case elfcpp::R_SPARC_TLS_IE_LO10:
2426     case elfcpp::R_SPARC_TLS_IE_LD:
2427     case elfcpp::R_SPARC_TLS_IE_LDX:
2428     case elfcpp::R_SPARC_TLS_IE_ADD:
2429     case elfcpp::R_SPARC_TLS_LE_HIX22:	// Local-exec
2430     case elfcpp::R_SPARC_TLS_LE_LOX10:
2431       {
2432 	bool output_is_shared = parameters->options().shared();
2433 	const tls::Tls_optimization optimized_type
2434 	    = optimize_tls_reloc(!output_is_shared, r_type);
2435 	switch (r_type)
2436 	  {
2437 	  case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2438 	  case elfcpp::R_SPARC_TLS_GD_LO10:
2439 	  case elfcpp::R_SPARC_TLS_GD_ADD:
2440 	  case elfcpp::R_SPARC_TLS_GD_CALL:
2441 	    if (optimized_type == tls::TLSOPT_NONE)
2442 	      {
2443 		// Create a pair of GOT entries for the module index and
2444 		// dtv-relative offset.
2445 		Output_data_got<size, big_endian>* got
2446 		    = target->got_section(symtab, layout);
2447 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2448 		unsigned int shndx = lsym.get_st_shndx();
2449 		bool is_ordinary;
2450 		shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2451 		if (!is_ordinary)
2452 		  object->error(_("local symbol %u has bad shndx %u"),
2453 				r_sym, shndx);
2454 		else
2455 		  got->add_local_pair_with_rel(object, r_sym,
2456 					       lsym.get_st_shndx(),
2457 					       GOT_TYPE_TLS_PAIR,
2458 					       target->rela_dyn_section(layout),
2459 					       (size == 64
2460 						? elfcpp::R_SPARC_TLS_DTPMOD64
2461 						: elfcpp::R_SPARC_TLS_DTPMOD32));
2462 		if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2463 		  generate_tls_call(symtab, layout, target);
2464 	      }
2465 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2466 	      unsupported_reloc_local(object, r_type);
2467 	    break;
2468 
2469 	  case elfcpp::R_SPARC_TLS_LDM_HI22 :	// Local-dynamic
2470 	  case elfcpp::R_SPARC_TLS_LDM_LO10:
2471 	  case elfcpp::R_SPARC_TLS_LDM_ADD:
2472 	  case elfcpp::R_SPARC_TLS_LDM_CALL:
2473 	    if (optimized_type == tls::TLSOPT_NONE)
2474 	      {
2475 		// Create a GOT entry for the module index.
2476 		target->got_mod_index_entry(symtab, layout, object);
2477 
2478 		if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2479 		  generate_tls_call(symtab, layout, target);
2480 	      }
2481 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2482 	      unsupported_reloc_local(object, r_type);
2483 	    break;
2484 
2485 	  case elfcpp::R_SPARC_TLS_LDO_HIX22:	// Alternate local-dynamic
2486 	  case elfcpp::R_SPARC_TLS_LDO_LOX10:
2487 	  case elfcpp::R_SPARC_TLS_LDO_ADD:
2488 	    break;
2489 
2490 	  case elfcpp::R_SPARC_TLS_IE_HI22:	// Initial-exec
2491 	  case elfcpp::R_SPARC_TLS_IE_LO10:
2492 	  case elfcpp::R_SPARC_TLS_IE_LD:
2493 	  case elfcpp::R_SPARC_TLS_IE_LDX:
2494 	  case elfcpp::R_SPARC_TLS_IE_ADD:
2495 	    layout->set_has_static_tls();
2496 	    if (optimized_type == tls::TLSOPT_NONE)
2497 	      {
2498 		// Create a GOT entry for the tp-relative offset.
2499 		Output_data_got<size, big_endian>* got
2500 		  = target->got_section(symtab, layout);
2501 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2502 
2503 		if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
2504 		  {
2505 		    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2506 		    unsigned int off = got->add_constant(0);
2507 
2508 		    object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET, off);
2509 
2510 		    rela_dyn->add_symbolless_local_addend(object, r_sym,
2511 							  (size == 64 ?
2512 							   elfcpp::R_SPARC_TLS_TPOFF64 :
2513 							   elfcpp::R_SPARC_TLS_TPOFF32),
2514 							  got, off, 0);
2515 		  }
2516 	      }
2517 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2518 	      unsupported_reloc_local(object, r_type);
2519 	    break;
2520 
2521 	  case elfcpp::R_SPARC_TLS_LE_HIX22:	// Local-exec
2522 	  case elfcpp::R_SPARC_TLS_LE_LOX10:
2523 	    layout->set_has_static_tls();
2524 	    if (output_is_shared)
2525 	      {
2526 		// We need to create a dynamic relocation.
2527 		gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2528 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2529 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2530 		rela_dyn->add_symbolless_local_addend(object, r_sym, r_type,
2531 						      output_section, data_shndx,
2532 						      reloc.get_r_offset(), 0);
2533 	      }
2534 	    break;
2535 	  }
2536       }
2537       break;
2538 
2539       // These are relocations which should only be seen by the
2540       // dynamic linker, and should never be seen here.
2541     case elfcpp::R_SPARC_COPY:
2542     case elfcpp::R_SPARC_GLOB_DAT:
2543     case elfcpp::R_SPARC_JMP_SLOT:
2544     case elfcpp::R_SPARC_JMP_IREL:
2545     case elfcpp::R_SPARC_RELATIVE:
2546     case elfcpp::R_SPARC_IRELATIVE:
2547     case elfcpp::R_SPARC_TLS_DTPMOD64:
2548     case elfcpp::R_SPARC_TLS_DTPMOD32:
2549     case elfcpp::R_SPARC_TLS_DTPOFF64:
2550     case elfcpp::R_SPARC_TLS_DTPOFF32:
2551     case elfcpp::R_SPARC_TLS_TPOFF64:
2552     case elfcpp::R_SPARC_TLS_TPOFF32:
2553       gold_error(_("%s: unexpected reloc %u in object file"),
2554 		 object->name().c_str(), r_type);
2555       break;
2556 
2557     default:
2558       unsupported_reloc_local(object, r_type);
2559       break;
2560     }
2561 }
2562 
2563 // Report an unsupported relocation against a global symbol.
2564 
2565 template<int size, bool big_endian>
2566 void
unsupported_reloc_global(Sized_relobj_file<size,big_endian> * object,unsigned int r_type,Symbol * gsym)2567 Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
2568 			Sized_relobj_file<size, big_endian>* object,
2569 			unsigned int r_type,
2570 			Symbol* gsym)
2571 {
2572   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2573 	     object->name().c_str(), r_type, gsym->demangled_name().c_str());
2574 }
2575 
2576 // Scan a relocation for a global symbol.
2577 
2578 template<int size, bool big_endian>
2579 inline void
global(Symbol_table * symtab,Layout * layout,Target_sparc<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)2580 Target_sparc<size, big_endian>::Scan::global(
2581 				Symbol_table* symtab,
2582 				Layout* layout,
2583 				Target_sparc<size, big_endian>* target,
2584 				Sized_relobj_file<size, big_endian>* object,
2585 				unsigned int data_shndx,
2586 				Output_section* output_section,
2587 				const elfcpp::Rela<size, big_endian>& reloc,
2588 				unsigned int r_type,
2589 				Symbol* gsym)
2590 {
2591   unsigned int orig_r_type = r_type;
2592   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
2593 
2594   // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
2595   // section.  We check here to avoid creating a dynamic reloc against
2596   // _GLOBAL_OFFSET_TABLE_.
2597   if (!target->has_got_section()
2598       && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
2599     target->got_section(symtab, layout);
2600 
2601   r_type &= 0xff;
2602 
2603   // A STT_GNU_IFUNC symbol may require a PLT entry.
2604   if (is_ifunc
2605       && this->reloc_needs_plt_for_ifunc(object, r_type))
2606     target->make_plt_entry(symtab, layout, gsym);
2607 
2608   switch (r_type)
2609     {
2610     case elfcpp::R_SPARC_NONE:
2611     case elfcpp::R_SPARC_REGISTER:
2612     case elfcpp::R_SPARC_GNU_VTINHERIT:
2613     case elfcpp::R_SPARC_GNU_VTENTRY:
2614       break;
2615 
2616     case elfcpp::R_SPARC_PLT64:
2617     case elfcpp::R_SPARC_PLT32:
2618     case elfcpp::R_SPARC_HIPLT22:
2619     case elfcpp::R_SPARC_LOPLT10:
2620     case elfcpp::R_SPARC_PCPLT32:
2621     case elfcpp::R_SPARC_PCPLT22:
2622     case elfcpp::R_SPARC_PCPLT10:
2623     case elfcpp::R_SPARC_WPLT30:
2624       // If the symbol is fully resolved, this is just a PC32 reloc.
2625       // Otherwise we need a PLT entry.
2626       if (gsym->final_value_is_known())
2627 	break;
2628       // If building a shared library, we can also skip the PLT entry
2629       // if the symbol is defined in the output file and is protected
2630       // or hidden.
2631       if (gsym->is_defined()
2632 	  && !gsym->is_from_dynobj()
2633 	  && !gsym->is_preemptible())
2634 	break;
2635       target->make_plt_entry(symtab, layout, gsym);
2636       break;
2637 
2638     case elfcpp::R_SPARC_DISP8:
2639     case elfcpp::R_SPARC_DISP16:
2640     case elfcpp::R_SPARC_DISP32:
2641     case elfcpp::R_SPARC_DISP64:
2642     case elfcpp::R_SPARC_PC_HH22:
2643     case elfcpp::R_SPARC_PC_HM10:
2644     case elfcpp::R_SPARC_PC_LM22:
2645     case elfcpp::R_SPARC_PC10:
2646     case elfcpp::R_SPARC_PC22:
2647     case elfcpp::R_SPARC_WDISP30:
2648     case elfcpp::R_SPARC_WDISP22:
2649     case elfcpp::R_SPARC_WDISP19:
2650     case elfcpp::R_SPARC_WDISP16:
2651     case elfcpp::R_SPARC_WDISP10:
2652       {
2653 	if (gsym->needs_plt_entry())
2654 	  target->make_plt_entry(symtab, layout, gsym);
2655 	// Make a dynamic relocation if necessary.
2656 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2657 	  {
2658 	    if (parameters->options().output_is_executable()
2659 		&& gsym->may_need_copy_reloc())
2660 	      {
2661 		target->copy_reloc(symtab, layout, object,
2662 				   data_shndx, output_section, gsym,
2663 				   reloc);
2664 	      }
2665 	    else
2666 	      {
2667 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2668 		check_non_pic(object, r_type);
2669 		rela_dyn->add_global(gsym, orig_r_type, output_section, object,
2670 				     data_shndx, reloc.get_r_offset(),
2671 				     reloc.get_r_addend());
2672 	      }
2673 	  }
2674       }
2675       break;
2676 
2677     case elfcpp::R_SPARC_UA64:
2678     case elfcpp::R_SPARC_64:
2679     case elfcpp::R_SPARC_HIX22:
2680     case elfcpp::R_SPARC_LOX10:
2681     case elfcpp::R_SPARC_H34:
2682     case elfcpp::R_SPARC_H44:
2683     case elfcpp::R_SPARC_M44:
2684     case elfcpp::R_SPARC_L44:
2685     case elfcpp::R_SPARC_HH22:
2686     case elfcpp::R_SPARC_HM10:
2687     case elfcpp::R_SPARC_LM22:
2688     case elfcpp::R_SPARC_HI22:
2689     case elfcpp::R_SPARC_LO10:
2690     case elfcpp::R_SPARC_OLO10:
2691     case elfcpp::R_SPARC_UA32:
2692     case elfcpp::R_SPARC_32:
2693     case elfcpp::R_SPARC_UA16:
2694     case elfcpp::R_SPARC_16:
2695     case elfcpp::R_SPARC_11:
2696     case elfcpp::R_SPARC_10:
2697     case elfcpp::R_SPARC_8:
2698     case elfcpp::R_SPARC_7:
2699     case elfcpp::R_SPARC_6:
2700     case elfcpp::R_SPARC_5:
2701       {
2702 	// Make a PLT entry if necessary.
2703 	if (gsym->needs_plt_entry())
2704 	  {
2705 	    target->make_plt_entry(symtab, layout, gsym);
2706 	    // Since this is not a PC-relative relocation, we may be
2707 	    // taking the address of a function. In that case we need to
2708 	    // set the entry in the dynamic symbol table to the address of
2709 	    // the PLT entry.
2710 	    if (gsym->is_from_dynobj() && !parameters->options().shared())
2711 	      gsym->set_needs_dynsym_value();
2712 	  }
2713 	// Make a dynamic relocation if necessary.
2714 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2715 	  {
2716 	    unsigned int r_off = reloc.get_r_offset();
2717 
2718 	    // The assembler can sometimes emit unaligned relocations
2719 	    // for dwarf2 cfi directives.
2720 	    switch (r_type)
2721 	      {
2722 	      case elfcpp::R_SPARC_16:
2723 		if (r_off & 0x1)
2724 		  orig_r_type = r_type = elfcpp::R_SPARC_UA16;
2725 		break;
2726 	      case elfcpp::R_SPARC_32:
2727 		if (r_off & 0x3)
2728 		  orig_r_type = r_type = elfcpp::R_SPARC_UA32;
2729 		break;
2730 	      case elfcpp::R_SPARC_64:
2731 		if (r_off & 0x7)
2732 		  orig_r_type = r_type = elfcpp::R_SPARC_UA64;
2733 		break;
2734 	      case elfcpp::R_SPARC_UA16:
2735 		if (!(r_off & 0x1))
2736 		  orig_r_type = r_type = elfcpp::R_SPARC_16;
2737 		break;
2738 	      case elfcpp::R_SPARC_UA32:
2739 		if (!(r_off & 0x3))
2740 		  orig_r_type = r_type = elfcpp::R_SPARC_32;
2741 		break;
2742 	      case elfcpp::R_SPARC_UA64:
2743 		if (!(r_off & 0x7))
2744 		  orig_r_type = r_type = elfcpp::R_SPARC_64;
2745 		break;
2746 	      }
2747 
2748 	    if (!parameters->options().output_is_position_independent()
2749 		&& gsym->may_need_copy_reloc())
2750 	      {
2751 		target->copy_reloc(symtab, layout, object,
2752 				   data_shndx, output_section, gsym, reloc);
2753 	      }
2754 	    else if (((size == 64 && r_type == elfcpp::R_SPARC_64)
2755 		      || (size == 32 && r_type == elfcpp::R_SPARC_32))
2756 		     && gsym->type() == elfcpp::STT_GNU_IFUNC
2757 		     && gsym->can_use_relative_reloc(false)
2758 		     && !gsym->is_from_dynobj()
2759 		     && !gsym->is_undefined()
2760 		     && !gsym->is_preemptible())
2761 	      {
2762 		// Use an IRELATIVE reloc for a locally defined
2763 		// STT_GNU_IFUNC symbol.  This makes a function
2764 		// address in a PIE executable match the address in a
2765 		// shared library that it links against.
2766 		Reloc_section* rela_dyn =
2767 		  target->rela_ifunc_section(layout);
2768 		unsigned int r_type = elfcpp::R_SPARC_IRELATIVE;
2769 		rela_dyn->add_symbolless_global_addend(gsym, r_type,
2770 						       output_section, object,
2771 						       data_shndx,
2772 						       reloc.get_r_offset(),
2773 						       reloc.get_r_addend());
2774 	      }
2775 	    else if (((size == 64 && r_type == elfcpp::R_SPARC_64)
2776 		      || (size == 32 && r_type == elfcpp::R_SPARC_32))
2777 		     && gsym->can_use_relative_reloc(false))
2778 	      {
2779 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2780 		rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2781 					      output_section, object,
2782 					      data_shndx, reloc.get_r_offset(),
2783 					      reloc.get_r_addend(), is_ifunc);
2784 	      }
2785 	    else
2786 	      {
2787 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2788 
2789 		check_non_pic(object, r_type);
2790 		if (gsym->is_from_dynobj()
2791 		    || gsym->is_undefined()
2792 		    || gsym->is_preemptible())
2793 		  rela_dyn->add_global(gsym, orig_r_type, output_section,
2794 				       object, data_shndx,
2795 				       reloc.get_r_offset(),
2796 				       reloc.get_r_addend());
2797 		else
2798 		  rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
2799 							 output_section,
2800 							 object, data_shndx,
2801 							 reloc.get_r_offset(),
2802 							 reloc.get_r_addend());
2803 	      }
2804 	  }
2805       }
2806       break;
2807 
2808     case elfcpp::R_SPARC_GOTDATA_OP:
2809     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2810     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2811       if (gsym->is_defined()
2812 	  && !gsym->is_from_dynobj()
2813 	  && !gsym->is_preemptible()
2814 	  && !is_ifunc)
2815 	{
2816 	  // We will optimize this into a GOT relative relocation
2817 	  // and code transform the GOT load into an addition.
2818 	  break;
2819 	}
2820       // Fall through.
2821     case elfcpp::R_SPARC_GOT10:
2822     case elfcpp::R_SPARC_GOT13:
2823     case elfcpp::R_SPARC_GOT22:
2824       {
2825 	// The symbol requires a GOT entry.
2826 	Output_data_got<size, big_endian>* got;
2827 
2828 	got = target->got_section(symtab, layout);
2829 	if (gsym->final_value_is_known())
2830 	  {
2831 	    // For a STT_GNU_IFUNC symbol we want the PLT address.
2832 	    if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2833 	      got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2834 	    else
2835 	      got->add_global(gsym, GOT_TYPE_STANDARD);
2836 	  }
2837 	else
2838 	  {
2839 	    // If this symbol is not fully resolved, we need to add a
2840 	    // GOT entry with a dynamic relocation.
2841 	    bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
2842 
2843 	    // Use a GLOB_DAT rather than a RELATIVE reloc if:
2844 	    //
2845 	    // 1) The symbol may be defined in some other module.
2846 	    //
2847 	    // 2) We are building a shared library and this is a
2848 	    // protected symbol; using GLOB_DAT means that the dynamic
2849 	    // linker can use the address of the PLT in the main
2850 	    // executable when appropriate so that function address
2851 	    // comparisons work.
2852 	    //
2853 	    // 3) This is a STT_GNU_IFUNC symbol in position dependent
2854 	    // code, again so that function address comparisons work.
2855 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2856 	    if (gsym->is_from_dynobj()
2857 		|| gsym->is_undefined()
2858 		|| gsym->is_preemptible()
2859 		|| (gsym->visibility() == elfcpp::STV_PROTECTED
2860 		    && parameters->options().shared())
2861 		|| (gsym->type() == elfcpp::STT_GNU_IFUNC
2862 		    && parameters->options().output_is_position_independent()
2863 		    && !gsym->is_forced_local()))
2864 	      {
2865 		unsigned int r_type = elfcpp::R_SPARC_GLOB_DAT;
2866 
2867 		// If this symbol is forced local, this relocation will
2868 		// not work properly.  That's because ld.so on sparc
2869 		// (and 32-bit powerpc) expects st_value in the r_addend
2870 		// of relocations for STB_LOCAL symbols.  Curiously the
2871 		// BFD linker does not promote global hidden symbols to be
2872 		// STB_LOCAL in the dynamic symbol table like Gold does.
2873 		gold_assert(!gsym->is_forced_local());
2874 		got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2875 					 r_type);
2876 	      }
2877 	    else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2878 	      {
2879 		unsigned int off = got->add_constant(0);
2880 
2881 		gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2882 		if (is_ifunc)
2883 		  {
2884 		    // Tell the dynamic linker to use the PLT address
2885 		    // when resolving relocations.
2886 		    if (gsym->is_from_dynobj()
2887 			&& !parameters->options().shared())
2888 		      gsym->set_needs_dynsym_value();
2889 		  }
2890 		rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2891 					      got, off, 0, is_ifunc);
2892 	      }
2893 	  }
2894       }
2895       break;
2896 
2897       // These are initial tls relocs, which are expected when
2898       // linking.
2899     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2900     case elfcpp::R_SPARC_TLS_GD_LO10:
2901     case elfcpp::R_SPARC_TLS_GD_ADD:
2902     case elfcpp::R_SPARC_TLS_GD_CALL:
2903     case elfcpp::R_SPARC_TLS_LDM_HI22:	// Local-dynamic
2904     case elfcpp::R_SPARC_TLS_LDM_LO10:
2905     case elfcpp::R_SPARC_TLS_LDM_ADD:
2906     case elfcpp::R_SPARC_TLS_LDM_CALL:
2907     case elfcpp::R_SPARC_TLS_LDO_HIX22:	// Alternate local-dynamic
2908     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2909     case elfcpp::R_SPARC_TLS_LDO_ADD:
2910     case elfcpp::R_SPARC_TLS_LE_HIX22:
2911     case elfcpp::R_SPARC_TLS_LE_LOX10:
2912     case elfcpp::R_SPARC_TLS_IE_HI22:	// Initial-exec
2913     case elfcpp::R_SPARC_TLS_IE_LO10:
2914     case elfcpp::R_SPARC_TLS_IE_LD:
2915     case elfcpp::R_SPARC_TLS_IE_LDX:
2916     case elfcpp::R_SPARC_TLS_IE_ADD:
2917       {
2918 	const bool is_final = gsym->final_value_is_known();
2919 	const tls::Tls_optimization optimized_type
2920 	    = optimize_tls_reloc(is_final, r_type);
2921 	switch (r_type)
2922 	  {
2923 	  case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2924 	  case elfcpp::R_SPARC_TLS_GD_LO10:
2925 	  case elfcpp::R_SPARC_TLS_GD_ADD:
2926 	  case elfcpp::R_SPARC_TLS_GD_CALL:
2927 	    if (optimized_type == tls::TLSOPT_NONE)
2928 	      {
2929 		// Create a pair of GOT entries for the module index and
2930 		// dtv-relative offset.
2931 		Output_data_got<size, big_endian>* got
2932 		    = target->got_section(symtab, layout);
2933 		got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2934 					      target->rela_dyn_section(layout),
2935 					      (size == 64
2936 					       ? elfcpp::R_SPARC_TLS_DTPMOD64
2937 					       : elfcpp::R_SPARC_TLS_DTPMOD32),
2938 					      (size == 64
2939 					       ? elfcpp::R_SPARC_TLS_DTPOFF64
2940 					       : elfcpp::R_SPARC_TLS_DTPOFF32));
2941 
2942 		// Emit R_SPARC_WPLT30 against "__tls_get_addr"
2943 		if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2944 		  generate_tls_call(symtab, layout, target);
2945 	      }
2946 	    else if (optimized_type == tls::TLSOPT_TO_IE)
2947 	      {
2948 		// Create a GOT entry for the tp-relative offset.
2949 		Output_data_got<size, big_endian>* got
2950 		    = target->got_section(symtab, layout);
2951 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2952 					 target->rela_dyn_section(layout),
2953 					 (size == 64 ?
2954 					  elfcpp::R_SPARC_TLS_TPOFF64 :
2955 					  elfcpp::R_SPARC_TLS_TPOFF32));
2956 	      }
2957 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2958 	      unsupported_reloc_global(object, r_type, gsym);
2959 	    break;
2960 
2961 	  case elfcpp::R_SPARC_TLS_LDM_HI22:	// Local-dynamic
2962 	  case elfcpp::R_SPARC_TLS_LDM_LO10:
2963 	  case elfcpp::R_SPARC_TLS_LDM_ADD:
2964 	  case elfcpp::R_SPARC_TLS_LDM_CALL:
2965 	    if (optimized_type == tls::TLSOPT_NONE)
2966 	      {
2967 		// Create a GOT entry for the module index.
2968 		target->got_mod_index_entry(symtab, layout, object);
2969 
2970 		if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2971 		  generate_tls_call(symtab, layout, target);
2972 	      }
2973 	    else if (optimized_type != tls::TLSOPT_TO_LE)
2974 	      unsupported_reloc_global(object, r_type, gsym);
2975 	    break;
2976 
2977 	  case elfcpp::R_SPARC_TLS_LDO_HIX22:	// Alternate local-dynamic
2978 	  case elfcpp::R_SPARC_TLS_LDO_LOX10:
2979 	  case elfcpp::R_SPARC_TLS_LDO_ADD:
2980 	    break;
2981 
2982 	  case elfcpp::R_SPARC_TLS_LE_HIX22:
2983 	  case elfcpp::R_SPARC_TLS_LE_LOX10:
2984 	    layout->set_has_static_tls();
2985 	    if (parameters->options().shared())
2986 	      {
2987 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2988 		rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
2989 						       output_section, object,
2990 						       data_shndx, reloc.get_r_offset(),
2991 						       0);
2992 	      }
2993 	    break;
2994 
2995 	  case elfcpp::R_SPARC_TLS_IE_HI22:	// Initial-exec
2996 	  case elfcpp::R_SPARC_TLS_IE_LO10:
2997 	  case elfcpp::R_SPARC_TLS_IE_LD:
2998 	  case elfcpp::R_SPARC_TLS_IE_LDX:
2999 	  case elfcpp::R_SPARC_TLS_IE_ADD:
3000 	    layout->set_has_static_tls();
3001 	    if (optimized_type == tls::TLSOPT_NONE)
3002 	      {
3003 		// Create a GOT entry for the tp-relative offset.
3004 		Output_data_got<size, big_endian>* got
3005 		  = target->got_section(symtab, layout);
3006 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3007 					 target->rela_dyn_section(layout),
3008 					 (size == 64
3009 					  ? elfcpp::R_SPARC_TLS_TPOFF64
3010 					  : elfcpp::R_SPARC_TLS_TPOFF32));
3011 	      }
3012 	    else if (optimized_type != tls::TLSOPT_TO_LE)
3013 	      unsupported_reloc_global(object, r_type, gsym);
3014 	    break;
3015 	  }
3016       }
3017       break;
3018 
3019       // These are relocations which should only be seen by the
3020       // dynamic linker, and should never be seen here.
3021     case elfcpp::R_SPARC_COPY:
3022     case elfcpp::R_SPARC_GLOB_DAT:
3023     case elfcpp::R_SPARC_JMP_SLOT:
3024     case elfcpp::R_SPARC_JMP_IREL:
3025     case elfcpp::R_SPARC_RELATIVE:
3026     case elfcpp::R_SPARC_IRELATIVE:
3027     case elfcpp::R_SPARC_TLS_DTPMOD64:
3028     case elfcpp::R_SPARC_TLS_DTPMOD32:
3029     case elfcpp::R_SPARC_TLS_DTPOFF64:
3030     case elfcpp::R_SPARC_TLS_DTPOFF32:
3031     case elfcpp::R_SPARC_TLS_TPOFF64:
3032     case elfcpp::R_SPARC_TLS_TPOFF32:
3033       gold_error(_("%s: unexpected reloc %u in object file"),
3034 		 object->name().c_str(), r_type);
3035       break;
3036 
3037     default:
3038       unsupported_reloc_global(object, r_type, gsym);
3039       break;
3040     }
3041 }
3042 
3043 // Make a new symbol table entry.
3044 // STT_SPARC_REGISTER symbols require special handling,
3045 // so we intercept these symbols and keep track of them separately.
3046 // We will resolve register symbols here and output them at symbol
3047 // finalization time.
3048 
3049 template<int size, bool big_endian>
3050 Sized_symbol<size>*
make_symbol(const char * name,elfcpp::STT type,Object * object,unsigned int shndx,uint64_t value)3051 Target_sparc<size, big_endian>::make_symbol(const char* name,
3052 					    elfcpp::STT type,
3053 					    Object* object,
3054 					    unsigned int shndx,
3055 					    uint64_t value)
3056 {
3057   // REGISTER symbols are used only on SPARC-64.
3058   if (size == 64 && type == elfcpp::STT_SPARC_REGISTER)
3059     {
3060       // Ignore REGISTER symbols in dynamic objects.
3061       if (object->is_dynamic())
3062 	return NULL;
3063       // Only registers 2, 3, 6, and 7 can be declared global.
3064       int reg = value;
3065       switch (reg)
3066 	{
3067 	case 2: case 3:
3068 	  reg -= 2;
3069 	  break;
3070 	case 6: case 7:
3071 	  reg -= 4;
3072 	  break;
3073 	default:
3074 	  gold_error(_("%s: only registers %%g[2367] can be declared "
3075 		       "using STT_REGISTER"),
3076 		     object->name().c_str());
3077 	  return NULL;
3078 	}
3079       Register_symbol& rsym = this->register_syms_[reg];
3080       if (rsym.name == NULL)
3081 	{
3082 	  rsym.name = name;
3083 	  rsym.shndx = shndx;
3084 	  rsym.obj = object;
3085 	}
3086       else
3087 	{
3088 	  if (strcmp(rsym.name, name) != 0)
3089 	    {
3090 	      gold_error(_("%s: register %%g%d declared as '%s'; "
3091 			   "previously declared as '%s' in %s"),
3092 			 object->name().c_str(),
3093 			 static_cast<int>(value),
3094 			 *name ? name : "#scratch",
3095 			 *rsym.name ? rsym.name : "#scratch",
3096 			 rsym.obj->name().c_str());
3097 	      return NULL;
3098 	    }
3099 	}
3100       return NULL;
3101     }
3102   return new Sized_symbol<size>();
3103 }
3104 
3105 // Process relocations for gc.
3106 
3107 template<int size, bool big_endian>
3108 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)3109 Target_sparc<size, big_endian>::gc_process_relocs(
3110 			Symbol_table* symtab,
3111 			Layout* layout,
3112 			Sized_relobj_file<size, big_endian>* object,
3113 			unsigned int data_shndx,
3114 			unsigned int,
3115 			const unsigned char* prelocs,
3116 			size_t reloc_count,
3117 			Output_section* output_section,
3118 			bool needs_special_offset_handling,
3119 			size_t local_symbol_count,
3120 			const unsigned char* plocal_symbols)
3121 {
3122   typedef Target_sparc<size, big_endian> Sparc;
3123   typedef typename Target_sparc<size, big_endian>::Scan Scan;
3124   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
3125       Classify_reloc;
3126 
3127   gold::gc_process_relocs<size, big_endian, Sparc, Scan, Classify_reloc>(
3128     symtab,
3129     layout,
3130     this,
3131     object,
3132     data_shndx,
3133     prelocs,
3134     reloc_count,
3135     output_section,
3136     needs_special_offset_handling,
3137     local_symbol_count,
3138     plocal_symbols);
3139 }
3140 
3141 // Scan relocations for a section.
3142 
3143 template<int size, bool big_endian>
3144 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)3145 Target_sparc<size, big_endian>::scan_relocs(
3146 			Symbol_table* symtab,
3147 			Layout* layout,
3148 			Sized_relobj_file<size, big_endian>* object,
3149 			unsigned int data_shndx,
3150 			unsigned int sh_type,
3151 			const unsigned char* prelocs,
3152 			size_t reloc_count,
3153 			Output_section* output_section,
3154 			bool needs_special_offset_handling,
3155 			size_t local_symbol_count,
3156 			const unsigned char* plocal_symbols)
3157 {
3158   typedef Target_sparc<size, big_endian> Sparc;
3159   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
3160       Classify_reloc;
3161 
3162   if (sh_type == elfcpp::SHT_REL)
3163     {
3164       gold_error(_("%s: unsupported REL reloc section"),
3165 		 object->name().c_str());
3166       return;
3167     }
3168 
3169   gold::scan_relocs<size, big_endian, Sparc, Scan, Classify_reloc>(
3170     symtab,
3171     layout,
3172     this,
3173     object,
3174     data_shndx,
3175     prelocs,
3176     reloc_count,
3177     output_section,
3178     needs_special_offset_handling,
3179     local_symbol_count,
3180     plocal_symbols);
3181 }
3182 
3183 // Finalize the sections.
3184 
3185 template<int size, bool big_endian>
3186 void
do_finalize_sections(Layout * layout,const Input_objects *,Symbol_table * symtab)3187 Target_sparc<size, big_endian>::do_finalize_sections(
3188     Layout* layout,
3189     const Input_objects*,
3190     Symbol_table* symtab)
3191 {
3192   if (this->plt_)
3193     this->plt_->emit_pending_ifunc_relocs();
3194 
3195   // Fill in some more dynamic tags.
3196   const Reloc_section* rel_plt = (this->plt_ == NULL
3197 				  ? NULL
3198 				  : this->plt_->rel_plt());
3199   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3200 				  this->rela_dyn_, true, true);
3201 
3202   // Emit any relocs we saved in an attempt to avoid generating COPY
3203   // relocs.
3204   if (this->copy_relocs_.any_saved_relocs())
3205     this->copy_relocs_.emit(this->rela_dyn_section(layout));
3206 
3207   if (parameters->doing_static_link()
3208       && (this->plt_ == NULL || !this->plt_->has_ifunc_section()))
3209     {
3210       // If linking statically, make sure that the __rela_iplt symbols
3211       // were defined if necessary, even if we didn't create a PLT.
3212       static const Define_symbol_in_segment syms[] =
3213 	{
3214 	  {
3215 	    "__rela_iplt_start",	// name
3216 	    elfcpp::PT_LOAD,		// segment_type
3217 	    elfcpp::PF_W,		// segment_flags_set
3218 	    elfcpp::PF(0),		// segment_flags_clear
3219 	    0,				// value
3220 	    0,				// size
3221 	    elfcpp::STT_NOTYPE,		// type
3222 	    elfcpp::STB_GLOBAL,		// binding
3223 	    elfcpp::STV_HIDDEN,		// visibility
3224 	    0,				// nonvis
3225 	    Symbol::SEGMENT_START,	// offset_from_base
3226 	    true			// only_if_ref
3227 	  },
3228 	  {
3229 	    "__rela_iplt_end",		// name
3230 	    elfcpp::PT_LOAD,		// segment_type
3231 	    elfcpp::PF_W,		// segment_flags_set
3232 	    elfcpp::PF(0),		// segment_flags_clear
3233 	    0,				// value
3234 	    0,				// size
3235 	    elfcpp::STT_NOTYPE,		// type
3236 	    elfcpp::STB_GLOBAL,		// binding
3237 	    elfcpp::STV_HIDDEN,		// visibility
3238 	    0,				// nonvis
3239 	    Symbol::SEGMENT_START,	// offset_from_base
3240 	    true			// only_if_ref
3241 	  }
3242 	};
3243 
3244       symtab->define_symbols(layout, 2, syms,
3245 			     layout->script_options()->saw_sections_clause());
3246     }
3247 
3248   for (int reg = 0; reg < 4; ++reg)
3249     {
3250       Register_symbol& rsym = this->register_syms_[reg];
3251       if (rsym.name != NULL)
3252 	{
3253 	  int value = reg < 3 ? reg + 2 : reg + 4;
3254 	  Sized_symbol<size>* sym = new Sized_symbol<size>();
3255 	  if (rsym.shndx == elfcpp::SHN_UNDEF)
3256 	    sym->init_undefined(rsym.name, NULL, value,
3257 				elfcpp::STT_SPARC_REGISTER, elfcpp::STB_GLOBAL,
3258 				elfcpp::STV_DEFAULT, 0);
3259 	  else
3260 	    sym->init_constant(rsym.name, NULL, value, 0,
3261 			       elfcpp::STT_SPARC_REGISTER, elfcpp::STB_GLOBAL,
3262 			       elfcpp::STV_DEFAULT, 0, false);
3263 	  symtab->add_target_global_symbol(sym);
3264 	  layout->add_target_specific_dynamic_tag(elfcpp::DT_SPARC_REGISTER,
3265 						  value);
3266 	}
3267     }
3268 }
3269 
3270 // Perform a relocation.
3271 
3272 template<int size, bool big_endian>
3273 inline bool
relocate(const Relocate_info<size,big_endian> * relinfo,unsigned int,Target_sparc * target,Output_section *,size_t relnum,const unsigned char * preloc,const Sized_symbol<size> * gsym,const Symbol_value<size> * psymval,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr address,section_size_type view_size)3274 Target_sparc<size, big_endian>::Relocate::relocate(
3275 			const Relocate_info<size, big_endian>* relinfo,
3276 			unsigned int,
3277 			Target_sparc* target,
3278 			Output_section*,
3279 			size_t relnum,
3280 			const unsigned char* preloc,
3281 			const Sized_symbol<size>* gsym,
3282 			const Symbol_value<size>* psymval,
3283 			unsigned char* view,
3284 			typename elfcpp::Elf_types<size>::Elf_Addr address,
3285 			section_size_type view_size)
3286 {
3287   const elfcpp::Rela<size, big_endian> rela(preloc);
3288   unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
3289   bool orig_is_ifunc = psymval->is_ifunc_symbol();
3290   r_type &= 0xff;
3291 
3292   if (this->ignore_gd_add_)
3293     {
3294       if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
3295 	gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3296 			       _("missing expected TLS relocation"));
3297       else
3298 	{
3299 	  this->ignore_gd_add_ = false;
3300 	  return false;
3301 	}
3302     }
3303 
3304   if (view == NULL)
3305     return true;
3306 
3307   if (this->reloc_adjust_addr_ == view)
3308     view -= 4;
3309 
3310   typedef Sparc_relocate_functions<size, big_endian> Reloc;
3311   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
3312 
3313   // Pick the value to use for symbols defined in shared objects.
3314   Symbol_value<size> symval;
3315   if (gsym != NULL
3316       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3317     {
3318       elfcpp::Elf_Xword value;
3319 
3320       value = target->plt_address_for_global(gsym);
3321 
3322       symval.set_output_value(value);
3323 
3324       psymval = &symval;
3325     }
3326   else if (gsym == NULL && orig_is_ifunc)
3327     {
3328       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3329       if (object->local_has_plt_offset(r_sym))
3330 	{
3331 	  symval.set_output_value(target->plt_address_for_local(object, r_sym));
3332 	  psymval = &symval;
3333 	}
3334     }
3335 
3336   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3337 
3338   // Get the GOT offset if needed.  Unlike i386 and x86_64, our GOT
3339   // pointer points to the beginning, not the end, of the table.
3340   // So we just use the plain offset.
3341   unsigned int got_offset = 0;
3342   bool gdop_valid = false;
3343   switch (r_type)
3344     {
3345     case elfcpp::R_SPARC_GOTDATA_OP:
3346     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
3347     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
3348       // If this is local, we did not create a GOT entry because we
3349       // intend to transform this into a GOT relative relocation.
3350       if (gsym == NULL
3351 	  || (gsym->is_defined()
3352 	      && !gsym->is_from_dynobj()
3353 	      && !gsym->is_preemptible()
3354 	      && !orig_is_ifunc))
3355 	{
3356 	  got_offset = psymval->value(object, addend) - target->got_address();
3357 	  gdop_valid = true;
3358 	  break;
3359 	}
3360       // Fall through.
3361     case elfcpp::R_SPARC_GOT10:
3362     case elfcpp::R_SPARC_GOT13:
3363     case elfcpp::R_SPARC_GOT22:
3364       if (gsym != NULL)
3365 	{
3366 	  gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3367 	  got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
3368 	}
3369       else
3370 	{
3371 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3372 	  gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3373 	  got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3374 	}
3375       break;
3376 
3377     default:
3378       break;
3379     }
3380 
3381   switch (r_type)
3382     {
3383     case elfcpp::R_SPARC_NONE:
3384     case elfcpp::R_SPARC_REGISTER:
3385     case elfcpp::R_SPARC_GNU_VTINHERIT:
3386     case elfcpp::R_SPARC_GNU_VTENTRY:
3387       break;
3388 
3389     case elfcpp::R_SPARC_8:
3390       Relocate_functions<size, big_endian>::rela8(view, object,
3391 						  psymval, addend);
3392       break;
3393 
3394     case elfcpp::R_SPARC_16:
3395       if (rela.get_r_offset() & 0x1)
3396 	{
3397 	  // The assembler can sometimes emit unaligned relocations
3398 	  // for dwarf2 cfi directives.
3399 	  Reloc::ua16(view, object, psymval, addend);
3400 	}
3401       else
3402 	Relocate_functions<size, big_endian>::rela16(view, object,
3403 						     psymval, addend);
3404       break;
3405 
3406     case elfcpp::R_SPARC_32:
3407       if (!parameters->options().output_is_position_independent())
3408 	{
3409 	  if (rela.get_r_offset() & 0x3)
3410 	    {
3411 	      // The assembler can sometimes emit unaligned relocations
3412 	      // for dwarf2 cfi directives.
3413 	      Reloc::ua32(view, object, psymval, addend);
3414 	    }
3415 	  else
3416 	    Relocate_functions<size, big_endian>::rela32(view, object,
3417 							 psymval, addend);
3418 	}
3419       break;
3420 
3421     case elfcpp::R_SPARC_DISP8:
3422       Reloc::disp8(view, object, psymval, addend, address);
3423       break;
3424 
3425     case elfcpp::R_SPARC_DISP16:
3426       Reloc::disp16(view, object, psymval, addend, address);
3427       break;
3428 
3429     case elfcpp::R_SPARC_DISP32:
3430       Reloc::disp32(view, object, psymval, addend, address);
3431       break;
3432 
3433     case elfcpp::R_SPARC_DISP64:
3434       Reloc::disp64(view, object, psymval, addend, address);
3435       break;
3436 
3437     case elfcpp::R_SPARC_WDISP30:
3438     case elfcpp::R_SPARC_WPLT30:
3439       Reloc::wdisp30(view, object, psymval, addend, address);
3440       if (target->may_relax())
3441 	relax_call(target, view, rela, view_size);
3442       break;
3443 
3444     case elfcpp::R_SPARC_WDISP22:
3445       Reloc::wdisp22(view, object, psymval, addend, address);
3446       break;
3447 
3448     case elfcpp::R_SPARC_WDISP19:
3449       Reloc::wdisp19(view, object, psymval, addend, address);
3450       break;
3451 
3452     case elfcpp::R_SPARC_WDISP16:
3453       Reloc::wdisp16(view, object, psymval, addend, address);
3454       break;
3455 
3456     case elfcpp::R_SPARC_WDISP10:
3457       Reloc::wdisp10(view, object, psymval, addend, address);
3458       break;
3459 
3460     case elfcpp::R_SPARC_HI22:
3461       Reloc::hi22(view, object, psymval, addend);
3462       break;
3463 
3464     case elfcpp::R_SPARC_22:
3465       Reloc::rela32_22(view, object, psymval, addend);
3466       break;
3467 
3468     case elfcpp::R_SPARC_13:
3469       Reloc::rela32_13(view, object, psymval, addend);
3470       break;
3471 
3472     case elfcpp::R_SPARC_LO10:
3473       Reloc::lo10(view, object, psymval, addend);
3474       break;
3475 
3476     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
3477       if (gdop_valid)
3478 	{
3479 	  Reloc::gdop_lox10(view, got_offset);
3480 	  break;
3481 	}
3482       // Fall through.
3483     case elfcpp::R_SPARC_GOT10:
3484       Reloc::lo10(view, got_offset, addend);
3485       break;
3486 
3487     case elfcpp::R_SPARC_GOTDATA_OP:
3488       if (gdop_valid)
3489 	{
3490 	  typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
3491 	  Insntype* wv = reinterpret_cast<Insntype*>(view);
3492 	  Insntype val;
3493 
3494 	  // {ld,ldx} [%rs1 + %rs2], %rd --> add %rs1, %rs2, %rd
3495 	  val = elfcpp::Swap<32, true>::readval(wv);
3496 	  val = 0x80000000 | (val & 0x3e07c01f);
3497 	  elfcpp::Swap<32, true>::writeval(wv, val);
3498 	}
3499       break;
3500 
3501     case elfcpp::R_SPARC_GOT13:
3502       Reloc::rela32_13(view, got_offset, addend);
3503       break;
3504 
3505     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
3506       if (gdop_valid)
3507 	{
3508 	  Reloc::gdop_hix22(view, got_offset);
3509 	  break;
3510 	}
3511       // Fall through.
3512     case elfcpp::R_SPARC_GOT22:
3513       Reloc::hi22(view, got_offset, addend);
3514       break;
3515 
3516     case elfcpp::R_SPARC_PC10:
3517       Reloc::pc10(view, object, psymval, addend, address);
3518       break;
3519 
3520     case elfcpp::R_SPARC_PC22:
3521       Reloc::pc22(view, object, psymval, addend, address);
3522       break;
3523 
3524     case elfcpp::R_SPARC_TLS_DTPOFF32:
3525     case elfcpp::R_SPARC_UA32:
3526       Reloc::ua32(view, object, psymval, addend);
3527       break;
3528 
3529     case elfcpp::R_SPARC_PLT64:
3530       Relocate_functions<size, big_endian>::rela64(view, object,
3531 						   psymval, addend);
3532       break;
3533 
3534     case elfcpp::R_SPARC_PLT32:
3535       Relocate_functions<size, big_endian>::rela32(view, object,
3536 						   psymval, addend);
3537       break;
3538 
3539     case elfcpp::R_SPARC_HIPLT22:
3540       Reloc::hi22(view, object, psymval, addend);
3541       break;
3542 
3543     case elfcpp::R_SPARC_LOPLT10:
3544       Reloc::lo10(view, object, psymval, addend);
3545       break;
3546 
3547     case elfcpp::R_SPARC_PCPLT32:
3548       Reloc::disp32(view, object, psymval, addend, address);
3549       break;
3550 
3551     case elfcpp::R_SPARC_PCPLT22:
3552       Reloc::pcplt22(view, object, psymval, addend, address);
3553       break;
3554 
3555     case elfcpp::R_SPARC_PCPLT10:
3556       Reloc::lo10(view, object, psymval, addend, address);
3557       break;
3558 
3559     case elfcpp::R_SPARC_64:
3560       if (!parameters->options().output_is_position_independent())
3561 	{
3562 	  if (rela.get_r_offset() & 0x7)
3563 	    {
3564 	      // The assembler can sometimes emit unaligned relocations
3565 	      // for dwarf2 cfi directives.
3566 	      Reloc::ua64(view, object, psymval, addend);
3567 	    }
3568 	  else
3569 	    Relocate_functions<size, big_endian>::rela64(view, object,
3570 							 psymval, addend);
3571 	}
3572       break;
3573 
3574     case elfcpp::R_SPARC_OLO10:
3575       {
3576 	unsigned int addend2 = rela.get_r_info() & 0xffffffff;
3577 	addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
3578 	Reloc::olo10(view, object, psymval, addend, addend2);
3579       }
3580       break;
3581 
3582     case elfcpp::R_SPARC_HH22:
3583       Reloc::hh22(view, object, psymval, addend);
3584       break;
3585 
3586     case elfcpp::R_SPARC_PC_HH22:
3587       Reloc::pc_hh22(view, object, psymval, addend, address);
3588       break;
3589 
3590     case elfcpp::R_SPARC_HM10:
3591       Reloc::hm10(view, object, psymval, addend);
3592       break;
3593 
3594     case elfcpp::R_SPARC_PC_HM10:
3595       Reloc::pc_hm10(view, object, psymval, addend, address);
3596       break;
3597 
3598     case elfcpp::R_SPARC_LM22:
3599       Reloc::hi22(view, object, psymval, addend);
3600       break;
3601 
3602     case elfcpp::R_SPARC_PC_LM22:
3603       Reloc::pcplt22(view, object, psymval, addend, address);
3604       break;
3605 
3606     case elfcpp::R_SPARC_11:
3607       Reloc::rela32_11(view, object, psymval, addend);
3608       break;
3609 
3610     case elfcpp::R_SPARC_10:
3611       Reloc::rela32_10(view, object, psymval, addend);
3612       break;
3613 
3614     case elfcpp::R_SPARC_7:
3615       Reloc::rela32_7(view, object, psymval, addend);
3616       break;
3617 
3618     case elfcpp::R_SPARC_6:
3619       Reloc::rela32_6(view, object, psymval, addend);
3620       break;
3621 
3622     case elfcpp::R_SPARC_5:
3623       Reloc::rela32_5(view, object, psymval, addend);
3624       break;
3625 
3626     case elfcpp::R_SPARC_HIX22:
3627       Reloc::hix22(view, object, psymval, addend);
3628       break;
3629 
3630     case elfcpp::R_SPARC_LOX10:
3631       Reloc::lox10(view, object, psymval, addend);
3632       break;
3633 
3634     case elfcpp::R_SPARC_H34:
3635       Reloc::h34(view, object, psymval, addend);
3636       break;
3637 
3638     case elfcpp::R_SPARC_H44:
3639       Reloc::h44(view, object, psymval, addend);
3640       break;
3641 
3642     case elfcpp::R_SPARC_M44:
3643       Reloc::m44(view, object, psymval, addend);
3644       break;
3645 
3646     case elfcpp::R_SPARC_L44:
3647       Reloc::l44(view, object, psymval, addend);
3648       break;
3649 
3650     case elfcpp::R_SPARC_TLS_DTPOFF64:
3651     case elfcpp::R_SPARC_UA64:
3652       Reloc::ua64(view, object, psymval, addend);
3653       break;
3654 
3655     case elfcpp::R_SPARC_UA16:
3656       Reloc::ua16(view, object, psymval, addend);
3657       break;
3658 
3659     case elfcpp::R_SPARC_TLS_GD_HI22:
3660     case elfcpp::R_SPARC_TLS_GD_LO10:
3661     case elfcpp::R_SPARC_TLS_GD_ADD:
3662     case elfcpp::R_SPARC_TLS_GD_CALL:
3663     case elfcpp::R_SPARC_TLS_LDM_HI22:
3664     case elfcpp::R_SPARC_TLS_LDM_LO10:
3665     case elfcpp::R_SPARC_TLS_LDM_ADD:
3666     case elfcpp::R_SPARC_TLS_LDM_CALL:
3667     case elfcpp::R_SPARC_TLS_LDO_HIX22:
3668     case elfcpp::R_SPARC_TLS_LDO_LOX10:
3669     case elfcpp::R_SPARC_TLS_LDO_ADD:
3670     case elfcpp::R_SPARC_TLS_IE_HI22:
3671     case elfcpp::R_SPARC_TLS_IE_LO10:
3672     case elfcpp::R_SPARC_TLS_IE_LD:
3673     case elfcpp::R_SPARC_TLS_IE_LDX:
3674     case elfcpp::R_SPARC_TLS_IE_ADD:
3675     case elfcpp::R_SPARC_TLS_LE_HIX22:
3676     case elfcpp::R_SPARC_TLS_LE_LOX10:
3677       this->relocate_tls(relinfo, target, relnum, rela,
3678 			 r_type, gsym, psymval, view,
3679 			 address, view_size);
3680       break;
3681 
3682     case elfcpp::R_SPARC_COPY:
3683     case elfcpp::R_SPARC_GLOB_DAT:
3684     case elfcpp::R_SPARC_JMP_SLOT:
3685     case elfcpp::R_SPARC_JMP_IREL:
3686     case elfcpp::R_SPARC_RELATIVE:
3687     case elfcpp::R_SPARC_IRELATIVE:
3688       // These are outstanding tls relocs, which are unexpected when
3689       // linking.
3690     case elfcpp::R_SPARC_TLS_DTPMOD64:
3691     case elfcpp::R_SPARC_TLS_DTPMOD32:
3692     case elfcpp::R_SPARC_TLS_TPOFF64:
3693     case elfcpp::R_SPARC_TLS_TPOFF32:
3694       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3695 			     _("unexpected reloc %u in object file"),
3696 			     r_type);
3697       break;
3698 
3699     default:
3700       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3701 			     _("unsupported reloc %u"),
3702 			     r_type);
3703       break;
3704     }
3705 
3706   return true;
3707 }
3708 
3709 // Perform a TLS relocation.
3710 
3711 template<int size, bool big_endian>
3712 inline void
relocate_tls(const Relocate_info<size,big_endian> * relinfo,Target_sparc<size,big_endian> * target,size_t relnum,const elfcpp::Rela<size,big_endian> & rela,unsigned int r_type,const Sized_symbol<size> * gsym,const Symbol_value<size> * psymval,unsigned char * view,typename elfcpp::Elf_types<size>::Elf_Addr address,section_size_type)3713 Target_sparc<size, big_endian>::Relocate::relocate_tls(
3714 			const Relocate_info<size, big_endian>* relinfo,
3715 			Target_sparc<size, big_endian>* target,
3716 			size_t relnum,
3717 			const elfcpp::Rela<size, big_endian>& rela,
3718 			unsigned int r_type,
3719 			const Sized_symbol<size>* gsym,
3720 			const Symbol_value<size>* psymval,
3721 			unsigned char* view,
3722 			typename elfcpp::Elf_types<size>::Elf_Addr address,
3723 			section_size_type)
3724 {
3725   Output_segment* tls_segment = relinfo->layout->tls_segment();
3726   typedef Sparc_relocate_functions<size, big_endian> Reloc;
3727   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
3728   typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
3729 
3730   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3731   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
3732 
3733   const bool is_final =
3734     (gsym == NULL
3735      ? !parameters->options().shared()
3736      : gsym->final_value_is_known());
3737   const tls::Tls_optimization optimized_type
3738       = optimize_tls_reloc(is_final, r_type);
3739 
3740   switch (r_type)
3741     {
3742     case elfcpp::R_SPARC_TLS_GD_HI22:
3743     case elfcpp::R_SPARC_TLS_GD_LO10:
3744     case elfcpp::R_SPARC_TLS_GD_ADD:
3745     case elfcpp::R_SPARC_TLS_GD_CALL:
3746       if (optimized_type == tls::TLSOPT_TO_LE)
3747 	{
3748 	  Insntype* wv = reinterpret_cast<Insntype*>(view);
3749 	  Insntype val;
3750 
3751 	  value -= tls_segment->memsz();
3752 
3753 	  switch (r_type)
3754 	    {
3755 	    case elfcpp::R_SPARC_TLS_GD_HI22:
3756 	      // TLS_GD_HI22 --> TLS_LE_HIX22
3757 	      Reloc::hix22(view, value, addend);
3758 	      break;
3759 
3760 	    case elfcpp::R_SPARC_TLS_GD_LO10:
3761 	      // TLS_GD_LO10 --> TLS_LE_LOX10
3762 	      Reloc::lox10(view, value, addend);
3763 	      break;
3764 
3765 	    case elfcpp::R_SPARC_TLS_GD_ADD:
3766 	      // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
3767 	      val = elfcpp::Swap<32, true>::readval(wv);
3768 	      val = (val & ~0x7c000) | 0x1c000;
3769 	      elfcpp::Swap<32, true>::writeval(wv, val);
3770 	      break;
3771 	    case elfcpp::R_SPARC_TLS_GD_CALL:
3772 	      // call __tls_get_addr --> nop
3773 	      elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
3774 	      break;
3775 	    }
3776 	  break;
3777 	}
3778       else
3779 	{
3780 	  unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3781 				   ? GOT_TYPE_TLS_OFFSET
3782 				   : GOT_TYPE_TLS_PAIR);
3783 	  if (gsym != NULL)
3784 	    {
3785 	      gold_assert(gsym->has_got_offset(got_type));
3786 	      value = gsym->got_offset(got_type);
3787 	    }
3788 	  else
3789 	    {
3790 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3791 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
3792 	      value = object->local_got_offset(r_sym, got_type);
3793 	    }
3794 	  if (optimized_type == tls::TLSOPT_TO_IE)
3795 	    {
3796 	      Insntype* wv = reinterpret_cast<Insntype*>(view);
3797 	      Insntype val;
3798 
3799 	      switch (r_type)
3800 		{
3801 		case elfcpp::R_SPARC_TLS_GD_HI22:
3802 		  // TLS_GD_HI22 --> TLS_IE_HI22
3803 		  Reloc::hi22(view, value, addend);
3804 		  break;
3805 
3806 		case elfcpp::R_SPARC_TLS_GD_LO10:
3807 		  // TLS_GD_LO10 --> TLS_IE_LO10
3808 		  Reloc::lo10(view, value, addend);
3809 		  break;
3810 
3811 		case elfcpp::R_SPARC_TLS_GD_ADD:
3812 		  // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
3813 		  val = elfcpp::Swap<32, true>::readval(wv);
3814 
3815 		  if (size == 64)
3816 		    val |= 0xc0580000;
3817 		  else
3818 		    val |= 0xc0000000;
3819 
3820 		  elfcpp::Swap<32, true>::writeval(wv, val);
3821 		  break;
3822 
3823 		case elfcpp::R_SPARC_TLS_GD_CALL:
3824 		  // The compiler can put the TLS_GD_ADD instruction
3825 		  // into the delay slot of the call.  If so, we need
3826 		  // to transpose the two instructions so that the
3827 		  // new sequence works properly.
3828 		  //
3829 		  // The test we use is if the instruction in the
3830 		  // delay slot is an add with destination register
3831 		  // equal to %o0
3832 		  val = elfcpp::Swap<32, true>::readval(wv + 1);
3833 		  if ((val & 0x81f80000) == 0x80000000
3834 		      && ((val >> 25) & 0x1f) == 0x8)
3835 		    {
3836 		      if (size == 64)
3837 			val |= 0xc0580000;
3838 		      else
3839 			val |= 0xc0000000;
3840 
3841 		      elfcpp::Swap<32, true>::writeval(wv, val);
3842 
3843 		      wv += 1;
3844 		      this->ignore_gd_add_ = true;
3845 		    }
3846 		  else
3847 		    {
3848 		      // Even if the delay slot isn't the TLS_GD_ADD
3849 		      // instruction, we still have to handle the case
3850 		      // where it sets up %o0 in some other way.
3851 		      elfcpp::Swap<32, true>::writeval(wv, val);
3852 		      wv += 1;
3853 		      this->reloc_adjust_addr_ = view + 4;
3854 		    }
3855 		  // call __tls_get_addr --> add %g7, %o0, %o0
3856 		  elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
3857 		  break;
3858 		}
3859 	      break;
3860 	    }
3861 	  else if (optimized_type == tls::TLSOPT_NONE)
3862 	    {
3863 	      switch (r_type)
3864 		{
3865 		case elfcpp::R_SPARC_TLS_GD_HI22:
3866 		  Reloc::hi22(view, value, addend);
3867 		  break;
3868 		case elfcpp::R_SPARC_TLS_GD_LO10:
3869 		  Reloc::lo10(view, value, addend);
3870 		  break;
3871 		case elfcpp::R_SPARC_TLS_GD_ADD:
3872 		  break;
3873 		case elfcpp::R_SPARC_TLS_GD_CALL:
3874 		  {
3875 		    Symbol_value<size> symval;
3876 		    elfcpp::Elf_Xword value;
3877 		    Symbol* tsym;
3878 
3879 		    tsym = target->tls_get_addr_sym_;
3880 		    gold_assert(tsym);
3881 		    value = (target->plt_section()->address() +
3882 			     tsym->plt_offset());
3883 		    symval.set_output_value(value);
3884 		    Reloc::wdisp30(view, object, &symval, addend, address);
3885 		  }
3886 		  break;
3887 		}
3888 	      break;
3889 	    }
3890 	}
3891       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3892 			     _("unsupported reloc %u"),
3893 			     r_type);
3894       break;
3895 
3896     case elfcpp::R_SPARC_TLS_LDM_HI22:
3897     case elfcpp::R_SPARC_TLS_LDM_LO10:
3898     case elfcpp::R_SPARC_TLS_LDM_ADD:
3899     case elfcpp::R_SPARC_TLS_LDM_CALL:
3900       if (optimized_type == tls::TLSOPT_TO_LE)
3901 	{
3902 	  Insntype* wv = reinterpret_cast<Insntype*>(view);
3903 
3904 	  switch (r_type)
3905 	    {
3906 	    case elfcpp::R_SPARC_TLS_LDM_HI22:
3907 	    case elfcpp::R_SPARC_TLS_LDM_LO10:
3908 	    case elfcpp::R_SPARC_TLS_LDM_ADD:
3909 	      elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
3910 	      break;
3911 
3912 	    case elfcpp::R_SPARC_TLS_LDM_CALL:
3913 	      elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
3914 	      break;
3915 	    }
3916 	  break;
3917 	}
3918       else if (optimized_type == tls::TLSOPT_NONE)
3919 	{
3920 	  // Relocate the field with the offset of the GOT entry for
3921 	  // the module index.
3922 	  unsigned int got_offset;
3923 
3924 	  got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
3925 	  switch (r_type)
3926 	    {
3927 	    case elfcpp::R_SPARC_TLS_LDM_HI22:
3928 	      Reloc::hi22(view, got_offset, addend);
3929 	      break;
3930 	    case elfcpp::R_SPARC_TLS_LDM_LO10:
3931 	      Reloc::lo10(view, got_offset, addend);
3932 	      break;
3933 	    case elfcpp::R_SPARC_TLS_LDM_ADD:
3934 	      break;
3935 	    case elfcpp::R_SPARC_TLS_LDM_CALL:
3936 	      {
3937 		Symbol_value<size> symval;
3938 		elfcpp::Elf_Xword value;
3939 		Symbol* tsym;
3940 
3941 		tsym = target->tls_get_addr_sym_;
3942 		gold_assert(tsym);
3943 		value = (target->plt_section()->address() +
3944 			 tsym->plt_offset());
3945 		symval.set_output_value(value);
3946 		Reloc::wdisp30(view, object, &symval, addend, address);
3947 	      }
3948 	      break;
3949 	    }
3950 	  break;
3951 	}
3952       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3953 			     _("unsupported reloc %u"),
3954 			     r_type);
3955       break;
3956 
3957       // These relocs can appear in debugging sections, in which case
3958       // we won't see the TLS_LDM relocs.  The local_dynamic_type
3959       // field tells us this.
3960     case elfcpp::R_SPARC_TLS_LDO_HIX22:
3961       if (optimized_type == tls::TLSOPT_TO_LE)
3962 	{
3963 	  value -= tls_segment->memsz();
3964 	  Reloc::hix22(view, value, addend);
3965 	}
3966       else
3967 	Reloc::ldo_hix22(view, value, addend);
3968       break;
3969     case elfcpp::R_SPARC_TLS_LDO_LOX10:
3970       if (optimized_type == tls::TLSOPT_TO_LE)
3971 	{
3972 	  value -= tls_segment->memsz();
3973 	  Reloc::lox10(view, value, addend);
3974 	}
3975       else
3976 	Reloc::ldo_lox10(view, value, addend);
3977       break;
3978     case elfcpp::R_SPARC_TLS_LDO_ADD:
3979       if (optimized_type == tls::TLSOPT_TO_LE)
3980 	{
3981 	  Insntype* wv = reinterpret_cast<Insntype*>(view);
3982 	  Insntype val;
3983 
3984 	  // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
3985 	  val = elfcpp::Swap<32, true>::readval(wv);
3986 	  val = (val & ~0x7c000) | 0x1c000;
3987 	  elfcpp::Swap<32, true>::writeval(wv, val);
3988 	}
3989       break;
3990 
3991       // When optimizing IE --> LE, the only relocation that is handled
3992       // differently is R_SPARC_TLS_IE_LD, it is rewritten from
3993       // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
3994       // rs2 and rd are the same.
3995     case elfcpp::R_SPARC_TLS_IE_LD:
3996     case elfcpp::R_SPARC_TLS_IE_LDX:
3997       if (optimized_type == tls::TLSOPT_TO_LE)
3998 	{
3999 	  Insntype* wv = reinterpret_cast<Insntype*>(view);
4000 	  Insntype val = elfcpp::Swap<32, true>::readval(wv);
4001 	  Insntype rs2 = val & 0x1f;
4002 	  Insntype rd = (val >> 25) & 0x1f;
4003 
4004 	  if (rs2 == rd)
4005 	    val = sparc_nop;
4006 	  else
4007 	    val = sparc_mov | (val & 0x3e00001f);
4008 
4009 	  elfcpp::Swap<32, true>::writeval(wv, val);
4010 	}
4011       break;
4012 
4013     case elfcpp::R_SPARC_TLS_IE_HI22:
4014     case elfcpp::R_SPARC_TLS_IE_LO10:
4015       if (optimized_type == tls::TLSOPT_TO_LE)
4016 	{
4017 	  value -= tls_segment->memsz();
4018 	  switch (r_type)
4019 	    {
4020 	    case elfcpp::R_SPARC_TLS_IE_HI22:
4021 	      // IE_HI22 --> LE_HIX22
4022 	      Reloc::hix22(view, value, addend);
4023 	      break;
4024 	    case elfcpp::R_SPARC_TLS_IE_LO10:
4025 	      // IE_LO10 --> LE_LOX10
4026 	      Reloc::lox10(view, value, addend);
4027 	      break;
4028 	    }
4029 	  break;
4030 	}
4031       else if (optimized_type == tls::TLSOPT_NONE)
4032 	{
4033 	  // Relocate the field with the offset of the GOT entry for
4034 	  // the tp-relative offset of the symbol.
4035 	  if (gsym != NULL)
4036 	    {
4037 	      gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4038 	      value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
4039 	    }
4040 	  else
4041 	    {
4042 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4043 	      gold_assert(object->local_has_got_offset(r_sym,
4044 						       GOT_TYPE_TLS_OFFSET));
4045 	      value = object->local_got_offset(r_sym,
4046 					       GOT_TYPE_TLS_OFFSET);
4047 	    }
4048 	  switch (r_type)
4049 	    {
4050 	    case elfcpp::R_SPARC_TLS_IE_HI22:
4051 	      Reloc::hi22(view, value, addend);
4052 	      break;
4053 	    case elfcpp::R_SPARC_TLS_IE_LO10:
4054 	      Reloc::lo10(view, value, addend);
4055 	      break;
4056 	    }
4057 	  break;
4058 	}
4059       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4060 			     _("unsupported reloc %u"),
4061 			     r_type);
4062       break;
4063 
4064     case elfcpp::R_SPARC_TLS_IE_ADD:
4065       // This seems to be mainly so that we can find the addition
4066       // instruction if there is one.  There doesn't seem to be any
4067       // actual relocation to apply.
4068       break;
4069 
4070     case elfcpp::R_SPARC_TLS_LE_HIX22:
4071       // If we're creating a shared library, a dynamic relocation will
4072       // have been created for this location, so do not apply it now.
4073       if (!parameters->options().shared())
4074 	{
4075 	  value -= tls_segment->memsz();
4076 	  Reloc::hix22(view, value, addend);
4077 	}
4078       break;
4079 
4080     case elfcpp::R_SPARC_TLS_LE_LOX10:
4081       // If we're creating a shared library, a dynamic relocation will
4082       // have been created for this location, so do not apply it now.
4083       if (!parameters->options().shared())
4084 	{
4085 	  value -= tls_segment->memsz();
4086 	  Reloc::lox10(view, value, addend);
4087 	}
4088       break;
4089     }
4090 }
4091 
4092 // Relax a call instruction.
4093 
4094 template<int size, bool big_endian>
4095 inline void
relax_call(Target_sparc<size,big_endian> * target,unsigned char * view,const elfcpp::Rela<size,big_endian> & rela,section_size_type view_size)4096 Target_sparc<size, big_endian>::Relocate::relax_call(
4097     Target_sparc<size, big_endian>* target,
4098     unsigned char* view,
4099     const elfcpp::Rela<size, big_endian>& rela,
4100     section_size_type view_size)
4101 {
4102   typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
4103   Insntype *wv = reinterpret_cast<Insntype*>(view);
4104   Insntype call_insn, delay_insn, set_insn;
4105   uint32_t op3, reg, off;
4106 
4107   // This code tries to relax call instructions that meet
4108   // certain criteria.
4109   //
4110   // The first criteria is that the call must be such that the return
4111   // address which the call writes into %o7 is unused.  Two sequences
4112   // meet this criteria, and are used to implement tail calls.
4113   //
4114   // Leaf function tail call:
4115   //
4116   // or %o7, %g0, %ANY_REG
4117   // call FUNC
4118   //  or %ANY_REG, %g0, %o7
4119   //
4120   // Non-leaf function tail call:
4121   //
4122   // call FUNC
4123   //  restore
4124   //
4125   // The second criteria is that the call destination is close.  If
4126   // the displacement can fit in a signed 22-bit immediate field of a
4127   // pre-V9 branch, we can do it.  If we are generating a 64-bit
4128   // object or a 32-bit object with ELF machine type EF_SPARC32PLUS,
4129   // and the displacement fits in a signed 19-bit immediate field,
4130   // then we can use a V9 branch.
4131 
4132   // Make sure the delay instruction can be safely accessed.
4133   if (rela.get_r_offset() + 8 > view_size)
4134     return;
4135 
4136   call_insn = elfcpp::Swap<32, true>::readval(wv);
4137   delay_insn = elfcpp::Swap<32, true>::readval(wv + 1);
4138 
4139   // Make sure it is really a call instruction.
4140   if (((call_insn >> 30) & 0x3) != 1)
4141     return;
4142 
4143   if (((delay_insn >> 30) & 0x3) != 2)
4144     return;
4145 
4146   // Accept only a restore or an integer arithmetic operation whose
4147   // sole side effect is to write the %o7 register (and perhaps set
4148   // the condition codes, which are considered clobbered across
4149   // function calls).
4150   //
4151   // For example, we don't want to match a tagged addition or
4152   // subtraction.  We also don't want to match something like a
4153   // divide.
4154   //
4155   // Specifically we accept add{,cc}, and{,cc}, or{,cc},
4156   // xor{,cc}, sub{,cc}, andn{,cc}, orn{,cc}, and xnor{,cc}.
4157 
4158   op3 = (delay_insn >> 19) & 0x3f;
4159   reg = (delay_insn >> 25) & 0x1f;
4160   if (op3 != 0x3d
4161       && ((op3 & 0x28) != 0 || reg != 15))
4162     return;
4163 
4164   // For non-restore instructions, make sure %o7 isn't
4165   // an input.
4166   if (op3 != 0x3d)
4167     {
4168       // First check RS1
4169       reg = (delay_insn >> 14) & 0x1f;
4170       if (reg == 15)
4171 	return;
4172 
4173       // And if non-immediate, check RS2
4174       if (((delay_insn >> 13) & 1) == 0)
4175 	{
4176 	  reg = (delay_insn & 0x1f);
4177 	  if (reg == 15)
4178 	    return;
4179 	}
4180     }
4181 
4182   // Now check the branch distance.  We are called after the
4183   // call has been relocated, so we just have to peek at the
4184   // offset contained in the instruction.
4185   off = call_insn & 0x3fffffff;
4186   if ((off & 0x3fe00000) != 0
4187       && (off & 0x3fe00000) != 0x3fe00000)
4188     return;
4189 
4190   if ((size == 64 || target->elf_machine_ == elfcpp::EM_SPARC32PLUS)
4191       && ((off & 0x3c0000) == 0
4192 	  || (off & 0x3c0000) == 0x3c0000))
4193     {
4194       // ba,pt %xcc, FUNC
4195       call_insn = 0x10680000 | (off & 0x07ffff);
4196     }
4197   else
4198     {
4199       // ba FUNC
4200       call_insn = 0x10800000 | (off & 0x3fffff);
4201     }
4202   elfcpp::Swap<32, true>::writeval(wv, call_insn);
4203 
4204   // See if we can NOP out the delay slot instruction.  We peek
4205   // at the instruction before the call to make sure we're dealing
4206   // with exactly the:
4207   //
4208   // or %o7, %g0, %ANY_REG
4209   // call
4210   //  or %ANY_REG, %g0, %o7
4211   //
4212   // case.  Otherwise this might be a tricky piece of hand written
4213   // assembler calculating %o7 in some non-trivial way, and therefore
4214   // we can't be sure that NOP'ing out the delay slot is safe.
4215   if (op3 == 0x02
4216       && rela.get_r_offset() >= 4)
4217     {
4218       if ((delay_insn & ~(0x1f << 14)) != 0x9e100000)
4219 	return;
4220 
4221       set_insn = elfcpp::Swap<32, true>::readval(wv - 1);
4222       if ((set_insn & ~(0x1f << 25)) != 0x8013c000)
4223 	return;
4224 
4225       reg = (set_insn >> 25) & 0x1f;
4226       if (reg == 0 || reg == 15)
4227 	return;
4228       if (reg != ((delay_insn >> 14) & 0x1f))
4229 	return;
4230 
4231       // All tests pass, nop it out.
4232       elfcpp::Swap<32, true>::writeval(wv + 1, sparc_nop);
4233     }
4234 }
4235 
4236 // Relocate section data.
4237 
4238 template<int size, bool big_endian>
4239 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,typename elfcpp::Elf_types<size>::Elf_Addr address,section_size_type view_size,const Reloc_symbol_changes * reloc_symbol_changes)4240 Target_sparc<size, big_endian>::relocate_section(
4241 			const Relocate_info<size, big_endian>* relinfo,
4242 			unsigned int sh_type,
4243 			const unsigned char* prelocs,
4244 			size_t reloc_count,
4245 			Output_section* output_section,
4246 			bool needs_special_offset_handling,
4247 			unsigned char* view,
4248 			typename elfcpp::Elf_types<size>::Elf_Addr address,
4249 			section_size_type view_size,
4250 			const Reloc_symbol_changes* reloc_symbol_changes)
4251 {
4252   typedef Target_sparc<size, big_endian> Sparc;
4253   typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
4254   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
4255       Classify_reloc;
4256 
4257   gold_assert(sh_type == elfcpp::SHT_RELA);
4258 
4259   gold::relocate_section<size, big_endian, Sparc, Sparc_relocate,
4260 			 gold::Default_comdat_behavior, Classify_reloc>(
4261     relinfo,
4262     this,
4263     prelocs,
4264     reloc_count,
4265     output_section,
4266     needs_special_offset_handling,
4267     view,
4268     address,
4269     view_size,
4270     reloc_symbol_changes);
4271 }
4272 
4273 // Scan the relocs during a relocatable link.
4274 
4275 template<int size, bool big_endian>
4276 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)4277 Target_sparc<size, big_endian>::scan_relocatable_relocs(
4278 			Symbol_table* symtab,
4279 			Layout* layout,
4280 			Sized_relobj_file<size, big_endian>* object,
4281 			unsigned int data_shndx,
4282 			unsigned int sh_type,
4283 			const unsigned char* prelocs,
4284 			size_t reloc_count,
4285 			Output_section* output_section,
4286 			bool needs_special_offset_handling,
4287 			size_t local_symbol_count,
4288 			const unsigned char* plocal_symbols,
4289 			Relocatable_relocs* rr)
4290 {
4291   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
4292       Classify_reloc;
4293   typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
4294       Scan_relocatable_relocs;
4295 
4296   gold_assert(sh_type == elfcpp::SHT_RELA);
4297 
4298   gold::scan_relocatable_relocs<size, big_endian, Scan_relocatable_relocs>(
4299     symtab,
4300     layout,
4301     object,
4302     data_shndx,
4303     prelocs,
4304     reloc_count,
4305     output_section,
4306     needs_special_offset_handling,
4307     local_symbol_count,
4308     plocal_symbols,
4309     rr);
4310 }
4311 
4312 // Scan the relocs for --emit-relocs.
4313 
4314 template<int size, bool big_endian>
4315 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)4316 Target_sparc<size, big_endian>::emit_relocs_scan(
4317     Symbol_table* symtab,
4318     Layout* layout,
4319     Sized_relobj_file<size, big_endian>* object,
4320     unsigned int data_shndx,
4321     unsigned int sh_type,
4322     const unsigned char* prelocs,
4323     size_t reloc_count,
4324     Output_section* output_section,
4325     bool needs_special_offset_handling,
4326     size_t local_symbol_count,
4327     const unsigned char* plocal_syms,
4328     Relocatable_relocs* rr)
4329 {
4330   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
4331       Classify_reloc;
4332   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
4333       Emit_relocs_strategy;
4334 
4335   gold_assert(sh_type == elfcpp::SHT_RELA);
4336 
4337   gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
4338     symtab,
4339     layout,
4340     object,
4341     data_shndx,
4342     prelocs,
4343     reloc_count,
4344     output_section,
4345     needs_special_offset_handling,
4346     local_symbol_count,
4347     plocal_syms,
4348     rr);
4349 }
4350 
4351 // Emit relocations for a section.
4352 
4353 template<int size, bool big_endian>
4354 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 * view,typename elfcpp::Elf_types<size>::Elf_Addr view_address,section_size_type view_size,unsigned char * reloc_view,section_size_type reloc_view_size)4355 Target_sparc<size, big_endian>::relocate_relocs(
4356     const Relocate_info<size, big_endian>* relinfo,
4357     unsigned int sh_type,
4358     const unsigned char* prelocs,
4359     size_t reloc_count,
4360     Output_section* output_section,
4361     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4362     unsigned char* view,
4363     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4364     section_size_type view_size,
4365     unsigned char* reloc_view,
4366     section_size_type reloc_view_size)
4367 {
4368   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
4369       Classify_reloc;
4370 
4371   gold_assert(sh_type == elfcpp::SHT_RELA);
4372 
4373   gold::relocate_relocs<size, big_endian, Classify_reloc>(
4374     relinfo,
4375     prelocs,
4376     reloc_count,
4377     output_section,
4378     offset_in_output_section,
4379     view,
4380     view_address,
4381     view_size,
4382     reloc_view,
4383     reloc_view_size);
4384 }
4385 
4386 // Return the value to use for a dynamic which requires special
4387 // treatment.  This is how we support equality comparisons of function
4388 // pointers across shared library boundaries, as described in the
4389 // processor specific ABI supplement.
4390 
4391 template<int size, bool big_endian>
4392 uint64_t
do_dynsym_value(const Symbol * gsym) const4393 Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4394 {
4395   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4396   return this->plt_section()->address() + gsym->plt_offset();
4397 }
4398 
4399 // do_make_elf_object to override the same function in the base class.
4400 // We need to use a target-specific sub-class of
4401 // Sized_relobj_file<size, big_endian> to process SPARC specific bits
4402 // of the ELF headers.  Hence we need to have our own ELF object creation.
4403 
4404 template<int size, bool big_endian>
4405 Object*
do_make_elf_object(const std::string & name,Input_file * input_file,off_t offset,const elfcpp::Ehdr<size,big_endian> & ehdr)4406 Target_sparc<size, big_endian>::do_make_elf_object(
4407     const std::string& name,
4408     Input_file* input_file,
4409     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
4410 {
4411   elfcpp::Elf_Half machine = ehdr.get_e_machine();
4412   elfcpp::Elf_Word flags = ehdr.get_e_flags();
4413   elfcpp::Elf_Word omm, mm;
4414 
4415   switch (machine)
4416     {
4417     case elfcpp::EM_SPARC32PLUS:
4418       this->elf_machine_ = elfcpp::EM_SPARC32PLUS;
4419       break;
4420 
4421     case elfcpp::EM_SPARC:
4422     case elfcpp::EM_SPARCV9:
4423       break;
4424 
4425     default:
4426       break;
4427     }
4428 
4429   if (!this->elf_flags_set_)
4430     {
4431       this->elf_flags_ = flags;
4432       this->elf_flags_set_ = true;
4433     }
4434   else
4435     {
4436       // Accumulate cpu feature bits.
4437       this->elf_flags_ |= (flags & (elfcpp::EF_SPARC_32PLUS
4438 				    | elfcpp::EF_SPARC_SUN_US1
4439 				    | elfcpp::EF_SPARC_HAL_R1
4440 				    | elfcpp::EF_SPARC_SUN_US3));
4441 
4442       // Bump the memory model setting to the most restrictive
4443       // one we encounter.
4444       omm = (this->elf_flags_ & elfcpp::EF_SPARCV9_MM);
4445       mm = (flags & elfcpp::EF_SPARCV9_MM);
4446       if (omm != mm)
4447 	{
4448 	  if (mm == elfcpp::EF_SPARCV9_TSO)
4449 	    {
4450 	      this->elf_flags_ &= ~elfcpp::EF_SPARCV9_MM;
4451 	      this->elf_flags_ |= elfcpp::EF_SPARCV9_TSO;
4452 	    }
4453 	  else if (mm == elfcpp::EF_SPARCV9_PSO
4454 		   && omm == elfcpp::EF_SPARCV9_RMO)
4455 	    {
4456 	      this->elf_flags_ &= ~elfcpp::EF_SPARCV9_MM;
4457 	      this->elf_flags_ |= elfcpp::EF_SPARCV9_PSO;
4458 	    }
4459 	}
4460     }
4461 
4462   // Validate that the little-endian flag matches how we've
4463   // been instantiated.
4464   if (!(flags & elfcpp::EF_SPARC_LEDATA) != big_endian)
4465     {
4466       if (big_endian)
4467 	gold_error(_("%s: little endian elf flag set on BE object"),
4468 		     name.c_str());
4469       else
4470 	gold_error(_("%s: little endian elf flag clear on LE object"),
4471 		     name.c_str());
4472     }
4473 
4474   return Target::do_make_elf_object(name, input_file, offset, ehdr);
4475 }
4476 
4477 // Adjust ELF file header.
4478 
4479 template<int size, bool big_endian>
4480 void
do_adjust_elf_header(unsigned char * view,int len)4481 Target_sparc<size, big_endian>::do_adjust_elf_header(
4482     unsigned char* view,
4483     int len)
4484 {
4485   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
4486 
4487   oehdr.put_e_machine(this->elf_machine_);
4488   oehdr.put_e_flags(this->elf_flags_);
4489 
4490   Sized_target<size, big_endian>::do_adjust_elf_header(view, len);
4491 }
4492 
4493 // The selector for sparc object files.
4494 
4495 template<int size, bool big_endian>
4496 class Target_selector_sparc : public Target_selector
4497 {
4498 public:
Target_selector_sparc()4499   Target_selector_sparc()
4500     : Target_selector(elfcpp::EM_NONE, size, big_endian,
4501 		      (size == 64 ? "elf64-sparc" : "elf32-sparc"),
4502 		      (size == 64 ? "elf64_sparc" : "elf32_sparc"))
4503   { }
4504 
4505   virtual Target*
do_recognize(Input_file *,off_t,int machine,int,int)4506   do_recognize(Input_file*, off_t, int machine, int, int)
4507   {
4508     switch (size)
4509       {
4510       case 64:
4511 	if (machine != elfcpp::EM_SPARCV9)
4512 	  return NULL;
4513 	break;
4514 
4515       case 32:
4516 	if (machine != elfcpp::EM_SPARC
4517 	    && machine != elfcpp::EM_SPARC32PLUS)
4518 	  return NULL;
4519 	break;
4520 
4521       default:
4522 	return NULL;
4523       }
4524 
4525     return this->instantiate_target();
4526   }
4527 
4528   virtual Target*
do_instantiate_target()4529   do_instantiate_target()
4530   { return new Target_sparc<size, big_endian>(); }
4531 };
4532 
4533 Target_selector_sparc<32, true> target_selector_sparc32;
4534 Target_selector_sparc<64, true> target_selector_sparc64;
4535 
4536 } // End anonymous namespace.
4537