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