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