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