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