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