1 // sparc.cc -- sparc target support for gold.
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>.
6 // This file is part of gold.
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.
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.
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.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
49 template<int size, bool big_endian>
50 class Output_data_plt_sparc;
52 template<int size, bool big_endian>
53 class Target_sparc : public Sized_target<size, big_endian>
56 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
59 : Sized_target<size, big_endian>(&sparc_info),
60 got_(NULL), plt_(NULL), rela_dyn_(NULL),
61 copy_relocs_(elfcpp::R_SPARC_COPY), dynbss_(NULL),
62 got_mod_index_offset_(-1U), tls_get_addr_sym_(NULL)
66 // Scan the relocations to look for symbol adjustments.
68 scan_relocs(const General_options& options,
71 Sized_relobj<size, big_endian>* object,
72 unsigned int data_shndx,
74 const unsigned char* prelocs,
76 Output_section* output_section,
77 bool needs_special_offset_handling,
78 size_t local_symbol_count,
79 const unsigned char* plocal_symbols);
80 // Finalize the sections.
82 do_finalize_sections(Layout*);
84 // Return the value to use for a dynamic which requires special
87 do_dynsym_value(const Symbol*) const;
89 // Relocate a section.
91 relocate_section(const Relocate_info<size, big_endian>*,
93 const unsigned char* prelocs,
95 Output_section* output_section,
96 bool needs_special_offset_handling,
98 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
99 section_size_type view_size);
101 // Scan the relocs during a relocatable link.
103 scan_relocatable_relocs(const General_options& options,
104 Symbol_table* symtab,
106 Sized_relobj<size, big_endian>* object,
107 unsigned int data_shndx,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
111 Output_section* output_section,
112 bool needs_special_offset_handling,
113 size_t local_symbol_count,
114 const unsigned char* plocal_symbols,
115 Relocatable_relocs*);
117 // Relocate a section during a relocatable link.
119 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
120 unsigned int sh_type,
121 const unsigned char* prelocs,
123 Output_section* output_section,
124 off_t offset_in_output_section,
125 const Relocatable_relocs*,
127 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
128 section_size_type view_size,
129 unsigned char* reloc_view,
130 section_size_type reloc_view_size);
131 // Return whether SYM is defined by the ABI.
133 do_is_defined_by_abi(Symbol* sym) const
135 // XXX Really need to support this better...
136 if (sym->type() == elfcpp::STT_SPARC_REGISTER)
139 return strcmp(sym->name(), "___tls_get_addr") == 0;
142 // Return the size of the GOT section.
146 gold_assert(this->got_ != NULL);
147 return this->got_->data_size();
152 // The class which scans relocations.
157 : issued_non_pic_error_(false)
161 local(const General_options& options, Symbol_table* symtab,
162 Layout* layout, Target_sparc* target,
163 Sized_relobj<size, big_endian>* object,
164 unsigned int data_shndx,
165 Output_section* output_section,
166 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
167 const elfcpp::Sym<size, big_endian>& lsym);
170 global(const General_options& options, Symbol_table* symtab,
171 Layout* layout, Target_sparc* target,
172 Sized_relobj<size, big_endian>* object,
173 unsigned int data_shndx,
174 Output_section* output_section,
175 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
180 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
181 unsigned int r_type);
184 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
185 unsigned int r_type, Symbol*);
188 generate_tls_call(Symbol_table* symtab, Layout* layout,
189 Target_sparc* target);
192 check_non_pic(Relobj*, unsigned int r_type);
194 // Whether we have issued an error about a non-PIC compilation.
195 bool issued_non_pic_error_;
198 // The class which implements relocation.
203 : ignore_gd_add_(false)
208 if (this->ignore_gd_add_)
210 // FIXME: This needs to specify the location somehow.
211 gold_error(_("missing expected TLS relocation"));
215 // Do a relocation. Return false if the caller should not issue
216 // any warnings about this relocation.
218 relocate(const Relocate_info<size, big_endian>*, Target_sparc*,
219 size_t relnum, const elfcpp::Rela<size, big_endian>&,
220 unsigned int r_type, const Sized_symbol<size>*,
221 const Symbol_value<size>*,
223 typename elfcpp::Elf_types<size>::Elf_Addr,
227 // Do a TLS relocation.
229 relocate_tls(const Relocate_info<size, big_endian>*, Target_sparc* target,
230 size_t relnum, const elfcpp::Rela<size, big_endian>&,
231 unsigned int r_type, const Sized_symbol<size>*,
232 const Symbol_value<size>*,
234 typename elfcpp::Elf_types<size>::Elf_Addr,
237 // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
241 // A class which returns the size required for a relocation type,
242 // used while scanning relocs during a relocatable link.
243 class Relocatable_size_for_reloc
247 get_size_for_reloc(unsigned int, Relobj*);
250 // Get the GOT section, creating it if necessary.
251 Output_data_got<size, big_endian>*
252 got_section(Symbol_table*, Layout*);
254 // Create a PLT entry for a global symbol.
256 make_plt_entry(Symbol_table*, Layout*, Symbol*);
258 // Create a GOT entry for the TLS module index.
260 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
261 Sized_relobj<size, big_endian>* object);
263 // Return the gsym for "__tls_get_addr". Cache if not already
266 tls_get_addr_sym(Symbol_table* symtab)
268 if (!this->tls_get_addr_sym_)
269 this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
270 gold_assert(this->tls_get_addr_sym_);
271 return this->tls_get_addr_sym_;
274 // Get the PLT section.
275 const Output_data_plt_sparc<size, big_endian>*
278 gold_assert(this->plt_ != NULL);
282 // Get the dynamic reloc section, creating it if necessary.
284 rela_dyn_section(Layout*);
286 // Return true if the symbol may need a COPY relocation.
287 // References from an executable object to non-function symbols
288 // defined in a dynamic object may need a COPY relocation.
290 may_need_copy_reloc(Symbol* gsym)
292 return (!parameters->options().shared()
293 && gsym->is_from_dynobj()
294 && gsym->type() != elfcpp::STT_FUNC);
297 // Copy a relocation against a global symbol.
299 copy_reloc(Symbol_table* symtab, Layout* layout, Relobj* object,
300 unsigned int shndx, Output_section* output_section,
301 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
303 this->copy_relocs_.copy_reloc(symtab, layout,
304 symtab->get_sized_symbol<size>(sym),
305 object, shndx, output_section,
306 reloc, this->rela_dyn_section(layout));
309 // Information about this specific target which we pass to the
310 // general Target structure.
311 static Target::Target_info sparc_info;
313 // The types of GOT entries needed for this platform.
316 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
317 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
318 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
322 Output_data_got<size, big_endian>* got_;
324 Output_data_plt_sparc<size, big_endian>* plt_;
325 // The dynamic reloc section.
326 Reloc_section* rela_dyn_;
327 // Relocs saved to avoid a COPY reloc.
328 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
329 // Space for variables copied with a COPY reloc.
330 Output_data_space* dynbss_;
331 // Offset of the GOT entry for the TLS module index;
332 unsigned int got_mod_index_offset_;
333 // Cached pointer to __tls_get_addr symbol
334 Symbol* tls_get_addr_sym_;
338 Target::Target_info Target_sparc<32, true>::sparc_info =
341 true, // is_big_endian
342 elfcpp::EM_SPARC, // machine_code
343 false, // has_make_symbol
344 false, // has_resolve
345 false, // has_code_fill
346 true, // is_default_stack_executable
348 "/usr/lib/ld.so.1", // dynamic_linker
349 0x00010000, // default_text_segment_address
350 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
351 8 * 1024 // common_pagesize (overridable by -z common-page-size)
355 Target::Target_info Target_sparc<64, true>::sparc_info =
358 true, // is_big_endian
359 elfcpp::EM_SPARCV9, // machine_code
360 false, // has_make_symbol
361 false, // has_resolve
362 false, // has_code_fill
363 true, // is_default_stack_executable
365 "/usr/lib/sparcv9/ld.so.1", // dynamic_linker
366 0x100000, // default_text_segment_address
367 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
368 8 * 1024 // common_pagesize (overridable by -z common-page-size)
371 // We have to take care here, even when operating in little-endian
372 // mode, sparc instructions are still big endian.
373 template<int size, bool big_endian>
374 class Sparc_relocate_functions
377 // Do a simple relocation with the addend in the relocation.
378 template<int valsize>
380 rela(unsigned char* view,
381 unsigned int right_shift,
382 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
383 typename elfcpp::Swap<size, big_endian>::Valtype value,
384 typename elfcpp::Swap<size, big_endian>::Valtype addend)
386 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
387 Valtype* wv = reinterpret_cast<Valtype*>(view);
388 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
389 Valtype reloc = ((value + addend) >> right_shift);
394 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
397 // Do a simple relocation using a symbol value with the addend in
399 template<int valsize>
401 rela(unsigned char* view,
402 unsigned int right_shift,
403 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
404 const Sized_relobj<size, big_endian>* object,
405 const Symbol_value<size>* psymval,
406 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
408 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
409 Valtype* wv = reinterpret_cast<Valtype*>(view);
410 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
411 Valtype reloc = (psymval->value(object, addend) >> right_shift);
416 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
419 // Do a simple relocation using a symbol value with the addend in
420 // the relocation, unaligned.
421 template<int valsize>
423 rela_ua(unsigned char* view,
424 unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
425 const Sized_relobj<size, big_endian>* object,
426 const Symbol_value<size>* psymval,
427 typename elfcpp::Swap<size, big_endian>::Valtype addend)
429 typedef typename elfcpp::Swap_unaligned<valsize,
430 big_endian>::Valtype Valtype;
431 unsigned char* wv = view;
432 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
433 Valtype reloc = (psymval->value(object, addend) >> right_shift);
438 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
441 // Do a simple PC relative relocation with a Symbol_value with the
442 // addend in the relocation.
443 template<int valsize>
445 pcrela(unsigned char* view,
446 unsigned int right_shift,
447 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
448 const Sized_relobj<size, big_endian>* object,
449 const Symbol_value<size>* psymval,
450 typename elfcpp::Swap<size, big_endian>::Valtype addend,
451 typename elfcpp::Elf_types<size>::Elf_Addr address)
453 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
454 Valtype* wv = reinterpret_cast<Valtype*>(view);
455 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
456 Valtype reloc = ((psymval->value(object, addend) - address)
462 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
465 template<int valsize>
467 pcrela_unaligned(unsigned char* view,
468 const Sized_relobj<size, big_endian>* object,
469 const Symbol_value<size>* psymval,
470 typename elfcpp::Swap<size, big_endian>::Valtype addend,
471 typename elfcpp::Elf_types<size>::Elf_Addr address)
473 typedef typename elfcpp::Swap_unaligned<valsize,
474 big_endian>::Valtype Valtype;
475 unsigned char* wv = view;
476 Valtype reloc = (psymval->value(object, addend) - address);
478 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
481 typedef Sparc_relocate_functions<size, big_endian> This;
482 typedef Sparc_relocate_functions<size, true> This_insn;
485 // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
487 wdisp30(unsigned char* view,
488 const Sized_relobj<size, big_endian>* object,
489 const Symbol_value<size>* psymval,
490 typename elfcpp::Elf_types<size>::Elf_Addr addend,
491 typename elfcpp::Elf_types<size>::Elf_Addr address)
493 This_insn::template pcrela<32>(view, 2, 0x3fffffff, object,
494 psymval, addend, address);
497 // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
499 wdisp22(unsigned char* view,
500 const Sized_relobj<size, big_endian>* object,
501 const Symbol_value<size>* psymval,
502 typename elfcpp::Elf_types<size>::Elf_Addr addend,
503 typename elfcpp::Elf_types<size>::Elf_Addr address)
505 This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
506 psymval, addend, address);
509 // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
511 wdisp19(unsigned char* view,
512 const Sized_relobj<size, big_endian>* object,
513 const Symbol_value<size>* psymval,
514 typename elfcpp::Elf_types<size>::Elf_Addr addend,
515 typename elfcpp::Elf_types<size>::Elf_Addr address)
517 This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
518 psymval, addend, address);
521 // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
523 wdisp16(unsigned char* view,
524 const Sized_relobj<size, big_endian>* object,
525 const Symbol_value<size>* psymval,
526 typename elfcpp::Elf_types<size>::Elf_Addr addend,
527 typename elfcpp::Elf_types<size>::Elf_Addr address)
529 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
530 Valtype* wv = reinterpret_cast<Valtype*>(view);
531 Valtype val = elfcpp::Swap<32, true>::readval(wv);
532 Valtype reloc = ((psymval->value(object, addend) - address)
535 // The relocation value is split between the low 14 bits,
537 val &= ~((0x3 << 20) | 0x3fff);
538 reloc = (((reloc & 0xc000) << (20 - 14))
539 | (reloc & 0x3ffff));
541 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
544 // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
546 pc22(unsigned char* view,
547 const Sized_relobj<size, big_endian>* object,
548 const Symbol_value<size>* psymval,
549 typename elfcpp::Elf_types<size>::Elf_Addr addend,
550 typename elfcpp::Elf_types<size>::Elf_Addr address)
552 This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
553 psymval, addend, address);
556 // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
558 pc10(unsigned char* view,
559 const Sized_relobj<size, big_endian>* object,
560 const Symbol_value<size>* psymval,
561 typename elfcpp::Elf_types<size>::Elf_Addr addend,
562 typename elfcpp::Elf_types<size>::Elf_Addr address)
564 This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
565 psymval, addend, address);
568 // R_SPARC_HI22: (Symbol + Addend) >> 10
570 hi22(unsigned char* view,
571 typename elfcpp::Elf_types<size>::Elf_Addr value,
572 typename elfcpp::Elf_types<size>::Elf_Addr addend)
574 This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
577 // R_SPARC_HI22: (Symbol + Addend) >> 10
579 hi22(unsigned char* view,
580 const Sized_relobj<size, big_endian>* object,
581 const Symbol_value<size>* psymval,
582 typename elfcpp::Elf_types<size>::Elf_Addr addend)
584 This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
587 // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
589 pcplt22(unsigned char* view,
590 const Sized_relobj<size, big_endian>* object,
591 const Symbol_value<size>* psymval,
592 typename elfcpp::Elf_types<size>::Elf_Addr addend,
593 typename elfcpp::Elf_types<size>::Elf_Addr address)
595 This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
596 psymval, addend, address);
599 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
601 lo10(unsigned char* view,
602 typename elfcpp::Elf_types<size>::Elf_Addr value,
603 typename elfcpp::Elf_types<size>::Elf_Addr addend)
605 This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
608 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
610 lo10(unsigned char* view,
611 const Sized_relobj<size, big_endian>* object,
612 const Symbol_value<size>* psymval,
613 typename elfcpp::Elf_types<size>::Elf_Addr addend)
615 This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
618 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
620 lo10(unsigned char* view,
621 const Sized_relobj<size, big_endian>* object,
622 const Symbol_value<size>* psymval,
623 typename elfcpp::Elf_types<size>::Elf_Addr addend,
624 typename elfcpp::Elf_types<size>::Elf_Addr address)
626 This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
627 psymval, addend, address);
630 // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
632 olo10(unsigned char* view,
633 const Sized_relobj<size, big_endian>* object,
634 const Symbol_value<size>* psymval,
635 typename elfcpp::Elf_types<size>::Elf_Addr addend,
636 typename elfcpp::Elf_types<size>::Elf_Addr addend2)
638 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
639 Valtype* wv = reinterpret_cast<Valtype*>(view);
640 Valtype val = elfcpp::Swap<32, true>::readval(wv);
641 Valtype reloc = psymval->value(object, addend);
648 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
651 // R_SPARC_22: (Symbol + Addend)
653 rela32_22(unsigned char* view,
654 const Sized_relobj<size, big_endian>* object,
655 const Symbol_value<size>* psymval,
656 typename elfcpp::Elf_types<size>::Elf_Addr addend)
658 This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
661 // R_SPARC_13: (Symbol + Addend)
663 rela32_13(unsigned char* view,
664 typename elfcpp::Elf_types<size>::Elf_Addr value,
665 typename elfcpp::Elf_types<size>::Elf_Addr addend)
667 This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
670 // R_SPARC_13: (Symbol + Addend)
672 rela32_13(unsigned char* view,
673 const Sized_relobj<size, big_endian>* object,
674 const Symbol_value<size>* psymval,
675 typename elfcpp::Elf_types<size>::Elf_Addr addend)
677 This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
680 // R_SPARC_UA16: (Symbol + Addend)
682 ua16(unsigned char* view,
683 const Sized_relobj<size, big_endian>* object,
684 const Symbol_value<size>* psymval,
685 typename elfcpp::Elf_types<size>::Elf_Addr addend)
687 This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
690 // R_SPARC_UA32: (Symbol + Addend)
692 ua32(unsigned char* view,
693 const Sized_relobj<size, big_endian>* object,
694 const Symbol_value<size>* psymval,
695 typename elfcpp::Elf_types<size>::Elf_Addr addend)
697 This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
700 // R_SPARC_UA64: (Symbol + Addend)
702 ua64(unsigned char* view,
703 const Sized_relobj<size, big_endian>* object,
704 const Symbol_value<size>* psymval,
705 typename elfcpp::Elf_types<size>::Elf_Addr addend)
707 This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
708 object, psymval, addend);
711 // R_SPARC_DISP8: (Symbol + Addend - Address)
713 disp8(unsigned char* view,
714 const Sized_relobj<size, big_endian>* object,
715 const Symbol_value<size>* psymval,
716 typename elfcpp::Elf_types<size>::Elf_Addr addend,
717 typename elfcpp::Elf_types<size>::Elf_Addr address)
719 This::template pcrela_unaligned<8>(view, object, psymval,
723 // R_SPARC_DISP16: (Symbol + Addend - Address)
725 disp16(unsigned char* view,
726 const Sized_relobj<size, big_endian>* object,
727 const Symbol_value<size>* psymval,
728 typename elfcpp::Elf_types<size>::Elf_Addr addend,
729 typename elfcpp::Elf_types<size>::Elf_Addr address)
731 This::template pcrela_unaligned<16>(view, object, psymval,
735 // R_SPARC_DISP32: (Symbol + Addend - Address)
737 disp32(unsigned char* view,
738 const Sized_relobj<size, big_endian>* object,
739 const Symbol_value<size>* psymval,
740 typename elfcpp::Elf_types<size>::Elf_Addr addend,
741 typename elfcpp::Elf_types<size>::Elf_Addr address)
743 This::template pcrela_unaligned<32>(view, object, psymval,
747 // R_SPARC_DISP64: (Symbol + Addend - Address)
749 disp64(unsigned char* view,
750 const Sized_relobj<size, big_endian>* object,
751 const Symbol_value<size>* psymval,
752 elfcpp::Elf_Xword addend,
753 typename elfcpp::Elf_types<size>::Elf_Addr address)
755 This::template pcrela_unaligned<64>(view, object, psymval,
759 // R_SPARC_H44: (Symbol + Addend) >> 22
761 h44(unsigned char* view,
762 const Sized_relobj<size, big_endian>* object,
763 const Symbol_value<size>* psymval,
764 typename elfcpp::Elf_types<size>::Elf_Addr addend)
766 This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
769 // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
771 m44(unsigned char* view,
772 const Sized_relobj<size, big_endian>* object,
773 const Symbol_value<size>* psymval,
774 typename elfcpp::Elf_types<size>::Elf_Addr addend)
776 This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
779 // R_SPARC_L44: (Symbol + Addend) & 0xfff
781 l44(unsigned char* view,
782 const Sized_relobj<size, big_endian>* object,
783 const Symbol_value<size>* psymval,
784 typename elfcpp::Elf_types<size>::Elf_Addr addend)
786 This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
789 // R_SPARC_HH22: (Symbol + Addend) >> 42
791 hh22(unsigned char* view,
792 const Sized_relobj<size, big_endian>* object,
793 const Symbol_value<size>* psymval,
794 typename elfcpp::Elf_types<size>::Elf_Addr addend)
796 This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
799 // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
801 pc_hh22(unsigned char* view,
802 const Sized_relobj<size, big_endian>* object,
803 const Symbol_value<size>* psymval,
804 typename elfcpp::Elf_types<size>::Elf_Addr addend,
805 typename elfcpp::Elf_types<size>::Elf_Addr address)
807 This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
808 psymval, addend, address);
811 // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
813 hm10(unsigned char* view,
814 const Sized_relobj<size, big_endian>* object,
815 const Symbol_value<size>* psymval,
816 typename elfcpp::Elf_types<size>::Elf_Addr addend)
818 This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
821 // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
823 pc_hm10(unsigned char* view,
824 const Sized_relobj<size, big_endian>* object,
825 const Symbol_value<size>* psymval,
826 typename elfcpp::Elf_types<size>::Elf_Addr addend,
827 typename elfcpp::Elf_types<size>::Elf_Addr address)
829 This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
830 psymval, addend, address);
833 // R_SPARC_11: (Symbol + Addend)
835 rela32_11(unsigned char* view,
836 const Sized_relobj<size, big_endian>* object,
837 const Symbol_value<size>* psymval,
838 typename elfcpp::Elf_types<size>::Elf_Addr addend)
840 This_insn::template rela<32>(view, 0, 0x000007ff, object, psymval, addend);
843 // R_SPARC_10: (Symbol + Addend)
845 rela32_10(unsigned char* view,
846 const Sized_relobj<size, big_endian>* object,
847 const Symbol_value<size>* psymval,
848 typename elfcpp::Elf_types<size>::Elf_Addr addend)
850 This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
853 // R_SPARC_7: (Symbol + Addend)
855 rela32_7(unsigned char* view,
856 const Sized_relobj<size, big_endian>* object,
857 const Symbol_value<size>* psymval,
858 typename elfcpp::Elf_types<size>::Elf_Addr addend)
860 This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
863 // R_SPARC_6: (Symbol + Addend)
865 rela32_6(unsigned char* view,
866 const Sized_relobj<size, big_endian>* object,
867 const Symbol_value<size>* psymval,
868 typename elfcpp::Elf_types<size>::Elf_Addr addend)
870 This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
873 // R_SPARC_5: (Symbol + Addend)
875 rela32_5(unsigned char* view,
876 const Sized_relobj<size, big_endian>* object,
877 const Symbol_value<size>* psymval,
878 typename elfcpp::Elf_types<size>::Elf_Addr addend)
880 This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
883 // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
885 ldo_hix22(unsigned char* view,
886 typename elfcpp::Elf_types<size>::Elf_Addr value,
887 typename elfcpp::Elf_types<size>::Elf_Addr addend)
889 This_insn::hi22(view, value, addend);
892 // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
894 ldo_lox10(unsigned char* view,
895 typename elfcpp::Elf_types<size>::Elf_Addr value,
896 typename elfcpp::Elf_types<size>::Elf_Addr addend)
898 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
899 Valtype* wv = reinterpret_cast<Valtype*>(view);
900 Valtype val = elfcpp::Swap<32, true>::readval(wv);
901 Valtype reloc = (value + addend);
906 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
909 // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
911 hix22(unsigned char* view,
912 typename elfcpp::Elf_types<size>::Elf_Addr value,
913 typename elfcpp::Elf_types<size>::Elf_Addr addend)
915 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
916 Valtype* wv = reinterpret_cast<Valtype*>(view);
917 Valtype val = elfcpp::Swap<32, true>::readval(wv);
918 Valtype reloc = (value + addend);
922 reloc ^= ~(Valtype)0;
927 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
930 // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
932 hix22(unsigned char* view,
933 const Sized_relobj<size, big_endian>* object,
934 const Symbol_value<size>* psymval,
935 typename elfcpp::Elf_types<size>::Elf_Addr addend)
937 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
938 Valtype* wv = reinterpret_cast<Valtype*>(view);
939 Valtype val = elfcpp::Swap<32, true>::readval(wv);
940 Valtype reloc = psymval->value(object, addend);
944 reloc ^= ~(Valtype)0;
949 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
953 // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
955 lox10(unsigned char* view,
956 typename elfcpp::Elf_types<size>::Elf_Addr value,
957 typename elfcpp::Elf_types<size>::Elf_Addr addend)
959 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
960 Valtype* wv = reinterpret_cast<Valtype*>(view);
961 Valtype val = elfcpp::Swap<32, true>::readval(wv);
962 Valtype reloc = (value + addend);
968 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
971 // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
973 lox10(unsigned char* view,
974 const Sized_relobj<size, big_endian>* object,
975 const Symbol_value<size>* psymval,
976 typename elfcpp::Elf_types<size>::Elf_Addr addend)
978 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
979 Valtype* wv = reinterpret_cast<Valtype*>(view);
980 Valtype val = elfcpp::Swap<32, true>::readval(wv);
981 Valtype reloc = psymval->value(object, addend);
987 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
991 // Get the GOT section, creating it if necessary.
993 template<int size, bool big_endian>
994 Output_data_got<size, big_endian>*
995 Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
998 if (this->got_ == NULL)
1000 gold_assert(symtab != NULL && layout != NULL);
1002 this->got_ = new Output_data_got<size, big_endian>();
1005 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1007 | elfcpp::SHF_WRITE),
1011 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1012 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1014 0, 0, elfcpp::STT_OBJECT,
1016 elfcpp::STV_HIDDEN, 0,
1023 // Get the dynamic reloc section, creating it if necessary.
1025 template<int size, bool big_endian>
1026 typename Target_sparc<size, big_endian>::Reloc_section*
1027 Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
1029 if (this->rela_dyn_ == NULL)
1031 gold_assert(layout != NULL);
1032 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1033 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1034 elfcpp::SHF_ALLOC, this->rela_dyn_);
1036 return this->rela_dyn_;
1039 // A class to handle the PLT data.
1041 template<int size, bool big_endian>
1042 class Output_data_plt_sparc : public Output_section_data
1045 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1046 size, big_endian> Reloc_section;
1048 Output_data_plt_sparc(Layout*);
1050 // Add an entry to the PLT.
1051 void add_entry(Symbol* gsym);
1053 // Return the .rela.plt section data.
1054 const Reloc_section* rel_plt() const
1060 void do_adjust_output_section(Output_section* os);
1063 // The size of an entry in the PLT.
1064 static const int base_plt_entry_size = (size == 32 ? 12 : 32);
1066 static const unsigned int plt_entries_per_block = 160;
1067 static const unsigned int plt_insn_chunk_size = 24;
1068 static const unsigned int plt_pointer_chunk_size = 8;
1069 static const unsigned int plt_block_size =
1070 (plt_entries_per_block
1071 * (plt_insn_chunk_size + plt_pointer_chunk_size));
1073 // Set the final size.
1075 set_final_data_size()
1077 unsigned int full_count = this->count_ + 4;
1078 unsigned int extra = (size == 32 ? 4 : 0);
1080 if (size == 32 || full_count < 32768)
1081 this->set_data_size((full_count * base_plt_entry_size) + extra);
1084 unsigned int ext_cnt = full_count - 32768;
1086 this->set_data_size((32768 * base_plt_entry_size)
1088 * (plt_insn_chunk_size
1089 + plt_pointer_chunk_size)));
1093 // Write out the PLT data.
1095 do_write(Output_file*);
1097 // The reloc section.
1098 Reloc_section* rel_;
1099 // The number of PLT entries.
1100 unsigned int count_;
1103 // Define the constants as required by C++ standard.
1105 template<int size, bool big_endian>
1106 const int Output_data_plt_sparc<size, big_endian>::base_plt_entry_size;
1108 template<int size, bool big_endian>
1110 Output_data_plt_sparc<size, big_endian>::plt_entries_per_block;
1112 template<int size, bool big_endian>
1113 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_insn_chunk_size;
1115 template<int size, bool big_endian>
1117 Output_data_plt_sparc<size, big_endian>::plt_pointer_chunk_size;
1119 template<int size, bool big_endian>
1120 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_block_size;
1122 // Create the PLT section. The ordinary .got section is an argument,
1123 // since we need to refer to the start.
1125 template<int size, bool big_endian>
1126 Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
1127 : Output_section_data(size == 32 ? 4 : 8), count_(0)
1129 this->rel_ = new Reloc_section(false);
1130 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1131 elfcpp::SHF_ALLOC, this->rel_);
1134 template<int size, bool big_endian>
1136 Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
1141 // Add an entry to the PLT.
1143 template<int size, bool big_endian>
1145 Output_data_plt_sparc<size, big_endian>::add_entry(Symbol* gsym)
1147 gold_assert(!gsym->has_plt_offset());
1149 unsigned int index = this->count_ + 4;
1150 section_offset_type plt_offset;
1152 if (size == 32 || index < 32768)
1153 plt_offset = index * base_plt_entry_size;
1156 unsigned int ext_index = index - 32768;
1158 plt_offset = (32768 * base_plt_entry_size)
1159 + ((ext_index / plt_entries_per_block)
1161 + ((ext_index % plt_entries_per_block)
1162 * plt_insn_chunk_size);
1165 gsym->set_plt_offset(plt_offset);
1169 // Every PLT entry needs a reloc.
1170 gsym->set_needs_dynsym_entry();
1171 this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
1174 // Note that we don't need to save the symbol. The contents of the
1175 // PLT are independent of which symbols are used. The symbols only
1176 // appear in the relocations.
1179 static const unsigned int sparc_nop = 0x01000000;
1180 static const unsigned int sparc_sethi_g1 = 0x03000000;
1181 static const unsigned int sparc_branch_always = 0x30800000;
1182 static const unsigned int sparc_branch_always_pt = 0x30680000;
1183 static const unsigned int sparc_mov = 0x80100000;
1184 static const unsigned int sparc_mov_g0_o0 = 0x90100000;
1185 static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
1186 static const unsigned int sparc_call_plus_8 = 0x40000002;
1187 static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
1188 static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
1189 static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
1191 // Write out the PLT.
1193 template<int size, bool big_endian>
1195 Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
1197 const off_t offset = this->offset();
1198 const section_size_type oview_size =
1199 convert_to_section_size_type(this->data_size());
1200 unsigned char* const oview = of->get_output_view(offset, oview_size);
1201 unsigned char* pov = oview;
1203 memset(pov, 0, base_plt_entry_size * 4);
1204 pov += base_plt_entry_size * 4;
1206 unsigned int plt_offset = base_plt_entry_size * 4;
1207 const unsigned int count = this->count_;
1213 limit = (count > 32768 ? 32768 : count);
1215 for (unsigned int i = 0; i < limit; ++i)
1217 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1218 sparc_sethi_g1 + plt_offset);
1219 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1220 sparc_branch_always_pt +
1221 (((base_plt_entry_size -
1222 (plt_offset + 4)) >> 2) &
1224 elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1225 elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
1226 elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
1227 elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
1228 elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
1229 elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
1231 pov += base_plt_entry_size;
1232 plt_offset += base_plt_entry_size;
1237 unsigned int ext_cnt = count - 32768;
1238 unsigned int blks = ext_cnt / plt_entries_per_block;
1240 for (unsigned int i = 0; i < blks; ++i)
1242 unsigned int data_off = (plt_entries_per_block
1243 * plt_insn_chunk_size) - 4;
1245 for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1247 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1249 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1251 elfcpp::Swap<32, true>::writeval(pov + 0x08,
1253 elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1254 sparc_ldx_o7_imm_g1 +
1255 (data_off & 0x1fff));
1256 elfcpp::Swap<32, true>::writeval(pov + 0x10,
1257 sparc_jmpl_o7_g1_g1);
1258 elfcpp::Swap<32, true>::writeval(pov + 0x14,
1261 elfcpp::Swap<64, big_endian>::writeval(
1262 pov + 0x4 + data_off,
1263 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1265 pov += plt_insn_chunk_size;
1270 unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
1271 for (unsigned int i = 0; i < sub_blk_cnt; ++i)
1273 unsigned int data_off = (sub_blk_cnt
1274 * plt_insn_chunk_size) - 4;
1276 for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1278 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1280 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1282 elfcpp::Swap<32, true>::writeval(pov + 0x08,
1284 elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1285 sparc_ldx_o7_imm_g1 +
1286 (data_off & 0x1fff));
1287 elfcpp::Swap<32, true>::writeval(pov + 0x10,
1288 sparc_jmpl_o7_g1_g1);
1289 elfcpp::Swap<32, true>::writeval(pov + 0x14,
1292 elfcpp::Swap<64, big_endian>::writeval(
1293 pov + 0x4 + data_off,
1294 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1296 pov += plt_insn_chunk_size;
1304 for (unsigned int i = 0; i < count; ++i)
1306 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1307 sparc_sethi_g1 + plt_offset);
1308 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1309 sparc_branch_always +
1310 (((- (plt_offset + 4)) >> 2) &
1312 elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1314 pov += base_plt_entry_size;
1315 plt_offset += base_plt_entry_size;
1318 elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
1322 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1324 of->write_output_view(offset, oview_size, oview);
1327 // Create a PLT entry for a global symbol.
1329 template<int size, bool big_endian>
1331 Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1335 if (gsym->has_plt_offset())
1338 if (this->plt_ == NULL)
1340 // Create the GOT sections first.
1341 this->got_section(symtab, layout);
1343 this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
1344 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1346 | elfcpp::SHF_EXECINSTR
1347 | elfcpp::SHF_WRITE),
1350 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1351 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1353 0, 0, elfcpp::STT_OBJECT,
1355 elfcpp::STV_HIDDEN, 0,
1359 this->plt_->add_entry(gsym);
1362 // Create a GOT entry for the TLS module index.
1364 template<int size, bool big_endian>
1366 Target_sparc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
1368 Sized_relobj<size, big_endian>* object)
1370 if (this->got_mod_index_offset_ == -1U)
1372 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1373 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1374 Output_data_got<size, big_endian>* got;
1375 unsigned int got_offset;
1377 got = this->got_section(symtab, layout);
1378 got_offset = got->add_constant(0);
1379 rela_dyn->add_local(object, 0,
1381 elfcpp::R_SPARC_TLS_DTPMOD64 :
1382 elfcpp::R_SPARC_TLS_DTPMOD32), got,
1384 got->add_constant(0);
1385 this->got_mod_index_offset_ = got_offset;
1387 return this->got_mod_index_offset_;
1390 // Optimize the TLS relocation type based on what we know about the
1391 // symbol. IS_FINAL is true if the final address of this symbol is
1392 // known at link time.
1394 static tls::Tls_optimization
1395 optimize_tls_reloc(bool is_final, int r_type)
1397 // If we are generating a shared library, then we can't do anything
1399 if (parameters->options().shared())
1400 return tls::TLSOPT_NONE;
1404 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1405 case elfcpp::R_SPARC_TLS_GD_LO10:
1406 case elfcpp::R_SPARC_TLS_GD_ADD:
1407 case elfcpp::R_SPARC_TLS_GD_CALL:
1408 // These are General-Dynamic which permits fully general TLS
1409 // access. Since we know that we are generating an executable,
1410 // we can convert this to Initial-Exec. If we also know that
1411 // this is a local symbol, we can further switch to Local-Exec.
1413 return tls::TLSOPT_TO_LE;
1414 return tls::TLSOPT_TO_IE;
1416 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
1417 case elfcpp::R_SPARC_TLS_LDM_LO10:
1418 case elfcpp::R_SPARC_TLS_LDM_ADD:
1419 case elfcpp::R_SPARC_TLS_LDM_CALL:
1420 // This is Local-Dynamic, which refers to a local symbol in the
1421 // dynamic TLS block. Since we know that we generating an
1422 // executable, we can switch to Local-Exec.
1423 return tls::TLSOPT_TO_LE;
1425 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1426 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1427 case elfcpp::R_SPARC_TLS_LDO_ADD:
1428 // Another type of Local-Dynamic relocation.
1429 return tls::TLSOPT_TO_LE;
1431 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1432 case elfcpp::R_SPARC_TLS_IE_LO10:
1433 case elfcpp::R_SPARC_TLS_IE_LD:
1434 case elfcpp::R_SPARC_TLS_IE_LDX:
1435 // These are Initial-Exec relocs which get the thread offset
1436 // from the GOT. If we know that we are linking against the
1437 // local symbol, we can switch to Local-Exec, which links the
1438 // thread offset into the instruction.
1440 return tls::TLSOPT_TO_LE;
1441 return tls::TLSOPT_NONE;
1443 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1444 case elfcpp::R_SPARC_TLS_LE_LOX10:
1445 // When we already have Local-Exec, there is nothing further we
1447 return tls::TLSOPT_NONE;
1454 // Generate a PLT entry slot for a call to __tls_get_addr
1455 template<int size, bool big_endian>
1457 Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
1459 Target_sparc<size, big_endian>* target)
1461 Symbol* gsym = target->tls_get_addr_sym(symtab);
1463 target->make_plt_entry(symtab, layout, gsym);
1466 // Report an unsupported relocation against a local symbol.
1468 template<int size, bool big_endian>
1470 Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
1471 Sized_relobj<size, big_endian>* object,
1472 unsigned int r_type)
1474 gold_error(_("%s: unsupported reloc %u against local symbol"),
1475 object->name().c_str(), r_type);
1478 // We are about to emit a dynamic relocation of type R_TYPE. If the
1479 // dynamic linker does not support it, issue an error.
1481 template<int size, bool big_endian>
1483 Target_sparc<size, big_endian>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1485 gold_assert(r_type != elfcpp::R_SPARC_NONE);
1491 // These are the relocation types supported by glibc for sparc 64-bit.
1492 case elfcpp::R_SPARC_RELATIVE:
1493 case elfcpp::R_SPARC_COPY:
1494 case elfcpp::R_SPARC_64:
1495 case elfcpp::R_SPARC_GLOB_DAT:
1496 case elfcpp::R_SPARC_JMP_SLOT:
1497 case elfcpp::R_SPARC_TLS_DTPMOD64:
1498 case elfcpp::R_SPARC_TLS_DTPOFF64:
1499 case elfcpp::R_SPARC_TLS_TPOFF64:
1500 case elfcpp::R_SPARC_TLS_LE_HIX22:
1501 case elfcpp::R_SPARC_TLS_LE_LOX10:
1502 case elfcpp::R_SPARC_8:
1503 case elfcpp::R_SPARC_16:
1504 case elfcpp::R_SPARC_DISP8:
1505 case elfcpp::R_SPARC_DISP16:
1506 case elfcpp::R_SPARC_DISP32:
1507 case elfcpp::R_SPARC_WDISP30:
1508 case elfcpp::R_SPARC_LO10:
1509 case elfcpp::R_SPARC_HI22:
1510 case elfcpp::R_SPARC_OLO10:
1511 case elfcpp::R_SPARC_H44:
1512 case elfcpp::R_SPARC_M44:
1513 case elfcpp::R_SPARC_L44:
1514 case elfcpp::R_SPARC_HH22:
1515 case elfcpp::R_SPARC_HM10:
1516 case elfcpp::R_SPARC_LM22:
1517 case elfcpp::R_SPARC_UA16:
1518 case elfcpp::R_SPARC_UA32:
1519 case elfcpp::R_SPARC_UA64:
1530 // These are the relocation types supported by glibc for sparc 32-bit.
1531 case elfcpp::R_SPARC_RELATIVE:
1532 case elfcpp::R_SPARC_COPY:
1533 case elfcpp::R_SPARC_GLOB_DAT:
1534 case elfcpp::R_SPARC_32:
1535 case elfcpp::R_SPARC_JMP_SLOT:
1536 case elfcpp::R_SPARC_TLS_DTPMOD32:
1537 case elfcpp::R_SPARC_TLS_DTPOFF32:
1538 case elfcpp::R_SPARC_TLS_TPOFF32:
1539 case elfcpp::R_SPARC_TLS_LE_HIX22:
1540 case elfcpp::R_SPARC_TLS_LE_LOX10:
1541 case elfcpp::R_SPARC_8:
1542 case elfcpp::R_SPARC_16:
1543 case elfcpp::R_SPARC_DISP8:
1544 case elfcpp::R_SPARC_DISP16:
1545 case elfcpp::R_SPARC_DISP32:
1546 case elfcpp::R_SPARC_LO10:
1547 case elfcpp::R_SPARC_WDISP30:
1548 case elfcpp::R_SPARC_HI22:
1549 case elfcpp::R_SPARC_UA16:
1550 case elfcpp::R_SPARC_UA32:
1558 // This prevents us from issuing more than one error per reloc
1559 // section. But we can still wind up issuing more than one
1560 // error per object file.
1561 if (this->issued_non_pic_error_)
1563 object->error(_("requires unsupported dynamic reloc; "
1564 "recompile with -fPIC"));
1565 this->issued_non_pic_error_ = true;
1569 // Scan a relocation for a local symbol.
1571 template<int size, bool big_endian>
1573 Target_sparc<size, big_endian>::Scan::local(
1574 const General_options&,
1575 Symbol_table* symtab,
1577 Target_sparc<size, big_endian>* target,
1578 Sized_relobj<size, big_endian>* object,
1579 unsigned int data_shndx,
1580 Output_section* output_section,
1581 const elfcpp::Rela<size, big_endian>& reloc,
1582 unsigned int r_type,
1583 const elfcpp::Sym<size, big_endian>& lsym)
1585 unsigned int orig_r_type = r_type;
1590 case elfcpp::R_SPARC_NONE:
1591 case elfcpp::R_SPARC_REGISTER:
1592 case elfcpp::R_SPARC_GNU_VTINHERIT:
1593 case elfcpp::R_SPARC_GNU_VTENTRY:
1596 case elfcpp::R_SPARC_64:
1597 case elfcpp::R_SPARC_32:
1598 // If building a shared library (or a position-independent
1599 // executable), we need to create a dynamic relocation for
1600 // this location. The relocation applied at link time will
1601 // apply the link-time value, so we flag the location with
1602 // an R_SPARC_RELATIVE relocation so the dynamic loader can
1603 // relocate it easily.
1604 if (parameters->options().output_is_position_independent())
1606 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1607 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1608 rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
1609 output_section, data_shndx,
1610 reloc.get_r_offset(),
1611 reloc.get_r_addend());
1615 case elfcpp::R_SPARC_HIX22:
1616 case elfcpp::R_SPARC_LOX10:
1617 case elfcpp::R_SPARC_H44:
1618 case elfcpp::R_SPARC_M44:
1619 case elfcpp::R_SPARC_L44:
1620 case elfcpp::R_SPARC_HH22:
1621 case elfcpp::R_SPARC_HM10:
1622 case elfcpp::R_SPARC_LM22:
1623 case elfcpp::R_SPARC_UA64:
1624 case elfcpp::R_SPARC_UA32:
1625 case elfcpp::R_SPARC_UA16:
1626 case elfcpp::R_SPARC_HI22:
1627 case elfcpp::R_SPARC_LO10:
1628 case elfcpp::R_SPARC_OLO10:
1629 case elfcpp::R_SPARC_16:
1630 case elfcpp::R_SPARC_11:
1631 case elfcpp::R_SPARC_10:
1632 case elfcpp::R_SPARC_8:
1633 case elfcpp::R_SPARC_7:
1634 case elfcpp::R_SPARC_6:
1635 case elfcpp::R_SPARC_5:
1636 // If building a shared library (or a position-independent
1637 // executable), we need to create a dynamic relocation for
1639 if (parameters->options().output_is_position_independent())
1641 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1643 check_non_pic(object, r_type);
1644 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1646 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1647 rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
1648 data_shndx, reloc.get_r_offset(),
1649 reloc.get_r_addend());
1653 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1654 gold_assert(lsym.get_st_value() == 0);
1655 rela_dyn->add_local_relative(object, r_sym, orig_r_type,
1656 output_section, data_shndx,
1657 reloc.get_r_offset(),
1658 reloc.get_r_addend());
1663 case elfcpp::R_SPARC_WDISP30:
1664 case elfcpp::R_SPARC_WDISP22:
1665 case elfcpp::R_SPARC_WDISP19:
1666 case elfcpp::R_SPARC_WDISP16:
1667 case elfcpp::R_SPARC_DISP8:
1668 case elfcpp::R_SPARC_DISP16:
1669 case elfcpp::R_SPARC_DISP32:
1670 case elfcpp::R_SPARC_DISP64:
1671 case elfcpp::R_SPARC_PC10:
1672 case elfcpp::R_SPARC_PC22:
1675 case elfcpp::R_SPARC_GOT10:
1676 case elfcpp::R_SPARC_GOT13:
1677 case elfcpp::R_SPARC_GOT22:
1679 // The symbol requires a GOT entry.
1680 Output_data_got<size, big_endian>* got;
1683 got = target->got_section(symtab, layout);
1684 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1686 // If we are generating a shared object, we need to add a
1687 // dynamic relocation for this symbol's GOT entry.
1688 if (parameters->options().output_is_position_independent())
1690 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1692 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1695 off = got->add_constant(0);
1696 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1697 rela_dyn->add_local_relative(object, r_sym,
1698 elfcpp::R_SPARC_RELATIVE,
1703 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1707 // These are initial TLS relocs, which are expected when
1709 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1710 case elfcpp::R_SPARC_TLS_GD_LO10:
1711 case elfcpp::R_SPARC_TLS_GD_ADD:
1712 case elfcpp::R_SPARC_TLS_GD_CALL:
1713 case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
1714 case elfcpp::R_SPARC_TLS_LDM_LO10:
1715 case elfcpp::R_SPARC_TLS_LDM_ADD:
1716 case elfcpp::R_SPARC_TLS_LDM_CALL:
1717 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1718 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1719 case elfcpp::R_SPARC_TLS_LDO_ADD:
1720 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1721 case elfcpp::R_SPARC_TLS_IE_LO10:
1722 case elfcpp::R_SPARC_TLS_IE_LD:
1723 case elfcpp::R_SPARC_TLS_IE_LDX:
1724 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1725 case elfcpp::R_SPARC_TLS_LE_LOX10:
1727 bool output_is_shared = parameters->options().shared();
1728 const tls::Tls_optimization optimized_type
1729 = optimize_tls_reloc(!output_is_shared, r_type);
1732 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1733 case elfcpp::R_SPARC_TLS_GD_LO10:
1734 case elfcpp::R_SPARC_TLS_GD_ADD:
1735 case elfcpp::R_SPARC_TLS_GD_CALL:
1736 if (optimized_type == tls::TLSOPT_NONE)
1738 // Create a pair of GOT entries for the module index and
1739 // dtv-relative offset.
1740 Output_data_got<size, big_endian>* got
1741 = target->got_section(symtab, layout);
1742 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1743 unsigned int shndx = lsym.get_st_shndx();
1745 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1747 object->error(_("local symbol %u has bad shndx %u"),
1750 got->add_local_pair_with_rela(object, r_sym,
1751 lsym.get_st_shndx(),
1753 target->rela_dyn_section(layout),
1755 ? elfcpp::R_SPARC_TLS_DTPMOD64
1756 : elfcpp::R_SPARC_TLS_DTPMOD32),
1758 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
1759 generate_tls_call(symtab, layout, target);
1761 else if (optimized_type != tls::TLSOPT_TO_LE)
1762 unsupported_reloc_local(object, r_type);
1765 case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
1766 case elfcpp::R_SPARC_TLS_LDM_LO10:
1767 case elfcpp::R_SPARC_TLS_LDM_ADD:
1768 case elfcpp::R_SPARC_TLS_LDM_CALL:
1769 if (optimized_type == tls::TLSOPT_NONE)
1771 // Create a GOT entry for the module index.
1772 target->got_mod_index_entry(symtab, layout, object);
1774 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
1775 generate_tls_call(symtab, layout, target);
1777 else if (optimized_type != tls::TLSOPT_TO_LE)
1778 unsupported_reloc_local(object, r_type);
1781 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1782 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1783 case elfcpp::R_SPARC_TLS_LDO_ADD:
1786 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1787 case elfcpp::R_SPARC_TLS_IE_LO10:
1788 case elfcpp::R_SPARC_TLS_IE_LD:
1789 case elfcpp::R_SPARC_TLS_IE_LDX:
1790 layout->set_has_static_tls();
1791 if (optimized_type == tls::TLSOPT_NONE)
1793 // Create a GOT entry for the tp-relative offset.
1794 Output_data_got<size, big_endian>* got
1795 = target->got_section(symtab, layout);
1796 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1798 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
1800 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1801 unsigned int off = got->add_constant(0);
1803 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET,
1805 rela_dyn->add_local_relative(object, r_sym,
1807 elfcpp::R_SPARC_TLS_TPOFF64 :
1808 elfcpp::R_SPARC_TLS_TPOFF32),
1812 else if (optimized_type != tls::TLSOPT_TO_LE)
1813 unsupported_reloc_local(object, r_type);
1816 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1817 case elfcpp::R_SPARC_TLS_LE_LOX10:
1818 layout->set_has_static_tls();
1819 if (output_is_shared)
1821 // We need to create a dynamic relocation.
1822 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1823 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1824 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1825 rela_dyn->add_local_relative(object, r_sym, r_type,
1826 output_section, data_shndx,
1827 reloc.get_r_offset(), 0);
1834 // These are relocations which should only be seen by the
1835 // dynamic linker, and should never be seen here.
1836 case elfcpp::R_SPARC_COPY:
1837 case elfcpp::R_SPARC_GLOB_DAT:
1838 case elfcpp::R_SPARC_JMP_SLOT:
1839 case elfcpp::R_SPARC_RELATIVE:
1840 case elfcpp::R_SPARC_TLS_DTPMOD64:
1841 case elfcpp::R_SPARC_TLS_DTPMOD32:
1842 case elfcpp::R_SPARC_TLS_DTPOFF64:
1843 case elfcpp::R_SPARC_TLS_DTPOFF32:
1844 case elfcpp::R_SPARC_TLS_TPOFF64:
1845 case elfcpp::R_SPARC_TLS_TPOFF32:
1846 gold_error(_("%s: unexpected reloc %u in object file"),
1847 object->name().c_str(), r_type);
1851 unsupported_reloc_local(object, r_type);
1856 // Report an unsupported relocation against a global symbol.
1858 template<int size, bool big_endian>
1860 Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
1861 Sized_relobj<size, big_endian>* object,
1862 unsigned int r_type,
1865 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1866 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1869 // Scan a relocation for a global symbol.
1871 template<int size, bool big_endian>
1873 Target_sparc<size, big_endian>::Scan::global(
1874 const General_options&,
1875 Symbol_table* symtab,
1877 Target_sparc<size, big_endian>* target,
1878 Sized_relobj<size, big_endian>* object,
1879 unsigned int data_shndx,
1880 Output_section* output_section,
1881 const elfcpp::Rela<size, big_endian>& reloc,
1882 unsigned int r_type,
1885 unsigned int orig_r_type = r_type;
1890 case elfcpp::R_SPARC_NONE:
1891 case elfcpp::R_SPARC_REGISTER:
1892 case elfcpp::R_SPARC_GNU_VTINHERIT:
1893 case elfcpp::R_SPARC_GNU_VTENTRY:
1896 case elfcpp::R_SPARC_PLT64:
1897 case elfcpp::R_SPARC_PLT32:
1898 case elfcpp::R_SPARC_HIPLT22:
1899 case elfcpp::R_SPARC_LOPLT10:
1900 case elfcpp::R_SPARC_PCPLT32:
1901 case elfcpp::R_SPARC_PCPLT22:
1902 case elfcpp::R_SPARC_PCPLT10:
1903 case elfcpp::R_SPARC_WPLT30:
1904 // If the symbol is fully resolved, this is just a PC32 reloc.
1905 // Otherwise we need a PLT entry.
1906 if (gsym->final_value_is_known())
1908 // If building a shared library, we can also skip the PLT entry
1909 // if the symbol is defined in the output file and is protected
1911 if (gsym->is_defined()
1912 && !gsym->is_from_dynobj()
1913 && !gsym->is_preemptible())
1915 target->make_plt_entry(symtab, layout, gsym);
1918 case elfcpp::R_SPARC_DISP8:
1919 case elfcpp::R_SPARC_DISP16:
1920 case elfcpp::R_SPARC_DISP32:
1921 case elfcpp::R_SPARC_DISP64:
1922 case elfcpp::R_SPARC_PC_HH22:
1923 case elfcpp::R_SPARC_PC_HM10:
1924 case elfcpp::R_SPARC_PC_LM22:
1925 case elfcpp::R_SPARC_PC10:
1926 case elfcpp::R_SPARC_PC22:
1927 case elfcpp::R_SPARC_WDISP30:
1928 case elfcpp::R_SPARC_WDISP22:
1929 case elfcpp::R_SPARC_WDISP19:
1930 case elfcpp::R_SPARC_WDISP16:
1932 if (gsym->needs_plt_entry())
1933 target->make_plt_entry(symtab, layout, gsym);
1934 // Make a dynamic relocation if necessary.
1935 int flags = Symbol::NON_PIC_REF;
1936 if (gsym->type() == elfcpp::STT_FUNC)
1937 flags |= Symbol::FUNCTION_CALL;
1938 if (gsym->needs_dynamic_reloc(flags))
1940 if (target->may_need_copy_reloc(gsym))
1942 target->copy_reloc(symtab, layout, object,
1943 data_shndx, output_section, gsym,
1948 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1949 check_non_pic(object, r_type);
1950 rela_dyn->add_global(gsym, orig_r_type, output_section, object,
1951 data_shndx, reloc.get_r_offset(),
1952 reloc.get_r_addend());
1958 case elfcpp::R_SPARC_UA64:
1959 case elfcpp::R_SPARC_64:
1960 case elfcpp::R_SPARC_HIX22:
1961 case elfcpp::R_SPARC_LOX10:
1962 case elfcpp::R_SPARC_H44:
1963 case elfcpp::R_SPARC_M44:
1964 case elfcpp::R_SPARC_L44:
1965 case elfcpp::R_SPARC_HH22:
1966 case elfcpp::R_SPARC_HM10:
1967 case elfcpp::R_SPARC_LM22:
1968 case elfcpp::R_SPARC_HI22:
1969 case elfcpp::R_SPARC_LO10:
1970 case elfcpp::R_SPARC_OLO10:
1971 case elfcpp::R_SPARC_UA32:
1972 case elfcpp::R_SPARC_32:
1973 case elfcpp::R_SPARC_UA16:
1974 case elfcpp::R_SPARC_16:
1975 case elfcpp::R_SPARC_11:
1976 case elfcpp::R_SPARC_10:
1977 case elfcpp::R_SPARC_8:
1978 case elfcpp::R_SPARC_7:
1979 case elfcpp::R_SPARC_6:
1980 case elfcpp::R_SPARC_5:
1982 // Make a PLT entry if necessary.
1983 if (gsym->needs_plt_entry())
1985 target->make_plt_entry(symtab, layout, gsym);
1986 // Since this is not a PC-relative relocation, we may be
1987 // taking the address of a function. In that case we need to
1988 // set the entry in the dynamic symbol table to the address of
1990 if (gsym->is_from_dynobj() && !parameters->options().shared())
1991 gsym->set_needs_dynsym_value();
1993 // Make a dynamic relocation if necessary.
1994 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1996 if (target->may_need_copy_reloc(gsym))
1998 target->copy_reloc(symtab, layout, object,
1999 data_shndx, output_section, gsym, reloc);
2001 else if ((r_type == elfcpp::R_SPARC_32
2002 || r_type == elfcpp::R_SPARC_64)
2003 && gsym->can_use_relative_reloc(false))
2005 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2006 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2007 output_section, object,
2008 data_shndx, reloc.get_r_offset(),
2009 reloc.get_r_addend());
2013 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2015 check_non_pic(object, r_type);
2016 if (gsym->is_from_dynobj()
2017 || gsym->is_undefined()
2018 || gsym->is_preemptible())
2019 rela_dyn->add_global(gsym, orig_r_type, output_section,
2021 reloc.get_r_offset(),
2022 reloc.get_r_addend());
2024 rela_dyn->add_global_relative(gsym, orig_r_type,
2025 output_section, object,
2027 reloc.get_r_offset(),
2028 reloc.get_r_addend());
2034 case elfcpp::R_SPARC_GOT10:
2035 case elfcpp::R_SPARC_GOT13:
2036 case elfcpp::R_SPARC_GOT22:
2038 // The symbol requires a GOT entry.
2039 Output_data_got<size, big_endian>* got;
2041 got = target->got_section(symtab, layout);
2042 if (gsym->final_value_is_known())
2043 got->add_global(gsym, GOT_TYPE_STANDARD);
2046 // If this symbol is not fully resolved, we need to add a
2047 // dynamic relocation for it.
2048 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2049 if (gsym->is_from_dynobj()
2050 || gsym->is_undefined()
2051 || gsym->is_preemptible())
2052 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2053 elfcpp::R_SPARC_GLOB_DAT);
2054 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2056 unsigned int off = got->add_constant(0);
2058 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2059 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2066 // These are initial tls relocs, which are expected when
2068 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2069 case elfcpp::R_SPARC_TLS_GD_LO10:
2070 case elfcpp::R_SPARC_TLS_GD_ADD:
2071 case elfcpp::R_SPARC_TLS_GD_CALL:
2072 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
2073 case elfcpp::R_SPARC_TLS_LDM_LO10:
2074 case elfcpp::R_SPARC_TLS_LDM_ADD:
2075 case elfcpp::R_SPARC_TLS_LDM_CALL:
2076 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2077 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2078 case elfcpp::R_SPARC_TLS_LDO_ADD:
2079 case elfcpp::R_SPARC_TLS_LE_HIX22:
2080 case elfcpp::R_SPARC_TLS_LE_LOX10:
2081 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2082 case elfcpp::R_SPARC_TLS_IE_LO10:
2083 case elfcpp::R_SPARC_TLS_IE_LD:
2084 case elfcpp::R_SPARC_TLS_IE_LDX:
2086 const bool is_final = gsym->final_value_is_known();
2087 const tls::Tls_optimization optimized_type
2088 = optimize_tls_reloc(is_final, r_type);
2091 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2092 case elfcpp::R_SPARC_TLS_GD_LO10:
2093 case elfcpp::R_SPARC_TLS_GD_ADD:
2094 case elfcpp::R_SPARC_TLS_GD_CALL:
2095 if (optimized_type == tls::TLSOPT_NONE)
2097 // Create a pair of GOT entries for the module index and
2098 // dtv-relative offset.
2099 Output_data_got<size, big_endian>* got
2100 = target->got_section(symtab, layout);
2101 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2102 target->rela_dyn_section(layout),
2104 elfcpp::R_SPARC_TLS_DTPMOD64 :
2105 elfcpp::R_SPARC_TLS_DTPMOD32),
2107 elfcpp::R_SPARC_TLS_DTPOFF64 :
2108 elfcpp::R_SPARC_TLS_DTPOFF32));
2110 // Emit R_SPARC_WPLT30 against "__tls_get_addr"
2111 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2112 generate_tls_call(symtab, layout, target);
2114 else if (optimized_type == tls::TLSOPT_TO_IE)
2116 // Create a GOT entry for the tp-relative offset.
2117 Output_data_got<size, big_endian>* got
2118 = target->got_section(symtab, layout);
2119 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2120 target->rela_dyn_section(layout),
2122 elfcpp::R_SPARC_TLS_TPOFF64 :
2123 elfcpp::R_SPARC_TLS_TPOFF32));
2125 else if (optimized_type != tls::TLSOPT_TO_LE)
2126 unsupported_reloc_global(object, r_type, gsym);
2129 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
2130 case elfcpp::R_SPARC_TLS_LDM_LO10:
2131 case elfcpp::R_SPARC_TLS_LDM_ADD:
2132 case elfcpp::R_SPARC_TLS_LDM_CALL:
2133 if (optimized_type == tls::TLSOPT_NONE)
2135 // Create a GOT entry for the module index.
2136 target->got_mod_index_entry(symtab, layout, object);
2138 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2139 generate_tls_call(symtab, layout, target);
2141 else if (optimized_type != tls::TLSOPT_TO_LE)
2142 unsupported_reloc_global(object, r_type, gsym);
2145 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2146 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2147 case elfcpp::R_SPARC_TLS_LDO_ADD:
2150 case elfcpp::R_SPARC_TLS_LE_HIX22:
2151 case elfcpp::R_SPARC_TLS_LE_LOX10:
2152 layout->set_has_static_tls();
2153 if (parameters->options().shared())
2155 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2156 rela_dyn->add_global_relative(gsym, orig_r_type,
2157 output_section, object,
2158 data_shndx, reloc.get_r_offset(),
2163 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2164 case elfcpp::R_SPARC_TLS_IE_LO10:
2165 case elfcpp::R_SPARC_TLS_IE_LD:
2166 case elfcpp::R_SPARC_TLS_IE_LDX:
2167 layout->set_has_static_tls();
2168 if (optimized_type == tls::TLSOPT_NONE)
2170 // Create a GOT entry for the tp-relative offset.
2171 Output_data_got<size, big_endian>* got
2172 = target->got_section(symtab, layout);
2173 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2174 target->rela_dyn_section(layout),
2176 elfcpp::R_SPARC_TLS_TPOFF64 :
2177 elfcpp::R_SPARC_TLS_TPOFF32));
2179 else if (optimized_type != tls::TLSOPT_TO_LE)
2180 unsupported_reloc_global(object, r_type, gsym);
2186 // These are relocations which should only be seen by the
2187 // dynamic linker, and should never be seen here.
2188 case elfcpp::R_SPARC_COPY:
2189 case elfcpp::R_SPARC_GLOB_DAT:
2190 case elfcpp::R_SPARC_JMP_SLOT:
2191 case elfcpp::R_SPARC_RELATIVE:
2192 case elfcpp::R_SPARC_TLS_DTPMOD64:
2193 case elfcpp::R_SPARC_TLS_DTPMOD32:
2194 case elfcpp::R_SPARC_TLS_DTPOFF64:
2195 case elfcpp::R_SPARC_TLS_DTPOFF32:
2196 case elfcpp::R_SPARC_TLS_TPOFF64:
2197 case elfcpp::R_SPARC_TLS_TPOFF32:
2198 gold_error(_("%s: unexpected reloc %u in object file"),
2199 object->name().c_str(), r_type);
2203 unsupported_reloc_global(object, r_type, gsym);
2208 // Scan relocations for a section.
2210 template<int size, bool big_endian>
2212 Target_sparc<size, big_endian>::scan_relocs(
2213 const General_options& options,
2214 Symbol_table* symtab,
2216 Sized_relobj<size, big_endian>* object,
2217 unsigned int data_shndx,
2218 unsigned int sh_type,
2219 const unsigned char* prelocs,
2221 Output_section* output_section,
2222 bool needs_special_offset_handling,
2223 size_t local_symbol_count,
2224 const unsigned char* plocal_symbols)
2226 typedef Target_sparc<size, big_endian> Sparc;
2227 typedef typename Target_sparc<size, big_endian>::Scan Scan;
2229 if (sh_type == elfcpp::SHT_REL)
2231 gold_error(_("%s: unsupported REL reloc section"),
2232 object->name().c_str());
2236 gold::scan_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan>(
2246 needs_special_offset_handling,
2251 // Finalize the sections.
2253 template<int size, bool big_endian>
2255 Target_sparc<size, big_endian>::do_finalize_sections(Layout* layout)
2257 // Fill in some more dynamic tags.
2258 Output_data_dynamic* const odyn = layout->dynamic_data();
2261 if (this->plt_ != NULL)
2263 const Output_data* od = this->plt_->rel_plt();
2264 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
2265 odyn->add_section_address(elfcpp::DT_JMPREL, od);
2266 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
2268 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
2271 if (this->rela_dyn_ != NULL)
2273 const Output_data* od = this->rela_dyn_;
2274 odyn->add_section_address(elfcpp::DT_RELA, od);
2275 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2276 odyn->add_constant(elfcpp::DT_RELAENT,
2277 elfcpp::Elf_sizes<size>::rela_size);
2280 if (!parameters->options().shared())
2282 // The value of the DT_DEBUG tag is filled in by the dynamic
2283 // linker at run time, and used by the debugger.
2284 odyn->add_constant(elfcpp::DT_DEBUG, 0);
2288 // Emit any relocs we saved in an attempt to avoid generating COPY
2290 if (this->copy_relocs_.any_saved_relocs())
2291 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2294 // Perform a relocation.
2296 template<int size, bool big_endian>
2298 Target_sparc<size, big_endian>::Relocate::relocate(
2299 const Relocate_info<size, big_endian>* relinfo,
2300 Target_sparc* target,
2302 const elfcpp::Rela<size, big_endian>& rela,
2303 unsigned int r_type,
2304 const Sized_symbol<size>* gsym,
2305 const Symbol_value<size>* psymval,
2306 unsigned char* view,
2307 typename elfcpp::Elf_types<size>::Elf_Addr address,
2308 section_size_type view_size)
2312 if (this->ignore_gd_add_)
2314 if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
2315 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2316 _("missing expected TLS relocation"));
2319 this->ignore_gd_add_ = false;
2324 typedef Sparc_relocate_functions<size, big_endian> Reloc;
2326 // Pick the value to use for symbols defined in shared objects.
2327 Symbol_value<size> symval;
2329 && (gsym->is_from_dynobj()
2330 || (parameters->options().shared()
2331 && (gsym->is_undefined() || gsym->is_preemptible())))
2332 && gsym->has_plt_offset())
2334 elfcpp::Elf_Xword value;
2336 value = target->plt_section()->address() + gsym->plt_offset();
2338 symval.set_output_value(value);
2343 const Sized_relobj<size, big_endian>* object = relinfo->object;
2344 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2346 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
2347 // pointer points to the beginning, not the end, of the table.
2348 // So we just use the plain offset.
2349 bool have_got_offset = false;
2350 unsigned int got_offset = 0;
2353 case elfcpp::R_SPARC_GOT10:
2354 case elfcpp::R_SPARC_GOT13:
2355 case elfcpp::R_SPARC_GOT22:
2358 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2359 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
2363 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2364 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2365 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2367 have_got_offset = true;
2376 case elfcpp::R_SPARC_NONE:
2377 case elfcpp::R_SPARC_REGISTER:
2378 case elfcpp::R_SPARC_GNU_VTINHERIT:
2379 case elfcpp::R_SPARC_GNU_VTENTRY:
2382 case elfcpp::R_SPARC_8:
2383 Relocate_functions<size, big_endian>::rela8(view, object,
2387 case elfcpp::R_SPARC_16:
2388 Relocate_functions<size, big_endian>::rela16(view, object,
2392 case elfcpp::R_SPARC_32:
2393 if (!parameters->options().output_is_position_independent())
2394 Relocate_functions<size, big_endian>::rela32(view, object,
2398 case elfcpp::R_SPARC_DISP8:
2399 Reloc::disp8(view, object, psymval, addend, address);
2402 case elfcpp::R_SPARC_DISP16:
2403 Reloc::disp16(view, object, psymval, addend, address);
2406 case elfcpp::R_SPARC_DISP32:
2407 Reloc::disp32(view, object, psymval, addend, address);
2410 case elfcpp::R_SPARC_DISP64:
2411 Reloc::disp64(view, object, psymval, addend, address);
2414 case elfcpp::R_SPARC_WDISP30:
2415 case elfcpp::R_SPARC_WPLT30:
2416 Reloc::wdisp30(view, object, psymval, addend, address);
2419 case elfcpp::R_SPARC_WDISP22:
2420 Reloc::wdisp22(view, object, psymval, addend, address);
2423 case elfcpp::R_SPARC_WDISP19:
2424 Reloc::wdisp19(view, object, psymval, addend, address);
2427 case elfcpp::R_SPARC_WDISP16:
2428 Reloc::wdisp16(view, object, psymval, addend, address);
2431 case elfcpp::R_SPARC_HI22:
2432 Reloc::hi22(view, object, psymval, addend);
2435 case elfcpp::R_SPARC_22:
2436 Reloc::rela32_22(view, object, psymval, addend);
2439 case elfcpp::R_SPARC_13:
2440 Reloc::rela32_13(view, object, psymval, addend);
2443 case elfcpp::R_SPARC_LO10:
2444 Reloc::lo10(view, object, psymval, addend);
2447 case elfcpp::R_SPARC_GOT10:
2448 Reloc::lo10(view, got_offset, addend);
2451 case elfcpp::R_SPARC_GOT13:
2452 Reloc::rela32_13(view, got_offset, addend);
2455 case elfcpp::R_SPARC_GOT22:
2456 Reloc::hi22(view, got_offset, addend);
2459 case elfcpp::R_SPARC_PC10:
2460 Reloc::pc10(view, object, psymval, addend, address);
2463 case elfcpp::R_SPARC_PC22:
2464 Reloc::pc22(view, object, psymval, addend, address);
2467 case elfcpp::R_SPARC_TLS_DTPOFF32:
2468 case elfcpp::R_SPARC_UA32:
2469 Reloc::ua32(view, object, psymval, addend);
2472 case elfcpp::R_SPARC_PLT64:
2473 Relocate_functions<size, big_endian>::rela64(view, object,
2477 case elfcpp::R_SPARC_PLT32:
2478 Relocate_functions<size, big_endian>::rela32(view, object,
2482 case elfcpp::R_SPARC_HIPLT22:
2483 Reloc::hi22(view, object, psymval, addend);
2486 case elfcpp::R_SPARC_LOPLT10:
2487 Reloc::lo10(view, object, psymval, addend);
2490 case elfcpp::R_SPARC_PCPLT32:
2491 Reloc::disp32(view, object, psymval, addend, address);
2494 case elfcpp::R_SPARC_PCPLT22:
2495 Reloc::pcplt22(view, object, psymval, addend, address);
2498 case elfcpp::R_SPARC_PCPLT10:
2499 Reloc::lo10(view, object, psymval, addend, address);
2502 case elfcpp::R_SPARC_64:
2503 if (!parameters->options().output_is_position_independent())
2504 Relocate_functions<size, big_endian>::rela64(view, object,
2508 case elfcpp::R_SPARC_OLO10:
2510 unsigned int addend2 = rela.get_r_info() & 0xffffffff;
2511 addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
2512 Reloc::olo10(view, object, psymval, addend, addend2);
2516 case elfcpp::R_SPARC_HH22:
2517 Reloc::hh22(view, object, psymval, addend);
2520 case elfcpp::R_SPARC_PC_HH22:
2521 Reloc::pc_hh22(view, object, psymval, addend, address);
2524 case elfcpp::R_SPARC_HM10:
2525 Reloc::hm10(view, object, psymval, addend);
2528 case elfcpp::R_SPARC_PC_HM10:
2529 Reloc::pc_hm10(view, object, psymval, addend, address);
2532 case elfcpp::R_SPARC_LM22:
2533 Reloc::hi22(view, object, psymval, addend);
2536 case elfcpp::R_SPARC_PC_LM22:
2537 Reloc::pcplt22(view, object, psymval, addend, address);
2540 case elfcpp::R_SPARC_11:
2541 Reloc::rela32_11(view, object, psymval, addend);
2544 case elfcpp::R_SPARC_10:
2545 Reloc::rela32_10(view, object, psymval, addend);
2548 case elfcpp::R_SPARC_7:
2549 Reloc::rela32_7(view, object, psymval, addend);
2552 case elfcpp::R_SPARC_6:
2553 Reloc::rela32_6(view, object, psymval, addend);
2556 case elfcpp::R_SPARC_5:
2557 Reloc::rela32_5(view, object, psymval, addend);
2560 case elfcpp::R_SPARC_HIX22:
2561 Reloc::hix22(view, object, psymval, addend);
2564 case elfcpp::R_SPARC_LOX10:
2565 Reloc::lox10(view, object, psymval, addend);
2568 case elfcpp::R_SPARC_H44:
2569 Reloc::h44(view, object, psymval, addend);
2572 case elfcpp::R_SPARC_M44:
2573 Reloc::m44(view, object, psymval, addend);
2576 case elfcpp::R_SPARC_L44:
2577 Reloc::l44(view, object, psymval, addend);
2580 case elfcpp::R_SPARC_TLS_DTPOFF64:
2581 case elfcpp::R_SPARC_UA64:
2582 Reloc::ua64(view, object, psymval, addend);
2585 case elfcpp::R_SPARC_UA16:
2586 Reloc::ua16(view, object, psymval, addend);
2589 case elfcpp::R_SPARC_TLS_GD_HI22:
2590 case elfcpp::R_SPARC_TLS_GD_LO10:
2591 case elfcpp::R_SPARC_TLS_GD_ADD:
2592 case elfcpp::R_SPARC_TLS_GD_CALL:
2593 case elfcpp::R_SPARC_TLS_LDM_HI22:
2594 case elfcpp::R_SPARC_TLS_LDM_LO10:
2595 case elfcpp::R_SPARC_TLS_LDM_ADD:
2596 case elfcpp::R_SPARC_TLS_LDM_CALL:
2597 case elfcpp::R_SPARC_TLS_LDO_HIX22:
2598 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2599 case elfcpp::R_SPARC_TLS_LDO_ADD:
2600 case elfcpp::R_SPARC_TLS_IE_HI22:
2601 case elfcpp::R_SPARC_TLS_IE_LO10:
2602 case elfcpp::R_SPARC_TLS_IE_LD:
2603 case elfcpp::R_SPARC_TLS_IE_LDX:
2604 case elfcpp::R_SPARC_TLS_LE_HIX22:
2605 case elfcpp::R_SPARC_TLS_LE_LOX10:
2606 this->relocate_tls(relinfo, target, relnum, rela,
2607 r_type, gsym, psymval, view,
2608 address, view_size);
2611 case elfcpp::R_SPARC_COPY:
2612 case elfcpp::R_SPARC_GLOB_DAT:
2613 case elfcpp::R_SPARC_JMP_SLOT:
2614 case elfcpp::R_SPARC_RELATIVE:
2615 // These are outstanding tls relocs, which are unexpected when
2617 case elfcpp::R_SPARC_TLS_DTPMOD64:
2618 case elfcpp::R_SPARC_TLS_DTPMOD32:
2619 case elfcpp::R_SPARC_TLS_TPOFF64:
2620 case elfcpp::R_SPARC_TLS_TPOFF32:
2621 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2622 _("unexpected reloc %u in object file"),
2627 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2628 _("unsupported reloc %u"),
2636 // Perform a TLS relocation.
2638 template<int size, bool big_endian>
2640 Target_sparc<size, big_endian>::Relocate::relocate_tls(
2641 const Relocate_info<size, big_endian>* relinfo,
2642 Target_sparc<size, big_endian>* target,
2644 const elfcpp::Rela<size, big_endian>& rela,
2645 unsigned int r_type,
2646 const Sized_symbol<size>* gsym,
2647 const Symbol_value<size>* psymval,
2648 unsigned char* view,
2649 typename elfcpp::Elf_types<size>::Elf_Addr address,
2652 Output_segment* tls_segment = relinfo->layout->tls_segment();
2653 typedef Sparc_relocate_functions<size, big_endian> Reloc;
2654 const Sized_relobj<size, big_endian>* object = relinfo->object;
2655 typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
2657 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2658 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2660 const bool is_final =
2662 ? !parameters->options().output_is_position_independent()
2663 : gsym->final_value_is_known());
2664 const tls::Tls_optimization optimized_type
2665 = optimize_tls_reloc(is_final, r_type);
2669 case elfcpp::R_SPARC_TLS_GD_HI22:
2670 case elfcpp::R_SPARC_TLS_GD_LO10:
2671 case elfcpp::R_SPARC_TLS_GD_ADD:
2672 case elfcpp::R_SPARC_TLS_GD_CALL:
2673 if (optimized_type == tls::TLSOPT_TO_LE)
2675 Insntype* wv = reinterpret_cast<Insntype*>(view);
2678 value -= tls_segment->memsz();
2682 case elfcpp::R_SPARC_TLS_GD_HI22:
2683 // TLS_GD_HI22 --> TLS_LE_HIX22
2684 Reloc::hix22(view, value, addend);
2687 case elfcpp::R_SPARC_TLS_GD_LO10:
2688 // TLS_GD_LO10 --> TLS_LE_LOX10
2689 Reloc::lox10(view, value, addend);
2692 case elfcpp::R_SPARC_TLS_GD_ADD:
2693 // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
2694 val = elfcpp::Swap<32, true>::readval(wv);
2695 val = (val & ~0x7c000) | 0x1c000;
2696 elfcpp::Swap<32, true>::writeval(wv, val);
2698 case elfcpp::R_SPARC_TLS_GD_CALL:
2699 // call __tls_get_addr --> nop
2700 elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
2707 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2708 ? GOT_TYPE_TLS_OFFSET
2709 : GOT_TYPE_TLS_PAIR);
2712 gold_assert(gsym->has_got_offset(got_type));
2713 value = gsym->got_offset(got_type);
2717 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2718 gold_assert(object->local_has_got_offset(r_sym, got_type));
2719 value = object->local_got_offset(r_sym, got_type);
2721 if (optimized_type == tls::TLSOPT_TO_IE)
2723 Insntype* wv = reinterpret_cast<Insntype*>(view);
2728 case elfcpp::R_SPARC_TLS_GD_HI22:
2729 // TLS_GD_HI22 --> TLS_IE_HI22
2730 Reloc::hi22(view, value, addend);
2733 case elfcpp::R_SPARC_TLS_GD_LO10:
2734 // TLS_GD_LO10 --> TLS_IE_LO10
2735 Reloc::lo10(view, value, addend);
2738 case elfcpp::R_SPARC_TLS_GD_ADD:
2739 // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
2740 val = elfcpp::Swap<32, true>::readval(wv);
2747 elfcpp::Swap<32, true>::writeval(wv, val);
2750 case elfcpp::R_SPARC_TLS_GD_CALL:
2751 // The compiler can put the TLS_GD_ADD instruction
2752 // into the delay slot of the call. If so, we need
2753 // to transpose the two instructions so that the
2754 // the new sequence works properly.
2756 // The test we use is if the instruction in the
2757 // delay slot is an add with destination register
2759 val = elfcpp::Swap<32, true>::readval(wv + 1);
2760 if ((val & 0x81f80000) == 0x80000000
2761 && ((val >> 25) & 0x1f) == 0x8)
2768 elfcpp::Swap<32, true>::writeval(wv, val);
2771 this->ignore_gd_add_ = true;
2774 // call __tls_get_addr --> add %g7, %o0, %o0
2775 elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
2780 else if (optimized_type == tls::TLSOPT_NONE)
2784 case elfcpp::R_SPARC_TLS_GD_HI22:
2785 Reloc::hi22(view, value, addend);
2787 case elfcpp::R_SPARC_TLS_GD_LO10:
2788 Reloc::lo10(view, value, addend);
2790 case elfcpp::R_SPARC_TLS_GD_ADD:
2792 case elfcpp::R_SPARC_TLS_GD_CALL:
2794 Symbol_value<size> symval;
2795 elfcpp::Elf_Xword value;
2798 tsym = target->tls_get_addr_sym_;
2800 value = (target->plt_section()->address() +
2801 tsym->plt_offset());
2802 symval.set_output_value(value);
2803 Reloc::wdisp30(view, object, &symval, addend, address);
2810 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2811 _("unsupported reloc %u"),
2815 case elfcpp::R_SPARC_TLS_LDM_HI22:
2816 case elfcpp::R_SPARC_TLS_LDM_LO10:
2817 case elfcpp::R_SPARC_TLS_LDM_ADD:
2818 case elfcpp::R_SPARC_TLS_LDM_CALL:
2819 if (optimized_type == tls::TLSOPT_TO_LE)
2821 Insntype* wv = reinterpret_cast<Insntype*>(view);
2825 case elfcpp::R_SPARC_TLS_LDM_HI22:
2826 case elfcpp::R_SPARC_TLS_LDM_LO10:
2827 case elfcpp::R_SPARC_TLS_LDM_ADD:
2828 elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
2831 case elfcpp::R_SPARC_TLS_LDM_CALL:
2832 elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
2837 else if (optimized_type == tls::TLSOPT_NONE)
2839 // Relocate the field with the offset of the GOT entry for
2840 // the module index.
2841 unsigned int got_offset;
2843 got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
2846 case elfcpp::R_SPARC_TLS_LDM_HI22:
2847 Reloc::hi22(view, got_offset, addend);
2849 case elfcpp::R_SPARC_TLS_LDM_LO10:
2850 Reloc::lo10(view, got_offset, addend);
2852 case elfcpp::R_SPARC_TLS_LDM_ADD:
2854 case elfcpp::R_SPARC_TLS_LDM_CALL:
2856 Symbol_value<size> symval;
2857 elfcpp::Elf_Xword value;
2860 tsym = target->tls_get_addr_sym_;
2862 value = (target->plt_section()->address() +
2863 tsym->plt_offset());
2864 symval.set_output_value(value);
2865 Reloc::wdisp30(view, object, &symval, addend, address);
2871 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2872 _("unsupported reloc %u"),
2876 // These relocs can appear in debugging sections, in which case
2877 // we won't see the TLS_LDM relocs. The local_dynamic_type
2878 // field tells us this.
2879 case elfcpp::R_SPARC_TLS_LDO_HIX22:
2880 if (optimized_type == tls::TLSOPT_TO_LE)
2882 value -= tls_segment->memsz();
2883 Reloc::hix22(view, value, addend);
2886 Reloc::ldo_hix22(view, value, addend);
2888 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2889 if (optimized_type == tls::TLSOPT_TO_LE)
2891 value -= tls_segment->memsz();
2892 Reloc::lox10(view, value, addend);
2895 Reloc::ldo_lox10(view, value, addend);
2897 case elfcpp::R_SPARC_TLS_LDO_ADD:
2898 if (optimized_type == tls::TLSOPT_TO_LE)
2900 Insntype* wv = reinterpret_cast<Insntype*>(view);
2903 // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
2904 val = elfcpp::Swap<32, true>::readval(wv);
2905 val = (val & ~0x7c000) | 0x1c000;
2906 elfcpp::Swap<32, true>::writeval(wv, val);
2910 // When optimizing IE --> LE, the only relocation that is handled
2911 // differently is R_SPARC_TLS_IE_LD, it is rewritten from
2912 // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
2913 // rs2 and rd are the same.
2914 case elfcpp::R_SPARC_TLS_IE_LD:
2915 case elfcpp::R_SPARC_TLS_IE_LDX:
2916 if (optimized_type == tls::TLSOPT_TO_LE)
2918 Insntype* wv = reinterpret_cast<Insntype*>(view);
2919 Insntype val = elfcpp::Swap<32, true>::readval(wv);
2920 Insntype rs2 = val & 0x1f;
2921 Insntype rd = (val >> 25) & 0x1f;
2926 val = sparc_mov | (val & 0x3e00001f);
2928 elfcpp::Swap<32, true>::writeval(wv, val);
2932 case elfcpp::R_SPARC_TLS_IE_HI22:
2933 case elfcpp::R_SPARC_TLS_IE_LO10:
2934 if (optimized_type == tls::TLSOPT_TO_LE)
2936 value -= tls_segment->memsz();
2939 case elfcpp::R_SPARC_TLS_IE_HI22:
2940 // IE_HI22 --> LE_HIX22
2941 Reloc::hix22(view, value, addend);
2943 case elfcpp::R_SPARC_TLS_IE_LO10:
2944 // IE_LO10 --> LE_LOX10
2945 Reloc::lox10(view, value, addend);
2950 else if (optimized_type == tls::TLSOPT_NONE)
2952 // Relocate the field with the offset of the GOT entry for
2953 // the tp-relative offset of the symbol.
2956 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2957 value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
2961 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2962 gold_assert(object->local_has_got_offset(r_sym,
2963 GOT_TYPE_TLS_OFFSET));
2964 value = object->local_got_offset(r_sym,
2965 GOT_TYPE_TLS_OFFSET);
2969 case elfcpp::R_SPARC_TLS_IE_HI22:
2970 Reloc::hi22(view, value, addend);
2972 case elfcpp::R_SPARC_TLS_IE_LO10:
2973 Reloc::lo10(view, value, addend);
2978 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2979 _("unsupported reloc %u"),
2983 case elfcpp::R_SPARC_TLS_LE_HIX22:
2984 // If we're creating a shared library, a dynamic relocation will
2985 // have been created for this location, so do not apply it now.
2986 if (!parameters->options().shared())
2988 value -= tls_segment->memsz();
2989 Reloc::hix22(view, value, addend);
2993 case elfcpp::R_SPARC_TLS_LE_LOX10:
2994 // If we're creating a shared library, a dynamic relocation will
2995 // have been created for this location, so do not apply it now.
2996 if (!parameters->options().shared())
2998 value -= tls_segment->memsz();
2999 Reloc::lox10(view, value, addend);
3005 // Relocate section data.
3007 template<int size, bool big_endian>
3009 Target_sparc<size, big_endian>::relocate_section(
3010 const Relocate_info<size, big_endian>* relinfo,
3011 unsigned int sh_type,
3012 const unsigned char* prelocs,
3014 Output_section* output_section,
3015 bool needs_special_offset_handling,
3016 unsigned char* view,
3017 typename elfcpp::Elf_types<size>::Elf_Addr address,
3018 section_size_type view_size)
3020 typedef Target_sparc<size, big_endian> Sparc;
3021 typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
3023 gold_assert(sh_type == elfcpp::SHT_RELA);
3025 gold::relocate_section<size, big_endian, Sparc, elfcpp::SHT_RELA,
3032 needs_special_offset_handling,
3038 // Return the size of a relocation while scanning during a relocatable
3041 template<int size, bool big_endian>
3043 Target_sparc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3047 // We are always SHT_RELA, so we should never get here.
3052 // Scan the relocs during a relocatable link.
3054 template<int size, bool big_endian>
3056 Target_sparc<size, big_endian>::scan_relocatable_relocs(
3057 const General_options& options,
3058 Symbol_table* symtab,
3060 Sized_relobj<size, big_endian>* object,
3061 unsigned int data_shndx,
3062 unsigned int sh_type,
3063 const unsigned char* prelocs,
3065 Output_section* output_section,
3066 bool needs_special_offset_handling,
3067 size_t local_symbol_count,
3068 const unsigned char* plocal_symbols,
3069 Relocatable_relocs* rr)
3071 gold_assert(sh_type == elfcpp::SHT_RELA);
3073 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3074 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3076 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3077 Scan_relocatable_relocs>(
3086 needs_special_offset_handling,
3092 // Relocate a section during a relocatable link.
3094 template<int size, bool big_endian>
3096 Target_sparc<size, big_endian>::relocate_for_relocatable(
3097 const Relocate_info<size, big_endian>* relinfo,
3098 unsigned int sh_type,
3099 const unsigned char* prelocs,
3101 Output_section* output_section,
3102 off_t offset_in_output_section,
3103 const Relocatable_relocs* rr,
3104 unsigned char* view,
3105 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3106 section_size_type view_size,
3107 unsigned char* reloc_view,
3108 section_size_type reloc_view_size)
3110 gold_assert(sh_type == elfcpp::SHT_RELA);
3112 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
3117 offset_in_output_section,
3126 // Return the value to use for a dynamic which requires special
3127 // treatment. This is how we support equality comparisons of function
3128 // pointers across shared library boundaries, as described in the
3129 // processor specific ABI supplement.
3131 template<int size, bool big_endian>
3133 Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
3135 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3136 return this->plt_section()->address() + gsym->plt_offset();
3139 // The selector for sparc object files.
3141 template<int size, bool big_endian>
3142 class Target_selector_sparc : public Target_selector
3145 Target_selector_sparc()
3146 : Target_selector(elfcpp::EM_NONE, size, big_endian,
3147 (size == 64 ? "elf64-sparc" : "elf32-sparc"))
3150 Target* instantiated_target_;
3152 Target* do_recognize(int machine, int, int)
3157 if (machine != elfcpp::EM_SPARCV9)
3162 if (machine != elfcpp::EM_SPARC
3163 && machine != elfcpp::EM_SPARC32PLUS)
3171 return do_instantiate_target();
3174 Target* do_instantiate_target()
3176 if (this->instantiated_target_ == NULL)
3177 this->instantiated_target_ = new Target_sparc<size, big_endian>();
3178 return this->instantiated_target_;
3182 Target_selector_sparc<32, true> target_selector_sparc32;
3183 Target_selector_sparc<64, true> target_selector_sparc64;
3185 } // End anonymous namespace.