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