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