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