toplevel/
[binutils.git] / gold / sparc.cc
blob2812fe1437049a331dbbd9c805d2534348055c39
1 // sparc.cc -- sparc target support for gold.
3 // Copyright 2008 Free Software Foundation, Inc.
4 // Written by David S. Miller <davem@davemloft.net>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstdlib>
26 #include <cstdio>
27 #include <cstring>
29 #include "elfcpp.h"
30 #include "parameters.h"
31 #include "reloc.h"
32 #include "sparc.h"
33 #include "object.h"
34 #include "symtab.h"
35 #include "layout.h"
36 #include "output.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "errors.h"
43 namespace
46 using namespace gold;
48 template<int size, bool big_endian>
49 class Output_data_plt_sparc;
51 template<int size, bool big_endian>
52 class Target_sparc : public Sized_target<size, big_endian>
54 public:
55 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian> Reloc_section;
57 Target_sparc()
58 : Sized_target<size, big_endian>(&sparc_info),
59 got_(NULL), plt_(NULL), rela_dyn_(NULL),
60 copy_relocs_(NULL), dynbss_(NULL), got_mod_index_offset_(-1U),
61 tls_get_addr_sym_(NULL)
65 // Scan the relocations to look for symbol adjustments.
66 void
67 scan_relocs(const General_options& options,
68 Symbol_table* symtab,
69 Layout* layout,
70 Sized_relobj<size, big_endian>* object,
71 unsigned int data_shndx,
72 unsigned int sh_type,
73 const unsigned char* prelocs,
74 size_t reloc_count,
75 Output_section* output_section,
76 bool needs_special_offset_handling,
77 size_t local_symbol_count,
78 const unsigned char* plocal_symbols);
79 // Finalize the sections.
80 void
81 do_finalize_sections(Layout*);
83 // Return the value to use for a dynamic which requires special
84 // treatment.
85 uint64_t
86 do_dynsym_value(const Symbol*) const;
88 // Relocate a section.
89 void
90 relocate_section(const Relocate_info<size, big_endian>*,
91 unsigned int sh_type,
92 const unsigned char* prelocs,
93 size_t reloc_count,
94 Output_section* output_section,
95 bool needs_special_offset_handling,
96 unsigned char* view,
97 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
98 section_size_type view_size);
100 // Scan the relocs during a relocatable link.
101 void
102 scan_relocatable_relocs(const General_options& options,
103 Symbol_table* symtab,
104 Layout* layout,
105 Sized_relobj<size, big_endian>* object,
106 unsigned int data_shndx,
107 unsigned int sh_type,
108 const unsigned char* prelocs,
109 size_t reloc_count,
110 Output_section* output_section,
111 bool needs_special_offset_handling,
112 size_t local_symbol_count,
113 const unsigned char* plocal_symbols,
114 Relocatable_relocs*);
116 // Relocate a section during a relocatable link.
117 void
118 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
119 unsigned int sh_type,
120 const unsigned char* prelocs,
121 size_t reloc_count,
122 Output_section* output_section,
123 off_t offset_in_output_section,
124 const Relocatable_relocs*,
125 unsigned char* view,
126 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
127 section_size_type view_size,
128 unsigned char* reloc_view,
129 section_size_type reloc_view_size);
130 // Return whether SYM is defined by the ABI.
131 bool
132 do_is_defined_by_abi(Symbol* sym) const
134 // XXX Really need to support this better...
135 if (sym->type() == elfcpp::STT_SPARC_REGISTER)
136 return 1;
138 return strcmp(sym->name(), "___tls_get_addr") == 0;
141 // Return the size of the GOT section.
142 section_size_type
143 got_size()
145 gold_assert(this->got_ != NULL);
146 return this->got_->data_size();
149 private:
151 // The class which scans relocations.
152 struct Scan
154 inline void
155 local(const General_options& options, Symbol_table* symtab,
156 Layout* layout, Target_sparc* target,
157 Sized_relobj<size, big_endian>* object,
158 unsigned int data_shndx,
159 Output_section* output_section,
160 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
161 const elfcpp::Sym<size, big_endian>& lsym);
163 inline void
164 global(const General_options& options, Symbol_table* symtab,
165 Layout* layout, Target_sparc* target,
166 Sized_relobj<size, big_endian>* object,
167 unsigned int data_shndx,
168 Output_section* output_section,
169 const elfcpp::Rela<size, big_endian>& reloc, unsigned int r_type,
170 Symbol* gsym);
172 static void
173 unsupported_reloc_local(Sized_relobj<size, big_endian>*,
174 unsigned int r_type);
176 static void
177 unsupported_reloc_global(Sized_relobj<size, big_endian>*,
178 unsigned int r_type, Symbol*);
180 static void
181 generate_tls_call(Symbol_table* symtab, Layout* layout,
182 Target_sparc* target);
185 // The class which implements relocation.
186 class Relocate
188 public:
189 Relocate()
190 : ignore_gd_add_(false)
193 ~Relocate()
195 if (this->ignore_gd_add_)
197 // FIXME: This needs to specify the location somehow.
198 gold_error(_("missing expected TLS relocation"));
202 // Do a relocation. Return false if the caller should not issue
203 // any warnings about this relocation.
204 inline bool
205 relocate(const Relocate_info<size, big_endian>*, Target_sparc*,
206 size_t relnum, const elfcpp::Rela<size, big_endian>&,
207 unsigned int r_type, const Sized_symbol<size>*,
208 const Symbol_value<size>*,
209 unsigned char*,
210 typename elfcpp::Elf_types<size>::Elf_Addr,
211 section_size_type);
213 private:
214 // Do a TLS relocation.
215 inline void
216 relocate_tls(const Relocate_info<size, big_endian>*, Target_sparc* target,
217 size_t relnum, const elfcpp::Rela<size, big_endian>&,
218 unsigned int r_type, const Sized_symbol<size>*,
219 const Symbol_value<size>*,
220 unsigned char*,
221 typename elfcpp::Elf_types<size>::Elf_Addr,
222 section_size_type);
224 // Ignore the next relocation which should be R_SPARC_TLS_GD_ADD
225 bool ignore_gd_add_;
228 // A class which returns the size required for a relocation type,
229 // used while scanning relocs during a relocatable link.
230 class Relocatable_size_for_reloc
232 public:
233 unsigned int
234 get_size_for_reloc(unsigned int, Relobj*);
237 // Get the GOT section, creating it if necessary.
238 Output_data_got<size, big_endian>*
239 got_section(Symbol_table*, Layout*);
241 // Create a PLT entry for a global symbol.
242 void
243 make_plt_entry(Symbol_table*, Layout*, Symbol*);
245 // Create a GOT entry for the TLS module index.
246 unsigned int
247 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
248 Sized_relobj<size, big_endian>* object);
250 // Return the gsym for "__tls_get_addr". Cache if not already
251 // cached.
252 Symbol*
253 tls_get_addr_sym(Symbol_table* symtab)
255 if (!this->tls_get_addr_sym_)
256 this->tls_get_addr_sym_ = symtab->lookup("__tls_get_addr", NULL);
257 gold_assert(this->tls_get_addr_sym_);
258 return this->tls_get_addr_sym_;
261 // Get the PLT section.
262 const Output_data_plt_sparc<size, big_endian>*
263 plt_section() const
265 gold_assert(this->plt_ != NULL);
266 return this->plt_;
269 // Get the dynamic reloc section, creating it if necessary.
270 Reloc_section*
271 rela_dyn_section(Layout*);
273 // Return true if the symbol may need a COPY relocation.
274 // References from an executable object to non-function symbols
275 // defined in a dynamic object may need a COPY relocation.
276 bool
277 may_need_copy_reloc(Symbol* gsym)
279 return (!parameters->options().shared()
280 && gsym->is_from_dynobj()
281 && gsym->type() != elfcpp::STT_FUNC);
284 // Copy a relocation against a global symbol.
285 void
286 copy_reloc(const General_options*, Symbol_table*, Layout*,
287 Sized_relobj<size, big_endian>*, unsigned int,
288 Output_section*, Symbol*, const elfcpp::Rela<size, big_endian>&);
290 // Information about this specific target which we pass to the
291 // general Target structure.
292 static Target::Target_info sparc_info;
294 // The types of GOT entries needed for this platform.
295 enum Got_type
297 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
298 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
299 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
302 // The GOT section.
303 Output_data_got<size, big_endian>* got_;
304 // The PLT section.
305 Output_data_plt_sparc<size, big_endian>* plt_;
306 // The dynamic reloc section.
307 Reloc_section* rela_dyn_;
308 // Relocs saved to avoid a COPY reloc.
309 Copy_relocs<size, big_endian>* copy_relocs_;
310 // Space for variables copied with a COPY reloc.
311 Output_data_space* dynbss_;
312 // Offset of the GOT entry for the TLS module index;
313 unsigned int got_mod_index_offset_;
314 // Cached pointer to __tls_get_addr symbol
315 Symbol* tls_get_addr_sym_;
318 template<>
319 Target::Target_info Target_sparc<32, true>::sparc_info =
321 32, // size
322 true, // is_big_endian
323 elfcpp::EM_SPARC, // machine_code
324 false, // has_make_symbol
325 false, // has_resolve
326 false, // has_code_fill
327 true, // is_default_stack_executable
328 '\0', // wrap_char
329 "/usr/lib/ld.so.1", // dynamic_linker
330 0x00010000, // default_text_segment_address
331 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
332 8 * 1024 // common_pagesize (overridable by -z common-page-size)
335 template<>
336 Target::Target_info Target_sparc<64, true>::sparc_info =
338 64, // size
339 true, // is_big_endian
340 elfcpp::EM_SPARCV9, // machine_code
341 false, // has_make_symbol
342 false, // has_resolve
343 false, // has_code_fill
344 true, // is_default_stack_executable
345 '\0', // wrap_char
346 "/usr/lib/sparcv9/ld.so.1", // dynamic_linker
347 0x100000, // default_text_segment_address
348 64 * 1024, // abi_pagesize (overridable by -z max-page-size)
349 8 * 1024 // common_pagesize (overridable by -z common-page-size)
352 // We have to take care here, even when operating in little-endian
353 // mode, sparc instructions are still big endian.
354 template<int size, bool big_endian>
355 class Sparc_relocate_functions
357 private:
358 // Do a simple relocation with the addend in the relocation.
359 template<int valsize>
360 static inline void
361 rela(unsigned char* view,
362 unsigned int right_shift,
363 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
364 typename elfcpp::Swap<size, big_endian>::Valtype value,
365 typename elfcpp::Swap<size, big_endian>::Valtype addend)
367 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
368 Valtype* wv = reinterpret_cast<Valtype*>(view);
369 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
370 Valtype reloc = ((value + addend) >> right_shift);
372 val &= ~dst_mask;
373 reloc &= dst_mask;
375 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
378 // Do a simple relocation using a symbol value with the addend in
379 // the relocation.
380 template<int valsize>
381 static inline void
382 rela(unsigned char* view,
383 unsigned int right_shift,
384 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
385 const Sized_relobj<size, big_endian>* object,
386 const Symbol_value<size>* psymval,
387 typename elfcpp::Swap<valsize, big_endian>::Valtype addend)
389 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
390 Valtype* wv = reinterpret_cast<Valtype*>(view);
391 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
392 Valtype reloc = (psymval->value(object, addend) >> right_shift);
394 val &= ~dst_mask;
395 reloc &= dst_mask;
397 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
400 // Do a simple relocation using a symbol value with the addend in
401 // the relocation, unaligned.
402 template<int valsize>
403 static inline void
404 rela_ua(unsigned char* view,
405 unsigned int right_shift, elfcpp::Elf_Xword dst_mask,
406 const Sized_relobj<size, big_endian>* object,
407 const Symbol_value<size>* psymval,
408 typename elfcpp::Swap<size, big_endian>::Valtype addend)
410 typedef typename elfcpp::Swap_unaligned<valsize,
411 big_endian>::Valtype Valtype;
412 unsigned char* wv = view;
413 Valtype val = elfcpp::Swap_unaligned<valsize, big_endian>::readval(wv);
414 Valtype reloc = (psymval->value(object, addend) >> right_shift);
416 val &= ~dst_mask;
417 reloc &= dst_mask;
419 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, val | reloc);
422 // Do a simple PC relative relocation with a Symbol_value with the
423 // addend in the relocation.
424 template<int valsize>
425 static inline void
426 pcrela(unsigned char* view,
427 unsigned int right_shift,
428 typename elfcpp::Elf_types<valsize>::Elf_Addr dst_mask,
429 const Sized_relobj<size, big_endian>* object,
430 const Symbol_value<size>* psymval,
431 typename elfcpp::Swap<size, big_endian>::Valtype addend,
432 typename elfcpp::Elf_types<size>::Elf_Addr address)
434 typedef typename elfcpp::Swap<valsize, big_endian>::Valtype Valtype;
435 Valtype* wv = reinterpret_cast<Valtype*>(view);
436 Valtype val = elfcpp::Swap<valsize, big_endian>::readval(wv);
437 Valtype reloc = ((psymval->value(object, addend) - address)
438 >> right_shift);
440 val &= ~dst_mask;
441 reloc &= dst_mask;
443 elfcpp::Swap<valsize, big_endian>::writeval(wv, val | reloc);
446 template<int valsize>
447 static inline void
448 pcrela_unaligned(unsigned char* view,
449 const Sized_relobj<size, big_endian>* object,
450 const Symbol_value<size>* psymval,
451 typename elfcpp::Swap<size, big_endian>::Valtype addend,
452 typename elfcpp::Elf_types<size>::Elf_Addr address)
454 typedef typename elfcpp::Swap_unaligned<valsize,
455 big_endian>::Valtype Valtype;
456 unsigned char* wv = view;
457 Valtype reloc = (psymval->value(object, addend) - address);
459 elfcpp::Swap_unaligned<valsize, big_endian>::writeval(wv, reloc);
462 typedef Sparc_relocate_functions<size, big_endian> This;
463 typedef Sparc_relocate_functions<size, true> This_insn;
465 public:
466 // R_SPARC_WDISP30: (Symbol + Addend - Address) >> 2
467 static inline void
468 wdisp30(unsigned char* view,
469 const Sized_relobj<size, big_endian>* object,
470 const Symbol_value<size>* psymval,
471 typename elfcpp::Elf_types<size>::Elf_Addr addend,
472 typename elfcpp::Elf_types<size>::Elf_Addr address)
474 This_insn::template pcrela<32>(view, 2, 0x3fffffff, object,
475 psymval, addend, address);
478 // R_SPARC_WDISP22: (Symbol + Addend - Address) >> 2
479 static inline void
480 wdisp22(unsigned char* view,
481 const Sized_relobj<size, big_endian>* object,
482 const Symbol_value<size>* psymval,
483 typename elfcpp::Elf_types<size>::Elf_Addr addend,
484 typename elfcpp::Elf_types<size>::Elf_Addr address)
486 This_insn::template pcrela<32>(view, 2, 0x003fffff, object,
487 psymval, addend, address);
490 // R_SPARC_WDISP19: (Symbol + Addend - Address) >> 2
491 static inline void
492 wdisp19(unsigned char* view,
493 const Sized_relobj<size, big_endian>* object,
494 const Symbol_value<size>* psymval,
495 typename elfcpp::Elf_types<size>::Elf_Addr addend,
496 typename elfcpp::Elf_types<size>::Elf_Addr address)
498 This_insn::template pcrela<32>(view, 2, 0x0007ffff, object,
499 psymval, addend, address);
502 // R_SPARC_WDISP16: (Symbol + Addend - Address) >> 2
503 static inline void
504 wdisp16(unsigned char* view,
505 const Sized_relobj<size, big_endian>* object,
506 const Symbol_value<size>* psymval,
507 typename elfcpp::Elf_types<size>::Elf_Addr addend,
508 typename elfcpp::Elf_types<size>::Elf_Addr address)
510 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
511 Valtype* wv = reinterpret_cast<Valtype*>(view);
512 Valtype val = elfcpp::Swap<32, true>::readval(wv);
513 Valtype reloc = ((psymval->value(object, addend) - address)
514 >> 2);
516 // The relocation value is split between the low 14 bits,
517 // and bits 20-21.
518 val &= ~((0x3 << 20) | 0x3fff);
519 reloc = (((reloc & 0xc000) << (20 - 14))
520 | (reloc & 0x3ffff));
522 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
525 // R_SPARC_PC22: (Symbol + Addend - Address) >> 10
526 static inline void
527 pc22(unsigned char* view,
528 const Sized_relobj<size, big_endian>* object,
529 const Symbol_value<size>* psymval,
530 typename elfcpp::Elf_types<size>::Elf_Addr addend,
531 typename elfcpp::Elf_types<size>::Elf_Addr address)
533 This_insn::template pcrela<32>(view, 10, 0x003fffff, object,
534 psymval, addend, address);
537 // R_SPARC_PC10: (Symbol + Addend - Address) & 0x3ff
538 static inline void
539 pc10(unsigned char* view,
540 const Sized_relobj<size, big_endian>* object,
541 const Symbol_value<size>* psymval,
542 typename elfcpp::Elf_types<size>::Elf_Addr addend,
543 typename elfcpp::Elf_types<size>::Elf_Addr address)
545 This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
546 psymval, addend, address);
549 // R_SPARC_HI22: (Symbol + Addend) >> 10
550 static inline void
551 hi22(unsigned char* view,
552 typename elfcpp::Elf_types<size>::Elf_Addr value,
553 typename elfcpp::Elf_types<size>::Elf_Addr addend)
555 This_insn::template rela<32>(view, 10, 0x003fffff, value, addend);
558 // R_SPARC_HI22: (Symbol + Addend) >> 10
559 static inline void
560 hi22(unsigned char* view,
561 const Sized_relobj<size, big_endian>* object,
562 const Symbol_value<size>* psymval,
563 typename elfcpp::Elf_types<size>::Elf_Addr addend)
565 This_insn::template rela<32>(view, 10, 0x003fffff, object, psymval, addend);
568 // R_SPARC_PCPLT22: (Symbol + Addend - Address) >> 10
569 static inline void
570 pcplt22(unsigned char* view,
571 const Sized_relobj<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, 10, 0x003fffff, object,
577 psymval, addend, address);
580 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
581 static inline void
582 lo10(unsigned char* view,
583 typename elfcpp::Elf_types<size>::Elf_Addr value,
584 typename elfcpp::Elf_types<size>::Elf_Addr addend)
586 This_insn::template rela<32>(view, 0, 0x000003ff, value, addend);
589 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
590 static inline void
591 lo10(unsigned char* view,
592 const Sized_relobj<size, big_endian>* object,
593 const Symbol_value<size>* psymval,
594 typename elfcpp::Elf_types<size>::Elf_Addr addend)
596 This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
599 // R_SPARC_LO10: (Symbol + Addend) & 0x3ff
600 static inline void
601 lo10(unsigned char* view,
602 const Sized_relobj<size, big_endian>* object,
603 const Symbol_value<size>* psymval,
604 typename elfcpp::Elf_types<size>::Elf_Addr addend,
605 typename elfcpp::Elf_types<size>::Elf_Addr address)
607 This_insn::template pcrela<32>(view, 0, 0x000003ff, object,
608 psymval, addend, address);
611 // R_SPARC_OLO10: ((Symbol + Addend) & 0x3ff) + Addend2
612 static inline void
613 olo10(unsigned char* view,
614 const Sized_relobj<size, big_endian>* object,
615 const Symbol_value<size>* psymval,
616 typename elfcpp::Elf_types<size>::Elf_Addr addend,
617 typename elfcpp::Elf_types<size>::Elf_Addr addend2)
619 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
620 Valtype* wv = reinterpret_cast<Valtype*>(view);
621 Valtype val = elfcpp::Swap<32, true>::readval(wv);
622 Valtype reloc = psymval->value(object, addend);
624 val &= ~0x1fff;
625 reloc &= 0x3ff;
626 reloc += addend2;
627 reloc &= 0x1fff;
629 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
632 // R_SPARC_22: (Symbol + Addend)
633 static inline void
634 rela32_22(unsigned char* view,
635 const Sized_relobj<size, big_endian>* object,
636 const Symbol_value<size>* psymval,
637 typename elfcpp::Elf_types<size>::Elf_Addr addend)
639 This_insn::template rela<32>(view, 0, 0x003fffff, object, psymval, addend);
642 // R_SPARC_13: (Symbol + Addend)
643 static inline void
644 rela32_13(unsigned char* view,
645 typename elfcpp::Elf_types<size>::Elf_Addr value,
646 typename elfcpp::Elf_types<size>::Elf_Addr addend)
648 This_insn::template rela<32>(view, 0, 0x00001fff, value, addend);
651 // R_SPARC_13: (Symbol + Addend)
652 static inline void
653 rela32_13(unsigned char* view,
654 const Sized_relobj<size, big_endian>* object,
655 const Symbol_value<size>* psymval,
656 typename elfcpp::Elf_types<size>::Elf_Addr addend)
658 This_insn::template rela<32>(view, 0, 0x00001fff, object, psymval, addend);
661 // R_SPARC_UA16: (Symbol + Addend)
662 static inline void
663 ua16(unsigned char* view,
664 const Sized_relobj<size, big_endian>* object,
665 const Symbol_value<size>* psymval,
666 typename elfcpp::Elf_types<size>::Elf_Addr addend)
668 This::template rela_ua<16>(view, 0, 0xffff, object, psymval, addend);
671 // R_SPARC_UA32: (Symbol + Addend)
672 static inline void
673 ua32(unsigned char* view,
674 const Sized_relobj<size, big_endian>* object,
675 const Symbol_value<size>* psymval,
676 typename elfcpp::Elf_types<size>::Elf_Addr addend)
678 This::template rela_ua<32>(view, 0, 0xffffffff, object, psymval, addend);
681 // R_SPARC_UA64: (Symbol + Addend)
682 static inline void
683 ua64(unsigned char* view,
684 const Sized_relobj<size, big_endian>* object,
685 const Symbol_value<size>* psymval,
686 typename elfcpp::Elf_types<size>::Elf_Addr addend)
688 This::template rela_ua<64>(view, 0, ~(elfcpp::Elf_Xword) 0,
689 object, psymval, addend);
692 // R_SPARC_DISP8: (Symbol + Addend - Address)
693 static inline void
694 disp8(unsigned char* view,
695 const Sized_relobj<size, big_endian>* object,
696 const Symbol_value<size>* psymval,
697 typename elfcpp::Elf_types<size>::Elf_Addr addend,
698 typename elfcpp::Elf_types<size>::Elf_Addr address)
700 This::template pcrela_unaligned<8>(view, object, psymval,
701 addend, address);
704 // R_SPARC_DISP16: (Symbol + Addend - Address)
705 static inline void
706 disp16(unsigned char* view,
707 const Sized_relobj<size, big_endian>* object,
708 const Symbol_value<size>* psymval,
709 typename elfcpp::Elf_types<size>::Elf_Addr addend,
710 typename elfcpp::Elf_types<size>::Elf_Addr address)
712 This::template pcrela_unaligned<16>(view, object, psymval,
713 addend, address);
716 // R_SPARC_DISP32: (Symbol + Addend - Address)
717 static inline void
718 disp32(unsigned char* view,
719 const Sized_relobj<size, big_endian>* object,
720 const Symbol_value<size>* psymval,
721 typename elfcpp::Elf_types<size>::Elf_Addr addend,
722 typename elfcpp::Elf_types<size>::Elf_Addr address)
724 This::template pcrela_unaligned<32>(view, object, psymval,
725 addend, address);
728 // R_SPARC_DISP64: (Symbol + Addend - Address)
729 static inline void
730 disp64(unsigned char* view,
731 const Sized_relobj<size, big_endian>* object,
732 const Symbol_value<size>* psymval,
733 elfcpp::Elf_Xword addend,
734 typename elfcpp::Elf_types<size>::Elf_Addr address)
736 This::template pcrela_unaligned<64>(view, object, psymval,
737 addend, address);
740 // R_SPARC_H44: (Symbol + Addend) >> 22
741 static inline void
742 h44(unsigned char* view,
743 const Sized_relobj<size, big_endian>* object,
744 const Symbol_value<size>* psymval,
745 typename elfcpp::Elf_types<size>::Elf_Addr addend)
747 This_insn::template rela<32>(view, 22, 0x003fffff, object, psymval, addend);
750 // R_SPARC_M44: ((Symbol + Addend) >> 12) & 0x3ff
751 static inline void
752 m44(unsigned char* view,
753 const Sized_relobj<size, big_endian>* object,
754 const Symbol_value<size>* psymval,
755 typename elfcpp::Elf_types<size>::Elf_Addr addend)
757 This_insn::template rela<32>(view, 12, 0x000003ff, object, psymval, addend);
760 // R_SPARC_L44: (Symbol + Addend) & 0xfff
761 static inline void
762 l44(unsigned char* view,
763 const Sized_relobj<size, big_endian>* object,
764 const Symbol_value<size>* psymval,
765 typename elfcpp::Elf_types<size>::Elf_Addr addend)
767 This_insn::template rela<32>(view, 0, 0x00000fff, object, psymval, addend);
770 // R_SPARC_HH22: (Symbol + Addend) >> 42
771 static inline void
772 hh22(unsigned char* view,
773 const Sized_relobj<size, big_endian>* object,
774 const Symbol_value<size>* psymval,
775 typename elfcpp::Elf_types<size>::Elf_Addr addend)
777 This_insn::template rela<32>(view, 42, 0x003fffff, object, psymval, addend);
780 // R_SPARC_PC_HH22: (Symbol + Addend - Address) >> 42
781 static inline void
782 pc_hh22(unsigned char* view,
783 const Sized_relobj<size, big_endian>* object,
784 const Symbol_value<size>* psymval,
785 typename elfcpp::Elf_types<size>::Elf_Addr addend,
786 typename elfcpp::Elf_types<size>::Elf_Addr address)
788 This_insn::template pcrela<32>(view, 42, 0x003fffff, object,
789 psymval, addend, address);
792 // R_SPARC_HM10: ((Symbol + Addend) >> 32) & 0x3ff
793 static inline void
794 hm10(unsigned char* view,
795 const Sized_relobj<size, big_endian>* object,
796 const Symbol_value<size>* psymval,
797 typename elfcpp::Elf_types<size>::Elf_Addr addend)
799 This_insn::template rela<32>(view, 32, 0x000003ff, object, psymval, addend);
802 // R_SPARC_PC_HM10: ((Symbol + Addend - Address) >> 32) & 0x3ff
803 static inline void
804 pc_hm10(unsigned char* view,
805 const Sized_relobj<size, big_endian>* object,
806 const Symbol_value<size>* psymval,
807 typename elfcpp::Elf_types<size>::Elf_Addr addend,
808 typename elfcpp::Elf_types<size>::Elf_Addr address)
810 This_insn::template pcrela<32>(view, 32, 0x000003ff, object,
811 psymval, addend, address);
814 // R_SPARC_11: (Symbol + Addend)
815 static inline void
816 rela32_11(unsigned char* view,
817 const Sized_relobj<size, big_endian>* object,
818 const Symbol_value<size>* psymval,
819 typename elfcpp::Elf_types<size>::Elf_Addr addend)
821 This_insn::template rela<32>(view, 0, 0x000007ff, object, psymval, addend);
824 // R_SPARC_10: (Symbol + Addend)
825 static inline void
826 rela32_10(unsigned char* view,
827 const Sized_relobj<size, big_endian>* object,
828 const Symbol_value<size>* psymval,
829 typename elfcpp::Elf_types<size>::Elf_Addr addend)
831 This_insn::template rela<32>(view, 0, 0x000003ff, object, psymval, addend);
834 // R_SPARC_7: (Symbol + Addend)
835 static inline void
836 rela32_7(unsigned char* view,
837 const Sized_relobj<size, big_endian>* object,
838 const Symbol_value<size>* psymval,
839 typename elfcpp::Elf_types<size>::Elf_Addr addend)
841 This_insn::template rela<32>(view, 0, 0x0000007f, object, psymval, addend);
844 // R_SPARC_6: (Symbol + Addend)
845 static inline void
846 rela32_6(unsigned char* view,
847 const Sized_relobj<size, big_endian>* object,
848 const Symbol_value<size>* psymval,
849 typename elfcpp::Elf_types<size>::Elf_Addr addend)
851 This_insn::template rela<32>(view, 0, 0x0000003f, object, psymval, addend);
854 // R_SPARC_5: (Symbol + Addend)
855 static inline void
856 rela32_5(unsigned char* view,
857 const Sized_relobj<size, big_endian>* object,
858 const Symbol_value<size>* psymval,
859 typename elfcpp::Elf_types<size>::Elf_Addr addend)
861 This_insn::template rela<32>(view, 0, 0x0000001f, object, psymval, addend);
864 // R_SPARC_TLS_LDO_HIX22: @dtpoff(Symbol + Addend) >> 10
865 static inline void
866 ldo_hix22(unsigned char* view,
867 typename elfcpp::Elf_types<size>::Elf_Addr value,
868 typename elfcpp::Elf_types<size>::Elf_Addr addend)
870 This_insn::hi22(view, value, addend);
873 // R_SPARC_TLS_LDO_LOX10: @dtpoff(Symbol + Addend) & 0x3ff
874 static inline void
875 ldo_lox10(unsigned char* view,
876 typename elfcpp::Elf_types<size>::Elf_Addr value,
877 typename elfcpp::Elf_types<size>::Elf_Addr addend)
879 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
880 Valtype* wv = reinterpret_cast<Valtype*>(view);
881 Valtype val = elfcpp::Swap<32, true>::readval(wv);
882 Valtype reloc = (value + addend);
884 val &= ~0x1fff;
885 reloc &= 0x3ff;
887 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
890 // R_SPARC_TLS_LE_HIX22: (@tpoff(Symbol + Addend) ^ 0xffffffffffffffff) >> 10
891 static inline void
892 hix22(unsigned char* view,
893 typename elfcpp::Elf_types<size>::Elf_Addr value,
894 typename elfcpp::Elf_types<size>::Elf_Addr addend)
896 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
897 Valtype* wv = reinterpret_cast<Valtype*>(view);
898 Valtype val = elfcpp::Swap<32, true>::readval(wv);
899 Valtype reloc = (value + addend);
901 val &= ~0x3fffff;
903 reloc ^= ~(Valtype)0;
904 reloc >>= 10;
906 reloc &= 0x3fffff;
908 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
911 // R_SPARC_HIX22: ((Symbol + Addend) ^ 0xffffffffffffffff) >> 10
912 static inline void
913 hix22(unsigned char* view,
914 const Sized_relobj<size, big_endian>* object,
915 const Symbol_value<size>* psymval,
916 typename elfcpp::Elf_types<size>::Elf_Addr addend)
918 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
919 Valtype* wv = reinterpret_cast<Valtype*>(view);
920 Valtype val = elfcpp::Swap<32, true>::readval(wv);
921 Valtype reloc = psymval->value(object, addend);
923 val &= ~0x3fffff;
925 reloc ^= ~(Valtype)0;
926 reloc >>= 10;
928 reloc &= 0x3fffff;
930 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
934 // R_SPARC_TLS_LE_LOX10: (@tpoff(Symbol + Addend) & 0x3ff) | 0x1c00
935 static inline void
936 lox10(unsigned char* view,
937 typename elfcpp::Elf_types<size>::Elf_Addr value,
938 typename elfcpp::Elf_types<size>::Elf_Addr addend)
940 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
941 Valtype* wv = reinterpret_cast<Valtype*>(view);
942 Valtype val = elfcpp::Swap<32, true>::readval(wv);
943 Valtype reloc = (value + addend);
945 val &= ~0x1fff;
946 reloc &= 0x3ff;
947 reloc |= 0x1c00;
949 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
952 // R_SPARC_LOX10: ((Symbol + Addend) & 0x3ff) | 0x1c00
953 static inline void
954 lox10(unsigned char* view,
955 const Sized_relobj<size, big_endian>* object,
956 const Symbol_value<size>* psymval,
957 typename elfcpp::Elf_types<size>::Elf_Addr addend)
959 typedef typename elfcpp::Swap<32, true>::Valtype Valtype;
960 Valtype* wv = reinterpret_cast<Valtype*>(view);
961 Valtype val = elfcpp::Swap<32, true>::readval(wv);
962 Valtype reloc = psymval->value(object, addend);
964 val &= ~0x1fff;
965 reloc &= 0x3ff;
966 reloc |= 0x1c00;
968 elfcpp::Swap<32, true>::writeval(wv, val | reloc);
972 // Get the GOT section, creating it if necessary.
974 template<int size, bool big_endian>
975 Output_data_got<size, big_endian>*
976 Target_sparc<size, big_endian>::got_section(Symbol_table* symtab,
977 Layout* layout)
979 if (this->got_ == NULL)
981 gold_assert(symtab != NULL && layout != NULL);
983 this->got_ = new Output_data_got<size, big_endian>();
985 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
986 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
987 this->got_);
989 // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
990 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
991 this->got_,
992 0, 0, elfcpp::STT_OBJECT,
993 elfcpp::STB_LOCAL,
994 elfcpp::STV_HIDDEN, 0,
995 false, false);
998 return this->got_;
1001 // Get the dynamic reloc section, creating it if necessary.
1003 template<int size, bool big_endian>
1004 typename Target_sparc<size, big_endian>::Reloc_section*
1005 Target_sparc<size, big_endian>::rela_dyn_section(Layout* layout)
1007 if (this->rela_dyn_ == NULL)
1009 gold_assert(layout != NULL);
1010 this->rela_dyn_ = new Reloc_section();
1011 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
1012 elfcpp::SHF_ALLOC, this->rela_dyn_);
1014 return this->rela_dyn_;
1017 // A class to handle the PLT data.
1019 template<int size, bool big_endian>
1020 class Output_data_plt_sparc : public Output_section_data
1022 public:
1023 typedef Output_data_reloc<elfcpp::SHT_RELA, true,
1024 size, big_endian> Reloc_section;
1026 Output_data_plt_sparc(Layout*);
1028 // Add an entry to the PLT.
1029 void add_entry(Symbol* gsym);
1031 // Return the .rela.plt section data.
1032 const Reloc_section* rel_plt() const
1034 return this->rel_;
1037 protected:
1038 void do_adjust_output_section(Output_section* os);
1040 private:
1041 // The size of an entry in the PLT.
1042 static const int base_plt_entry_size = (size == 32 ? 12 : 32);
1044 static const unsigned int plt_entries_per_block = 160;
1045 static const unsigned int plt_insn_chunk_size = 24;
1046 static const unsigned int plt_pointer_chunk_size = 8;
1047 static const unsigned int plt_block_size =
1048 (plt_entries_per_block
1049 * (plt_insn_chunk_size + plt_pointer_chunk_size));
1051 // Set the final size.
1052 void
1053 set_final_data_size()
1055 unsigned int full_count = this->count_ + 4;
1056 unsigned int extra = (size == 32 ? 4 : 0);
1058 if (size == 32 || full_count < 32768)
1059 this->set_data_size((full_count * base_plt_entry_size) + extra);
1060 else
1062 unsigned int ext_cnt = full_count - 32768;
1064 this->set_data_size((32768 * base_plt_entry_size)
1065 + (ext_cnt
1066 * (plt_insn_chunk_size
1067 + plt_pointer_chunk_size)));
1071 // Write out the PLT data.
1072 void
1073 do_write(Output_file*);
1075 // The reloc section.
1076 Reloc_section* rel_;
1077 // The number of PLT entries.
1078 unsigned int count_;
1081 // Create the PLT section. The ordinary .got section is an argument,
1082 // since we need to refer to the start.
1084 template<int size, bool big_endian>
1085 Output_data_plt_sparc<size, big_endian>::Output_data_plt_sparc(Layout* layout)
1086 : Output_section_data(size == 32 ? 4 : 8), count_(0)
1088 this->rel_ = new Reloc_section();
1089 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
1090 elfcpp::SHF_ALLOC, this->rel_);
1093 template<int size, bool big_endian>
1094 void
1095 Output_data_plt_sparc<size, big_endian>::do_adjust_output_section(Output_section* os)
1097 os->set_entsize(0);
1100 // Add an entry to the PLT.
1102 template<int size, bool big_endian>
1103 void
1104 Output_data_plt_sparc<size, big_endian>::add_entry(Symbol* gsym)
1106 gold_assert(!gsym->has_plt_offset());
1108 unsigned int index = this->count_ + 4;
1109 section_offset_type plt_offset;
1111 if (size == 32 || index < 32768)
1112 plt_offset = index * base_plt_entry_size;
1113 else
1115 unsigned int ext_index = index - 32768;
1117 plt_offset = (32768 * base_plt_entry_size)
1118 + ((ext_index / plt_entries_per_block)
1119 * plt_block_size)
1120 + ((ext_index % plt_entries_per_block)
1121 * plt_insn_chunk_size);
1124 gsym->set_plt_offset(plt_offset);
1126 ++this->count_;
1128 // Every PLT entry needs a reloc.
1129 gsym->set_needs_dynsym_entry();
1130 this->rel_->add_global(gsym, elfcpp::R_SPARC_JMP_SLOT, this,
1131 plt_offset, 0);
1133 // Note that we don't need to save the symbol. The contents of the
1134 // PLT are independent of which symbols are used. The symbols only
1135 // appear in the relocations.
1138 static const unsigned int sparc_nop = 0x01000000;
1139 static const unsigned int sparc_sethi_g1 = 0x03000000;
1140 static const unsigned int sparc_branch_always = 0x30800000;
1141 static const unsigned int sparc_branch_always_pt = 0x30680000;
1142 static const unsigned int sparc_mov = 0x80100000;
1143 static const unsigned int sparc_mov_g0_o0 = 0x90100000;
1144 static const unsigned int sparc_mov_o7_g5 = 0x8a10000f;
1145 static const unsigned int sparc_call_plus_8 = 0x40000002;
1146 static const unsigned int sparc_ldx_o7_imm_g1 = 0xc25be000;
1147 static const unsigned int sparc_jmpl_o7_g1_g1 = 0x83c3c001;
1148 static const unsigned int sparc_mov_g5_o7 = 0x9e100005;
1150 // Write out the PLT.
1152 template<int size, bool big_endian>
1153 void
1154 Output_data_plt_sparc<size, big_endian>::do_write(Output_file* of)
1156 const off_t offset = this->offset();
1157 const section_size_type oview_size =
1158 convert_to_section_size_type(this->data_size());
1159 unsigned char* const oview = of->get_output_view(offset, oview_size);
1160 unsigned char* pov = oview;
1162 memset(pov, 0, base_plt_entry_size * 4);
1163 pov += base_plt_entry_size * 4;
1165 unsigned int plt_offset = base_plt_entry_size * 4;
1166 const unsigned int count = this->count_;
1168 if (size == 64)
1170 unsigned int limit;
1172 limit = (count > 32768 ? 32768 : count);
1174 for (unsigned int i = 0; i < limit; ++i)
1176 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1177 sparc_sethi_g1 + plt_offset);
1178 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1179 sparc_branch_always_pt +
1180 (((base_plt_entry_size -
1181 (plt_offset + 4)) >> 2) &
1182 0x7ffff));
1183 elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1184 elfcpp::Swap<32, true>::writeval(pov + 0x0c, sparc_nop);
1185 elfcpp::Swap<32, true>::writeval(pov + 0x10, sparc_nop);
1186 elfcpp::Swap<32, true>::writeval(pov + 0x14, sparc_nop);
1187 elfcpp::Swap<32, true>::writeval(pov + 0x18, sparc_nop);
1188 elfcpp::Swap<32, true>::writeval(pov + 0x1c, sparc_nop);
1190 pov += base_plt_entry_size;
1191 plt_offset += base_plt_entry_size;
1194 if (count > 32768)
1196 unsigned int ext_cnt = count - 32768;
1197 unsigned int blks = ext_cnt / plt_entries_per_block;
1199 for (unsigned int i = 0; i < blks; ++i)
1201 unsigned int data_off = (plt_entries_per_block
1202 * plt_insn_chunk_size) - 4;
1204 for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1206 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1207 sparc_mov_o7_g5);
1208 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1209 sparc_call_plus_8);
1210 elfcpp::Swap<32, true>::writeval(pov + 0x08,
1211 sparc_nop);
1212 elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1213 sparc_ldx_o7_imm_g1 +
1214 (data_off & 0x1fff));
1215 elfcpp::Swap<32, true>::writeval(pov + 0x10,
1216 sparc_jmpl_o7_g1_g1);
1217 elfcpp::Swap<32, true>::writeval(pov + 0x14,
1218 sparc_mov_g5_o7);
1220 elfcpp::Swap<64, big_endian>::writeval(
1221 pov + 0x4 + data_off,
1222 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1224 pov += plt_insn_chunk_size;
1225 data_off -= 16;
1229 unsigned int sub_blk_cnt = ext_cnt % plt_entries_per_block;
1230 for (unsigned int i = 0; i < sub_blk_cnt; ++i)
1232 unsigned int data_off = (sub_blk_cnt
1233 * plt_insn_chunk_size) - 4;
1235 for (unsigned int j = 0; j < plt_entries_per_block; ++j)
1237 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1238 sparc_mov_o7_g5);
1239 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1240 sparc_call_plus_8);
1241 elfcpp::Swap<32, true>::writeval(pov + 0x08,
1242 sparc_nop);
1243 elfcpp::Swap<32, true>::writeval(pov + 0x0c,
1244 sparc_ldx_o7_imm_g1 +
1245 (data_off & 0x1fff));
1246 elfcpp::Swap<32, true>::writeval(pov + 0x10,
1247 sparc_jmpl_o7_g1_g1);
1248 elfcpp::Swap<32, true>::writeval(pov + 0x14,
1249 sparc_mov_g5_o7);
1251 elfcpp::Swap<64, big_endian>::writeval(
1252 pov + 0x4 + data_off,
1253 (elfcpp::Elf_Xword) (oview - (pov + 0x04)));
1255 pov += plt_insn_chunk_size;
1256 data_off -= 16;
1261 else
1263 for (unsigned int i = 0; i < count; ++i)
1265 elfcpp::Swap<32, true>::writeval(pov + 0x00,
1266 sparc_sethi_g1 + plt_offset);
1267 elfcpp::Swap<32, true>::writeval(pov + 0x04,
1268 sparc_branch_always +
1269 (((- (plt_offset + 4)) >> 2) &
1270 0x003fffff));
1271 elfcpp::Swap<32, true>::writeval(pov + 0x08, sparc_nop);
1273 pov += base_plt_entry_size;
1274 plt_offset += base_plt_entry_size;
1277 elfcpp::Swap<32, true>::writeval(pov, sparc_nop);
1278 pov += 4;
1281 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1283 of->write_output_view(offset, oview_size, oview);
1286 // Create a PLT entry for a global symbol.
1288 template<int size, bool big_endian>
1289 void
1290 Target_sparc<size, big_endian>::make_plt_entry(Symbol_table* symtab,
1291 Layout* layout,
1292 Symbol* gsym)
1294 if (gsym->has_plt_offset())
1295 return;
1297 if (this->plt_ == NULL)
1299 // Create the GOT sections first.
1300 this->got_section(symtab, layout);
1302 this->plt_ = new Output_data_plt_sparc<size, big_endian>(layout);
1303 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1304 (elfcpp::SHF_ALLOC
1305 | elfcpp::SHF_EXECINSTR
1306 | elfcpp::SHF_WRITE),
1307 this->plt_);
1309 // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
1310 symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
1311 this->plt_,
1312 0, 0, elfcpp::STT_OBJECT,
1313 elfcpp::STB_LOCAL,
1314 elfcpp::STV_HIDDEN, 0,
1315 false, false);
1318 this->plt_->add_entry(gsym);
1321 // Create a GOT entry for the TLS module index.
1323 template<int size, bool big_endian>
1324 unsigned int
1325 Target_sparc<size, big_endian>::got_mod_index_entry(Symbol_table* symtab,
1326 Layout* layout,
1327 Sized_relobj<size, big_endian>* object)
1329 if (this->got_mod_index_offset_ == -1U)
1331 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1332 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1333 Output_data_got<size, big_endian>* got;
1334 unsigned int got_offset;
1336 got = this->got_section(symtab, layout);
1337 got_offset = got->add_constant(0);
1338 rela_dyn->add_local(object, 0,
1339 (size == 64 ?
1340 elfcpp::R_SPARC_TLS_DTPMOD64 :
1341 elfcpp::R_SPARC_TLS_DTPMOD32), got,
1342 got_offset, 0);
1343 got->add_constant(0);
1344 this->got_mod_index_offset_ = got_offset;
1346 return this->got_mod_index_offset_;
1349 // Handle a relocation against a non-function symbol defined in a
1350 // dynamic object. The traditional way to handle this is to generate
1351 // a COPY relocation to copy the variable at runtime from the shared
1352 // object into the executable's data segment. However, this is
1353 // undesirable in general, as if the size of the object changes in the
1354 // dynamic object, the executable will no longer work correctly. If
1355 // this relocation is in a writable section, then we can create a
1356 // dynamic reloc and the dynamic linker will resolve it to the correct
1357 // address at runtime. However, we do not want do that if the
1358 // relocation is in a read-only section, as it would prevent the
1359 // readonly segment from being shared. And if we have to eventually
1360 // generate a COPY reloc, then any dynamic relocations will be
1361 // useless. So this means that if this is a writable section, we need
1362 // to save the relocation until we see whether we have to create a
1363 // COPY relocation for this symbol for any other relocation.
1365 template<int size, bool big_endian>
1366 void
1367 Target_sparc<size, big_endian>::copy_reloc(const General_options* options,
1368 Symbol_table* symtab,
1369 Layout* layout,
1370 Sized_relobj<size, big_endian>* object,
1371 unsigned int data_shndx,
1372 Output_section* output_section,
1373 Symbol* gsym,
1374 const elfcpp::Rela<size, big_endian>& rel)
1376 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(gsym);
1378 if (!Copy_relocs<size, big_endian>::need_copy_reloc(options, object,
1379 data_shndx, ssym))
1381 // So far we do not need a COPY reloc. Save this relocation.
1382 // If it turns out that we never need a COPY reloc for this
1383 // symbol, then we will emit the relocation.
1384 if (this->copy_relocs_ == NULL)
1385 this->copy_relocs_ = new Copy_relocs<size, big_endian>();
1386 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rel);
1388 else
1390 // Allocate space for this symbol in the .bss section.
1392 typename elfcpp::Elf_types<size>::Elf_WXword symsize = ssym->symsize();
1394 // There is no defined way to determine the required alignment
1395 // of the symbol. We pick the alignment based on the size. We
1396 // set an arbitrary maximum of 256.
1397 unsigned int align;
1398 // XXX remove this when bss alignment issue is fixed...
1399 for (align = (size == 32 ? 4 : 8); align < 512; align <<= 1)
1400 if ((symsize & align) != 0)
1401 break;
1403 if (this->dynbss_ == NULL)
1405 this->dynbss_ = new Output_data_space(align);
1406 layout->add_output_section_data(".bss",
1407 elfcpp::SHT_NOBITS,
1408 (elfcpp::SHF_ALLOC
1409 | elfcpp::SHF_WRITE),
1410 this->dynbss_);
1413 Output_data_space* dynbss = this->dynbss_;
1415 if (align > dynbss->addralign())
1416 dynbss->set_space_alignment(align);
1418 section_size_type dynbss_size =
1419 convert_to_section_size_type(dynbss->current_data_size());
1420 dynbss_size = align_address(dynbss_size, align);
1421 section_size_type offset = dynbss_size;
1422 dynbss->set_current_data_size(dynbss_size + symsize);
1424 symtab->define_with_copy_reloc(ssym, dynbss, offset);
1426 // Add the COPY reloc.
1427 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1428 rela_dyn->add_global(ssym, elfcpp::R_SPARC_COPY, dynbss, offset, 0);
1432 // Optimize the TLS relocation type based on what we know about the
1433 // symbol. IS_FINAL is true if the final address of this symbol is
1434 // known at link time.
1436 static tls::Tls_optimization
1437 optimize_tls_reloc(bool is_final, int r_type)
1439 // If we are generating a shared library, then we can't do anything
1440 // in the linker.
1441 if (parameters->options().shared())
1442 return tls::TLSOPT_NONE;
1444 switch (r_type)
1446 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1447 case elfcpp::R_SPARC_TLS_GD_LO10:
1448 case elfcpp::R_SPARC_TLS_GD_ADD:
1449 case elfcpp::R_SPARC_TLS_GD_CALL:
1450 // These are General-Dynamic which permits fully general TLS
1451 // access. Since we know that we are generating an executable,
1452 // we can convert this to Initial-Exec. If we also know that
1453 // this is a local symbol, we can further switch to Local-Exec.
1454 if (is_final)
1455 return tls::TLSOPT_TO_LE;
1456 return tls::TLSOPT_TO_IE;
1458 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
1459 case elfcpp::R_SPARC_TLS_LDM_LO10:
1460 case elfcpp::R_SPARC_TLS_LDM_ADD:
1461 case elfcpp::R_SPARC_TLS_LDM_CALL:
1462 // This is Local-Dynamic, which refers to a local symbol in the
1463 // dynamic TLS block. Since we know that we generating an
1464 // executable, we can switch to Local-Exec.
1465 return tls::TLSOPT_TO_LE;
1467 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1468 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1469 case elfcpp::R_SPARC_TLS_LDO_ADD:
1470 // Another type of Local-Dynamic relocation.
1471 return tls::TLSOPT_TO_LE;
1473 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1474 case elfcpp::R_SPARC_TLS_IE_LO10:
1475 case elfcpp::R_SPARC_TLS_IE_LD:
1476 case elfcpp::R_SPARC_TLS_IE_LDX:
1477 // These are Initial-Exec relocs which get the thread offset
1478 // from the GOT. If we know that we are linking against the
1479 // local symbol, we can switch to Local-Exec, which links the
1480 // thread offset into the instruction.
1481 if (is_final)
1482 return tls::TLSOPT_TO_LE;
1483 return tls::TLSOPT_NONE;
1485 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1486 case elfcpp::R_SPARC_TLS_LE_LOX10:
1487 // When we already have Local-Exec, there is nothing further we
1488 // can do.
1489 return tls::TLSOPT_NONE;
1491 default:
1492 gold_unreachable();
1496 // Generate a PLT entry slot for a call to __tls_get_addr
1497 template<int size, bool big_endian>
1498 void
1499 Target_sparc<size, big_endian>::Scan::generate_tls_call(Symbol_table* symtab,
1500 Layout* layout,
1501 Target_sparc<size, big_endian>* target)
1503 Symbol* gsym = target->tls_get_addr_sym(symtab);
1505 target->make_plt_entry(symtab, layout, gsym);
1508 // Report an unsupported relocation against a local symbol.
1510 template<int size, bool big_endian>
1511 void
1512 Target_sparc<size, big_endian>::Scan::unsupported_reloc_local(
1513 Sized_relobj<size, big_endian>* object,
1514 unsigned int r_type)
1516 gold_error(_("%s: unsupported reloc %u against local symbol"),
1517 object->name().c_str(), r_type);
1520 // Scan a relocation for a local symbol.
1522 template<int size, bool big_endian>
1523 inline void
1524 Target_sparc<size, big_endian>::Scan::local(
1525 const General_options&,
1526 Symbol_table* symtab,
1527 Layout* layout,
1528 Target_sparc<size, big_endian>* target,
1529 Sized_relobj<size, big_endian>* object,
1530 unsigned int data_shndx,
1531 Output_section* output_section,
1532 const elfcpp::Rela<size, big_endian>& reloc,
1533 unsigned int r_type,
1534 const elfcpp::Sym<size, big_endian>& lsym)
1536 unsigned int orig_r_type = r_type;
1538 r_type &= 0xff;
1539 switch (r_type)
1541 case elfcpp::R_SPARC_NONE:
1542 case elfcpp::R_SPARC_REGISTER:
1543 case elfcpp::R_SPARC_GNU_VTINHERIT:
1544 case elfcpp::R_SPARC_GNU_VTENTRY:
1545 break;
1547 case elfcpp::R_SPARC_64:
1548 case elfcpp::R_SPARC_32:
1549 // If building a shared library (or a position-independent
1550 // executable), we need to create a dynamic relocation for
1551 // this location. The relocation applied at link time will
1552 // apply the link-time value, so we flag the location with
1553 // an R_SPARC_RELATIVE relocation so the dynamic loader can
1554 // relocate it easily.
1555 if (parameters->options().output_is_position_independent())
1557 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1558 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1559 rela_dyn->add_local_relative(object, r_sym, elfcpp::R_SPARC_RELATIVE,
1560 output_section, data_shndx,
1561 reloc.get_r_offset(),
1562 reloc.get_r_addend());
1564 break;
1566 case elfcpp::R_SPARC_HIX22:
1567 case elfcpp::R_SPARC_LOX10:
1568 case elfcpp::R_SPARC_H44:
1569 case elfcpp::R_SPARC_M44:
1570 case elfcpp::R_SPARC_L44:
1571 case elfcpp::R_SPARC_HH22:
1572 case elfcpp::R_SPARC_HM10:
1573 case elfcpp::R_SPARC_LM22:
1574 case elfcpp::R_SPARC_UA64:
1575 case elfcpp::R_SPARC_UA32:
1576 case elfcpp::R_SPARC_UA16:
1577 case elfcpp::R_SPARC_HI22:
1578 case elfcpp::R_SPARC_LO10:
1579 case elfcpp::R_SPARC_OLO10:
1580 case elfcpp::R_SPARC_16:
1581 case elfcpp::R_SPARC_11:
1582 case elfcpp::R_SPARC_10:
1583 case elfcpp::R_SPARC_8:
1584 case elfcpp::R_SPARC_7:
1585 case elfcpp::R_SPARC_6:
1586 case elfcpp::R_SPARC_5:
1587 // If building a shared library (or a position-independent
1588 // executable), we need to create a dynamic relocation for
1589 // this location.
1590 if (parameters->options().output_is_position_independent())
1592 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1593 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1595 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1596 rela_dyn->add_local(object, r_sym, orig_r_type, output_section,
1597 data_shndx, reloc.get_r_offset(),
1598 reloc.get_r_addend());
1600 else
1602 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1603 gold_assert(lsym.get_st_value() == 0);
1604 rela_dyn->add_local_relative(object, r_sym, orig_r_type,
1605 output_section, data_shndx,
1606 reloc.get_r_offset(),
1607 reloc.get_r_addend());
1610 break;
1612 case elfcpp::R_SPARC_WDISP30:
1613 case elfcpp::R_SPARC_WDISP22:
1614 case elfcpp::R_SPARC_WDISP19:
1615 case elfcpp::R_SPARC_WDISP16:
1616 case elfcpp::R_SPARC_DISP8:
1617 case elfcpp::R_SPARC_DISP16:
1618 case elfcpp::R_SPARC_DISP32:
1619 case elfcpp::R_SPARC_DISP64:
1620 case elfcpp::R_SPARC_PC10:
1621 case elfcpp::R_SPARC_PC22:
1622 break;
1624 case elfcpp::R_SPARC_GOT10:
1625 case elfcpp::R_SPARC_GOT13:
1626 case elfcpp::R_SPARC_GOT22:
1628 // The symbol requires a GOT entry.
1629 Output_data_got<size, big_endian>* got;
1630 unsigned int r_sym;
1632 got = target->got_section(symtab, layout);
1633 r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1635 // If we are generating a shared object, we need to add a
1636 // dynamic relocation for this symbol's GOT entry.
1637 if (parameters->options().output_is_position_independent())
1639 if (!object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD))
1641 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1642 unsigned int off;
1644 off = got->add_constant(0);
1645 object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
1646 rela_dyn->add_local_relative(object, r_sym,
1647 elfcpp::R_SPARC_RELATIVE,
1648 got, off, 0);
1651 else
1652 got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1654 break;
1656 // These are initial TLS relocs, which are expected when
1657 // linking.
1658 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1659 case elfcpp::R_SPARC_TLS_GD_LO10:
1660 case elfcpp::R_SPARC_TLS_GD_ADD:
1661 case elfcpp::R_SPARC_TLS_GD_CALL:
1662 case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
1663 case elfcpp::R_SPARC_TLS_LDM_LO10:
1664 case elfcpp::R_SPARC_TLS_LDM_ADD:
1665 case elfcpp::R_SPARC_TLS_LDM_CALL:
1666 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1667 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1668 case elfcpp::R_SPARC_TLS_LDO_ADD:
1669 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1670 case elfcpp::R_SPARC_TLS_IE_LO10:
1671 case elfcpp::R_SPARC_TLS_IE_LD:
1672 case elfcpp::R_SPARC_TLS_IE_LDX:
1673 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1674 case elfcpp::R_SPARC_TLS_LE_LOX10:
1676 bool output_is_shared = parameters->options().shared();
1677 const tls::Tls_optimization optimized_type
1678 = optimize_tls_reloc(!output_is_shared, r_type);
1679 switch (r_type)
1681 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
1682 case elfcpp::R_SPARC_TLS_GD_LO10:
1683 case elfcpp::R_SPARC_TLS_GD_ADD:
1684 case elfcpp::R_SPARC_TLS_GD_CALL:
1685 if (optimized_type == tls::TLSOPT_NONE)
1687 // Create a pair of GOT entries for the module index and
1688 // dtv-relative offset.
1689 Output_data_got<size, big_endian>* got
1690 = target->got_section(symtab, layout);
1691 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1692 got->add_local_pair_with_rela(object, r_sym,
1693 lsym.get_st_shndx(),
1694 GOT_TYPE_TLS_PAIR,
1695 target->rela_dyn_section(layout),
1696 (size == 64 ?
1697 elfcpp::R_SPARC_TLS_DTPMOD64 :
1698 elfcpp::R_SPARC_TLS_DTPMOD32), 0);
1699 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
1700 generate_tls_call(symtab, layout, target);
1702 else if (optimized_type != tls::TLSOPT_TO_LE)
1703 unsupported_reloc_local(object, r_type);
1704 break;
1706 case elfcpp::R_SPARC_TLS_LDM_HI22 : // Local-dynamic
1707 case elfcpp::R_SPARC_TLS_LDM_LO10:
1708 case elfcpp::R_SPARC_TLS_LDM_ADD:
1709 case elfcpp::R_SPARC_TLS_LDM_CALL:
1710 if (optimized_type == tls::TLSOPT_NONE)
1712 // Create a GOT entry for the module index.
1713 target->got_mod_index_entry(symtab, layout, object);
1715 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
1716 generate_tls_call(symtab, layout, target);
1718 else if (optimized_type != tls::TLSOPT_TO_LE)
1719 unsupported_reloc_local(object, r_type);
1720 break;
1722 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
1723 case elfcpp::R_SPARC_TLS_LDO_LOX10:
1724 case elfcpp::R_SPARC_TLS_LDO_ADD:
1725 break;
1727 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
1728 case elfcpp::R_SPARC_TLS_IE_LO10:
1729 case elfcpp::R_SPARC_TLS_IE_LD:
1730 case elfcpp::R_SPARC_TLS_IE_LDX:
1731 layout->set_has_static_tls();
1732 if (optimized_type == tls::TLSOPT_NONE)
1734 // Create a GOT entry for the tp-relative offset.
1735 Output_data_got<size, big_endian>* got
1736 = target->got_section(symtab, layout);
1737 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1739 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET))
1741 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1742 unsigned int off = got->add_constant(0);
1744 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET,
1745 off);
1746 rela_dyn->add_local_relative(object, r_sym,
1747 (size == 64 ?
1748 elfcpp::R_SPARC_TLS_TPOFF64 :
1749 elfcpp::R_SPARC_TLS_TPOFF32),
1750 got, off, 0);
1753 else if (optimized_type != tls::TLSOPT_TO_LE)
1754 unsupported_reloc_local(object, r_type);
1755 break;
1757 case elfcpp::R_SPARC_TLS_LE_HIX22: // Local-exec
1758 case elfcpp::R_SPARC_TLS_LE_LOX10:
1759 layout->set_has_static_tls();
1760 if (output_is_shared)
1762 // We need to create a dynamic relocation.
1763 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1764 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
1765 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1766 rela_dyn->add_local_relative(object, r_sym, r_type,
1767 output_section, data_shndx,
1768 reloc.get_r_offset(), 0);
1770 break;
1773 break;
1775 // These are relocations which should only be seen by the
1776 // dynamic linker, and should never be seen here.
1777 case elfcpp::R_SPARC_COPY:
1778 case elfcpp::R_SPARC_GLOB_DAT:
1779 case elfcpp::R_SPARC_JMP_SLOT:
1780 case elfcpp::R_SPARC_RELATIVE:
1781 case elfcpp::R_SPARC_TLS_DTPMOD64:
1782 case elfcpp::R_SPARC_TLS_DTPMOD32:
1783 case elfcpp::R_SPARC_TLS_DTPOFF64:
1784 case elfcpp::R_SPARC_TLS_DTPOFF32:
1785 case elfcpp::R_SPARC_TLS_TPOFF64:
1786 case elfcpp::R_SPARC_TLS_TPOFF32:
1787 gold_error(_("%s: unexpected reloc %u in object file"),
1788 object->name().c_str(), r_type);
1789 break;
1791 default:
1792 unsupported_reloc_local(object, r_type);
1793 break;
1797 // Report an unsupported relocation against a global symbol.
1799 template<int size, bool big_endian>
1800 void
1801 Target_sparc<size, big_endian>::Scan::unsupported_reloc_global(
1802 Sized_relobj<size, big_endian>* object,
1803 unsigned int r_type,
1804 Symbol* gsym)
1806 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1807 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1810 // Scan a relocation for a global symbol.
1812 template<int size, bool big_endian>
1813 inline void
1814 Target_sparc<size, big_endian>::Scan::global(
1815 const General_options& options,
1816 Symbol_table* symtab,
1817 Layout* layout,
1818 Target_sparc<size, big_endian>* target,
1819 Sized_relobj<size, big_endian>* object,
1820 unsigned int data_shndx,
1821 Output_section* output_section,
1822 const elfcpp::Rela<size, big_endian>& reloc,
1823 unsigned int r_type,
1824 Symbol* gsym)
1826 unsigned int orig_r_type = r_type;
1828 r_type &= 0xff;
1829 switch (r_type)
1831 case elfcpp::R_SPARC_NONE:
1832 case elfcpp::R_SPARC_REGISTER:
1833 case elfcpp::R_SPARC_GNU_VTINHERIT:
1834 case elfcpp::R_SPARC_GNU_VTENTRY:
1835 break;
1837 case elfcpp::R_SPARC_PLT64:
1838 case elfcpp::R_SPARC_PLT32:
1839 case elfcpp::R_SPARC_HIPLT22:
1840 case elfcpp::R_SPARC_LOPLT10:
1841 case elfcpp::R_SPARC_PCPLT32:
1842 case elfcpp::R_SPARC_PCPLT22:
1843 case elfcpp::R_SPARC_PCPLT10:
1844 case elfcpp::R_SPARC_WPLT30:
1845 // If the symbol is fully resolved, this is just a PC32 reloc.
1846 // Otherwise we need a PLT entry.
1847 if (gsym->final_value_is_known())
1848 break;
1849 // If building a shared library, we can also skip the PLT entry
1850 // if the symbol is defined in the output file and is protected
1851 // or hidden.
1852 if (gsym->is_defined()
1853 && !gsym->is_from_dynobj()
1854 && !gsym->is_preemptible())
1855 break;
1856 target->make_plt_entry(symtab, layout, gsym);
1857 break;
1859 case elfcpp::R_SPARC_DISP8:
1860 case elfcpp::R_SPARC_DISP16:
1861 case elfcpp::R_SPARC_DISP32:
1862 case elfcpp::R_SPARC_DISP64:
1863 case elfcpp::R_SPARC_PC_HH22:
1864 case elfcpp::R_SPARC_PC_HM10:
1865 case elfcpp::R_SPARC_PC_LM22:
1866 case elfcpp::R_SPARC_PC10:
1867 case elfcpp::R_SPARC_PC22:
1868 case elfcpp::R_SPARC_WDISP30:
1869 case elfcpp::R_SPARC_WDISP22:
1870 case elfcpp::R_SPARC_WDISP19:
1871 case elfcpp::R_SPARC_WDISP16:
1873 if (gsym->needs_plt_entry())
1874 target->make_plt_entry(symtab, layout, gsym);
1875 // Make a dynamic relocation if necessary.
1876 int flags = Symbol::NON_PIC_REF;
1877 if (gsym->type() == elfcpp::STT_FUNC)
1878 flags |= Symbol::FUNCTION_CALL;
1879 if (gsym->needs_dynamic_reloc(flags))
1881 if (target->may_need_copy_reloc(gsym))
1883 target->copy_reloc(&options, symtab, layout, object,
1884 data_shndx, output_section, gsym,
1885 reloc);
1887 else
1889 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1890 rela_dyn->add_global(gsym, orig_r_type, output_section, object,
1891 data_shndx, reloc.get_r_offset(),
1892 reloc.get_r_addend());
1896 break;
1898 case elfcpp::R_SPARC_UA64:
1899 case elfcpp::R_SPARC_64:
1900 case elfcpp::R_SPARC_HIX22:
1901 case elfcpp::R_SPARC_LOX10:
1902 case elfcpp::R_SPARC_H44:
1903 case elfcpp::R_SPARC_M44:
1904 case elfcpp::R_SPARC_L44:
1905 case elfcpp::R_SPARC_HH22:
1906 case elfcpp::R_SPARC_HM10:
1907 case elfcpp::R_SPARC_LM22:
1908 case elfcpp::R_SPARC_HI22:
1909 case elfcpp::R_SPARC_LO10:
1910 case elfcpp::R_SPARC_OLO10:
1911 case elfcpp::R_SPARC_UA32:
1912 case elfcpp::R_SPARC_32:
1913 case elfcpp::R_SPARC_UA16:
1914 case elfcpp::R_SPARC_16:
1915 case elfcpp::R_SPARC_11:
1916 case elfcpp::R_SPARC_10:
1917 case elfcpp::R_SPARC_8:
1918 case elfcpp::R_SPARC_7:
1919 case elfcpp::R_SPARC_6:
1920 case elfcpp::R_SPARC_5:
1922 // Make a PLT entry if necessary.
1923 if (gsym->needs_plt_entry())
1925 target->make_plt_entry(symtab, layout, gsym);
1926 // Since this is not a PC-relative relocation, we may be
1927 // taking the address of a function. In that case we need to
1928 // set the entry in the dynamic symbol table to the address of
1929 // the PLT entry.
1930 if (gsym->is_from_dynobj() && !parameters->options().shared())
1931 gsym->set_needs_dynsym_value();
1933 // Make a dynamic relocation if necessary.
1934 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1936 if (target->may_need_copy_reloc(gsym))
1938 target->copy_reloc(&options, symtab, layout, object,
1939 data_shndx, output_section, gsym, reloc);
1941 else if ((r_type == elfcpp::R_SPARC_32
1942 || r_type == elfcpp::R_SPARC_64)
1943 && gsym->can_use_relative_reloc(false))
1945 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1946 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
1947 output_section, object,
1948 data_shndx, reloc.get_r_offset(),
1949 reloc.get_r_addend());
1951 else
1953 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1955 if (gsym->is_from_dynobj()
1956 || gsym->is_undefined()
1957 || gsym->is_preemptible())
1958 rela_dyn->add_global(gsym, orig_r_type, output_section,
1959 object, data_shndx,
1960 reloc.get_r_offset(),
1961 reloc.get_r_addend());
1962 else
1963 rela_dyn->add_global_relative(gsym, orig_r_type,
1964 output_section, object,
1965 data_shndx,
1966 reloc.get_r_offset(),
1967 reloc.get_r_addend());
1971 break;
1973 case elfcpp::R_SPARC_GOT10:
1974 case elfcpp::R_SPARC_GOT13:
1975 case elfcpp::R_SPARC_GOT22:
1977 // The symbol requires a GOT entry.
1978 Output_data_got<size, big_endian>* got;
1980 got = target->got_section(symtab, layout);
1981 if (gsym->final_value_is_known())
1982 got->add_global(gsym, GOT_TYPE_STANDARD);
1983 else
1985 // If this symbol is not fully resolved, we need to add a
1986 // dynamic relocation for it.
1987 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1988 if (gsym->is_from_dynobj()
1989 || gsym->is_undefined()
1990 || gsym->is_preemptible())
1991 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1992 elfcpp::R_SPARC_GLOB_DAT);
1993 else if (!gsym->has_got_offset(GOT_TYPE_STANDARD))
1995 unsigned int off = got->add_constant(0);
1997 gsym->set_got_offset(GOT_TYPE_STANDARD, off);
1998 rela_dyn->add_global_relative(gsym, elfcpp::R_SPARC_RELATIVE,
1999 got, off, 0);
2003 break;
2005 // These are initial tls relocs, which are expected when
2006 // linking.
2007 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2008 case elfcpp::R_SPARC_TLS_GD_LO10:
2009 case elfcpp::R_SPARC_TLS_GD_ADD:
2010 case elfcpp::R_SPARC_TLS_GD_CALL:
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 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2016 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2017 case elfcpp::R_SPARC_TLS_LDO_ADD:
2018 case elfcpp::R_SPARC_TLS_LE_HIX22:
2019 case elfcpp::R_SPARC_TLS_LE_LOX10:
2020 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2021 case elfcpp::R_SPARC_TLS_IE_LO10:
2022 case elfcpp::R_SPARC_TLS_IE_LD:
2023 case elfcpp::R_SPARC_TLS_IE_LDX:
2025 const bool is_final = gsym->final_value_is_known();
2026 const tls::Tls_optimization optimized_type
2027 = optimize_tls_reloc(is_final, r_type);
2028 switch (r_type)
2030 case elfcpp::R_SPARC_TLS_GD_HI22: // Global-dynamic
2031 case elfcpp::R_SPARC_TLS_GD_LO10:
2032 case elfcpp::R_SPARC_TLS_GD_ADD:
2033 case elfcpp::R_SPARC_TLS_GD_CALL:
2034 if (optimized_type == tls::TLSOPT_NONE)
2036 // Create a pair of GOT entries for the module index and
2037 // dtv-relative offset.
2038 Output_data_got<size, big_endian>* got
2039 = target->got_section(symtab, layout);
2040 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2041 target->rela_dyn_section(layout),
2042 (size == 64 ?
2043 elfcpp::R_SPARC_TLS_DTPMOD64 :
2044 elfcpp::R_SPARC_TLS_DTPMOD32),
2045 (size == 64 ?
2046 elfcpp::R_SPARC_TLS_DTPOFF64 :
2047 elfcpp::R_SPARC_TLS_DTPOFF32));
2049 // Emit R_SPARC_WPLT30 against "__tls_get_addr"
2050 if (r_type == elfcpp::R_SPARC_TLS_GD_CALL)
2051 generate_tls_call(symtab, layout, target);
2053 else if (optimized_type == tls::TLSOPT_TO_IE)
2055 // Create a GOT entry for the tp-relative offset.
2056 Output_data_got<size, big_endian>* got
2057 = target->got_section(symtab, layout);
2058 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2059 target->rela_dyn_section(layout),
2060 (size == 64 ?
2061 elfcpp::R_SPARC_TLS_TPOFF64 :
2062 elfcpp::R_SPARC_TLS_TPOFF32));
2064 else if (optimized_type != tls::TLSOPT_TO_LE)
2065 unsupported_reloc_global(object, r_type, gsym);
2066 break;
2068 case elfcpp::R_SPARC_TLS_LDM_HI22: // Local-dynamic
2069 case elfcpp::R_SPARC_TLS_LDM_LO10:
2070 case elfcpp::R_SPARC_TLS_LDM_ADD:
2071 case elfcpp::R_SPARC_TLS_LDM_CALL:
2072 if (optimized_type == tls::TLSOPT_NONE)
2074 // Create a GOT entry for the module index.
2075 target->got_mod_index_entry(symtab, layout, object);
2077 if (r_type == elfcpp::R_SPARC_TLS_LDM_CALL)
2078 generate_tls_call(symtab, layout, target);
2080 else if (optimized_type != tls::TLSOPT_TO_LE)
2081 unsupported_reloc_global(object, r_type, gsym);
2082 break;
2084 case elfcpp::R_SPARC_TLS_LDO_HIX22: // Alternate local-dynamic
2085 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2086 case elfcpp::R_SPARC_TLS_LDO_ADD:
2087 break;
2089 case elfcpp::R_SPARC_TLS_LE_HIX22:
2090 case elfcpp::R_SPARC_TLS_LE_LOX10:
2091 layout->set_has_static_tls();
2092 if (parameters->options().shared())
2094 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2095 rela_dyn->add_global_relative(gsym, orig_r_type,
2096 output_section, object,
2097 data_shndx, reloc.get_r_offset(),
2100 break;
2102 case elfcpp::R_SPARC_TLS_IE_HI22: // Initial-exec
2103 case elfcpp::R_SPARC_TLS_IE_LO10:
2104 case elfcpp::R_SPARC_TLS_IE_LD:
2105 case elfcpp::R_SPARC_TLS_IE_LDX:
2106 layout->set_has_static_tls();
2107 if (optimized_type == tls::TLSOPT_NONE)
2109 // Create a GOT entry for the tp-relative offset.
2110 Output_data_got<size, big_endian>* got
2111 = target->got_section(symtab, layout);
2112 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2113 target->rela_dyn_section(layout),
2114 (size == 64 ?
2115 elfcpp::R_SPARC_TLS_TPOFF64 :
2116 elfcpp::R_SPARC_TLS_TPOFF32));
2118 else if (optimized_type != tls::TLSOPT_TO_LE)
2119 unsupported_reloc_global(object, r_type, gsym);
2120 break;
2123 break;
2125 // These are relocations which should only be seen by the
2126 // dynamic linker, and should never be seen here.
2127 case elfcpp::R_SPARC_COPY:
2128 case elfcpp::R_SPARC_GLOB_DAT:
2129 case elfcpp::R_SPARC_JMP_SLOT:
2130 case elfcpp::R_SPARC_RELATIVE:
2131 case elfcpp::R_SPARC_TLS_DTPMOD64:
2132 case elfcpp::R_SPARC_TLS_DTPMOD32:
2133 case elfcpp::R_SPARC_TLS_DTPOFF64:
2134 case elfcpp::R_SPARC_TLS_DTPOFF32:
2135 case elfcpp::R_SPARC_TLS_TPOFF64:
2136 case elfcpp::R_SPARC_TLS_TPOFF32:
2137 gold_error(_("%s: unexpected reloc %u in object file"),
2138 object->name().c_str(), r_type);
2139 break;
2141 default:
2142 unsupported_reloc_global(object, r_type, gsym);
2143 break;
2147 // Scan relocations for a section.
2149 template<int size, bool big_endian>
2150 void
2151 Target_sparc<size, big_endian>::scan_relocs(
2152 const General_options& options,
2153 Symbol_table* symtab,
2154 Layout* layout,
2155 Sized_relobj<size, big_endian>* object,
2156 unsigned int data_shndx,
2157 unsigned int sh_type,
2158 const unsigned char* prelocs,
2159 size_t reloc_count,
2160 Output_section* output_section,
2161 bool needs_special_offset_handling,
2162 size_t local_symbol_count,
2163 const unsigned char* plocal_symbols)
2165 typedef Target_sparc<size, big_endian> Sparc;
2166 typedef typename Target_sparc<size, big_endian>::Scan Scan;
2168 if (sh_type == elfcpp::SHT_REL)
2170 gold_error(_("%s: unsupported REL reloc section"),
2171 object->name().c_str());
2172 return;
2175 gold::scan_relocs<size, big_endian, Sparc, elfcpp::SHT_RELA, Scan>(
2176 options,
2177 symtab,
2178 layout,
2179 this,
2180 object,
2181 data_shndx,
2182 prelocs,
2183 reloc_count,
2184 output_section,
2185 needs_special_offset_handling,
2186 local_symbol_count,
2187 plocal_symbols);
2190 // Finalize the sections.
2192 template<int size, bool big_endian>
2193 void
2194 Target_sparc<size, big_endian>::do_finalize_sections(Layout* layout)
2196 // Fill in some more dynamic tags.
2197 Output_data_dynamic* const odyn = layout->dynamic_data();
2198 if (odyn != NULL)
2200 if (this->plt_ != NULL)
2202 const Output_data* od = this->plt_->rel_plt();
2203 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
2204 odyn->add_section_address(elfcpp::DT_JMPREL, od);
2205 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
2207 odyn->add_section_address(elfcpp::DT_PLTGOT, this->plt_);
2210 if (this->rela_dyn_ != NULL)
2212 const Output_data* od = this->rela_dyn_;
2213 odyn->add_section_address(elfcpp::DT_RELA, od);
2214 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2215 odyn->add_constant(elfcpp::DT_RELAENT,
2216 elfcpp::Elf_sizes<size>::rela_size);
2219 if (!parameters->options().shared())
2221 // The value of the DT_DEBUG tag is filled in by the dynamic
2222 // linker at run time, and used by the debugger.
2223 odyn->add_constant(elfcpp::DT_DEBUG, 0);
2227 // Emit any relocs we saved in an attempt to avoid generating COPY
2228 // relocs.
2229 if (this->copy_relocs_ == NULL)
2230 return;
2231 if (this->copy_relocs_->any_to_emit())
2233 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
2234 this->copy_relocs_->emit(rela_dyn);
2236 delete this->copy_relocs_;
2237 this->copy_relocs_ = NULL;
2240 // Perform a relocation.
2242 template<int size, bool big_endian>
2243 inline bool
2244 Target_sparc<size, big_endian>::Relocate::relocate(
2245 const Relocate_info<size, big_endian>* relinfo,
2246 Target_sparc* target,
2247 size_t relnum,
2248 const elfcpp::Rela<size, big_endian>& rela,
2249 unsigned int r_type,
2250 const Sized_symbol<size>* gsym,
2251 const Symbol_value<size>* psymval,
2252 unsigned char* view,
2253 typename elfcpp::Elf_types<size>::Elf_Addr address,
2254 section_size_type view_size)
2256 r_type &= 0xff;
2258 if (this->ignore_gd_add_)
2260 if (r_type != elfcpp::R_SPARC_TLS_GD_ADD)
2261 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2262 _("missing expected TLS relocation"));
2263 else
2265 this->ignore_gd_add_ = false;
2266 return false;
2270 typedef Sparc_relocate_functions<size, big_endian> Reloc;
2272 // Pick the value to use for symbols defined in shared objects.
2273 Symbol_value<size> symval;
2274 if (gsym != NULL
2275 && (gsym->is_from_dynobj()
2276 || (parameters->options().shared()
2277 && (gsym->is_undefined() || gsym->is_preemptible())))
2278 && gsym->has_plt_offset())
2280 elfcpp::Elf_Xword value;
2282 value = target->plt_section()->address() + gsym->plt_offset();
2284 symval.set_output_value(value);
2286 psymval = &symval;
2289 const Sized_relobj<size, big_endian>* object = relinfo->object;
2290 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2292 // Get the GOT offset if needed. Unlike i386 and x86_64, our GOT
2293 // pointer points to the beginning, not the end, of the table.
2294 // So we just use the plain offset.
2295 bool have_got_offset = false;
2296 unsigned int got_offset = 0;
2297 switch (r_type)
2299 case elfcpp::R_SPARC_GOT10:
2300 case elfcpp::R_SPARC_GOT13:
2301 case elfcpp::R_SPARC_GOT22:
2302 if (gsym != NULL)
2304 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2305 got_offset = gsym->got_offset(GOT_TYPE_STANDARD);
2307 else
2309 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2310 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2311 got_offset = object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
2313 have_got_offset = true;
2314 break;
2316 default:
2317 break;
2320 switch (r_type)
2322 case elfcpp::R_SPARC_NONE:
2323 case elfcpp::R_SPARC_REGISTER:
2324 case elfcpp::R_SPARC_GNU_VTINHERIT:
2325 case elfcpp::R_SPARC_GNU_VTENTRY:
2326 break;
2328 case elfcpp::R_SPARC_8:
2329 Relocate_functions<size, big_endian>::rela8(view, object,
2330 psymval, addend);
2331 break;
2333 case elfcpp::R_SPARC_16:
2334 Relocate_functions<size, big_endian>::rela16(view, object,
2335 psymval, addend);
2336 break;
2338 case elfcpp::R_SPARC_32:
2339 if (!parameters->options().output_is_position_independent())
2340 Relocate_functions<size, big_endian>::rela32(view, object,
2341 psymval, addend);
2342 break;
2344 case elfcpp::R_SPARC_DISP8:
2345 Reloc::disp8(view, object, psymval, addend, address);
2346 break;
2348 case elfcpp::R_SPARC_DISP16:
2349 Reloc::disp16(view, object, psymval, addend, address);
2350 break;
2352 case elfcpp::R_SPARC_DISP32:
2353 Reloc::disp32(view, object, psymval, addend, address);
2354 break;
2356 case elfcpp::R_SPARC_DISP64:
2357 Reloc::disp64(view, object, psymval, addend, address);
2358 break;
2360 case elfcpp::R_SPARC_WDISP30:
2361 case elfcpp::R_SPARC_WPLT30:
2362 Reloc::wdisp30(view, object, psymval, addend, address);
2363 break;
2365 case elfcpp::R_SPARC_WDISP22:
2366 Reloc::wdisp22(view, object, psymval, addend, address);
2367 break;
2369 case elfcpp::R_SPARC_WDISP19:
2370 Reloc::wdisp19(view, object, psymval, addend, address);
2371 break;
2373 case elfcpp::R_SPARC_WDISP16:
2374 Reloc::wdisp16(view, object, psymval, addend, address);
2375 break;
2377 case elfcpp::R_SPARC_HI22:
2378 Reloc::hi22(view, object, psymval, addend);
2379 break;
2381 case elfcpp::R_SPARC_22:
2382 Reloc::rela32_22(view, object, psymval, addend);
2383 break;
2385 case elfcpp::R_SPARC_13:
2386 Reloc::rela32_13(view, object, psymval, addend);
2387 break;
2389 case elfcpp::R_SPARC_LO10:
2390 Reloc::lo10(view, object, psymval, addend);
2391 break;
2393 case elfcpp::R_SPARC_GOT10:
2394 Reloc::lo10(view, got_offset, addend);
2395 break;
2397 case elfcpp::R_SPARC_GOT13:
2398 Reloc::rela32_13(view, got_offset, addend);
2399 break;
2401 case elfcpp::R_SPARC_GOT22:
2402 Reloc::hi22(view, got_offset, addend);
2403 break;
2405 case elfcpp::R_SPARC_PC10:
2406 Reloc::pc10(view, object, psymval, addend, address);
2407 break;
2409 case elfcpp::R_SPARC_PC22:
2410 Reloc::pc22(view, object, psymval, addend, address);
2411 break;
2413 case elfcpp::R_SPARC_TLS_DTPOFF32:
2414 case elfcpp::R_SPARC_UA32:
2415 Reloc::ua32(view, object, psymval, addend);
2416 break;
2418 case elfcpp::R_SPARC_PLT64:
2419 Relocate_functions<size, big_endian>::rela64(view, object,
2420 psymval, addend);
2421 break;
2423 case elfcpp::R_SPARC_PLT32:
2424 Relocate_functions<size, big_endian>::rela32(view, object,
2425 psymval, addend);
2426 break;
2428 case elfcpp::R_SPARC_HIPLT22:
2429 Reloc::hi22(view, object, psymval, addend);
2430 break;
2432 case elfcpp::R_SPARC_LOPLT10:
2433 Reloc::lo10(view, object, psymval, addend);
2434 break;
2436 case elfcpp::R_SPARC_PCPLT32:
2437 Reloc::disp32(view, object, psymval, addend, address);
2438 break;
2440 case elfcpp::R_SPARC_PCPLT22:
2441 Reloc::pcplt22(view, object, psymval, addend, address);
2442 break;
2444 case elfcpp::R_SPARC_PCPLT10:
2445 Reloc::lo10(view, object, psymval, addend, address);
2446 break;
2448 case elfcpp::R_SPARC_64:
2449 if (!parameters->options().output_is_position_independent())
2450 Relocate_functions<size, big_endian>::rela64(view, object,
2451 psymval, addend);
2452 break;
2454 case elfcpp::R_SPARC_OLO10:
2456 unsigned int addend2 = rela.get_r_info() & 0xffffffff;
2457 addend2 = ((addend2 >> 8) ^ 0x800000) - 0x800000;
2458 Reloc::olo10(view, object, psymval, addend, addend2);
2460 break;
2462 case elfcpp::R_SPARC_HH22:
2463 Reloc::hh22(view, object, psymval, addend);
2464 break;
2466 case elfcpp::R_SPARC_PC_HH22:
2467 Reloc::pc_hh22(view, object, psymval, addend, address);
2468 break;
2470 case elfcpp::R_SPARC_HM10:
2471 Reloc::hm10(view, object, psymval, addend);
2472 break;
2474 case elfcpp::R_SPARC_PC_HM10:
2475 Reloc::pc_hm10(view, object, psymval, addend, address);
2476 break;
2478 case elfcpp::R_SPARC_LM22:
2479 Reloc::hi22(view, object, psymval, addend);
2480 break;
2482 case elfcpp::R_SPARC_PC_LM22:
2483 Reloc::pcplt22(view, object, psymval, addend, address);
2484 break;
2486 case elfcpp::R_SPARC_11:
2487 Reloc::rela32_11(view, object, psymval, addend);
2488 break;
2490 case elfcpp::R_SPARC_10:
2491 Reloc::rela32_10(view, object, psymval, addend);
2492 break;
2494 case elfcpp::R_SPARC_7:
2495 Reloc::rela32_7(view, object, psymval, addend);
2496 break;
2498 case elfcpp::R_SPARC_6:
2499 Reloc::rela32_6(view, object, psymval, addend);
2500 break;
2502 case elfcpp::R_SPARC_5:
2503 Reloc::rela32_5(view, object, psymval, addend);
2504 break;
2506 case elfcpp::R_SPARC_HIX22:
2507 Reloc::hix22(view, object, psymval, addend);
2508 break;
2510 case elfcpp::R_SPARC_LOX10:
2511 Reloc::lox10(view, object, psymval, addend);
2512 break;
2514 case elfcpp::R_SPARC_H44:
2515 Reloc::h44(view, object, psymval, addend);
2516 break;
2518 case elfcpp::R_SPARC_M44:
2519 Reloc::m44(view, object, psymval, addend);
2520 break;
2522 case elfcpp::R_SPARC_L44:
2523 Reloc::l44(view, object, psymval, addend);
2524 break;
2526 case elfcpp::R_SPARC_TLS_DTPOFF64:
2527 case elfcpp::R_SPARC_UA64:
2528 Reloc::ua64(view, object, psymval, addend);
2529 break;
2531 case elfcpp::R_SPARC_UA16:
2532 Reloc::ua16(view, object, psymval, addend);
2533 break;
2535 case elfcpp::R_SPARC_TLS_GD_HI22:
2536 case elfcpp::R_SPARC_TLS_GD_LO10:
2537 case elfcpp::R_SPARC_TLS_GD_ADD:
2538 case elfcpp::R_SPARC_TLS_GD_CALL:
2539 case elfcpp::R_SPARC_TLS_LDM_HI22:
2540 case elfcpp::R_SPARC_TLS_LDM_LO10:
2541 case elfcpp::R_SPARC_TLS_LDM_ADD:
2542 case elfcpp::R_SPARC_TLS_LDM_CALL:
2543 case elfcpp::R_SPARC_TLS_LDO_HIX22:
2544 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2545 case elfcpp::R_SPARC_TLS_LDO_ADD:
2546 case elfcpp::R_SPARC_TLS_IE_HI22:
2547 case elfcpp::R_SPARC_TLS_IE_LO10:
2548 case elfcpp::R_SPARC_TLS_IE_LD:
2549 case elfcpp::R_SPARC_TLS_IE_LDX:
2550 case elfcpp::R_SPARC_TLS_LE_HIX22:
2551 case elfcpp::R_SPARC_TLS_LE_LOX10:
2552 this->relocate_tls(relinfo, target, relnum, rela,
2553 r_type, gsym, psymval, view,
2554 address, view_size);
2555 break;
2557 case elfcpp::R_SPARC_COPY:
2558 case elfcpp::R_SPARC_GLOB_DAT:
2559 case elfcpp::R_SPARC_JMP_SLOT:
2560 case elfcpp::R_SPARC_RELATIVE:
2561 // These are outstanding tls relocs, which are unexpected when
2562 // linking.
2563 case elfcpp::R_SPARC_TLS_DTPMOD64:
2564 case elfcpp::R_SPARC_TLS_DTPMOD32:
2565 case elfcpp::R_SPARC_TLS_TPOFF64:
2566 case elfcpp::R_SPARC_TLS_TPOFF32:
2567 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2568 _("unexpected reloc %u in object file"),
2569 r_type);
2570 break;
2572 default:
2573 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2574 _("unsupported reloc %u"),
2575 r_type);
2576 break;
2579 return true;
2582 // Perform a TLS relocation.
2584 template<int size, bool big_endian>
2585 inline void
2586 Target_sparc<size, big_endian>::Relocate::relocate_tls(
2587 const Relocate_info<size, big_endian>* relinfo,
2588 Target_sparc<size, big_endian>* target,
2589 size_t relnum,
2590 const elfcpp::Rela<size, big_endian>& rela,
2591 unsigned int r_type,
2592 const Sized_symbol<size>* gsym,
2593 const Symbol_value<size>* psymval,
2594 unsigned char* view,
2595 typename elfcpp::Elf_types<size>::Elf_Addr address,
2596 section_size_type)
2598 Output_segment* tls_segment = relinfo->layout->tls_segment();
2599 typedef Sparc_relocate_functions<size, big_endian> Reloc;
2600 const Sized_relobj<size, big_endian>* object = relinfo->object;
2601 typedef typename elfcpp::Swap<32, true>::Valtype Insntype;
2603 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2604 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(object, 0);
2606 const bool is_final =
2607 (gsym == NULL
2608 ? !parameters->options().output_is_position_independent()
2609 : gsym->final_value_is_known());
2610 const tls::Tls_optimization optimized_type
2611 = optimize_tls_reloc(is_final, r_type);
2613 switch (r_type)
2615 case elfcpp::R_SPARC_TLS_GD_HI22:
2616 case elfcpp::R_SPARC_TLS_GD_LO10:
2617 case elfcpp::R_SPARC_TLS_GD_ADD:
2618 case elfcpp::R_SPARC_TLS_GD_CALL:
2619 if (optimized_type == tls::TLSOPT_TO_LE)
2621 Insntype* wv = reinterpret_cast<Insntype*>(view);
2622 Insntype val;
2624 value -= tls_segment->memsz();
2626 switch (r_type)
2628 case elfcpp::R_SPARC_TLS_GD_HI22:
2629 // TLS_GD_HI22 --> TLS_LE_HIX22
2630 Reloc::hix22(view, value, addend);
2631 break;
2633 case elfcpp::R_SPARC_TLS_GD_LO10:
2634 // TLS_GD_LO10 --> TLS_LE_LOX10
2635 Reloc::lox10(view, value, addend);
2636 break;
2638 case elfcpp::R_SPARC_TLS_GD_ADD:
2639 // add %reg1, %reg2, %reg3 --> mov %g7, %reg2, %reg3
2640 val = elfcpp::Swap<32, true>::readval(wv);
2641 val = (val & ~0x7c000) | 0x1c000;
2642 elfcpp::Swap<32, true>::writeval(wv, val);
2643 break;
2644 case elfcpp::R_SPARC_TLS_GD_CALL:
2645 // call __tls_get_addr --> nop
2646 elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
2647 break;
2649 break;
2651 else
2653 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2654 ? GOT_TYPE_TLS_OFFSET
2655 : GOT_TYPE_TLS_PAIR);
2656 if (gsym != NULL)
2658 gold_assert(gsym->has_got_offset(got_type));
2659 value = gsym->got_offset(got_type);
2661 else
2663 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2664 gold_assert(object->local_has_got_offset(r_sym, got_type));
2665 value = object->local_got_offset(r_sym, got_type);
2667 if (optimized_type == tls::TLSOPT_TO_IE)
2669 Insntype* wv = reinterpret_cast<Insntype*>(view);
2670 Insntype val;
2672 switch (r_type)
2674 case elfcpp::R_SPARC_TLS_GD_HI22:
2675 // TLS_GD_HI22 --> TLS_IE_HI22
2676 Reloc::hi22(view, value, addend);
2677 break;
2679 case elfcpp::R_SPARC_TLS_GD_LO10:
2680 // TLS_GD_LO10 --> TLS_IE_LO10
2681 Reloc::lo10(view, value, addend);
2682 break;
2684 case elfcpp::R_SPARC_TLS_GD_ADD:
2685 // add %reg1, %reg2, %reg3 --> ld [%reg1 + %reg2], %reg3
2686 val = elfcpp::Swap<32, true>::readval(wv);
2688 if (size == 64)
2689 val |= 0xc0580000;
2690 else
2691 val |= 0xc0000000;
2693 elfcpp::Swap<32, true>::writeval(wv, val);
2694 break;
2696 case elfcpp::R_SPARC_TLS_GD_CALL:
2697 // The compiler can put the TLS_GD_ADD instruction
2698 // into the delay slot of the call. If so, we need
2699 // to transpose the two instructions so that the
2700 // the new sequence works properly.
2702 // The test we use is if the instruction in the
2703 // delay slot is an add with destination register
2704 // equal to %o0
2705 val = elfcpp::Swap<32, true>::readval(wv + 1);
2706 if ((val & 0x81f80000) == 0x80000000
2707 && ((val >> 25) & 0x1f) == 0x8)
2709 if (size == 64)
2710 val |= 0xc0580000;
2711 else
2712 val |= 0xc0000000;
2714 elfcpp::Swap<32, true>::writeval(wv, val);
2716 wv += 1;
2717 this->ignore_gd_add_ = true;
2720 // call __tls_get_addr --> add %g7, %o0, %o0
2721 elfcpp::Swap<32, true>::writeval(wv, 0x9001c008);
2722 break;
2724 break;
2726 else if (optimized_type == tls::TLSOPT_NONE)
2728 switch (r_type)
2730 case elfcpp::R_SPARC_TLS_GD_HI22:
2731 Reloc::hi22(view, value, addend);
2732 break;
2733 case elfcpp::R_SPARC_TLS_GD_LO10:
2734 Reloc::lo10(view, value, addend);
2735 break;
2736 case elfcpp::R_SPARC_TLS_GD_ADD:
2737 break;
2738 case elfcpp::R_SPARC_TLS_GD_CALL:
2740 Symbol_value<size> symval;
2741 elfcpp::Elf_Xword value;
2742 Symbol* tsym;
2744 tsym = target->tls_get_addr_sym_;
2745 gold_assert(tsym);
2746 value = (target->plt_section()->address() +
2747 tsym->plt_offset());
2748 symval.set_output_value(value);
2749 Reloc::wdisp30(view, object, &symval, addend, address);
2751 break;
2753 break;
2756 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2757 _("unsupported reloc %u"),
2758 r_type);
2759 break;
2761 case elfcpp::R_SPARC_TLS_LDM_HI22:
2762 case elfcpp::R_SPARC_TLS_LDM_LO10:
2763 case elfcpp::R_SPARC_TLS_LDM_ADD:
2764 case elfcpp::R_SPARC_TLS_LDM_CALL:
2765 if (optimized_type == tls::TLSOPT_TO_LE)
2767 Insntype* wv = reinterpret_cast<Insntype*>(view);
2769 switch (r_type)
2771 case elfcpp::R_SPARC_TLS_LDM_HI22:
2772 case elfcpp::R_SPARC_TLS_LDM_LO10:
2773 case elfcpp::R_SPARC_TLS_LDM_ADD:
2774 elfcpp::Swap<32, true>::writeval(wv, sparc_nop);
2775 break;
2777 case elfcpp::R_SPARC_TLS_LDM_CALL:
2778 elfcpp::Swap<32, true>::writeval(wv, sparc_mov_g0_o0);
2779 break;
2781 break;
2783 else if (optimized_type == tls::TLSOPT_NONE)
2785 // Relocate the field with the offset of the GOT entry for
2786 // the module index.
2787 unsigned int got_offset;
2789 got_offset = target->got_mod_index_entry(NULL, NULL, NULL);
2790 switch (r_type)
2792 case elfcpp::R_SPARC_TLS_LDM_HI22:
2793 Reloc::hi22(view, got_offset, addend);
2794 break;
2795 case elfcpp::R_SPARC_TLS_LDM_LO10:
2796 Reloc::lo10(view, got_offset, addend);
2797 break;
2798 case elfcpp::R_SPARC_TLS_LDM_ADD:
2799 break;
2800 case elfcpp::R_SPARC_TLS_LDM_CALL:
2802 Symbol_value<size> symval;
2803 elfcpp::Elf_Xword value;
2804 Symbol* tsym;
2806 tsym = target->tls_get_addr_sym_;
2807 gold_assert(tsym);
2808 value = (target->plt_section()->address() +
2809 tsym->plt_offset());
2810 symval.set_output_value(value);
2811 Reloc::wdisp30(view, object, &symval, addend, address);
2813 break;
2815 break;
2817 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2818 _("unsupported reloc %u"),
2819 r_type);
2820 break;
2822 // These relocs can appear in debugging sections, in which case
2823 // we won't see the TLS_LDM relocs. The local_dynamic_type
2824 // field tells us this.
2825 case elfcpp::R_SPARC_TLS_LDO_HIX22:
2826 if (optimized_type == tls::TLSOPT_TO_LE)
2828 value -= tls_segment->memsz();
2829 Reloc::hix22(view, value, addend);
2831 else
2832 Reloc::ldo_hix22(view, value, addend);
2833 break;
2834 case elfcpp::R_SPARC_TLS_LDO_LOX10:
2835 if (optimized_type == tls::TLSOPT_TO_LE)
2837 value -= tls_segment->memsz();
2838 Reloc::lox10(view, value, addend);
2840 else
2841 Reloc::ldo_lox10(view, value, addend);
2842 break;
2843 case elfcpp::R_SPARC_TLS_LDO_ADD:
2844 if (optimized_type == tls::TLSOPT_TO_LE)
2846 Insntype* wv = reinterpret_cast<Insntype*>(view);
2847 Insntype val;
2849 // add %reg1, %reg2, %reg3 --> add %g7, %reg2, %reg3
2850 val = elfcpp::Swap<32, true>::readval(wv);
2851 val = (val & ~0x7c000) | 0x1c000;
2852 elfcpp::Swap<32, true>::writeval(wv, val);
2854 break;
2856 // When optimizing IE --> LE, the only relocation that is handled
2857 // differently is R_SPARC_TLS_IE_LD, it is rewritten from
2858 // 'ld{,x} [rs1 + rs2], rd' into 'mov rs2, rd' or simply a NOP is
2859 // rs2 and rd are the same.
2860 case elfcpp::R_SPARC_TLS_IE_LD:
2861 case elfcpp::R_SPARC_TLS_IE_LDX:
2862 if (optimized_type == tls::TLSOPT_TO_LE)
2864 Insntype* wv = reinterpret_cast<Insntype*>(view);
2865 Insntype val = elfcpp::Swap<32, true>::readval(wv);
2866 Insntype rs2 = val & 0x1f;
2867 Insntype rd = (val >> 25) & 0x1f;
2869 if (rs2 == rd)
2870 val = sparc_nop;
2871 else
2872 val = sparc_mov | (val & 0x3e00001f);
2874 elfcpp::Swap<32, true>::writeval(wv, val);
2876 break;
2878 case elfcpp::R_SPARC_TLS_IE_HI22:
2879 case elfcpp::R_SPARC_TLS_IE_LO10:
2880 if (optimized_type == tls::TLSOPT_TO_LE)
2882 value -= tls_segment->memsz();
2883 switch (r_type)
2885 case elfcpp::R_SPARC_TLS_IE_HI22:
2886 // IE_HI22 --> LE_HIX22
2887 Reloc::hix22(view, value, addend);
2888 break;
2889 case elfcpp::R_SPARC_TLS_IE_LO10:
2890 // IE_LO10 --> LE_LOX10
2891 Reloc::lox10(view, value, addend);
2892 break;
2894 break;
2896 else if (optimized_type == tls::TLSOPT_NONE)
2898 // Relocate the field with the offset of the GOT entry for
2899 // the tp-relative offset of the symbol.
2900 if (gsym != NULL)
2902 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2903 value = gsym->got_offset(GOT_TYPE_TLS_OFFSET);
2905 else
2907 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
2908 gold_assert(object->local_has_got_offset(r_sym,
2909 GOT_TYPE_TLS_OFFSET));
2910 value = object->local_got_offset(r_sym,
2911 GOT_TYPE_TLS_OFFSET);
2913 switch (r_type)
2915 case elfcpp::R_SPARC_TLS_IE_HI22:
2916 Reloc::hi22(view, value, addend);
2917 break;
2918 case elfcpp::R_SPARC_TLS_IE_LO10:
2919 Reloc::lo10(view, value, addend);
2920 break;
2922 break;
2924 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2925 _("unsupported reloc %u"),
2926 r_type);
2927 break;
2929 case elfcpp::R_SPARC_TLS_LE_HIX22:
2930 // If we're creating a shared library, a dynamic relocation will
2931 // have been created for this location, so do not apply it now.
2932 if (!parameters->options().shared())
2934 value -= tls_segment->memsz();
2935 Reloc::hix22(view, value, addend);
2937 break;
2939 case elfcpp::R_SPARC_TLS_LE_LOX10:
2940 // If we're creating a shared library, a dynamic relocation will
2941 // have been created for this location, so do not apply it now.
2942 if (!parameters->options().shared())
2944 value -= tls_segment->memsz();
2945 Reloc::lox10(view, value, addend);
2947 break;
2951 // Relocate section data.
2953 template<int size, bool big_endian>
2954 void
2955 Target_sparc<size, big_endian>::relocate_section(
2956 const Relocate_info<size, big_endian>* relinfo,
2957 unsigned int sh_type,
2958 const unsigned char* prelocs,
2959 size_t reloc_count,
2960 Output_section* output_section,
2961 bool needs_special_offset_handling,
2962 unsigned char* view,
2963 typename elfcpp::Elf_types<size>::Elf_Addr address,
2964 section_size_type view_size)
2966 typedef Target_sparc<size, big_endian> Sparc;
2967 typedef typename Target_sparc<size, big_endian>::Relocate Sparc_relocate;
2969 gold_assert(sh_type == elfcpp::SHT_RELA);
2971 gold::relocate_section<size, big_endian, Sparc, elfcpp::SHT_RELA,
2972 Sparc_relocate>(
2973 relinfo,
2974 this,
2975 prelocs,
2976 reloc_count,
2977 output_section,
2978 needs_special_offset_handling,
2979 view,
2980 address,
2981 view_size);
2984 // Return the size of a relocation while scanning during a relocatable
2985 // link.
2987 template<int size, bool big_endian>
2988 unsigned int
2989 Target_sparc<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2990 unsigned int,
2991 Relobj*)
2993 // We are always SHT_RELA, so we should never get here.
2994 gold_unreachable();
2995 return 0;
2998 // Scan the relocs during a relocatable link.
3000 template<int size, bool big_endian>
3001 void
3002 Target_sparc<size, big_endian>::scan_relocatable_relocs(
3003 const General_options& options,
3004 Symbol_table* symtab,
3005 Layout* layout,
3006 Sized_relobj<size, big_endian>* object,
3007 unsigned int data_shndx,
3008 unsigned int sh_type,
3009 const unsigned char* prelocs,
3010 size_t reloc_count,
3011 Output_section* output_section,
3012 bool needs_special_offset_handling,
3013 size_t local_symbol_count,
3014 const unsigned char* plocal_symbols,
3015 Relocatable_relocs* rr)
3017 gold_assert(sh_type == elfcpp::SHT_RELA);
3019 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3020 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3022 gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
3023 Scan_relocatable_relocs>(
3024 options,
3025 symtab,
3026 layout,
3027 object,
3028 data_shndx,
3029 prelocs,
3030 reloc_count,
3031 output_section,
3032 needs_special_offset_handling,
3033 local_symbol_count,
3034 plocal_symbols,
3035 rr);
3038 // Relocate a section during a relocatable link.
3040 template<int size, bool big_endian>
3041 void
3042 Target_sparc<size, big_endian>::relocate_for_relocatable(
3043 const Relocate_info<size, big_endian>* relinfo,
3044 unsigned int sh_type,
3045 const unsigned char* prelocs,
3046 size_t reloc_count,
3047 Output_section* output_section,
3048 off_t offset_in_output_section,
3049 const Relocatable_relocs* rr,
3050 unsigned char* view,
3051 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
3052 section_size_type view_size,
3053 unsigned char* reloc_view,
3054 section_size_type reloc_view_size)
3056 gold_assert(sh_type == elfcpp::SHT_RELA);
3058 gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_RELA>(
3059 relinfo,
3060 prelocs,
3061 reloc_count,
3062 output_section,
3063 offset_in_output_section,
3065 view,
3066 view_address,
3067 view_size,
3068 reloc_view,
3069 reloc_view_size);
3072 // Return the value to use for a dynamic which requires special
3073 // treatment. This is how we support equality comparisons of function
3074 // pointers across shared library boundaries, as described in the
3075 // processor specific ABI supplement.
3077 template<int size, bool big_endian>
3078 uint64_t
3079 Target_sparc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
3081 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3082 return this->plt_section()->address() + gsym->plt_offset();
3085 // The selector for sparc object files.
3087 template<int size, bool big_endian>
3088 class Target_selector_sparc : public Target_selector
3090 public:
3091 Target_selector_sparc()
3092 : Target_selector(elfcpp::EM_NONE, size, big_endian,
3093 (size == 64 ? "elf64-sparc" : "elf32-sparc"))
3096 Target* instantiated_target_;
3098 Target* do_recognize(int machine, int, int)
3100 switch (size)
3102 case 64:
3103 if (machine != elfcpp::EM_SPARCV9)
3104 return NULL;
3105 break;
3107 case 32:
3108 if (machine != elfcpp::EM_SPARC
3109 && machine != elfcpp::EM_SPARC32PLUS)
3110 return NULL;
3111 break;
3113 default:
3114 return NULL;
3117 return do_instantiate_target();
3120 Target* do_instantiate_target()
3122 if (this->instantiated_target_ == NULL)
3123 this->instantiated_target_ = new Target_sparc<size, big_endian>();
3124 return this->instantiated_target_;
3128 Target_selector_sparc<32, true> target_selector_sparc32;
3129 Target_selector_sparc<64, true> target_selector_sparc64;
3131 } // End anonymous namespace.