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