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