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