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