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