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