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