OSDN Git Service

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