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