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