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