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