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