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