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