1 // sparc.cc -- sparc target support for gold.
3 // Copyright 2008, 2009, 2010 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"
50 template<int size, bool big_endian>
51 class Output_data_plt_sparc;
53 template<int size, bool big_endian>
54 class Target_sparc : public Sized_target<size, big_endian>
57 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
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)
67 // Process the relocations to determine unreferenced sections for
68 // garbage collection.
70 gc_process_relocs(Symbol_table* symtab,
72 Sized_relobj_file<size, big_endian>* object,
73 unsigned int data_shndx,
75 const unsigned char* prelocs,
77 Output_section* output_section,
78 bool needs_special_offset_handling,
79 size_t local_symbol_count,
80 const unsigned char* plocal_symbols);
82 // Scan the relocations to look for symbol adjustments.
84 scan_relocs(Symbol_table* symtab,
86 Sized_relobj_file<size, big_endian>* object,
87 unsigned int data_shndx,
89 const unsigned char* prelocs,
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.
97 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
99 // Return the value to use for a dynamic which requires special
102 do_dynsym_value(const Symbol*) const;
104 // Relocate a section.
106 relocate_section(const Relocate_info<size, big_endian>*,
107 unsigned int sh_type,
108 const unsigned char* prelocs,
110 Output_section* output_section,
111 bool needs_special_offset_handling,
113 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
114 section_size_type view_size,
115 const Reloc_symbol_changes*);
117 // Scan the relocs during a relocatable link.
119 scan_relocatable_relocs(Symbol_table* symtab,
121 Sized_relobj_file<size, big_endian>* object,
122 unsigned int data_shndx,
123 unsigned int sh_type,
124 const unsigned char* prelocs,
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*);
132 // Relocate a section during a relocatable link.
134 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
135 unsigned int sh_type,
136 const unsigned char* prelocs,
138 Output_section* output_section,
139 off_t offset_in_output_section,
140 const Relocatable_relocs*,
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.
148 do_is_defined_by_abi(const Symbol* sym) const
150 // XXX Really need to support this better...
151 if (sym->type() == elfcpp::STT_SPARC_REGISTER)
154 return strcmp(sym->name(), "___tls_get_addr") == 0;
157 // Return whether there is a GOT section.
159 has_got_section() const
160 { return this->got_ != NULL; }
162 // Return the size of the GOT section.
166 gold_assert(this->got_ != NULL);
167 return this->got_->data_size();
170 // Return the number of entries in the GOT.
172 got_entry_count() const
174 if (this->got_ == NULL)
176 return this->got_size() / (size / 8);
179 // Return the number of entries in the PLT.
181 plt_entry_count() const;
183 // Return the offset of the first non-reserved PLT entry.
185 first_plt_entry_offset() const;
187 // Return the size of each PLT entry.
189 plt_entry_size() const;
193 // The class which scans relocations.
198 : issued_non_pic_error_(false)
202 get_reference_flags(unsigned int r_type);
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);
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,
221 local_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
223 Sized_relobj_file<size, big_endian>* ,
226 const elfcpp::Rela<size, big_endian>& ,
228 const elfcpp::Sym<size, big_endian>&)
232 global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
234 Sized_relobj_file<size, big_endian>* ,
237 const elfcpp::Rela<size,
239 unsigned int , Symbol*)
245 unsupported_reloc_local(Sized_relobj_file<size, big_endian>*,
246 unsigned int r_type);
249 unsupported_reloc_global(Sized_relobj_file<size, big_endian>*,
250 unsigned int r_type, Symbol*);
253 generate_tls_call(Symbol_table* symtab, Layout* layout,
254 Target_sparc* target);
257 check_non_pic(Relobj*, unsigned int r_type);
259 // Whether we have issued an error about a non-PIC compilation.
260 bool issued_non_pic_error_;
263 // The class which implements relocation.
268 : ignore_gd_add_(false)
273 if (this->ignore_gd_add_)
275 // FIXME: This needs to specify the location somehow.
276 gold_error(_("missing expected TLS relocation"));
280 // Do a relocation. Return false if the caller should not issue
281 // any warnings about this relocation.
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>*,
289 typename elfcpp::Elf_types<size>::Elf_Addr,
293 // Do a TLS relocation.
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>*,
300 typename elfcpp::Elf_types<size>::Elf_Addr,
303 // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
307 // A class which returns the size required for a relocation type,
308 // used while scanning relocs during a relocatable link.
309 class Relocatable_size_for_reloc
313 get_size_for_reloc(unsigned int, Relobj*);
316 // Get the GOT section, creating it if necessary.
317 Output_data_got<size, big_endian>*
318 got_section(Symbol_table*, Layout*);
320 // Create a PLT entry for a global symbol.
322 make_plt_entry(Symbol_table*, Layout*, Symbol*);
324 // Create a GOT entry for the TLS module index.
326 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
327 Sized_relobj_file<size, big_endian>* object);
329 // Return the gsym for "__tls_get_addr". Cache if not already
332 tls_get_addr_sym(Symbol_table* symtab)
334 if (!this->tls_get_addr_sym_)
335 this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
336 gold_assert(this->tls_get_addr_sym_);
337 return this->tls_get_addr_sym_;
340 // Get the PLT section.
341 const Output_data_plt_sparc<size, big_endian>*
344 gold_assert(this->plt_ != NULL);
348 // Get the dynamic reloc section, creating it if necessary.
350 rela_dyn_section(Layout*);
352 // Copy a relocation against a global symbol.
354 copy_reloc(Symbol_table* symtab, Layout* layout,
355 Sized_relobj_file<size, big_endian>* object,
356 unsigned int shndx, Output_section* output_section,
357 Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
359 this->copy_relocs_.copy_reloc(symtab, layout,
360 symtab->get_sized_symbol<size>(sym),
361 object, shndx, output_section,
362 reloc, this->rela_dyn_section(layout));
365 // Information about this specific target which we pass to the
366 // general Target structure.
367 static Target::Target_info sparc_info;
369 // The types of GOT entries needed for this platform.
370 // These values are exposed to the ABI in an incremental link.
371 // Do not renumber existing values without changing the version
372 // number of the .gnu_incremental_inputs section.
375 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
376 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
377 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
381 Output_data_got<size, big_endian>* got_;
383 Output_data_plt_sparc<size, big_endian>* plt_;
384 // The dynamic reloc section.
385 Reloc_section* rela_dyn_;
386 // Relocs saved to avoid a COPY reloc.
387 Copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocs_;
388 // Space for variables copied with a COPY reloc.
389 Output_data_space* dynbss_;
390 // Offset of the GOT entry for the TLS module index;
391 unsigned int got_mod_index_offset_;
392 // Cached pointer to __tls_get_addr symbol
393 Symbol* tls_get_addr_sym_;
397 Target::Target_info Target_sparc<32, true>::sparc_info =
400 true, // is_big_endian
401 elfcpp::EM_SPARC, // machine_code
402 false, // has_make_symbol
403 false, // has_resolve
404 false, // has_code_fill
405 true, // is_default_stack_executable
407 "/usr/lib/ld.so.1", // dynamic_linker
408 0x00010000, // default_text_segment_address
409 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
410 8 * 1024, // common_pagesize (overridable by -z common-page-size)
411 elfcpp::SHN_UNDEF, // small_common_shndx
412 elfcpp::SHN_UNDEF, // large_common_shndx
413 0, // small_common_section_flags
414 0, // large_common_section_flags
415 NULL, // attributes_section
416 NULL // attributes_vendor
420 Target::Target_info Target_sparc<64, true>::sparc_info =
423 true, // is_big_endian
424 elfcpp::EM_SPARCV9, // machine_code
425 false, // has_make_symbol
426 false, // has_resolve
427 false, // has_code_fill
428 true, // is_default_stack_executable
430 "/usr/lib/sparcv9/ld.so.1", // dynamic_linker
431 0x100000, // default_text_segment_address
432 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
433 8 * 1024, // common_pagesize (overridable by -z common-page-size)
434 elfcpp::SHN_UNDEF, // small_common_shndx
435 elfcpp::SHN_UNDEF, // large_common_shndx
436 0, // small_common_section_flags
437 0, // large_common_section_flags
438 NULL, // attributes_section
439 NULL // attributes_vendor
442 // We have to take care here, even when operating in little-endian
443 // mode, sparc instructions are still big endian.
444 template<int size, bool big_endian>
445 class Sparc_relocate_functions
448 // Do a simple relocation with the addend in the relocation.
449 template<int valsize>
451 rela(unsigned char* view,
452 unsigned int right_shift,
453 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
454 typename elfcpp::Swap<size, big_endian>::Valtype value,
455 typename elfcpp::Swap<size, big_endian>::Valtype addend)
457 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
458 Valtype* wv = reinterpret_cast<Valtype*>(view);
459 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
460 Valtype reloc = ((value + addend) >> right_shift);
465 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
468 // Do a simple relocation using a symbol value with the addend in
470 template<int valsize>
472 rela(unsigned char* view,
473 unsigned int right_shift,
474 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
475 const Sized_relobj_file<size, big_endian>* object,
476 const Symbol_value<size>* psymval,
477 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
479 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
480 Valtype* wv = reinterpret_cast<Valtype*>(view);
481 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
482 Valtype reloc = (psymval->value(object, addend) >> right_shift);
487 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
490 // Do a simple relocation using a symbol value with the addend in
491 // the relocation, unaligned.
492 template<int valsize>
494 rela_ua(unsigned char* view,
495 unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
496 const Sized_relobj_file<size, big_endian>* object,
497 const Symbol_value<size>* psymval,
498 typename elfcpp::Swap<size, big_endian>::Valtype addend)
500 typedef typename elfcpp::Swap_unaligned<valsize,
501 big_endian>::Valtype Valtype;
502 unsigned char* wv = view;
503 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
504 Valtype reloc = (psymval->value(object, addend) >> right_shift);
509 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
512 // Do a simple PC relative relocation with a Symbol_value with the
513 // addend in the relocation.
514 template<int valsize>
516 pcrela(unsigned char* view,
517 unsigned int right_shift,
518 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
519 const Sized_relobj_file<size, big_endian>* object,
520 const Symbol_value<size>* psymval,
521 typename elfcpp::Swap<size, big_endian>::Valtype addend,
522 typename elfcpp::Elf_types<size>::Elf_Addr address)
524 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
525 Valtype* wv = reinterpret_cast<Valtype*>(view);
526 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
527 Valtype reloc = ((psymval->value(object, addend) - address)
533 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
536 template<int valsize>
538 pcrela_unaligned(unsigned char* view,
539 const Sized_relobj_file<size, big_endian>* object,
540 const Symbol_value<size>* psymval,
541 typename elfcpp::Swap<size, big_endian>::Valtype addend,
542 typename elfcpp::Elf_types<size>::Elf_Addr address)
544 typedef typename elfcpp::Swap_unaligned<valsize,
545 big_endian>::Valtype Valtype;
546 unsigned char* wv = view;
547 Valtype reloc = (psymval->value(object, addend) - address);
549 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
552 typedef Sparc_relocate_functions<size, big_endian> This;
553 typedef Sparc_relocate_functions<size, true> This_insn;
556 // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
558 wdisp30(unsigned char* view,
559 const Sized_relobj_file<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, 2, 0x3fffffff, object,
565 psymval, addend, address);
568 // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
570 wdisp22(unsigned char* view,
571 const Sized_relobj_file<size, big_endian>* object,
572 const Symbol_value<size>* psymval,
573 typename elfcpp::Elf_types<size>::Elf_Addr addend,
574 typename elfcpp::Elf_types<size>::Elf_Addr address)
576 This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
577 psymval, addend, address);
580 // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
582 wdisp19(unsigned char* view,
583 const Sized_relobj_file<size, big_endian>* object,
584 const Symbol_value<size>* psymval,
585 typename elfcpp::Elf_types<size>::Elf_Addr addend,
586 typename elfcpp::Elf_types<size>::Elf_Addr address)
588 This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
589 psymval, addend, address);
592 // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
594 wdisp16(unsigned char* view,
595 const Sized_relobj_file<size, big_endian>* object,
596 const Symbol_value<size>* psymval,
597 typename elfcpp::Elf_types<size>::Elf_Addr addend,
598 typename elfcpp::Elf_types<size>::Elf_Addr address)
600 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
601 Valtype* wv = reinterpret_cast<Valtype*>(view);
602 Valtype val = elfcpp::Swap<32, true>::readval(wv);
603 Valtype reloc = ((psymval->value(object, addend) - address)
606 // The relocation value is split between the low 14 bits,
608 val &= ~((0x3 << 20) | 0x3fff);
609 reloc = (((reloc & 0xc000) << (20 - 14))
610 | (reloc & 0x3ffff));
612 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
615 // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
617 pc22(unsigned char* view,
618 const Sized_relobj_file<size, big_endian>* object,
619 const Symbol_value<size>* psymval,
620 typename elfcpp::Elf_types<size>::Elf_Addr addend,
621 typename elfcpp::Elf_types<size>::Elf_Addr address)
623 This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
624 psymval, addend, address);
627 // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
629 pc10(unsigned char* view,
630 const Sized_relobj_file<size, big_endian>* object,
631 const Symbol_value<size>* psymval,
632 typename elfcpp::Elf_types<size>::Elf_Addr addend,
633 typename elfcpp::Elf_types<size>::Elf_Addr address)
635 This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
636 psymval, addend, address);
639 // R_SPARC_HI22: (Symbol + Addend) >> 10
641 hi22(unsigned char* view,
642 typename elfcpp::Elf_types<size>::Elf_Addr value,
643 typename elfcpp::Elf_types<size>::Elf_Addr addend)
645 This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
648 // R_SPARC_HI22: (Symbol + Addend) >> 10
650 hi22(unsigned char* view,
651 const Sized_relobj_file<size, big_endian>* object,
652 const Symbol_value<size>* psymval,
653 typename elfcpp::Elf_types<size>::Elf_Addr addend)
655 This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
658 // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
660 pcplt22(unsigned char* view,
661 const Sized_relobj_file<size, big_endian>* object,
662 const Symbol_value<size>* psymval,
663 typename elfcpp::Elf_types<size>::Elf_Addr addend,
664 typename elfcpp::Elf_types<size>::Elf_Addr address)
666 This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
667 psymval, addend, address);
670 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
672 lo10(unsigned char* view,
673 typename elfcpp::Elf_types<size>::Elf_Addr value,
674 typename elfcpp::Elf_types<size>::Elf_Addr addend)
676 This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
679 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
681 lo10(unsigned char* view,
682 const Sized_relobj_file<size, big_endian>* object,
683 const Symbol_value<size>* psymval,
684 typename elfcpp::Elf_types<size>::Elf_Addr addend)
686 This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
689 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
691 lo10(unsigned char* view,
692 const Sized_relobj_file<size, big_endian>* object,
693 const Symbol_value<size>* psymval,
694 typename elfcpp::Elf_types<size>::Elf_Addr addend,
695 typename elfcpp::Elf_types<size>::Elf_Addr address)
697 This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
698 psymval, addend, address);
701 // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
703 olo10(unsigned char* view,
704 const Sized_relobj_file<size, big_endian>* object,
705 const Symbol_value<size>* psymval,
706 typename elfcpp::Elf_types<size>::Elf_Addr addend,
707 typename elfcpp::Elf_types<size>::Elf_Addr addend2)
709 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
710 Valtype* wv = reinterpret_cast<Valtype*>(view);
711 Valtype val = elfcpp::Swap<32, true>::readval(wv);
712 Valtype reloc = psymval->value(object, addend);
719 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
722 // R_SPARC_22: (Symbol + Addend)
724 rela32_22(unsigned char* view,
725 const Sized_relobj_file<size, big_endian>* object,
726 const Symbol_value<size>* psymval,
727 typename elfcpp::Elf_types<size>::Elf_Addr addend)
729 This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
732 // R_SPARC_13: (Symbol + Addend)
734 rela32_13(unsigned char* view,
735 typename elfcpp::Elf_types<size>::Elf_Addr value,
736 typename elfcpp::Elf_types<size>::Elf_Addr addend)
738 This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
741 // R_SPARC_13: (Symbol + Addend)
743 rela32_13(unsigned char* view,
744 const Sized_relobj_file<size, big_endian>* object,
745 const Symbol_value<size>* psymval,
746 typename elfcpp::Elf_types<size>::Elf_Addr addend)
748 This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
751 // R_SPARC_UA16: (Symbol + Addend)
753 ua16(unsigned char* view,
754 const Sized_relobj_file<size, big_endian>* object,
755 const Symbol_value<size>* psymval,
756 typename elfcpp::Elf_types<size>::Elf_Addr addend)
758 This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
761 // R_SPARC_UA32: (Symbol + Addend)
763 ua32(unsigned char* view,
764 const Sized_relobj_file<size, big_endian>* object,
765 const Symbol_value<size>* psymval,
766 typename elfcpp::Elf_types<size>::Elf_Addr addend)
768 This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
771 // R_SPARC_UA64: (Symbol + Addend)
773 ua64(unsigned char* view,
774 const Sized_relobj_file<size, big_endian>* object,
775 const Symbol_value<size>* psymval,
776 typename elfcpp::Elf_types<size>::Elf_Addr addend)
778 This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
779 object, psymval, addend);
782 // R_SPARC_DISP8: (Symbol + Addend - Address)
784 disp8(unsigned char* view,
785 const Sized_relobj_file<size, big_endian>* object,
786 const Symbol_value<size>* psymval,
787 typename elfcpp::Elf_types<size>::Elf_Addr addend,
788 typename elfcpp::Elf_types<size>::Elf_Addr address)
790 This::template pcrela_unaligned<8>(view, object, psymval,
794 // R_SPARC_DISP16: (Symbol + Addend - Address)
796 disp16(unsigned char* view,
797 const Sized_relobj_file<size, big_endian>* object,
798 const Symbol_value<size>* psymval,
799 typename elfcpp::Elf_types<size>::Elf_Addr addend,
800 typename elfcpp::Elf_types<size>::Elf_Addr address)
802 This::template pcrela_unaligned<16>(view, object, psymval,
806 // R_SPARC_DISP32: (Symbol + Addend - Address)
808 disp32(unsigned char* view,
809 const Sized_relobj_file<size, big_endian>* object,
810 const Symbol_value<size>* psymval,
811 typename elfcpp::Elf_types<size>::Elf_Addr addend,
812 typename elfcpp::Elf_types<size>::Elf_Addr address)
814 This::template pcrela_unaligned<32>(view, object, psymval,
818 // R_SPARC_DISP64: (Symbol + Addend - Address)
820 disp64(unsigned char* view,
821 const Sized_relobj_file<size, big_endian>* object,
822 const Symbol_value<size>* psymval,
823 elfcpp::Elf_Xword addend,
824 typename elfcpp::Elf_types<size>::Elf_Addr address)
826 This::template pcrela_unaligned<64>(view, object, psymval,
830 // R_SPARC_H44: (Symbol + Addend) >> 22
832 h44(unsigned char* view,
833 const Sized_relobj_file<size, big_endian>* object,
834 const Symbol_value<size>* psymval,
835 typename elfcpp::Elf_types<size>::Elf_Addr addend)
837 This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
840 // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
842 m44(unsigned char* view,
843 const Sized_relobj_file<size, big_endian>* object,
844 const Symbol_value<size>* psymval,
845 typename elfcpp::Elf_types<size>::Elf_Addr addend)
847 This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
850 // R_SPARC_L44: (Symbol + Addend) & 0xfff
852 l44(unsigned char* view,
853 const Sized_relobj_file<size, big_endian>* object,
854 const Symbol_value<size>* psymval,
855 typename elfcpp::Elf_types<size>::Elf_Addr addend)
857 This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
860 // R_SPARC_HH22: (Symbol + Addend) >> 42
862 hh22(unsigned char* view,
863 const Sized_relobj_file<size, big_endian>* object,
864 const Symbol_value<size>* psymval,
865 typename elfcpp::Elf_types<size>::Elf_Addr addend)
867 This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
870 // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
872 pc_hh22(unsigned char* view,
873 const Sized_relobj_file<size, big_endian>* object,
874 const Symbol_value<size>* psymval,
875 typename elfcpp::Elf_types<size>::Elf_Addr addend,
876 typename elfcpp::Elf_types<size>::Elf_Addr address)
878 This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
879 psymval, addend, address);
882 // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
884 hm10(unsigned char* view,
885 const Sized_relobj_file<size, big_endian>* object,
886 const Symbol_value<size>* psymval,
887 typename elfcpp::Elf_types<size>::Elf_Addr addend)
889 This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
892 // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
894 pc_hm10(unsigned char* view,
895 const Sized_relobj_file<size, big_endian>* object,
896 const Symbol_value<size>* psymval,
897 typename elfcpp::Elf_types<size>::Elf_Addr addend,
898 typename elfcpp::Elf_types<size>::Elf_Addr address)
900 This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
901 psymval, addend, address);
904 // R_SPARC_11: (Symbol + Addend)
906 rela32_11(unsigned char* view,
907 const Sized_relobj_file<size, big_endian>* object,
908 const Symbol_value<size>* psymval,
909 typename elfcpp::Elf_types<size>::Elf_Addr addend)
911 This_insn::template rela<32>(view, 0, 0x000007ff, object, psymval, addend);
914 // R_SPARC_10: (Symbol + Addend)
916 rela32_10(unsigned char* view,
917 const Sized_relobj_file<size, big_endian>* object,
918 const Symbol_value<size>* psymval,
919 typename elfcpp::Elf_types<size>::Elf_Addr addend)
921 This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
924 // R_SPARC_7: (Symbol + Addend)
926 rela32_7(unsigned char* view,
927 const Sized_relobj_file<size, big_endian>* object,
928 const Symbol_value<size>* psymval,
929 typename elfcpp::Elf_types<size>::Elf_Addr addend)
931 This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
934 // R_SPARC_6: (Symbol + Addend)
936 rela32_6(unsigned char* view,
937 const Sized_relobj_file<size, big_endian>* object,
938 const Symbol_value<size>* psymval,
939 typename elfcpp::Elf_types<size>::Elf_Addr addend)
941 This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
944 // R_SPARC_5: (Symbol + Addend)
946 rela32_5(unsigned char* view,
947 const Sized_relobj_file<size, big_endian>* object,
948 const Symbol_value<size>* psymval,
949 typename elfcpp::Elf_types<size>::Elf_Addr addend)
951 This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
954 // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
956 ldo_hix22(unsigned char* view,
957 typename elfcpp::Elf_types<size>::Elf_Addr value,
958 typename elfcpp::Elf_types<size>::Elf_Addr addend)
960 This_insn::hi22(view, value, addend);
963 // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
965 ldo_lox10(unsigned char* view,
966 typename elfcpp::Elf_types<size>::Elf_Addr value,
967 typename elfcpp::Elf_types<size>::Elf_Addr addend)
969 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
970 Valtype* wv = reinterpret_cast<Valtype*>(view);
971 Valtype val = elfcpp::Swap<32, true>::readval(wv);
972 Valtype reloc = (value + addend);
977 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
980 // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
982 hix22(unsigned char* view,
983 typename elfcpp::Elf_types<size>::Elf_Addr value,
984 typename elfcpp::Elf_types<size>::Elf_Addr addend)
986 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
987 Valtype* wv = reinterpret_cast<Valtype*>(view);
988 Valtype val = elfcpp::Swap<32, true>::readval(wv);
989 Valtype reloc = (value + addend);
993 reloc ^= ~(Valtype)0;
998 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1001 // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
1003 hix22(unsigned char* view,
1004 const Sized_relobj_file<size, big_endian>* object,
1005 const Symbol_value<size>* psymval,
1006 typename elfcpp::Elf_types<size>::Elf_Addr addend)
1008 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1009 Valtype* wv = reinterpret_cast<Valtype*>(view);
1010 Valtype val = elfcpp::Swap<32, true>::readval(wv);
1011 Valtype reloc = psymval->value(object, addend);
1015 reloc ^= ~(Valtype)0;
1020 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1024 // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
1026 lox10(unsigned char* view,
1027 typename elfcpp::Elf_types<size>::Elf_Addr value,
1028 typename elfcpp::Elf_types<size>::Elf_Addr addend)
1030 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1031 Valtype* wv = reinterpret_cast<Valtype*>(view);
1032 Valtype val = elfcpp::Swap<32, true>::readval(wv);
1033 Valtype reloc = (value + addend);
1039 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1042 // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
1044 lox10(unsigned char* view,
1045 const Sized_relobj_file<size, big_endian>* object,
1046 const Symbol_value<size>* psymval,
1047 typename elfcpp::Elf_types<size>::Elf_Addr addend)
1049 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
1050 Valtype* wv = reinterpret_cast<Valtype*>(view);
1051 Valtype val = elfcpp::Swap<32, true>::readval(wv);
1052 Valtype reloc = psymval->value(object, addend);
1058 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
1062 // Get the GOT section, creating it if necessary.
1064 template<int size, bool big_endian>
1065 Output_data_got<size, big_endian>*
1066 Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
1069 if (this->got_ == NULL)
1071 gold_assert(symtab != NULL && layout != NULL);
1073 this->got_ = new Output_data_got<size, big_endian>();
1075 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1077 | elfcpp::SHF_WRITE),
1078 this->got_, ORDER_RELRO, true);
1080 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
1081 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1082 Symbol_table::PREDEFINED,
1084 0, 0, elfcpp::STT_OBJECT,
1086 elfcpp::STV_HIDDEN, 0,
1093 // Get the dynamic reloc section, creating it if necessary.
1095 template<int size, bool big_endian>
1096 typename Target_sparc<size, big_endian>::Reloc_section*
1097 Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
1099 if (this->rela_dyn_ == NULL)
1101 gold_assert(layout != NULL);
1102 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
1103 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1104 elfcpp::SHF_ALLOC, this->rela_dyn_,
1105 ORDER_DYNAMIC_RELOCS, false);
1107 return this->rela_dyn_;
1110 // A class to handle the PLT data.
1112 template<int size, bool big_endian>
1113 class Output_data_plt_sparc : public Output_section_data
1116 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1117 size, big_endian> Reloc_section;
1119 Output_data_plt_sparc(Layout*);
1121 // Add an entry to the PLT.
1122 void add_entry(Symbol* gsym);
1124 // Return the .rela.plt section data.
1125 const Reloc_section* rel_plt() const
1130 // Return the number of PLT entries.
1133 { return this->count_; }
1135 // Return the offset of the first non-reserved PLT entry.
1137 first_plt_entry_offset()
1138 { return 4 * base_plt_entry_size; }
1140 // Return the size of a PLT entry.
1142 get_plt_entry_size()
1143 { return base_plt_entry_size; }
1146 void do_adjust_output_section(Output_section* os);
1148 // Write to a map file.
1150 do_print_to_mapfile(Mapfile* mapfile) const
1151 { mapfile->print_output_data(this, _("** PLT")); }
1154 // The size of an entry in the PLT.
1155 static const int base_plt_entry_size = (size == 32 ? 12 : 32);
1157 static const unsigned int plt_entries_per_block = 160;
1158 static const unsigned int plt_insn_chunk_size = 24;
1159 static const unsigned int plt_pointer_chunk_size = 8;
1160 static const unsigned int plt_block_size =
1161 (plt_entries_per_block
1162 * (plt_insn_chunk_size + plt_pointer_chunk_size));
1164 // Set the final size.
1166 set_final_data_size()
1168 unsigned int full_count = this->count_ + 4;
1169 unsigned int extra = (size == 32 ? 4 : 0);
1171 if (size == 32 || full_count < 32768)
1172 this->set_data_size((full_count * base_plt_entry_size) + extra);
1175 unsigned int ext_cnt = full_count - 32768;
1177 this->set_data_size((32768 * base_plt_entry_size)
1179 * (plt_insn_chunk_size
1180 + plt_pointer_chunk_size)));
1184 // Write out the PLT data.
1186 do_write(Output_file*);
1188 // The reloc section.
1189 Reloc_section* rel_;
1190 // The number of PLT entries.
1191 unsigned int count_;
1194 // Define the constants as required by C++ standard.
1196 template<int size, bool big_endian>
1197 const int Output_data_plt_sparc<size, big_endian>::base_plt_entry_size;
1199 template<int size, bool big_endian>
1201 Output_data_plt_sparc<size, big_endian>::plt_entries_per_block;
1203 template<int size, bool big_endian>
1204 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_insn_chunk_size;
1206 template<int size, bool big_endian>
1208 Output_data_plt_sparc<size, big_endian>::plt_pointer_chunk_size;
1210 template<int size, bool big_endian>
1211 const unsigned int Output_data_plt_sparc<size, big_endian>::plt_block_size;
1213 // Create the PLT section. The ordinary .got section is an argument,
1214 // since we need to refer to the start.
1216 template<int size, bool big_endian>
1217 Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
1218 : Output_section_data(size == 32 ? 4 : 8), count_(0)
1220 this->rel_ = new Reloc_section(false);
1221 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1222 elfcpp::SHF_ALLOC, this->rel_,
1223 ORDER_DYNAMIC_PLT_RELOCS, false);
1226 template<int size, bool big_endian>
1228 Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
1233 // Add an entry to the PLT.
1235 template<int size, bool big_endian>
1237 Output_data_plt_sparc<size, big_endian>::add_entry(Symbol* gsym)
1239 gold_assert(!gsym->has_plt_offset());
1241 unsigned int index = this->count_ + 4;
1242 section_offset_type plt_offset;
1244 if (size == 32 || index < 32768)
1245 plt_offset = index * base_plt_entry_size;
1248 unsigned int ext_index = index - 32768;
1250 plt_offset = (32768 * base_plt_entry_size)
1251 + ((ext_index / plt_entries_per_block)
1253 + ((ext_index % plt_entries_per_block)
1254 * plt_insn_chunk_size);
1257 gsym->set_plt_offset(plt_offset);
1261 // Every PLT entry needs a reloc.
1262 gsym->set_needs_dynsym_entry();
1263 this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
1266 // Note that we don't need to save the symbol. The contents of the
1267 // PLT are independent of which symbols are used. The symbols only
1268 // appear in the relocations.
1271 static const unsigned int sparc_nop = 0x01000000;
1272 static const unsigned int sparc_sethi_g1 = 0x03000000;
1273 static const unsigned int sparc_branch_always = 0x30800000;
1274 static const unsigned int sparc_branch_always_pt = 0x30680000;
1275 static const unsigned int sparc_mov = 0x80100000;
1276 static const unsigned int sparc_mov_g0_o0 = 0x90100000;
1277 static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
1278 static const unsigned int sparc_call_plus_8 = 0x40000002;
1279 static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
1280 static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
1281 static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
1283 // Write out the PLT.
1285 template<int size, bool big_endian>
1287 Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
1289 const off_t offset = this->offset();
1290 const section_size_type oview_size =
1291 convert_to_section_size_type(this->data_size());
1292 unsigned char* const oview = of->get_output_view(offset, oview_size);
1293 unsigned char* pov = oview;
1295 memset(pov, 0, base_plt_entry_size * 4);
1296 pov += base_plt_entry_size * 4;
1298 unsigned int plt_offset = base_plt_entry_size * 4;
1299 const unsigned int count = this->count_;
1305 limit = (count > 32768 ? 32768 : count);
1307 for (unsigned int i = 0; i < limit; ++i)
1309 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1310 sparc_sethi_g1 + plt_offset);
1311 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1312 sparc_branch_always_pt +
1313 (((base_plt_entry_size -
1314 (plt_offset + 4)) >> 2) &
1316 elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1317 elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
1318 elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
1319 elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
1320 elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
1321 elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
1323 pov += base_plt_entry_size;
1324 plt_offset += base_plt_entry_size;
1329 unsigned int ext_cnt = count - 32768;
1330 unsigned int blks = ext_cnt / plt_entries_per_block;
1332 for (unsigned int i = 0; i < blks; ++i)
1334 unsigned int data_off = (plt_entries_per_block
1335 * plt_insn_chunk_size) - 4;
1337 for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1339 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1341 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1343 elfcpp::Swap<32, true>::writeval(pov + 0x08,
1345 elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1346 sparc_ldx_o7_imm_g1 +
1347 (data_off & 0x1fff));
1348 elfcpp::Swap<32, true>::writeval(pov + 0x10,
1349 sparc_jmpl_o7_g1_g1);
1350 elfcpp::Swap<32, true>::writeval(pov + 0x14,
1353 elfcpp::Swap<64, big_endian>::writeval(
1354 pov + 0x4 + data_off,
1355 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1357 pov += plt_insn_chunk_size;
1362 unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
1363 for (unsigned int i = 0; i < sub_blk_cnt; ++i)
1365 unsigned int data_off = (sub_blk_cnt
1366 * plt_insn_chunk_size) - 4;
1368 for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1370 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1372 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1374 elfcpp::Swap<32, true>::writeval(pov + 0x08,
1376 elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1377 sparc_ldx_o7_imm_g1 +
1378 (data_off & 0x1fff));
1379 elfcpp::Swap<32, true>::writeval(pov + 0x10,
1380 sparc_jmpl_o7_g1_g1);
1381 elfcpp::Swap<32, true>::writeval(pov + 0x14,
1384 elfcpp::Swap<64, big_endian>::writeval(
1385 pov + 0x4 + data_off,
1386 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1388 pov += plt_insn_chunk_size;
1396 for (unsigned int i = 0; i < count; ++i)
1398 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1399 sparc_sethi_g1 + plt_offset);
1400 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1401 sparc_branch_always +
1402 (((- (plt_offset + 4)) >> 2) &
1404 elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1406 pov += base_plt_entry_size;
1407 plt_offset += base_plt_entry_size;
1410 elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
1414 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1416 of->write_output_view(offset, oview_size, oview);
1419 // Create a PLT entry for a global symbol.
1421 template<int size, bool big_endian>
1423 Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1427 if (gsym->has_plt_offset())
1430 if (this->plt_ == NULL)
1432 // Create the GOT sections first.
1433 this->got_section(symtab, layout);
1435 // Ensure that .rela.dyn always appears before .rela.plt This is
1436 // necessary due to how, on Sparc and some other targets, .rela.dyn
1437 // needs to include .rela.plt in it's range.
1438 this->rela_dyn_section(layout);
1440 this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
1441 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1443 | elfcpp::SHF_EXECINSTR
1444 | elfcpp::SHF_WRITE),
1445 this->plt_, ORDER_PLT, false);
1447 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1448 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1449 Symbol_table::PREDEFINED,
1451 0, 0, elfcpp::STT_OBJECT,
1453 elfcpp::STV_HIDDEN, 0,
1457 this->plt_->add_entry(gsym);
1460 // Return the number of entries in the PLT.
1462 template<int size, bool big_endian>
1464 Target_sparc<size, big_endian>::plt_entry_count() const
1466 if (this->plt_ == NULL)
1468 return this->plt_->entry_count();
1471 // Return the offset of the first non-reserved PLT entry.
1473 template<int size, bool big_endian>
1475 Target_sparc<size, big_endian>::first_plt_entry_offset() const
1477 return Output_data_plt_sparc<size, big_endian>::first_plt_entry_offset();
1480 // Return the size of each PLT entry.
1482 template<int size, bool big_endian>
1484 Target_sparc<size, big_endian>::plt_entry_size() const
1486 return Output_data_plt_sparc<size, big_endian>::get_plt_entry_size();
1489 // Create a GOT entry for the TLS module index.
1491 template<int size, bool big_endian>
1493 Target_sparc<size, big_endian>::got_mod_index_entry(
1494 Symbol_table* symtab,
1496 Sized_relobj_file<size, big_endian>* object)
1498 if (this->got_mod_index_offset_ == -1U)
1500 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1501 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1502 Output_data_got<size, big_endian>* got;
1503 unsigned int got_offset;
1505 got = this->got_section(symtab, layout);
1506 got_offset = got->add_constant(0);
1507 rela_dyn->add_local(object, 0,
1509 elfcpp::R_SPARC_TLS_DTPMOD64 :
1510 elfcpp::R_SPARC_TLS_DTPMOD32), got,
1512 got->add_constant(0);
1513 this->got_mod_index_offset_ = got_offset;
1515 return this->got_mod_index_offset_;
1518 // Optimize the TLS relocation type based on what we know about the
1519 // symbol. IS_FINAL is true if the final address of this symbol is
1520 // known at link time.
1522 static tls::Tls_optimization
1523 optimize_tls_reloc(bool is_final, int r_type)
1525 // If we are generating a shared library, then we can't do anything
1527 if (parameters->options().shared())
1528 return tls::TLSOPT_NONE;
1532 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1533 case elfcpp::R_SPARC_TLS_GD_LO10:
1534 case elfcpp::R_SPARC_TLS_GD_ADD:
1535 case elfcpp::R_SPARC_TLS_GD_CALL:
1536 // These are General-Dynamic which permits fully general TLS
1537 // access. Since we know that we are generating an executable,
1538 // we can convert this to Initial-Exec. If we also know that
1539 // this is a local symbol, we can further switch to Local-Exec.
1541 return tls::TLSOPT_TO_LE;
1542 return tls::TLSOPT_TO_IE;
1544 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
1545 case elfcpp::R_SPARC_TLS_LDM_LO10:
1546 case elfcpp::R_SPARC_TLS_LDM_ADD:
1547 case elfcpp::R_SPARC_TLS_LDM_CALL:
1548 // This is Local-Dynamic, which refers to a local symbol in the
1549 // dynamic TLS block. Since we know that we generating an
1550 // executable, we can switch to Local-Exec.
1551 return tls::TLSOPT_TO_LE;
1553 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1554 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1555 case elfcpp::R_SPARC_TLS_LDO_ADD:
1556 // Another type of Local-Dynamic relocation.
1557 return tls::TLSOPT_TO_LE;
1559 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1560 case elfcpp::R_SPARC_TLS_IE_LO10:
1561 case elfcpp::R_SPARC_TLS_IE_LD:
1562 case elfcpp::R_SPARC_TLS_IE_LDX:
1563 case elfcpp::R_SPARC_TLS_IE_ADD:
1564 // These are Initial-Exec relocs which get the thread offset
1565 // from the GOT. If we know that we are linking against the
1566 // local symbol, we can switch to Local-Exec, which links the
1567 // thread offset into the instruction.
1569 return tls::TLSOPT_TO_LE;
1570 return tls::TLSOPT_NONE;
1572 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1573 case elfcpp::R_SPARC_TLS_LE_LOX10:
1574 // When we already have Local-Exec, there is nothing further we
1576 return tls::TLSOPT_NONE;
1583 // Get the Reference_flags for a particular relocation.
1585 template<int size, bool big_endian>
1587 Target_sparc<size, big_endian>::Scan::get_reference_flags(unsigned int r_type)
1592 case elfcpp::R_SPARC_NONE:
1593 case elfcpp::R_SPARC_REGISTER:
1594 case elfcpp::R_SPARC_GNU_VTINHERIT:
1595 case elfcpp::R_SPARC_GNU_VTENTRY:
1596 // No symbol reference.
1599 case elfcpp::R_SPARC_UA64:
1600 case elfcpp::R_SPARC_64:
1601 case elfcpp::R_SPARC_HIX22:
1602 case elfcpp::R_SPARC_LOX10:
1603 case elfcpp::R_SPARC_H44:
1604 case elfcpp::R_SPARC_M44:
1605 case elfcpp::R_SPARC_L44:
1606 case elfcpp::R_SPARC_HH22:
1607 case elfcpp::R_SPARC_HM10:
1608 case elfcpp::R_SPARC_LM22:
1609 case elfcpp::R_SPARC_HI22:
1610 case elfcpp::R_SPARC_LO10:
1611 case elfcpp::R_SPARC_OLO10:
1612 case elfcpp::R_SPARC_UA32:
1613 case elfcpp::R_SPARC_32:
1614 case elfcpp::R_SPARC_UA16:
1615 case elfcpp::R_SPARC_16:
1616 case elfcpp::R_SPARC_11:
1617 case elfcpp::R_SPARC_10:
1618 case elfcpp::R_SPARC_8:
1619 case elfcpp::R_SPARC_7:
1620 case elfcpp::R_SPARC_6:
1621 case elfcpp::R_SPARC_5:
1622 return Symbol::ABSOLUTE_REF;
1624 case elfcpp::R_SPARC_DISP8:
1625 case elfcpp::R_SPARC_DISP16:
1626 case elfcpp::R_SPARC_DISP32:
1627 case elfcpp::R_SPARC_DISP64:
1628 case elfcpp::R_SPARC_PC_HH22:
1629 case elfcpp::R_SPARC_PC_HM10:
1630 case elfcpp::R_SPARC_PC_LM22:
1631 case elfcpp::R_SPARC_PC10:
1632 case elfcpp::R_SPARC_PC22:
1633 case elfcpp::R_SPARC_WDISP30:
1634 case elfcpp::R_SPARC_WDISP22:
1635 case elfcpp::R_SPARC_WDISP19:
1636 case elfcpp::R_SPARC_WDISP16:
1637 return Symbol::RELATIVE_REF;
1639 case elfcpp::R_SPARC_PLT64:
1640 case elfcpp::R_SPARC_PLT32:
1641 case elfcpp::R_SPARC_HIPLT22:
1642 case elfcpp::R_SPARC_LOPLT10:
1643 case elfcpp::R_SPARC_PCPLT10:
1644 return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
1646 case elfcpp::R_SPARC_PCPLT32:
1647 case elfcpp::R_SPARC_PCPLT22:
1648 case elfcpp::R_SPARC_WPLT30:
1649 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1651 case elfcpp::R_SPARC_GOTDATA_OP:
1652 case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
1653 case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
1654 case elfcpp::R_SPARC_GOT10:
1655 case elfcpp::R_SPARC_GOT13:
1656 case elfcpp::R_SPARC_GOT22:
1658 return Symbol::ABSOLUTE_REF;
1660 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1661 case elfcpp::R_SPARC_TLS_GD_LO10:
1662 case elfcpp::R_SPARC_TLS_GD_ADD:
1663 case elfcpp::R_SPARC_TLS_GD_CALL:
1664 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
1665 case elfcpp::R_SPARC_TLS_LDM_LO10:
1666 case elfcpp::R_SPARC_TLS_LDM_ADD:
1667 case elfcpp::R_SPARC_TLS_LDM_CALL:
1668 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1669 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1670 case elfcpp::R_SPARC_TLS_LDO_ADD:
1671 case elfcpp::R_SPARC_TLS_LE_HIX22:
1672 case elfcpp::R_SPARC_TLS_LE_LOX10:
1673 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1674 case elfcpp::R_SPARC_TLS_IE_LO10:
1675 case elfcpp::R_SPARC_TLS_IE_LD:
1676 case elfcpp::R_SPARC_TLS_IE_LDX:
1677 case elfcpp::R_SPARC_TLS_IE_ADD:
1678 return Symbol::TLS_REF;
1680 case elfcpp::R_SPARC_COPY:
1681 case elfcpp::R_SPARC_GLOB_DAT:
1682 case elfcpp::R_SPARC_JMP_SLOT:
1683 case elfcpp::R_SPARC_RELATIVE:
1684 case elfcpp::R_SPARC_TLS_DTPMOD64:
1685 case elfcpp::R_SPARC_TLS_DTPMOD32:
1686 case elfcpp::R_SPARC_TLS_DTPOFF64:
1687 case elfcpp::R_SPARC_TLS_DTPOFF32:
1688 case elfcpp::R_SPARC_TLS_TPOFF64:
1689 case elfcpp::R_SPARC_TLS_TPOFF32:
1691 // Not expected. We will give an error later.
1696 // Generate a PLT entry slot for a call to __tls_get_addr
1697 template<int size, bool big_endian>
1699 Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
1701 Target_sparc<size, big_endian>* target)
1703 Symbol* gsym = target->tls_get_addr_sym(symtab);
1705 target->make_plt_entry(symtab, layout, gsym);
1708 // Report an unsupported relocation against a local symbol.
1710 template<int size, bool big_endian>
1712 Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
1713 Sized_relobj_file<size, big_endian>* object,
1714 unsigned int r_type)
1716 gold_error(_("%s: unsupported reloc %u against local symbol"),
1717 object->name().c_str(), r_type);
1720 // We are about to emit a dynamic relocation of type R_TYPE. If the
1721 // dynamic linker does not support it, issue an error.
1723 template<int size, bool big_endian>
1725 Target_sparc<size, big_endian>::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1727 gold_assert(r_type != elfcpp::R_SPARC_NONE);
1733 // These are the relocation types supported by glibc for sparc 64-bit.
1734 case elfcpp::R_SPARC_RELATIVE:
1735 case elfcpp::R_SPARC_COPY:
1736 case elfcpp::R_SPARC_64:
1737 case elfcpp::R_SPARC_GLOB_DAT:
1738 case elfcpp::R_SPARC_JMP_SLOT:
1739 case elfcpp::R_SPARC_TLS_DTPMOD64:
1740 case elfcpp::R_SPARC_TLS_DTPOFF64:
1741 case elfcpp::R_SPARC_TLS_TPOFF64:
1742 case elfcpp::R_SPARC_TLS_LE_HIX22:
1743 case elfcpp::R_SPARC_TLS_LE_LOX10:
1744 case elfcpp::R_SPARC_8:
1745 case elfcpp::R_SPARC_16:
1746 case elfcpp::R_SPARC_DISP8:
1747 case elfcpp::R_SPARC_DISP16:
1748 case elfcpp::R_SPARC_DISP32:
1749 case elfcpp::R_SPARC_WDISP30:
1750 case elfcpp::R_SPARC_LO10:
1751 case elfcpp::R_SPARC_HI22:
1752 case elfcpp::R_SPARC_OLO10:
1753 case elfcpp::R_SPARC_H44:
1754 case elfcpp::R_SPARC_M44:
1755 case elfcpp::R_SPARC_L44:
1756 case elfcpp::R_SPARC_HH22:
1757 case elfcpp::R_SPARC_HM10:
1758 case elfcpp::R_SPARC_LM22:
1759 case elfcpp::R_SPARC_UA16:
1760 case elfcpp::R_SPARC_UA32:
1761 case elfcpp::R_SPARC_UA64:
1772 // These are the relocation types supported by glibc for sparc 32-bit.
1773 case elfcpp::R_SPARC_RELATIVE:
1774 case elfcpp::R_SPARC_COPY:
1775 case elfcpp::R_SPARC_GLOB_DAT:
1776 case elfcpp::R_SPARC_32:
1777 case elfcpp::R_SPARC_JMP_SLOT:
1778 case elfcpp::R_SPARC_TLS_DTPMOD32:
1779 case elfcpp::R_SPARC_TLS_DTPOFF32:
1780 case elfcpp::R_SPARC_TLS_TPOFF32:
1781 case elfcpp::R_SPARC_TLS_LE_HIX22:
1782 case elfcpp::R_SPARC_TLS_LE_LOX10:
1783 case elfcpp::R_SPARC_8:
1784 case elfcpp::R_SPARC_16:
1785 case elfcpp::R_SPARC_DISP8:
1786 case elfcpp::R_SPARC_DISP16:
1787 case elfcpp::R_SPARC_DISP32:
1788 case elfcpp::R_SPARC_LO10:
1789 case elfcpp::R_SPARC_WDISP30:
1790 case elfcpp::R_SPARC_HI22:
1791 case elfcpp::R_SPARC_UA16:
1792 case elfcpp::R_SPARC_UA32:
1800 // This prevents us from issuing more than one error per reloc
1801 // section. But we can still wind up issuing more than one
1802 // error per object file.
1803 if (this->issued_non_pic_error_)
1805 gold_assert(parameters->options().output_is_position_independent());
1806 object->error(_("requires unsupported dynamic reloc; "
1807 "recompile with -fPIC"));
1808 this->issued_non_pic_error_ = true;
1812 // Scan a relocation for a local symbol.
1814 template<int size, bool big_endian>
1816 Target_sparc<size, big_endian>::Scan::local(
1817 Symbol_table* symtab,
1819 Target_sparc<size, big_endian>* target,
1820 Sized_relobj_file<size, big_endian>* object,
1821 unsigned int data_shndx,
1822 Output_section* output_section,
1823 const elfcpp::Rela<size, big_endian>& reloc,
1824 unsigned int r_type,
1825 const elfcpp::Sym<size, big_endian>& lsym)
1827 unsigned int orig_r_type = r_type;
1832 case elfcpp::R_SPARC_NONE:
1833 case elfcpp::R_SPARC_REGISTER:
1834 case elfcpp::R_SPARC_GNU_VTINHERIT:
1835 case elfcpp::R_SPARC_GNU_VTENTRY:
1838 case elfcpp::R_SPARC_64:
1839 case elfcpp::R_SPARC_32:
1840 // If building a shared library (or a position-independent
1841 // executable), we need to create a dynamic relocation for
1842 // this location. The relocation applied at link time will
1843 // apply the link-time value, so we flag the location with
1844 // an R_SPARC_RELATIVE relocation so the dynamic loader can
1845 // relocate it easily.
1846 if (parameters->options().output_is_position_independent())
1848 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1849 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1850 rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
1851 output_section, data_shndx,
1852 reloc.get_r_offset(),
1853 reloc.get_r_addend());
1857 case elfcpp::R_SPARC_HIX22:
1858 case elfcpp::R_SPARC_LOX10:
1859 case elfcpp::R_SPARC_H44:
1860 case elfcpp::R_SPARC_M44:
1861 case elfcpp::R_SPARC_L44:
1862 case elfcpp::R_SPARC_HH22:
1863 case elfcpp::R_SPARC_HM10:
1864 case elfcpp::R_SPARC_LM22:
1865 case elfcpp::R_SPARC_UA64:
1866 case elfcpp::R_SPARC_UA32:
1867 case elfcpp::R_SPARC_UA16:
1868 case elfcpp::R_SPARC_HI22:
1869 case elfcpp::R_SPARC_LO10:
1870 case elfcpp::R_SPARC_OLO10:
1871 case elfcpp::R_SPARC_16:
1872 case elfcpp::R_SPARC_11:
1873 case elfcpp::R_SPARC_10:
1874 case elfcpp::R_SPARC_8:
1875 case elfcpp::R_SPARC_7:
1876 case elfcpp::R_SPARC_6:
1877 case elfcpp::R_SPARC_5:
1878 // If building a shared library (or a position-independent
1879 // executable), we need to create a dynamic relocation for
1881 if (parameters->options().output_is_position_independent())
1883 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1884 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1886 check_non_pic(object, r_type);
1887 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1889 rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
1890 data_shndx, reloc.get_r_offset(),
1891 reloc.get_r_addend());
1895 gold_assert(lsym.get_st_value() == 0);
1896 rela_dyn->add_symbolless_local_addend(object, r_sym, orig_r_type,
1897 output_section, data_shndx,
1898 reloc.get_r_offset(),
1899 reloc.get_r_addend());
1904 case elfcpp::R_SPARC_WDISP30:
1905 case elfcpp::R_SPARC_WPLT30:
1906 case elfcpp::R_SPARC_WDISP22:
1907 case elfcpp::R_SPARC_WDISP19:
1908 case elfcpp::R_SPARC_WDISP16:
1909 case elfcpp::R_SPARC_DISP8:
1910 case elfcpp::R_SPARC_DISP16:
1911 case elfcpp::R_SPARC_DISP32:
1912 case elfcpp::R_SPARC_DISP64:
1913 case elfcpp::R_SPARC_PC10:
1914 case elfcpp::R_SPARC_PC22:
1917 case elfcpp::R_SPARC_GOTDATA_OP:
1918 case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
1919 case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
1920 case elfcpp::R_SPARC_GOT10:
1921 case elfcpp::R_SPARC_GOT13:
1922 case elfcpp::R_SPARC_GOT22:
1924 // The symbol requires a GOT entry.
1925 Output_data_got<size, big_endian>* got;
1928 got = target->got_section(symtab, layout);
1929 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1931 // If we are generating a shared object, we need to add a
1932 // dynamic relocation for this symbol's GOT entry.
1933 if (parameters->options().output_is_position_independent())
1935 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1937 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1940 off = got->add_constant(0);
1941 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1942 rela_dyn->add_local_relative(object, r_sym,
1943 elfcpp::R_SPARC_RELATIVE,
1948 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1952 // These are initial TLS relocs, which are expected when
1954 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1955 case elfcpp::R_SPARC_TLS_GD_LO10:
1956 case elfcpp::R_SPARC_TLS_GD_ADD:
1957 case elfcpp::R_SPARC_TLS_GD_CALL:
1958 case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
1959 case elfcpp::R_SPARC_TLS_LDM_LO10:
1960 case elfcpp::R_SPARC_TLS_LDM_ADD:
1961 case elfcpp::R_SPARC_TLS_LDM_CALL:
1962 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1963 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1964 case elfcpp::R_SPARC_TLS_LDO_ADD:
1965 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1966 case elfcpp::R_SPARC_TLS_IE_LO10:
1967 case elfcpp::R_SPARC_TLS_IE_LD:
1968 case elfcpp::R_SPARC_TLS_IE_LDX:
1969 case elfcpp::R_SPARC_TLS_IE_ADD:
1970 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1971 case elfcpp::R_SPARC_TLS_LE_LOX10:
1973 bool output_is_shared = parameters->options().shared();
1974 const tls::Tls_optimization optimized_type
1975 = optimize_tls_reloc(!output_is_shared, r_type);
1978 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1979 case elfcpp::R_SPARC_TLS_GD_LO10:
1980 case elfcpp::R_SPARC_TLS_GD_ADD:
1981 case elfcpp::R_SPARC_TLS_GD_CALL:
1982 if (optimized_type == tls::TLSOPT_NONE)
1984 // Create a pair of GOT entries for the module index and
1985 // dtv-relative offset.
1986 Output_data_got<size, big_endian>* got
1987 = target->got_section(symtab, layout);
1988 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1989 unsigned int shndx = lsym.get_st_shndx();
1991 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1993 object->error(_("local symbol %u has bad shndx %u"),
1996 got->add_local_pair_with_rela(object, r_sym,
1997 lsym.get_st_shndx(),
1999 target->rela_dyn_section(layout),
2001 ? elfcpp::R_SPARC_TLS_DTPMOD64
2002 : elfcpp::R_SPARC_TLS_DTPMOD32),
2004 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2005 generate_tls_call(symtab, layout, target);
2007 else if (optimized_type != tls::TLSOPT_TO_LE)
2008 unsupported_reloc_local(object, r_type);
2011 case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
2012 case elfcpp::R_SPARC_TLS_LDM_LO10:
2013 case elfcpp::R_SPARC_TLS_LDM_ADD:
2014 case elfcpp::R_SPARC_TLS_LDM_CALL:
2015 if (optimized_type == tls::TLSOPT_NONE)
2017 // Create a GOT entry for the module index.
2018 target->got_mod_index_entry(symtab, layout, object);
2020 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2021 generate_tls_call(symtab, layout, target);
2023 else if (optimized_type != tls::TLSOPT_TO_LE)
2024 unsupported_reloc_local(object, r_type);
2027 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2028 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2029 case elfcpp::R_SPARC_TLS_LDO_ADD:
2032 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2033 case elfcpp::R_SPARC_TLS_IE_LO10:
2034 case elfcpp::R_SPARC_TLS_IE_LD:
2035 case elfcpp::R_SPARC_TLS_IE_LDX:
2036 case elfcpp::R_SPARC_TLS_IE_ADD:
2037 layout->set_has_static_tls();
2038 if (optimized_type == tls::TLSOPT_NONE)
2040 // Create a GOT entry for the tp-relative offset.
2041 Output_data_got<size, big_endian>* got
2042 = target->got_section(symtab, layout);
2043 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2045 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
2047 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2048 unsigned int off = got->add_constant(0);
2050 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET, off);
2052 rela_dyn->add_symbolless_local_addend(object, r_sym,
2054 elfcpp::R_SPARC_TLS_TPOFF64 :
2055 elfcpp::R_SPARC_TLS_TPOFF32),
2059 else if (optimized_type != tls::TLSOPT_TO_LE)
2060 unsupported_reloc_local(object, r_type);
2063 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
2064 case elfcpp::R_SPARC_TLS_LE_LOX10:
2065 layout->set_has_static_tls();
2066 if (output_is_shared)
2068 // We need to create a dynamic relocation.
2069 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
2070 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
2071 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2072 rela_dyn->add_symbolless_local_addend(object, r_sym, r_type,
2073 output_section, data_shndx,
2074 reloc.get_r_offset(), 0);
2081 // These are relocations which should only be seen by the
2082 // dynamic linker, and should never be seen here.
2083 case elfcpp::R_SPARC_COPY:
2084 case elfcpp::R_SPARC_GLOB_DAT:
2085 case elfcpp::R_SPARC_JMP_SLOT:
2086 case elfcpp::R_SPARC_RELATIVE:
2087 case elfcpp::R_SPARC_TLS_DTPMOD64:
2088 case elfcpp::R_SPARC_TLS_DTPMOD32:
2089 case elfcpp::R_SPARC_TLS_DTPOFF64:
2090 case elfcpp::R_SPARC_TLS_DTPOFF32:
2091 case elfcpp::R_SPARC_TLS_TPOFF64:
2092 case elfcpp::R_SPARC_TLS_TPOFF32:
2093 gold_error(_("%s: unexpected reloc %u in object file"),
2094 object->name().c_str(), r_type);
2098 unsupported_reloc_local(object, r_type);
2103 // Report an unsupported relocation against a global symbol.
2105 template<int size, bool big_endian>
2107 Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
2108 Sized_relobj_file<size, big_endian>* object,
2109 unsigned int r_type,
2112 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2113 object->name().c_str(), r_type, gsym->demangled_name().c_str());
2116 // Scan a relocation for a global symbol.
2118 template<int size, bool big_endian>
2120 Target_sparc<size, big_endian>::Scan::global(
2121 Symbol_table* symtab,
2123 Target_sparc<size, big_endian>* target,
2124 Sized_relobj_file<size, big_endian>* object,
2125 unsigned int data_shndx,
2126 Output_section* output_section,
2127 const elfcpp::Rela<size, big_endian>& reloc,
2128 unsigned int r_type,
2131 unsigned int orig_r_type = r_type;
2133 // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
2134 // section. We check here to avoid creating a dynamic reloc against
2135 // _GLOBAL_OFFSET_TABLE_.
2136 if (!target->has_got_section()
2137 && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
2138 target->got_section(symtab, layout);
2143 case elfcpp::R_SPARC_NONE:
2144 case elfcpp::R_SPARC_REGISTER:
2145 case elfcpp::R_SPARC_GNU_VTINHERIT:
2146 case elfcpp::R_SPARC_GNU_VTENTRY:
2149 case elfcpp::R_SPARC_PLT64:
2150 case elfcpp::R_SPARC_PLT32:
2151 case elfcpp::R_SPARC_HIPLT22:
2152 case elfcpp::R_SPARC_LOPLT10:
2153 case elfcpp::R_SPARC_PCPLT32:
2154 case elfcpp::R_SPARC_PCPLT22:
2155 case elfcpp::R_SPARC_PCPLT10:
2156 case elfcpp::R_SPARC_WPLT30:
2157 // If the symbol is fully resolved, this is just a PC32 reloc.
2158 // Otherwise we need a PLT entry.
2159 if (gsym->final_value_is_known())
2161 // If building a shared library, we can also skip the PLT entry
2162 // if the symbol is defined in the output file and is protected
2164 if (gsym->is_defined()
2165 && !gsym->is_from_dynobj()
2166 && !gsym->is_preemptible())
2168 target->make_plt_entry(symtab, layout, gsym);
2171 case elfcpp::R_SPARC_DISP8:
2172 case elfcpp::R_SPARC_DISP16:
2173 case elfcpp::R_SPARC_DISP32:
2174 case elfcpp::R_SPARC_DISP64:
2175 case elfcpp::R_SPARC_PC_HH22:
2176 case elfcpp::R_SPARC_PC_HM10:
2177 case elfcpp::R_SPARC_PC_LM22:
2178 case elfcpp::R_SPARC_PC10:
2179 case elfcpp::R_SPARC_PC22:
2180 case elfcpp::R_SPARC_WDISP30:
2181 case elfcpp::R_SPARC_WDISP22:
2182 case elfcpp::R_SPARC_WDISP19:
2183 case elfcpp::R_SPARC_WDISP16:
2185 if (gsym->needs_plt_entry())
2186 target->make_plt_entry(symtab, layout, gsym);
2187 // Make a dynamic relocation if necessary.
2188 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2190 if (gsym->may_need_copy_reloc())
2192 target->copy_reloc(symtab, layout, object,
2193 data_shndx, output_section, gsym,
2198 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2199 check_non_pic(object, r_type);
2200 rela_dyn->add_global(gsym, orig_r_type, output_section, object,
2201 data_shndx, reloc.get_r_offset(),
2202 reloc.get_r_addend());
2208 case elfcpp::R_SPARC_UA64:
2209 case elfcpp::R_SPARC_64:
2210 case elfcpp::R_SPARC_HIX22:
2211 case elfcpp::R_SPARC_LOX10:
2212 case elfcpp::R_SPARC_H44:
2213 case elfcpp::R_SPARC_M44:
2214 case elfcpp::R_SPARC_L44:
2215 case elfcpp::R_SPARC_HH22:
2216 case elfcpp::R_SPARC_HM10:
2217 case elfcpp::R_SPARC_LM22:
2218 case elfcpp::R_SPARC_HI22:
2219 case elfcpp::R_SPARC_LO10:
2220 case elfcpp::R_SPARC_OLO10:
2221 case elfcpp::R_SPARC_UA32:
2222 case elfcpp::R_SPARC_32:
2223 case elfcpp::R_SPARC_UA16:
2224 case elfcpp::R_SPARC_16:
2225 case elfcpp::R_SPARC_11:
2226 case elfcpp::R_SPARC_10:
2227 case elfcpp::R_SPARC_8:
2228 case elfcpp::R_SPARC_7:
2229 case elfcpp::R_SPARC_6:
2230 case elfcpp::R_SPARC_5:
2232 // Make a PLT entry if necessary.
2233 if (gsym->needs_plt_entry())
2235 target->make_plt_entry(symtab, layout, gsym);
2236 // Since this is not a PC-relative relocation, we may be
2237 // taking the address of a function. In that case we need to
2238 // set the entry in the dynamic symbol table to the address of
2240 if (gsym->is_from_dynobj() && !parameters->options().shared())
2241 gsym->set_needs_dynsym_value();
2243 // Make a dynamic relocation if necessary.
2244 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2246 unsigned int r_off = reloc.get_r_offset();
2248 // The assembler can sometimes emit unaligned relocations
2249 // for dwarf2 cfi directives.
2252 case elfcpp::R_SPARC_16:
2254 orig_r_type = r_type = elfcpp::R_SPARC_UA16;
2256 case elfcpp::R_SPARC_32:
2258 orig_r_type = r_type = elfcpp::R_SPARC_UA32;
2260 case elfcpp::R_SPARC_64:
2262 orig_r_type = r_type = elfcpp::R_SPARC_UA64;
2264 case elfcpp::R_SPARC_UA16:
2266 orig_r_type = r_type = elfcpp::R_SPARC_16;
2268 case elfcpp::R_SPARC_UA32:
2270 orig_r_type = r_type = elfcpp::R_SPARC_32;
2272 case elfcpp::R_SPARC_UA64:
2274 orig_r_type = r_type = elfcpp::R_SPARC_64;
2278 if (gsym->may_need_copy_reloc())
2280 target->copy_reloc(symtab, layout, object,
2281 data_shndx, output_section, gsym, reloc);
2283 else if ((r_type == elfcpp::R_SPARC_32
2284 || r_type == elfcpp::R_SPARC_64)
2285 && gsym->can_use_relative_reloc(false))
2287 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2288 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2289 output_section, object,
2290 data_shndx, reloc.get_r_offset(),
2291 reloc.get_r_addend());
2295 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2297 check_non_pic(object, r_type);
2298 if (gsym->is_from_dynobj()
2299 || gsym->is_undefined()
2300 || gsym->is_preemptible())
2301 rela_dyn->add_global(gsym, orig_r_type, output_section,
2303 reloc.get_r_offset(),
2304 reloc.get_r_addend());
2306 rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
2309 reloc.get_r_offset(),
2310 reloc.get_r_addend());
2316 case elfcpp::R_SPARC_GOTDATA_OP:
2317 case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2318 case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2319 case elfcpp::R_SPARC_GOT10:
2320 case elfcpp::R_SPARC_GOT13:
2321 case elfcpp::R_SPARC_GOT22:
2323 // The symbol requires a GOT entry.
2324 Output_data_got<size, big_endian>* got;
2326 got = target->got_section(symtab, layout);
2327 if (gsym->final_value_is_known())
2328 got->add_global(gsym, GOT_TYPE_STANDARD);
2331 // If this symbol is not fully resolved, we need to add a
2332 // dynamic relocation for it.
2333 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2334 if (gsym->is_from_dynobj()
2335 || gsym->is_undefined()
2336 || gsym->is_preemptible())
2337 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2338 elfcpp::R_SPARC_GLOB_DAT);
2339 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
2341 unsigned int off = got->add_constant(0);
2343 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
2344 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
2351 // These are initial tls relocs, which are expected when
2353 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2354 case elfcpp::R_SPARC_TLS_GD_LO10:
2355 case elfcpp::R_SPARC_TLS_GD_ADD:
2356 case elfcpp::R_SPARC_TLS_GD_CALL:
2357 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
2358 case elfcpp::R_SPARC_TLS_LDM_LO10:
2359 case elfcpp::R_SPARC_TLS_LDM_ADD:
2360 case elfcpp::R_SPARC_TLS_LDM_CALL:
2361 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2362 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2363 case elfcpp::R_SPARC_TLS_LDO_ADD:
2364 case elfcpp::R_SPARC_TLS_LE_HIX22:
2365 case elfcpp::R_SPARC_TLS_LE_LOX10:
2366 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2367 case elfcpp::R_SPARC_TLS_IE_LO10:
2368 case elfcpp::R_SPARC_TLS_IE_LD:
2369 case elfcpp::R_SPARC_TLS_IE_LDX:
2370 case elfcpp::R_SPARC_TLS_IE_ADD:
2372 const bool is_final = gsym->final_value_is_known();
2373 const tls::Tls_optimization optimized_type
2374 = optimize_tls_reloc(is_final, r_type);
2377 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2378 case elfcpp::R_SPARC_TLS_GD_LO10:
2379 case elfcpp::R_SPARC_TLS_GD_ADD:
2380 case elfcpp::R_SPARC_TLS_GD_CALL:
2381 if (optimized_type == tls::TLSOPT_NONE)
2383 // Create a pair of GOT entries for the module index and
2384 // dtv-relative offset.
2385 Output_data_got<size, big_endian>* got
2386 = target->got_section(symtab, layout);
2387 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2388 target->rela_dyn_section(layout),
2390 elfcpp::R_SPARC_TLS_DTPMOD64 :
2391 elfcpp::R_SPARC_TLS_DTPMOD32),
2393 elfcpp::R_SPARC_TLS_DTPOFF64 :
2394 elfcpp::R_SPARC_TLS_DTPOFF32));
2396 // Emit R_SPARC_WPLT30 against "__tls_get_addr"
2397 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2398 generate_tls_call(symtab, layout, target);
2400 else if (optimized_type == tls::TLSOPT_TO_IE)
2402 // Create a GOT entry for the tp-relative offset.
2403 Output_data_got<size, big_endian>* got
2404 = target->got_section(symtab, layout);
2405 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2406 target->rela_dyn_section(layout),
2408 elfcpp::R_SPARC_TLS_TPOFF64 :
2409 elfcpp::R_SPARC_TLS_TPOFF32));
2411 else if (optimized_type != tls::TLSOPT_TO_LE)
2412 unsupported_reloc_global(object, r_type, gsym);
2415 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
2416 case elfcpp::R_SPARC_TLS_LDM_LO10:
2417 case elfcpp::R_SPARC_TLS_LDM_ADD:
2418 case elfcpp::R_SPARC_TLS_LDM_CALL:
2419 if (optimized_type == tls::TLSOPT_NONE)
2421 // Create a GOT entry for the module index.
2422 target->got_mod_index_entry(symtab, layout, object);
2424 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2425 generate_tls_call(symtab, layout, target);
2427 else if (optimized_type != tls::TLSOPT_TO_LE)
2428 unsupported_reloc_global(object, r_type, gsym);
2431 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2432 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2433 case elfcpp::R_SPARC_TLS_LDO_ADD:
2436 case elfcpp::R_SPARC_TLS_LE_HIX22:
2437 case elfcpp::R_SPARC_TLS_LE_LOX10:
2438 layout->set_has_static_tls();
2439 if (parameters->options().shared())
2441 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2442 rela_dyn->add_symbolless_global_addend(gsym, orig_r_type,
2443 output_section, object,
2444 data_shndx, reloc.get_r_offset(),
2449 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2450 case elfcpp::R_SPARC_TLS_IE_LO10:
2451 case elfcpp::R_SPARC_TLS_IE_LD:
2452 case elfcpp::R_SPARC_TLS_IE_LDX:
2453 case elfcpp::R_SPARC_TLS_IE_ADD:
2454 layout->set_has_static_tls();
2455 if (optimized_type == tls::TLSOPT_NONE)
2457 // Create a GOT entry for the tp-relative offset.
2458 Output_data_got<size, big_endian>* got
2459 = target->got_section(symtab, layout);
2460 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2461 target->rela_dyn_section(layout),
2463 elfcpp::R_SPARC_TLS_TPOFF64 :
2464 elfcpp::R_SPARC_TLS_TPOFF32));
2466 else if (optimized_type != tls::TLSOPT_TO_LE)
2467 unsupported_reloc_global(object, r_type, gsym);
2473 // These are relocations which should only be seen by the
2474 // dynamic linker, and should never be seen here.
2475 case elfcpp::R_SPARC_COPY:
2476 case elfcpp::R_SPARC_GLOB_DAT:
2477 case elfcpp::R_SPARC_JMP_SLOT:
2478 case elfcpp::R_SPARC_RELATIVE:
2479 case elfcpp::R_SPARC_TLS_DTPMOD64:
2480 case elfcpp::R_SPARC_TLS_DTPMOD32:
2481 case elfcpp::R_SPARC_TLS_DTPOFF64:
2482 case elfcpp::R_SPARC_TLS_DTPOFF32:
2483 case elfcpp::R_SPARC_TLS_TPOFF64:
2484 case elfcpp::R_SPARC_TLS_TPOFF32:
2485 gold_error(_("%s: unexpected reloc %u in object file"),
2486 object->name().c_str(), r_type);
2490 unsupported_reloc_global(object, r_type, gsym);
2495 // Process relocations for gc.
2497 template<int size, bool big_endian>
2499 Target_sparc<size, big_endian>::gc_process_relocs(
2500 Symbol_table* symtab,
2502 Sized_relobj_file<size, big_endian>* object,
2503 unsigned int data_shndx,
2505 const unsigned char* prelocs,
2507 Output_section* output_section,
2508 bool needs_special_offset_handling,
2509 size_t local_symbol_count,
2510 const unsigned char* plocal_symbols)
2512 typedef Target_sparc<size, big_endian> Sparc;
2513 typedef typename Target_sparc<size, big_endian>::Scan Scan;
2515 gold::gc_process_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan,
2516 typename Target_sparc::Relocatable_size_for_reloc>(
2525 needs_special_offset_handling,
2530 // Scan relocations for a section.
2532 template<int size, bool big_endian>
2534 Target_sparc<size, big_endian>::scan_relocs(
2535 Symbol_table* symtab,
2537 Sized_relobj_file<size, big_endian>* object,
2538 unsigned int data_shndx,
2539 unsigned int sh_type,
2540 const unsigned char* prelocs,
2542 Output_section* output_section,
2543 bool needs_special_offset_handling,
2544 size_t local_symbol_count,
2545 const unsigned char* plocal_symbols)
2547 typedef Target_sparc<size, big_endian> Sparc;
2548 typedef typename Target_sparc<size, big_endian>::Scan Scan;
2550 if (sh_type == elfcpp::SHT_REL)
2552 gold_error(_("%s: unsupported REL reloc section"),
2553 object->name().c_str());
2557 gold::scan_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan>(
2566 needs_special_offset_handling,
2571 // Finalize the sections.
2573 template<int size, bool big_endian>
2575 Target_sparc<size, big_endian>::do_finalize_sections(
2577 const Input_objects*,
2580 // Fill in some more dynamic tags.
2581 const Reloc_section* rel_plt = (this->plt_ == NULL
2583 : this->plt_->rel_plt());
2584 layout->add_target_dynamic_tags(false, this->plt_, rel_plt,
2585 this->rela_dyn_, true, true);
2587 // Emit any relocs we saved in an attempt to avoid generating COPY
2589 if (this->copy_relocs_.any_saved_relocs())
2590 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2593 // Perform a relocation.
2595 template<int size, bool big_endian>
2597 Target_sparc<size, big_endian>::Relocate::relocate(
2598 const Relocate_info<size, big_endian>* relinfo,
2599 Target_sparc* target,
2602 const elfcpp::Rela<size, big_endian>& rela,
2603 unsigned int r_type,
2604 const Sized_symbol<size>* gsym,
2605 const Symbol_value<size>* psymval,
2606 unsigned char* view,
2607 typename elfcpp::Elf_types<size>::Elf_Addr address,
2608 section_size_type view_size)
2612 if (this->ignore_gd_add_)
2614 if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
2615 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2616 _("missing expected TLS relocation"));
2619 this->ignore_gd_add_ = false;
2624 typedef Sparc_relocate_functions<size, big_endian> Reloc;
2626 // Pick the value to use for symbols defined in shared objects.
2627 Symbol_value<size> symval;
2629 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2631 elfcpp::Elf_Xword value;
2633 value = target->plt_section()->address() + gsym->plt_offset();
2635 symval.set_output_value(value);
2640 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2641 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2643 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
2644 // pointer points to the beginning, not the end, of the table.
2645 // So we just use the plain offset.
2646 unsigned int got_offset = 0;
2649 case elfcpp::R_SPARC_GOTDATA_OP:
2650 case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2651 case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2652 case elfcpp::R_SPARC_GOT10:
2653 case elfcpp::R_SPARC_GOT13:
2654 case elfcpp::R_SPARC_GOT22:
2657 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2658 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
2662 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2663 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2664 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2674 case elfcpp::R_SPARC_NONE:
2675 case elfcpp::R_SPARC_REGISTER:
2676 case elfcpp::R_SPARC_GNU_VTINHERIT:
2677 case elfcpp::R_SPARC_GNU_VTENTRY:
2680 case elfcpp::R_SPARC_8:
2681 Relocate_functions<size, big_endian>::rela8(view, object,
2685 case elfcpp::R_SPARC_16:
2686 if (rela.get_r_offset() & 0x1)
2688 // The assembler can sometimes emit unaligned relocations
2689 // for dwarf2 cfi directives.
2690 Reloc::ua16(view, object, psymval, addend);
2693 Relocate_functions<size, big_endian>::rela16(view, object,
2697 case elfcpp::R_SPARC_32:
2698 if (!parameters->options().output_is_position_independent())
2700 if (rela.get_r_offset() & 0x3)
2702 // The assembler can sometimes emit unaligned relocations
2703 // for dwarf2 cfi directives.
2704 Reloc::ua32(view, object, psymval, addend);
2707 Relocate_functions<size, big_endian>::rela32(view, object,
2712 case elfcpp::R_SPARC_DISP8:
2713 Reloc::disp8(view, object, psymval, addend, address);
2716 case elfcpp::R_SPARC_DISP16:
2717 Reloc::disp16(view, object, psymval, addend, address);
2720 case elfcpp::R_SPARC_DISP32:
2721 Reloc::disp32(view, object, psymval, addend, address);
2724 case elfcpp::R_SPARC_DISP64:
2725 Reloc::disp64(view, object, psymval, addend, address);
2728 case elfcpp::R_SPARC_WDISP30:
2729 case elfcpp::R_SPARC_WPLT30:
2730 Reloc::wdisp30(view, object, psymval, addend, address);
2733 case elfcpp::R_SPARC_WDISP22:
2734 Reloc::wdisp22(view, object, psymval, addend, address);
2737 case elfcpp::R_SPARC_WDISP19:
2738 Reloc::wdisp19(view, object, psymval, addend, address);
2741 case elfcpp::R_SPARC_WDISP16:
2742 Reloc::wdisp16(view, object, psymval, addend, address);
2745 case elfcpp::R_SPARC_HI22:
2746 Reloc::hi22(view, object, psymval, addend);
2749 case elfcpp::R_SPARC_22:
2750 Reloc::rela32_22(view, object, psymval, addend);
2753 case elfcpp::R_SPARC_13:
2754 Reloc::rela32_13(view, object, psymval, addend);
2757 case elfcpp::R_SPARC_LO10:
2758 Reloc::lo10(view, object, psymval, addend);
2761 case elfcpp::R_SPARC_GOT10:
2762 Reloc::lo10(view, got_offset, addend);
2765 case elfcpp::R_SPARC_GOTDATA_OP:
2768 case elfcpp::R_SPARC_GOTDATA_OP_LOX10:
2769 case elfcpp::R_SPARC_GOT13:
2770 Reloc::rela32_13(view, got_offset, addend);
2773 case elfcpp::R_SPARC_GOTDATA_OP_HIX22:
2774 case elfcpp::R_SPARC_GOT22:
2775 Reloc::hi22(view, got_offset, addend);
2778 case elfcpp::R_SPARC_PC10:
2779 Reloc::pc10(view, object, psymval, addend, address);
2782 case elfcpp::R_SPARC_PC22:
2783 Reloc::pc22(view, object, psymval, addend, address);
2786 case elfcpp::R_SPARC_TLS_DTPOFF32:
2787 case elfcpp::R_SPARC_UA32:
2788 Reloc::ua32(view, object, psymval, addend);
2791 case elfcpp::R_SPARC_PLT64:
2792 Relocate_functions<size, big_endian>::rela64(view, object,
2796 case elfcpp::R_SPARC_PLT32:
2797 Relocate_functions<size, big_endian>::rela32(view, object,
2801 case elfcpp::R_SPARC_HIPLT22:
2802 Reloc::hi22(view, object, psymval, addend);
2805 case elfcpp::R_SPARC_LOPLT10:
2806 Reloc::lo10(view, object, psymval, addend);
2809 case elfcpp::R_SPARC_PCPLT32:
2810 Reloc::disp32(view, object, psymval, addend, address);
2813 case elfcpp::R_SPARC_PCPLT22:
2814 Reloc::pcplt22(view, object, psymval, addend, address);
2817 case elfcpp::R_SPARC_PCPLT10:
2818 Reloc::lo10(view, object, psymval, addend, address);
2821 case elfcpp::R_SPARC_64:
2822 if (!parameters->options().output_is_position_independent())
2824 if (rela.get_r_offset() & 0x7)
2826 // The assembler can sometimes emit unaligned relocations
2827 // for dwarf2 cfi directives.
2828 Reloc::ua64(view, object, psymval, addend);
2831 Relocate_functions<size, big_endian>::rela64(view, object,
2836 case elfcpp::R_SPARC_OLO10:
2838 unsigned int addend2 = rela.get_r_info() & 0xffffffff;
2839 addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
2840 Reloc::olo10(view, object, psymval, addend, addend2);
2844 case elfcpp::R_SPARC_HH22:
2845 Reloc::hh22(view, object, psymval, addend);
2848 case elfcpp::R_SPARC_PC_HH22:
2849 Reloc::pc_hh22(view, object, psymval, addend, address);
2852 case elfcpp::R_SPARC_HM10:
2853 Reloc::hm10(view, object, psymval, addend);
2856 case elfcpp::R_SPARC_PC_HM10:
2857 Reloc::pc_hm10(view, object, psymval, addend, address);
2860 case elfcpp::R_SPARC_LM22:
2861 Reloc::hi22(view, object, psymval, addend);
2864 case elfcpp::R_SPARC_PC_LM22:
2865 Reloc::pcplt22(view, object, psymval, addend, address);
2868 case elfcpp::R_SPARC_11:
2869 Reloc::rela32_11(view, object, psymval, addend);
2872 case elfcpp::R_SPARC_10:
2873 Reloc::rela32_10(view, object, psymval, addend);
2876 case elfcpp::R_SPARC_7:
2877 Reloc::rela32_7(view, object, psymval, addend);
2880 case elfcpp::R_SPARC_6:
2881 Reloc::rela32_6(view, object, psymval, addend);
2884 case elfcpp::R_SPARC_5:
2885 Reloc::rela32_5(view, object, psymval, addend);
2888 case elfcpp::R_SPARC_HIX22:
2889 Reloc::hix22(view, object, psymval, addend);
2892 case elfcpp::R_SPARC_LOX10:
2893 Reloc::lox10(view, object, psymval, addend);
2896 case elfcpp::R_SPARC_H44:
2897 Reloc::h44(view, object, psymval, addend);
2900 case elfcpp::R_SPARC_M44:
2901 Reloc::m44(view, object, psymval, addend);
2904 case elfcpp::R_SPARC_L44:
2905 Reloc::l44(view, object, psymval, addend);
2908 case elfcpp::R_SPARC_TLS_DTPOFF64:
2909 case elfcpp::R_SPARC_UA64:
2910 Reloc::ua64(view, object, psymval, addend);
2913 case elfcpp::R_SPARC_UA16:
2914 Reloc::ua16(view, object, psymval, addend);
2917 case elfcpp::R_SPARC_TLS_GD_HI22:
2918 case elfcpp::R_SPARC_TLS_GD_LO10:
2919 case elfcpp::R_SPARC_TLS_GD_ADD:
2920 case elfcpp::R_SPARC_TLS_GD_CALL:
2921 case elfcpp::R_SPARC_TLS_LDM_HI22:
2922 case elfcpp::R_SPARC_TLS_LDM_LO10:
2923 case elfcpp::R_SPARC_TLS_LDM_ADD:
2924 case elfcpp::R_SPARC_TLS_LDM_CALL:
2925 case elfcpp::R_SPARC_TLS_LDO_HIX22:
2926 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2927 case elfcpp::R_SPARC_TLS_LDO_ADD:
2928 case elfcpp::R_SPARC_TLS_IE_HI22:
2929 case elfcpp::R_SPARC_TLS_IE_LO10:
2930 case elfcpp::R_SPARC_TLS_IE_LD:
2931 case elfcpp::R_SPARC_TLS_IE_LDX:
2932 case elfcpp::R_SPARC_TLS_IE_ADD:
2933 case elfcpp::R_SPARC_TLS_LE_HIX22:
2934 case elfcpp::R_SPARC_TLS_LE_LOX10:
2935 this->relocate_tls(relinfo, target, relnum, rela,
2936 r_type, gsym, psymval, view,
2937 address, view_size);
2940 case elfcpp::R_SPARC_COPY:
2941 case elfcpp::R_SPARC_GLOB_DAT:
2942 case elfcpp::R_SPARC_JMP_SLOT:
2943 case elfcpp::R_SPARC_RELATIVE:
2944 // These are outstanding tls relocs, which are unexpected when
2946 case elfcpp::R_SPARC_TLS_DTPMOD64:
2947 case elfcpp::R_SPARC_TLS_DTPMOD32:
2948 case elfcpp::R_SPARC_TLS_TPOFF64:
2949 case elfcpp::R_SPARC_TLS_TPOFF32:
2950 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2951 _("unexpected reloc %u in object file"),
2956 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2957 _("unsupported reloc %u"),
2965 // Perform a TLS relocation.
2967 template<int size, bool big_endian>
2969 Target_sparc<size, big_endian>::Relocate::relocate_tls(
2970 const Relocate_info<size, big_endian>* relinfo,
2971 Target_sparc<size, big_endian>* target,
2973 const elfcpp::Rela<size, big_endian>& rela,
2974 unsigned int r_type,
2975 const Sized_symbol<size>* gsym,
2976 const Symbol_value<size>* psymval,
2977 unsigned char* view,
2978 typename elfcpp::Elf_types<size>::Elf_Addr address,
2981 Output_segment* tls_segment = relinfo->layout->tls_segment();
2982 typedef Sparc_relocate_functions<size, big_endian> Reloc;
2983 const Sized_relobj_file<size, big_endian>* object = relinfo->object;
2984 typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
2986 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2987 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2989 const bool is_final =
2991 ? !parameters->options().output_is_position_independent()
2992 : gsym->final_value_is_known());
2993 const tls::Tls_optimization optimized_type
2994 = optimize_tls_reloc(is_final, r_type);
2998 case elfcpp::R_SPARC_TLS_GD_HI22:
2999 case elfcpp::R_SPARC_TLS_GD_LO10:
3000 case elfcpp::R_SPARC_TLS_GD_ADD:
3001 case elfcpp::R_SPARC_TLS_GD_CALL:
3002 if (optimized_type == tls::TLSOPT_TO_LE)
3004 Insntype* wv = reinterpret_cast<Insntype*>(view);
3007 value -= tls_segment->memsz();
3011 case elfcpp::R_SPARC_TLS_GD_HI22:
3012 // TLS_GD_HI22 --> TLS_LE_HIX22
3013 Reloc::hix22(view, value, addend);
3016 case elfcpp::R_SPARC_TLS_GD_LO10:
3017 // TLS_GD_LO10 --> TLS_LE_LOX10
3018 Reloc::lox10(view, value, addend);
3021 case elfcpp::R_SPARC_TLS_GD_ADD:
3022 // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
3023 val = elfcpp::Swap<32, true>::readval(wv);
3024 val = (val & ~0x7c000) | 0x1c000;
3025 elfcpp::Swap<32, true>::writeval(wv, val);
3027 case elfcpp::R_SPARC_TLS_GD_CALL:
3028 // call __tls_get_addr --> nop
3029 elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
3036 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
3037 ? GOT_TYPE_TLS_OFFSET
3038 : GOT_TYPE_TLS_PAIR);
3041 gold_assert(gsym->has_got_offset(got_type));
3042 value = gsym->got_offset(got_type);
3046 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3047 gold_assert(object->local_has_got_offset(r_sym, got_type));
3048 value = object->local_got_offset(r_sym, got_type);
3050 if (optimized_type == tls::TLSOPT_TO_IE)
3052 Insntype* wv = reinterpret_cast<Insntype*>(view);
3057 case elfcpp::R_SPARC_TLS_GD_HI22:
3058 // TLS_GD_HI22 --> TLS_IE_HI22
3059 Reloc::hi22(view, value, addend);
3062 case elfcpp::R_SPARC_TLS_GD_LO10:
3063 // TLS_GD_LO10 --> TLS_IE_LO10
3064 Reloc::lo10(view, value, addend);
3067 case elfcpp::R_SPARC_TLS_GD_ADD:
3068 // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
3069 val = elfcpp::Swap<32, true>::readval(wv);
3076 elfcpp::Swap<32, true>::writeval(wv, val);
3079 case elfcpp::R_SPARC_TLS_GD_CALL:
3080 // The compiler can put the TLS_GD_ADD instruction
3081 // into the delay slot of the call. If so, we need
3082 // to transpose the two instructions so that the
3083 // new sequence works properly.
3085 // The test we use is if the instruction in the
3086 // delay slot is an add with destination register
3088 val = elfcpp::Swap<32, true>::readval(wv + 1);
3089 if ((val & 0x81f80000) == 0x80000000
3090 && ((val >> 25) & 0x1f) == 0x8)
3097 elfcpp::Swap<32, true>::writeval(wv, val);
3100 this->ignore_gd_add_ = true;
3103 // call __tls_get_addr --> add %g7, %o0, %o0
3104 elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
3109 else if (optimized_type == tls::TLSOPT_NONE)
3113 case elfcpp::R_SPARC_TLS_GD_HI22:
3114 Reloc::hi22(view, value, addend);
3116 case elfcpp::R_SPARC_TLS_GD_LO10:
3117 Reloc::lo10(view, value, addend);
3119 case elfcpp::R_SPARC_TLS_GD_ADD:
3121 case elfcpp::R_SPARC_TLS_GD_CALL:
3123 Symbol_value<size> symval;
3124 elfcpp::Elf_Xword value;
3127 tsym = target->tls_get_addr_sym_;
3129 value = (target->plt_section()->address() +
3130 tsym->plt_offset());
3131 symval.set_output_value(value);
3132 Reloc::wdisp30(view, object, &symval, addend, address);
3139 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3140 _("unsupported reloc %u"),
3144 case elfcpp::R_SPARC_TLS_LDM_HI22:
3145 case elfcpp::R_SPARC_TLS_LDM_LO10:
3146 case elfcpp::R_SPARC_TLS_LDM_ADD:
3147 case elfcpp::R_SPARC_TLS_LDM_CALL:
3148 if (optimized_type == tls::TLSOPT_TO_LE)
3150 Insntype* wv = reinterpret_cast<Insntype*>(view);
3154 case elfcpp::R_SPARC_TLS_LDM_HI22:
3155 case elfcpp::R_SPARC_TLS_LDM_LO10:
3156 case elfcpp::R_SPARC_TLS_LDM_ADD:
3157 elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
3160 case elfcpp::R_SPARC_TLS_LDM_CALL:
3161 elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
3166 else if (optimized_type == tls::TLSOPT_NONE)
3168 // Relocate the field with the offset of the GOT entry for
3169 // the module index.
3170 unsigned int got_offset;
3172 got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
3175 case elfcpp::R_SPARC_TLS_LDM_HI22:
3176 Reloc::hi22(view, got_offset, addend);
3178 case elfcpp::R_SPARC_TLS_LDM_LO10:
3179 Reloc::lo10(view, got_offset, addend);
3181 case elfcpp::R_SPARC_TLS_LDM_ADD:
3183 case elfcpp::R_SPARC_TLS_LDM_CALL:
3185 Symbol_value<size> symval;
3186 elfcpp::Elf_Xword value;
3189 tsym = target->tls_get_addr_sym_;
3191 value = (target->plt_section()->address() +
3192 tsym->plt_offset());
3193 symval.set_output_value(value);
3194 Reloc::wdisp30(view, object, &symval, addend, address);
3200 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3201 _("unsupported reloc %u"),
3205 // These relocs can appear in debugging sections, in which case
3206 // we won't see the TLS_LDM relocs. The local_dynamic_type
3207 // field tells us this.
3208 case elfcpp::R_SPARC_TLS_LDO_HIX22:
3209 if (optimized_type == tls::TLSOPT_TO_LE)
3211 value -= tls_segment->memsz();
3212 Reloc::hix22(view, value, addend);
3215 Reloc::ldo_hix22(view, value, addend);
3217 case elfcpp::R_SPARC_TLS_LDO_LOX10:
3218 if (optimized_type == tls::TLSOPT_TO_LE)
3220 value -= tls_segment->memsz();
3221 Reloc::lox10(view, value, addend);
3224 Reloc::ldo_lox10(view, value, addend);
3226 case elfcpp::R_SPARC_TLS_LDO_ADD:
3227 if (optimized_type == tls::TLSOPT_TO_LE)
3229 Insntype* wv = reinterpret_cast<Insntype*>(view);
3232 // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
3233 val = elfcpp::Swap<32, true>::readval(wv);
3234 val = (val & ~0x7c000) | 0x1c000;
3235 elfcpp::Swap<32, true>::writeval(wv, val);
3239 // When optimizing IE --> LE, the only relocation that is handled
3240 // differently is R_SPARC_TLS_IE_LD, it is rewritten from
3241 // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
3242 // rs2 and rd are the same.
3243 case elfcpp::R_SPARC_TLS_IE_LD:
3244 case elfcpp::R_SPARC_TLS_IE_LDX:
3245 if (optimized_type == tls::TLSOPT_TO_LE)
3247 Insntype* wv = reinterpret_cast<Insntype*>(view);
3248 Insntype val = elfcpp::Swap<32, true>::readval(wv);
3249 Insntype rs2 = val & 0x1f;
3250 Insntype rd = (val >> 25) & 0x1f;
3255 val = sparc_mov | (val & 0x3e00001f);
3257 elfcpp::Swap<32, true>::writeval(wv, val);
3261 case elfcpp::R_SPARC_TLS_IE_HI22:
3262 case elfcpp::R_SPARC_TLS_IE_LO10:
3263 if (optimized_type == tls::TLSOPT_TO_LE)
3265 value -= tls_segment->memsz();
3268 case elfcpp::R_SPARC_TLS_IE_HI22:
3269 // IE_HI22 --> LE_HIX22
3270 Reloc::hix22(view, value, addend);
3272 case elfcpp::R_SPARC_TLS_IE_LO10:
3273 // IE_LO10 --> LE_LOX10
3274 Reloc::lox10(view, value, addend);
3279 else if (optimized_type == tls::TLSOPT_NONE)
3281 // Relocate the field with the offset of the GOT entry for
3282 // the tp-relative offset of the symbol.
3285 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3286 value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
3290 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
3291 gold_assert(object->local_has_got_offset(r_sym,
3292 GOT_TYPE_TLS_OFFSET));
3293 value = object->local_got_offset(r_sym,
3294 GOT_TYPE_TLS_OFFSET);
3298 case elfcpp::R_SPARC_TLS_IE_HI22:
3299 Reloc::hi22(view, value, addend);
3301 case elfcpp::R_SPARC_TLS_IE_LO10:
3302 Reloc::lo10(view, value, addend);
3307 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3308 _("unsupported reloc %u"),
3312 case elfcpp::R_SPARC_TLS_IE_ADD:
3313 // This seems to be mainly so that we can find the addition
3314 // instruction if there is one. There doesn't seem to be any
3315 // actual relocation to apply.
3318 case elfcpp::R_SPARC_TLS_LE_HIX22:
3319 // If we're creating a shared library, a dynamic relocation will
3320 // have been created for this location, so do not apply it now.
3321 if (!parameters->options().shared())
3323 value -= tls_segment->memsz();
3324 Reloc::hix22(view, value, addend);
3328 case elfcpp::R_SPARC_TLS_LE_LOX10:
3329 // If we're creating a shared library, a dynamic relocation will
3330 // have been created for this location, so do not apply it now.
3331 if (!parameters->options().shared())
3333 value -= tls_segment->memsz();
3334 Reloc::lox10(view, value, addend);
3340 // Relocate section data.
3342 template<int size, bool big_endian>
3344 Target_sparc<size, big_endian>::relocate_section(
3345 const Relocate_info<size, big_endian>* relinfo,
3346 unsigned int sh_type,
3347 const unsigned char* prelocs,
3349 Output_section* output_section,
3350 bool needs_special_offset_handling,
3351 unsigned char* view,
3352 typename elfcpp::Elf_types<size>::Elf_Addr address,
3353 section_size_type view_size,
3354 const Reloc_symbol_changes* reloc_symbol_changes)
3356 typedef Target_sparc<size, big_endian> Sparc;
3357 typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
3359 gold_assert(sh_type == elfcpp::SHT_RELA);
3361 gold::relocate_section<size, big_endian, Sparc, elfcpp::SHT_RELA,
3368 needs_special_offset_handling,
3372 reloc_symbol_changes);
3375 // Return the size of a relocation while scanning during a relocatable
3378 template<int size, bool big_endian>
3380 Target_sparc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
3384 // We are always SHT_RELA, so we should never get here.
3389 // Scan the relocs during a relocatable link.
3391 template<int size, bool big_endian>
3393 Target_sparc<size, big_endian>::scan_relocatable_relocs(
3394 Symbol_table* symtab,
3396 Sized_relobj_file<size, big_endian>* object,
3397 unsigned int data_shndx,
3398 unsigned int sh_type,
3399 const unsigned char* prelocs,
3401 Output_section* output_section,
3402 bool needs_special_offset_handling,
3403 size_t local_symbol_count,
3404 const unsigned char* plocal_symbols,
3405 Relocatable_relocs* rr)
3407 gold_assert(sh_type == elfcpp::SHT_RELA);
3409 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3410 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3412 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3413 Scan_relocatable_relocs>(
3421 needs_special_offset_handling,
3427 // Relocate a section during a relocatable link.
3429 template<int size, bool big_endian>
3431 Target_sparc<size, big_endian>::relocate_for_relocatable(
3432 const Relocate_info<size, big_endian>* relinfo,
3433 unsigned int sh_type,
3434 const unsigned char* prelocs,
3436 Output_section* output_section,
3437 off_t offset_in_output_section,
3438 const Relocatable_relocs* rr,
3439 unsigned char* view,
3440 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3441 section_size_type view_size,
3442 unsigned char* reloc_view,
3443 section_size_type reloc_view_size)
3445 gold_assert(sh_type == elfcpp::SHT_RELA);
3447 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
3452 offset_in_output_section,
3461 // Return the value to use for a dynamic which requires special
3462 // treatment. This is how we support equality comparisons of function
3463 // pointers across shared library boundaries, as described in the
3464 // processor specific ABI supplement.
3466 template<int size, bool big_endian>
3468 Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
3470 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3471 return this->plt_section()->address() + gsym->plt_offset();
3474 // The selector for sparc object files.
3476 template<int size, bool big_endian>
3477 class Target_selector_sparc : public Target_selector
3480 Target_selector_sparc()
3481 : Target_selector(elfcpp::EM_NONE, size, big_endian,
3482 (size == 64 ? "elf64-sparc" : "elf32-sparc"))
3485 Target* do_recognize(int machine, int, int)
3490 if (machine != elfcpp::EM_SPARCV9)
3495 if (machine != elfcpp::EM_SPARC
3496 && machine != elfcpp::EM_SPARC32PLUS)
3504 return this->instantiate_target();
3507 Target* do_instantiate_target()
3508 { return new Target_sparc<size, big_endian>(); }
3511 Target_selector_sparc<32, true> target_selector_sparc32;
3512 Target_selector_sparc<64, true> target_selector_sparc64;
3514 } // End anonymous namespace.