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