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