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