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