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