]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/binutils-tumbl.git/blob - gold/sparc.cc
Change cond. branching to BRC/BRCI and add CLZ instruction
[fpga/lx-cpu1/binutils-tumbl.git] / gold / sparc.cc
1 // sparc.cc -- sparc target support for gold.
2
3 // Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdlib>
26 #include <cstdio>
27 #include <cstring>
28
29 #include "elfcpp.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "sparc.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "copy-relocs.h"
38 #include "target.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
41 #include "tls.h"
42 #include "errors.h"
43 #include "gc.h"
44
45 namespace
46 {
47
48 using namespace gold;
49
50 template<int size, bool big_endian>
51 class Output_data_plt_sparc;
52
53 template<int size, bool big_endian>
54 class Target_sparc : public Sized_target<size, big_endian>
55 {
56  public:
57   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
58
59   Target_sparc()
60     : Sized_target<size, big_endian>(&sparc_info),
61       got_(NULL), plt_(NULL), rela_dyn_(NULL), rela_ifunc_(NULL),
62       copy_relocs_(elfcpp::R_SPARC_COPY), dynbss_(NULL),
63       got_mod_index_offset_(-1U), tls_get_addr_sym_(NULL),
64       elf_machine_(sparc_info.machine_code), elf_flags_(0),
65       elf_flags_set_(false)
66   {
67   }
68
69   // Process the relocations to determine unreferenced sections for
70   // garbage collection.
71   void
72   gc_process_relocs(Symbol_table* symtab,
73                     Layout* layout,
74                     Sized_relobj_file<size, big_endian>* object,
75                     unsigned int data_shndx,
76                     unsigned int sh_type,
77                     const unsigned char* prelocs,
78                     size_t reloc_count,
79                     Output_section* output_section,
80                     bool needs_special_offset_handling,
81                     size_t local_symbol_count,
82                     const unsigned char* plocal_symbols);
83
84   // Scan the relocations to look for symbol adjustments.
85   void
86   scan_relocs(Symbol_table* symtab,
87               Layout* layout,
88               Sized_relobj_file<size, big_endian>* object,
89               unsigned int data_shndx,
90               unsigned int sh_type,
91               const unsigned char* prelocs,
92               size_t reloc_count,
93               Output_section* output_section,
94               bool needs_special_offset_handling,
95               size_t local_symbol_count,
96               const unsigned char* plocal_symbols);
97   // Finalize the sections.
98   void
99   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
100
101   // Return the value to use for a dynamic which requires special
102   // treatment.
103   uint64_t
104   do_dynsym_value(const Symbol*) const;
105
106   // Relocate a section.
107   void
108   relocate_section(const Relocate_info<size, big_endian>*,
109                    unsigned int sh_type,
110                    const unsigned char* prelocs,
111                    size_t reloc_count,
112                    Output_section* output_section,
113                    bool needs_special_offset_handling,
114                    unsigned char* view,
115                    typename elfcpp::Elf_types<size>::Elf_Addr view_address,
116                    section_size_type view_size,
117                    const Reloc_symbol_changes*);
118
119   // Scan the relocs during a relocatable link.
120   void
121   scan_relocatable_relocs(Symbol_table* symtab,
122                           Layout* layout,
123                           Sized_relobj_file<size, big_endian>* object,
124                           unsigned int data_shndx,
125                           unsigned int sh_type,
126                           const unsigned char* prelocs,
127                           size_t reloc_count,
128                           Output_section* output_section,
129                           bool needs_special_offset_handling,
130                           size_t local_symbol_count,
131                           const unsigned char* plocal_symbols,
132                           Relocatable_relocs*);
133
134   // Relocate a section during a relocatable link.
135   void
136   relocate_for_relocatable(const Relocate_info<size, big_endian>*,
137                            unsigned int sh_type,
138                            const unsigned char* prelocs,
139                            size_t reloc_count,
140                            Output_section* output_section,
141                            typename elfcpp::Elf_types<size>::Elf_Off
142                              offset_in_output_section,
143                            const Relocatable_relocs*,
144                            unsigned char* view,
145                            typename elfcpp::Elf_types<size>::Elf_Addr view_address,
146                            section_size_type view_size,
147                            unsigned char* reloc_view,
148                            section_size_type reloc_view_size);
149
150   // Return whether SYM is defined by the ABI.
151   bool
152   do_is_defined_by_abi(const Symbol* sym) const
153   {
154     // XXX Really need to support this better...
155     if (sym->type() == elfcpp::STT_SPARC_REGISTER)
156       return 1;
157
158     return strcmp(sym->name(), "___tls_get_addr") == 0;
159   }
160
161   // Return the PLT address to use for a global symbol.
162   uint64_t
163   do_plt_address_for_global(const Symbol* gsym) const
164   { return this->plt_section()->address_for_global(gsym); }
165
166   uint64_t
167   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
168   { return this->plt_section()->address_for_local(relobj, symndx); }
169
170   // Return whether there is a GOT section.
171   bool
172   has_got_section() const
173   { return this->got_ != NULL; }
174
175   // Return the size of the GOT section.
176   section_size_type
177   got_size() const
178   {
179     gold_assert(this->got_ != NULL);
180     return this->got_->data_size();
181   }
182
183   // Return the number of entries in the GOT.
184   unsigned int
185   got_entry_count() const
186   {
187     if (this->got_ == NULL)
188       return 0;
189     return this->got_size() / (size / 8);
190   }
191
192   // Return the address of the GOT.
193   uint64_t
194   got_address() const
195   {
196     if (this->got_ == NULL)
197       return 0;
198     return this->got_->address();
199   }
200
201   // Return the number of entries in the PLT.
202   unsigned int
203   plt_entry_count() const;
204
205   // Return the offset of the first non-reserved PLT entry.
206   unsigned int
207   first_plt_entry_offset() const;
208
209   // Return the size of each PLT entry.
210   unsigned int
211   plt_entry_size() const;
212
213  protected:
214   // Make an ELF object.
215   Object*
216   do_make_elf_object(const std::string&, Input_file*, off_t,
217                      const elfcpp::Ehdr<size, big_endian>& ehdr);
218
219   void
220   do_adjust_elf_header(unsigned char* view, int len) const;
221
222  private:
223
224   // The class which scans relocations.
225   class Scan
226   {
227   public:
228     Scan()
229       : issued_non_pic_error_(false)
230     { }
231
232     static inline int
233     get_reference_flags(unsigned int r_type);
234
235     inline void
236     local(Symbol_table* symtab, Layout* layout, Target_sparc* target,
237           Sized_relobj_file<size, big_endian>* object,
238           unsigned int data_shndx,
239           Output_section* output_section,
240           const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
241           const elfcpp::Sym<size, big_endian>& lsym);
242
243     inline void
244     global(Symbol_table* symtab, Layout* layout, Target_sparc* target,
245            Sized_relobj_file<size, big_endian>* object,
246            unsigned int data_shndx,
247            Output_section* output_section,
248            const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
249            Symbol* gsym);
250
251     inline bool
252     local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
253                                         Target_sparc* ,
254                                         Sized_relobj_file<size, big_endian>* ,
255                                         unsigned int ,
256                                         Output_section* ,
257                                         const elfcpp::Rela<size, big_endian>& ,
258                                         unsigned int ,
259                                         const elfcpp::Sym<size, big_endian>&)
260     { return false; }
261
262     inline bool
263     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
264                                          Target_sparc* ,
265                                          Sized_relobj_file<size, big_endian>* ,
266                                          unsigned int ,
267                                          Output_section* ,
268                                          const elfcpp::Rela<size,
269                                                             big_endian>& ,
270                                          unsigned int , Symbol*)
271     { return false; }
272
273
274   private:
275     static void
276     unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
277                             unsigned int r_type);
278
279     static void
280     unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
281                              unsigned int r_type, Symbol*);
282
283     static void
284     generate_tls_call(Symbol_table* symtab, Layout* layout,
285                       Target_sparc* target);
286
287     void
288     check_non_pic(Relobj*, unsigned int r_type);
289
290     bool
291     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>*,
292                               unsigned int r_type);
293
294     // Whether we have issued an error about a non-PIC compilation.
295     bool issued_non_pic_error_;
296   };
297
298   // The class which implements relocation.
299   class Relocate
300   {
301    public:
302     Relocate()
303       : ignore_gd_add_(false), reloc_adjust_addr_(NULL)
304     { }
305
306     ~Relocate()
307     {
308       if (this->ignore_gd_add_)
309         {
310           // FIXME: This needs to specify the location somehow.
311           gold_error(_("missing expected TLS relocation"));
312         }
313     }
314
315     // Do a relocation.  Return false if the caller should not issue
316     // any warnings about this relocation.
317     inline bool
318     relocate(const Relocate_info<size, big_endian>*, Target_sparc*,
319              Output_section*, size_t relnum,
320              const elfcpp::Rela<size, big_endian>&,
321              unsigned int r_type, const Sized_symbol<size>*,
322              const Symbol_value<size>*,
323              unsigned char*,
324              typename elfcpp::Elf_types<size>::Elf_Addr,
325              section_size_type);
326
327    private:
328     // Do a TLS relocation.
329     inline void
330     relocate_tls(const Relocate_info<size, big_endian>*, Target_sparc* target,
331                  size_t relnum, const elfcpp::Rela<size, big_endian>&,
332                  unsigned int r_type, const Sized_symbol<size>*,
333                  const Symbol_value<size>*,
334                  unsigned char*,
335                  typename elfcpp::Elf_types<size>::Elf_Addr,
336                  section_size_type);
337
338     inline void
339     relax_call(Target_sparc<size, big_endian>* target,
340                unsigned char* view,
341                const elfcpp::Rela<size, big_endian>& rela,
342                section_size_type view_size);
343
344     // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
345     bool ignore_gd_add_;
346
347     // If we hit a reloc at this view address, adjust it back by 4 bytes.
348     unsigned char *reloc_adjust_addr_;
349   };
350
351   // A class which returns the size required for a relocation type,
352   // used while scanning relocs during a relocatable link.
353   class Relocatable_size_for_reloc
354   {
355    public:
356     unsigned int
357     get_size_for_reloc(unsigned int, Relobj*);
358   };
359
360   // Get the GOT section, creating it if necessary.
361   Output_data_got<size, big_endian>*
362   got_section(Symbol_table*, Layout*);
363
364   // Create the PLT section.
365   void
366   make_plt_section(Symbol_table* symtab, Layout* layout);
367
368   // Create a PLT entry for a global symbol.
369   void
370   make_plt_entry(Symbol_table*, Layout*, Symbol*);
371
372   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
373   void
374   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
375                              Sized_relobj_file<size, big_endian>* relobj,
376                              unsigned int local_sym_index);
377
378   // Create a GOT entry for the TLS module index.
379   unsigned int
380   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
381                       Sized_relobj_file<size, big_endian>* object);
382
383   // Return the gsym for "__tls_get_addr".  Cache if not already
384   // cached.
385   Symbol*
386   tls_get_addr_sym(Symbol_table* symtab)
387   {
388     if (!this->tls_get_addr_sym_)
389       this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
390     gold_assert(this->tls_get_addr_sym_);
391     return this->tls_get_addr_sym_;
392   }
393
394   // Get the PLT section.
395   Output_data_plt_sparc<size, big_endian>*
396   plt_section() const
397   {
398     gold_assert(this->plt_ != NULL);
399     return this->plt_;
400   }
401
402   // Get the dynamic reloc section, creating it if necessary.
403   Reloc_section*
404   rela_dyn_section(Layout*);
405
406   // Get the section to use for IFUNC relocations.
407   Reloc_section*
408   rela_ifunc_section(Layout*);
409
410   // Copy a relocation against a global symbol.
411   void
412   copy_reloc(Symbol_table* symtab, Layout* layout,
413              Sized_relobj_file<size, big_endian>* object,
414              unsigned int shndx, Output_section* output_section,
415              Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
416   {
417     this->copy_relocs_.copy_reloc(symtab, layout,
418                                   symtab->get_sized_symbol<size>(sym),
419                                   object, shndx, output_section,
420                                   reloc, this->rela_dyn_section(layout));
421   }
422
423   // Information about this specific target which we pass to the
424   // general Target structure.
425   static Target::Target_info sparc_info;
426
427   // The types of GOT entries needed for this platform.
428   // These values are exposed to the ABI in an incremental link.
429   // Do not renumber existing values without changing the version
430   // number of the .gnu_incremental_inputs section.
431   enum Got_type
432   {
433     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
434     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
435     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
436   };
437
438   // The GOT section.
439   Output_data_got<size, big_endian>* got_;
440   // The PLT section.
441   Output_data_plt_sparc<size, big_endian>* plt_;
442   // The dynamic reloc section.
443   Reloc_section* rela_dyn_;
444   // The section to use for IFUNC relocs.
445   Reloc_section* rela_ifunc_;
446   // Relocs saved to avoid a COPY reloc.
447   Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
448   // Space for variables copied with a COPY reloc.
449   Output_data_space* dynbss_;
450   // Offset of the GOT entry for the TLS module index;
451   unsigned int got_mod_index_offset_;
452   // Cached pointer to __tls_get_addr symbol
453   Symbol* tls_get_addr_sym_;
454   // Accumulated elf machine type
455   elfcpp::Elf_Half elf_machine_;
456   // Accumulated elf header flags
457   elfcpp::Elf_Word elf_flags_;
458   // Whether elf_flags_ has been set for the first time yet
459   bool elf_flags_set_;
460 };
461
462 template<>
463 Target::Target_info Target_sparc<32, true>::sparc_info =
464 {
465   32,                   // size
466   true,                 // is_big_endian
467   elfcpp::EM_SPARC,     // machine_code
468   false,                // has_make_symbol
469   false,                // has_resolve
470   false,                // has_code_fill
471   true,                 // is_default_stack_executable
472   false,                // can_icf_inline_merge_sections
473   '\0',                 // wrap_char
474   "/usr/lib/ld.so.1",   // dynamic_linker
475   0x00010000,           // default_text_segment_address
476   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
477   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
478   false,                // isolate_execinstr
479   0,                    // rosegment_gap
480   elfcpp::SHN_UNDEF,    // small_common_shndx
481   elfcpp::SHN_UNDEF,    // large_common_shndx
482   0,                    // small_common_section_flags
483   0,                    // large_common_section_flags
484   NULL,                 // attributes_section
485   NULL                  // attributes_vendor
486 };
487
488 template<>
489 Target::Target_info Target_sparc<64, true>::sparc_info =
490 {
491   64,                   // size
492   true,                 // is_big_endian
493   elfcpp::EM_SPARCV9,   // machine_code
494   false,                // has_make_symbol
495   false,                // has_resolve
496   false,                // has_code_fill
497   true,                 // is_default_stack_executable
498   false,                // can_icf_inline_merge_sections
499   '\0',                 // wrap_char
500   "/usr/lib/sparcv9/ld.so.1",   // dynamic_linker
501   0x100000,             // default_text_segment_address
502   64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
503   8 * 1024,             // common_pagesize (overridable by -z common-page-size)
504   false,                // isolate_execinstr
505   0,                    // rosegment_gap
506   elfcpp::SHN_UNDEF,    // small_common_shndx
507   elfcpp::SHN_UNDEF,    // large_common_shndx
508   0,                    // small_common_section_flags
509   0,                    // large_common_section_flags
510   NULL,                 // attributes_section
511   NULL                  // attributes_vendor
512 };
513
514 // We have to take care here, even when operating in little-endian
515 // mode, sparc instructions are still big endian.
516 template<int size, bool big_endian>
517 class Sparc_relocate_functions
518 {
519 private:
520   // Do a simple relocation with the addend in the relocation.
521   template<int valsize>
522   static inline void
523   rela(unsigned char* view,
524        unsigned int right_shift,
525        typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
526        typename elfcpp::Swap<size, big_endian>::Valtype value,
527        typename elfcpp::Swap<size, big_endian>::Valtype addend)
528   {
529     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
530     Valtype* wv = reinterpret_cast<Valtype*>(view);
531     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
532     Valtype reloc = ((value + addend) >> right_shift);
533
534     val &= ~dst_mask;
535     reloc &= dst_mask;
536
537     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
538   }
539
540   // Do a simple relocation using a symbol value with the addend in
541   // the relocation.
542   template<int valsize>
543   static inline void
544   rela(unsigned char* view,
545        unsigned int right_shift,
546        typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
547        const Sized_relobj_file<size, big_endian>* object,
548        const Symbol_value<size>* psymval,
549        typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
550   {
551     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
552     Valtype* wv = reinterpret_cast<Valtype*>(view);
553     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
554     Valtype reloc = (psymval->value(object, addend) >> right_shift);
555
556     val &= ~dst_mask;
557     reloc &= dst_mask;
558
559     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
560   }
561
562   // Do a simple relocation using a symbol value with the addend in
563   // the relocation, unaligned.
564   template<int valsize>
565   static inline void
566   rela_ua(unsigned char* view,
567           unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
568           const Sized_relobj_file<size, big_endian>* object,
569           const Symbol_value<size>* psymval,
570           typename elfcpp::Swap<size, big_endian>::Valtype addend)
571   {
572     typedef typename elfcpp::Swap_unaligned<valsize,
573             big_endian>::Valtype Valtype;
574     unsigned char* wv = view;
575     Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
576     Valtype reloc = (psymval->value(object, addend) >> right_shift);
577
578     val &= ~dst_mask;
579     reloc &= dst_mask;
580
581     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
582   }
583
584   // Do a simple PC relative relocation with a Symbol_value with the
585   // addend in the relocation.
586   template<int valsize>
587   static inline void
588   pcrela(unsigned char* view,
589          unsigned int right_shift,
590          typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
591          const Sized_relobj_file<size, big_endian>* object,
592          const Symbol_value<size>* psymval,
593          typename elfcpp::Swap<size, big_endian>::Valtype addend,
594          typename elfcpp::Elf_types<size>::Elf_Addr address)
595   {
596     typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
597     Valtype* wv = reinterpret_cast<Valtype*>(view);
598     Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
599     Valtype reloc = ((psymval->value(object, addend) - address)
600                      >> right_shift);
601
602     val &= ~dst_mask;
603     reloc &= dst_mask;
604
605     elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
606   }
607
608   template<int valsize>
609   static inline void
610   pcrela_unaligned(unsigned char* view,
611                    const Sized_relobj_file<size, big_endian>* object,
612                    const Symbol_value<size>* psymval,
613                    typename elfcpp::Swap<size, big_endian>::Valtype addend,
614                    typename elfcpp::Elf_types<size>::Elf_Addr address)
615   {
616     typedef typename elfcpp::Swap_unaligned<valsize,
617             big_endian>::Valtype Valtype;
618     unsigned char* wv = view;
619     Valtype reloc = (psymval->value(object, addend) - address);
620
621     elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
622   }
623
624   typedef Sparc_relocate_functions<size, big_endian> This;
625   typedef Sparc_relocate_functions<size, true> This_insn;
626
627 public:
628   // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
629   static inline void
630   wdisp30(unsigned char* view,
631            const Sized_relobj_file<size, big_endian>* object,
632            const Symbol_value<size>* psymval,
633            typename elfcpp::Elf_types<size>::Elf_Addr addend,
634            typename elfcpp::Elf_types<size>::Elf_Addr address)
635   {
636     This_insn::template pcrela<32>(view, 2, 0x3fffffff, object,
637                                    psymval, addend, address);
638   }
639
640   // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
641   static inline void
642   wdisp22(unsigned char* view,
643            const Sized_relobj_file<size, big_endian>* object,
644            const Symbol_value<size>* psymval,
645            typename elfcpp::Elf_types<size>::Elf_Addr addend,
646            typename elfcpp::Elf_types<size>::Elf_Addr address)
647   {
648     This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
649                                    psymval, addend, address);
650   }
651
652   // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
653   static inline void
654   wdisp19(unsigned char* view,
655           const Sized_relobj_file<size, big_endian>* object,
656           const Symbol_value<size>* psymval,
657           typename elfcpp::Elf_types<size>::Elf_Addr addend,
658           typename elfcpp::Elf_types<size>::Elf_Addr address)
659   {
660     This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
661                                    psymval, addend, address);
662   }
663
664   // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
665   static inline void
666   wdisp16(unsigned char* view,
667           const Sized_relobj_file<size, big_endian>* object,
668           const Symbol_value<size>* psymval,
669           typename elfcpp::Elf_types<size>::Elf_Addr addend,
670           typename elfcpp::Elf_types<size>::Elf_Addr address)
671   {
672     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
673     Valtype* wv = reinterpret_cast<Valtype*>(view);
674     Valtype val = elfcpp::Swap<32, true>::readval(wv);
675     Valtype reloc = ((psymval->value(object, addend) - address)
676                      >> 2);
677
678     // The relocation value is split between the low 14 bits,
679     // and bits 20-21.
680     val &= ~((0x3 << 20) | 0x3fff);
681     reloc = (((reloc & 0xc000) << (20 - 14))
682              | (reloc & 0x3ffff));
683
684     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
685   }
686
687   // R_SPARC_WDISP10: (Symbol + Addend - Address) >> 2
688   static inline void
689   wdisp10(unsigned char* view,
690           const Sized_relobj_file<size, big_endian>* object,
691           const Symbol_value<size>* psymval,
692           typename elfcpp::Elf_types<size>::Elf_Addr addend,
693           typename elfcpp::Elf_types<size>::Elf_Addr address)
694   {
695     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
696     Valtype* wv = reinterpret_cast<Valtype*>(view);
697     Valtype val = elfcpp::Swap<32, true>::readval(wv);
698     Valtype reloc = ((psymval->value(object, addend) - address)
699                      >> 2);
700
701     // The relocation value is split between the low bits 5-12,
702     // and high bits 19-20.
703     val &= ~((0x3 << 19) | (0xff << 5));
704     reloc = (((reloc & 0x300) << (19 - 8))
705              | ((reloc & 0xff) << (5 - 0)));
706
707     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
708   }
709
710   // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
711   static inline void
712   pc22(unsigned char* view,
713        const Sized_relobj_file<size, big_endian>* object,
714        const Symbol_value<size>* psymval,
715        typename elfcpp::Elf_types<size>::Elf_Addr addend,
716        typename elfcpp::Elf_types<size>::Elf_Addr address)
717   {
718     This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
719                                    psymval, addend, address);
720   }
721
722   // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
723   static inline void
724   pc10(unsigned char* view,
725        const Sized_relobj_file<size, big_endian>* object,
726        const Symbol_value<size>* psymval,
727        typename elfcpp::Elf_types<size>::Elf_Addr addend,
728        typename elfcpp::Elf_types<size>::Elf_Addr address)
729   {
730     This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
731                                    psymval, addend, address);
732   }
733
734   // R_SPARC_HI22: (Symbol + Addend) >> 10
735   static inline void
736   hi22(unsigned char* view,
737        typename elfcpp::Elf_types<size>::Elf_Addr value,
738        typename elfcpp::Elf_types<size>::Elf_Addr addend)
739   {
740     This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
741   }
742
743   // R_SPARC_HI22: (Symbol + Addend) >> 10
744   static inline void
745   hi22(unsigned char* view,
746        const Sized_relobj_file<size, big_endian>* object,
747        const Symbol_value<size>* psymval,
748        typename elfcpp::Elf_types<size>::Elf_Addr addend)
749   {
750     This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
751   }
752
753   // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
754   static inline void
755   pcplt22(unsigned char* view,
756           const Sized_relobj_file<size, big_endian>* object,
757           const Symbol_value<size>* psymval,
758           typename elfcpp::Elf_types<size>::Elf_Addr addend,
759           typename elfcpp::Elf_types<size>::Elf_Addr address)
760   {
761     This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
762                                    psymval, addend, address);
763   }
764
765   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
766   static inline void
767   lo10(unsigned char* view,
768        typename elfcpp::Elf_types<size>::Elf_Addr value,
769        typename elfcpp::Elf_types<size>::Elf_Addr addend)
770   {
771     This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
772   }
773
774   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
775   static inline void
776   lo10(unsigned char* view,
777        const Sized_relobj_file<size, big_endian>* object,
778        const Symbol_value<size>* psymval,
779        typename elfcpp::Elf_types<size>::Elf_Addr addend)
780   {
781     This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
782   }
783
784   // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
785   static inline void
786   lo10(unsigned char* view,
787        const Sized_relobj_file<size, big_endian>* object,
788        const Symbol_value<size>* psymval,
789        typename elfcpp::Elf_types<size>::Elf_Addr addend,
790        typename elfcpp::Elf_types<size>::Elf_Addr address)
791   {
792     This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
793                                    psymval, addend, address);
794   }
795
796   // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
797   static inline void
798   olo10(unsigned char* view,
799         const Sized_relobj_file<size, big_endian>* object,
800         const Symbol_value<size>* psymval,
801         typename elfcpp::Elf_types<size>::Elf_Addr addend,
802         typename elfcpp::Elf_types<size>::Elf_Addr addend2)
803   {
804     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
805     Valtype* wv = reinterpret_cast<Valtype*>(view);
806     Valtype val = elfcpp::Swap<32, true>::readval(wv);
807     Valtype reloc = psymval->value(object, addend);
808
809     val &= ~0x1fff;
810     reloc &= 0x3ff;
811     reloc += addend2;
812     reloc &= 0x1fff;
813
814     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
815   }
816
817   // R_SPARC_22: (Symbol + Addend)
818   static inline void
819   rela32_22(unsigned char* view,
820             const Sized_relobj_file<size, big_endian>* object,
821             const Symbol_value<size>* psymval,
822             typename elfcpp::Elf_types<size>::Elf_Addr addend)
823   {
824     This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
825   }
826
827   // R_SPARC_13: (Symbol + Addend)
828   static inline void
829   rela32_13(unsigned char* view,
830             typename elfcpp::Elf_types<size>::Elf_Addr value,
831             typename elfcpp::Elf_types<size>::Elf_Addr addend)
832   {
833     This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
834   }
835
836   // R_SPARC_13: (Symbol + Addend)
837   static inline void
838   rela32_13(unsigned char* view,
839             const Sized_relobj_file<size, big_endian>* object,
840             const Symbol_value<size>* psymval,
841             typename elfcpp::Elf_types<size>::Elf_Addr addend)
842   {
843     This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
844   }
845
846   // R_SPARC_UA16: (Symbol + Addend)
847   static inline void
848   ua16(unsigned char* view,
849        const Sized_relobj_file<size, big_endian>* object,
850        const Symbol_value<size>* psymval,
851        typename elfcpp::Elf_types<size>::Elf_Addr addend)
852   {
853     This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
854   }
855
856   // R_SPARC_UA32: (Symbol + Addend)
857   static inline void
858   ua32(unsigned char* view,
859        const Sized_relobj_file<size, big_endian>* object,
860        const Symbol_value<size>* psymval,
861        typename elfcpp::Elf_types<size>::Elf_Addr addend)
862   {
863     This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
864   }
865
866   // R_SPARC_UA64: (Symbol + Addend)
867   static inline void
868   ua64(unsigned char* view,
869        const Sized_relobj_file<size, big_endian>* object,
870        const Symbol_value<size>* psymval,
871        typename elfcpp::Elf_types<size>::Elf_Addr addend)
872   {
873     This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
874                                object, psymval, addend);
875   }
876
877   // R_SPARC_DISP8: (Symbol + Addend - Address)
878   static inline void
879   disp8(unsigned char* view,
880         const Sized_relobj_file<size, big_endian>* object,
881         const Symbol_value<size>* psymval,
882         typename elfcpp::Elf_types<size>::Elf_Addr addend,
883         typename elfcpp::Elf_types<size>::Elf_Addr address)
884   {
885     This::template pcrela_unaligned<8>(view, object, psymval,
886                                        addend, address);
887   }
888
889   // R_SPARC_DISP16: (Symbol + Addend - Address)
890   static inline void
891   disp16(unsigned char* view,
892          const Sized_relobj_file<size, big_endian>* object,
893          const Symbol_value<size>* psymval,
894          typename elfcpp::Elf_types<size>::Elf_Addr addend,
895          typename elfcpp::Elf_types<size>::Elf_Addr address)
896   {
897     This::template pcrela_unaligned<16>(view, object, psymval,
898                                         addend, address);
899   }
900
901   // R_SPARC_DISP32: (Symbol + Addend - Address)
902   static inline void
903   disp32(unsigned char* view,
904          const Sized_relobj_file<size, big_endian>* object,
905          const Symbol_value<size>* psymval,
906          typename elfcpp::Elf_types<size>::Elf_Addr addend,
907          typename elfcpp::Elf_types<size>::Elf_Addr address)
908   {
909     This::template pcrela_unaligned<32>(view, object, psymval,
910                                         addend, address);
911   }
912
913   // R_SPARC_DISP64: (Symbol + Addend - Address)
914   static inline void
915   disp64(unsigned char* view,
916          const Sized_relobj_file<size, big_endian>* object,
917          const Symbol_value<size>* psymval,
918          elfcpp::Elf_Xword addend,
919          typename elfcpp::Elf_types<size>::Elf_Addr address)
920   {
921     This::template pcrela_unaligned<64>(view, object, psymval,
922                                         addend, address);
923   }
924
925   // R_SPARC_H34: (Symbol + Addend) >> 12
926   static inline void
927   h34(unsigned char* view,
928       const Sized_relobj_file<size, big_endian>* object,
929       const Symbol_value<size>* psymval,
930       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
931   {
932     This_insn::template rela<32>(view, 12, 0x003fffff, object, psymval, addend);
933   }
934
935   // R_SPARC_H44: (Symbol + Addend) >> 22
936   static inline void
937   h44(unsigned char* view,
938       const Sized_relobj_file<size, big_endian>* object,
939       const Symbol_value<size>* psymval,
940       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
941   {
942     This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
943   }
944
945   // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
946   static inline void
947   m44(unsigned char* view,
948       const Sized_relobj_file<size, big_endian>* object,
949       const Symbol_value<size>* psymval,
950       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
951   {
952     This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
953   }
954
955   // R_SPARC_L44: (Symbol + Addend) & 0xfff
956   static inline void
957   l44(unsigned char* view,
958       const Sized_relobj_file<size, big_endian>* object,
959       const Symbol_value<size>* psymval,
960       typename elfcpp::Elf_types<size>::Elf_Addr  addend)
961   {
962     This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
963   }
964
965   // R_SPARC_HH22: (Symbol + Addend) >> 42
966   static inline void
967   hh22(unsigned char* view,
968        const Sized_relobj_file<size, big_endian>* object,
969        const Symbol_value<size>* psymval,
970        typename elfcpp::Elf_types<size>::Elf_Addr addend)
971   {
972     This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
973   }
974
975   // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
976   static inline void
977   pc_hh22(unsigned char* view,
978           const Sized_relobj_file<size, big_endian>* object,
979           const Symbol_value<size>* psymval,
980           typename elfcpp::Elf_types<size>::Elf_Addr addend,
981           typename elfcpp::Elf_types<size>::Elf_Addr address)
982   {
983     This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
984                                    psymval, addend, address);
985   }
986
987   // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
988   static inline void
989   hm10(unsigned char* view,
990        const Sized_relobj_file<size, big_endian>* object,
991        const Symbol_value<size>* psymval,
992        typename elfcpp::Elf_types<size>::Elf_Addr addend)
993   {
994     This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
995   }
996
997   // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
998   static inline void
999   pc_hm10(unsigned char* view,
1000           const Sized_relobj_file<size, big_endian>* object,
1001           const Symbol_value<size>* psymval,
1002           typename elfcpp::Elf_types<size>::Elf_Addr addend,
1003           typename elfcpp::Elf_types<size>::Elf_Addr address)
1004   {
1005     This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
1006                                    psymval, addend, address);
1007   }
1008
1009   // R_SPARC_11: (Symbol + Addend)
1010   static inline void
1011   rela32_11(unsigned char* view,
1012             const Sized_relobj_file<size, big_endian>* object,
1013             const Symbol_value<size>* psymval,
1014             typename elfcpp::Elf_types<size>::Elf_Addr addend)
1015   {
1016     This_insn::template rela<32>(view, 0, 0x000007ff, object, psymval, addend);
1017   }
1018
1019   // R_SPARC_10: (Symbol + Addend)
1020   static inline void
1021   rela32_10(unsigned char* view,
1022             const Sized_relobj_file<size, big_endian>* object,
1023             const Symbol_value<size>* psymval,
1024             typename elfcpp::Elf_types<size>::Elf_Addr addend)
1025   {
1026     This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
1027   }
1028
1029   // R_SPARC_7: (Symbol + Addend)
1030   static inline void
1031   rela32_7(unsigned char* view,
1032            const Sized_relobj_file<size, big_endian>* object,
1033            const Symbol_value<size>* psymval,
1034            typename elfcpp::Elf_types<size>::Elf_Addr addend)
1035   {
1036     This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
1037   }
1038
1039   // R_SPARC_6: (Symbol + Addend)
1040   static inline void
1041   rela32_6(unsigned char* view,
1042            const Sized_relobj_file<size, big_endian>* object,
1043            const Symbol_value<size>* psymval,
1044            typename elfcpp::Elf_types<size>::Elf_Addr addend)
1045   {
1046     This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
1047   }
1048
1049   // R_SPARC_5: (Symbol + Addend)
1050   static inline void
1051   rela32_5(unsigned char* view,
1052            const Sized_relobj_file<size, big_endian>* object,
1053            const Symbol_value<size>* psymval,
1054            typename elfcpp::Elf_types<size>::Elf_Addr addend)
1055   {
1056     This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
1057   }
1058
1059   // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
1060   static inline void
1061   ldo_hix22(unsigned char* view,
1062             typename elfcpp::Elf_types<size>::Elf_Addr value,
1063             typename elfcpp::Elf_types<size>::Elf_Addr addend)
1064   {
1065     This_insn::hi22(view, value, addend);
1066   }
1067
1068   // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
1069   static inline void
1070   ldo_lox10(unsigned char* view,
1071             typename elfcpp::Elf_types<size>::Elf_Addr value,
1072             typename elfcpp::Elf_types<size>::Elf_Addr addend)
1073   {
1074     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1075     Valtype* wv = reinterpret_cast<Valtype*>(view);
1076     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1077     Valtype reloc = (value + addend);
1078
1079     val &= ~0x1fff;
1080     reloc &= 0x3ff;
1081
1082     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1083   }
1084
1085   // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
1086   static inline void
1087   hix22(unsigned char* view,
1088         typename elfcpp::Elf_types<size>::Elf_Addr value,
1089         typename elfcpp::Elf_types<size>::Elf_Addr addend)
1090   {
1091     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1092     Valtype* wv = reinterpret_cast<Valtype*>(view);
1093     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1094     Valtype reloc = (value + addend);
1095
1096     val &= ~0x3fffff;
1097
1098     reloc ^= ~(Valtype)0;
1099     reloc >>= 10;
1100
1101     reloc &= 0x3fffff;
1102
1103     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1104   }
1105
1106   // R_SPARC_GOTDATA_OP_HIX22: @gdopoff(Symbol + Addend) >> 10
1107   static inline void
1108   gdop_hix22(unsigned char* view,
1109              typename elfcpp::Elf_types<size>::Elf_Addr value,
1110              typename elfcpp::Elf_types<size>::Elf_Addr addend)
1111   {
1112     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1113     Valtype* wv = reinterpret_cast<Valtype*>(view);
1114     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1115     int32_t reloc = static_cast<int32_t>(value + addend);
1116
1117     val &= ~0x3fffff;
1118
1119     if (reloc < 0)
1120       reloc ^= ~static_cast<int32_t>(0);
1121     reloc >>= 10;
1122
1123     reloc &= 0x3fffff;
1124
1125     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1126   }
1127
1128   // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
1129   static inline void
1130   hix22(unsigned char* view,
1131         const Sized_relobj_file<size, big_endian>* object,
1132         const Symbol_value<size>* psymval,
1133         typename elfcpp::Elf_types<size>::Elf_Addr addend)
1134   {
1135     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1136     Valtype* wv = reinterpret_cast<Valtype*>(view);
1137     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1138     Valtype reloc = psymval->value(object, addend);
1139
1140     val &= ~0x3fffff;
1141
1142     reloc ^= ~(Valtype)0;
1143     reloc >>= 10;
1144
1145     reloc &= 0x3fffff;
1146
1147     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1148   }
1149
1150
1151   // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
1152   static inline void
1153   lox10(unsigned char* view,
1154         typename elfcpp::Elf_types<size>::Elf_Addr value,
1155         typename elfcpp::Elf_types<size>::Elf_Addr addend)
1156   {
1157     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1158     Valtype* wv = reinterpret_cast<Valtype*>(view);
1159     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1160     Valtype reloc = (value + addend);
1161
1162     val &= ~0x1fff;
1163     reloc &= 0x3ff;
1164     reloc |= 0x1c00;
1165
1166     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1167   }
1168
1169   // R_SPARC_GOTDATA_OP_LOX10: (@gdopoff(Symbol + Addend) & 0x3ff) | 0x1c00
1170   static inline void
1171   gdop_lox10(unsigned char* view,
1172              typename elfcpp::Elf_types<size>::Elf_Addr value,
1173              typename elfcpp::Elf_types<size>::Elf_Addr addend)
1174   {
1175     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1176     Valtype* wv = reinterpret_cast<Valtype*>(view);
1177     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1178     int32_t reloc = static_cast<int32_t>(value + addend);
1179
1180     if (reloc < 0)
1181       reloc = (reloc & 0x3ff) | 0x1c00;
1182     else
1183       reloc = (reloc & 0x3ff);
1184
1185     val &= ~0x1fff;
1186     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1187   }
1188
1189   // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
1190   static inline void
1191   lox10(unsigned char* view,
1192         const Sized_relobj_file<size, big_endian>* object,
1193         const Symbol_value<size>* psymval,
1194         typename elfcpp::Elf_types<size>::Elf_Addr addend)
1195   {
1196     typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1197     Valtype* wv = reinterpret_cast<Valtype*>(view);
1198     Valtype val = elfcpp::Swap<32, true>::readval(wv);
1199     Valtype reloc = psymval->value(object, addend);
1200
1201     val &= ~0x1fff;
1202     reloc &= 0x3ff;
1203     reloc |= 0x1c00;
1204
1205     elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1206   }
1207 };
1208
1209 // Get the GOT section, creating it if necessary.
1210
1211 template<int size, bool big_endian>
1212 Output_data_got<size, big_endian>*
1213 Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
1214                                             Layout* layout)
1215 {
1216   if (this->got_ == NULL)
1217     {
1218       gold_assert(symtab != NULL && layout != NULL);
1219
1220       this->got_ = new Output_data_got<size, big_endian>();
1221
1222       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1223                                       (elfcpp::SHF_ALLOC
1224                                        | elfcpp::SHF_WRITE),
1225                                       this->got_, ORDER_RELRO, true);
1226
1227       // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1228       symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1229                                     Symbol_table::PREDEFINED,
1230                                     this->got_,
1231                                     0, 0, elfcpp::STT_OBJECT,
1232                                     elfcpp::STB_LOCAL,
1233                                     elfcpp::STV_HIDDEN, 0,
1234                                     false, false);
1235     }
1236
1237   return this->got_;
1238 }
1239
1240 // Get the dynamic reloc section, creating it if necessary.
1241
1242 template<int size, bool big_endian>
1243 typename Target_sparc<size, big_endian>::Reloc_section*
1244 Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
1245 {
1246   if (this->rela_dyn_ == NULL)
1247     {
1248       gold_assert(layout != NULL);
1249       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1250       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1251                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
1252                                       ORDER_DYNAMIC_RELOCS, false);
1253     }
1254   return this->rela_dyn_;
1255 }
1256
1257 // Get the section to use for IFUNC relocs, creating it if
1258 // necessary.  These go in .rela.dyn, but only after all other dynamic
1259 // relocations.  They need to follow the other dynamic relocations so
1260 // that they can refer to global variables initialized by those
1261 // relocs.
1262
1263 template<int size, bool big_endian>
1264 typename Target_sparc<size, big_endian>::Reloc_section*
1265 Target_sparc<size, big_endian>::rela_ifunc_section(Layout* layout)
1266 {
1267   if (this->rela_ifunc_ == NULL)
1268     {
1269       // Make sure we have already created the dynamic reloc section.
1270       this->rela_dyn_section(layout);
1271       this->rela_ifunc_ = new Reloc_section(false);
1272       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1273                                       elfcpp::SHF_ALLOC, this->rela_ifunc_,
1274                                       ORDER_DYNAMIC_RELOCS, false);
1275       gold_assert(this->rela_dyn_->output_section()
1276                   == this->rela_ifunc_->output_section());
1277     }
1278   return this->rela_ifunc_;
1279 }
1280
1281 // A class to handle the PLT data.
1282
1283 template<int size, bool big_endian>
1284 class Output_data_plt_sparc : public Output_section_data
1285 {
1286  public:
1287   typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1288                             size, big_endian> Reloc_section;
1289
1290   Output_data_plt_sparc(Layout*);
1291
1292   // Add an entry to the PLT.
1293   void add_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym);
1294
1295   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
1296   unsigned int
1297   add_local_ifunc_entry(Symbol_table*, Layout*,
1298                         Sized_relobj_file<size, big_endian>* relobj,
1299                         unsigned int local_sym_index);
1300
1301   // Return the .rela.plt section data.
1302   const Reloc_section* rel_plt() const
1303   {
1304     return this->rel_;
1305   }
1306
1307   // Return where the IFUNC relocations should go.
1308   Reloc_section*
1309   rela_ifunc(Symbol_table*, Layout*);
1310
1311   void
1312   emit_pending_ifunc_relocs();
1313
1314   // Return whether we created a section for IFUNC relocations.
1315   bool
1316   has_ifunc_section() const
1317   { return this->ifunc_rel_ != NULL; }
1318
1319   // Return the number of PLT entries.
1320   unsigned int
1321   entry_count() const
1322   { return this->count_ + this->ifunc_count_; }
1323
1324   // Return the offset of the first non-reserved PLT entry.
1325   static unsigned int
1326   first_plt_entry_offset()
1327   { return 4 * base_plt_entry_size; }
1328
1329   // Return the size of a PLT entry.
1330   static unsigned int
1331   get_plt_entry_size()
1332   { return base_plt_entry_size; }
1333
1334   // Return the PLT address to use for a global symbol.
1335   uint64_t
1336   address_for_global(const Symbol*);
1337
1338   // Return the PLT address to use for a local symbol.
1339   uint64_t
1340   address_for_local(const Relobj*, unsigned int symndx);
1341
1342  protected:
1343   void do_adjust_output_section(Output_section* os);
1344
1345   // Write to a map file.
1346   void
1347   do_print_to_mapfile(Mapfile* mapfile) const
1348   { mapfile->print_output_data(this, _("** PLT")); }
1349
1350  private:
1351   // The size of an entry in the PLT.
1352   static const int base_plt_entry_size = (size == 32 ? 12 : 32);
1353
1354   static const unsigned int plt_entries_per_block = 160;
1355   static const unsigned int plt_insn_chunk_size = 24;
1356   static const unsigned int plt_pointer_chunk_size = 8;
1357   static const unsigned int plt_block_size =
1358     (plt_entries_per_block
1359      * (plt_insn_chunk_size + plt_pointer_chunk_size));
1360
1361   section_offset_type
1362   plt_index_to_offset(unsigned int index)
1363   {
1364     section_offset_type offset;
1365
1366     if (size == 32 || index < 32768)
1367       offset = index * base_plt_entry_size;
1368     else
1369       {
1370         unsigned int ext_index = index - 32768;
1371
1372         offset = (32768 * base_plt_entry_size)
1373           + ((ext_index / plt_entries_per_block)
1374              * plt_block_size)
1375           + ((ext_index % plt_entries_per_block)
1376              * plt_insn_chunk_size);
1377       }
1378     return offset;
1379   }
1380
1381   // Set the final size.
1382   void
1383   set_final_data_size()
1384   {
1385     unsigned int full_count = this->entry_count() + 4;
1386     unsigned int extra = (size == 32 ? 4 : 0);
1387     section_offset_type sz = plt_index_to_offset(full_count) + extra;
1388
1389     return this->set_data_size(sz);
1390   }
1391
1392   // Write out the PLT data.
1393   void
1394   do_write(Output_file*);
1395
1396   struct Global_ifunc
1397   {
1398     Reloc_section* rel;
1399     Symbol* gsym;
1400     unsigned int plt_index;
1401   };
1402
1403   struct Local_ifunc
1404   {
1405     Reloc_section* rel;
1406     Sized_relobj_file<size, big_endian>* object;
1407     unsigned int local_sym_index;
1408     unsigned int plt_index;
1409   };
1410
1411   // The reloc section.
1412   Reloc_section* rel_;
1413   // The IFUNC relocations, if necessary.  These must follow the
1414   // regular relocations.
1415   Reloc_section* ifunc_rel_;
1416   // The number of PLT entries.
1417   unsigned int count_;
1418   // The number of PLT entries for IFUNC symbols.
1419   unsigned int ifunc_count_;
1420   // Global STT_GNU_IFUNC symbols.
1421   std::vector<Global_ifunc> global_ifuncs_;
1422   // Local STT_GNU_IFUNC symbols.
1423   std::vector<Local_ifunc> local_ifuncs_;
1424 };
1425
1426 // Define the constants as required by C++ standard.
1427
1428 template<int size, bool big_endian>
1429 const int Output_data_plt_sparc<size, big_endian>::base_plt_entry_size;
1430
1431 template<int size, bool big_endian>
1432 const unsigned int
1433 Output_data_plt_sparc<size, big_endian>::plt_entries_per_block;
1434
1435 template<int size, bool big_endian>
1436 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_insn_chunk_size;
1437
1438 template<int size, bool big_endian>
1439 const unsigned int
1440 Output_data_plt_sparc<size, big_endian>::plt_pointer_chunk_size;
1441
1442 template<int size, bool big_endian>
1443 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_block_size;
1444
1445 // Create the PLT section.  The ordinary .got section is an argument,
1446 // since we need to refer to the start.
1447
1448 template<int size, bool big_endian>
1449 Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
1450   : Output_section_data(size == 32 ? 4 : 8), ifunc_rel_(NULL),
1451     count_(0), ifunc_count_(0), global_ifuncs_(), local_ifuncs_()
1452 {
1453   this->rel_ = new Reloc_section(false);
1454   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1455                                   elfcpp::SHF_ALLOC, this->rel_,
1456                                   ORDER_DYNAMIC_PLT_RELOCS, false);
1457 }
1458
1459 template<int size, bool big_endian>
1460 void
1461 Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
1462 {
1463   os->set_entsize(0);
1464 }
1465
1466 // Add an entry to the PLT.
1467
1468 template<int size, bool big_endian>
1469 void
1470 Output_data_plt_sparc<size, big_endian>::add_entry(Symbol_table* symtab,
1471                                                    Layout* layout,
1472                                                    Symbol* gsym)
1473 {
1474   gold_assert(!gsym->has_plt_offset());
1475
1476   section_offset_type plt_offset;
1477   unsigned int index;
1478
1479   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1480       && gsym->can_use_relative_reloc(false))
1481     {
1482       index = this->ifunc_count_;
1483       plt_offset = plt_index_to_offset(index);
1484       gsym->set_plt_offset(plt_offset);
1485       ++this->ifunc_count_;
1486       Reloc_section* rel = this->rela_ifunc(symtab, layout);
1487
1488       struct Global_ifunc gi;
1489       gi.rel = rel;
1490       gi.gsym = gsym;
1491       gi.plt_index = index;
1492       this->global_ifuncs_.push_back(gi);
1493     }
1494   else
1495     {
1496       plt_offset = plt_index_to_offset(this->count_ + 4);
1497       gsym->set_plt_offset(plt_offset);
1498       ++this->count_;
1499       gsym->set_needs_dynsym_entry();
1500       this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
1501                              plt_offset, 0);
1502     }
1503
1504   // Note that we don't need to save the symbol.  The contents of the
1505   // PLT are independent of which symbols are used.  The symbols only
1506   // appear in the relocations.
1507 }
1508
1509 template<int size, bool big_endian>
1510 unsigned int
1511 Output_data_plt_sparc<size, big_endian>::add_local_ifunc_entry(
1512     Symbol_table* symtab,
1513     Layout* layout,
1514     Sized_relobj_file<size, big_endian>* relobj,
1515     unsigned int local_sym_index)
1516 {
1517   unsigned int index = this->ifunc_count_;
1518   section_offset_type plt_offset;
1519
1520   plt_offset = plt_index_to_offset(index);
1521   ++this->ifunc_count_;
1522
1523   Reloc_section* rel = this->rela_ifunc(symtab, layout);
1524
1525   struct Local_ifunc li;
1526   li.rel = rel;
1527   li.object = relobj;
1528   li.local_sym_index = local_sym_index;
1529   li.plt_index = index;
1530   this->local_ifuncs_.push_back(li);
1531
1532   return plt_offset;
1533 }
1534
1535 // Emit any pending IFUNC plt relocations.
1536
1537 template<int size, bool big_endian>
1538 void
1539 Output_data_plt_sparc<size, big_endian>::emit_pending_ifunc_relocs()
1540 {
1541   // Emit any pending IFUNC relocs.
1542   for (typename std::vector<Global_ifunc>::const_iterator p =
1543          this->global_ifuncs_.begin();
1544        p != this->global_ifuncs_.end();
1545        ++p)
1546     {
1547       section_offset_type plt_offset;
1548       unsigned int index;
1549
1550       index = this->count_ + p->plt_index + 4;
1551       plt_offset = this->plt_index_to_offset(index);
1552       p->rel->add_symbolless_global_addend(p->gsym, elfcpp::R_SPARC_JMP_IREL,
1553                                            this, plt_offset, 0);
1554     }
1555
1556   for (typename std::vector<Local_ifunc>::const_iterator p =
1557          this->local_ifuncs_.begin();
1558        p != this->local_ifuncs_.end();
1559        ++p)
1560     {
1561       section_offset_type plt_offset;
1562       unsigned int index;
1563
1564       index = this->count_ + p->plt_index + 4;
1565       plt_offset = this->plt_index_to_offset(index);
1566       p->rel->add_symbolless_local_addend(p->object, p->local_sym_index,
1567                                           elfcpp::R_SPARC_JMP_IREL,
1568                                           this, plt_offset, 0);
1569     }
1570 }
1571
1572 // Return where the IFUNC relocations should go in the PLT.  These
1573 // follow the non-IFUNC relocations.
1574
1575 template<int size, bool big_endian>
1576 typename Output_data_plt_sparc<size, big_endian>::Reloc_section*
1577 Output_data_plt_sparc<size, big_endian>::rela_ifunc(
1578         Symbol_table* symtab,
1579         Layout* layout)
1580 {
1581   if (this->ifunc_rel_ == NULL)
1582     {
1583       this->ifunc_rel_ = new Reloc_section(false);
1584       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1585                                       elfcpp::SHF_ALLOC, this->ifunc_rel_,
1586                                       ORDER_DYNAMIC_PLT_RELOCS, false);
1587       gold_assert(this->ifunc_rel_->output_section()
1588                   == this->rel_->output_section());
1589
1590       if (parameters->doing_static_link())
1591         {
1592           // A statically linked executable will only have a .rel.plt
1593           // section to hold R_SPARC_IRELATIVE and R_SPARC_JMP_IREL
1594           // relocs for STT_GNU_IFUNC symbols.  The library will use
1595           // these symbols to locate the IRELATIVE and JMP_IREL relocs
1596           // at program startup time.
1597           symtab->define_in_output_data("__rela_iplt_start", NULL,
1598                                         Symbol_table::PREDEFINED,
1599                                         this->ifunc_rel_, 0, 0,
1600                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1601                                         elfcpp::STV_HIDDEN, 0, false, true);
1602           symtab->define_in_output_data("__rela_iplt_end", NULL,
1603                                         Symbol_table::PREDEFINED,
1604                                         this->ifunc_rel_, 0, 0,
1605                                         elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1606                                         elfcpp::STV_HIDDEN, 0, true, true);
1607         }
1608     }
1609   return this->ifunc_rel_;
1610 }
1611
1612 // Return the PLT address to use for a global symbol.
1613
1614 template<int size, bool big_endian>
1615 uint64_t
1616 Output_data_plt_sparc<size, big_endian>::address_for_global(const Symbol* gsym)
1617 {
1618   uint64_t offset = 0;
1619   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1620       && gsym->can_use_relative_reloc(false))
1621     offset = plt_index_to_offset(this->count_ + 4);
1622   return this->address() + offset;
1623 }
1624
1625 // Return the PLT address to use for a local symbol.  These are always
1626 // IRELATIVE relocs.
1627
1628 template<int size, bool big_endian>
1629 uint64_t
1630 Output_data_plt_sparc<size, big_endian>::address_for_local(
1631         const Relobj*,
1632         unsigned int)
1633 {
1634   return this->address() + plt_index_to_offset(this->count_ + 4);
1635 }
1636
1637 static const unsigned int sparc_nop = 0x01000000;
1638 static const unsigned int sparc_sethi_g1 = 0x03000000;
1639 static const unsigned int sparc_branch_always = 0x30800000;
1640 static const unsigned int sparc_branch_always_pt = 0x30680000;
1641 static const unsigned int sparc_mov = 0x80100000;
1642 static const unsigned int sparc_mov_g0_o0 = 0x90100000;
1643 static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
1644 static const unsigned int sparc_call_plus_8 = 0x40000002;
1645 static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
1646 static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
1647 static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
1648
1649 // Write out the PLT.
1650
1651 template<int size, bool big_endian>
1652 void
1653 Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
1654 {
1655   const off_t offset = this->offset();
1656   const section_size_type oview_size =
1657     convert_to_section_size_type(this->data_size());
1658   unsigned char* const oview = of->get_output_view(offset, oview_size);
1659   unsigned char* pov = oview;
1660
1661   memset(pov, 0, base_plt_entry_size * 4);
1662   pov += this->first_plt_entry_offset();
1663
1664   unsigned int plt_offset = base_plt_entry_size * 4;
1665   const unsigned int count = this->entry_count();
1666
1667   if (size == 64)
1668     {
1669       unsigned int limit;
1670
1671       limit = (count > 32768 ? 32768 : count);
1672
1673       for (unsigned int i = 0; i < limit; ++i)
1674         {
1675           elfcpp::Swap<32, true>::writeval(pov + 0x00,
1676                                            sparc_sethi_g1 + plt_offset);
1677           elfcpp::Swap<32, true>::writeval(pov + 0x04,
1678                                            sparc_branch_always_pt +
1679                                            (((base_plt_entry_size -
1680                                               (plt_offset + 4)) >> 2) &
1681                                             0x7ffff));
1682           elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1683           elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
1684           elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
1685           elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
1686           elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
1687           elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
1688
1689           pov += base_plt_entry_size;
1690           plt_offset += base_plt_entry_size;
1691         }
1692
1693       if (count > 32768)
1694         {
1695           unsigned int ext_cnt = count - 32768;
1696           unsigned int blks = ext_cnt / plt_entries_per_block;
1697
1698           for (unsigned int i = 0; i < blks; ++i)
1699             {
1700               unsigned int data_off = (plt_entries_per_block
1701                                        * plt_insn_chunk_size) - 4;
1702
1703               for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1704                 {
1705                   elfcpp::Swap<32, true>::writeval(pov + 0x00,
1706                                                    sparc_mov_o7_g5);
1707                   elfcpp::Swap<32, true>::writeval(pov + 0x04,
1708                                                    sparc_call_plus_8);
1709                   elfcpp::Swap<32, true>::writeval(pov + 0x08,
1710                                                    sparc_nop);
1711                   elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1712                                                    sparc_ldx_o7_imm_g1 +
1713                                                    (data_off & 0x1fff));
1714                   elfcpp::Swap<32, true>::writeval(pov + 0x10,
1715                                                    sparc_jmpl_o7_g1_g1);
1716                   elfcpp::Swap<32, true>::writeval(pov + 0x14,
1717                                                    sparc_mov_g5_o7);
1718
1719                   elfcpp::Swap<64, big_endian>::writeval(
1720                                 pov + 0x4 + data_off,
1721                                 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1722
1723                   pov += plt_insn_chunk_size;
1724                   data_off -= 16;
1725                 }
1726             }
1727
1728           unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
1729           for (unsigned int i = 0; i < sub_blk_cnt; ++i)
1730             {
1731               unsigned int data_off = (sub_blk_cnt
1732                                        * plt_insn_chunk_size) - 4;
1733
1734               for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1735                 {
1736                   elfcpp::Swap<32, true>::writeval(pov + 0x00,
1737                                                    sparc_mov_o7_g5);
1738                   elfcpp::Swap<32, true>::writeval(pov + 0x04,
1739                                                    sparc_call_plus_8);
1740                   elfcpp::Swap<32, true>::writeval(pov + 0x08,
1741                                                    sparc_nop);
1742                   elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1743                                                    sparc_ldx_o7_imm_g1 +
1744                                                    (data_off & 0x1fff));
1745                   elfcpp::Swap<32, true>::writeval(pov + 0x10,
1746                                                    sparc_jmpl_o7_g1_g1);
1747                   elfcpp::Swap<32, true>::writeval(pov + 0x14,
1748                                                    sparc_mov_g5_o7);
1749
1750                   elfcpp::Swap<64, big_endian>::writeval(
1751                                 pov + 0x4 + data_off,
1752                                 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1753
1754                   pov += plt_insn_chunk_size;
1755                   data_off -= 16;
1756                 }
1757             }
1758         }
1759     }
1760   else
1761     {
1762       for (unsigned int i = 0; i < count; ++i)
1763         {
1764           elfcpp::Swap<32, true>::writeval(pov + 0x00,
1765                                            sparc_sethi_g1 + plt_offset);
1766           elfcpp::Swap<32, true>::writeval(pov + 0x04,
1767                                            sparc_branch_always +
1768                                            (((- (plt_offset + 4)) >> 2) &
1769                                             0x003fffff));
1770           elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1771
1772           pov += base_plt_entry_size;
1773           plt_offset += base_plt_entry_size;
1774         }
1775
1776       elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
1777       pov += 4;
1778     }
1779
1780   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1781
1782   of->write_output_view(offset, oview_size, oview);
1783 }
1784
1785 // Create the PLT section.
1786
1787 template<int size, bool big_endian>
1788 void
1789 Target_sparc<size, big_endian>::make_plt_section(Symbol_table* symtab,
1790                                                  Layout* layout)
1791 {
1792   // Create the GOT sections first.
1793   this->got_section(symtab, layout);
1794
1795   // Ensure that .rela.dyn always appears before .rela.plt  This is
1796   // necessary due to how, on Sparc and some other targets, .rela.dyn
1797   // needs to include .rela.plt in it's range.
1798   this->rela_dyn_section(layout);
1799
1800   this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
1801   layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1802                                   (elfcpp::SHF_ALLOC
1803                                    | elfcpp::SHF_EXECINSTR
1804                                    | elfcpp::SHF_WRITE),
1805                                   this->plt_, ORDER_NON_RELRO_FIRST, false);
1806
1807   // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1808   symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1809                                 Symbol_table::PREDEFINED,
1810                                 this->plt_,
1811                                 0, 0, elfcpp::STT_OBJECT,
1812                                 elfcpp::STB_LOCAL,
1813                                 elfcpp::STV_HIDDEN, 0,
1814                                 false, false);
1815 }
1816
1817 // Create a PLT entry for a global symbol.
1818
1819 template<int size, bool big_endian>
1820 void
1821 Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1822                                                Layout* layout,
1823                                                Symbol* gsym)
1824 {
1825   if (gsym->has_plt_offset())
1826     return;
1827
1828   if (this->plt_ == NULL)
1829     this->make_plt_section(symtab, layout);
1830
1831   this->plt_->add_entry(symtab, layout, gsym);
1832 }
1833
1834 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1835
1836 template<int size, bool big_endian>
1837 void
1838 Target_sparc<size, big_endian>::make_local_ifunc_plt_entry(
1839         Symbol_table* symtab,
1840         Layout* layout,
1841         Sized_relobj_file<size, big_endian>* relobj,
1842         unsigned int local_sym_index)
1843 {
1844   if (relobj->local_has_plt_offset(local_sym_index))
1845     return;
1846   if (this->plt_ == NULL)
1847     this->make_plt_section(symtab, layout);
1848   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1849                                                               relobj,
1850                                                               local_sym_index);
1851   relobj->set_local_plt_offset(local_sym_index, plt_offset);
1852 }
1853
1854 // Return the number of entries in the PLT.
1855
1856 template<int size, bool big_endian>
1857 unsigned int
1858 Target_sparc<size, big_endian>::plt_entry_count() const
1859 {
1860   if (this->plt_ == NULL)
1861     return 0;
1862   return this->plt_->entry_count();
1863 }
1864
1865 // Return the offset of the first non-reserved PLT entry.
1866
1867 template<int size, bool big_endian>
1868 unsigned int
1869 Target_sparc<size, big_endian>::first_plt_entry_offset() const
1870 {
1871   return Output_data_plt_sparc<size, big_endian>::first_plt_entry_offset();
1872 }
1873
1874 // Return the size of each PLT entry.
1875
1876 template<int size, bool big_endian>
1877 unsigned int
1878 Target_sparc<size, big_endian>::plt_entry_size() const
1879 {
1880   return Output_data_plt_sparc<size, big_endian>::get_plt_entry_size();
1881 }
1882
1883 // Create a GOT entry for the TLS module index.
1884
1885 template<int size, bool big_endian>
1886 unsigned int
1887 Target_sparc<size, big_endian>::got_mod_index_entry(
1888      Symbol_table* symtab,
1889      Layout* layout,
1890      Sized_relobj_file<size, big_endian>* object)
1891 {
1892   if (this->got_mod_index_offset_ == -1U)
1893     {
1894       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1895       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1896       Output_data_got<size, big_endian>* got;
1897       unsigned int got_offset;
1898
1899       got = this->got_section(symtab, layout);
1900       got_offset = got->add_constant(0);
1901       rela_dyn->add_local(object, 0,
1902                           (size == 64 ?
1903                            elfcpp::R_SPARC_TLS_DTPMOD64 :
1904                            elfcpp::R_SPARC_TLS_DTPMOD32), got,
1905                           got_offset, 0);
1906       got->add_constant(0);
1907       this->got_mod_index_offset_ = got_offset;
1908     }
1909   return this->got_mod_index_offset_;
1910 }
1911
1912 // Optimize the TLS relocation type based on what we know about the
1913 // symbol.  IS_FINAL is true if the final address of this symbol is
1914 // known at link time.
1915
1916 static tls::Tls_optimization
1917 optimize_tls_reloc(bool is_final, int r_type)
1918 {
1919   // If we are generating a shared library, then we can't do anything
1920   // in the linker.
1921   if (parameters->options().shared())
1922     return tls::TLSOPT_NONE;
1923
1924   switch (r_type)
1925     {
1926     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1927     case elfcpp::R_SPARC_TLS_GD_LO10:
1928     case elfcpp::R_SPARC_TLS_GD_ADD:
1929     case elfcpp::R_SPARC_TLS_GD_CALL:
1930       // These are General-Dynamic which permits fully general TLS
1931       // access.  Since we know that we are generating an executable,
1932       // we can convert this to Initial-Exec.  If we also know that
1933       // this is a local symbol, we can further switch to Local-Exec.
1934       if (is_final)
1935         return tls::TLSOPT_TO_LE;
1936       return tls::TLSOPT_TO_IE;
1937
1938     case elfcpp::R_SPARC_TLS_LDM_HI22:  // Local-dynamic
1939     case elfcpp::R_SPARC_TLS_LDM_LO10:
1940     case elfcpp::R_SPARC_TLS_LDM_ADD:
1941     case elfcpp::R_SPARC_TLS_LDM_CALL:
1942       // This is Local-Dynamic, which refers to a local symbol in the
1943       // dynamic TLS block.  Since we know that we generating an
1944       // executable, we can switch to Local-Exec.
1945       return tls::TLSOPT_TO_LE;
1946
1947     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1948     case elfcpp::R_SPARC_TLS_LDO_LOX10:
1949     case elfcpp::R_SPARC_TLS_LDO_ADD:
1950       // Another type of Local-Dynamic relocation.
1951       return tls::TLSOPT_TO_LE;
1952
1953     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
1954     case elfcpp::R_SPARC_TLS_IE_LO10:
1955     case elfcpp::R_SPARC_TLS_IE_LD:
1956     case elfcpp::R_SPARC_TLS_IE_LDX:
1957     case elfcpp::R_SPARC_TLS_IE_ADD:
1958       // These are Initial-Exec relocs which get the thread offset
1959       // from the GOT.  If we know that we are linking against the
1960       // local symbol, we can switch to Local-Exec, which links the
1961       // thread offset into the instruction.
1962       if (is_final)
1963         return tls::TLSOPT_TO_LE;
1964       return tls::TLSOPT_NONE;
1965
1966     case elfcpp::R_SPARC_TLS_LE_HIX22:  // Local-exec
1967     case elfcpp::R_SPARC_TLS_LE_LOX10:
1968       // When we already have Local-Exec, there is nothing further we
1969       // can do.
1970       return tls::TLSOPT_NONE;
1971
1972     default:
1973       gold_unreachable();
1974     }
1975 }
1976
1977 // Get the Reference_flags for a particular relocation.
1978
1979 template<int size, bool big_endian>
1980 int
1981 Target_sparc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
1982 {
1983   r_type &= 0xff;
1984   switch (r_type)
1985     {
1986     case elfcpp::R_SPARC_NONE:
1987     case elfcpp::R_SPARC_REGISTER:
1988     case elfcpp::R_SPARC_GNU_VTINHERIT:
1989     case elfcpp::R_SPARC_GNU_VTENTRY:
1990       // No symbol reference.
1991       return 0;
1992
1993     case elfcpp::R_SPARC_UA64:
1994     case elfcpp::R_SPARC_64:
1995     case elfcpp::R_SPARC_HIX22:
1996     case elfcpp::R_SPARC_LOX10:
1997     case elfcpp::R_SPARC_H34:
1998     case elfcpp::R_SPARC_H44:
1999     case elfcpp::R_SPARC_M44:
2000     case elfcpp::R_SPARC_L44:
2001     case elfcpp::R_SPARC_HH22:
2002     case elfcpp::R_SPARC_HM10:
2003     case elfcpp::R_SPARC_LM22:
2004     case elfcpp::R_SPARC_HI22:
2005     case elfcpp::R_SPARC_LO10:
2006     case elfcpp::R_SPARC_OLO10:
2007     case elfcpp::R_SPARC_UA32:
2008     case elfcpp::R_SPARC_32:
2009     case elfcpp::R_SPARC_UA16:
2010     case elfcpp::R_SPARC_16:
2011     case elfcpp::R_SPARC_11:
2012     case elfcpp::R_SPARC_10:
2013     case elfcpp::R_SPARC_8:
2014     case elfcpp::R_SPARC_7:
2015     case elfcpp::R_SPARC_6:
2016     case elfcpp::R_SPARC_5:
2017       return Symbol::ABSOLUTE_REF;
2018
2019     case elfcpp::R_SPARC_DISP8:
2020     case elfcpp::R_SPARC_DISP16:
2021     case elfcpp::R_SPARC_DISP32:
2022     case elfcpp::R_SPARC_DISP64:
2023     case elfcpp::R_SPARC_PC_HH22:
2024     case elfcpp::R_SPARC_PC_HM10:
2025     case elfcpp::R_SPARC_PC_LM22:
2026     case elfcpp::R_SPARC_PC10:
2027     case elfcpp::R_SPARC_PC22:
2028     case elfcpp::R_SPARC_WDISP30:
2029     case elfcpp::R_SPARC_WDISP22:
2030     case elfcpp::R_SPARC_WDISP19:
2031     case elfcpp::R_SPARC_WDISP16:
2032     case elfcpp::R_SPARC_WDISP10:
2033       return Symbol::RELATIVE_REF;
2034
2035     case elfcpp::R_SPARC_PLT64:
2036     case elfcpp::R_SPARC_PLT32:
2037     case elfcpp::R_SPARC_HIPLT22:
2038     case elfcpp::R_SPARC_LOPLT10:
2039     case elfcpp::R_SPARC_PCPLT10:
2040       return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
2041
2042     case elfcpp::R_SPARC_PCPLT32:
2043     case elfcpp::R_SPARC_PCPLT22:
2044     case elfcpp::R_SPARC_WPLT30:
2045       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
2046
2047     case elfcpp::R_SPARC_GOTDATA_OP:
2048     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2049     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2050     case elfcpp::R_SPARC_GOT10:
2051     case elfcpp::R_SPARC_GOT13:
2052     case elfcpp::R_SPARC_GOT22:
2053       // Absolute in GOT.
2054       return Symbol::ABSOLUTE_REF;
2055
2056     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2057     case elfcpp::R_SPARC_TLS_GD_LO10:
2058     case elfcpp::R_SPARC_TLS_GD_ADD:
2059     case elfcpp::R_SPARC_TLS_GD_CALL:
2060     case elfcpp::R_SPARC_TLS_LDM_HI22:  // Local-dynamic
2061     case elfcpp::R_SPARC_TLS_LDM_LO10:
2062     case elfcpp::R_SPARC_TLS_LDM_ADD:
2063     case elfcpp::R_SPARC_TLS_LDM_CALL:
2064     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2065     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2066     case elfcpp::R_SPARC_TLS_LDO_ADD:
2067     case elfcpp::R_SPARC_TLS_LE_HIX22:
2068     case elfcpp::R_SPARC_TLS_LE_LOX10:
2069     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
2070     case elfcpp::R_SPARC_TLS_IE_LO10:
2071     case elfcpp::R_SPARC_TLS_IE_LD:
2072     case elfcpp::R_SPARC_TLS_IE_LDX:
2073     case elfcpp::R_SPARC_TLS_IE_ADD:
2074       return Symbol::TLS_REF;
2075
2076     case elfcpp::R_SPARC_COPY:
2077     case elfcpp::R_SPARC_GLOB_DAT:
2078     case elfcpp::R_SPARC_JMP_SLOT:
2079     case elfcpp::R_SPARC_JMP_IREL:
2080     case elfcpp::R_SPARC_RELATIVE:
2081     case elfcpp::R_SPARC_IRELATIVE:
2082     case elfcpp::R_SPARC_TLS_DTPMOD64:
2083     case elfcpp::R_SPARC_TLS_DTPMOD32:
2084     case elfcpp::R_SPARC_TLS_DTPOFF64:
2085     case elfcpp::R_SPARC_TLS_DTPOFF32:
2086     case elfcpp::R_SPARC_TLS_TPOFF64:
2087     case elfcpp::R_SPARC_TLS_TPOFF32:
2088     default:
2089       // Not expected.  We will give an error later.
2090       return 0;
2091     }
2092 }
2093
2094 // Generate a PLT entry slot for a call to __tls_get_addr
2095 template<int size, bool big_endian>
2096 void
2097 Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
2098                                                         Layout* layout,
2099                                                         Target_sparc<size, big_endian>* target)
2100 {
2101   Symbol* gsym = target->tls_get_addr_sym(symtab);
2102
2103   target->make_plt_entry(symtab, layout, gsym);
2104 }
2105
2106 // Report an unsupported relocation against a local symbol.
2107
2108 template<int size, bool big_endian>
2109 void
2110 Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
2111                         Sized_relobj_file<size, big_endian>* object,
2112                         unsigned int r_type)
2113 {
2114   gold_error(_("%s: unsupported reloc %u against local symbol"),
2115              object->name().c_str(), r_type);
2116 }
2117
2118 // We are about to emit a dynamic relocation of type R_TYPE.  If the
2119 // dynamic linker does not support it, issue an error.
2120
2121 template<int size, bool big_endian>
2122 void
2123 Target_sparc<size, big_endian>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
2124 {
2125   gold_assert(r_type != elfcpp::R_SPARC_NONE);
2126
2127   if (size == 64)
2128     {
2129       switch (r_type)
2130         {
2131           // These are the relocation types supported by glibc for sparc 64-bit.
2132         case elfcpp::R_SPARC_RELATIVE:
2133         case elfcpp::R_SPARC_IRELATIVE:
2134         case elfcpp::R_SPARC_COPY:
2135         case elfcpp::R_SPARC_64:
2136         case elfcpp::R_SPARC_GLOB_DAT:
2137         case elfcpp::R_SPARC_JMP_SLOT:
2138         case elfcpp::R_SPARC_JMP_IREL:
2139         case elfcpp::R_SPARC_TLS_DTPMOD64:
2140         case elfcpp::R_SPARC_TLS_DTPOFF64:
2141         case elfcpp::R_SPARC_TLS_TPOFF64:
2142         case elfcpp::R_SPARC_TLS_LE_HIX22:
2143         case elfcpp::R_SPARC_TLS_LE_LOX10:
2144         case elfcpp::R_SPARC_8:
2145         case elfcpp::R_SPARC_16:
2146         case elfcpp::R_SPARC_DISP8:
2147         case elfcpp::R_SPARC_DISP16:
2148         case elfcpp::R_SPARC_DISP32:
2149         case elfcpp::R_SPARC_WDISP30:
2150         case elfcpp::R_SPARC_LO10:
2151         case elfcpp::R_SPARC_HI22:
2152         case elfcpp::R_SPARC_OLO10:
2153         case elfcpp::R_SPARC_H34:
2154         case elfcpp::R_SPARC_H44:
2155         case elfcpp::R_SPARC_M44:
2156         case elfcpp::R_SPARC_L44:
2157         case elfcpp::R_SPARC_HH22:
2158         case elfcpp::R_SPARC_HM10:
2159         case elfcpp::R_SPARC_LM22:
2160         case elfcpp::R_SPARC_UA16:
2161         case elfcpp::R_SPARC_UA32:
2162         case elfcpp::R_SPARC_UA64:
2163           return;
2164
2165         default:
2166           break;
2167         }
2168     }
2169   else
2170     {
2171       switch (r_type)
2172         {
2173           // These are the relocation types supported by glibc for sparc 32-bit.
2174         case elfcpp::R_SPARC_RELATIVE:
2175         case elfcpp::R_SPARC_IRELATIVE:
2176         case elfcpp::R_SPARC_COPY:
2177         case elfcpp::R_SPARC_GLOB_DAT:
2178         case elfcpp::R_SPARC_32:
2179         case elfcpp::R_SPARC_JMP_SLOT:
2180         case elfcpp::R_SPARC_JMP_IREL:
2181         case elfcpp::R_SPARC_TLS_DTPMOD32:
2182         case elfcpp::R_SPARC_TLS_DTPOFF32:
2183         case elfcpp::R_SPARC_TLS_TPOFF32:
2184         case elfcpp::R_SPARC_TLS_LE_HIX22:
2185         case elfcpp::R_SPARC_TLS_LE_LOX10:
2186         case elfcpp::R_SPARC_8:
2187         case elfcpp::R_SPARC_16:
2188         case elfcpp::R_SPARC_DISP8:
2189         case elfcpp::R_SPARC_DISP16:
2190         case elfcpp::R_SPARC_DISP32:
2191         case elfcpp::R_SPARC_LO10:
2192         case elfcpp::R_SPARC_WDISP30:
2193         case elfcpp::R_SPARC_HI22:
2194         case elfcpp::R_SPARC_UA16:
2195         case elfcpp::R_SPARC_UA32:
2196           return;
2197
2198         default:
2199           break;
2200         }
2201     }
2202
2203   // This prevents us from issuing more than one error per reloc
2204   // section.  But we can still wind up issuing more than one
2205   // error per object file.
2206   if (this->issued_non_pic_error_)
2207     return;
2208   gold_assert(parameters->options().output_is_position_independent());
2209   object->error(_("requires unsupported dynamic reloc; "
2210                   "recompile with -fPIC"));
2211   this->issued_non_pic_error_ = true;
2212   return;
2213 }
2214
2215 // Return whether we need to make a PLT entry for a relocation of the
2216 // given type against a STT_GNU_IFUNC symbol.
2217
2218 template<int size, bool big_endian>
2219 bool
2220 Target_sparc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
2221      Sized_relobj_file<size, big_endian>* object,
2222      unsigned int r_type)
2223 {
2224   int flags = Scan::get_reference_flags(r_type);
2225   if (flags & Symbol::TLS_REF)
2226     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2227                object->name().c_str(), r_type);
2228   return flags != 0;
2229 }
2230
2231 // Scan a relocation for a local symbol.
2232
2233 template<int size, bool big_endian>
2234 inline void
2235 Target_sparc<size, big_endian>::Scan::local(
2236                         Symbol_table* symtab,
2237                         Layout* layout,
2238                         Target_sparc<size, big_endian>* target,
2239                         Sized_relobj_file<size, big_endian>* object,
2240                         unsigned int data_shndx,
2241                         Output_section* output_section,
2242                         const elfcpp::Rela<size, big_endian>& reloc,
2243                         unsigned int r_type,
2244                         const elfcpp::Sym<size, big_endian>& lsym)
2245 {
2246   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
2247   unsigned int orig_r_type = r_type;
2248   r_type &= 0xff;
2249
2250   if (is_ifunc
2251       && this->reloc_needs_plt_for_ifunc(object, r_type))
2252     {
2253       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2254       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
2255     }
2256
2257   switch (r_type)
2258     {
2259     case elfcpp::R_SPARC_NONE:
2260     case elfcpp::R_SPARC_REGISTER:
2261     case elfcpp::R_SPARC_GNU_VTINHERIT:
2262     case elfcpp::R_SPARC_GNU_VTENTRY:
2263       break;
2264
2265     case elfcpp::R_SPARC_64:
2266     case elfcpp::R_SPARC_32:
2267       // If building a shared library (or a position-independent
2268       // executable), we need to create a dynamic relocation for
2269       // this location. The relocation applied at link time will
2270       // apply the link-time value, so we flag the location with
2271       // an R_SPARC_RELATIVE relocation so the dynamic loader can
2272       // relocate it easily.
2273       if (parameters->options().output_is_position_independent())
2274         {
2275           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2276           unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2277           rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
2278                                        output_section, data_shndx,
2279                                        reloc.get_r_offset(),
2280                                        reloc.get_r_addend(), is_ifunc);
2281         }
2282       break;
2283
2284     case elfcpp::R_SPARC_HIX22:
2285     case elfcpp::R_SPARC_LOX10:
2286     case elfcpp::R_SPARC_H34:
2287     case elfcpp::R_SPARC_H44:
2288     case elfcpp::R_SPARC_M44:
2289     case elfcpp::R_SPARC_L44:
2290     case elfcpp::R_SPARC_HH22:
2291     case elfcpp::R_SPARC_HM10:
2292     case elfcpp::R_SPARC_LM22:
2293     case elfcpp::R_SPARC_UA64:
2294     case elfcpp::R_SPARC_UA32:
2295     case elfcpp::R_SPARC_UA16:
2296     case elfcpp::R_SPARC_HI22:
2297     case elfcpp::R_SPARC_LO10:
2298     case elfcpp::R_SPARC_OLO10:
2299     case elfcpp::R_SPARC_16:
2300     case elfcpp::R_SPARC_11:
2301     case elfcpp::R_SPARC_10:
2302     case elfcpp::R_SPARC_8:
2303     case elfcpp::R_SPARC_7:
2304     case elfcpp::R_SPARC_6:
2305     case elfcpp::R_SPARC_5:
2306       // If building a shared library (or a position-independent
2307       // executable), we need to create a dynamic relocation for
2308       // this location.
2309       if (parameters->options().output_is_position_independent())
2310         {
2311           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2312           unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2313
2314           check_non_pic(object, r_type);
2315           if (lsym.get_st_type() != elfcpp::STT_SECTION)
2316             {
2317               rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
2318                                   data_shndx, reloc.get_r_offset(),
2319                                   reloc.get_r_addend());
2320             }
2321           else
2322             {
2323               gold_assert(lsym.get_st_value() == 0);
2324               rela_dyn->add_symbolless_local_addend(object, r_sym, orig_r_type,
2325                                                     output_section, data_shndx,
2326                                                     reloc.get_r_offset(),
2327                                                     reloc.get_r_addend());
2328             }
2329         }
2330       break;
2331
2332     case elfcpp::R_SPARC_WDISP30:
2333     case elfcpp::R_SPARC_WPLT30:
2334     case elfcpp::R_SPARC_WDISP22:
2335     case elfcpp::R_SPARC_WDISP19:
2336     case elfcpp::R_SPARC_WDISP16:
2337     case elfcpp::R_SPARC_WDISP10:
2338     case elfcpp::R_SPARC_DISP8:
2339     case elfcpp::R_SPARC_DISP16:
2340     case elfcpp::R_SPARC_DISP32:
2341     case elfcpp::R_SPARC_DISP64:
2342     case elfcpp::R_SPARC_PC10:
2343     case elfcpp::R_SPARC_PC22:
2344       break;
2345
2346     case elfcpp::R_SPARC_GOTDATA_OP:
2347     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2348     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2349       // We will optimize this into a GOT relative relocation
2350       // and code transform the GOT load into an addition.
2351       break;
2352
2353     case elfcpp::R_SPARC_GOT10:
2354     case elfcpp::R_SPARC_GOT13:
2355     case elfcpp::R_SPARC_GOT22:
2356       {
2357         // The symbol requires a GOT entry.
2358         Output_data_got<size, big_endian>* got;
2359         unsigned int r_sym;
2360
2361         got = target->got_section(symtab, layout);
2362         r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2363
2364         // If we are generating a shared object, we need to add a
2365         // dynamic relocation for this symbol's GOT entry.
2366         if (parameters->options().output_is_position_independent())
2367           {
2368             if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
2369               {
2370                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2371                 unsigned int off = got->add_constant(0);
2372                 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
2373                 rela_dyn->add_local_relative(object, r_sym,
2374                                              elfcpp::R_SPARC_RELATIVE,
2375                                              got, off, 0, is_ifunc);
2376               }
2377           }
2378         else
2379           got->add_local(object, r_sym, GOT_TYPE_STANDARD);
2380       }
2381       break;
2382
2383       // These are initial TLS relocs, which are expected when
2384       // linking.
2385     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2386     case elfcpp::R_SPARC_TLS_GD_LO10:
2387     case elfcpp::R_SPARC_TLS_GD_ADD:
2388     case elfcpp::R_SPARC_TLS_GD_CALL:
2389     case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
2390     case elfcpp::R_SPARC_TLS_LDM_LO10:
2391     case elfcpp::R_SPARC_TLS_LDM_ADD:
2392     case elfcpp::R_SPARC_TLS_LDM_CALL:
2393     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2394     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2395     case elfcpp::R_SPARC_TLS_LDO_ADD:
2396     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
2397     case elfcpp::R_SPARC_TLS_IE_LO10:
2398     case elfcpp::R_SPARC_TLS_IE_LD:
2399     case elfcpp::R_SPARC_TLS_IE_LDX:
2400     case elfcpp::R_SPARC_TLS_IE_ADD:
2401     case elfcpp::R_SPARC_TLS_LE_HIX22:  // Local-exec
2402     case elfcpp::R_SPARC_TLS_LE_LOX10:
2403       {
2404         bool output_is_shared = parameters->options().shared();
2405         const tls::Tls_optimization optimized_type
2406             = optimize_tls_reloc(!output_is_shared, r_type);
2407         switch (r_type)
2408           {
2409           case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2410           case elfcpp::R_SPARC_TLS_GD_LO10:
2411           case elfcpp::R_SPARC_TLS_GD_ADD:
2412           case elfcpp::R_SPARC_TLS_GD_CALL:
2413             if (optimized_type == tls::TLSOPT_NONE)
2414               {
2415                 // Create a pair of GOT entries for the module index and
2416                 // dtv-relative offset.
2417                 Output_data_got<size, big_endian>* got
2418                     = target->got_section(symtab, layout);
2419                 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2420                 unsigned int shndx = lsym.get_st_shndx();
2421                 bool is_ordinary;
2422                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
2423                 if (!is_ordinary)
2424                   object->error(_("local symbol %u has bad shndx %u"),
2425                                 r_sym, shndx);
2426                 else
2427                   got->add_local_pair_with_rel(object, r_sym,
2428                                                lsym.get_st_shndx(),
2429                                                GOT_TYPE_TLS_PAIR,
2430                                                target->rela_dyn_section(layout),
2431                                                (size == 64
2432                                                 ? elfcpp::R_SPARC_TLS_DTPMOD64
2433                                                 : elfcpp::R_SPARC_TLS_DTPMOD32),
2434                                                0);
2435                 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2436                   generate_tls_call(symtab, layout, target);
2437               }
2438             else if (optimized_type != tls::TLSOPT_TO_LE)
2439               unsupported_reloc_local(object, r_type);
2440             break;
2441
2442           case elfcpp::R_SPARC_TLS_LDM_HI22 :   // Local-dynamic
2443           case elfcpp::R_SPARC_TLS_LDM_LO10:
2444           case elfcpp::R_SPARC_TLS_LDM_ADD:
2445           case elfcpp::R_SPARC_TLS_LDM_CALL:
2446             if (optimized_type == tls::TLSOPT_NONE)
2447               {
2448                 // Create a GOT entry for the module index.
2449                 target->got_mod_index_entry(symtab, layout, object);
2450
2451                 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2452                   generate_tls_call(symtab, layout, target);
2453               }
2454             else if (optimized_type != tls::TLSOPT_TO_LE)
2455               unsupported_reloc_local(object, r_type);
2456             break;
2457
2458           case elfcpp::R_SPARC_TLS_LDO_HIX22:   // Alternate local-dynamic
2459           case elfcpp::R_SPARC_TLS_LDO_LOX10:
2460           case elfcpp::R_SPARC_TLS_LDO_ADD:
2461             break;
2462
2463           case elfcpp::R_SPARC_TLS_IE_HI22:     // Initial-exec
2464           case elfcpp::R_SPARC_TLS_IE_LO10:
2465           case elfcpp::R_SPARC_TLS_IE_LD:
2466           case elfcpp::R_SPARC_TLS_IE_LDX:
2467           case elfcpp::R_SPARC_TLS_IE_ADD:
2468             layout->set_has_static_tls();
2469             if (optimized_type == tls::TLSOPT_NONE)
2470               {
2471                 // Create a GOT entry for the tp-relative offset.
2472                 Output_data_got<size, big_endian>* got
2473                   = target->got_section(symtab, layout);
2474                 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2475
2476                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
2477                   {
2478                     Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2479                     unsigned int off = got->add_constant(0);
2480
2481                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET, off);
2482
2483                     rela_dyn->add_symbolless_local_addend(object, r_sym,
2484                                                           (size == 64 ?
2485                                                            elfcpp::R_SPARC_TLS_TPOFF64 :
2486                                                            elfcpp::R_SPARC_TLS_TPOFF32),
2487                                                           got, off, 0);
2488                   }
2489               }
2490             else if (optimized_type != tls::TLSOPT_TO_LE)
2491               unsupported_reloc_local(object, r_type);
2492             break;
2493
2494           case elfcpp::R_SPARC_TLS_LE_HIX22:    // Local-exec
2495           case elfcpp::R_SPARC_TLS_LE_LOX10:
2496             layout->set_has_static_tls();
2497             if (output_is_shared)
2498               {
2499                 // We need to create a dynamic relocation.
2500                 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2501                 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2502                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2503                 rela_dyn->add_symbolless_local_addend(object, r_sym, r_type,
2504                                                       output_section, data_shndx,
2505                                                       reloc.get_r_offset(), 0);
2506               }
2507             break;
2508           }
2509       }
2510       break;
2511
2512       // These are relocations which should only be seen by the
2513       // dynamic linker, and should never be seen here.
2514     case elfcpp::R_SPARC_COPY:
2515     case elfcpp::R_SPARC_GLOB_DAT:
2516     case elfcpp::R_SPARC_JMP_SLOT:
2517     case elfcpp::R_SPARC_JMP_IREL:
2518     case elfcpp::R_SPARC_RELATIVE:
2519     case elfcpp::R_SPARC_IRELATIVE:
2520     case elfcpp::R_SPARC_TLS_DTPMOD64:
2521     case elfcpp::R_SPARC_TLS_DTPMOD32:
2522     case elfcpp::R_SPARC_TLS_DTPOFF64:
2523     case elfcpp::R_SPARC_TLS_DTPOFF32:
2524     case elfcpp::R_SPARC_TLS_TPOFF64:
2525     case elfcpp::R_SPARC_TLS_TPOFF32:
2526       gold_error(_("%s: unexpected reloc %u in object file"),
2527                  object->name().c_str(), r_type);
2528       break;
2529
2530     default:
2531       unsupported_reloc_local(object, r_type);
2532       break;
2533     }
2534 }
2535
2536 // Report an unsupported relocation against a global symbol.
2537
2538 template<int size, bool big_endian>
2539 void
2540 Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
2541                         Sized_relobj_file<size, big_endian>* object,
2542                         unsigned int r_type,
2543                         Symbol* gsym)
2544 {
2545   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2546              object->name().c_str(), r_type, gsym->demangled_name().c_str());
2547 }
2548
2549 // Scan a relocation for a global symbol.
2550
2551 template<int size, bool big_endian>
2552 inline void
2553 Target_sparc<size, big_endian>::Scan::global(
2554                                 Symbol_table* symtab,
2555                                 Layout* layout,
2556                                 Target_sparc<size, big_endian>* target,
2557                                 Sized_relobj_file<size, big_endian>* object,
2558                                 unsigned int data_shndx,
2559                                 Output_section* output_section,
2560                                 const elfcpp::Rela<size, big_endian>& reloc,
2561                                 unsigned int r_type,
2562                                 Symbol* gsym)
2563 {
2564   unsigned int orig_r_type = r_type;
2565   bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
2566
2567   // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
2568   // section.  We check here to avoid creating a dynamic reloc against
2569   // _GLOBAL_OFFSET_TABLE_.
2570   if (!target->has_got_section()
2571       && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
2572     target->got_section(symtab, layout);
2573
2574   r_type &= 0xff;
2575
2576   // A STT_GNU_IFUNC symbol may require a PLT entry.
2577   if (is_ifunc
2578       && this->reloc_needs_plt_for_ifunc(object, r_type))
2579     target->make_plt_entry(symtab, layout, gsym);
2580
2581   switch (r_type)
2582     {
2583     case elfcpp::R_SPARC_NONE:
2584     case elfcpp::R_SPARC_REGISTER:
2585     case elfcpp::R_SPARC_GNU_VTINHERIT:
2586     case elfcpp::R_SPARC_GNU_VTENTRY:
2587       break;
2588
2589     case elfcpp::R_SPARC_PLT64:
2590     case elfcpp::R_SPARC_PLT32:
2591     case elfcpp::R_SPARC_HIPLT22:
2592     case elfcpp::R_SPARC_LOPLT10:
2593     case elfcpp::R_SPARC_PCPLT32:
2594     case elfcpp::R_SPARC_PCPLT22:
2595     case elfcpp::R_SPARC_PCPLT10:
2596     case elfcpp::R_SPARC_WPLT30:
2597       // If the symbol is fully resolved, this is just a PC32 reloc.
2598       // Otherwise we need a PLT entry.
2599       if (gsym->final_value_is_known())
2600         break;
2601       // If building a shared library, we can also skip the PLT entry
2602       // if the symbol is defined in the output file and is protected
2603       // or hidden.
2604       if (gsym->is_defined()
2605           && !gsym->is_from_dynobj()
2606           && !gsym->is_preemptible())
2607         break;
2608       target->make_plt_entry(symtab, layout, gsym);
2609       break;
2610
2611     case elfcpp::R_SPARC_DISP8:
2612     case elfcpp::R_SPARC_DISP16:
2613     case elfcpp::R_SPARC_DISP32:
2614     case elfcpp::R_SPARC_DISP64:
2615     case elfcpp::R_SPARC_PC_HH22:
2616     case elfcpp::R_SPARC_PC_HM10:
2617     case elfcpp::R_SPARC_PC_LM22:
2618     case elfcpp::R_SPARC_PC10:
2619     case elfcpp::R_SPARC_PC22:
2620     case elfcpp::R_SPARC_WDISP30:
2621     case elfcpp::R_SPARC_WDISP22:
2622     case elfcpp::R_SPARC_WDISP19:
2623     case elfcpp::R_SPARC_WDISP16:
2624     case elfcpp::R_SPARC_WDISP10:
2625       {
2626         if (gsym->needs_plt_entry())
2627           target->make_plt_entry(symtab, layout, gsym);
2628         // Make a dynamic relocation if necessary.
2629         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2630           {
2631             if (gsym->may_need_copy_reloc())
2632               {
2633                 target->copy_reloc(symtab, layout, object,
2634                                    data_shndx, output_section, gsym,
2635                                    reloc);
2636               }
2637             else
2638               {
2639                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2640                 check_non_pic(object, r_type);
2641                 rela_dyn->add_global(gsym, orig_r_type, output_section, object,
2642                                      data_shndx, reloc.get_r_offset(),
2643                                      reloc.get_r_addend());
2644               }
2645           }
2646       }
2647       break;
2648
2649     case elfcpp::R_SPARC_UA64:
2650     case elfcpp::R_SPARC_64:
2651     case elfcpp::R_SPARC_HIX22:
2652     case elfcpp::R_SPARC_LOX10:
2653     case elfcpp::R_SPARC_H34:
2654     case elfcpp::R_SPARC_H44:
2655     case elfcpp::R_SPARC_M44:
2656     case elfcpp::R_SPARC_L44:
2657     case elfcpp::R_SPARC_HH22:
2658     case elfcpp::R_SPARC_HM10:
2659     case elfcpp::R_SPARC_LM22:
2660     case elfcpp::R_SPARC_HI22:
2661     case elfcpp::R_SPARC_LO10:
2662     case elfcpp::R_SPARC_OLO10:
2663     case elfcpp::R_SPARC_UA32:
2664     case elfcpp::R_SPARC_32:
2665     case elfcpp::R_SPARC_UA16:
2666     case elfcpp::R_SPARC_16:
2667     case elfcpp::R_SPARC_11:
2668     case elfcpp::R_SPARC_10:
2669     case elfcpp::R_SPARC_8:
2670     case elfcpp::R_SPARC_7:
2671     case elfcpp::R_SPARC_6:
2672     case elfcpp::R_SPARC_5:
2673       {
2674         // Make a PLT entry if necessary.
2675         if (gsym->needs_plt_entry())
2676           {
2677             target->make_plt_entry(symtab, layout, gsym);
2678             // Since this is not a PC-relative relocation, we may be
2679             // taking the address of a function. In that case we need to
2680             // set the entry in the dynamic symbol table to the address of
2681             // the PLT entry.
2682             if (gsym->is_from_dynobj() && !parameters->options().shared())
2683               gsym->set_needs_dynsym_value();
2684           }
2685         // Make a dynamic relocation if necessary.
2686         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2687           {
2688             unsigned int r_off = reloc.get_r_offset();
2689
2690             // The assembler can sometimes emit unaligned relocations
2691             // for dwarf2 cfi directives.
2692             switch (r_type)
2693               {
2694               case elfcpp::R_SPARC_16:
2695                 if (r_off & 0x1)
2696                   orig_r_type = r_type = elfcpp::R_SPARC_UA16;
2697                 break;
2698               case elfcpp::R_SPARC_32:
2699                 if (r_off & 0x3)
2700                   orig_r_type = r_type = elfcpp::R_SPARC_UA32;
2701                 break;
2702               case elfcpp::R_SPARC_64:
2703                 if (r_off & 0x7)
2704                   orig_r_type = r_type = elfcpp::R_SPARC_UA64;
2705                 break;
2706               case elfcpp::R_SPARC_UA16:
2707                 if (!(r_off & 0x1))
2708                   orig_r_type = r_type = elfcpp::R_SPARC_16;
2709                 break;
2710               case elfcpp::R_SPARC_UA32:
2711                 if (!(r_off & 0x3))
2712                   orig_r_type = r_type = elfcpp::R_SPARC_32;
2713                 break;
2714               case elfcpp::R_SPARC_UA64:
2715                 if (!(r_off & 0x7))
2716                   orig_r_type = r_type = elfcpp::R_SPARC_64;
2717                 break;
2718               }
2719
2720             if (gsym->may_need_copy_reloc())
2721               {
2722                 target->copy_reloc(symtab, layout, object,
2723                                    data_shndx, output_section, gsym, reloc);
2724               }
2725             else if (((size == 64 && r_type == elfcpp::R_SPARC_64)
2726                       || (size == 32 && r_type == elfcpp::R_SPARC_32))
2727                      && gsym->type() == elfcpp::STT_GNU_IFUNC
2728                      && gsym->can_use_relative_reloc(false)
2729                      && !gsym->is_from_dynobj()
2730                      && !gsym->is_undefined()
2731                      && !gsym->is_preemptible())
2732               {
2733                 // Use an IRELATIVE reloc for a locally defined
2734                 // STT_GNU_IFUNC symbol.  This makes a function
2735                 // address in a PIE executable match the address in a
2736                 // shared library that it links against.
2737                 Reloc_section* rela_dyn =
2738                   target->rela_ifunc_section(layout);
2739                 unsigned int r_type = elfcpp::R_SPARC_IRELATIVE;
2740                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2741                                                        output_section, object,
2742                                                        data_shndx,
2743                                                        reloc.get_r_offset(),
2744                                                        reloc.get_r_addend());
2745               }
2746             else if ((r_type == elfcpp::R_SPARC_32
2747                       || r_type == elfcpp::R_SPARC_64)
2748                      && gsym->can_use_relative_reloc(false))
2749               {
2750                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2751                 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2752                                               output_section, object,
2753                                               data_shndx, reloc.get_r_offset(),
2754                                               reloc.get_r_addend(), is_ifunc);
2755               }
2756             else
2757               {
2758                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2759
2760                 check_non_pic(object, r_type);
2761                 if (gsym->is_from_dynobj()
2762                     || gsym->is_undefined()
2763                     || gsym->is_preemptible())
2764                   rela_dyn->add_global(gsym, orig_r_type, output_section,
2765                                        object, data_shndx,
2766                                        reloc.get_r_offset(),
2767                                        reloc.get_r_addend());
2768                 else
2769                   rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
2770                                                          output_section,
2771                                                          object, data_shndx,
2772                                                          reloc.get_r_offset(),
2773                                                          reloc.get_r_addend());
2774               }
2775           }
2776       }
2777       break;
2778
2779     case elfcpp::R_SPARC_GOTDATA_OP:
2780     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2781     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2782       if (gsym->is_defined()
2783           && !gsym->is_from_dynobj()
2784           && !gsym->is_preemptible()
2785           && !is_ifunc)
2786         {
2787           // We will optimize this into a GOT relative relocation
2788           // and code transform the GOT load into an addition.
2789           break;
2790         }
2791     case elfcpp::R_SPARC_GOT10:
2792     case elfcpp::R_SPARC_GOT13:
2793     case elfcpp::R_SPARC_GOT22:
2794       {
2795         // The symbol requires a GOT entry.
2796         Output_data_got<size, big_endian>* got;
2797
2798         got = target->got_section(symtab, layout);
2799         if (gsym->final_value_is_known())
2800           {
2801             // For a STT_GNU_IFUNC symbol we want the PLT address.
2802             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2803               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2804             else
2805               got->add_global(gsym, GOT_TYPE_STANDARD);
2806           }
2807         else
2808           {
2809             // If this symbol is not fully resolved, we need to add a
2810             // GOT entry with a dynamic relocation.
2811             bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
2812
2813             // Use a GLOB_DAT rather than a RELATIVE reloc if:
2814             //
2815             // 1) The symbol may be defined in some other module.
2816             //
2817             // 2) We are building a shared library and this is a
2818             // protected symbol; using GLOB_DAT means that the dynamic
2819             // linker can use the address of the PLT in the main
2820             // executable when appropriate so that function address
2821             // comparisons work.
2822             //
2823             // 3) This is a STT_GNU_IFUNC symbol in position dependent
2824             // code, again so that function address comparisons work.
2825             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2826             if (gsym->is_from_dynobj()
2827                 || gsym->is_undefined()
2828                 || gsym->is_preemptible()
2829                 || (gsym->visibility() == elfcpp::STV_PROTECTED
2830                     && parameters->options().shared())
2831                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2832                     && parameters->options().output_is_position_independent()
2833                     && !gsym->is_forced_local()))
2834               {
2835                 unsigned int r_type = elfcpp::R_SPARC_GLOB_DAT;
2836
2837                 // If this symbol is forced local, this relocation will
2838                 // not work properly.  That's because ld.so on sparc
2839                 // (and 32-bit powerpc) expects st_value in the r_addend
2840                 // of relocations for STB_LOCAL symbols.  Curiously the
2841                 // BFD linker does not promote global hidden symbols to be
2842                 // STB_LOCAL in the dynamic symbol table like Gold does.
2843                 gold_assert(!gsym->is_forced_local());
2844                 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
2845                                          r_type);
2846               }
2847             else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2848               {
2849                 unsigned int off = got->add_constant(0);
2850
2851                 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2852                 if (is_ifunc)
2853                   {
2854                     // Tell the dynamic linker to use the PLT address
2855                     // when resolving relocations.
2856                     if (gsym->is_from_dynobj()
2857                         && !parameters->options().shared())
2858                       gsym->set_needs_dynsym_value();
2859                   }
2860                 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2861                                               got, off, 0, is_ifunc);
2862               }
2863           }
2864       }
2865       break;
2866
2867       // These are initial tls relocs, which are expected when
2868       // linking.
2869     case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2870     case elfcpp::R_SPARC_TLS_GD_LO10:
2871     case elfcpp::R_SPARC_TLS_GD_ADD:
2872     case elfcpp::R_SPARC_TLS_GD_CALL:
2873     case elfcpp::R_SPARC_TLS_LDM_HI22:  // Local-dynamic
2874     case elfcpp::R_SPARC_TLS_LDM_LO10:
2875     case elfcpp::R_SPARC_TLS_LDM_ADD:
2876     case elfcpp::R_SPARC_TLS_LDM_CALL:
2877     case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2878     case elfcpp::R_SPARC_TLS_LDO_LOX10:
2879     case elfcpp::R_SPARC_TLS_LDO_ADD:
2880     case elfcpp::R_SPARC_TLS_LE_HIX22:
2881     case elfcpp::R_SPARC_TLS_LE_LOX10:
2882     case elfcpp::R_SPARC_TLS_IE_HI22:   // Initial-exec
2883     case elfcpp::R_SPARC_TLS_IE_LO10:
2884     case elfcpp::R_SPARC_TLS_IE_LD:
2885     case elfcpp::R_SPARC_TLS_IE_LDX:
2886     case elfcpp::R_SPARC_TLS_IE_ADD:
2887       {
2888         const bool is_final = gsym->final_value_is_known();
2889         const tls::Tls_optimization optimized_type
2890             = optimize_tls_reloc(is_final, r_type);
2891         switch (r_type)
2892           {
2893           case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2894           case elfcpp::R_SPARC_TLS_GD_LO10:
2895           case elfcpp::R_SPARC_TLS_GD_ADD:
2896           case elfcpp::R_SPARC_TLS_GD_CALL:
2897             if (optimized_type == tls::TLSOPT_NONE)
2898               {
2899                 // Create a pair of GOT entries for the module index and
2900                 // dtv-relative offset.
2901                 Output_data_got<size, big_endian>* got
2902                     = target->got_section(symtab, layout);
2903                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2904                                               target->rela_dyn_section(layout),
2905                                               (size == 64
2906                                                ? elfcpp::R_SPARC_TLS_DTPMOD64
2907                                                : elfcpp::R_SPARC_TLS_DTPMOD32),
2908                                               (size == 64
2909                                                ? elfcpp::R_SPARC_TLS_DTPOFF64
2910                                                : elfcpp::R_SPARC_TLS_DTPOFF32));
2911
2912                 // Emit R_SPARC_WPLT30 against "__tls_get_addr"
2913                 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2914                   generate_tls_call(symtab, layout, target);
2915               }
2916             else if (optimized_type == tls::TLSOPT_TO_IE)
2917               {
2918                 // Create a GOT entry for the tp-relative offset.
2919                 Output_data_got<size, big_endian>* got
2920                     = target->got_section(symtab, layout);
2921                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2922                                          target->rela_dyn_section(layout),
2923                                          (size == 64 ?
2924                                           elfcpp::R_SPARC_TLS_TPOFF64 :
2925                                           elfcpp::R_SPARC_TLS_TPOFF32));
2926               }
2927             else if (optimized_type != tls::TLSOPT_TO_LE)
2928               unsupported_reloc_global(object, r_type, gsym);
2929             break;
2930
2931           case elfcpp::R_SPARC_TLS_LDM_HI22:    // Local-dynamic
2932           case elfcpp::R_SPARC_TLS_LDM_LO10:
2933           case elfcpp::R_SPARC_TLS_LDM_ADD:
2934           case elfcpp::R_SPARC_TLS_LDM_CALL:
2935             if (optimized_type == tls::TLSOPT_NONE)
2936               {
2937                 // Create a GOT entry for the module index.
2938                 target->got_mod_index_entry(symtab, layout, object);
2939
2940                 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2941                   generate_tls_call(symtab, layout, target);
2942               }
2943             else if (optimized_type != tls::TLSOPT_TO_LE)
2944               unsupported_reloc_global(object, r_type, gsym);
2945             break;
2946
2947           case elfcpp::R_SPARC_TLS_LDO_HIX22:   // Alternate local-dynamic
2948           case elfcpp::R_SPARC_TLS_LDO_LOX10:
2949           case elfcpp::R_SPARC_TLS_LDO_ADD:
2950             break;
2951
2952           case elfcpp::R_SPARC_TLS_LE_HIX22:
2953           case elfcpp::R_SPARC_TLS_LE_LOX10:
2954             layout->set_has_static_tls();
2955             if (parameters->options().shared())
2956               {
2957                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2958                 rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
2959                                                        output_section, object,
2960                                                        data_shndx, reloc.get_r_offset(),
2961                                                        0);
2962               }
2963             break;
2964
2965           case elfcpp::R_SPARC_TLS_IE_HI22:     // Initial-exec
2966           case elfcpp::R_SPARC_TLS_IE_LO10:
2967           case elfcpp::R_SPARC_TLS_IE_LD:
2968           case elfcpp::R_SPARC_TLS_IE_LDX:
2969           case elfcpp::R_SPARC_TLS_IE_ADD:
2970             layout->set_has_static_tls();
2971             if (optimized_type == tls::TLSOPT_NONE)
2972               {
2973                 // Create a GOT entry for the tp-relative offset.
2974                 Output_data_got<size, big_endian>* got
2975                   = target->got_section(symtab, layout);
2976                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
2977                                          target->rela_dyn_section(layout),
2978                                          (size == 64
2979                                           ? elfcpp::R_SPARC_TLS_TPOFF64
2980                                           : elfcpp::R_SPARC_TLS_TPOFF32));
2981               }
2982             else if (optimized_type != tls::TLSOPT_TO_LE)
2983               unsupported_reloc_global(object, r_type, gsym);
2984             break;
2985           }
2986       }
2987       break;
2988
2989       // These are relocations which should only be seen by the
2990       // dynamic linker, and should never be seen here.
2991     case elfcpp::R_SPARC_COPY:
2992     case elfcpp::R_SPARC_GLOB_DAT:
2993     case elfcpp::R_SPARC_JMP_SLOT:
2994     case elfcpp::R_SPARC_JMP_IREL:
2995     case elfcpp::R_SPARC_RELATIVE:
2996     case elfcpp::R_SPARC_IRELATIVE:
2997     case elfcpp::R_SPARC_TLS_DTPMOD64:
2998     case elfcpp::R_SPARC_TLS_DTPMOD32:
2999     case elfcpp::R_SPARC_TLS_DTPOFF64:
3000     case elfcpp::R_SPARC_TLS_DTPOFF32:
3001     case elfcpp::R_SPARC_TLS_TPOFF64:
3002     case elfcpp::R_SPARC_TLS_TPOFF32:
3003       gold_error(_("%s: unexpected reloc %u in object file"),
3004                  object->name().c_str(), r_type);
3005       break;
3006
3007     default:
3008       unsupported_reloc_global(object, r_type, gsym);
3009       break;
3010     }
3011 }
3012
3013 // Process relocations for gc.
3014
3015 template<int size, bool big_endian>
3016 void
3017 Target_sparc<size, big_endian>::gc_process_relocs(
3018                         Symbol_table* symtab,
3019                         Layout* layout,
3020                         Sized_relobj_file<size, big_endian>* object,
3021                         unsigned int data_shndx,
3022                         unsigned int,
3023                         const unsigned char* prelocs,
3024                         size_t reloc_count,
3025                         Output_section* output_section,
3026                         bool needs_special_offset_handling,
3027                         size_t local_symbol_count,
3028                         const unsigned char* plocal_symbols)
3029 {
3030   typedef Target_sparc<size, big_endian> Sparc;
3031   typedef typename Target_sparc<size, big_endian>::Scan Scan;
3032
3033   gold::gc_process_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan,
3034                           typename Target_sparc::Relocatable_size_for_reloc>(
3035     symtab,
3036     layout,
3037     this,
3038     object,
3039     data_shndx,
3040     prelocs,
3041     reloc_count,
3042     output_section,
3043     needs_special_offset_handling,
3044     local_symbol_count,
3045     plocal_symbols);
3046 }
3047
3048 // Scan relocations for a section.
3049
3050 template<int size, bool big_endian>
3051 void
3052 Target_sparc<size, big_endian>::scan_relocs(
3053                         Symbol_table* symtab,
3054                         Layout* layout,
3055                         Sized_relobj_file<size, big_endian>* object,
3056                         unsigned int data_shndx,
3057                         unsigned int sh_type,
3058                         const unsigned char* prelocs,
3059                         size_t reloc_count,
3060                         Output_section* output_section,
3061                         bool needs_special_offset_handling,
3062                         size_t local_symbol_count,
3063                         const unsigned char* plocal_symbols)
3064 {
3065   typedef Target_sparc<size, big_endian> Sparc;
3066   typedef typename Target_sparc<size, big_endian>::Scan Scan;
3067
3068   if (sh_type == elfcpp::SHT_REL)
3069     {
3070       gold_error(_("%s: unsupported REL reloc section"),
3071                  object->name().c_str());
3072       return;
3073     }
3074
3075   gold::scan_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan>(
3076     symtab,
3077     layout,
3078     this,
3079     object,
3080     data_shndx,
3081     prelocs,
3082     reloc_count,
3083     output_section,
3084     needs_special_offset_handling,
3085     local_symbol_count,
3086     plocal_symbols);
3087 }
3088
3089 // Finalize the sections.
3090
3091 template<int size, bool big_endian>
3092 void
3093 Target_sparc<size, big_endian>::do_finalize_sections(
3094     Layout* layout,
3095     const Input_objects*,
3096     Symbol_table* symtab)
3097 {
3098   if (this->plt_)
3099     this->plt_->emit_pending_ifunc_relocs();
3100
3101   // Fill in some more dynamic tags.
3102   const Reloc_section* rel_plt = (this->plt_ == NULL
3103                                   ? NULL
3104                                   : this->plt_->rel_plt());
3105   layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
3106                                   this->rela_dyn_, true, true);
3107
3108   // Emit any relocs we saved in an attempt to avoid generating COPY
3109   // relocs.
3110   if (this->copy_relocs_.any_saved_relocs())
3111     this->copy_relocs_.emit(this->rela_dyn_section(layout));
3112
3113   if (parameters->doing_static_link()
3114       && (this->plt_ == NULL || !this->plt_->has_ifunc_section()))
3115     {
3116       // If linking statically, make sure that the __rela_iplt symbols
3117       // were defined if necessary, even if we didn't create a PLT.
3118       static const Define_symbol_in_segment syms[] =
3119         {
3120           {
3121             "__rela_iplt_start",        // name
3122             elfcpp::PT_LOAD,            // segment_type
3123             elfcpp::PF_W,               // segment_flags_set
3124             elfcpp::PF(0),              // segment_flags_clear
3125             0,                          // value
3126             0,                          // size
3127             elfcpp::STT_NOTYPE,         // type
3128             elfcpp::STB_GLOBAL,         // binding
3129             elfcpp::STV_HIDDEN,         // visibility
3130             0,                          // nonvis
3131             Symbol::SEGMENT_START,      // offset_from_base
3132             true                        // only_if_ref
3133           },
3134           {
3135             "__rela_iplt_end",          // name
3136             elfcpp::PT_LOAD,            // segment_type
3137             elfcpp::PF_W,               // segment_flags_set
3138             elfcpp::PF(0),              // segment_flags_clear
3139             0,                          // value
3140             0,                          // size
3141             elfcpp::STT_NOTYPE,         // type
3142             elfcpp::STB_GLOBAL,         // binding
3143             elfcpp::STV_HIDDEN,         // visibility
3144             0,                          // nonvis
3145             Symbol::SEGMENT_START,      // offset_from_base
3146             true                        // only_if_ref
3147           }
3148         };
3149
3150       symtab->define_symbols(layout, 2, syms,
3151                              layout->script_options()->saw_sections_clause());
3152     }
3153 }
3154
3155 // Perform a relocation.
3156
3157 template<int size, bool big_endian>
3158 inline bool
3159 Target_sparc<size, big_endian>::Relocate::relocate(
3160                         const Relocate_info<size, big_endian>* relinfo,
3161                         Target_sparc* target,
3162                         Output_section*,
3163                         size_t relnum,
3164                         const elfcpp::Rela<size, big_endian>& rela,
3165                         unsigned int r_type,
3166                         const Sized_symbol<size>* gsym,
3167                         const Symbol_value<size>* psymval,
3168                         unsigned char* view,
3169                         typename elfcpp::Elf_types<size>::Elf_Addr address,
3170                         section_size_type view_size)
3171 {
3172   bool orig_is_ifunc = psymval->is_ifunc_symbol();
3173   r_type &= 0xff;
3174
3175   if (this->ignore_gd_add_)
3176     {
3177       if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
3178         gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3179                                _("missing expected TLS relocation"));
3180       else
3181         {
3182           this->ignore_gd_add_ = false;
3183           return false;
3184         }
3185     }
3186   if (this->reloc_adjust_addr_ == view)
3187     view -= 4;
3188
3189   typedef Sparc_relocate_functions<size, big_endian> Reloc;
3190   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
3191
3192   // Pick the value to use for symbols defined in shared objects.
3193   Symbol_value<size> symval;
3194   if (gsym != NULL
3195       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
3196     {
3197       elfcpp::Elf_Xword value;
3198
3199       value = target->plt_address_for_global(gsym) + gsym->plt_offset();
3200
3201       symval.set_output_value(value);
3202
3203       psymval = &symval;
3204     }
3205   else if (gsym == NULL && orig_is_ifunc)
3206     {
3207       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3208       if (object->local_has_plt_offset(r_sym))
3209         {
3210           symval.set_output_value(target->plt_address_for_local(object, r_sym)
3211                                   + object->local_plt_offset(r_sym));
3212           psymval = &symval;
3213         }
3214     }
3215
3216   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3217
3218   // Get the GOT offset if needed.  Unlike i386 and x86_64, our GOT
3219   // pointer points to the beginning, not the end, of the table.
3220   // So we just use the plain offset.
3221   unsigned int got_offset = 0;
3222   bool gdop_valid = false;
3223   switch (r_type)
3224     {
3225     case elfcpp::R_SPARC_GOTDATA_OP:
3226     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
3227     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
3228       // If this is local, we did not create a GOT entry because we
3229       // intend to transform this into a GOT relative relocation.
3230       if (gsym == NULL
3231           || (gsym->is_defined()
3232               && !gsym->is_from_dynobj()
3233               && !gsym->is_preemptible()
3234               && !orig_is_ifunc))
3235         {
3236           got_offset = psymval->value(object, 0) - target->got_address();
3237           gdop_valid = true;
3238           break;
3239         }
3240     case elfcpp::R_SPARC_GOT10:
3241     case elfcpp::R_SPARC_GOT13:
3242     case elfcpp::R_SPARC_GOT22:
3243       if (gsym != NULL)
3244         {
3245           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
3246           got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
3247         }
3248       else
3249         {
3250           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3251           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
3252           got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
3253         }
3254       break;
3255
3256     default:
3257       break;
3258     }
3259
3260   switch (r_type)
3261     {
3262     case elfcpp::R_SPARC_NONE:
3263     case elfcpp::R_SPARC_REGISTER:
3264     case elfcpp::R_SPARC_GNU_VTINHERIT:
3265     case elfcpp::R_SPARC_GNU_VTENTRY:
3266       break;
3267
3268     case elfcpp::R_SPARC_8:
3269       Relocate_functions<size, big_endian>::rela8(view, object,
3270                                                   psymval, addend);
3271       break;
3272
3273     case elfcpp::R_SPARC_16:
3274       if (rela.get_r_offset() & 0x1)
3275         {
3276           // The assembler can sometimes emit unaligned relocations
3277           // for dwarf2 cfi directives.
3278           Reloc::ua16(view, object, psymval, addend);
3279         }
3280       else
3281         Relocate_functions<size, big_endian>::rela16(view, object,
3282                                                      psymval, addend);
3283       break;
3284
3285     case elfcpp::R_SPARC_32:
3286       if (!parameters->options().output_is_position_independent())
3287         {
3288           if (rela.get_r_offset() & 0x3)
3289             {
3290               // The assembler can sometimes emit unaligned relocations
3291               // for dwarf2 cfi directives.
3292               Reloc::ua32(view, object, psymval, addend);
3293             }
3294           else
3295             Relocate_functions<size, big_endian>::rela32(view, object,
3296                                                          psymval, addend);
3297         }
3298       break;
3299
3300     case elfcpp::R_SPARC_DISP8:
3301       Reloc::disp8(view, object, psymval, addend, address);
3302       break;
3303
3304     case elfcpp::R_SPARC_DISP16:
3305       Reloc::disp16(view, object, psymval, addend, address);
3306       break;
3307
3308     case elfcpp::R_SPARC_DISP32:
3309       Reloc::disp32(view, object, psymval, addend, address);
3310       break;
3311
3312     case elfcpp::R_SPARC_DISP64:
3313       Reloc::disp64(view, object, psymval, addend, address);
3314       break;
3315
3316     case elfcpp::R_SPARC_WDISP30:
3317     case elfcpp::R_SPARC_WPLT30:
3318       Reloc::wdisp30(view, object, psymval, addend, address);
3319       if (target->may_relax())
3320         relax_call(target, view, rela, view_size);
3321       break;
3322
3323     case elfcpp::R_SPARC_WDISP22:
3324       Reloc::wdisp22(view, object, psymval, addend, address);
3325       break;
3326
3327     case elfcpp::R_SPARC_WDISP19:
3328       Reloc::wdisp19(view, object, psymval, addend, address);
3329       break;
3330
3331     case elfcpp::R_SPARC_WDISP16:
3332       Reloc::wdisp16(view, object, psymval, addend, address);
3333       break;
3334
3335     case elfcpp::R_SPARC_WDISP10:
3336       Reloc::wdisp10(view, object, psymval, addend, address);
3337       break;
3338
3339     case elfcpp::R_SPARC_HI22:
3340       Reloc::hi22(view, object, psymval, addend);
3341       break;
3342
3343     case elfcpp::R_SPARC_22:
3344       Reloc::rela32_22(view, object, psymval, addend);
3345       break;
3346
3347     case elfcpp::R_SPARC_13:
3348       Reloc::rela32_13(view, object, psymval, addend);
3349       break;
3350
3351     case elfcpp::R_SPARC_LO10:
3352       Reloc::lo10(view, object, psymval, addend);
3353       break;
3354
3355     case elfcpp::R_SPARC_GOT10:
3356       Reloc::lo10(view, got_offset, addend);
3357       break;
3358
3359     case elfcpp::R_SPARC_GOTDATA_OP:
3360       if (gdop_valid)
3361         {
3362           typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
3363           Insntype* wv = reinterpret_cast<Insntype*>(view);
3364           Insntype val;
3365
3366           // {ld,ldx} [%rs1 + %rs2], %rd --> add %rs1, %rs2, %rd
3367           val = elfcpp::Swap<32, true>::readval(wv);
3368           val = 0x80000000 | (val & 0x3e07c01f);
3369           elfcpp::Swap<32, true>::writeval(wv, val);
3370         }
3371       break;
3372
3373     case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
3374       if (gdop_valid)
3375         {
3376           Reloc::gdop_lox10(view, got_offset, addend);
3377           break;
3378         }
3379       /* Fall through.  */
3380     case elfcpp::R_SPARC_GOT13:
3381       Reloc::rela32_13(view, got_offset, addend);
3382       break;
3383
3384     case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
3385       if (gdop_valid)
3386         {
3387           Reloc::gdop_hix22(view, got_offset, addend);
3388           break;
3389         }
3390       /* Fall through.  */
3391     case elfcpp::R_SPARC_GOT22:
3392       Reloc::hi22(view, got_offset, addend);
3393       break;
3394
3395     case elfcpp::R_SPARC_PC10:
3396       Reloc::pc10(view, object, psymval, addend, address);
3397       break;
3398
3399     case elfcpp::R_SPARC_PC22:
3400       Reloc::pc22(view, object, psymval, addend, address);
3401       break;
3402
3403     case elfcpp::R_SPARC_TLS_DTPOFF32:
3404     case elfcpp::R_SPARC_UA32:
3405       Reloc::ua32(view, object, psymval, addend);
3406       break;
3407
3408     case elfcpp::R_SPARC_PLT64:
3409       Relocate_functions<size, big_endian>::rela64(view, object,
3410                                                    psymval, addend);
3411       break;
3412
3413     case elfcpp::R_SPARC_PLT32:
3414       Relocate_functions<size, big_endian>::rela32(view, object,
3415                                                    psymval, addend);
3416       break;
3417
3418     case elfcpp::R_SPARC_HIPLT22:
3419       Reloc::hi22(view, object, psymval, addend);
3420       break;
3421
3422     case elfcpp::R_SPARC_LOPLT10:
3423       Reloc::lo10(view, object, psymval, addend);
3424       break;
3425
3426     case elfcpp::R_SPARC_PCPLT32:
3427       Reloc::disp32(view, object, psymval, addend, address);
3428       break;
3429
3430     case elfcpp::R_SPARC_PCPLT22:
3431       Reloc::pcplt22(view, object, psymval, addend, address);
3432       break;
3433
3434     case elfcpp::R_SPARC_PCPLT10:
3435       Reloc::lo10(view, object, psymval, addend, address);
3436       break;
3437
3438     case elfcpp::R_SPARC_64:
3439       if (!parameters->options().output_is_position_independent())
3440         {
3441           if (rela.get_r_offset() & 0x7)
3442             {
3443               // The assembler can sometimes emit unaligned relocations
3444               // for dwarf2 cfi directives.
3445               Reloc::ua64(view, object, psymval, addend);
3446             }
3447           else
3448             Relocate_functions<size, big_endian>::rela64(view, object,
3449                                                          psymval, addend);
3450         }
3451       break;
3452
3453     case elfcpp::R_SPARC_OLO10:
3454       {
3455         unsigned int addend2 = rela.get_r_info() & 0xffffffff;
3456         addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
3457         Reloc::olo10(view, object, psymval, addend, addend2);
3458       }
3459       break;
3460
3461     case elfcpp::R_SPARC_HH22:
3462       Reloc::hh22(view, object, psymval, addend);
3463       break;
3464
3465     case elfcpp::R_SPARC_PC_HH22:
3466       Reloc::pc_hh22(view, object, psymval, addend, address);
3467       break;
3468
3469     case elfcpp::R_SPARC_HM10:
3470       Reloc::hm10(view, object, psymval, addend);
3471       break;
3472
3473     case elfcpp::R_SPARC_PC_HM10:
3474       Reloc::pc_hm10(view, object, psymval, addend, address);
3475       break;
3476
3477     case elfcpp::R_SPARC_LM22:
3478       Reloc::hi22(view, object, psymval, addend);
3479       break;
3480
3481     case elfcpp::R_SPARC_PC_LM22:
3482       Reloc::pcplt22(view, object, psymval, addend, address);
3483       break;
3484
3485     case elfcpp::R_SPARC_11:
3486       Reloc::rela32_11(view, object, psymval, addend);
3487       break;
3488
3489     case elfcpp::R_SPARC_10:
3490       Reloc::rela32_10(view, object, psymval, addend);
3491       break;
3492
3493     case elfcpp::R_SPARC_7:
3494       Reloc::rela32_7(view, object, psymval, addend);
3495       break;
3496
3497     case elfcpp::R_SPARC_6:
3498       Reloc::rela32_6(view, object, psymval, addend);
3499       break;
3500
3501     case elfcpp::R_SPARC_5:
3502       Reloc::rela32_5(view, object, psymval, addend);
3503       break;
3504
3505     case elfcpp::R_SPARC_HIX22:
3506       Reloc::hix22(view, object, psymval, addend);
3507       break;
3508
3509     case elfcpp::R_SPARC_LOX10:
3510       Reloc::lox10(view, object, psymval, addend);
3511       break;
3512
3513     case elfcpp::R_SPARC_H34:
3514       Reloc::h34(view, object, psymval, addend);
3515       break;
3516
3517     case elfcpp::R_SPARC_H44:
3518       Reloc::h44(view, object, psymval, addend);
3519       break;
3520
3521     case elfcpp::R_SPARC_M44:
3522       Reloc::m44(view, object, psymval, addend);
3523       break;
3524
3525     case elfcpp::R_SPARC_L44:
3526       Reloc::l44(view, object, psymval, addend);
3527       break;
3528
3529     case elfcpp::R_SPARC_TLS_DTPOFF64:
3530     case elfcpp::R_SPARC_UA64:
3531       Reloc::ua64(view, object, psymval, addend);
3532       break;
3533
3534     case elfcpp::R_SPARC_UA16:
3535       Reloc::ua16(view, object, psymval, addend);
3536       break;
3537
3538     case elfcpp::R_SPARC_TLS_GD_HI22:
3539     case elfcpp::R_SPARC_TLS_GD_LO10:
3540     case elfcpp::R_SPARC_TLS_GD_ADD:
3541     case elfcpp::R_SPARC_TLS_GD_CALL:
3542     case elfcpp::R_SPARC_TLS_LDM_HI22:
3543     case elfcpp::R_SPARC_TLS_LDM_LO10:
3544     case elfcpp::R_SPARC_TLS_LDM_ADD:
3545     case elfcpp::R_SPARC_TLS_LDM_CALL:
3546     case elfcpp::R_SPARC_TLS_LDO_HIX22:
3547     case elfcpp::R_SPARC_TLS_LDO_LOX10:
3548     case elfcpp::R_SPARC_TLS_LDO_ADD:
3549     case elfcpp::R_SPARC_TLS_IE_HI22:
3550     case elfcpp::R_SPARC_TLS_IE_LO10:
3551     case elfcpp::R_SPARC_TLS_IE_LD:
3552     case elfcpp::R_SPARC_TLS_IE_LDX:
3553     case elfcpp::R_SPARC_TLS_IE_ADD:
3554     case elfcpp::R_SPARC_TLS_LE_HIX22:
3555     case elfcpp::R_SPARC_TLS_LE_LOX10:
3556       this->relocate_tls(relinfo, target, relnum, rela,
3557                          r_type, gsym, psymval, view,
3558                          address, view_size);
3559       break;
3560
3561     case elfcpp::R_SPARC_COPY:
3562     case elfcpp::R_SPARC_GLOB_DAT:
3563     case elfcpp::R_SPARC_JMP_SLOT:
3564     case elfcpp::R_SPARC_JMP_IREL:
3565     case elfcpp::R_SPARC_RELATIVE:
3566     case elfcpp::R_SPARC_IRELATIVE:
3567       // These are outstanding tls relocs, which are unexpected when
3568       // linking.
3569     case elfcpp::R_SPARC_TLS_DTPMOD64:
3570     case elfcpp::R_SPARC_TLS_DTPMOD32:
3571     case elfcpp::R_SPARC_TLS_TPOFF64:
3572     case elfcpp::R_SPARC_TLS_TPOFF32:
3573       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3574                              _("unexpected reloc %u in object file"),
3575                              r_type);
3576       break;
3577
3578     default:
3579       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3580                              _("unsupported reloc %u"),
3581                              r_type);
3582       break;
3583     }
3584
3585   return true;
3586 }
3587
3588 // Perform a TLS relocation.
3589
3590 template<int size, bool big_endian>
3591 inline void
3592 Target_sparc<size, big_endian>::Relocate::relocate_tls(
3593                         const Relocate_info<size, big_endian>* relinfo,
3594                         Target_sparc<size, big_endian>* target,
3595                         size_t relnum,
3596                         const elfcpp::Rela<size, big_endian>& rela,
3597                         unsigned int r_type,
3598                         const Sized_symbol<size>* gsym,
3599                         const Symbol_value<size>* psymval,
3600                         unsigned char* view,
3601                         typename elfcpp::Elf_types<size>::Elf_Addr address,
3602                         section_size_type)
3603 {
3604   Output_segment* tls_segment = relinfo->layout->tls_segment();
3605   typedef Sparc_relocate_functions<size, big_endian> Reloc;
3606   const Sized_relobj_file<size, big_endian>* object = relinfo->object;
3607   typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
3608
3609   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3610   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
3611
3612   const bool is_final =
3613     (gsym == NULL
3614      ? !parameters->options().output_is_position_independent()
3615      : gsym->final_value_is_known());
3616   const tls::Tls_optimization optimized_type
3617       = optimize_tls_reloc(is_final, r_type);
3618
3619   switch (r_type)
3620     {
3621     case elfcpp::R_SPARC_TLS_GD_HI22:
3622     case elfcpp::R_SPARC_TLS_GD_LO10:
3623     case elfcpp::R_SPARC_TLS_GD_ADD:
3624     case elfcpp::R_SPARC_TLS_GD_CALL:
3625       if (optimized_type == tls::TLSOPT_TO_LE)
3626         {
3627           Insntype* wv = reinterpret_cast<Insntype*>(view);
3628           Insntype val;
3629
3630           value -= tls_segment->memsz();
3631
3632           switch (r_type)
3633             {
3634             case elfcpp::R_SPARC_TLS_GD_HI22:
3635               // TLS_GD_HI22 --> TLS_LE_HIX22
3636               Reloc::hix22(view, value, addend);
3637               break;
3638
3639             case elfcpp::R_SPARC_TLS_GD_LO10:
3640               // TLS_GD_LO10 --> TLS_LE_LOX10
3641               Reloc::lox10(view, value, addend);
3642               break;
3643
3644             case elfcpp::R_SPARC_TLS_GD_ADD:
3645               // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
3646               val = elfcpp::Swap<32, true>::readval(wv);
3647               val = (val & ~0x7c000) | 0x1c000;
3648               elfcpp::Swap<32, true>::writeval(wv, val);
3649               break;
3650             case elfcpp::R_SPARC_TLS_GD_CALL:
3651               // call __tls_get_addr --> nop
3652               elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
3653               break;
3654             }
3655           break;
3656         }
3657       else
3658         {
3659           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3660                                    ? GOT_TYPE_TLS_OFFSET
3661                                    : GOT_TYPE_TLS_PAIR);
3662           if (gsym != NULL)
3663             {
3664               gold_assert(gsym->has_got_offset(got_type));
3665               value = gsym->got_offset(got_type);
3666             }
3667           else
3668             {
3669               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3670               gold_assert(object->local_has_got_offset(r_sym, got_type));
3671               value = object->local_got_offset(r_sym, got_type);
3672             }
3673           if (optimized_type == tls::TLSOPT_TO_IE)
3674             {
3675               Insntype* wv = reinterpret_cast<Insntype*>(view);
3676               Insntype val;
3677
3678               switch (r_type)
3679                 {
3680                 case elfcpp::R_SPARC_TLS_GD_HI22:
3681                   // TLS_GD_HI22 --> TLS_IE_HI22
3682                   Reloc::hi22(view, value, addend);
3683                   break;
3684
3685                 case elfcpp::R_SPARC_TLS_GD_LO10:
3686                   // TLS_GD_LO10 --> TLS_IE_LO10
3687                   Reloc::lo10(view, value, addend);
3688                   break;
3689
3690                 case elfcpp::R_SPARC_TLS_GD_ADD:
3691                   // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
3692                   val = elfcpp::Swap<32, true>::readval(wv);
3693
3694                   if (size == 64)
3695                     val |= 0xc0580000;
3696                   else
3697                     val |= 0xc0000000;
3698
3699                   elfcpp::Swap<32, true>::writeval(wv, val);
3700                   break;
3701
3702                 case elfcpp::R_SPARC_TLS_GD_CALL:
3703                   // The compiler can put the TLS_GD_ADD instruction
3704                   // into the delay slot of the call.  If so, we need
3705                   // to transpose the two instructions so that the
3706                   // new sequence works properly.
3707                   //
3708                   // The test we use is if the instruction in the
3709                   // delay slot is an add with destination register
3710                   // equal to %o0
3711                   val = elfcpp::Swap<32, true>::readval(wv + 1);
3712                   if ((val & 0x81f80000) == 0x80000000
3713                       && ((val >> 25) & 0x1f) == 0x8)
3714                     {
3715                       if (size == 64)
3716                         val |= 0xc0580000;
3717                       else
3718                         val |= 0xc0000000;
3719
3720                       elfcpp::Swap<32, true>::writeval(wv, val);
3721
3722                       wv += 1;
3723                       this->ignore_gd_add_ = true;
3724                     }
3725                   else
3726                     {
3727                       // Even if the delay slot isn't the TLS_GD_ADD
3728                       // instruction, we still have to handle the case
3729                       // where it sets up %o0 in some other way.
3730                       elfcpp::Swap<32, true>::writeval(wv, val);
3731                       wv += 1;
3732                       this->reloc_adjust_addr_ = view + 4;
3733                     }
3734                   // call __tls_get_addr --> add %g7, %o0, %o0
3735                   elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
3736                   break;
3737                 }
3738               break;
3739             }
3740           else if (optimized_type == tls::TLSOPT_NONE)
3741             {
3742               switch (r_type)
3743                 {
3744                 case elfcpp::R_SPARC_TLS_GD_HI22:
3745                   Reloc::hi22(view, value, addend);
3746                   break;
3747                 case elfcpp::R_SPARC_TLS_GD_LO10:
3748                   Reloc::lo10(view, value, addend);
3749                   break;
3750                 case elfcpp::R_SPARC_TLS_GD_ADD:
3751                   break;
3752                 case elfcpp::R_SPARC_TLS_GD_CALL:
3753                   {
3754                     Symbol_value<size> symval;
3755                     elfcpp::Elf_Xword value;
3756                     Symbol* tsym;
3757
3758                     tsym = target->tls_get_addr_sym_;
3759                     gold_assert(tsym);
3760                     value = (target->plt_section()->address() +
3761                              tsym->plt_offset());
3762                     symval.set_output_value(value);
3763                     Reloc::wdisp30(view, object, &symval, addend, address);
3764                   }
3765                   break;
3766                 }
3767               break;
3768             }
3769         }
3770       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3771                              _("unsupported reloc %u"),
3772                              r_type);
3773       break;
3774
3775     case elfcpp::R_SPARC_TLS_LDM_HI22:
3776     case elfcpp::R_SPARC_TLS_LDM_LO10:
3777     case elfcpp::R_SPARC_TLS_LDM_ADD:
3778     case elfcpp::R_SPARC_TLS_LDM_CALL:
3779       if (optimized_type == tls::TLSOPT_TO_LE)
3780         {
3781           Insntype* wv = reinterpret_cast<Insntype*>(view);
3782
3783           switch (r_type)
3784             {
3785             case elfcpp::R_SPARC_TLS_LDM_HI22:
3786             case elfcpp::R_SPARC_TLS_LDM_LO10:
3787             case elfcpp::R_SPARC_TLS_LDM_ADD:
3788               elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
3789               break;
3790
3791             case elfcpp::R_SPARC_TLS_LDM_CALL:
3792               elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
3793               break;
3794             }
3795           break;
3796         }
3797       else if (optimized_type == tls::TLSOPT_NONE)
3798         {
3799           // Relocate the field with the offset of the GOT entry for
3800           // the module index.
3801           unsigned int got_offset;
3802
3803           got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
3804           switch (r_type)
3805             {
3806             case elfcpp::R_SPARC_TLS_LDM_HI22:
3807               Reloc::hi22(view, got_offset, addend);
3808               break;
3809             case elfcpp::R_SPARC_TLS_LDM_LO10:
3810               Reloc::lo10(view, got_offset, addend);
3811               break;
3812             case elfcpp::R_SPARC_TLS_LDM_ADD:
3813               break;
3814             case elfcpp::R_SPARC_TLS_LDM_CALL:
3815               {
3816                 Symbol_value<size> symval;
3817                 elfcpp::Elf_Xword value;
3818                 Symbol* tsym;
3819
3820                 tsym = target->tls_get_addr_sym_;
3821                 gold_assert(tsym);
3822                 value = (target->plt_section()->address() +
3823                          tsym->plt_offset());
3824                 symval.set_output_value(value);
3825                 Reloc::wdisp30(view, object, &symval, addend, address);
3826               }
3827               break;
3828             }
3829           break;
3830         }
3831       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3832                              _("unsupported reloc %u"),
3833                              r_type);
3834       break;
3835
3836       // These relocs can appear in debugging sections, in which case
3837       // we won't see the TLS_LDM relocs.  The local_dynamic_type
3838       // field tells us this.
3839     case elfcpp::R_SPARC_TLS_LDO_HIX22:
3840       if (optimized_type == tls::TLSOPT_TO_LE)
3841         {
3842           value -= tls_segment->memsz();
3843           Reloc::hix22(view, value, addend);
3844         }
3845       else
3846         Reloc::ldo_hix22(view, value, addend);
3847       break;
3848     case elfcpp::R_SPARC_TLS_LDO_LOX10:
3849       if (optimized_type == tls::TLSOPT_TO_LE)
3850         {
3851           value -= tls_segment->memsz();
3852           Reloc::lox10(view, value, addend);
3853         }
3854       else
3855         Reloc::ldo_lox10(view, value, addend);
3856       break;
3857     case elfcpp::R_SPARC_TLS_LDO_ADD:
3858       if (optimized_type == tls::TLSOPT_TO_LE)
3859         {
3860           Insntype* wv = reinterpret_cast<Insntype*>(view);
3861           Insntype val;
3862
3863           // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
3864           val = elfcpp::Swap<32, true>::readval(wv);
3865           val = (val & ~0x7c000) | 0x1c000;
3866           elfcpp::Swap<32, true>::writeval(wv, val);
3867         }
3868       break;
3869
3870       // When optimizing IE --> LE, the only relocation that is handled
3871       // differently is R_SPARC_TLS_IE_LD, it is rewritten from
3872       // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
3873       // rs2 and rd are the same.
3874     case elfcpp::R_SPARC_TLS_IE_LD:
3875     case elfcpp::R_SPARC_TLS_IE_LDX:
3876       if (optimized_type == tls::TLSOPT_TO_LE)
3877         {
3878           Insntype* wv = reinterpret_cast<Insntype*>(view);
3879           Insntype val = elfcpp::Swap<32, true>::readval(wv);
3880           Insntype rs2 = val & 0x1f;
3881           Insntype rd = (val >> 25) & 0x1f;
3882
3883           if (rs2 == rd)
3884             val = sparc_nop;
3885           else
3886             val = sparc_mov | (val & 0x3e00001f);
3887
3888           elfcpp::Swap<32, true>::writeval(wv, val);
3889         }
3890       break;
3891
3892     case elfcpp::R_SPARC_TLS_IE_HI22:
3893     case elfcpp::R_SPARC_TLS_IE_LO10:
3894       if (optimized_type == tls::TLSOPT_TO_LE)
3895         {
3896           value -= tls_segment->memsz();
3897           switch (r_type)
3898             {
3899             case elfcpp::R_SPARC_TLS_IE_HI22:
3900               // IE_HI22 --> LE_HIX22
3901               Reloc::hix22(view, value, addend);
3902               break;
3903             case elfcpp::R_SPARC_TLS_IE_LO10:
3904               // IE_LO10 --> LE_LOX10
3905               Reloc::lox10(view, value, addend);
3906               break;
3907             }
3908           break;
3909         }
3910       else if (optimized_type == tls::TLSOPT_NONE)
3911         {
3912           // Relocate the field with the offset of the GOT entry for
3913           // the tp-relative offset of the symbol.
3914           if (gsym != NULL)
3915             {
3916               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3917               value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
3918             }
3919           else
3920             {
3921               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3922               gold_assert(object->local_has_got_offset(r_sym,
3923                                                        GOT_TYPE_TLS_OFFSET));
3924               value = object->local_got_offset(r_sym,
3925                                                GOT_TYPE_TLS_OFFSET);
3926             }
3927           switch (r_type)
3928             {
3929             case elfcpp::R_SPARC_TLS_IE_HI22:
3930               Reloc::hi22(view, value, addend);
3931               break;
3932             case elfcpp::R_SPARC_TLS_IE_LO10:
3933               Reloc::lo10(view, value, addend);
3934               break;
3935             }
3936           break;
3937         }
3938       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3939                              _("unsupported reloc %u"),
3940                              r_type);
3941       break;
3942
3943     case elfcpp::R_SPARC_TLS_IE_ADD:
3944       // This seems to be mainly so that we can find the addition
3945       // instruction if there is one.  There doesn't seem to be any
3946       // actual relocation to apply.
3947       break;
3948
3949     case elfcpp::R_SPARC_TLS_LE_HIX22:
3950       // If we're creating a shared library, a dynamic relocation will
3951       // have been created for this location, so do not apply it now.
3952       if (!parameters->options().shared())
3953         {
3954           value -= tls_segment->memsz();
3955           Reloc::hix22(view, value, addend);
3956         }
3957       break;
3958
3959     case elfcpp::R_SPARC_TLS_LE_LOX10:
3960       // If we're creating a shared library, a dynamic relocation will
3961       // have been created for this location, so do not apply it now.
3962       if (!parameters->options().shared())
3963         {
3964           value -= tls_segment->memsz();
3965           Reloc::lox10(view, value, addend);
3966         }
3967       break;
3968     }
3969 }
3970
3971 // Relax a call instruction.
3972
3973 template<int size, bool big_endian>
3974 inline void
3975 Target_sparc<size, big_endian>::Relocate::relax_call(
3976     Target_sparc<size, big_endian>* target,
3977     unsigned char* view,
3978     const elfcpp::Rela<size, big_endian>& rela,
3979     section_size_type view_size)
3980 {
3981   typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
3982   Insntype *wv = reinterpret_cast<Insntype*>(view);
3983   Insntype call_insn, delay_insn, set_insn;
3984   uint32_t op3, reg, off;
3985
3986   // This code tries to relax call instructions that meet
3987   // certain criteria.
3988   //
3989   // The first criteria is that the call must be such that the return
3990   // address which the call writes into %o7 is unused.  Two sequences
3991   // meet this criteria, and are used to implement tail calls.
3992   //
3993   // Leaf function tail call:
3994   //
3995   // or %o7, %g0, %ANY_REG
3996   // call FUNC
3997   //  or %ANY_REG, %g0, %o7
3998   //
3999   // Non-leaf function tail call:
4000   //
4001   // call FUNC
4002   //  restore
4003   //
4004   // The second criteria is that the call destination is close.  If
4005   // the displacement can fit in a signed 22-bit immediate field of a
4006   // pre-V9 branch, we can do it.  If we are generating a 64-bit
4007   // object or a 32-bit object with ELF machine type EF_SPARC32PLUS,
4008   // and the displacement fits in a signed 19-bit immediate field,
4009   // then we can use a V9 branch.
4010
4011   // Make sure the delay instruction can be safely accessed.
4012   if (rela.get_r_offset() + 8 > view_size)
4013     return;
4014
4015   call_insn = elfcpp::Swap<32, true>::readval(wv);
4016   delay_insn = elfcpp::Swap<32, true>::readval(wv + 1);
4017
4018   // Make sure it is really a call instruction.
4019   if (((call_insn >> 30) & 0x3) != 1)
4020     return;
4021
4022   if (((delay_insn >> 30) & 0x3) != 2)
4023     return;
4024
4025   // Accept only a restore or an integer arithmetic operation whose
4026   // sole side effect is to write the %o7 register (and perhaps set
4027   // the condition codes, which are considered clobbered across
4028   // function calls).
4029   //
4030   // For example, we don't want to match a tagged addition or
4031   // subtraction.  We also don't want to match something like a
4032   // divide.
4033   //
4034   // Specifically we accept add{,cc}, and{,cc}, or{,cc},
4035   // xor{,cc}, sub{,cc}, andn{,cc}, orn{,cc}, and xnor{,cc}.
4036
4037   op3 = (delay_insn >> 19) & 0x3f;
4038   reg = (delay_insn >> 25) & 0x1f;
4039   if (op3 != 0x3d
4040       && ((op3 & 0x28) != 0 || reg != 15))
4041     return;
4042
4043   // For non-restore instructions, make sure %o7 isn't
4044   // an input.
4045   if (op3 != 0x3d)
4046     {
4047       // First check RS1
4048       reg = (delay_insn >> 14) & 0x15;
4049       if (reg == 15)
4050         return;
4051
4052       // And if non-immediate, check RS2
4053       if (((delay_insn >> 13) & 1) == 0)
4054         {
4055           reg = (delay_insn & 0x1f);
4056           if (reg == 15)
4057             return;
4058         }
4059     }
4060
4061   // Now check the branch distance.  We are called after the
4062   // call has been relocated, so we just have to peek at the
4063   // offset contained in the instruction.
4064   off = call_insn & 0x3fffffff;
4065   if ((off & 0x3fe00000) != 0
4066       && (off & 0x3fe00000) != 0x3fe00000)
4067     return;
4068
4069   if ((size == 64 || target->elf_machine_ == elfcpp::EM_SPARC32PLUS)
4070       && ((off & 0x3c0000) == 0
4071           || (off & 0x3c0000) == 0x3c0000))
4072     {
4073       // ba,pt %xcc, FUNC
4074       call_insn = 0x10680000 | (off & 0x07ffff);
4075     }
4076   else
4077     {
4078       // ba FUNC
4079       call_insn = 0x10800000 | (off & 0x3fffff);
4080     }
4081   elfcpp::Swap<32, true>::writeval(wv, call_insn);
4082
4083   // See if we can NOP out the delay slot instruction.  We peek
4084   // at the instruction before the call to make sure we're dealing
4085   // with exactly the:
4086   //
4087   // or %o7, %g0, %ANY_REG
4088   // call
4089   //  or %ANY_REG, %g0, %o7
4090   //
4091   // case.  Otherwise this might be a tricky piece of hand written
4092   // assembler calculating %o7 in some non-trivial way, and therefore
4093   // we can't be sure that NOP'ing out the delay slot is safe.
4094   if (op3 == 0x02
4095       && rela.get_r_offset() >= 4)
4096     {
4097       if ((delay_insn & ~(0x1f << 14)) != 0x9e100000)
4098         return;
4099
4100       set_insn = elfcpp::Swap<32, true>::readval(wv - 1);
4101       if ((set_insn & ~(0x1f << 25)) != 0x8013c000)
4102         return;
4103
4104       reg = (set_insn >> 25) & 0x1f;
4105       if (reg == 0 || reg == 15)
4106         return;
4107       if (reg != ((delay_insn >> 14) & 0x1f))
4108         return;
4109
4110       // All tests pass, nop it out.
4111       elfcpp::Swap<32, true>::writeval(wv + 1, sparc_nop);
4112     }
4113 }
4114
4115 // Relocate section data.
4116
4117 template<int size, bool big_endian>
4118 void
4119 Target_sparc<size, big_endian>::relocate_section(
4120                         const Relocate_info<size, big_endian>* relinfo,
4121                         unsigned int sh_type,
4122                         const unsigned char* prelocs,
4123                         size_t reloc_count,
4124                         Output_section* output_section,
4125                         bool needs_special_offset_handling,
4126                         unsigned char* view,
4127                         typename elfcpp::Elf_types<size>::Elf_Addr address,
4128                         section_size_type view_size,
4129                         const Reloc_symbol_changes* reloc_symbol_changes)
4130 {
4131   typedef Target_sparc<size, big_endian> Sparc;
4132   typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
4133
4134   gold_assert(sh_type == elfcpp::SHT_RELA);
4135
4136   gold::relocate_section<size, big_endian, Sparc, elfcpp::SHT_RELA,
4137     Sparc_relocate>(
4138     relinfo,
4139     this,
4140     prelocs,
4141     reloc_count,
4142     output_section,
4143     needs_special_offset_handling,
4144     view,
4145     address,
4146     view_size,
4147     reloc_symbol_changes);
4148 }
4149
4150 // Return the size of a relocation while scanning during a relocatable
4151 // link.
4152
4153 template<int size, bool big_endian>
4154 unsigned int
4155 Target_sparc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
4156     unsigned int,
4157     Relobj*)
4158 {
4159   // We are always SHT_RELA, so we should never get here.
4160   gold_unreachable();
4161   return 0;
4162 }
4163
4164 // Scan the relocs during a relocatable link.
4165
4166 template<int size, bool big_endian>
4167 void
4168 Target_sparc<size, big_endian>::scan_relocatable_relocs(
4169                         Symbol_table* symtab,
4170                         Layout* layout,
4171                         Sized_relobj_file<size, big_endian>* object,
4172                         unsigned int data_shndx,
4173                         unsigned int sh_type,
4174                         const unsigned char* prelocs,
4175                         size_t reloc_count,
4176                         Output_section* output_section,
4177                         bool needs_special_offset_handling,
4178                         size_t local_symbol_count,
4179                         const unsigned char* plocal_symbols,
4180                         Relocatable_relocs* rr)
4181 {
4182   gold_assert(sh_type == elfcpp::SHT_RELA);
4183
4184   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
4185     Relocatable_size_for_reloc> Scan_relocatable_relocs;
4186
4187   gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
4188       Scan_relocatable_relocs>(
4189     symtab,
4190     layout,
4191     object,
4192     data_shndx,
4193     prelocs,
4194     reloc_count,
4195     output_section,
4196     needs_special_offset_handling,
4197     local_symbol_count,
4198     plocal_symbols,
4199     rr);
4200 }
4201
4202 // Relocate a section during a relocatable link.
4203
4204 template<int size, bool big_endian>
4205 void
4206 Target_sparc<size, big_endian>::relocate_for_relocatable(
4207     const Relocate_info<size, big_endian>* relinfo,
4208     unsigned int sh_type,
4209     const unsigned char* prelocs,
4210     size_t reloc_count,
4211     Output_section* output_section,
4212     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
4213     const Relocatable_relocs* rr,
4214     unsigned char* view,
4215     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
4216     section_size_type view_size,
4217     unsigned char* reloc_view,
4218     section_size_type reloc_view_size)
4219 {
4220   gold_assert(sh_type == elfcpp::SHT_RELA);
4221
4222   gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
4223     relinfo,
4224     prelocs,
4225     reloc_count,
4226     output_section,
4227     offset_in_output_section,
4228     rr,
4229     view,
4230     view_address,
4231     view_size,
4232     reloc_view,
4233     reloc_view_size);
4234 }
4235
4236 // Return the value to use for a dynamic which requires special
4237 // treatment.  This is how we support equality comparisons of function
4238 // pointers across shared library boundaries, as described in the
4239 // processor specific ABI supplement.
4240
4241 template<int size, bool big_endian>
4242 uint64_t
4243 Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
4244 {
4245   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
4246   return this->plt_section()->address() + gsym->plt_offset();
4247 }
4248
4249 // do_make_elf_object to override the same function in the base class.
4250 // We need to use a target-specific sub-class of
4251 // Sized_relobj_file<size, big_endian> to process SPARC specific bits
4252 // of the ELF headers.  Hence we need to have our own ELF object creation.
4253
4254 template<int size, bool big_endian>
4255 Object*
4256 Target_sparc<size, big_endian>::do_make_elf_object(
4257     const std::string& name,
4258     Input_file* input_file,
4259     off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
4260 {
4261   elfcpp::Elf_Half machine = ehdr.get_e_machine();
4262   elfcpp::Elf_Word flags = ehdr.get_e_flags();
4263   elfcpp::Elf_Word omm, mm;
4264
4265   switch (machine)
4266     {
4267     case elfcpp::EM_SPARC32PLUS:
4268       this->elf_machine_ = elfcpp::EM_SPARC32PLUS;
4269       break;
4270
4271     case elfcpp::EM_SPARC:
4272     case elfcpp::EM_SPARCV9:
4273       break;
4274
4275     default:
4276       break;
4277     }
4278
4279   if (!this->elf_flags_set_)
4280     {
4281       this->elf_flags_ = flags;
4282       this->elf_flags_set_ = true;
4283     }
4284   else
4285     {
4286       // Accumulate cpu feature bits.
4287       this->elf_flags_ |= (flags & (elfcpp::EF_SPARC_32PLUS
4288                                     | elfcpp::EF_SPARC_SUN_US1
4289                                     | elfcpp::EF_SPARC_HAL_R1
4290                                     | elfcpp::EF_SPARC_SUN_US3));
4291
4292       // Bump the memory model setting to the most restrictive
4293       // one we encounter.
4294       omm = (this->elf_flags_ & elfcpp::EF_SPARCV9_MM);
4295       mm = (flags & elfcpp::EF_SPARCV9_MM);
4296       if (omm != mm)
4297         {
4298           if (mm == elfcpp::EF_SPARCV9_TSO)
4299             {
4300               this->elf_flags_ &= ~elfcpp::EF_SPARCV9_MM;
4301               this->elf_flags_ |= elfcpp::EF_SPARCV9_TSO;
4302             }
4303           else if (mm == elfcpp::EF_SPARCV9_PSO
4304                    && omm == elfcpp::EF_SPARCV9_RMO)
4305             {
4306               this->elf_flags_ &= ~elfcpp::EF_SPARCV9_MM;
4307               this->elf_flags_ |= elfcpp::EF_SPARCV9_PSO;
4308             }
4309         }
4310     }
4311
4312   // Validate that the little-endian flag matches how we've
4313   // been instantiated.
4314   if (!(flags & elfcpp::EF_SPARC_LEDATA) != big_endian)
4315     {
4316       if (big_endian)
4317         gold_error(_("%s: little endian elf flag set on BE object"),
4318                      name.c_str());
4319       else
4320         gold_error(_("%s: little endian elf flag clear on LE object"),
4321                      name.c_str());
4322     }
4323
4324   return Target::do_make_elf_object(name, input_file, offset, ehdr);
4325 }
4326
4327 // Adjust ELF file header.
4328
4329 template<int size, bool big_endian>
4330 void
4331 Target_sparc<size, big_endian>::do_adjust_elf_header(
4332     unsigned char* view,
4333     int len) const
4334 {
4335   elfcpp::Ehdr_write<size, big_endian> oehdr(view);
4336
4337   oehdr.put_e_machine(this->elf_machine_);
4338   oehdr.put_e_flags(this->elf_flags_);
4339
4340   Sized_target<size, big_endian>::do_adjust_elf_header(view, len);
4341 }
4342
4343 // The selector for sparc object files.
4344
4345 template<int size, bool big_endian>
4346 class Target_selector_sparc : public Target_selector
4347 {
4348 public:
4349   Target_selector_sparc()
4350     : Target_selector(elfcpp::EM_NONE, size, big_endian,
4351                       (size == 64 ? "elf64-sparc" : "elf32-sparc"),
4352                       (size == 64 ? "elf64_sparc" : "elf32_sparc"))
4353   { }
4354
4355   virtual Target*
4356   do_recognize(Input_file*, off_t, int machine, int, int)
4357   {
4358     switch (size)
4359       {
4360       case 64:
4361         if (machine != elfcpp::EM_SPARCV9)
4362           return NULL;
4363         break;
4364
4365       case 32:
4366         if (machine != elfcpp::EM_SPARC
4367             && machine != elfcpp::EM_SPARC32PLUS)
4368           return NULL;
4369         break;
4370
4371       default:
4372         return NULL;
4373       }
4374
4375     return this->instantiate_target();
4376   }
4377
4378   virtual Target*
4379   do_instantiate_target()
4380   { return new Target_sparc<size, big_endian>(); }
4381 };
4382
4383 Target_selector_sparc<32, true> target_selector_sparc32;
4384 Target_selector_sparc<64, true> target_selector_sparc64;
4385
4386 } // End anonymous namespace.