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