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