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