* options.h (class General_options): Define
[external/binutils.git] / gold / sparc.cc
1 // sparc.cc -- sparc target support for gold.
2
3 // Copyright 2008, 2009 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
44 namespace
45 {
46
47 using namespace gold;
48
49 template<int size, bool big_endian>
50 class Output_data_plt_sparc;
51
52 template<int size, bool big_endian>
53 class Target_sparc : public Sized_target<size, big_endian>
54 {
55  public:
56   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
57
58   Target_sparc()
59     : Sized_target<size, big_endian>(&sparc_info),
60       got_(NULL), plt_(NULL), rela_dyn_(NULL),
61       copy_relocs_(elfcpp::R_SPARC_COPY), dynbss_(NULL),
62       got_mod_index_offset_(-1U), tls_get_addr_sym_(NULL)
63   {
64   }
65
66   // Process the relocations to determine unreferenced sections for 
67   // garbage collection.
68   void
69   gc_process_relocs(const General_options& options,
70                     Symbol_table* symtab,
71                     Layout* layout,
72                     Sized_relobj<size, big_endian>* object,
73                     unsigned int data_shndx,
74                     unsigned int sh_type,
75                     const unsigned char* prelocs,
76                     size_t reloc_count,
77                     Output_section* output_section,
78                     bool needs_special_offset_handling,
79                     size_t local_symbol_count,
80                     const unsigned char* plocal_symbols);
81
82   // Scan the relocations to look for symbol adjustments.
83   void
84   scan_relocs(const General_options& options,
85               Symbol_table* symtab,
86               Layout* layout,
87               Sized_relobj<size, big_endian>* object,
88               unsigned int data_shndx,
89               unsigned int sh_type,
90               const unsigned char* prelocs,
91               size_t reloc_count,
92               Output_section* output_section,
93               bool needs_special_offset_handling,
94               size_t local_symbol_count,
95               const unsigned char* plocal_symbols);
96   // Finalize the sections.
97   void
98   do_finalize_sections(Layout*);
99
100   // Return the value to use for a dynamic which requires special
101   // treatment.
102   uint64_t
103   do_dynsym_value(const Symbol*) const;
104
105   // Relocate a section.
106   void
107   relocate_section(const Relocate_info<size, big_endian>*,
108                    unsigned int sh_type,
109                    const unsigned char* prelocs,
110                    size_t reloc_count,
111                    Output_section* output_section,
112                    bool needs_special_offset_handling,
113                    unsigned char* view,
114                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
115                    section_size_type view_size,
116                    const Reloc_symbol_changes*);
117
118   // Scan the relocs during a relocatable link.
119   void
120   scan_relocatable_relocs(const General_options& options,
121                           Symbol_table* symtab,
122                           Layout* layout,
123                           Sized_relobj<size, big_endian>* object,
124                           unsigned int data_shndx,
125                           unsigned int sh_type,
126                           const unsigned char* prelocs,
127                           size_t reloc_count,
128                           Output_section* output_section,
129                           bool needs_special_offset_handling,
130                           size_t local_symbol_count,
131                           const unsigned char* plocal_symbols,
132                           Relocatable_relocs*);
133
134   // Relocate a section during a relocatable link.
135   void
136   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
137                            unsigned int sh_type,
138                            const unsigned char* prelocs,
139                            size_t reloc_count,
140                            Output_section* output_section,
141                            off_t offset_in_output_section,
142                            const Relocatable_relocs*,
143                            unsigned char* view,
144                            typename elfcpp::Elf_types<size>::Elf_Addr view_address,
145                            section_size_type view_size,
146                            unsigned char* reloc_view,
147                            section_size_type reloc_view_size);
148   // Return whether SYM is defined by the ABI.
149   bool
150   do_is_defined_by_abi(const Symbol* sym) const
151   {
152     // XXX Really need to support this better...
153     if (sym->type() == elfcpp::STT_SPARC_REGISTER)
154       return 1;
155
156     return strcmp(sym->name(), "___tls_get_addr") == 0;
157   }
158
159   // Return whether there is a GOT section.
160   bool
161   has_got_section() const
162   { return this->got_ != NULL; }
163
164   // Return the size of the GOT section.
165   section_size_type
166   got_size()
167   {
168     gold_assert(this->got_ != NULL);
169     return this->got_->data_size();
170   }
171
172  private:
173
174   // The class which scans relocations.
175   class Scan
176   {
177   public:
178     Scan()
179       : issued_non_pic_error_(false)
180     { }
181
182     inline void
183     local(const General_options& options, Symbol_table* symtab,
184           Layout* layout, Target_sparc* target,
185           Sized_relobj<size, big_endian>* object,
186           unsigned int data_shndx,
187           Output_section* output_section,
188           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
189           const elfcpp::Sym<size, big_endian>& lsym);
190
191     inline void
192     global(const General_options& options, Symbol_table* symtab,
193            Layout* layout, Target_sparc* target,
194            Sized_relobj<size, big_endian>* object,
195            unsigned int data_shndx,
196            Output_section* output_section,
197            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
198            Symbol* gsym);
199
200   private:
201     static void
202     unsupported_reloc_local(Sized_relobj<size, big_endian>*,
203                             unsigned int r_type);
204
205     static void
206     unsupported_reloc_global(Sized_relobj<size, big_endian>*,
207                              unsigned int r_type, Symbol*);
208
209     static void
210     generate_tls_call(Symbol_table* symtab, Layout* layout,
211                       Target_sparc* target);
212
213     void
214     check_non_pic(Relobj*, unsigned int r_type);
215
216     // Whether we have issued an error about a non-PIC compilation.
217     bool issued_non_pic_error_;
218   };
219
220   // The class which implements relocation.
221   class Relocate
222   {
223    public:
224     Relocate()
225       : ignore_gd_add_(false)
226     { }
227
228     ~Relocate()
229     {
230       if (this->ignore_gd_add_)
231         {
232           // FIXME: This needs to specify the location somehow.
233           gold_error(_("missing expected TLS relocation"));
234         }
235     }
236
237     // Do a relocation.  Return false if the caller should not issue
238     // any warnings about this relocation.
239     inline bool
240     relocate(const Relocate_info<size, big_endian>*, Target_sparc*,
241              Output_section*, size_t relnum,
242              const elfcpp::Rela<size, big_endian>&,
243              unsigned int r_type, const Sized_symbol<size>*,
244              const Symbol_value<size>*,
245              unsigned char*,
246              typename elfcpp::Elf_types<size>::Elf_Addr,
247              section_size_type);
248
249    private:
250     // Do a TLS relocation.
251     inline void
252     relocate_tls(const Relocate_info<size, big_endian>*, Target_sparc* target,
253                  size_t relnum, const elfcpp::Rela<size, big_endian>&,
254                  unsigned int r_type, const Sized_symbol<size>*,
255                  const Symbol_value<size>*,
256                  unsigned char*,
257                  typename elfcpp::Elf_types<size>::Elf_Addr,
258                  section_size_type);
259
260     // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
261     bool ignore_gd_add_;
262   };
263
264   // A class which returns the size required for a relocation type,
265   // used while scanning relocs during a relocatable link.
266   class Relocatable_size_for_reloc
267   {
268    public:
269     unsigned int
270     get_size_for_reloc(unsigned int, Relobj*);
271   };
272
273   // Get the GOT section, creating it if necessary.
274   Output_data_got<size, big_endian>*
275   got_section(Symbol_table*, Layout*);
276
277   // Create a PLT entry for a global symbol.
278   void
279   make_plt_entry(Symbol_table*, Layout*, Symbol*);
280
281   // Create a GOT entry for the TLS module index.
282   unsigned int
283   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
284                       Sized_relobj<size, big_endian>* object);
285
286   // Return the gsym for "__tls_get_addr".  Cache if not already
287   // cached.
288   Symbol*
289   tls_get_addr_sym(Symbol_table* symtab)
290   {
291     if (!this->tls_get_addr_sym_)
292       this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
293     gold_assert(this->tls_get_addr_sym_);
294     return this->tls_get_addr_sym_;
295   }
296
297   // Get the PLT section.
298   const Output_data_plt_sparc<size, big_endian>*
299   plt_section() const
300   {
301     gold_assert(this->plt_ != NULL);
302     return this->plt_;
303   }
304
305   // Get the dynamic reloc section, creating it if necessary.
306   Reloc_section*
307   rela_dyn_section(Layout*);
308
309   // Copy a relocation against a global symbol.
310   void
311   copy_reloc(Symbol_table* symtab, Layout* layout,
312              Sized_relobj<size, big_endian>* object,
313              unsigned int shndx, Output_section* output_section,
314              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
315   {
316     this->copy_relocs_.copy_reloc(symtab, layout,
317                                   symtab->get_sized_symbol<size>(sym),
318                                   object, shndx, output_section,
319                                   reloc, this->rela_dyn_section(layout));
320   }
321
322   // Information about this specific target which we pass to the
323   // general Target structure.
324   static Target::Target_info sparc_info;
325
326   // The types of GOT entries needed for this platform.
327   enum Got_type
328   {
329     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
330     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
331     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
332   };
333
334   // The GOT section.
335   Output_data_got<size, big_endian>* got_;
336   // The PLT section.
337   Output_data_plt_sparc<size, big_endian>* plt_;
338   // The dynamic reloc section.
339   Reloc_section* rela_dyn_;
340   // Relocs saved to avoid a COPY reloc.
341   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
342   // Space for variables copied with a COPY reloc.
343   Output_data_space* dynbss_;
344   // Offset of the GOT entry for the TLS module index;
345   unsigned int got_mod_index_offset_;
346   // Cached pointer to __tls_get_addr symbol
347   Symbol* tls_get_addr_sym_;
348 };
349
350 template<>
351 Target::Target_info Target_sparc<32, true>::sparc_info =
352 {
353   32,                   // size
354   true,                 // is_big_endian
355   elfcpp::EM_SPARC,     // machine_code
356   false,                // has_make_symbol
357   false,                // has_resolve
358   false,                // has_code_fill
359   true,                 // is_default_stack_executable
360   '\0',                 // wrap_char
361   "/usr/lib/ld.so.1",   // dynamic_linker
362   0x00010000,           // default_text_segment_address
363   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
364   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
365   elfcpp::SHN_UNDEF,    // small_common_shndx
366   elfcpp::SHN_UNDEF,    // large_common_shndx
367   0,                    // small_common_section_flags
368   0                     // large_common_section_flags
369 };
370
371 template<>
372 Target::Target_info Target_sparc<64, true>::sparc_info =
373 {
374   64,                   // size
375   true,                 // is_big_endian
376   elfcpp::EM_SPARCV9,   // machine_code
377   false,                // has_make_symbol
378   false,                // has_resolve
379   false,                // has_code_fill
380   true,                 // is_default_stack_executable
381   '\0',                 // wrap_char
382   "/usr/lib/sparcv9/ld.so.1",   // dynamic_linker
383   0x100000,             // default_text_segment_address
384   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
385   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
386   elfcpp::SHN_UNDEF,    // small_common_shndx
387   elfcpp::SHN_UNDEF,    // large_common_shndx
388   0,                    // small_common_section_flags
389   0                     // large_common_section_flags
390 };
391
392 // We have to take care here, even when operating in little-endian
393 // mode, sparc instructions are still big endian.
394 template<int size, bool big_endian>
395 class Sparc_relocate_functions
396 {
397 private:
398   // Do a simple relocation with the addend in the relocation.
399   template<int valsize>
400   static inline void
401   rela(unsigned char* view,
402        unsigned int right_shift,
403        typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
404        typename elfcpp::Swap<size, big_endian>::Valtype value,
405        typename elfcpp::Swap<size, big_endian>::Valtype addend)
406   {
407     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
408     Valtype* wv = reinterpret_cast<Valtype*>(view);
409     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
410     Valtype reloc = ((value + addend) >> right_shift);
411
412     val &= ~dst_mask;
413     reloc &= dst_mask;
414
415     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
416   }
417
418   // Do a simple relocation using a symbol value with the addend in
419   // the relocation.
420   template<int valsize>
421   static inline void
422   rela(unsigned char* view,
423        unsigned int right_shift,
424        typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
425        const Sized_relobj<size, big_endian>* object,
426        const Symbol_value<size>* psymval,
427        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
428   {
429     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
430     Valtype* wv = reinterpret_cast<Valtype*>(view);
431     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
432     Valtype reloc = (psymval->value(object, addend) >> right_shift);
433
434     val &= ~dst_mask;
435     reloc &= dst_mask;
436
437     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
438   }
439
440   // Do a simple relocation using a symbol value with the addend in
441   // the relocation, unaligned.
442   template<int valsize>
443   static inline void
444   rela_ua(unsigned char* view,
445           unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
446           const Sized_relobj<size, big_endian>* object,
447           const Symbol_value<size>* psymval,
448           typename elfcpp::Swap<size, big_endian>::Valtype addend)
449   {
450     typedef typename elfcpp::Swap_unaligned<valsize,
451             big_endian>::Valtype Valtype;
452     unsigned char* wv = view;
453     Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
454     Valtype reloc = (psymval->value(object, addend) >> right_shift);
455
456     val &= ~dst_mask;
457     reloc &= dst_mask;
458
459     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
460   }
461
462   // Do a simple PC relative relocation with a Symbol_value with the
463   // addend in the relocation.
464   template<int valsize>
465   static inline void
466   pcrela(unsigned char* view,
467          unsigned int right_shift,
468          typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
469          const Sized_relobj<size, big_endian>* object,
470          const Symbol_value<size>* psymval,
471          typename elfcpp::Swap<size, big_endian>::Valtype addend,
472          typename elfcpp::Elf_types<size>::Elf_Addr address)
473   {
474     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
475     Valtype* wv = reinterpret_cast<Valtype*>(view);
476     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
477     Valtype reloc = ((psymval->value(object, addend) - address)
478                      >> right_shift);
479
480     val &= ~dst_mask;
481     reloc &= dst_mask;
482
483     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
484   }
485
486   template<int valsize>
487   static inline void
488   pcrela_unaligned(unsigned char* view,
489                    const Sized_relobj<size, big_endian>* object,
490                    const Symbol_value<size>* psymval,
491                    typename elfcpp::Swap<size, big_endian>::Valtype addend,
492                    typename elfcpp::Elf_types<size>::Elf_Addr address)
493   {
494     typedef typename elfcpp::Swap_unaligned<valsize,
495             big_endian>::Valtype Valtype;
496     unsigned char* wv = view;
497     Valtype reloc = (psymval->value(object, addend) - address);
498
499     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
500   }
501
502   typedef Sparc_relocate_functions<size, big_endian> This;
503   typedef Sparc_relocate_functions<size, true> This_insn;
504
505 public:
506   // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
507   static inline void
508   wdisp30(unsigned char* view,
509            const Sized_relobj<size, big_endian>* object,
510            const Symbol_value<size>* psymval,
511            typename elfcpp::Elf_types<size>::Elf_Addr addend,
512            typename elfcpp::Elf_types<size>::Elf_Addr address)
513   {
514     This_insn::template pcrela<32>(view, 2, 0x3fffffff, object,
515                                    psymval, addend, address);
516   }
517
518   // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
519   static inline void
520   wdisp22(unsigned char* view,
521            const Sized_relobj<size, big_endian>* object,
522            const Symbol_value<size>* psymval,
523            typename elfcpp::Elf_types<size>::Elf_Addr addend,
524            typename elfcpp::Elf_types<size>::Elf_Addr address)
525   {
526     This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
527                                    psymval, addend, address);
528   }
529
530   // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
531   static inline void
532   wdisp19(unsigned char* view,
533           const Sized_relobj<size, big_endian>* object,
534           const Symbol_value<size>* psymval,
535           typename elfcpp::Elf_types<size>::Elf_Addr addend,
536           typename elfcpp::Elf_types<size>::Elf_Addr address)
537   {
538     This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
539                                    psymval, addend, address);
540   }
541
542   // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
543   static inline void
544   wdisp16(unsigned char* view,
545           const Sized_relobj<size, big_endian>* object,
546           const Symbol_value<size>* psymval,
547           typename elfcpp::Elf_types<size>::Elf_Addr addend,
548           typename elfcpp::Elf_types<size>::Elf_Addr address)
549   {
550     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
551     Valtype* wv = reinterpret_cast<Valtype*>(view);
552     Valtype val = elfcpp::Swap<32, true>::readval(wv);
553     Valtype reloc = ((psymval->value(object, addend) - address)
554                      >> 2);
555
556     // The relocation value is split between the low 14 bits,
557     // and bits 20-21.
558     val &= ~((0x3 << 20) | 0x3fff);
559     reloc = (((reloc & 0xc000) << (20 - 14))
560              | (reloc & 0x3ffff));
561
562     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
563   }
564
565   // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
566   static inline void
567   pc22(unsigned char* view,
568        const Sized_relobj<size, big_endian>* object,
569        const Symbol_value<size>* psymval,
570        typename elfcpp::Elf_types<size>::Elf_Addr addend,
571        typename elfcpp::Elf_types<size>::Elf_Addr address)
572   {
573     This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
574                                    psymval, addend, address);
575   }
576
577   // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
578   static inline void
579   pc10(unsigned char* view,
580        const Sized_relobj<size, big_endian>* object,
581        const Symbol_value<size>* psymval,
582        typename elfcpp::Elf_types<size>::Elf_Addr addend,
583        typename elfcpp::Elf_types<size>::Elf_Addr address)
584   {
585     This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
586                                    psymval, addend, address);
587   }
588
589   // R_SPARC_HI22: (Symbol + Addend) >> 10
590   static inline void
591   hi22(unsigned char* view,
592        typename elfcpp::Elf_types<size>::Elf_Addr value,
593        typename elfcpp::Elf_types<size>::Elf_Addr addend)
594   {
595     This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
596   }
597
598   // R_SPARC_HI22: (Symbol + Addend) >> 10
599   static inline void
600   hi22(unsigned char* view,
601        const Sized_relobj<size, big_endian>* object,
602        const Symbol_value<size>* psymval,
603        typename elfcpp::Elf_types<size>::Elf_Addr addend)
604   {
605     This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
606   }
607
608   // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
609   static inline void
610   pcplt22(unsigned char* view,
611           const Sized_relobj<size, big_endian>* object,
612           const Symbol_value<size>* psymval,
613           typename elfcpp::Elf_types<size>::Elf_Addr addend,
614           typename elfcpp::Elf_types<size>::Elf_Addr address)
615   {
616     This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
617                                    psymval, addend, address);
618   }
619
620   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
621   static inline void
622   lo10(unsigned char* view,
623        typename elfcpp::Elf_types<size>::Elf_Addr value,
624        typename elfcpp::Elf_types<size>::Elf_Addr addend)
625   {
626     This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
627   }
628
629   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
630   static inline void
631   lo10(unsigned char* view,
632        const Sized_relobj<size, big_endian>* object,
633        const Symbol_value<size>* psymval,
634        typename elfcpp::Elf_types<size>::Elf_Addr addend)
635   {
636     This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
637   }
638
639   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
640   static inline void
641   lo10(unsigned char* view,
642        const Sized_relobj<size, big_endian>* object,
643        const Symbol_value<size>* psymval,
644        typename elfcpp::Elf_types<size>::Elf_Addr addend,
645        typename elfcpp::Elf_types<size>::Elf_Addr address)
646   {
647     This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
648                                    psymval, addend, address);
649   }
650
651   // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
652   static inline void
653   olo10(unsigned char* view,
654         const Sized_relobj<size, big_endian>* object,
655         const Symbol_value<size>* psymval,
656         typename elfcpp::Elf_types<size>::Elf_Addr addend,
657         typename elfcpp::Elf_types<size>::Elf_Addr addend2)
658   {
659     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
660     Valtype* wv = reinterpret_cast<Valtype*>(view);
661     Valtype val = elfcpp::Swap<32, true>::readval(wv);
662     Valtype reloc = psymval->value(object, addend);
663
664     val &= ~0x1fff;
665     reloc &= 0x3ff;
666     reloc += addend2;
667     reloc &= 0x1fff;
668
669     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
670   }
671
672   // R_SPARC_22: (Symbol + Addend)
673   static inline void
674   rela32_22(unsigned char* view,
675             const Sized_relobj<size, big_endian>* object,
676             const Symbol_value<size>* psymval,
677             typename elfcpp::Elf_types<size>::Elf_Addr addend)
678   {
679     This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
680   }
681
682   // R_SPARC_13: (Symbol + Addend)
683   static inline void
684   rela32_13(unsigned char* view,
685             typename elfcpp::Elf_types<size>::Elf_Addr value,
686             typename elfcpp::Elf_types<size>::Elf_Addr addend)
687   {
688     This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
689   }
690
691   // R_SPARC_13: (Symbol + Addend)
692   static inline void
693   rela32_13(unsigned char* view,
694             const Sized_relobj<size, big_endian>* object,
695             const Symbol_value<size>* psymval,
696             typename elfcpp::Elf_types<size>::Elf_Addr addend)
697   {
698     This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
699   }
700
701   // R_SPARC_UA16: (Symbol + Addend)
702   static inline void
703   ua16(unsigned char* view,
704        const Sized_relobj<size, big_endian>* object,
705        const Symbol_value<size>* psymval,
706        typename elfcpp::Elf_types<size>::Elf_Addr addend)
707   {
708     This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
709   }
710
711   // R_SPARC_UA32: (Symbol + Addend)
712   static inline void
713   ua32(unsigned char* view,
714        const Sized_relobj<size, big_endian>* object,
715        const Symbol_value<size>* psymval,
716        typename elfcpp::Elf_types<size>::Elf_Addr addend)
717   {
718     This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
719   }
720
721   // R_SPARC_UA64: (Symbol + Addend)
722   static inline void
723   ua64(unsigned char* view,
724        const Sized_relobj<size, big_endian>* object,
725        const Symbol_value<size>* psymval,
726        typename elfcpp::Elf_types<size>::Elf_Addr addend)
727   {
728     This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
729                                object, psymval, addend);
730   }
731
732   // R_SPARC_DISP8: (Symbol + Addend - Address)
733   static inline void
734   disp8(unsigned char* view,
735         const Sized_relobj<size, big_endian>* object,
736         const Symbol_value<size>* psymval,
737         typename elfcpp::Elf_types<size>::Elf_Addr addend,
738         typename elfcpp::Elf_types<size>::Elf_Addr address)
739   {
740     This::template pcrela_unaligned<8>(view, object, psymval,
741                                        addend, address);
742   }
743
744   // R_SPARC_DISP16: (Symbol + Addend - Address)
745   static inline void
746   disp16(unsigned char* view,
747          const Sized_relobj<size, big_endian>* object,
748          const Symbol_value<size>* psymval,
749          typename elfcpp::Elf_types<size>::Elf_Addr addend,
750          typename elfcpp::Elf_types<size>::Elf_Addr address)
751   {
752     This::template pcrela_unaligned<16>(view, object, psymval,
753                                         addend, address);
754   }
755
756   // R_SPARC_DISP32: (Symbol + Addend - Address)
757   static inline void
758   disp32(unsigned char* view,
759          const Sized_relobj<size, big_endian>* object,
760          const Symbol_value<size>* psymval,
761          typename elfcpp::Elf_types<size>::Elf_Addr addend,
762          typename elfcpp::Elf_types<size>::Elf_Addr address)
763   {
764     This::template pcrela_unaligned<32>(view, object, psymval,
765                                         addend, address);
766   }
767
768   // R_SPARC_DISP64: (Symbol + Addend - Address)
769   static inline void
770   disp64(unsigned char* view,
771          const Sized_relobj<size, big_endian>* object,
772          const Symbol_value<size>* psymval,
773          elfcpp::Elf_Xword addend,
774          typename elfcpp::Elf_types<size>::Elf_Addr address)
775   {
776     This::template pcrela_unaligned<64>(view, object, psymval,
777                                         addend, address);
778   }
779
780   // R_SPARC_H44: (Symbol + Addend) >> 22
781   static inline void
782   h44(unsigned char* view,
783       const Sized_relobj<size, big_endian>* object,
784       const Symbol_value<size>* psymval,
785       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
786   {
787     This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
788   }
789
790   // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
791   static inline void
792   m44(unsigned char* view,
793       const Sized_relobj<size, big_endian>* object,
794       const Symbol_value<size>* psymval,
795       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
796   {
797     This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
798   }
799
800   // R_SPARC_L44: (Symbol + Addend) & 0xfff
801   static inline void
802   l44(unsigned char* view,
803       const Sized_relobj<size, big_endian>* object,
804       const Symbol_value<size>* psymval,
805       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
806   {
807     This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
808   }
809
810   // R_SPARC_HH22: (Symbol + Addend) >> 42
811   static inline void
812   hh22(unsigned char* view,
813        const Sized_relobj<size, big_endian>* object,
814        const Symbol_value<size>* psymval,
815        typename elfcpp::Elf_types<size>::Elf_Addr addend)
816   {
817     This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
818   }
819
820   // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
821   static inline void
822   pc_hh22(unsigned char* view,
823           const Sized_relobj<size, big_endian>* object,
824           const Symbol_value<size>* psymval,
825           typename elfcpp::Elf_types<size>::Elf_Addr addend,
826           typename elfcpp::Elf_types<size>::Elf_Addr address)
827   {
828     This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
829                                    psymval, addend, address);
830   }
831
832   // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
833   static inline void
834   hm10(unsigned char* view,
835        const Sized_relobj<size, big_endian>* object,
836        const Symbol_value<size>* psymval,
837        typename elfcpp::Elf_types<size>::Elf_Addr addend)
838   {
839     This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
840   }
841
842   // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
843   static inline void
844   pc_hm10(unsigned char* view,
845           const Sized_relobj<size, big_endian>* object,
846           const Symbol_value<size>* psymval,
847           typename elfcpp::Elf_types<size>::Elf_Addr addend,
848           typename elfcpp::Elf_types<size>::Elf_Addr address)
849   {
850     This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
851                                    psymval, addend, address);
852   }
853
854   // R_SPARC_11: (Symbol + Addend)
855   static inline void
856   rela32_11(unsigned char* view,
857             const Sized_relobj<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, 0x000007ff, object, psymval, addend);
862   }
863
864   // R_SPARC_10: (Symbol + Addend)
865   static inline void
866   rela32_10(unsigned char* view,
867             const Sized_relobj<size, big_endian>* object,
868             const Symbol_value<size>* psymval,
869             typename elfcpp::Elf_types<size>::Elf_Addr addend)
870   {
871     This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
872   }
873
874   // R_SPARC_7: (Symbol + Addend)
875   static inline void
876   rela32_7(unsigned char* view,
877            const Sized_relobj<size, big_endian>* object,
878            const Symbol_value<size>* psymval,
879            typename elfcpp::Elf_types<size>::Elf_Addr addend)
880   {
881     This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
882   }
883
884   // R_SPARC_6: (Symbol + Addend)
885   static inline void
886   rela32_6(unsigned char* view,
887            const Sized_relobj<size, big_endian>* object,
888            const Symbol_value<size>* psymval,
889            typename elfcpp::Elf_types<size>::Elf_Addr addend)
890   {
891     This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
892   }
893
894   // R_SPARC_5: (Symbol + Addend)
895   static inline void
896   rela32_5(unsigned char* view,
897            const Sized_relobj<size, big_endian>* object,
898            const Symbol_value<size>* psymval,
899            typename elfcpp::Elf_types<size>::Elf_Addr addend)
900   {
901     This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
902   }
903
904   // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
905   static inline void
906   ldo_hix22(unsigned char* view,
907             typename elfcpp::Elf_types<size>::Elf_Addr value,
908             typename elfcpp::Elf_types<size>::Elf_Addr addend)
909   {
910     This_insn::hi22(view, value, addend);
911   }
912
913   // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
914   static inline void
915   ldo_lox10(unsigned char* view,
916             typename elfcpp::Elf_types<size>::Elf_Addr value,
917             typename elfcpp::Elf_types<size>::Elf_Addr addend)
918   {
919     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
920     Valtype* wv = reinterpret_cast<Valtype*>(view);
921     Valtype val = elfcpp::Swap<32, true>::readval(wv);
922     Valtype reloc = (value + addend);
923
924     val &= ~0x1fff;
925     reloc &= 0x3ff;
926
927     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
928   }
929
930   // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
931   static inline void
932   hix22(unsigned char* view,
933         typename elfcpp::Elf_types<size>::Elf_Addr value,
934         typename elfcpp::Elf_types<size>::Elf_Addr addend)
935   {
936     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
937     Valtype* wv = reinterpret_cast<Valtype*>(view);
938     Valtype val = elfcpp::Swap<32, true>::readval(wv);
939     Valtype reloc = (value + addend);
940
941     val &= ~0x3fffff;
942
943     reloc ^= ~(Valtype)0;
944     reloc >>= 10;
945
946     reloc &= 0x3fffff;
947
948     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
949   }
950
951   // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
952   static inline void
953   hix22(unsigned char* view,
954         const Sized_relobj<size, big_endian>* object,
955         const Symbol_value<size>* psymval,
956         typename elfcpp::Elf_types<size>::Elf_Addr addend)
957   {
958     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
959     Valtype* wv = reinterpret_cast<Valtype*>(view);
960     Valtype val = elfcpp::Swap<32, true>::readval(wv);
961     Valtype reloc = psymval->value(object, addend);
962
963     val &= ~0x3fffff;
964
965     reloc ^= ~(Valtype)0;
966     reloc >>= 10;
967
968     reloc &= 0x3fffff;
969
970     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
971   }
972
973
974   // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
975   static inline void
976   lox10(unsigned char* view,
977         typename elfcpp::Elf_types<size>::Elf_Addr value,
978         typename elfcpp::Elf_types<size>::Elf_Addr addend)
979   {
980     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
981     Valtype* wv = reinterpret_cast<Valtype*>(view);
982     Valtype val = elfcpp::Swap<32, true>::readval(wv);
983     Valtype reloc = (value + addend);
984
985     val &= ~0x1fff;
986     reloc &= 0x3ff;
987     reloc |= 0x1c00;
988
989     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
990   }
991
992   // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
993   static inline void
994   lox10(unsigned char* view,
995         const Sized_relobj<size, big_endian>* object,
996         const Symbol_value<size>* psymval,
997         typename elfcpp::Elf_types<size>::Elf_Addr addend)
998   {
999     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1000     Valtype* wv = reinterpret_cast<Valtype*>(view);
1001     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1002     Valtype reloc = psymval->value(object, addend);
1003
1004     val &= ~0x1fff;
1005     reloc &= 0x3ff;
1006     reloc |= 0x1c00;
1007
1008     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1009   }
1010 };
1011
1012 // Get the GOT section, creating it if necessary.
1013
1014 template<int size, bool big_endian>
1015 Output_data_got<size, big_endian>*
1016 Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
1017                                             Layout* layout)
1018 {
1019   if (this->got_ == NULL)
1020     {
1021       gold_assert(symtab != NULL && layout != NULL);
1022
1023       this->got_ = new Output_data_got<size, big_endian>();
1024
1025       Output_section* os;
1026       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1027                                            (elfcpp::SHF_ALLOC
1028                                             | elfcpp::SHF_WRITE),
1029                                            this->got_);
1030       os->set_is_relro();
1031
1032       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1033       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1034                                     this->got_,
1035                                     0, 0, elfcpp::STT_OBJECT,
1036                                     elfcpp::STB_LOCAL,
1037                                     elfcpp::STV_HIDDEN, 0,
1038                                     false, false);
1039     }
1040
1041   return this->got_;
1042 }
1043
1044 // Get the dynamic reloc section, creating it if necessary.
1045
1046 template<int size, bool big_endian>
1047 typename Target_sparc<size, big_endian>::Reloc_section*
1048 Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
1049 {
1050   if (this->rela_dyn_ == NULL)
1051     {
1052       gold_assert(layout != NULL);
1053       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1054       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1055                                       elfcpp::SHF_ALLOC, this->rela_dyn_);
1056     }
1057   return this->rela_dyn_;
1058 }
1059
1060 // A class to handle the PLT data.
1061
1062 template<int size, bool big_endian>
1063 class Output_data_plt_sparc : public Output_section_data
1064 {
1065  public:
1066   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1067                             size, big_endian> Reloc_section;
1068
1069   Output_data_plt_sparc(Layout*);
1070
1071   // Add an entry to the PLT.
1072   void add_entry(Symbol* gsym);
1073
1074   // Return the .rela.plt section data.
1075   const Reloc_section* rel_plt() const
1076   {
1077     return this->rel_;
1078   }
1079
1080  protected:
1081   void do_adjust_output_section(Output_section* os);
1082
1083   // Write to a map file.
1084   void
1085   do_print_to_mapfile(Mapfile* mapfile) const
1086   { mapfile->print_output_data(this, _("** PLT")); }
1087
1088  private:
1089   // The size of an entry in the PLT.
1090   static const int base_plt_entry_size = (size == 32 ? 12 : 32);
1091
1092   static const unsigned int plt_entries_per_block = 160;
1093   static const unsigned int plt_insn_chunk_size = 24;
1094   static const unsigned int plt_pointer_chunk_size = 8;
1095   static const unsigned int plt_block_size =
1096     (plt_entries_per_block
1097      * (plt_insn_chunk_size + plt_pointer_chunk_size));
1098
1099   // Set the final size.
1100   void
1101   set_final_data_size()
1102   {
1103     unsigned int full_count = this->count_ + 4;
1104     unsigned int extra = (size == 32 ? 4 : 0);
1105
1106     if (size == 32 || full_count < 32768)
1107       this->set_data_size((full_count * base_plt_entry_size) + extra);
1108     else
1109       {
1110         unsigned int ext_cnt = full_count - 32768;
1111
1112         this->set_data_size((32768 * base_plt_entry_size)
1113                             + (ext_cnt
1114                                * (plt_insn_chunk_size
1115                                   + plt_pointer_chunk_size)));
1116       }
1117   }
1118
1119   // Write out the PLT data.
1120   void
1121   do_write(Output_file*);
1122
1123   // The reloc section.
1124   Reloc_section* rel_;
1125   // The number of PLT entries.
1126   unsigned int count_;
1127 };
1128
1129 // Define the constants as required by C++ standard.
1130
1131 template<int size, bool big_endian>
1132 const int Output_data_plt_sparc<size, big_endian>::base_plt_entry_size;
1133
1134 template<int size, bool big_endian>
1135 const unsigned int
1136 Output_data_plt_sparc<size, big_endian>::plt_entries_per_block;
1137
1138 template<int size, bool big_endian>
1139 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_insn_chunk_size;
1140
1141 template<int size, bool big_endian>
1142 const unsigned int
1143 Output_data_plt_sparc<size, big_endian>::plt_pointer_chunk_size;
1144
1145 template<int size, bool big_endian>
1146 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_block_size;
1147
1148 // Create the PLT section.  The ordinary .got section is an argument,
1149 // since we need to refer to the start.
1150
1151 template<int size, bool big_endian>
1152 Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
1153   : Output_section_data(size == 32 ? 4 : 8), count_(0)
1154 {
1155   this->rel_ = new Reloc_section(false);
1156   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1157                                   elfcpp::SHF_ALLOC, this->rel_);
1158 }
1159
1160 template<int size, bool big_endian>
1161 void
1162 Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
1163 {
1164   os->set_entsize(0);
1165 }
1166
1167 // Add an entry to the PLT.
1168
1169 template<int size, bool big_endian>
1170 void
1171 Output_data_plt_sparc<size, big_endian>::add_entry(Symbol* gsym)
1172 {
1173   gold_assert(!gsym->has_plt_offset());
1174
1175   unsigned int index = this->count_ + 4;
1176   section_offset_type plt_offset;
1177
1178   if (size == 32 || index < 32768)
1179     plt_offset = index * base_plt_entry_size;
1180   else
1181     {
1182         unsigned int ext_index = index - 32768;
1183
1184         plt_offset = (32768 * base_plt_entry_size)
1185           + ((ext_index / plt_entries_per_block)
1186              * plt_block_size)
1187           + ((ext_index % plt_entries_per_block)
1188              * plt_insn_chunk_size);
1189     }
1190
1191   gsym->set_plt_offset(plt_offset);
1192
1193   ++this->count_;
1194
1195   // Every PLT entry needs a reloc.
1196   gsym->set_needs_dynsym_entry();
1197   this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
1198                          plt_offset, 0);
1199
1200   // Note that we don't need to save the symbol.  The contents of the
1201   // PLT are independent of which symbols are used.  The symbols only
1202   // appear in the relocations.
1203 }
1204
1205 static const unsigned int sparc_nop = 0x01000000;
1206 static const unsigned int sparc_sethi_g1 = 0x03000000;
1207 static const unsigned int sparc_branch_always = 0x30800000;
1208 static const unsigned int sparc_branch_always_pt = 0x30680000;
1209 static const unsigned int sparc_mov = 0x80100000;
1210 static const unsigned int sparc_mov_g0_o0 = 0x90100000;
1211 static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
1212 static const unsigned int sparc_call_plus_8 = 0x40000002;
1213 static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
1214 static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
1215 static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
1216
1217 // Write out the PLT.
1218
1219 template<int size, bool big_endian>
1220 void
1221 Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
1222 {
1223   const off_t offset = this->offset();
1224   const section_size_type oview_size =
1225     convert_to_section_size_type(this->data_size());
1226   unsigned char* const oview = of->get_output_view(offset, oview_size);
1227   unsigned char* pov = oview;
1228
1229   memset(pov, 0, base_plt_entry_size * 4);
1230   pov += base_plt_entry_size * 4;
1231
1232   unsigned int plt_offset = base_plt_entry_size * 4;
1233   const unsigned int count = this->count_;
1234
1235   if (size == 64)
1236     {
1237       unsigned int limit;
1238
1239       limit = (count > 32768 ? 32768 : count);
1240
1241       for (unsigned int i = 0; i < limit; ++i)
1242         {
1243           elfcpp::Swap<32, true>::writeval(pov + 0x00,
1244                                            sparc_sethi_g1 + plt_offset);
1245           elfcpp::Swap<32, true>::writeval(pov + 0x04,
1246                                            sparc_branch_always_pt +
1247                                            (((base_plt_entry_size -
1248                                               (plt_offset + 4)) >> 2) &
1249                                             0x7ffff));
1250           elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1251           elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
1252           elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
1253           elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
1254           elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
1255           elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
1256
1257           pov += base_plt_entry_size;
1258           plt_offset += base_plt_entry_size;
1259         }
1260
1261       if (count > 32768)
1262         {
1263           unsigned int ext_cnt = count - 32768;
1264           unsigned int blks = ext_cnt / plt_entries_per_block;
1265
1266           for (unsigned int i = 0; i < blks; ++i)
1267             {
1268               unsigned int data_off = (plt_entries_per_block
1269                                        * plt_insn_chunk_size) - 4;
1270
1271               for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1272                 {
1273                   elfcpp::Swap<32, true>::writeval(pov + 0x00,
1274                                                    sparc_mov_o7_g5);
1275                   elfcpp::Swap<32, true>::writeval(pov + 0x04,
1276                                                    sparc_call_plus_8);
1277                   elfcpp::Swap<32, true>::writeval(pov + 0x08,
1278                                                    sparc_nop);
1279                   elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1280                                                    sparc_ldx_o7_imm_g1 +
1281                                                    (data_off & 0x1fff));
1282                   elfcpp::Swap<32, true>::writeval(pov + 0x10,
1283                                                    sparc_jmpl_o7_g1_g1);
1284                   elfcpp::Swap<32, true>::writeval(pov + 0x14,
1285                                                    sparc_mov_g5_o7);
1286
1287                   elfcpp::Swap<64, big_endian>::writeval(
1288                                 pov + 0x4 + data_off,
1289                                 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1290
1291                   pov += plt_insn_chunk_size;
1292                   data_off -= 16;
1293                 }
1294             }
1295
1296           unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
1297           for (unsigned int i = 0; i < sub_blk_cnt; ++i)
1298             {
1299               unsigned int data_off = (sub_blk_cnt
1300                                        * plt_insn_chunk_size) - 4;
1301
1302               for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1303                 {
1304                   elfcpp::Swap<32, true>::writeval(pov + 0x00,
1305                                                    sparc_mov_o7_g5);
1306                   elfcpp::Swap<32, true>::writeval(pov + 0x04,
1307                                                    sparc_call_plus_8);
1308                   elfcpp::Swap<32, true>::writeval(pov + 0x08,
1309                                                    sparc_nop);
1310                   elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1311                                                    sparc_ldx_o7_imm_g1 +
1312                                                    (data_off & 0x1fff));
1313                   elfcpp::Swap<32, true>::writeval(pov + 0x10,
1314                                                    sparc_jmpl_o7_g1_g1);
1315                   elfcpp::Swap<32, true>::writeval(pov + 0x14,
1316                                                    sparc_mov_g5_o7);
1317
1318                   elfcpp::Swap<64, big_endian>::writeval(
1319                                 pov + 0x4 + data_off,
1320                                 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1321
1322                   pov += plt_insn_chunk_size;
1323                   data_off -= 16;
1324                 }
1325             }
1326         }
1327     }
1328   else
1329     {
1330       for (unsigned int i = 0; i < count; ++i)
1331         {
1332           elfcpp::Swap<32, true>::writeval(pov + 0x00,
1333                                            sparc_sethi_g1 + plt_offset);
1334           elfcpp::Swap<32, true>::writeval(pov + 0x04,
1335                                            sparc_branch_always +
1336                                            (((- (plt_offset + 4)) >> 2) &
1337                                             0x003fffff));
1338           elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1339
1340           pov += base_plt_entry_size;
1341           plt_offset += base_plt_entry_size;
1342         }
1343
1344       elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
1345       pov += 4;
1346     }
1347
1348   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1349
1350   of->write_output_view(offset, oview_size, oview);
1351 }
1352
1353 // Create a PLT entry for a global symbol.
1354
1355 template<int size, bool big_endian>
1356 void
1357 Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1358                                                Layout* layout,
1359                                                Symbol* gsym)
1360 {
1361   if (gsym->has_plt_offset())
1362     return;
1363
1364   if (this->plt_ == NULL)
1365     {
1366       // Create the GOT sections first.
1367       this->got_section(symtab, layout);
1368
1369       this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
1370       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1371                                       (elfcpp::SHF_ALLOC
1372                                        | elfcpp::SHF_EXECINSTR
1373                                        | elfcpp::SHF_WRITE),
1374                                       this->plt_);
1375
1376       // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1377       symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1378                                     this->plt_,
1379                                     0, 0, elfcpp::STT_OBJECT,
1380                                     elfcpp::STB_LOCAL,
1381                                     elfcpp::STV_HIDDEN, 0,
1382                                     false, false);
1383     }
1384
1385   this->plt_->add_entry(gsym);
1386 }
1387
1388 // Create a GOT entry for the TLS module index.
1389
1390 template<int size, bool big_endian>
1391 unsigned int
1392 Target_sparc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
1393                                                     Layout* layout,
1394                                                     Sized_relobj<size, big_endian>* object)
1395 {
1396   if (this->got_mod_index_offset_ == -1U)
1397     {
1398       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1399       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1400       Output_data_got<size, big_endian>* got;
1401       unsigned int got_offset;
1402
1403       got = this->got_section(symtab, layout);
1404       got_offset = got->add_constant(0);
1405       rela_dyn->add_local(object, 0,
1406                           (size == 64 ?
1407                            elfcpp::R_SPARC_TLS_DTPMOD64 :
1408                            elfcpp::R_SPARC_TLS_DTPMOD32), got,
1409                           got_offset, 0);
1410       got->add_constant(0);
1411       this->got_mod_index_offset_ = got_offset;
1412     }
1413   return this->got_mod_index_offset_;
1414 }
1415
1416 // Optimize the TLS relocation type based on what we know about the
1417 // symbol.  IS_FINAL is true if the final address of this symbol is
1418 // known at link time.
1419
1420 static tls::Tls_optimization
1421 optimize_tls_reloc(bool is_final, int r_type)
1422 {
1423   // If we are generating a shared library, then we can't do anything
1424   // in the linker.
1425   if (parameters->options().shared())
1426     return tls::TLSOPT_NONE;
1427
1428   switch (r_type)
1429     {
1430     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1431     case elfcpp::R_SPARC_TLS_GD_LO10:
1432     case elfcpp::R_SPARC_TLS_GD_ADD:
1433     case elfcpp::R_SPARC_TLS_GD_CALL:
1434       // These are General-Dynamic which permits fully general TLS
1435       // access.  Since we know that we are generating an executable,
1436       // we can convert this to Initial-Exec.  If we also know that
1437       // this is a local symbol, we can further switch to Local-Exec.
1438       if (is_final)
1439         return tls::TLSOPT_TO_LE;
1440       return tls::TLSOPT_TO_IE;
1441
1442     case elfcpp::R_SPARC_TLS_LDM_HI22:  // Local-dynamic
1443     case elfcpp::R_SPARC_TLS_LDM_LO10:
1444     case elfcpp::R_SPARC_TLS_LDM_ADD:
1445     case elfcpp::R_SPARC_TLS_LDM_CALL:
1446       // This is Local-Dynamic, which refers to a local symbol in the
1447       // dynamic TLS block.  Since we know that we generating an
1448       // executable, we can switch to Local-Exec.
1449       return tls::TLSOPT_TO_LE;
1450
1451     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1452     case elfcpp::R_SPARC_TLS_LDO_LOX10:
1453     case elfcpp::R_SPARC_TLS_LDO_ADD:
1454       // Another type of Local-Dynamic relocation.
1455       return tls::TLSOPT_TO_LE;
1456
1457     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
1458     case elfcpp::R_SPARC_TLS_IE_LO10:
1459     case elfcpp::R_SPARC_TLS_IE_LD:
1460     case elfcpp::R_SPARC_TLS_IE_LDX:
1461     case elfcpp::R_SPARC_TLS_IE_ADD:
1462       // These are Initial-Exec relocs which get the thread offset
1463       // from the GOT.  If we know that we are linking against the
1464       // local symbol, we can switch to Local-Exec, which links the
1465       // thread offset into the instruction.
1466       if (is_final)
1467         return tls::TLSOPT_TO_LE;
1468       return tls::TLSOPT_NONE;
1469
1470     case elfcpp::R_SPARC_TLS_LE_HIX22:  // Local-exec
1471     case elfcpp::R_SPARC_TLS_LE_LOX10:
1472       // When we already have Local-Exec, there is nothing further we
1473       // can do.
1474       return tls::TLSOPT_NONE;
1475
1476     default:
1477       gold_unreachable();
1478     }
1479 }
1480
1481 // Generate a PLT entry slot for a call to __tls_get_addr
1482 template<int size, bool big_endian>
1483 void
1484 Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
1485                                                         Layout* layout,
1486                                                         Target_sparc<size, big_endian>* target)
1487 {
1488   Symbol* gsym = target->tls_get_addr_sym(symtab);
1489
1490   target->make_plt_entry(symtab, layout, gsym);
1491 }
1492
1493 // Report an unsupported relocation against a local symbol.
1494
1495 template<int size, bool big_endian>
1496 void
1497 Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
1498                         Sized_relobj<size, big_endian>* object,
1499                         unsigned int r_type)
1500 {
1501   gold_error(_("%s: unsupported reloc %u against local symbol"),
1502              object->name().c_str(), r_type);
1503 }
1504
1505 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1506 // dynamic linker does not support it, issue an error.
1507
1508 template<int size, bool big_endian>
1509 void
1510 Target_sparc<size, big_endian>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1511 {
1512   gold_assert(r_type != elfcpp::R_SPARC_NONE);
1513
1514   if (size == 64)
1515     {
1516       switch (r_type)
1517         {
1518           // These are the relocation types supported by glibc for sparc 64-bit.
1519         case elfcpp::R_SPARC_RELATIVE:
1520         case elfcpp::R_SPARC_COPY:
1521         case elfcpp::R_SPARC_64:
1522         case elfcpp::R_SPARC_GLOB_DAT:
1523         case elfcpp::R_SPARC_JMP_SLOT:
1524         case elfcpp::R_SPARC_TLS_DTPMOD64:
1525         case elfcpp::R_SPARC_TLS_DTPOFF64:
1526         case elfcpp::R_SPARC_TLS_TPOFF64:
1527         case elfcpp::R_SPARC_TLS_LE_HIX22:
1528         case elfcpp::R_SPARC_TLS_LE_LOX10:
1529         case elfcpp::R_SPARC_8:
1530         case elfcpp::R_SPARC_16:
1531         case elfcpp::R_SPARC_DISP8:
1532         case elfcpp::R_SPARC_DISP16:
1533         case elfcpp::R_SPARC_DISP32:
1534         case elfcpp::R_SPARC_WDISP30:
1535         case elfcpp::R_SPARC_LO10:
1536         case elfcpp::R_SPARC_HI22:
1537         case elfcpp::R_SPARC_OLO10:
1538         case elfcpp::R_SPARC_H44:
1539         case elfcpp::R_SPARC_M44:
1540         case elfcpp::R_SPARC_L44:
1541         case elfcpp::R_SPARC_HH22:
1542         case elfcpp::R_SPARC_HM10:
1543         case elfcpp::R_SPARC_LM22:
1544         case elfcpp::R_SPARC_UA16:
1545         case elfcpp::R_SPARC_UA32:
1546         case elfcpp::R_SPARC_UA64:
1547           return;
1548
1549         default:
1550           break;
1551         }
1552     }
1553   else
1554     {
1555       switch (r_type)
1556         {
1557           // These are the relocation types supported by glibc for sparc 32-bit.
1558         case elfcpp::R_SPARC_RELATIVE:
1559         case elfcpp::R_SPARC_COPY:
1560         case elfcpp::R_SPARC_GLOB_DAT:
1561         case elfcpp::R_SPARC_32:
1562         case elfcpp::R_SPARC_JMP_SLOT:
1563         case elfcpp::R_SPARC_TLS_DTPMOD32:
1564         case elfcpp::R_SPARC_TLS_DTPOFF32:
1565         case elfcpp::R_SPARC_TLS_TPOFF32:
1566         case elfcpp::R_SPARC_TLS_LE_HIX22:
1567         case elfcpp::R_SPARC_TLS_LE_LOX10:
1568         case elfcpp::R_SPARC_8:
1569         case elfcpp::R_SPARC_16:
1570         case elfcpp::R_SPARC_DISP8:
1571         case elfcpp::R_SPARC_DISP16:
1572         case elfcpp::R_SPARC_DISP32:
1573         case elfcpp::R_SPARC_LO10:
1574         case elfcpp::R_SPARC_WDISP30:
1575         case elfcpp::R_SPARC_HI22:
1576         case elfcpp::R_SPARC_UA16:
1577         case elfcpp::R_SPARC_UA32:
1578           return;
1579
1580         default:
1581           break;
1582         }
1583     }
1584
1585   // This prevents us from issuing more than one error per reloc
1586   // section.  But we can still wind up issuing more than one
1587   // error per object file.
1588   if (this->issued_non_pic_error_)
1589     return;
1590   gold_assert(parameters->options().output_is_position_independent());
1591   object->error(_("requires unsupported dynamic reloc; "
1592                   "recompile with -fPIC"));
1593   this->issued_non_pic_error_ = true;
1594   return;
1595 }
1596
1597 // Scan a relocation for a local symbol.
1598
1599 template<int size, bool big_endian>
1600 inline void
1601 Target_sparc<size, big_endian>::Scan::local(
1602                         const General_options&,
1603                         Symbol_table* symtab,
1604                         Layout* layout,
1605                         Target_sparc<size, big_endian>* target,
1606                         Sized_relobj<size, big_endian>* object,
1607                         unsigned int data_shndx,
1608                         Output_section* output_section,
1609                         const elfcpp::Rela<size, big_endian>& reloc,
1610                         unsigned int r_type,
1611                         const elfcpp::Sym<size, big_endian>& lsym)
1612 {
1613   unsigned int orig_r_type = r_type;
1614
1615   r_type &= 0xff;
1616   switch (r_type)
1617     {
1618     case elfcpp::R_SPARC_NONE:
1619     case elfcpp::R_SPARC_REGISTER:
1620     case elfcpp::R_SPARC_GNU_VTINHERIT:
1621     case elfcpp::R_SPARC_GNU_VTENTRY:
1622       break;
1623
1624     case elfcpp::R_SPARC_64:
1625     case elfcpp::R_SPARC_32:
1626       // If building a shared library (or a position-independent
1627       // executable), we need to create a dynamic relocation for
1628       // this location. The relocation applied at link time will
1629       // apply the link-time value, so we flag the location with
1630       // an R_SPARC_RELATIVE relocation so the dynamic loader can
1631       // relocate it easily.
1632       if (parameters->options().output_is_position_independent())
1633         {
1634           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1635           unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1636           rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
1637                                        output_section, data_shndx,
1638                                        reloc.get_r_offset(),
1639                                        reloc.get_r_addend());
1640         }
1641       break;
1642
1643     case elfcpp::R_SPARC_HIX22:
1644     case elfcpp::R_SPARC_LOX10:
1645     case elfcpp::R_SPARC_H44:
1646     case elfcpp::R_SPARC_M44:
1647     case elfcpp::R_SPARC_L44:
1648     case elfcpp::R_SPARC_HH22:
1649     case elfcpp::R_SPARC_HM10:
1650     case elfcpp::R_SPARC_LM22:
1651     case elfcpp::R_SPARC_UA64:
1652     case elfcpp::R_SPARC_UA32:
1653     case elfcpp::R_SPARC_UA16:
1654     case elfcpp::R_SPARC_HI22:
1655     case elfcpp::R_SPARC_LO10:
1656     case elfcpp::R_SPARC_OLO10:
1657     case elfcpp::R_SPARC_16:
1658     case elfcpp::R_SPARC_11:
1659     case elfcpp::R_SPARC_10:
1660     case elfcpp::R_SPARC_8:
1661     case elfcpp::R_SPARC_7:
1662     case elfcpp::R_SPARC_6:
1663     case elfcpp::R_SPARC_5:
1664       // If building a shared library (or a position-independent
1665       // executable), we need to create a dynamic relocation for
1666       // this location.
1667       if (parameters->options().output_is_position_independent())
1668         {
1669           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1670
1671           check_non_pic(object, r_type);
1672           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1673             {
1674               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1675               rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
1676                                   data_shndx, reloc.get_r_offset(),
1677                                   reloc.get_r_addend());
1678             }
1679           else
1680             {
1681               unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1682               gold_assert(lsym.get_st_value() == 0);
1683               rela_dyn->add_local_relative(object, r_sym, orig_r_type,
1684                                            output_section, data_shndx,
1685                                            reloc.get_r_offset(),
1686                                            reloc.get_r_addend());
1687             }
1688         }
1689       break;
1690
1691     case elfcpp::R_SPARC_WDISP30:
1692     case elfcpp::R_SPARC_WDISP22:
1693     case elfcpp::R_SPARC_WDISP19:
1694     case elfcpp::R_SPARC_WDISP16:
1695     case elfcpp::R_SPARC_DISP8:
1696     case elfcpp::R_SPARC_DISP16:
1697     case elfcpp::R_SPARC_DISP32:
1698     case elfcpp::R_SPARC_DISP64:
1699     case elfcpp::R_SPARC_PC10:
1700     case elfcpp::R_SPARC_PC22:
1701       break;
1702
1703     case elfcpp::R_SPARC_GOT10:
1704     case elfcpp::R_SPARC_GOT13:
1705     case elfcpp::R_SPARC_GOT22:
1706       {
1707         // The symbol requires a GOT entry.
1708         Output_data_got<size, big_endian>* got;
1709         unsigned int r_sym;
1710
1711         got = target->got_section(symtab, layout);
1712         r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1713
1714         // If we are generating a shared object, we need to add a
1715         // dynamic relocation for this symbol's GOT entry.
1716         if (parameters->options().output_is_position_independent())
1717           {
1718             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1719               {
1720                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1721                 unsigned int off;
1722
1723                 off = got->add_constant(0);
1724                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1725                 rela_dyn->add_local_relative(object, r_sym,
1726                                              elfcpp::R_SPARC_RELATIVE,
1727                                              got, off, 0);
1728               }
1729           }
1730         else
1731           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1732       }
1733       break;
1734
1735       // These are initial TLS relocs, which are expected when
1736       // linking.
1737     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1738     case elfcpp::R_SPARC_TLS_GD_LO10:
1739     case elfcpp::R_SPARC_TLS_GD_ADD:
1740     case elfcpp::R_SPARC_TLS_GD_CALL:
1741     case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
1742     case elfcpp::R_SPARC_TLS_LDM_LO10:
1743     case elfcpp::R_SPARC_TLS_LDM_ADD:
1744     case elfcpp::R_SPARC_TLS_LDM_CALL:
1745     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1746     case elfcpp::R_SPARC_TLS_LDO_LOX10:
1747     case elfcpp::R_SPARC_TLS_LDO_ADD:
1748     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
1749     case elfcpp::R_SPARC_TLS_IE_LO10:
1750     case elfcpp::R_SPARC_TLS_IE_LD:
1751     case elfcpp::R_SPARC_TLS_IE_LDX:
1752     case elfcpp::R_SPARC_TLS_IE_ADD:
1753     case elfcpp::R_SPARC_TLS_LE_HIX22:  // Local-exec
1754     case elfcpp::R_SPARC_TLS_LE_LOX10:
1755       {
1756         bool output_is_shared = parameters->options().shared();
1757         const tls::Tls_optimization optimized_type
1758             = optimize_tls_reloc(!output_is_shared, r_type);
1759         switch (r_type)
1760           {
1761           case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1762           case elfcpp::R_SPARC_TLS_GD_LO10:
1763           case elfcpp::R_SPARC_TLS_GD_ADD:
1764           case elfcpp::R_SPARC_TLS_GD_CALL:
1765             if (optimized_type == tls::TLSOPT_NONE)
1766               {
1767                 // Create a pair of GOT entries for the module index and
1768                 // dtv-relative offset.
1769                 Output_data_got<size, big_endian>* got
1770                     = target->got_section(symtab, layout);
1771                 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1772                 unsigned int shndx = lsym.get_st_shndx();
1773                 bool is_ordinary;
1774                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1775                 if (!is_ordinary)
1776                   object->error(_("local symbol %u has bad shndx %u"),
1777                                 r_sym, shndx);
1778                 else
1779                   got->add_local_pair_with_rela(object, r_sym, 
1780                                                 lsym.get_st_shndx(),
1781                                                 GOT_TYPE_TLS_PAIR,
1782                                                 target->rela_dyn_section(layout),
1783                                                 (size == 64
1784                                                  ? elfcpp::R_SPARC_TLS_DTPMOD64
1785                                                  : elfcpp::R_SPARC_TLS_DTPMOD32),
1786                                                  0);
1787                 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
1788                   generate_tls_call(symtab, layout, target);
1789               }
1790             else if (optimized_type != tls::TLSOPT_TO_LE)
1791               unsupported_reloc_local(object, r_type);
1792             break;
1793
1794           case elfcpp::R_SPARC_TLS_LDM_HI22 :   // Local-dynamic
1795           case elfcpp::R_SPARC_TLS_LDM_LO10:
1796           case elfcpp::R_SPARC_TLS_LDM_ADD:
1797           case elfcpp::R_SPARC_TLS_LDM_CALL:
1798             if (optimized_type == tls::TLSOPT_NONE)
1799               {
1800                 // Create a GOT entry for the module index.
1801                 target->got_mod_index_entry(symtab, layout, object);
1802
1803                 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
1804                   generate_tls_call(symtab, layout, target);
1805               }
1806             else if (optimized_type != tls::TLSOPT_TO_LE)
1807               unsupported_reloc_local(object, r_type);
1808             break;
1809
1810           case elfcpp::R_SPARC_TLS_LDO_HIX22:   // Alternate local-dynamic
1811           case elfcpp::R_SPARC_TLS_LDO_LOX10:
1812           case elfcpp::R_SPARC_TLS_LDO_ADD:
1813             break;
1814
1815           case elfcpp::R_SPARC_TLS_IE_HI22:     // Initial-exec
1816           case elfcpp::R_SPARC_TLS_IE_LO10:
1817           case elfcpp::R_SPARC_TLS_IE_LD:
1818           case elfcpp::R_SPARC_TLS_IE_LDX:
1819           case elfcpp::R_SPARC_TLS_IE_ADD:
1820             layout->set_has_static_tls();
1821             if (optimized_type == tls::TLSOPT_NONE)
1822               {
1823                 // Create a GOT entry for the tp-relative offset.
1824                 Output_data_got<size, big_endian>* got
1825                   = target->got_section(symtab, layout);
1826                 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1827
1828                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
1829                   {
1830                     Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1831                     unsigned int off = got->add_constant(0);
1832
1833                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET,
1834                                                  off);
1835                     rela_dyn->add_local_relative(object, r_sym,
1836                                                  (size == 64 ?
1837                                                   elfcpp::R_SPARC_TLS_TPOFF64 :
1838                                                   elfcpp::R_SPARC_TLS_TPOFF32),
1839                                                  got, off, 0);
1840                   }
1841               }
1842             else if (optimized_type != tls::TLSOPT_TO_LE)
1843               unsupported_reloc_local(object, r_type);
1844             break;
1845
1846           case elfcpp::R_SPARC_TLS_LE_HIX22:    // Local-exec
1847           case elfcpp::R_SPARC_TLS_LE_LOX10:
1848             layout->set_has_static_tls();
1849             if (output_is_shared)
1850               {
1851                 // We need to create a dynamic relocation.
1852                 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1853                 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1854                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1855                 rela_dyn->add_local_relative(object, r_sym, r_type,
1856                                              output_section, data_shndx,
1857                                              reloc.get_r_offset(), 0);
1858               }
1859             break;
1860           }
1861       }
1862       break;
1863
1864       // These are relocations which should only be seen by the
1865       // dynamic linker, and should never be seen here.
1866     case elfcpp::R_SPARC_COPY:
1867     case elfcpp::R_SPARC_GLOB_DAT:
1868     case elfcpp::R_SPARC_JMP_SLOT:
1869     case elfcpp::R_SPARC_RELATIVE:
1870     case elfcpp::R_SPARC_TLS_DTPMOD64:
1871     case elfcpp::R_SPARC_TLS_DTPMOD32:
1872     case elfcpp::R_SPARC_TLS_DTPOFF64:
1873     case elfcpp::R_SPARC_TLS_DTPOFF32:
1874     case elfcpp::R_SPARC_TLS_TPOFF64:
1875     case elfcpp::R_SPARC_TLS_TPOFF32:
1876       gold_error(_("%s: unexpected reloc %u in object file"),
1877                  object->name().c_str(), r_type);
1878       break;
1879
1880     default:
1881       unsupported_reloc_local(object, r_type);
1882       break;
1883     }
1884 }
1885
1886 // Report an unsupported relocation against a global symbol.
1887
1888 template<int size, bool big_endian>
1889 void
1890 Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
1891                         Sized_relobj<size, big_endian>* object,
1892                         unsigned int r_type,
1893                         Symbol* gsym)
1894 {
1895   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1896              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1897 }
1898
1899 // Scan a relocation for a global symbol.
1900
1901 template<int size, bool big_endian>
1902 inline void
1903 Target_sparc<size, big_endian>::Scan::global(
1904                                 const General_options&,
1905                                 Symbol_table* symtab,
1906                                 Layout* layout,
1907                                 Target_sparc<size, big_endian>* target,
1908                                 Sized_relobj<size, big_endian>* object,
1909                                 unsigned int data_shndx,
1910                                 Output_section* output_section,
1911                                 const elfcpp::Rela<size, big_endian>& reloc,
1912                                 unsigned int r_type,
1913                                 Symbol* gsym)
1914 {
1915   unsigned int orig_r_type = r_type;
1916
1917   // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
1918   // section.  We check here to avoid creating a dynamic reloc against
1919   // _GLOBAL_OFFSET_TABLE_.
1920   if (!target->has_got_section()
1921       && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
1922     target->got_section(symtab, layout);
1923
1924   r_type &= 0xff;
1925   switch (r_type)
1926     {
1927     case elfcpp::R_SPARC_NONE:
1928     case elfcpp::R_SPARC_REGISTER:
1929     case elfcpp::R_SPARC_GNU_VTINHERIT:
1930     case elfcpp::R_SPARC_GNU_VTENTRY:
1931       break;
1932
1933     case elfcpp::R_SPARC_PLT64:
1934     case elfcpp::R_SPARC_PLT32:
1935     case elfcpp::R_SPARC_HIPLT22:
1936     case elfcpp::R_SPARC_LOPLT10:
1937     case elfcpp::R_SPARC_PCPLT32:
1938     case elfcpp::R_SPARC_PCPLT22:
1939     case elfcpp::R_SPARC_PCPLT10:
1940     case elfcpp::R_SPARC_WPLT30:
1941       // If the symbol is fully resolved, this is just a PC32 reloc.
1942       // Otherwise we need a PLT entry.
1943       if (gsym->final_value_is_known())
1944         break;
1945       // If building a shared library, we can also skip the PLT entry
1946       // if the symbol is defined in the output file and is protected
1947       // or hidden.
1948       if (gsym->is_defined()
1949           && !gsym->is_from_dynobj()
1950           && !gsym->is_preemptible())
1951         break;
1952       target->make_plt_entry(symtab, layout, gsym);
1953       break;
1954
1955     case elfcpp::R_SPARC_DISP8:
1956     case elfcpp::R_SPARC_DISP16:
1957     case elfcpp::R_SPARC_DISP32:
1958     case elfcpp::R_SPARC_DISP64:
1959     case elfcpp::R_SPARC_PC_HH22:
1960     case elfcpp::R_SPARC_PC_HM10:
1961     case elfcpp::R_SPARC_PC_LM22:
1962     case elfcpp::R_SPARC_PC10:
1963     case elfcpp::R_SPARC_PC22:
1964     case elfcpp::R_SPARC_WDISP30:
1965     case elfcpp::R_SPARC_WDISP22:
1966     case elfcpp::R_SPARC_WDISP19:
1967     case elfcpp::R_SPARC_WDISP16:
1968       {
1969         if (gsym->needs_plt_entry())
1970           target->make_plt_entry(symtab, layout, gsym);
1971         // Make a dynamic relocation if necessary.
1972         int flags = Symbol::NON_PIC_REF;
1973         if (gsym->type() == elfcpp::STT_FUNC)
1974           flags |= Symbol::FUNCTION_CALL;
1975         if (gsym->needs_dynamic_reloc(flags))
1976           {
1977             if (gsym->may_need_copy_reloc())
1978               {
1979                 target->copy_reloc(symtab, layout, object,
1980                                    data_shndx, output_section, gsym,
1981                                    reloc);
1982               }
1983             else
1984               {
1985                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1986                 check_non_pic(object, r_type);
1987                 rela_dyn->add_global(gsym, orig_r_type, output_section, object,
1988                                      data_shndx, reloc.get_r_offset(),
1989                                      reloc.get_r_addend());
1990               }
1991           }
1992       }
1993       break;
1994
1995     case elfcpp::R_SPARC_UA64:
1996     case elfcpp::R_SPARC_64:
1997     case elfcpp::R_SPARC_HIX22:
1998     case elfcpp::R_SPARC_LOX10:
1999     case elfcpp::R_SPARC_H44:
2000     case elfcpp::R_SPARC_M44:
2001     case elfcpp::R_SPARC_L44:
2002     case elfcpp::R_SPARC_HH22:
2003     case elfcpp::R_SPARC_HM10:
2004     case elfcpp::R_SPARC_LM22:
2005     case elfcpp::R_SPARC_HI22:
2006     case elfcpp::R_SPARC_LO10:
2007     case elfcpp::R_SPARC_OLO10:
2008     case elfcpp::R_SPARC_UA32:
2009     case elfcpp::R_SPARC_32:
2010     case elfcpp::R_SPARC_UA16:
2011     case elfcpp::R_SPARC_16:
2012     case elfcpp::R_SPARC_11:
2013     case elfcpp::R_SPARC_10:
2014     case elfcpp::R_SPARC_8:
2015     case elfcpp::R_SPARC_7:
2016     case elfcpp::R_SPARC_6:
2017     case elfcpp::R_SPARC_5:
2018       {
2019         // Make a PLT entry if necessary.
2020         if (gsym->needs_plt_entry())
2021           {
2022             target->make_plt_entry(symtab, layout, gsym);
2023             // Since this is not a PC-relative relocation, we may be
2024             // taking the address of a function. In that case we need to
2025             // set the entry in the dynamic symbol table to the address of
2026             // the PLT entry.
2027             if (gsym->is_from_dynobj() && !parameters->options().shared())
2028               gsym->set_needs_dynsym_value();
2029           }
2030         // Make a dynamic relocation if necessary.
2031         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
2032           {
2033             if (gsym->may_need_copy_reloc())
2034               {
2035                 target->copy_reloc(symtab, layout, object,
2036                                    data_shndx, output_section, gsym, reloc);
2037               }
2038             else if ((r_type == elfcpp::R_SPARC_32
2039                       || r_type == elfcpp::R_SPARC_64)
2040                      && gsym->can_use_relative_reloc(false))
2041               {
2042                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2043                 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2044                                               output_section, object,
2045                                               data_shndx, reloc.get_r_offset(),
2046                                               reloc.get_r_addend());
2047               }
2048             else
2049               {
2050                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2051
2052                 check_non_pic(object, r_type);
2053                 if (gsym->is_from_dynobj()
2054                     || gsym->is_undefined()
2055                     || gsym->is_preemptible())
2056                   rela_dyn->add_global(gsym, orig_r_type, output_section,
2057                                        object, data_shndx,
2058                                        reloc.get_r_offset(),
2059                                        reloc.get_r_addend());
2060                 else
2061                   rela_dyn->add_global_relative(gsym, orig_r_type,
2062                                                 output_section, object,
2063                                                 data_shndx,
2064                                                 reloc.get_r_offset(),
2065                                                 reloc.get_r_addend());
2066               }
2067           }
2068       }
2069       break;
2070
2071     case elfcpp::R_SPARC_GOT10:
2072     case elfcpp::R_SPARC_GOT13:
2073     case elfcpp::R_SPARC_GOT22:
2074       {
2075         // The symbol requires a GOT entry.
2076         Output_data_got<size, big_endian>* got;
2077
2078         got = target->got_section(symtab, layout);
2079         if (gsym->final_value_is_known())
2080           got->add_global(gsym, GOT_TYPE_STANDARD);
2081         else
2082           {
2083             // If this symbol is not fully resolved, we need to add a
2084             // dynamic relocation for it.
2085             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2086             if (gsym->is_from_dynobj()
2087                 || gsym->is_undefined()
2088                 || gsym->is_preemptible())
2089               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2090                                         elfcpp::R_SPARC_GLOB_DAT);
2091             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2092               {
2093                 unsigned int off = got->add_constant(0);
2094
2095                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2096                 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2097                                               got, off, 0);
2098               }
2099           }
2100       }
2101       break;
2102
2103       // These are initial tls relocs, which are expected when
2104       // linking.
2105     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2106     case elfcpp::R_SPARC_TLS_GD_LO10:
2107     case elfcpp::R_SPARC_TLS_GD_ADD:
2108     case elfcpp::R_SPARC_TLS_GD_CALL:
2109     case elfcpp::R_SPARC_TLS_LDM_HI22:  // Local-dynamic
2110     case elfcpp::R_SPARC_TLS_LDM_LO10:
2111     case elfcpp::R_SPARC_TLS_LDM_ADD:
2112     case elfcpp::R_SPARC_TLS_LDM_CALL:
2113     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2114     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2115     case elfcpp::R_SPARC_TLS_LDO_ADD:
2116     case elfcpp::R_SPARC_TLS_LE_HIX22:
2117     case elfcpp::R_SPARC_TLS_LE_LOX10:
2118     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
2119     case elfcpp::R_SPARC_TLS_IE_LO10:
2120     case elfcpp::R_SPARC_TLS_IE_LD:
2121     case elfcpp::R_SPARC_TLS_IE_LDX:
2122     case elfcpp::R_SPARC_TLS_IE_ADD:
2123       {
2124         const bool is_final = gsym->final_value_is_known();
2125         const tls::Tls_optimization optimized_type
2126             = optimize_tls_reloc(is_final, r_type);
2127         switch (r_type)
2128           {
2129           case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2130           case elfcpp::R_SPARC_TLS_GD_LO10:
2131           case elfcpp::R_SPARC_TLS_GD_ADD:
2132           case elfcpp::R_SPARC_TLS_GD_CALL:
2133             if (optimized_type == tls::TLSOPT_NONE)
2134               {
2135                 // Create a pair of GOT entries for the module index and
2136                 // dtv-relative offset.
2137                 Output_data_got<size, big_endian>* got
2138                     = target->got_section(symtab, layout);
2139                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2140                                                target->rela_dyn_section(layout),
2141                                                (size == 64 ?
2142                                                 elfcpp::R_SPARC_TLS_DTPMOD64 :
2143                                                 elfcpp::R_SPARC_TLS_DTPMOD32),
2144                                                (size == 64 ?
2145                                                 elfcpp::R_SPARC_TLS_DTPOFF64 :
2146                                                 elfcpp::R_SPARC_TLS_DTPOFF32));
2147
2148                 // Emit R_SPARC_WPLT30 against "__tls_get_addr"
2149                 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2150                   generate_tls_call(symtab, layout, target);
2151               }
2152             else if (optimized_type == tls::TLSOPT_TO_IE)
2153               {
2154                 // Create a GOT entry for the tp-relative offset.
2155                 Output_data_got<size, big_endian>* got
2156                     = target->got_section(symtab, layout);
2157                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2158                                           target->rela_dyn_section(layout),
2159                                           (size == 64 ?
2160                                            elfcpp::R_SPARC_TLS_TPOFF64 :
2161                                            elfcpp::R_SPARC_TLS_TPOFF32));
2162               }
2163             else if (optimized_type != tls::TLSOPT_TO_LE)
2164               unsupported_reloc_global(object, r_type, gsym);
2165             break;
2166
2167           case elfcpp::R_SPARC_TLS_LDM_HI22:    // Local-dynamic
2168           case elfcpp::R_SPARC_TLS_LDM_LO10:
2169           case elfcpp::R_SPARC_TLS_LDM_ADD:
2170           case elfcpp::R_SPARC_TLS_LDM_CALL:
2171             if (optimized_type == tls::TLSOPT_NONE)
2172               {
2173                 // Create a GOT entry for the module index.
2174                 target->got_mod_index_entry(symtab, layout, object);
2175
2176                 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2177                   generate_tls_call(symtab, layout, target);
2178               }
2179             else if (optimized_type != tls::TLSOPT_TO_LE)
2180               unsupported_reloc_global(object, r_type, gsym);
2181             break;
2182
2183           case elfcpp::R_SPARC_TLS_LDO_HIX22:   // Alternate local-dynamic
2184           case elfcpp::R_SPARC_TLS_LDO_LOX10:
2185           case elfcpp::R_SPARC_TLS_LDO_ADD:
2186             break;
2187
2188           case elfcpp::R_SPARC_TLS_LE_HIX22:
2189           case elfcpp::R_SPARC_TLS_LE_LOX10:
2190             layout->set_has_static_tls();
2191             if (parameters->options().shared())
2192               {
2193                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2194                 rela_dyn->add_global_relative(gsym, orig_r_type,
2195                                               output_section, object,
2196                                               data_shndx, reloc.get_r_offset(),
2197                                               0);
2198               }
2199             break;
2200
2201           case elfcpp::R_SPARC_TLS_IE_HI22:     // Initial-exec
2202           case elfcpp::R_SPARC_TLS_IE_LO10:
2203           case elfcpp::R_SPARC_TLS_IE_LD:
2204           case elfcpp::R_SPARC_TLS_IE_LDX:
2205           case elfcpp::R_SPARC_TLS_IE_ADD:
2206             layout->set_has_static_tls();
2207             if (optimized_type == tls::TLSOPT_NONE)
2208               {
2209                 // Create a GOT entry for the tp-relative offset.
2210                 Output_data_got<size, big_endian>* got
2211                   = target->got_section(symtab, layout);
2212                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2213                                           target->rela_dyn_section(layout),
2214                                           (size == 64 ?
2215                                            elfcpp::R_SPARC_TLS_TPOFF64 :
2216                                            elfcpp::R_SPARC_TLS_TPOFF32));
2217               }
2218             else if (optimized_type != tls::TLSOPT_TO_LE)
2219               unsupported_reloc_global(object, r_type, gsym);
2220             break;
2221           }
2222       }
2223       break;
2224
2225       // These are relocations which should only be seen by the
2226       // dynamic linker, and should never be seen here.
2227     case elfcpp::R_SPARC_COPY:
2228     case elfcpp::R_SPARC_GLOB_DAT:
2229     case elfcpp::R_SPARC_JMP_SLOT:
2230     case elfcpp::R_SPARC_RELATIVE:
2231     case elfcpp::R_SPARC_TLS_DTPMOD64:
2232     case elfcpp::R_SPARC_TLS_DTPMOD32:
2233     case elfcpp::R_SPARC_TLS_DTPOFF64:
2234     case elfcpp::R_SPARC_TLS_DTPOFF32:
2235     case elfcpp::R_SPARC_TLS_TPOFF64:
2236     case elfcpp::R_SPARC_TLS_TPOFF32:
2237       gold_error(_("%s: unexpected reloc %u in object file"),
2238                  object->name().c_str(), r_type);
2239       break;
2240
2241     default:
2242       unsupported_reloc_global(object, r_type, gsym);
2243       break;
2244     }
2245 }
2246
2247 // Process relocations for gc.
2248
2249 template<int size, bool big_endian>
2250 void
2251 Target_sparc<size, big_endian>::gc_process_relocs(
2252                         const General_options& options,
2253                         Symbol_table* symtab,
2254                         Layout* layout,
2255                         Sized_relobj<size, big_endian>* object,
2256                         unsigned int data_shndx,
2257                         unsigned int,
2258                         const unsigned char* prelocs,
2259                         size_t reloc_count,
2260                         Output_section* output_section,
2261                         bool needs_special_offset_handling,
2262                         size_t local_symbol_count,
2263                         const unsigned char* plocal_symbols)
2264 {
2265   typedef Target_sparc<size, big_endian> Sparc;
2266   typedef typename Target_sparc<size, big_endian>::Scan Scan;
2267
2268   gold::gc_process_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan>(
2269     options,
2270     symtab,
2271     layout,
2272     this,
2273     object,
2274     data_shndx,
2275     prelocs,
2276     reloc_count,
2277     output_section,
2278     needs_special_offset_handling,
2279     local_symbol_count,
2280     plocal_symbols);
2281 }
2282
2283 // Scan relocations for a section.
2284
2285 template<int size, bool big_endian>
2286 void
2287 Target_sparc<size, big_endian>::scan_relocs(
2288                         const General_options& options,
2289                         Symbol_table* symtab,
2290                         Layout* layout,
2291                         Sized_relobj<size, big_endian>* object,
2292                         unsigned int data_shndx,
2293                         unsigned int sh_type,
2294                         const unsigned char* prelocs,
2295                         size_t reloc_count,
2296                         Output_section* output_section,
2297                         bool needs_special_offset_handling,
2298                         size_t local_symbol_count,
2299                         const unsigned char* plocal_symbols)
2300 {
2301   typedef Target_sparc<size, big_endian> Sparc;
2302   typedef typename Target_sparc<size, big_endian>::Scan Scan;
2303
2304   if (sh_type == elfcpp::SHT_REL)
2305     {
2306       gold_error(_("%s: unsupported REL reloc section"),
2307                  object->name().c_str());
2308       return;
2309     }
2310
2311   gold::scan_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan>(
2312     options,
2313     symtab,
2314     layout,
2315     this,
2316     object,
2317     data_shndx,
2318     prelocs,
2319     reloc_count,
2320     output_section,
2321     needs_special_offset_handling,
2322     local_symbol_count,
2323     plocal_symbols);
2324 }
2325
2326 // Finalize the sections.
2327
2328 template<int size, bool big_endian>
2329 void
2330 Target_sparc<size, big_endian>::do_finalize_sections(Layout* layout)
2331 {
2332   // Fill in some more dynamic tags.
2333   Output_data_dynamic* const odyn = layout->dynamic_data();
2334   if (odyn != NULL)
2335     {
2336       if (this->plt_ != NULL)
2337         {
2338           const Output_data* od = this->plt_->rel_plt();
2339           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
2340           odyn->add_section_address(elfcpp::DT_JMPREL, od);
2341           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
2342
2343           odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
2344         }
2345
2346       if (this->rela_dyn_ != NULL)
2347         {
2348           const Output_data* od = this->rela_dyn_;
2349           odyn->add_section_address(elfcpp::DT_RELA, od);
2350           odyn->add_section_size(elfcpp::DT_RELASZ, od);
2351           odyn->add_constant(elfcpp::DT_RELAENT,
2352                              elfcpp::Elf_sizes<size>::rela_size);
2353         }
2354
2355       if (!parameters->options().shared())
2356         {
2357           // The value of the DT_DEBUG tag is filled in by the dynamic
2358           // linker at run time, and used by the debugger.
2359           odyn->add_constant(elfcpp::DT_DEBUG, 0);
2360         }
2361     }
2362
2363   // Emit any relocs we saved in an attempt to avoid generating COPY
2364   // relocs.
2365   if (this->copy_relocs_.any_saved_relocs())
2366     this->copy_relocs_.emit(this->rela_dyn_section(layout));
2367 }
2368
2369 // Perform a relocation.
2370
2371 template<int size, bool big_endian>
2372 inline bool
2373 Target_sparc<size, big_endian>::Relocate::relocate(
2374                         const Relocate_info<size, big_endian>* relinfo,
2375                         Target_sparc* target,
2376                         Output_section*,
2377                         size_t relnum,
2378                         const elfcpp::Rela<size, big_endian>& rela,
2379                         unsigned int r_type,
2380                         const Sized_symbol<size>* gsym,
2381                         const Symbol_value<size>* psymval,
2382                         unsigned char* view,
2383                         typename elfcpp::Elf_types<size>::Elf_Addr address,
2384                         section_size_type view_size)
2385 {
2386   r_type &= 0xff;
2387
2388   if (this->ignore_gd_add_)
2389     {
2390       if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
2391         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2392                                _("missing expected TLS relocation"));
2393       else
2394         {
2395           this->ignore_gd_add_ = false;
2396           return false;
2397         }
2398     }
2399
2400   typedef Sparc_relocate_functions<size, big_endian> Reloc;
2401
2402   // Pick the value to use for symbols defined in shared objects.
2403   Symbol_value<size> symval;
2404   if (gsym != NULL
2405       && gsym->use_plt_offset(r_type == elfcpp::R_SPARC_DISP8
2406                               || r_type == elfcpp::R_SPARC_DISP16
2407                               || r_type == elfcpp::R_SPARC_DISP32
2408                               || r_type == elfcpp::R_SPARC_DISP64
2409                               || r_type == elfcpp::R_SPARC_PC_HH22
2410                               || r_type == elfcpp::R_SPARC_PC_HM10
2411                               || r_type == elfcpp::R_SPARC_PC_LM22
2412                               || r_type == elfcpp::R_SPARC_PC10
2413                               || r_type == elfcpp::R_SPARC_PC22
2414                               || r_type == elfcpp::R_SPARC_WDISP30
2415                               || r_type == elfcpp::R_SPARC_WDISP22
2416                               || r_type == elfcpp::R_SPARC_WDISP19
2417                               || r_type == elfcpp::R_SPARC_WDISP16))
2418     {
2419       elfcpp::Elf_Xword value;
2420
2421       value = target->plt_section()->address() + gsym->plt_offset();
2422
2423       symval.set_output_value(value);
2424
2425       psymval = &symval;
2426     }
2427
2428   const Sized_relobj<size, big_endian>* object = relinfo->object;
2429   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2430
2431   // Get the GOT offset if needed.  Unlike i386 and x86_64, our GOT
2432   // pointer points to the beginning, not the end, of the table.
2433   // So we just use the plain offset.
2434   bool have_got_offset = false;
2435   unsigned int got_offset = 0;
2436   switch (r_type)
2437     {
2438     case elfcpp::R_SPARC_GOT10:
2439     case elfcpp::R_SPARC_GOT13:
2440     case elfcpp::R_SPARC_GOT22:
2441       if (gsym != NULL)
2442         {
2443           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2444           got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
2445         }
2446       else
2447         {
2448           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2449           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2450           got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2451         }
2452       have_got_offset = true;
2453       break;
2454
2455     default:
2456       break;
2457     }
2458
2459   switch (r_type)
2460     {
2461     case elfcpp::R_SPARC_NONE:
2462     case elfcpp::R_SPARC_REGISTER:
2463     case elfcpp::R_SPARC_GNU_VTINHERIT:
2464     case elfcpp::R_SPARC_GNU_VTENTRY:
2465       break;
2466
2467     case elfcpp::R_SPARC_8:
2468       Relocate_functions<size, big_endian>::rela8(view, object,
2469                                                   psymval, addend);
2470       break;
2471
2472     case elfcpp::R_SPARC_16:
2473       Relocate_functions<size, big_endian>::rela16(view, object,
2474                                                    psymval, addend);
2475       break;
2476
2477     case elfcpp::R_SPARC_32:
2478       if (!parameters->options().output_is_position_independent())
2479         Relocate_functions<size, big_endian>::rela32(view, object,
2480                                                      psymval, addend);
2481       break;
2482
2483     case elfcpp::R_SPARC_DISP8:
2484       Reloc::disp8(view, object, psymval, addend, address);
2485       break;
2486
2487     case elfcpp::R_SPARC_DISP16:
2488       Reloc::disp16(view, object, psymval, addend, address);
2489       break;
2490
2491     case elfcpp::R_SPARC_DISP32:
2492       Reloc::disp32(view, object, psymval, addend, address);
2493       break;
2494
2495     case elfcpp::R_SPARC_DISP64:
2496       Reloc::disp64(view, object, psymval, addend, address);
2497       break;
2498
2499     case elfcpp::R_SPARC_WDISP30:
2500     case elfcpp::R_SPARC_WPLT30:
2501       Reloc::wdisp30(view, object, psymval, addend, address);
2502       break;
2503
2504     case elfcpp::R_SPARC_WDISP22:
2505       Reloc::wdisp22(view, object, psymval, addend, address);
2506       break;
2507
2508     case elfcpp::R_SPARC_WDISP19:
2509       Reloc::wdisp19(view, object, psymval, addend, address);
2510       break;
2511
2512     case elfcpp::R_SPARC_WDISP16:
2513       Reloc::wdisp16(view, object, psymval, addend, address);
2514       break;
2515
2516     case elfcpp::R_SPARC_HI22:
2517       Reloc::hi22(view, object, psymval, addend);
2518       break;
2519
2520     case elfcpp::R_SPARC_22:
2521       Reloc::rela32_22(view, object, psymval, addend);
2522       break;
2523
2524     case elfcpp::R_SPARC_13:
2525       Reloc::rela32_13(view, object, psymval, addend);
2526       break;
2527
2528     case elfcpp::R_SPARC_LO10:
2529       Reloc::lo10(view, object, psymval, addend);
2530       break;
2531
2532     case elfcpp::R_SPARC_GOT10:
2533       Reloc::lo10(view, got_offset, addend);
2534       break;
2535
2536     case elfcpp::R_SPARC_GOT13:
2537       Reloc::rela32_13(view, got_offset, addend);
2538       break;
2539
2540     case elfcpp::R_SPARC_GOT22:
2541       Reloc::hi22(view, got_offset, addend);
2542       break;
2543
2544     case elfcpp::R_SPARC_PC10:
2545       Reloc::pc10(view, object, psymval, addend, address);
2546       break;
2547
2548     case elfcpp::R_SPARC_PC22:
2549       Reloc::pc22(view, object, psymval, addend, address);
2550       break;
2551
2552     case elfcpp::R_SPARC_TLS_DTPOFF32:
2553     case elfcpp::R_SPARC_UA32:
2554       Reloc::ua32(view, object, psymval, addend);
2555       break;
2556
2557     case elfcpp::R_SPARC_PLT64:
2558       Relocate_functions<size, big_endian>::rela64(view, object,
2559                                                    psymval, addend);
2560       break;
2561
2562     case elfcpp::R_SPARC_PLT32:
2563       Relocate_functions<size, big_endian>::rela32(view, object,
2564                                                    psymval, addend);
2565       break;
2566
2567     case elfcpp::R_SPARC_HIPLT22:
2568       Reloc::hi22(view, object, psymval, addend);
2569       break;
2570
2571     case elfcpp::R_SPARC_LOPLT10:
2572       Reloc::lo10(view, object, psymval, addend);
2573       break;
2574
2575     case elfcpp::R_SPARC_PCPLT32:
2576       Reloc::disp32(view, object, psymval, addend, address);
2577       break;
2578
2579     case elfcpp::R_SPARC_PCPLT22:
2580       Reloc::pcplt22(view, object, psymval, addend, address);
2581       break;
2582
2583     case elfcpp::R_SPARC_PCPLT10:
2584       Reloc::lo10(view, object, psymval, addend, address);
2585       break;
2586
2587     case elfcpp::R_SPARC_64:
2588       if (!parameters->options().output_is_position_independent())
2589               Relocate_functions<size, big_endian>::rela64(view, object,
2590                                                            psymval, addend);
2591       break;
2592
2593     case elfcpp::R_SPARC_OLO10:
2594       {
2595         unsigned int addend2 = rela.get_r_info() & 0xffffffff;
2596         addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
2597         Reloc::olo10(view, object, psymval, addend, addend2);
2598       }
2599       break;
2600
2601     case elfcpp::R_SPARC_HH22:
2602       Reloc::hh22(view, object, psymval, addend);
2603       break;
2604
2605     case elfcpp::R_SPARC_PC_HH22:
2606       Reloc::pc_hh22(view, object, psymval, addend, address);
2607       break;
2608
2609     case elfcpp::R_SPARC_HM10:
2610       Reloc::hm10(view, object, psymval, addend);
2611       break;
2612
2613     case elfcpp::R_SPARC_PC_HM10:
2614       Reloc::pc_hm10(view, object, psymval, addend, address);
2615       break;
2616
2617     case elfcpp::R_SPARC_LM22:
2618       Reloc::hi22(view, object, psymval, addend);
2619       break;
2620
2621     case elfcpp::R_SPARC_PC_LM22:
2622       Reloc::pcplt22(view, object, psymval, addend, address);
2623       break;
2624
2625     case elfcpp::R_SPARC_11:
2626       Reloc::rela32_11(view, object, psymval, addend);
2627       break;
2628
2629     case elfcpp::R_SPARC_10:
2630       Reloc::rela32_10(view, object, psymval, addend);
2631       break;
2632
2633     case elfcpp::R_SPARC_7:
2634       Reloc::rela32_7(view, object, psymval, addend);
2635       break;
2636
2637     case elfcpp::R_SPARC_6:
2638       Reloc::rela32_6(view, object, psymval, addend);
2639       break;
2640
2641     case elfcpp::R_SPARC_5:
2642       Reloc::rela32_5(view, object, psymval, addend);
2643       break;
2644
2645     case elfcpp::R_SPARC_HIX22:
2646       Reloc::hix22(view, object, psymval, addend);
2647       break;
2648
2649     case elfcpp::R_SPARC_LOX10:
2650       Reloc::lox10(view, object, psymval, addend);
2651       break;
2652
2653     case elfcpp::R_SPARC_H44:
2654       Reloc::h44(view, object, psymval, addend);
2655       break;
2656
2657     case elfcpp::R_SPARC_M44:
2658       Reloc::m44(view, object, psymval, addend);
2659       break;
2660
2661     case elfcpp::R_SPARC_L44:
2662       Reloc::l44(view, object, psymval, addend);
2663       break;
2664
2665     case elfcpp::R_SPARC_TLS_DTPOFF64:
2666     case elfcpp::R_SPARC_UA64:
2667       Reloc::ua64(view, object, psymval, addend);
2668       break;
2669
2670     case elfcpp::R_SPARC_UA16:
2671       Reloc::ua16(view, object, psymval, addend);
2672       break;
2673
2674     case elfcpp::R_SPARC_TLS_GD_HI22:
2675     case elfcpp::R_SPARC_TLS_GD_LO10:
2676     case elfcpp::R_SPARC_TLS_GD_ADD:
2677     case elfcpp::R_SPARC_TLS_GD_CALL:
2678     case elfcpp::R_SPARC_TLS_LDM_HI22:
2679     case elfcpp::R_SPARC_TLS_LDM_LO10:
2680     case elfcpp::R_SPARC_TLS_LDM_ADD:
2681     case elfcpp::R_SPARC_TLS_LDM_CALL:
2682     case elfcpp::R_SPARC_TLS_LDO_HIX22:
2683     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2684     case elfcpp::R_SPARC_TLS_LDO_ADD:
2685     case elfcpp::R_SPARC_TLS_IE_HI22:
2686     case elfcpp::R_SPARC_TLS_IE_LO10:
2687     case elfcpp::R_SPARC_TLS_IE_LD:
2688     case elfcpp::R_SPARC_TLS_IE_LDX:
2689     case elfcpp::R_SPARC_TLS_IE_ADD:
2690     case elfcpp::R_SPARC_TLS_LE_HIX22:
2691     case elfcpp::R_SPARC_TLS_LE_LOX10:
2692       this->relocate_tls(relinfo, target, relnum, rela,
2693                          r_type, gsym, psymval, view,
2694                          address, view_size);
2695       break;
2696
2697     case elfcpp::R_SPARC_COPY:
2698     case elfcpp::R_SPARC_GLOB_DAT:
2699     case elfcpp::R_SPARC_JMP_SLOT:
2700     case elfcpp::R_SPARC_RELATIVE:
2701       // These are outstanding tls relocs, which are unexpected when
2702       // linking.
2703     case elfcpp::R_SPARC_TLS_DTPMOD64:
2704     case elfcpp::R_SPARC_TLS_DTPMOD32:
2705     case elfcpp::R_SPARC_TLS_TPOFF64:
2706     case elfcpp::R_SPARC_TLS_TPOFF32:
2707       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2708                              _("unexpected reloc %u in object file"),
2709                              r_type);
2710       break;
2711
2712     default:
2713       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2714                              _("unsupported reloc %u"),
2715                              r_type);
2716       break;
2717     }
2718
2719   return true;
2720 }
2721
2722 // Perform a TLS relocation.
2723
2724 template<int size, bool big_endian>
2725 inline void
2726 Target_sparc<size, big_endian>::Relocate::relocate_tls(
2727                         const Relocate_info<size, big_endian>* relinfo,
2728                         Target_sparc<size, big_endian>* target,
2729                         size_t relnum,
2730                         const elfcpp::Rela<size, big_endian>& rela,
2731                         unsigned int r_type,
2732                         const Sized_symbol<size>* gsym,
2733                         const Symbol_value<size>* psymval,
2734                         unsigned char* view,
2735                         typename elfcpp::Elf_types<size>::Elf_Addr address,
2736                         section_size_type)
2737 {
2738   Output_segment* tls_segment = relinfo->layout->tls_segment();
2739   typedef Sparc_relocate_functions<size, big_endian> Reloc;
2740   const Sized_relobj<size, big_endian>* object = relinfo->object;
2741   typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
2742
2743   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2744   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2745
2746   const bool is_final =
2747     (gsym == NULL
2748      ? !parameters->options().output_is_position_independent()
2749      : gsym->final_value_is_known());
2750   const tls::Tls_optimization optimized_type
2751       = optimize_tls_reloc(is_final, r_type);
2752
2753   switch (r_type)
2754     {
2755     case elfcpp::R_SPARC_TLS_GD_HI22:
2756     case elfcpp::R_SPARC_TLS_GD_LO10:
2757     case elfcpp::R_SPARC_TLS_GD_ADD:
2758     case elfcpp::R_SPARC_TLS_GD_CALL:
2759       if (optimized_type == tls::TLSOPT_TO_LE)
2760         {
2761           Insntype* wv = reinterpret_cast<Insntype*>(view);
2762           Insntype val;
2763
2764           value -= tls_segment->memsz();
2765
2766           switch (r_type)
2767             {
2768             case elfcpp::R_SPARC_TLS_GD_HI22:
2769               // TLS_GD_HI22 --> TLS_LE_HIX22
2770               Reloc::hix22(view, value, addend);
2771               break;
2772
2773             case elfcpp::R_SPARC_TLS_GD_LO10:
2774               // TLS_GD_LO10 --> TLS_LE_LOX10
2775               Reloc::lox10(view, value, addend);
2776               break;
2777
2778             case elfcpp::R_SPARC_TLS_GD_ADD:
2779               // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
2780               val = elfcpp::Swap<32, true>::readval(wv);
2781               val = (val & ~0x7c000) | 0x1c000;
2782               elfcpp::Swap<32, true>::writeval(wv, val);
2783               break;
2784             case elfcpp::R_SPARC_TLS_GD_CALL:
2785               // call __tls_get_addr --> nop
2786               elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
2787               break;
2788             }
2789           break;
2790         }
2791       else
2792         {
2793           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2794                                    ? GOT_TYPE_TLS_OFFSET
2795                                    : GOT_TYPE_TLS_PAIR);
2796           if (gsym != NULL)
2797             {
2798               gold_assert(gsym->has_got_offset(got_type));
2799               value = gsym->got_offset(got_type);
2800             }
2801           else
2802             {
2803               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2804               gold_assert(object->local_has_got_offset(r_sym, got_type));
2805               value = object->local_got_offset(r_sym, got_type);
2806             }
2807           if (optimized_type == tls::TLSOPT_TO_IE)
2808             {
2809               Insntype* wv = reinterpret_cast<Insntype*>(view);
2810               Insntype val;
2811
2812               switch (r_type)
2813                 {
2814                 case elfcpp::R_SPARC_TLS_GD_HI22:
2815                   // TLS_GD_HI22 --> TLS_IE_HI22
2816                   Reloc::hi22(view, value, addend);
2817                   break;
2818
2819                 case elfcpp::R_SPARC_TLS_GD_LO10:
2820                   // TLS_GD_LO10 --> TLS_IE_LO10
2821                   Reloc::lo10(view, value, addend);
2822                   break;
2823
2824                 case elfcpp::R_SPARC_TLS_GD_ADD:
2825                   // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
2826                   val = elfcpp::Swap<32, true>::readval(wv);
2827
2828                   if (size == 64)
2829                     val |= 0xc0580000;
2830                   else
2831                     val |= 0xc0000000;
2832
2833                   elfcpp::Swap<32, true>::writeval(wv, val);
2834                   break;
2835
2836                 case elfcpp::R_SPARC_TLS_GD_CALL:
2837                   // The compiler can put the TLS_GD_ADD instruction
2838                   // into the delay slot of the call.  If so, we need
2839                   // to transpose the two instructions so that the
2840                   // the new sequence works properly.
2841                   //
2842                   // The test we use is if the instruction in the
2843                   // delay slot is an add with destination register
2844                   // equal to %o0
2845                   val = elfcpp::Swap<32, true>::readval(wv + 1);
2846                   if ((val & 0x81f80000) == 0x80000000
2847                       && ((val >> 25) & 0x1f) == 0x8)
2848                     {
2849                       if (size == 64)
2850                         val |= 0xc0580000;
2851                       else
2852                         val |= 0xc0000000;
2853
2854                       elfcpp::Swap<32, true>::writeval(wv, val);
2855
2856                       wv += 1;
2857                       this->ignore_gd_add_ = true;
2858                     }
2859
2860                   // call __tls_get_addr --> add %g7, %o0, %o0
2861                   elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
2862                   break;
2863                 }
2864               break;
2865             }
2866           else if (optimized_type == tls::TLSOPT_NONE)
2867             {
2868               switch (r_type)
2869                 {
2870                 case elfcpp::R_SPARC_TLS_GD_HI22:
2871                   Reloc::hi22(view, value, addend);
2872                   break;
2873                 case elfcpp::R_SPARC_TLS_GD_LO10:
2874                   Reloc::lo10(view, value, addend);
2875                   break;
2876                 case elfcpp::R_SPARC_TLS_GD_ADD:
2877                   break;
2878                 case elfcpp::R_SPARC_TLS_GD_CALL:
2879                   {
2880                     Symbol_value<size> symval;
2881                     elfcpp::Elf_Xword value;
2882                     Symbol* tsym;
2883
2884                     tsym = target->tls_get_addr_sym_;
2885                     gold_assert(tsym);
2886                     value = (target->plt_section()->address() +
2887                              tsym->plt_offset());
2888                     symval.set_output_value(value);
2889                     Reloc::wdisp30(view, object, &symval, addend, address);
2890                   }
2891                   break;
2892                 }
2893               break;
2894             }
2895         }
2896       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2897                              _("unsupported reloc %u"),
2898                              r_type);
2899       break;
2900
2901     case elfcpp::R_SPARC_TLS_LDM_HI22:
2902     case elfcpp::R_SPARC_TLS_LDM_LO10:
2903     case elfcpp::R_SPARC_TLS_LDM_ADD:
2904     case elfcpp::R_SPARC_TLS_LDM_CALL:
2905       if (optimized_type == tls::TLSOPT_TO_LE)
2906         {
2907           Insntype* wv = reinterpret_cast<Insntype*>(view);
2908
2909           switch (r_type)
2910             {
2911             case elfcpp::R_SPARC_TLS_LDM_HI22:
2912             case elfcpp::R_SPARC_TLS_LDM_LO10:
2913             case elfcpp::R_SPARC_TLS_LDM_ADD:
2914               elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
2915               break;
2916
2917             case elfcpp::R_SPARC_TLS_LDM_CALL:
2918               elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
2919               break;
2920             }
2921           break;
2922         }
2923       else if (optimized_type == tls::TLSOPT_NONE)
2924         {
2925           // Relocate the field with the offset of the GOT entry for
2926           // the module index.
2927           unsigned int got_offset;
2928
2929           got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
2930           switch (r_type)
2931             {
2932             case elfcpp::R_SPARC_TLS_LDM_HI22:
2933               Reloc::hi22(view, got_offset, addend);
2934               break;
2935             case elfcpp::R_SPARC_TLS_LDM_LO10:
2936               Reloc::lo10(view, got_offset, addend);
2937               break;
2938             case elfcpp::R_SPARC_TLS_LDM_ADD:
2939               break;
2940             case elfcpp::R_SPARC_TLS_LDM_CALL:
2941               {
2942                 Symbol_value<size> symval;
2943                 elfcpp::Elf_Xword value;
2944                 Symbol* tsym;
2945
2946                 tsym = target->tls_get_addr_sym_;
2947                 gold_assert(tsym);
2948                 value = (target->plt_section()->address() +
2949                          tsym->plt_offset());
2950                 symval.set_output_value(value);
2951                 Reloc::wdisp30(view, object, &symval, addend, address);
2952               }
2953               break;
2954             }
2955           break;
2956         }
2957       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2958                              _("unsupported reloc %u"),
2959                              r_type);
2960       break;
2961
2962       // These relocs can appear in debugging sections, in which case
2963       // we won't see the TLS_LDM relocs.  The local_dynamic_type
2964       // field tells us this.
2965     case elfcpp::R_SPARC_TLS_LDO_HIX22:
2966       if (optimized_type == tls::TLSOPT_TO_LE)
2967         {
2968           value -= tls_segment->memsz();
2969           Reloc::hix22(view, value, addend);
2970         }
2971       else
2972         Reloc::ldo_hix22(view, value, addend);
2973       break;
2974     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2975       if (optimized_type == tls::TLSOPT_TO_LE)
2976         {
2977           value -= tls_segment->memsz();
2978           Reloc::lox10(view, value, addend);
2979         }
2980       else
2981         Reloc::ldo_lox10(view, value, addend);
2982       break;
2983     case elfcpp::R_SPARC_TLS_LDO_ADD:
2984       if (optimized_type == tls::TLSOPT_TO_LE)
2985         {
2986           Insntype* wv = reinterpret_cast<Insntype*>(view);
2987           Insntype val;
2988
2989           // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
2990           val = elfcpp::Swap<32, true>::readval(wv);
2991           val = (val & ~0x7c000) | 0x1c000;
2992           elfcpp::Swap<32, true>::writeval(wv, val);
2993         }
2994       break;
2995
2996       // When optimizing IE --> LE, the only relocation that is handled
2997       // differently is R_SPARC_TLS_IE_LD, it is rewritten from
2998       // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
2999       // rs2 and rd are the same.
3000     case elfcpp::R_SPARC_TLS_IE_LD:
3001     case elfcpp::R_SPARC_TLS_IE_LDX:
3002       if (optimized_type == tls::TLSOPT_TO_LE)
3003         {
3004           Insntype* wv = reinterpret_cast<Insntype*>(view);
3005           Insntype val = elfcpp::Swap<32, true>::readval(wv);
3006           Insntype rs2 = val & 0x1f;
3007           Insntype rd = (val >> 25) & 0x1f;
3008
3009           if (rs2 == rd)
3010             val = sparc_nop;
3011           else
3012             val = sparc_mov | (val & 0x3e00001f);
3013
3014           elfcpp::Swap<32, true>::writeval(wv, val);
3015         }
3016       break;
3017
3018     case elfcpp::R_SPARC_TLS_IE_HI22:
3019     case elfcpp::R_SPARC_TLS_IE_LO10:
3020       if (optimized_type == tls::TLSOPT_TO_LE)
3021         {
3022           value -= tls_segment->memsz();
3023           switch (r_type)
3024             {
3025             case elfcpp::R_SPARC_TLS_IE_HI22:
3026               // IE_HI22 --> LE_HIX22
3027               Reloc::hix22(view, value, addend);
3028               break;
3029             case elfcpp::R_SPARC_TLS_IE_LO10:
3030               // IE_LO10 --> LE_LOX10
3031               Reloc::lox10(view, value, addend);
3032               break;
3033             }
3034           break;
3035         }
3036       else if (optimized_type == tls::TLSOPT_NONE)
3037         {
3038           // Relocate the field with the offset of the GOT entry for
3039           // the tp-relative offset of the symbol.
3040           if (gsym != NULL)
3041             {
3042               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3043               value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
3044             }
3045           else
3046             {
3047               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3048               gold_assert(object->local_has_got_offset(r_sym,
3049                                                        GOT_TYPE_TLS_OFFSET));
3050               value = object->local_got_offset(r_sym,
3051                                                GOT_TYPE_TLS_OFFSET);
3052             }
3053           switch (r_type)
3054             {
3055             case elfcpp::R_SPARC_TLS_IE_HI22:
3056               Reloc::hi22(view, value, addend);
3057               break;
3058             case elfcpp::R_SPARC_TLS_IE_LO10:
3059               Reloc::lo10(view, value, addend);
3060               break;
3061             }
3062           break;
3063         }
3064       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3065                              _("unsupported reloc %u"),
3066                              r_type);
3067       break;
3068
3069     case elfcpp::R_SPARC_TLS_IE_ADD:
3070       // This seems to be mainly so that we can find the addition
3071       // instruction if there is one.  There doesn't seem to be any
3072       // actual relocation to apply.
3073       break;
3074
3075     case elfcpp::R_SPARC_TLS_LE_HIX22:
3076       // If we're creating a shared library, a dynamic relocation will
3077       // have been created for this location, so do not apply it now.
3078       if (!parameters->options().shared())
3079         {
3080           value -= tls_segment->memsz();
3081           Reloc::hix22(view, value, addend);
3082         }
3083       break;
3084
3085     case elfcpp::R_SPARC_TLS_LE_LOX10:
3086       // If we're creating a shared library, a dynamic relocation will
3087       // have been created for this location, so do not apply it now.
3088       if (!parameters->options().shared())
3089         {
3090           value -= tls_segment->memsz();
3091           Reloc::lox10(view, value, addend);
3092         }
3093       break;
3094     }
3095 }
3096
3097 // Relocate section data.
3098
3099 template<int size, bool big_endian>
3100 void
3101 Target_sparc<size, big_endian>::relocate_section(
3102                         const Relocate_info<size, big_endian>* relinfo,
3103                         unsigned int sh_type,
3104                         const unsigned char* prelocs,
3105                         size_t reloc_count,
3106                         Output_section* output_section,
3107                         bool needs_special_offset_handling,
3108                         unsigned char* view,
3109                         typename elfcpp::Elf_types<size>::Elf_Addr address,
3110                         section_size_type view_size,
3111                         const Reloc_symbol_changes* reloc_symbol_changes)
3112 {
3113   typedef Target_sparc<size, big_endian> Sparc;
3114   typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
3115
3116   gold_assert(sh_type == elfcpp::SHT_RELA);
3117
3118   gold::relocate_section<size, big_endian, Sparc, elfcpp::SHT_RELA,
3119     Sparc_relocate>(
3120     relinfo,
3121     this,
3122     prelocs,
3123     reloc_count,
3124     output_section,
3125     needs_special_offset_handling,
3126     view,
3127     address,
3128     view_size,
3129     reloc_symbol_changes);
3130 }
3131
3132 // Return the size of a relocation while scanning during a relocatable
3133 // link.
3134
3135 template<int size, bool big_endian>
3136 unsigned int
3137 Target_sparc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3138     unsigned int,
3139     Relobj*)
3140 {
3141   // We are always SHT_RELA, so we should never get here.
3142   gold_unreachable();
3143   return 0;
3144 }
3145
3146 // Scan the relocs during a relocatable link.
3147
3148 template<int size, bool big_endian>
3149 void
3150 Target_sparc<size, big_endian>::scan_relocatable_relocs(
3151                         const General_options& options,
3152                         Symbol_table* symtab,
3153                         Layout* layout,
3154                         Sized_relobj<size, big_endian>* object,
3155                         unsigned int data_shndx,
3156                         unsigned int sh_type,
3157                         const unsigned char* prelocs,
3158                         size_t reloc_count,
3159                         Output_section* output_section,
3160                         bool needs_special_offset_handling,
3161                         size_t local_symbol_count,
3162                         const unsigned char* plocal_symbols,
3163                         Relocatable_relocs* rr)
3164 {
3165   gold_assert(sh_type == elfcpp::SHT_RELA);
3166
3167   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3168     Relocatable_size_for_reloc> Scan_relocatable_relocs;
3169
3170   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3171       Scan_relocatable_relocs>(
3172     options,
3173     symtab,
3174     layout,
3175     object,
3176     data_shndx,
3177     prelocs,
3178     reloc_count,
3179     output_section,
3180     needs_special_offset_handling,
3181     local_symbol_count,
3182     plocal_symbols,
3183     rr);
3184 }
3185
3186 // Relocate a section during a relocatable link.
3187
3188 template<int size, bool big_endian>
3189 void
3190 Target_sparc<size, big_endian>::relocate_for_relocatable(
3191     const Relocate_info<size, big_endian>* relinfo,
3192     unsigned int sh_type,
3193     const unsigned char* prelocs,
3194     size_t reloc_count,
3195     Output_section* output_section,
3196     off_t offset_in_output_section,
3197     const Relocatable_relocs* rr,
3198     unsigned char* view,
3199     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3200     section_size_type view_size,
3201     unsigned char* reloc_view,
3202     section_size_type reloc_view_size)
3203 {
3204   gold_assert(sh_type == elfcpp::SHT_RELA);
3205
3206   gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
3207     relinfo,
3208     prelocs,
3209     reloc_count,
3210     output_section,
3211     offset_in_output_section,
3212     rr,
3213     view,
3214     view_address,
3215     view_size,
3216     reloc_view,
3217     reloc_view_size);
3218 }
3219
3220 // Return the value to use for a dynamic which requires special
3221 // treatment.  This is how we support equality comparisons of function
3222 // pointers across shared library boundaries, as described in the
3223 // processor specific ABI supplement.
3224
3225 template<int size, bool big_endian>
3226 uint64_t
3227 Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
3228 {
3229   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3230   return this->plt_section()->address() + gsym->plt_offset();
3231 }
3232
3233 // The selector for sparc object files.
3234
3235 template<int size, bool big_endian>
3236 class Target_selector_sparc : public Target_selector
3237 {
3238 public:
3239   Target_selector_sparc()
3240     : Target_selector(elfcpp::EM_NONE, size, big_endian,
3241                       (size == 64 ? "elf64-sparc" : "elf32-sparc"))
3242   { }
3243
3244   Target* do_recognize(int machine, int, int)
3245   {
3246     switch (size)
3247       {
3248       case 64:
3249         if (machine != elfcpp::EM_SPARCV9)
3250           return NULL;
3251         break;
3252
3253       case 32:
3254         if (machine != elfcpp::EM_SPARC
3255             && machine != elfcpp::EM_SPARC32PLUS)
3256           return NULL;
3257         break;
3258
3259       default:
3260         return NULL;
3261       }
3262
3263     return this->instantiate_target();
3264   }
3265
3266   Target* do_instantiate_target()
3267   { return new Target_sparc<size, big_endian>(); }
3268 };
3269
3270 Target_selector_sparc<32, true> target_selector_sparc32;
3271 Target_selector_sparc<64, true> target_selector_sparc64;
3272
3273 } // End anonymous namespace.