Add target_id to elf_backend_data.
[binutils.git] / gold / x86_64.cc
blob4fec45ce6bd92e7733b71757f4e3900e4ebffb72
1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "x86_64.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41 #include "gc.h"
42 #include "icf.h"
44 namespace
47 using namespace gold;
49 // A class to handle the PLT data.
51 class Output_data_plt_x86_64 : public Output_section_data
53 public:
54 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
56 Output_data_plt_x86_64(Symbol_table*, Layout*, Output_data_got<64, false>*,
57 Output_data_space*);
59 // Add an entry to the PLT.
60 void
61 add_entry(Symbol* gsym);
63 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
64 unsigned int
65 add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
66 unsigned int local_sym_index);
68 // Add the reserved TLSDESC_PLT entry to the PLT.
69 void
70 reserve_tlsdesc_entry(unsigned int got_offset)
71 { this->tlsdesc_got_offset_ = got_offset; }
73 // Return true if a TLSDESC_PLT entry has been reserved.
74 bool
75 has_tlsdesc_entry() const
76 { return this->tlsdesc_got_offset_ != -1U; }
78 // Return the GOT offset for the reserved TLSDESC_PLT entry.
79 unsigned int
80 get_tlsdesc_got_offset() const
81 { return this->tlsdesc_got_offset_; }
83 // Return the offset of the reserved TLSDESC_PLT entry.
84 unsigned int
85 get_tlsdesc_plt_offset() const
86 { return (this->count_ + 1) * plt_entry_size; }
88 // Return the .rela.plt section data.
89 Reloc_section*
90 rela_plt()
91 { return this->rel_; }
93 // Return where the TLSDESC relocations should go.
94 Reloc_section*
95 rela_tlsdesc(Layout*);
97 // Return the number of PLT entries.
98 unsigned int
99 entry_count() const
100 { return this->count_; }
102 // Return the offset of the first non-reserved PLT entry.
103 static unsigned int
104 first_plt_entry_offset()
105 { return plt_entry_size; }
107 // Return the size of a PLT entry.
108 static unsigned int
109 get_plt_entry_size()
110 { return plt_entry_size; }
112 protected:
113 void
114 do_adjust_output_section(Output_section* os);
116 // Write to a map file.
117 void
118 do_print_to_mapfile(Mapfile* mapfile) const
119 { mapfile->print_output_data(this, _("** PLT")); }
121 private:
122 // The size of an entry in the PLT.
123 static const int plt_entry_size = 16;
125 // The first entry in the PLT.
126 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
127 // procedure linkage table for both programs and shared objects."
128 static unsigned char first_plt_entry[plt_entry_size];
130 // Other entries in the PLT for an executable.
131 static unsigned char plt_entry[plt_entry_size];
133 // The reserved TLSDESC entry in the PLT for an executable.
134 static unsigned char tlsdesc_plt_entry[plt_entry_size];
136 // Set the final size.
137 void
138 set_final_data_size();
140 // Write out the PLT data.
141 void
142 do_write(Output_file*);
144 // The reloc section.
145 Reloc_section* rel_;
146 // The TLSDESC relocs, if necessary. These must follow the regular
147 // PLT relocs.
148 Reloc_section* tlsdesc_rel_;
149 // The .got section.
150 Output_data_got<64, false>* got_;
151 // The .got.plt section.
152 Output_data_space* got_plt_;
153 // The number of PLT entries.
154 unsigned int count_;
155 // Offset of the reserved TLSDESC_GOT entry when needed.
156 unsigned int tlsdesc_got_offset_;
159 // The x86_64 target class.
160 // See the ABI at
161 // http://www.x86-64.org/documentation/abi.pdf
162 // TLS info comes from
163 // http://people.redhat.com/drepper/tls.pdf
164 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
166 class Target_x86_64 : public Target_freebsd<64, false>
168 public:
169 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
170 // uses only Elf64_Rela relocation entries with explicit addends."
171 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
173 Target_x86_64()
174 : Target_freebsd<64, false>(&x86_64_info),
175 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
176 global_offset_table_(NULL), rela_dyn_(NULL),
177 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
178 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
179 tls_base_symbol_defined_(false)
182 // This function should be defined in targets that can use relocation
183 // types to determine (implemented in local_reloc_may_be_function_pointer
184 // and global_reloc_may_be_function_pointer)
185 // if a function's pointer is taken. ICF uses this in safe mode to only
186 // fold those functions whose pointer is defintely not taken. For x86_64
187 // pie binaries, safe ICF cannot be done by looking at relocation types.
188 inline bool
189 can_check_for_function_pointers() const
190 { return !parameters->options().pie(); }
192 // Hook for a new output section.
193 void
194 do_new_output_section(Output_section*) const;
196 // Scan the relocations to look for symbol adjustments.
197 void
198 gc_process_relocs(Symbol_table* symtab,
199 Layout* layout,
200 Sized_relobj<64, false>* object,
201 unsigned int data_shndx,
202 unsigned int sh_type,
203 const unsigned char* prelocs,
204 size_t reloc_count,
205 Output_section* output_section,
206 bool needs_special_offset_handling,
207 size_t local_symbol_count,
208 const unsigned char* plocal_symbols);
210 // Scan the relocations to look for symbol adjustments.
211 void
212 scan_relocs(Symbol_table* symtab,
213 Layout* layout,
214 Sized_relobj<64, false>* object,
215 unsigned int data_shndx,
216 unsigned int sh_type,
217 const unsigned char* prelocs,
218 size_t reloc_count,
219 Output_section* output_section,
220 bool needs_special_offset_handling,
221 size_t local_symbol_count,
222 const unsigned char* plocal_symbols);
224 // Finalize the sections.
225 void
226 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
228 // Return the value to use for a dynamic which requires special
229 // treatment.
230 uint64_t
231 do_dynsym_value(const Symbol*) const;
233 // Relocate a section.
234 void
235 relocate_section(const Relocate_info<64, false>*,
236 unsigned int sh_type,
237 const unsigned char* prelocs,
238 size_t reloc_count,
239 Output_section* output_section,
240 bool needs_special_offset_handling,
241 unsigned char* view,
242 elfcpp::Elf_types<64>::Elf_Addr view_address,
243 section_size_type view_size,
244 const Reloc_symbol_changes*);
246 // Scan the relocs during a relocatable link.
247 void
248 scan_relocatable_relocs(Symbol_table* symtab,
249 Layout* layout,
250 Sized_relobj<64, false>* object,
251 unsigned int data_shndx,
252 unsigned int sh_type,
253 const unsigned char* prelocs,
254 size_t reloc_count,
255 Output_section* output_section,
256 bool needs_special_offset_handling,
257 size_t local_symbol_count,
258 const unsigned char* plocal_symbols,
259 Relocatable_relocs*);
261 // Relocate a section during a relocatable link.
262 void
263 relocate_for_relocatable(const Relocate_info<64, false>*,
264 unsigned int sh_type,
265 const unsigned char* prelocs,
266 size_t reloc_count,
267 Output_section* output_section,
268 off_t offset_in_output_section,
269 const Relocatable_relocs*,
270 unsigned char* view,
271 elfcpp::Elf_types<64>::Elf_Addr view_address,
272 section_size_type view_size,
273 unsigned char* reloc_view,
274 section_size_type reloc_view_size);
276 // Return a string used to fill a code section with nops.
277 std::string
278 do_code_fill(section_size_type length) const;
280 // Return whether SYM is defined by the ABI.
281 bool
282 do_is_defined_by_abi(const Symbol* sym) const
283 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
285 // Return the symbol index to use for a target specific relocation.
286 // The only target specific relocation is R_X86_64_TLSDESC for a
287 // local symbol, which is an absolute reloc.
288 unsigned int
289 do_reloc_symbol_index(void*, unsigned int r_type) const
291 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
292 return 0;
295 // Return the addend to use for a target specific relocation.
296 uint64_t
297 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
299 // Return the PLT section.
300 Output_data*
301 do_plt_section_for_global(const Symbol*) const
302 { return this->plt_section(); }
304 Output_data*
305 do_plt_section_for_local(const Relobj*, unsigned int) const
306 { return this->plt_section(); }
308 // Adjust -fstack-split code which calls non-stack-split code.
309 void
310 do_calls_non_split(Relobj* object, unsigned int shndx,
311 section_offset_type fnoffset, section_size_type fnsize,
312 unsigned char* view, section_size_type view_size,
313 std::string* from, std::string* to) const;
315 // Return the size of the GOT section.
316 section_size_type
317 got_size() const
319 gold_assert(this->got_ != NULL);
320 return this->got_->data_size();
323 // Return the number of entries in the GOT.
324 unsigned int
325 got_entry_count() const
327 if (this->got_ == NULL)
328 return 0;
329 return this->got_size() / 8;
332 // Return the number of entries in the PLT.
333 unsigned int
334 plt_entry_count() const;
336 // Return the offset of the first non-reserved PLT entry.
337 unsigned int
338 first_plt_entry_offset() const;
340 // Return the size of each PLT entry.
341 unsigned int
342 plt_entry_size() const;
344 // Add a new reloc argument, returning the index in the vector.
345 size_t
346 add_tlsdesc_info(Sized_relobj<64, false>* object, unsigned int r_sym)
348 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
349 return this->tlsdesc_reloc_info_.size() - 1;
352 private:
353 // The class which scans relocations.
354 class Scan
356 public:
357 Scan()
358 : issued_non_pic_error_(false)
361 inline void
362 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
363 Sized_relobj<64, false>* object,
364 unsigned int data_shndx,
365 Output_section* output_section,
366 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
367 const elfcpp::Sym<64, false>& lsym);
369 inline void
370 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
371 Sized_relobj<64, false>* object,
372 unsigned int data_shndx,
373 Output_section* output_section,
374 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
375 Symbol* gsym);
377 inline bool
378 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
379 Target_x86_64* target,
380 Sized_relobj<64, false>* object,
381 unsigned int data_shndx,
382 Output_section* output_section,
383 const elfcpp::Rela<64, false>& reloc,
384 unsigned int r_type,
385 const elfcpp::Sym<64, false>& lsym);
387 inline bool
388 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
389 Target_x86_64* target,
390 Sized_relobj<64, false>* object,
391 unsigned int data_shndx,
392 Output_section* output_section,
393 const elfcpp::Rela<64, false>& reloc,
394 unsigned int r_type,
395 Symbol* gsym);
397 private:
398 static void
399 unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
401 static void
402 unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
403 Symbol*);
405 void
406 check_non_pic(Relobj*, unsigned int r_type);
408 inline bool
409 possible_function_pointer_reloc(unsigned int r_type);
411 bool
412 reloc_needs_plt_for_ifunc(Sized_relobj<64, false>*, unsigned int r_type);
414 // Whether we have issued an error about a non-PIC compilation.
415 bool issued_non_pic_error_;
418 // The class which implements relocation.
419 class Relocate
421 public:
422 Relocate()
423 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
426 ~Relocate()
428 if (this->skip_call_tls_get_addr_)
430 // FIXME: This needs to specify the location somehow.
431 gold_error(_("missing expected TLS relocation"));
435 // Do a relocation. Return false if the caller should not issue
436 // any warnings about this relocation.
437 inline bool
438 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
439 size_t relnum, const elfcpp::Rela<64, false>&,
440 unsigned int r_type, const Sized_symbol<64>*,
441 const Symbol_value<64>*,
442 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
443 section_size_type);
445 private:
446 // Do a TLS relocation.
447 inline void
448 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
449 size_t relnum, const elfcpp::Rela<64, false>&,
450 unsigned int r_type, const Sized_symbol<64>*,
451 const Symbol_value<64>*,
452 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
453 section_size_type);
455 // Do a TLS General-Dynamic to Initial-Exec transition.
456 inline void
457 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
458 Output_segment* tls_segment,
459 const elfcpp::Rela<64, false>&, unsigned int r_type,
460 elfcpp::Elf_types<64>::Elf_Addr value,
461 unsigned char* view,
462 elfcpp::Elf_types<64>::Elf_Addr,
463 section_size_type view_size);
465 // Do a TLS General-Dynamic to Local-Exec transition.
466 inline void
467 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
468 Output_segment* tls_segment,
469 const elfcpp::Rela<64, false>&, unsigned int r_type,
470 elfcpp::Elf_types<64>::Elf_Addr value,
471 unsigned char* view,
472 section_size_type view_size);
474 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
475 inline void
476 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
477 Output_segment* tls_segment,
478 const elfcpp::Rela<64, false>&, unsigned int r_type,
479 elfcpp::Elf_types<64>::Elf_Addr value,
480 unsigned char* view,
481 elfcpp::Elf_types<64>::Elf_Addr,
482 section_size_type view_size);
484 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
485 inline void
486 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
487 Output_segment* tls_segment,
488 const elfcpp::Rela<64, false>&, unsigned int r_type,
489 elfcpp::Elf_types<64>::Elf_Addr value,
490 unsigned char* view,
491 section_size_type view_size);
493 // Do a TLS Local-Dynamic to Local-Exec transition.
494 inline void
495 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
496 Output_segment* tls_segment,
497 const elfcpp::Rela<64, false>&, unsigned int r_type,
498 elfcpp::Elf_types<64>::Elf_Addr value,
499 unsigned char* view,
500 section_size_type view_size);
502 // Do a TLS Initial-Exec to Local-Exec transition.
503 static inline void
504 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
505 Output_segment* tls_segment,
506 const elfcpp::Rela<64, false>&, unsigned int r_type,
507 elfcpp::Elf_types<64>::Elf_Addr value,
508 unsigned char* view,
509 section_size_type view_size);
511 // This is set if we should skip the next reloc, which should be a
512 // PLT32 reloc against ___tls_get_addr.
513 bool skip_call_tls_get_addr_;
515 // This is set if we see a relocation which could load the address
516 // of the TLS block. Whether we see such a relocation determines
517 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
518 // in debugging sections.
519 bool saw_tls_block_reloc_;
522 // A class which returns the size required for a relocation type,
523 // used while scanning relocs during a relocatable link.
524 class Relocatable_size_for_reloc
526 public:
527 unsigned int
528 get_size_for_reloc(unsigned int, Relobj*);
531 // Adjust TLS relocation type based on the options and whether this
532 // is a local symbol.
533 static tls::Tls_optimization
534 optimize_tls_reloc(bool is_final, int r_type);
536 // Get the GOT section, creating it if necessary.
537 Output_data_got<64, false>*
538 got_section(Symbol_table*, Layout*);
540 // Get the GOT PLT section.
541 Output_data_space*
542 got_plt_section() const
544 gold_assert(this->got_plt_ != NULL);
545 return this->got_plt_;
548 // Get the GOT section for TLSDESC entries.
549 Output_data_got<64, false>*
550 got_tlsdesc_section() const
552 gold_assert(this->got_tlsdesc_ != NULL);
553 return this->got_tlsdesc_;
556 // Create the PLT section.
557 void
558 make_plt_section(Symbol_table* symtab, Layout* layout);
560 // Create a PLT entry for a global symbol.
561 void
562 make_plt_entry(Symbol_table*, Layout*, Symbol*);
564 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
565 void
566 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
567 Sized_relobj<64, false>* relobj,
568 unsigned int local_sym_index);
570 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
571 void
572 define_tls_base_symbol(Symbol_table*, Layout*);
574 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
575 void
576 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
578 // Create a GOT entry for the TLS module index.
579 unsigned int
580 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
581 Sized_relobj<64, false>* object);
583 // Get the PLT section.
584 Output_data_plt_x86_64*
585 plt_section() const
587 gold_assert(this->plt_ != NULL);
588 return this->plt_;
591 // Get the dynamic reloc section, creating it if necessary.
592 Reloc_section*
593 rela_dyn_section(Layout*);
595 // Get the section to use for TLSDESC relocations.
596 Reloc_section*
597 rela_tlsdesc_section(Layout*) const;
599 // Add a potential copy relocation.
600 void
601 copy_reloc(Symbol_table* symtab, Layout* layout,
602 Sized_relobj<64, false>* object,
603 unsigned int shndx, Output_section* output_section,
604 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
606 this->copy_relocs_.copy_reloc(symtab, layout,
607 symtab->get_sized_symbol<64>(sym),
608 object, shndx, output_section,
609 reloc, this->rela_dyn_section(layout));
612 // Information about this specific target which we pass to the
613 // general Target structure.
614 static const Target::Target_info x86_64_info;
616 // The types of GOT entries needed for this platform.
617 // These values are exposed to the ABI in an incremental link.
618 // Do not renumber existing values without changing the version
619 // number of the .gnu_incremental_inputs section.
620 enum Got_type
622 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
623 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
624 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
625 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
628 // This type is used as the argument to the target specific
629 // relocation routines. The only target specific reloc is
630 // R_X86_64_TLSDESC against a local symbol.
631 struct Tlsdesc_info
633 Tlsdesc_info(Sized_relobj<64, false>* a_object, unsigned int a_r_sym)
634 : object(a_object), r_sym(a_r_sym)
637 // The object in which the local symbol is defined.
638 Sized_relobj<64, false>* object;
639 // The local symbol index in the object.
640 unsigned int r_sym;
643 // The GOT section.
644 Output_data_got<64, false>* got_;
645 // The PLT section.
646 Output_data_plt_x86_64* plt_;
647 // The GOT PLT section.
648 Output_data_space* got_plt_;
649 // The GOT section for TLSDESC relocations.
650 Output_data_got<64, false>* got_tlsdesc_;
651 // The _GLOBAL_OFFSET_TABLE_ symbol.
652 Symbol* global_offset_table_;
653 // The dynamic reloc section.
654 Reloc_section* rela_dyn_;
655 // Relocs saved to avoid a COPY reloc.
656 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
657 // Space for variables copied with a COPY reloc.
658 Output_data_space* dynbss_;
659 // Offset of the GOT entry for the TLS module index.
660 unsigned int got_mod_index_offset_;
661 // We handle R_X86_64_TLSDESC against a local symbol as a target
662 // specific relocation. Here we store the object and local symbol
663 // index for the relocation.
664 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
665 // True if the _TLS_MODULE_BASE_ symbol has been defined.
666 bool tls_base_symbol_defined_;
669 const Target::Target_info Target_x86_64::x86_64_info =
671 64, // size
672 false, // is_big_endian
673 elfcpp::EM_X86_64, // machine_code
674 false, // has_make_symbol
675 false, // has_resolve
676 true, // has_code_fill
677 true, // is_default_stack_executable
678 '\0', // wrap_char
679 "/lib/ld64.so.1", // program interpreter
680 0x400000, // default_text_segment_address
681 0x1000, // abi_pagesize (overridable by -z max-page-size)
682 0x1000, // common_pagesize (overridable by -z common-page-size)
683 elfcpp::SHN_UNDEF, // small_common_shndx
684 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
685 0, // small_common_section_flags
686 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
687 NULL, // attributes_section
688 NULL // attributes_vendor
691 // This is called when a new output section is created. This is where
692 // we handle the SHF_X86_64_LARGE.
694 void
695 Target_x86_64::do_new_output_section(Output_section* os) const
697 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
698 os->set_is_large_section();
701 // Get the GOT section, creating it if necessary.
703 Output_data_got<64, false>*
704 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
706 if (this->got_ == NULL)
708 gold_assert(symtab != NULL && layout != NULL);
710 this->got_ = new Output_data_got<64, false>();
712 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
713 (elfcpp::SHF_ALLOC
714 | elfcpp::SHF_WRITE),
715 this->got_, ORDER_RELRO_LAST,
716 true);
718 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
719 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
720 (elfcpp::SHF_ALLOC
721 | elfcpp::SHF_WRITE),
722 this->got_plt_, ORDER_NON_RELRO_FIRST,
723 false);
725 // The first three entries are reserved.
726 this->got_plt_->set_current_data_size(3 * 8);
728 // Those bytes can go into the relro segment.
729 layout->increase_relro(3 * 8);
731 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
732 this->global_offset_table_ =
733 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
734 Symbol_table::PREDEFINED,
735 this->got_plt_,
736 0, 0, elfcpp::STT_OBJECT,
737 elfcpp::STB_LOCAL,
738 elfcpp::STV_HIDDEN, 0,
739 false, false);
741 // If there are any TLSDESC relocations, they get GOT entries in
742 // .got.plt after the jump slot entries.
743 this->got_tlsdesc_ = new Output_data_got<64, false>();
744 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
745 (elfcpp::SHF_ALLOC
746 | elfcpp::SHF_WRITE),
747 this->got_tlsdesc_,
748 ORDER_NON_RELRO_FIRST, false);
751 return this->got_;
754 // Get the dynamic reloc section, creating it if necessary.
756 Target_x86_64::Reloc_section*
757 Target_x86_64::rela_dyn_section(Layout* layout)
759 if (this->rela_dyn_ == NULL)
761 gold_assert(layout != NULL);
762 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
763 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
764 elfcpp::SHF_ALLOC, this->rela_dyn_,
765 ORDER_DYNAMIC_RELOCS, false);
767 return this->rela_dyn_;
770 // Create the PLT section. The ordinary .got section is an argument,
771 // since we need to refer to the start. We also create our own .got
772 // section just for PLT entries.
774 Output_data_plt_x86_64::Output_data_plt_x86_64(Symbol_table* symtab,
775 Layout* layout,
776 Output_data_got<64, false>* got,
777 Output_data_space* got_plt)
778 : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
779 count_(0), tlsdesc_got_offset_(-1U)
781 this->rel_ = new Reloc_section(false);
782 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
783 elfcpp::SHF_ALLOC, this->rel_,
784 ORDER_DYNAMIC_PLT_RELOCS, false);
786 if (parameters->doing_static_link())
788 // A statically linked executable will only have a .rela.plt
789 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
790 // symbols. The library will use these symbols to locate the
791 // IRELATIVE relocs at program startup time.
792 symtab->define_in_output_data("__rela_iplt_start", NULL,
793 Symbol_table::PREDEFINED,
794 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
795 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
796 0, false, true);
797 symtab->define_in_output_data("__rela_iplt_end", NULL,
798 Symbol_table::PREDEFINED,
799 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
800 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
801 0, true, true);
805 void
806 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
808 os->set_entsize(plt_entry_size);
811 // Add an entry to the PLT.
813 void
814 Output_data_plt_x86_64::add_entry(Symbol* gsym)
816 gold_assert(!gsym->has_plt_offset());
818 // Note that when setting the PLT offset we skip the initial
819 // reserved PLT entry.
820 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
822 ++this->count_;
824 section_offset_type got_offset = this->got_plt_->current_data_size();
826 // Every PLT entry needs a GOT entry which points back to the PLT
827 // entry (this will be changed by the dynamic linker, normally
828 // lazily when the function is called).
829 this->got_plt_->set_current_data_size(got_offset + 8);
831 // Every PLT entry needs a reloc.
832 if (gsym->type() == elfcpp::STT_GNU_IFUNC
833 && gsym->can_use_relative_reloc(false))
834 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
835 this->got_plt_, got_offset, 0);
836 else
838 gsym->set_needs_dynsym_entry();
839 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
840 got_offset, 0);
843 // Note that we don't need to save the symbol. The contents of the
844 // PLT are independent of which symbols are used. The symbols only
845 // appear in the relocations.
848 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
849 // the PLT offset.
851 unsigned int
852 Output_data_plt_x86_64::add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
853 unsigned int local_sym_index)
855 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
856 ++this->count_;
858 section_offset_type got_offset = this->got_plt_->current_data_size();
860 // Every PLT entry needs a GOT entry which points back to the PLT
861 // entry.
862 this->got_plt_->set_current_data_size(got_offset + 8);
864 // Every PLT entry needs a reloc.
865 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
866 elfcpp::R_X86_64_IRELATIVE,
867 this->got_plt_, got_offset, 0);
869 return plt_offset;
872 // Return where the TLSDESC relocations should go, creating it if
873 // necessary. These follow the JUMP_SLOT relocations.
875 Output_data_plt_x86_64::Reloc_section*
876 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
878 if (this->tlsdesc_rel_ == NULL)
880 this->tlsdesc_rel_ = new Reloc_section(false);
881 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
882 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
883 ORDER_DYNAMIC_PLT_RELOCS, false);
884 gold_assert(this->tlsdesc_rel_->output_section() ==
885 this->rel_->output_section());
887 return this->tlsdesc_rel_;
890 // Set the final size.
891 void
892 Output_data_plt_x86_64::set_final_data_size()
894 unsigned int count = this->count_;
895 if (this->has_tlsdesc_entry())
896 ++count;
897 this->set_data_size((count + 1) * plt_entry_size);
900 // The first entry in the PLT for an executable.
902 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
904 // From AMD64 ABI Draft 0.98, page 76
905 0xff, 0x35, // pushq contents of memory address
906 0, 0, 0, 0, // replaced with address of .got + 8
907 0xff, 0x25, // jmp indirect
908 0, 0, 0, 0, // replaced with address of .got + 16
909 0x90, 0x90, 0x90, 0x90 // noop (x4)
912 // Subsequent entries in the PLT for an executable.
914 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
916 // From AMD64 ABI Draft 0.98, page 76
917 0xff, 0x25, // jmpq indirect
918 0, 0, 0, 0, // replaced with address of symbol in .got
919 0x68, // pushq immediate
920 0, 0, 0, 0, // replaced with offset into relocation table
921 0xe9, // jmpq relative
922 0, 0, 0, 0 // replaced with offset to start of .plt
925 // The reserved TLSDESC entry in the PLT for an executable.
927 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
929 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
930 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
931 0xff, 0x35, // pushq x(%rip)
932 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
933 0xff, 0x25, // jmpq *y(%rip)
934 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
935 0x0f, 0x1f, // nop
936 0x40, 0
939 // Write out the PLT. This uses the hand-coded instructions above,
940 // and adjusts them as needed. This is specified by the AMD64 ABI.
942 void
943 Output_data_plt_x86_64::do_write(Output_file* of)
945 const off_t offset = this->offset();
946 const section_size_type oview_size =
947 convert_to_section_size_type(this->data_size());
948 unsigned char* const oview = of->get_output_view(offset, oview_size);
950 const off_t got_file_offset = this->got_plt_->offset();
951 const section_size_type got_size =
952 convert_to_section_size_type(this->got_plt_->data_size());
953 unsigned char* const got_view = of->get_output_view(got_file_offset,
954 got_size);
956 unsigned char* pov = oview;
958 // The base address of the .plt section.
959 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
960 // The base address of the .got section.
961 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
962 // The base address of the PLT portion of the .got section,
963 // which is where the GOT pointer will point, and where the
964 // three reserved GOT entries are located.
965 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
967 memcpy(pov, first_plt_entry, plt_entry_size);
968 // We do a jmp relative to the PC at the end of this instruction.
969 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
970 (got_address + 8
971 - (plt_address + 6)));
972 elfcpp::Swap<32, false>::writeval(pov + 8,
973 (got_address + 16
974 - (plt_address + 12)));
975 pov += plt_entry_size;
977 unsigned char* got_pov = got_view;
979 memset(got_pov, 0, 24);
980 got_pov += 24;
982 unsigned int plt_offset = plt_entry_size;
983 unsigned int got_offset = 24;
984 const unsigned int count = this->count_;
985 for (unsigned int plt_index = 0;
986 plt_index < count;
987 ++plt_index,
988 pov += plt_entry_size,
989 got_pov += 8,
990 plt_offset += plt_entry_size,
991 got_offset += 8)
993 // Set and adjust the PLT entry itself.
994 memcpy(pov, plt_entry, plt_entry_size);
995 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
996 (got_address + got_offset
997 - (plt_address + plt_offset
998 + 6)));
1000 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1001 elfcpp::Swap<32, false>::writeval(pov + 12,
1002 - (plt_offset + plt_entry_size));
1004 // Set the entry in the GOT.
1005 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1008 if (this->has_tlsdesc_entry())
1010 // Set and adjust the reserved TLSDESC PLT entry.
1011 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1012 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1013 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1014 (got_address + 8
1015 - (plt_address + plt_offset
1016 + 6)));
1017 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1018 (got_base
1019 + tlsdesc_got_offset
1020 - (plt_address + plt_offset
1021 + 12)));
1022 pov += plt_entry_size;
1025 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1026 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1028 of->write_output_view(offset, oview_size, oview);
1029 of->write_output_view(got_file_offset, got_size, got_view);
1032 // Create the PLT section.
1034 void
1035 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1037 if (this->plt_ == NULL)
1039 // Create the GOT sections first.
1040 this->got_section(symtab, layout);
1042 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1043 this->got_plt_);
1044 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1045 (elfcpp::SHF_ALLOC
1046 | elfcpp::SHF_EXECINSTR),
1047 this->plt_, ORDER_PLT, false);
1049 // Make the sh_info field of .rela.plt point to .plt.
1050 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1051 rela_plt_os->set_info_section(this->plt_->output_section());
1055 // Return the section for TLSDESC relocations.
1057 Target_x86_64::Reloc_section*
1058 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1060 return this->plt_section()->rela_tlsdesc(layout);
1063 // Create a PLT entry for a global symbol.
1065 void
1066 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1067 Symbol* gsym)
1069 if (gsym->has_plt_offset())
1070 return;
1072 if (this->plt_ == NULL)
1073 this->make_plt_section(symtab, layout);
1075 this->plt_->add_entry(gsym);
1078 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1080 void
1081 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1082 Sized_relobj<64, false>* relobj,
1083 unsigned int local_sym_index)
1085 if (relobj->local_has_plt_offset(local_sym_index))
1086 return;
1087 if (this->plt_ == NULL)
1088 this->make_plt_section(symtab, layout);
1089 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1090 local_sym_index);
1091 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1094 // Return the number of entries in the PLT.
1096 unsigned int
1097 Target_x86_64::plt_entry_count() const
1099 if (this->plt_ == NULL)
1100 return 0;
1101 return this->plt_->entry_count();
1104 // Return the offset of the first non-reserved PLT entry.
1106 unsigned int
1107 Target_x86_64::first_plt_entry_offset() const
1109 return Output_data_plt_x86_64::first_plt_entry_offset();
1112 // Return the size of each PLT entry.
1114 unsigned int
1115 Target_x86_64::plt_entry_size() const
1117 return Output_data_plt_x86_64::get_plt_entry_size();
1120 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1122 void
1123 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1125 if (this->tls_base_symbol_defined_)
1126 return;
1128 Output_segment* tls_segment = layout->tls_segment();
1129 if (tls_segment != NULL)
1131 bool is_exec = parameters->options().output_is_executable();
1132 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1133 Symbol_table::PREDEFINED,
1134 tls_segment, 0, 0,
1135 elfcpp::STT_TLS,
1136 elfcpp::STB_LOCAL,
1137 elfcpp::STV_HIDDEN, 0,
1138 (is_exec
1139 ? Symbol::SEGMENT_END
1140 : Symbol::SEGMENT_START),
1141 true);
1143 this->tls_base_symbol_defined_ = true;
1146 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1148 void
1149 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1150 Layout* layout)
1152 if (this->plt_ == NULL)
1153 this->make_plt_section(symtab, layout);
1155 if (!this->plt_->has_tlsdesc_entry())
1157 // Allocate the TLSDESC_GOT entry.
1158 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1159 unsigned int got_offset = got->add_constant(0);
1161 // Allocate the TLSDESC_PLT entry.
1162 this->plt_->reserve_tlsdesc_entry(got_offset);
1166 // Create a GOT entry for the TLS module index.
1168 unsigned int
1169 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1170 Sized_relobj<64, false>* object)
1172 if (this->got_mod_index_offset_ == -1U)
1174 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1175 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1176 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1177 unsigned int got_offset = got->add_constant(0);
1178 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1179 got_offset, 0);
1180 got->add_constant(0);
1181 this->got_mod_index_offset_ = got_offset;
1183 return this->got_mod_index_offset_;
1186 // Optimize the TLS relocation type based on what we know about the
1187 // symbol. IS_FINAL is true if the final address of this symbol is
1188 // known at link time.
1190 tls::Tls_optimization
1191 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1193 // If we are generating a shared library, then we can't do anything
1194 // in the linker.
1195 if (parameters->options().shared())
1196 return tls::TLSOPT_NONE;
1198 switch (r_type)
1200 case elfcpp::R_X86_64_TLSGD:
1201 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1202 case elfcpp::R_X86_64_TLSDESC_CALL:
1203 // These are General-Dynamic which permits fully general TLS
1204 // access. Since we know that we are generating an executable,
1205 // we can convert this to Initial-Exec. If we also know that
1206 // this is a local symbol, we can further switch to Local-Exec.
1207 if (is_final)
1208 return tls::TLSOPT_TO_LE;
1209 return tls::TLSOPT_TO_IE;
1211 case elfcpp::R_X86_64_TLSLD:
1212 // This is Local-Dynamic, which refers to a local symbol in the
1213 // dynamic TLS block. Since we know that we generating an
1214 // executable, we can switch to Local-Exec.
1215 return tls::TLSOPT_TO_LE;
1217 case elfcpp::R_X86_64_DTPOFF32:
1218 case elfcpp::R_X86_64_DTPOFF64:
1219 // Another Local-Dynamic reloc.
1220 return tls::TLSOPT_TO_LE;
1222 case elfcpp::R_X86_64_GOTTPOFF:
1223 // These are Initial-Exec relocs which get the thread offset
1224 // from the GOT. If we know that we are linking against the
1225 // local symbol, we can switch to Local-Exec, which links the
1226 // thread offset into the instruction.
1227 if (is_final)
1228 return tls::TLSOPT_TO_LE;
1229 return tls::TLSOPT_NONE;
1231 case elfcpp::R_X86_64_TPOFF32:
1232 // When we already have Local-Exec, there is nothing further we
1233 // can do.
1234 return tls::TLSOPT_NONE;
1236 default:
1237 gold_unreachable();
1241 // Report an unsupported relocation against a local symbol.
1243 void
1244 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1245 unsigned int r_type)
1247 gold_error(_("%s: unsupported reloc %u against local symbol"),
1248 object->name().c_str(), r_type);
1251 // We are about to emit a dynamic relocation of type R_TYPE. If the
1252 // dynamic linker does not support it, issue an error. The GNU linker
1253 // only issues a non-PIC error for an allocated read-only section.
1254 // Here we know the section is allocated, but we don't know that it is
1255 // read-only. But we check for all the relocation types which the
1256 // glibc dynamic linker supports, so it seems appropriate to issue an
1257 // error even if the section is not read-only.
1259 void
1260 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1262 switch (r_type)
1264 // These are the relocation types supported by glibc for x86_64.
1265 case elfcpp::R_X86_64_RELATIVE:
1266 case elfcpp::R_X86_64_IRELATIVE:
1267 case elfcpp::R_X86_64_GLOB_DAT:
1268 case elfcpp::R_X86_64_JUMP_SLOT:
1269 case elfcpp::R_X86_64_DTPMOD64:
1270 case elfcpp::R_X86_64_DTPOFF64:
1271 case elfcpp::R_X86_64_TPOFF64:
1272 case elfcpp::R_X86_64_64:
1273 case elfcpp::R_X86_64_32:
1274 case elfcpp::R_X86_64_PC32:
1275 case elfcpp::R_X86_64_COPY:
1276 return;
1278 default:
1279 // This prevents us from issuing more than one error per reloc
1280 // section. But we can still wind up issuing more than one
1281 // error per object file.
1282 if (this->issued_non_pic_error_)
1283 return;
1284 gold_assert(parameters->options().output_is_position_independent());
1285 object->error(_("requires unsupported dynamic reloc; "
1286 "recompile with -fPIC"));
1287 this->issued_non_pic_error_ = true;
1288 return;
1290 case elfcpp::R_X86_64_NONE:
1291 gold_unreachable();
1295 // Return whether we need to make a PLT entry for a relocation of the
1296 // given type against a STT_GNU_IFUNC symbol.
1298 bool
1299 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<64, false>* object,
1300 unsigned int r_type)
1302 switch (r_type)
1304 case elfcpp::R_X86_64_NONE:
1305 case elfcpp::R_X86_64_GNU_VTINHERIT:
1306 case elfcpp::R_X86_64_GNU_VTENTRY:
1307 return false;
1309 case elfcpp::R_X86_64_64:
1310 case elfcpp::R_X86_64_32:
1311 case elfcpp::R_X86_64_32S:
1312 case elfcpp::R_X86_64_16:
1313 case elfcpp::R_X86_64_8:
1314 case elfcpp::R_X86_64_PC64:
1315 case elfcpp::R_X86_64_PC32:
1316 case elfcpp::R_X86_64_PC16:
1317 case elfcpp::R_X86_64_PC8:
1318 case elfcpp::R_X86_64_PLT32:
1319 case elfcpp::R_X86_64_GOTPC32:
1320 case elfcpp::R_X86_64_GOTOFF64:
1321 case elfcpp::R_X86_64_GOTPC64:
1322 case elfcpp::R_X86_64_PLTOFF64:
1323 case elfcpp::R_X86_64_GOT64:
1324 case elfcpp::R_X86_64_GOT32:
1325 case elfcpp::R_X86_64_GOTPCREL64:
1326 case elfcpp::R_X86_64_GOTPCREL:
1327 case elfcpp::R_X86_64_GOTPLT64:
1328 return true;
1330 case elfcpp::R_X86_64_COPY:
1331 case elfcpp::R_X86_64_GLOB_DAT:
1332 case elfcpp::R_X86_64_JUMP_SLOT:
1333 case elfcpp::R_X86_64_RELATIVE:
1334 case elfcpp::R_X86_64_IRELATIVE:
1335 case elfcpp::R_X86_64_TPOFF64:
1336 case elfcpp::R_X86_64_DTPMOD64:
1337 case elfcpp::R_X86_64_TLSDESC:
1338 // We will give an error later.
1339 return false;
1341 case elfcpp::R_X86_64_TLSGD:
1342 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1343 case elfcpp::R_X86_64_TLSDESC_CALL:
1344 case elfcpp::R_X86_64_TLSLD:
1345 case elfcpp::R_X86_64_DTPOFF32:
1346 case elfcpp::R_X86_64_DTPOFF64:
1347 case elfcpp::R_X86_64_GOTTPOFF:
1348 case elfcpp::R_X86_64_TPOFF32:
1349 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1350 object->name().c_str(), r_type);
1351 return false;
1353 case elfcpp::R_X86_64_SIZE32:
1354 case elfcpp::R_X86_64_SIZE64:
1355 default:
1356 // We will give an error later.
1357 return false;
1361 // Scan a relocation for a local symbol.
1363 inline void
1364 Target_x86_64::Scan::local(Symbol_table* symtab,
1365 Layout* layout,
1366 Target_x86_64* target,
1367 Sized_relobj<64, false>* object,
1368 unsigned int data_shndx,
1369 Output_section* output_section,
1370 const elfcpp::Rela<64, false>& reloc,
1371 unsigned int r_type,
1372 const elfcpp::Sym<64, false>& lsym)
1374 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1375 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1376 && this->reloc_needs_plt_for_ifunc(object, r_type))
1378 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1379 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1382 switch (r_type)
1384 case elfcpp::R_X86_64_NONE:
1385 case elfcpp::R_X86_64_GNU_VTINHERIT:
1386 case elfcpp::R_X86_64_GNU_VTENTRY:
1387 break;
1389 case elfcpp::R_X86_64_64:
1390 // If building a shared library (or a position-independent
1391 // executable), we need to create a dynamic relocation for this
1392 // location. The relocation applied at link time will apply the
1393 // link-time value, so we flag the location with an
1394 // R_X86_64_RELATIVE relocation so the dynamic loader can
1395 // relocate it easily.
1396 if (parameters->options().output_is_position_independent())
1398 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1399 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1400 rela_dyn->add_local_relative(object, r_sym,
1401 elfcpp::R_X86_64_RELATIVE,
1402 output_section, data_shndx,
1403 reloc.get_r_offset(),
1404 reloc.get_r_addend());
1406 break;
1408 case elfcpp::R_X86_64_32:
1409 case elfcpp::R_X86_64_32S:
1410 case elfcpp::R_X86_64_16:
1411 case elfcpp::R_X86_64_8:
1412 // If building a shared library (or a position-independent
1413 // executable), we need to create a dynamic relocation for this
1414 // location. We can't use an R_X86_64_RELATIVE relocation
1415 // because that is always a 64-bit relocation.
1416 if (parameters->options().output_is_position_independent())
1418 this->check_non_pic(object, r_type);
1420 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1421 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1422 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1423 rela_dyn->add_local(object, r_sym, r_type, output_section,
1424 data_shndx, reloc.get_r_offset(),
1425 reloc.get_r_addend());
1426 else
1428 gold_assert(lsym.get_st_value() == 0);
1429 unsigned int shndx = lsym.get_st_shndx();
1430 bool is_ordinary;
1431 shndx = object->adjust_sym_shndx(r_sym, shndx,
1432 &is_ordinary);
1433 if (!is_ordinary)
1434 object->error(_("section symbol %u has bad shndx %u"),
1435 r_sym, shndx);
1436 else
1437 rela_dyn->add_local_section(object, shndx,
1438 r_type, output_section,
1439 data_shndx, reloc.get_r_offset(),
1440 reloc.get_r_addend());
1443 break;
1445 case elfcpp::R_X86_64_PC64:
1446 case elfcpp::R_X86_64_PC32:
1447 case elfcpp::R_X86_64_PC16:
1448 case elfcpp::R_X86_64_PC8:
1449 break;
1451 case elfcpp::R_X86_64_PLT32:
1452 // Since we know this is a local symbol, we can handle this as a
1453 // PC32 reloc.
1454 break;
1456 case elfcpp::R_X86_64_GOTPC32:
1457 case elfcpp::R_X86_64_GOTOFF64:
1458 case elfcpp::R_X86_64_GOTPC64:
1459 case elfcpp::R_X86_64_PLTOFF64:
1460 // We need a GOT section.
1461 target->got_section(symtab, layout);
1462 // For PLTOFF64, we'd normally want a PLT section, but since we
1463 // know this is a local symbol, no PLT is needed.
1464 break;
1466 case elfcpp::R_X86_64_GOT64:
1467 case elfcpp::R_X86_64_GOT32:
1468 case elfcpp::R_X86_64_GOTPCREL64:
1469 case elfcpp::R_X86_64_GOTPCREL:
1470 case elfcpp::R_X86_64_GOTPLT64:
1472 // The symbol requires a GOT entry.
1473 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1474 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1476 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1477 // lets function pointers compare correctly with shared
1478 // libraries. Otherwise we would need an IRELATIVE reloc.
1479 bool is_new;
1480 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1481 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1482 else
1483 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1484 if (is_new)
1486 // If we are generating a shared object, we need to add a
1487 // dynamic relocation for this symbol's GOT entry.
1488 if (parameters->options().output_is_position_independent())
1490 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1491 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1492 if (r_type != elfcpp::R_X86_64_GOT32)
1494 unsigned int got_offset =
1495 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1496 rela_dyn->add_local_relative(object, r_sym,
1497 elfcpp::R_X86_64_RELATIVE,
1498 got, got_offset, 0);
1500 else
1502 this->check_non_pic(object, r_type);
1504 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1505 rela_dyn->add_local(
1506 object, r_sym, r_type, got,
1507 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1511 // For GOTPLT64, we'd normally want a PLT section, but since
1512 // we know this is a local symbol, no PLT is needed.
1514 break;
1516 case elfcpp::R_X86_64_COPY:
1517 case elfcpp::R_X86_64_GLOB_DAT:
1518 case elfcpp::R_X86_64_JUMP_SLOT:
1519 case elfcpp::R_X86_64_RELATIVE:
1520 case elfcpp::R_X86_64_IRELATIVE:
1521 // These are outstanding tls relocs, which are unexpected when linking
1522 case elfcpp::R_X86_64_TPOFF64:
1523 case elfcpp::R_X86_64_DTPMOD64:
1524 case elfcpp::R_X86_64_TLSDESC:
1525 gold_error(_("%s: unexpected reloc %u in object file"),
1526 object->name().c_str(), r_type);
1527 break;
1529 // These are initial tls relocs, which are expected when linking
1530 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1531 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1532 case elfcpp::R_X86_64_TLSDESC_CALL:
1533 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1534 case elfcpp::R_X86_64_DTPOFF32:
1535 case elfcpp::R_X86_64_DTPOFF64:
1536 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1537 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1539 bool output_is_shared = parameters->options().shared();
1540 const tls::Tls_optimization optimized_type
1541 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1542 switch (r_type)
1544 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1545 if (optimized_type == tls::TLSOPT_NONE)
1547 // Create a pair of GOT entries for the module index and
1548 // dtv-relative offset.
1549 Output_data_got<64, false>* got
1550 = target->got_section(symtab, layout);
1551 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1552 unsigned int shndx = lsym.get_st_shndx();
1553 bool is_ordinary;
1554 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1555 if (!is_ordinary)
1556 object->error(_("local symbol %u has bad shndx %u"),
1557 r_sym, shndx);
1558 else
1559 got->add_local_pair_with_rela(object, r_sym,
1560 shndx,
1561 GOT_TYPE_TLS_PAIR,
1562 target->rela_dyn_section(layout),
1563 elfcpp::R_X86_64_DTPMOD64, 0);
1565 else if (optimized_type != tls::TLSOPT_TO_LE)
1566 unsupported_reloc_local(object, r_type);
1567 break;
1569 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1570 target->define_tls_base_symbol(symtab, layout);
1571 if (optimized_type == tls::TLSOPT_NONE)
1573 // Create reserved PLT and GOT entries for the resolver.
1574 target->reserve_tlsdesc_entries(symtab, layout);
1576 // Generate a double GOT entry with an
1577 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1578 // is resolved lazily, so the GOT entry needs to be in
1579 // an area in .got.plt, not .got. Call got_section to
1580 // make sure the section has been created.
1581 target->got_section(symtab, layout);
1582 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1583 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1584 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1586 unsigned int got_offset = got->add_constant(0);
1587 got->add_constant(0);
1588 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1589 got_offset);
1590 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1591 // We store the arguments we need in a vector, and
1592 // use the index into the vector as the parameter
1593 // to pass to the target specific routines.
1594 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1595 void* arg = reinterpret_cast<void*>(intarg);
1596 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1597 got, got_offset, 0);
1600 else if (optimized_type != tls::TLSOPT_TO_LE)
1601 unsupported_reloc_local(object, r_type);
1602 break;
1604 case elfcpp::R_X86_64_TLSDESC_CALL:
1605 break;
1607 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1608 if (optimized_type == tls::TLSOPT_NONE)
1610 // Create a GOT entry for the module index.
1611 target->got_mod_index_entry(symtab, layout, object);
1613 else if (optimized_type != tls::TLSOPT_TO_LE)
1614 unsupported_reloc_local(object, r_type);
1615 break;
1617 case elfcpp::R_X86_64_DTPOFF32:
1618 case elfcpp::R_X86_64_DTPOFF64:
1619 break;
1621 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1622 layout->set_has_static_tls();
1623 if (optimized_type == tls::TLSOPT_NONE)
1625 // Create a GOT entry for the tp-relative offset.
1626 Output_data_got<64, false>* got
1627 = target->got_section(symtab, layout);
1628 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1629 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1630 target->rela_dyn_section(layout),
1631 elfcpp::R_X86_64_TPOFF64);
1633 else if (optimized_type != tls::TLSOPT_TO_LE)
1634 unsupported_reloc_local(object, r_type);
1635 break;
1637 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1638 layout->set_has_static_tls();
1639 if (output_is_shared)
1640 unsupported_reloc_local(object, r_type);
1641 break;
1643 default:
1644 gold_unreachable();
1647 break;
1649 case elfcpp::R_X86_64_SIZE32:
1650 case elfcpp::R_X86_64_SIZE64:
1651 default:
1652 gold_error(_("%s: unsupported reloc %u against local symbol"),
1653 object->name().c_str(), r_type);
1654 break;
1659 // Report an unsupported relocation against a global symbol.
1661 void
1662 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1663 unsigned int r_type,
1664 Symbol* gsym)
1666 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1667 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1670 // Returns true if this relocation type could be that of a function pointer.
1671 inline bool
1672 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1674 switch (r_type)
1676 case elfcpp::R_X86_64_64:
1677 case elfcpp::R_X86_64_32:
1678 case elfcpp::R_X86_64_32S:
1679 case elfcpp::R_X86_64_16:
1680 case elfcpp::R_X86_64_8:
1681 case elfcpp::R_X86_64_GOT64:
1682 case elfcpp::R_X86_64_GOT32:
1683 case elfcpp::R_X86_64_GOTPCREL64:
1684 case elfcpp::R_X86_64_GOTPCREL:
1685 case elfcpp::R_X86_64_GOTPLT64:
1687 return true;
1690 return false;
1693 // For safe ICF, scan a relocation for a local symbol to check if it
1694 // corresponds to a function pointer being taken. In that case mark
1695 // the function whose pointer was taken as not foldable.
1697 inline bool
1698 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1699 Symbol_table* ,
1700 Layout* ,
1701 Target_x86_64* ,
1702 Sized_relobj<64, false>* ,
1703 unsigned int ,
1704 Output_section* ,
1705 const elfcpp::Rela<64, false>& ,
1706 unsigned int r_type,
1707 const elfcpp::Sym<64, false>&)
1709 // When building a shared library, do not fold any local symbols as it is
1710 // not possible to distinguish pointer taken versus a call by looking at
1711 // the relocation types.
1712 return (parameters->options().shared()
1713 || possible_function_pointer_reloc(r_type));
1716 // For safe ICF, scan a relocation for a global symbol to check if it
1717 // corresponds to a function pointer being taken. In that case mark
1718 // the function whose pointer was taken as not foldable.
1720 inline bool
1721 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1722 Symbol_table*,
1723 Layout* ,
1724 Target_x86_64* ,
1725 Sized_relobj<64, false>* ,
1726 unsigned int ,
1727 Output_section* ,
1728 const elfcpp::Rela<64, false>& ,
1729 unsigned int r_type,
1730 Symbol* gsym)
1732 // When building a shared library, do not fold symbols whose visibility
1733 // is hidden, internal or protected.
1734 return ((parameters->options().shared()
1735 && (gsym->visibility() == elfcpp::STV_INTERNAL
1736 || gsym->visibility() == elfcpp::STV_PROTECTED
1737 || gsym->visibility() == elfcpp::STV_HIDDEN))
1738 || possible_function_pointer_reloc(r_type));
1741 // Scan a relocation for a global symbol.
1743 inline void
1744 Target_x86_64::Scan::global(Symbol_table* symtab,
1745 Layout* layout,
1746 Target_x86_64* target,
1747 Sized_relobj<64, false>* object,
1748 unsigned int data_shndx,
1749 Output_section* output_section,
1750 const elfcpp::Rela<64, false>& reloc,
1751 unsigned int r_type,
1752 Symbol* gsym)
1754 // A STT_GNU_IFUNC symbol may require a PLT entry.
1755 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1756 && this->reloc_needs_plt_for_ifunc(object, r_type))
1757 target->make_plt_entry(symtab, layout, gsym);
1759 switch (r_type)
1761 case elfcpp::R_X86_64_NONE:
1762 case elfcpp::R_X86_64_GNU_VTINHERIT:
1763 case elfcpp::R_X86_64_GNU_VTENTRY:
1764 break;
1766 case elfcpp::R_X86_64_64:
1767 case elfcpp::R_X86_64_32:
1768 case elfcpp::R_X86_64_32S:
1769 case elfcpp::R_X86_64_16:
1770 case elfcpp::R_X86_64_8:
1772 // Make a PLT entry if necessary.
1773 if (gsym->needs_plt_entry())
1775 target->make_plt_entry(symtab, layout, gsym);
1776 // Since this is not a PC-relative relocation, we may be
1777 // taking the address of a function. In that case we need to
1778 // set the entry in the dynamic symbol table to the address of
1779 // the PLT entry.
1780 if (gsym->is_from_dynobj() && !parameters->options().shared())
1781 gsym->set_needs_dynsym_value();
1783 // Make a dynamic relocation if necessary.
1784 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1786 if (gsym->may_need_copy_reloc())
1788 target->copy_reloc(symtab, layout, object,
1789 data_shndx, output_section, gsym, reloc);
1791 else if (r_type == elfcpp::R_X86_64_64
1792 && gsym->type() == elfcpp::STT_GNU_IFUNC
1793 && gsym->can_use_relative_reloc(false)
1794 && !gsym->is_from_dynobj()
1795 && !gsym->is_undefined()
1796 && !gsym->is_preemptible())
1798 // Use an IRELATIVE reloc for a locally defined
1799 // STT_GNU_IFUNC symbol. This makes a function
1800 // address in a PIE executable match the address in a
1801 // shared library that it links against.
1802 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1803 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
1804 rela_dyn->add_symbolless_global_addend(gsym, r_type,
1805 output_section, object,
1806 data_shndx,
1807 reloc.get_r_offset(),
1808 reloc.get_r_addend());
1810 else if (r_type == elfcpp::R_X86_64_64
1811 && gsym->can_use_relative_reloc(false))
1813 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1814 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1815 output_section, object,
1816 data_shndx,
1817 reloc.get_r_offset(),
1818 reloc.get_r_addend());
1820 else
1822 this->check_non_pic(object, r_type);
1823 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1824 rela_dyn->add_global(gsym, r_type, output_section, object,
1825 data_shndx, reloc.get_r_offset(),
1826 reloc.get_r_addend());
1830 break;
1832 case elfcpp::R_X86_64_PC64:
1833 case elfcpp::R_X86_64_PC32:
1834 case elfcpp::R_X86_64_PC16:
1835 case elfcpp::R_X86_64_PC8:
1837 // Make a PLT entry if necessary.
1838 if (gsym->needs_plt_entry())
1839 target->make_plt_entry(symtab, layout, gsym);
1840 // Make a dynamic relocation if necessary.
1841 int flags = Symbol::NON_PIC_REF;
1842 if (gsym->is_func())
1843 flags |= Symbol::FUNCTION_CALL;
1844 if (gsym->needs_dynamic_reloc(flags))
1846 if (gsym->may_need_copy_reloc())
1848 target->copy_reloc(symtab, layout, object,
1849 data_shndx, output_section, gsym, reloc);
1851 else
1853 this->check_non_pic(object, r_type);
1854 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1855 rela_dyn->add_global(gsym, r_type, output_section, object,
1856 data_shndx, reloc.get_r_offset(),
1857 reloc.get_r_addend());
1861 break;
1863 case elfcpp::R_X86_64_GOT64:
1864 case elfcpp::R_X86_64_GOT32:
1865 case elfcpp::R_X86_64_GOTPCREL64:
1866 case elfcpp::R_X86_64_GOTPCREL:
1867 case elfcpp::R_X86_64_GOTPLT64:
1869 // The symbol requires a GOT entry.
1870 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1871 if (gsym->final_value_is_known())
1873 // For a STT_GNU_IFUNC symbol we want the PLT address.
1874 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1875 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1876 else
1877 got->add_global(gsym, GOT_TYPE_STANDARD);
1879 else
1881 // If this symbol is not fully resolved, we need to add a
1882 // dynamic relocation for it.
1883 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1884 if (gsym->is_from_dynobj()
1885 || gsym->is_undefined()
1886 || gsym->is_preemptible()
1887 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1888 && parameters->options().output_is_position_independent()))
1889 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1890 elfcpp::R_X86_64_GLOB_DAT);
1891 else
1893 // For a STT_GNU_IFUNC symbol we want to write the PLT
1894 // offset into the GOT, so that function pointer
1895 // comparisons work correctly.
1896 bool is_new;
1897 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1898 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1899 else
1901 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1902 // Tell the dynamic linker to use the PLT address
1903 // when resolving relocations.
1904 if (gsym->is_from_dynobj()
1905 && !parameters->options().shared())
1906 gsym->set_needs_dynsym_value();
1908 if (is_new)
1910 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1911 rela_dyn->add_global_relative(gsym,
1912 elfcpp::R_X86_64_RELATIVE,
1913 got, got_off, 0);
1917 // For GOTPLT64, we also need a PLT entry (but only if the
1918 // symbol is not fully resolved).
1919 if (r_type == elfcpp::R_X86_64_GOTPLT64
1920 && !gsym->final_value_is_known())
1921 target->make_plt_entry(symtab, layout, gsym);
1923 break;
1925 case elfcpp::R_X86_64_PLT32:
1926 // If the symbol is fully resolved, this is just a PC32 reloc.
1927 // Otherwise we need a PLT entry.
1928 if (gsym->final_value_is_known())
1929 break;
1930 // If building a shared library, we can also skip the PLT entry
1931 // if the symbol is defined in the output file and is protected
1932 // or hidden.
1933 if (gsym->is_defined()
1934 && !gsym->is_from_dynobj()
1935 && !gsym->is_preemptible())
1936 break;
1937 target->make_plt_entry(symtab, layout, gsym);
1938 break;
1940 case elfcpp::R_X86_64_GOTPC32:
1941 case elfcpp::R_X86_64_GOTOFF64:
1942 case elfcpp::R_X86_64_GOTPC64:
1943 case elfcpp::R_X86_64_PLTOFF64:
1944 // We need a GOT section.
1945 target->got_section(symtab, layout);
1946 // For PLTOFF64, we also need a PLT entry (but only if the
1947 // symbol is not fully resolved).
1948 if (r_type == elfcpp::R_X86_64_PLTOFF64
1949 && !gsym->final_value_is_known())
1950 target->make_plt_entry(symtab, layout, gsym);
1951 break;
1953 case elfcpp::R_X86_64_COPY:
1954 case elfcpp::R_X86_64_GLOB_DAT:
1955 case elfcpp::R_X86_64_JUMP_SLOT:
1956 case elfcpp::R_X86_64_RELATIVE:
1957 case elfcpp::R_X86_64_IRELATIVE:
1958 // These are outstanding tls relocs, which are unexpected when linking
1959 case elfcpp::R_X86_64_TPOFF64:
1960 case elfcpp::R_X86_64_DTPMOD64:
1961 case elfcpp::R_X86_64_TLSDESC:
1962 gold_error(_("%s: unexpected reloc %u in object file"),
1963 object->name().c_str(), r_type);
1964 break;
1966 // These are initial tls relocs, which are expected for global()
1967 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1968 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1969 case elfcpp::R_X86_64_TLSDESC_CALL:
1970 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1971 case elfcpp::R_X86_64_DTPOFF32:
1972 case elfcpp::R_X86_64_DTPOFF64:
1973 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1974 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1976 const bool is_final = gsym->final_value_is_known();
1977 const tls::Tls_optimization optimized_type
1978 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1979 switch (r_type)
1981 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1982 if (optimized_type == tls::TLSOPT_NONE)
1984 // Create a pair of GOT entries for the module index and
1985 // dtv-relative offset.
1986 Output_data_got<64, false>* got
1987 = target->got_section(symtab, layout);
1988 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1989 target->rela_dyn_section(layout),
1990 elfcpp::R_X86_64_DTPMOD64,
1991 elfcpp::R_X86_64_DTPOFF64);
1993 else if (optimized_type == tls::TLSOPT_TO_IE)
1995 // Create a GOT entry for the tp-relative offset.
1996 Output_data_got<64, false>* got
1997 = target->got_section(symtab, layout);
1998 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1999 target->rela_dyn_section(layout),
2000 elfcpp::R_X86_64_TPOFF64);
2002 else if (optimized_type != tls::TLSOPT_TO_LE)
2003 unsupported_reloc_global(object, r_type, gsym);
2004 break;
2006 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2007 target->define_tls_base_symbol(symtab, layout);
2008 if (optimized_type == tls::TLSOPT_NONE)
2010 // Create reserved PLT and GOT entries for the resolver.
2011 target->reserve_tlsdesc_entries(symtab, layout);
2013 // Create a double GOT entry with an R_X86_64_TLSDESC
2014 // reloc. The R_X86_64_TLSDESC reloc is resolved
2015 // lazily, so the GOT entry needs to be in an area in
2016 // .got.plt, not .got. Call got_section to make sure
2017 // the section has been created.
2018 target->got_section(symtab, layout);
2019 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2020 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2021 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2022 elfcpp::R_X86_64_TLSDESC, 0);
2024 else if (optimized_type == tls::TLSOPT_TO_IE)
2026 // Create a GOT entry for the tp-relative offset.
2027 Output_data_got<64, false>* got
2028 = target->got_section(symtab, layout);
2029 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2030 target->rela_dyn_section(layout),
2031 elfcpp::R_X86_64_TPOFF64);
2033 else if (optimized_type != tls::TLSOPT_TO_LE)
2034 unsupported_reloc_global(object, r_type, gsym);
2035 break;
2037 case elfcpp::R_X86_64_TLSDESC_CALL:
2038 break;
2040 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2041 if (optimized_type == tls::TLSOPT_NONE)
2043 // Create a GOT entry for the module index.
2044 target->got_mod_index_entry(symtab, layout, object);
2046 else if (optimized_type != tls::TLSOPT_TO_LE)
2047 unsupported_reloc_global(object, r_type, gsym);
2048 break;
2050 case elfcpp::R_X86_64_DTPOFF32:
2051 case elfcpp::R_X86_64_DTPOFF64:
2052 break;
2054 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2055 layout->set_has_static_tls();
2056 if (optimized_type == tls::TLSOPT_NONE)
2058 // Create a GOT entry for the tp-relative offset.
2059 Output_data_got<64, false>* got
2060 = target->got_section(symtab, layout);
2061 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2062 target->rela_dyn_section(layout),
2063 elfcpp::R_X86_64_TPOFF64);
2065 else if (optimized_type != tls::TLSOPT_TO_LE)
2066 unsupported_reloc_global(object, r_type, gsym);
2067 break;
2069 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2070 layout->set_has_static_tls();
2071 if (parameters->options().shared())
2072 unsupported_reloc_local(object, r_type);
2073 break;
2075 default:
2076 gold_unreachable();
2079 break;
2081 case elfcpp::R_X86_64_SIZE32:
2082 case elfcpp::R_X86_64_SIZE64:
2083 default:
2084 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2085 object->name().c_str(), r_type,
2086 gsym->demangled_name().c_str());
2087 break;
2091 void
2092 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2093 Layout* layout,
2094 Sized_relobj<64, false>* object,
2095 unsigned int data_shndx,
2096 unsigned int sh_type,
2097 const unsigned char* prelocs,
2098 size_t reloc_count,
2099 Output_section* output_section,
2100 bool needs_special_offset_handling,
2101 size_t local_symbol_count,
2102 const unsigned char* plocal_symbols)
2105 if (sh_type == elfcpp::SHT_REL)
2107 return;
2110 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2111 Target_x86_64::Scan,
2112 Target_x86_64::Relocatable_size_for_reloc>(
2113 symtab,
2114 layout,
2115 this,
2116 object,
2117 data_shndx,
2118 prelocs,
2119 reloc_count,
2120 output_section,
2121 needs_special_offset_handling,
2122 local_symbol_count,
2123 plocal_symbols);
2126 // Scan relocations for a section.
2128 void
2129 Target_x86_64::scan_relocs(Symbol_table* symtab,
2130 Layout* layout,
2131 Sized_relobj<64, false>* object,
2132 unsigned int data_shndx,
2133 unsigned int sh_type,
2134 const unsigned char* prelocs,
2135 size_t reloc_count,
2136 Output_section* output_section,
2137 bool needs_special_offset_handling,
2138 size_t local_symbol_count,
2139 const unsigned char* plocal_symbols)
2141 if (sh_type == elfcpp::SHT_REL)
2143 gold_error(_("%s: unsupported REL reloc section"),
2144 object->name().c_str());
2145 return;
2148 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2149 Target_x86_64::Scan>(
2150 symtab,
2151 layout,
2152 this,
2153 object,
2154 data_shndx,
2155 prelocs,
2156 reloc_count,
2157 output_section,
2158 needs_special_offset_handling,
2159 local_symbol_count,
2160 plocal_symbols);
2163 // Finalize the sections.
2165 void
2166 Target_x86_64::do_finalize_sections(
2167 Layout* layout,
2168 const Input_objects*,
2169 Symbol_table* symtab)
2171 const Reloc_section* rel_plt = (this->plt_ == NULL
2172 ? NULL
2173 : this->plt_->rela_plt());
2174 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2175 this->rela_dyn_, true, false);
2177 // Fill in some more dynamic tags.
2178 Output_data_dynamic* const odyn = layout->dynamic_data();
2179 if (odyn != NULL)
2181 if (this->plt_ != NULL
2182 && this->plt_->output_section() != NULL
2183 && this->plt_->has_tlsdesc_entry())
2185 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2186 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2187 this->got_->finalize_data_size();
2188 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2189 this->plt_, plt_offset);
2190 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2191 this->got_, got_offset);
2195 // Emit any relocs we saved in an attempt to avoid generating COPY
2196 // relocs.
2197 if (this->copy_relocs_.any_saved_relocs())
2198 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2200 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2201 // the .got.plt section.
2202 Symbol* sym = this->global_offset_table_;
2203 if (sym != NULL)
2205 uint64_t data_size = this->got_plt_->current_data_size();
2206 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2210 // Perform a relocation.
2212 inline bool
2213 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2214 Target_x86_64* target,
2215 Output_section*,
2216 size_t relnum,
2217 const elfcpp::Rela<64, false>& rela,
2218 unsigned int r_type,
2219 const Sized_symbol<64>* gsym,
2220 const Symbol_value<64>* psymval,
2221 unsigned char* view,
2222 elfcpp::Elf_types<64>::Elf_Addr address,
2223 section_size_type view_size)
2225 if (this->skip_call_tls_get_addr_)
2227 if ((r_type != elfcpp::R_X86_64_PLT32
2228 && r_type != elfcpp::R_X86_64_PC32)
2229 || gsym == NULL
2230 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2232 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2233 _("missing expected TLS relocation"));
2235 else
2237 this->skip_call_tls_get_addr_ = false;
2238 return false;
2242 const Sized_relobj<64, false>* object = relinfo->object;
2244 // Pick the value to use for symbols defined in the PLT.
2245 Symbol_value<64> symval;
2246 if (gsym != NULL
2247 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
2248 || r_type == elfcpp::R_X86_64_PC32
2249 || r_type == elfcpp::R_X86_64_PC16
2250 || r_type == elfcpp::R_X86_64_PC8))
2252 symval.set_output_value(target->plt_section()->address()
2253 + gsym->plt_offset());
2254 psymval = &symval;
2256 else if (gsym == NULL && psymval->is_ifunc_symbol())
2258 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2259 if (object->local_has_plt_offset(r_sym))
2261 symval.set_output_value(target->plt_section()->address()
2262 + object->local_plt_offset(r_sym));
2263 psymval = &symval;
2267 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2269 // Get the GOT offset if needed.
2270 // The GOT pointer points to the end of the GOT section.
2271 // We need to subtract the size of the GOT section to get
2272 // the actual offset to use in the relocation.
2273 bool have_got_offset = false;
2274 unsigned int got_offset = 0;
2275 switch (r_type)
2277 case elfcpp::R_X86_64_GOT32:
2278 case elfcpp::R_X86_64_GOT64:
2279 case elfcpp::R_X86_64_GOTPLT64:
2280 case elfcpp::R_X86_64_GOTPCREL:
2281 case elfcpp::R_X86_64_GOTPCREL64:
2282 if (gsym != NULL)
2284 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2285 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2287 else
2289 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2290 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2291 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2292 - target->got_size());
2294 have_got_offset = true;
2295 break;
2297 default:
2298 break;
2301 switch (r_type)
2303 case elfcpp::R_X86_64_NONE:
2304 case elfcpp::R_X86_64_GNU_VTINHERIT:
2305 case elfcpp::R_X86_64_GNU_VTENTRY:
2306 break;
2308 case elfcpp::R_X86_64_64:
2309 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2310 break;
2312 case elfcpp::R_X86_64_PC64:
2313 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2314 address);
2315 break;
2317 case elfcpp::R_X86_64_32:
2318 // FIXME: we need to verify that value + addend fits into 32 bits:
2319 // uint64_t x = value + addend;
2320 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2321 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2322 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2323 break;
2325 case elfcpp::R_X86_64_32S:
2326 // FIXME: we need to verify that value + addend fits into 32 bits:
2327 // int64_t x = value + addend; // note this quantity is signed!
2328 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2329 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2330 break;
2332 case elfcpp::R_X86_64_PC32:
2333 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2334 address);
2335 break;
2337 case elfcpp::R_X86_64_16:
2338 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2339 break;
2341 case elfcpp::R_X86_64_PC16:
2342 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2343 address);
2344 break;
2346 case elfcpp::R_X86_64_8:
2347 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2348 break;
2350 case elfcpp::R_X86_64_PC8:
2351 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2352 address);
2353 break;
2355 case elfcpp::R_X86_64_PLT32:
2356 gold_assert(gsym == NULL
2357 || gsym->has_plt_offset()
2358 || gsym->final_value_is_known()
2359 || (gsym->is_defined()
2360 && !gsym->is_from_dynobj()
2361 && !gsym->is_preemptible()));
2362 // Note: while this code looks the same as for R_X86_64_PC32, it
2363 // behaves differently because psymval was set to point to
2364 // the PLT entry, rather than the symbol, in Scan::global().
2365 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2366 address);
2367 break;
2369 case elfcpp::R_X86_64_PLTOFF64:
2371 gold_assert(gsym);
2372 gold_assert(gsym->has_plt_offset()
2373 || gsym->final_value_is_known());
2374 elfcpp::Elf_types<64>::Elf_Addr got_address;
2375 got_address = target->got_section(NULL, NULL)->address();
2376 Relocate_functions<64, false>::rela64(view, object, psymval,
2377 addend - got_address);
2380 case elfcpp::R_X86_64_GOT32:
2381 gold_assert(have_got_offset);
2382 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2383 break;
2385 case elfcpp::R_X86_64_GOTPC32:
2387 gold_assert(gsym);
2388 elfcpp::Elf_types<64>::Elf_Addr value;
2389 value = target->got_plt_section()->address();
2390 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2392 break;
2394 case elfcpp::R_X86_64_GOT64:
2395 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2396 // Since we always add a PLT entry, this is equivalent.
2397 case elfcpp::R_X86_64_GOTPLT64:
2398 gold_assert(have_got_offset);
2399 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2400 break;
2402 case elfcpp::R_X86_64_GOTPC64:
2404 gold_assert(gsym);
2405 elfcpp::Elf_types<64>::Elf_Addr value;
2406 value = target->got_plt_section()->address();
2407 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2409 break;
2411 case elfcpp::R_X86_64_GOTOFF64:
2413 elfcpp::Elf_types<64>::Elf_Addr value;
2414 value = (psymval->value(object, 0)
2415 - target->got_plt_section()->address());
2416 Relocate_functions<64, false>::rela64(view, value, addend);
2418 break;
2420 case elfcpp::R_X86_64_GOTPCREL:
2422 gold_assert(have_got_offset);
2423 elfcpp::Elf_types<64>::Elf_Addr value;
2424 value = target->got_plt_section()->address() + got_offset;
2425 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2427 break;
2429 case elfcpp::R_X86_64_GOTPCREL64:
2431 gold_assert(have_got_offset);
2432 elfcpp::Elf_types<64>::Elf_Addr value;
2433 value = target->got_plt_section()->address() + got_offset;
2434 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2436 break;
2438 case elfcpp::R_X86_64_COPY:
2439 case elfcpp::R_X86_64_GLOB_DAT:
2440 case elfcpp::R_X86_64_JUMP_SLOT:
2441 case elfcpp::R_X86_64_RELATIVE:
2442 case elfcpp::R_X86_64_IRELATIVE:
2443 // These are outstanding tls relocs, which are unexpected when linking
2444 case elfcpp::R_X86_64_TPOFF64:
2445 case elfcpp::R_X86_64_DTPMOD64:
2446 case elfcpp::R_X86_64_TLSDESC:
2447 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2448 _("unexpected reloc %u in object file"),
2449 r_type);
2450 break;
2452 // These are initial tls relocs, which are expected when linking
2453 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2454 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2455 case elfcpp::R_X86_64_TLSDESC_CALL:
2456 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2457 case elfcpp::R_X86_64_DTPOFF32:
2458 case elfcpp::R_X86_64_DTPOFF64:
2459 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2460 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2461 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2462 view, address, view_size);
2463 break;
2465 case elfcpp::R_X86_64_SIZE32:
2466 case elfcpp::R_X86_64_SIZE64:
2467 default:
2468 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2469 _("unsupported reloc %u"),
2470 r_type);
2471 break;
2474 return true;
2477 // Perform a TLS relocation.
2479 inline void
2480 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2481 Target_x86_64* target,
2482 size_t relnum,
2483 const elfcpp::Rela<64, false>& rela,
2484 unsigned int r_type,
2485 const Sized_symbol<64>* gsym,
2486 const Symbol_value<64>* psymval,
2487 unsigned char* view,
2488 elfcpp::Elf_types<64>::Elf_Addr address,
2489 section_size_type view_size)
2491 Output_segment* tls_segment = relinfo->layout->tls_segment();
2493 const Sized_relobj<64, false>* object = relinfo->object;
2494 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2496 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2498 const bool is_final = (gsym == NULL
2499 ? !parameters->options().shared()
2500 : gsym->final_value_is_known());
2501 const tls::Tls_optimization optimized_type
2502 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2503 switch (r_type)
2505 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2506 this->saw_tls_block_reloc_ = true;
2507 if (optimized_type == tls::TLSOPT_TO_LE)
2509 gold_assert(tls_segment != NULL);
2510 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2511 rela, r_type, value, view,
2512 view_size);
2513 break;
2515 else
2517 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2518 ? GOT_TYPE_TLS_OFFSET
2519 : GOT_TYPE_TLS_PAIR);
2520 unsigned int got_offset;
2521 if (gsym != NULL)
2523 gold_assert(gsym->has_got_offset(got_type));
2524 got_offset = gsym->got_offset(got_type) - target->got_size();
2526 else
2528 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2529 gold_assert(object->local_has_got_offset(r_sym, got_type));
2530 got_offset = (object->local_got_offset(r_sym, got_type)
2531 - target->got_size());
2533 if (optimized_type == tls::TLSOPT_TO_IE)
2535 gold_assert(tls_segment != NULL);
2536 value = target->got_plt_section()->address() + got_offset;
2537 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2538 value, view, address, view_size);
2539 break;
2541 else if (optimized_type == tls::TLSOPT_NONE)
2543 // Relocate the field with the offset of the pair of GOT
2544 // entries.
2545 value = target->got_plt_section()->address() + got_offset;
2546 Relocate_functions<64, false>::pcrela32(view, value, addend,
2547 address);
2548 break;
2551 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2552 _("unsupported reloc %u"), r_type);
2553 break;
2555 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2556 case elfcpp::R_X86_64_TLSDESC_CALL:
2557 this->saw_tls_block_reloc_ = true;
2558 if (optimized_type == tls::TLSOPT_TO_LE)
2560 gold_assert(tls_segment != NULL);
2561 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2562 rela, r_type, value, view,
2563 view_size);
2564 break;
2566 else
2568 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2569 ? GOT_TYPE_TLS_OFFSET
2570 : GOT_TYPE_TLS_DESC);
2571 unsigned int got_offset = 0;
2572 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2573 && optimized_type == tls::TLSOPT_NONE)
2575 // We created GOT entries in the .got.tlsdesc portion of
2576 // the .got.plt section, but the offset stored in the
2577 // symbol is the offset within .got.tlsdesc.
2578 got_offset = (target->got_size()
2579 + target->got_plt_section()->data_size());
2581 if (gsym != NULL)
2583 gold_assert(gsym->has_got_offset(got_type));
2584 got_offset += gsym->got_offset(got_type) - target->got_size();
2586 else
2588 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2589 gold_assert(object->local_has_got_offset(r_sym, got_type));
2590 got_offset += (object->local_got_offset(r_sym, got_type)
2591 - target->got_size());
2593 if (optimized_type == tls::TLSOPT_TO_IE)
2595 gold_assert(tls_segment != NULL);
2596 value = target->got_plt_section()->address() + got_offset;
2597 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2598 rela, r_type, value, view, address,
2599 view_size);
2600 break;
2602 else if (optimized_type == tls::TLSOPT_NONE)
2604 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2606 // Relocate the field with the offset of the pair of GOT
2607 // entries.
2608 value = target->got_plt_section()->address() + got_offset;
2609 Relocate_functions<64, false>::pcrela32(view, value, addend,
2610 address);
2612 break;
2615 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2616 _("unsupported reloc %u"), r_type);
2617 break;
2619 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2620 this->saw_tls_block_reloc_ = true;
2621 if (optimized_type == tls::TLSOPT_TO_LE)
2623 gold_assert(tls_segment != NULL);
2624 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2625 value, view, view_size);
2626 break;
2628 else if (optimized_type == tls::TLSOPT_NONE)
2630 // Relocate the field with the offset of the GOT entry for
2631 // the module index.
2632 unsigned int got_offset;
2633 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2634 - target->got_size());
2635 value = target->got_plt_section()->address() + got_offset;
2636 Relocate_functions<64, false>::pcrela32(view, value, addend,
2637 address);
2638 break;
2640 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2641 _("unsupported reloc %u"), r_type);
2642 break;
2644 case elfcpp::R_X86_64_DTPOFF32:
2645 if (optimized_type == tls::TLSOPT_TO_LE)
2647 // This relocation type is used in debugging information.
2648 // In that case we need to not optimize the value. If we
2649 // haven't seen a TLSLD reloc, then we assume we should not
2650 // optimize this reloc.
2651 if (this->saw_tls_block_reloc_)
2653 gold_assert(tls_segment != NULL);
2654 value -= tls_segment->memsz();
2657 Relocate_functions<64, false>::rela32(view, value, addend);
2658 break;
2660 case elfcpp::R_X86_64_DTPOFF64:
2661 if (optimized_type == tls::TLSOPT_TO_LE)
2663 // See R_X86_64_DTPOFF32, just above, for why we test this.
2664 if (this->saw_tls_block_reloc_)
2666 gold_assert(tls_segment != NULL);
2667 value -= tls_segment->memsz();
2670 Relocate_functions<64, false>::rela64(view, value, addend);
2671 break;
2673 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2674 if (optimized_type == tls::TLSOPT_TO_LE)
2676 gold_assert(tls_segment != NULL);
2677 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2678 rela, r_type, value, view,
2679 view_size);
2680 break;
2682 else if (optimized_type == tls::TLSOPT_NONE)
2684 // Relocate the field with the offset of the GOT entry for
2685 // the tp-relative offset of the symbol.
2686 unsigned int got_offset;
2687 if (gsym != NULL)
2689 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2690 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2691 - target->got_size());
2693 else
2695 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2696 gold_assert(object->local_has_got_offset(r_sym,
2697 GOT_TYPE_TLS_OFFSET));
2698 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2699 - target->got_size());
2701 value = target->got_plt_section()->address() + got_offset;
2702 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2703 break;
2705 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2706 _("unsupported reloc type %u"),
2707 r_type);
2708 break;
2710 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2711 value -= tls_segment->memsz();
2712 Relocate_functions<64, false>::rela32(view, value, addend);
2713 break;
2717 // Do a relocation in which we convert a TLS General-Dynamic to an
2718 // Initial-Exec.
2720 inline void
2721 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2722 size_t relnum,
2723 Output_segment*,
2724 const elfcpp::Rela<64, false>& rela,
2725 unsigned int,
2726 elfcpp::Elf_types<64>::Elf_Addr value,
2727 unsigned char* view,
2728 elfcpp::Elf_types<64>::Elf_Addr address,
2729 section_size_type view_size)
2731 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2732 // .word 0x6666; rex64; call __tls_get_addr
2733 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2735 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2736 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2738 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2739 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2740 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2741 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2743 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2745 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2746 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2748 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2749 // We can skip it.
2750 this->skip_call_tls_get_addr_ = true;
2753 // Do a relocation in which we convert a TLS General-Dynamic to a
2754 // Local-Exec.
2756 inline void
2757 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2758 size_t relnum,
2759 Output_segment* tls_segment,
2760 const elfcpp::Rela<64, false>& rela,
2761 unsigned int,
2762 elfcpp::Elf_types<64>::Elf_Addr value,
2763 unsigned char* view,
2764 section_size_type view_size)
2766 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2767 // .word 0x6666; rex64; call __tls_get_addr
2768 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2770 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2771 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2773 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2774 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2775 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2776 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2778 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2780 value -= tls_segment->memsz();
2781 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2783 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2784 // We can skip it.
2785 this->skip_call_tls_get_addr_ = true;
2788 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2790 inline void
2791 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2792 const Relocate_info<64, false>* relinfo,
2793 size_t relnum,
2794 Output_segment*,
2795 const elfcpp::Rela<64, false>& rela,
2796 unsigned int r_type,
2797 elfcpp::Elf_types<64>::Elf_Addr value,
2798 unsigned char* view,
2799 elfcpp::Elf_types<64>::Elf_Addr address,
2800 section_size_type view_size)
2802 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2804 // leaq foo@tlsdesc(%rip), %rax
2805 // ==> movq foo@gottpoff(%rip), %rax
2806 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2807 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2808 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2809 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2810 view[-2] = 0x8b;
2811 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2812 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2814 else
2816 // call *foo@tlscall(%rax)
2817 // ==> nop; nop
2818 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2819 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2820 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2821 view[0] == 0xff && view[1] == 0x10);
2822 view[0] = 0x66;
2823 view[1] = 0x90;
2827 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2829 inline void
2830 Target_x86_64::Relocate::tls_desc_gd_to_le(
2831 const Relocate_info<64, false>* relinfo,
2832 size_t relnum,
2833 Output_segment* tls_segment,
2834 const elfcpp::Rela<64, false>& rela,
2835 unsigned int r_type,
2836 elfcpp::Elf_types<64>::Elf_Addr value,
2837 unsigned char* view,
2838 section_size_type view_size)
2840 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2842 // leaq foo@tlsdesc(%rip), %rax
2843 // ==> movq foo@tpoff, %rax
2844 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2845 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2846 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2847 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2848 view[-2] = 0xc7;
2849 view[-1] = 0xc0;
2850 value -= tls_segment->memsz();
2851 Relocate_functions<64, false>::rela32(view, value, 0);
2853 else
2855 // call *foo@tlscall(%rax)
2856 // ==> nop; nop
2857 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2858 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2859 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2860 view[0] == 0xff && view[1] == 0x10);
2861 view[0] = 0x66;
2862 view[1] = 0x90;
2866 inline void
2867 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2868 size_t relnum,
2869 Output_segment*,
2870 const elfcpp::Rela<64, false>& rela,
2871 unsigned int,
2872 elfcpp::Elf_types<64>::Elf_Addr,
2873 unsigned char* view,
2874 section_size_type view_size)
2876 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2877 // ... leq foo@dtpoff(%rax),%reg
2878 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2880 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2881 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2883 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2884 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2886 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2888 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2890 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2891 // We can skip it.
2892 this->skip_call_tls_get_addr_ = true;
2895 // Do a relocation in which we convert a TLS Initial-Exec to a
2896 // Local-Exec.
2898 inline void
2899 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2900 size_t relnum,
2901 Output_segment* tls_segment,
2902 const elfcpp::Rela<64, false>& rela,
2903 unsigned int,
2904 elfcpp::Elf_types<64>::Elf_Addr value,
2905 unsigned char* view,
2906 section_size_type view_size)
2908 // We need to examine the opcodes to figure out which instruction we
2909 // are looking at.
2911 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2912 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2914 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2915 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2917 unsigned char op1 = view[-3];
2918 unsigned char op2 = view[-2];
2919 unsigned char op3 = view[-1];
2920 unsigned char reg = op3 >> 3;
2922 if (op2 == 0x8b)
2924 // movq
2925 if (op1 == 0x4c)
2926 view[-3] = 0x49;
2927 view[-2] = 0xc7;
2928 view[-1] = 0xc0 | reg;
2930 else if (reg == 4)
2932 // Special handling for %rsp.
2933 if (op1 == 0x4c)
2934 view[-3] = 0x49;
2935 view[-2] = 0x81;
2936 view[-1] = 0xc0 | reg;
2938 else
2940 // addq
2941 if (op1 == 0x4c)
2942 view[-3] = 0x4d;
2943 view[-2] = 0x8d;
2944 view[-1] = 0x80 | reg | (reg << 3);
2947 value -= tls_segment->memsz();
2948 Relocate_functions<64, false>::rela32(view, value, 0);
2951 // Relocate section data.
2953 void
2954 Target_x86_64::relocate_section(
2955 const Relocate_info<64, false>* relinfo,
2956 unsigned int sh_type,
2957 const unsigned char* prelocs,
2958 size_t reloc_count,
2959 Output_section* output_section,
2960 bool needs_special_offset_handling,
2961 unsigned char* view,
2962 elfcpp::Elf_types<64>::Elf_Addr address,
2963 section_size_type view_size,
2964 const Reloc_symbol_changes* reloc_symbol_changes)
2966 gold_assert(sh_type == elfcpp::SHT_RELA);
2968 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2969 Target_x86_64::Relocate>(
2970 relinfo,
2971 this,
2972 prelocs,
2973 reloc_count,
2974 output_section,
2975 needs_special_offset_handling,
2976 view,
2977 address,
2978 view_size,
2979 reloc_symbol_changes);
2982 // Return the size of a relocation while scanning during a relocatable
2983 // link.
2985 unsigned int
2986 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2987 unsigned int r_type,
2988 Relobj* object)
2990 switch (r_type)
2992 case elfcpp::R_X86_64_NONE:
2993 case elfcpp::R_X86_64_GNU_VTINHERIT:
2994 case elfcpp::R_X86_64_GNU_VTENTRY:
2995 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2996 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2997 case elfcpp::R_X86_64_TLSDESC_CALL:
2998 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2999 case elfcpp::R_X86_64_DTPOFF32:
3000 case elfcpp::R_X86_64_DTPOFF64:
3001 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3002 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3003 return 0;
3005 case elfcpp::R_X86_64_64:
3006 case elfcpp::R_X86_64_PC64:
3007 case elfcpp::R_X86_64_GOTOFF64:
3008 case elfcpp::R_X86_64_GOTPC64:
3009 case elfcpp::R_X86_64_PLTOFF64:
3010 case elfcpp::R_X86_64_GOT64:
3011 case elfcpp::R_X86_64_GOTPCREL64:
3012 case elfcpp::R_X86_64_GOTPCREL:
3013 case elfcpp::R_X86_64_GOTPLT64:
3014 return 8;
3016 case elfcpp::R_X86_64_32:
3017 case elfcpp::R_X86_64_32S:
3018 case elfcpp::R_X86_64_PC32:
3019 case elfcpp::R_X86_64_PLT32:
3020 case elfcpp::R_X86_64_GOTPC32:
3021 case elfcpp::R_X86_64_GOT32:
3022 return 4;
3024 case elfcpp::R_X86_64_16:
3025 case elfcpp::R_X86_64_PC16:
3026 return 2;
3028 case elfcpp::R_X86_64_8:
3029 case elfcpp::R_X86_64_PC8:
3030 return 1;
3032 case elfcpp::R_X86_64_COPY:
3033 case elfcpp::R_X86_64_GLOB_DAT:
3034 case elfcpp::R_X86_64_JUMP_SLOT:
3035 case elfcpp::R_X86_64_RELATIVE:
3036 case elfcpp::R_X86_64_IRELATIVE:
3037 // These are outstanding tls relocs, which are unexpected when linking
3038 case elfcpp::R_X86_64_TPOFF64:
3039 case elfcpp::R_X86_64_DTPMOD64:
3040 case elfcpp::R_X86_64_TLSDESC:
3041 object->error(_("unexpected reloc %u in object file"), r_type);
3042 return 0;
3044 case elfcpp::R_X86_64_SIZE32:
3045 case elfcpp::R_X86_64_SIZE64:
3046 default:
3047 object->error(_("unsupported reloc %u against local symbol"), r_type);
3048 return 0;
3052 // Scan the relocs during a relocatable link.
3054 void
3055 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3056 Layout* layout,
3057 Sized_relobj<64, false>* object,
3058 unsigned int data_shndx,
3059 unsigned int sh_type,
3060 const unsigned char* prelocs,
3061 size_t reloc_count,
3062 Output_section* output_section,
3063 bool needs_special_offset_handling,
3064 size_t local_symbol_count,
3065 const unsigned char* plocal_symbols,
3066 Relocatable_relocs* rr)
3068 gold_assert(sh_type == elfcpp::SHT_RELA);
3070 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3071 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3073 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3074 Scan_relocatable_relocs>(
3075 symtab,
3076 layout,
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 rr);
3088 // Relocate a section during a relocatable link.
3090 void
3091 Target_x86_64::relocate_for_relocatable(
3092 const Relocate_info<64, false>* relinfo,
3093 unsigned int sh_type,
3094 const unsigned char* prelocs,
3095 size_t reloc_count,
3096 Output_section* output_section,
3097 off_t offset_in_output_section,
3098 const Relocatable_relocs* rr,
3099 unsigned char* view,
3100 elfcpp::Elf_types<64>::Elf_Addr view_address,
3101 section_size_type view_size,
3102 unsigned char* reloc_view,
3103 section_size_type reloc_view_size)
3105 gold_assert(sh_type == elfcpp::SHT_RELA);
3107 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3108 relinfo,
3109 prelocs,
3110 reloc_count,
3111 output_section,
3112 offset_in_output_section,
3114 view,
3115 view_address,
3116 view_size,
3117 reloc_view,
3118 reloc_view_size);
3121 // Return the value to use for a dynamic which requires special
3122 // treatment. This is how we support equality comparisons of function
3123 // pointers across shared library boundaries, as described in the
3124 // processor specific ABI supplement.
3126 uint64_t
3127 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3129 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3130 return this->plt_section()->address() + gsym->plt_offset();
3133 // Return a string used to fill a code section with nops to take up
3134 // the specified length.
3136 std::string
3137 Target_x86_64::do_code_fill(section_size_type length) const
3139 if (length >= 16)
3141 // Build a jmpq instruction to skip over the bytes.
3142 unsigned char jmp[5];
3143 jmp[0] = 0xe9;
3144 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3145 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3146 + std::string(length - 5, '\0'));
3149 // Nop sequences of various lengths.
3150 const char nop1[1] = { 0x90 }; // nop
3151 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3152 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3153 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3154 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3155 0x00 };
3156 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3157 0x00, 0x00 };
3158 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3159 0x00, 0x00, 0x00 };
3160 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3161 0x00, 0x00, 0x00, 0x00 };
3162 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3163 0x00, 0x00, 0x00, 0x00,
3164 0x00 };
3165 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3166 0x84, 0x00, 0x00, 0x00,
3167 0x00, 0x00 };
3168 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3169 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3170 0x00, 0x00, 0x00 };
3171 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3172 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3173 0x00, 0x00, 0x00, 0x00 };
3174 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3175 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3176 0x00, 0x00, 0x00, 0x00,
3177 0x00 };
3178 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3179 0x66, 0x2e, 0x0f, 0x1f, // data16
3180 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3181 0x00, 0x00 };
3182 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3183 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3184 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3185 0x00, 0x00, 0x00 };
3187 const char* nops[16] = {
3188 NULL,
3189 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3190 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3193 return std::string(nops[length], length);
3196 // Return the addend to use for a target specific relocation. The
3197 // only target specific relocation is R_X86_64_TLSDESC for a local
3198 // symbol. We want to set the addend is the offset of the local
3199 // symbol in the TLS segment.
3201 uint64_t
3202 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3203 uint64_t) const
3205 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3206 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3207 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3208 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3209 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3210 gold_assert(psymval->is_tls_symbol());
3211 // The value of a TLS symbol is the offset in the TLS segment.
3212 return psymval->value(ti.object, 0);
3215 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3216 // compiled with -fstack-split. The function calls non-stack-split
3217 // code. We have to change the function so that it always ensures
3218 // that it has enough stack space to run some random function.
3220 void
3221 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3222 section_offset_type fnoffset,
3223 section_size_type fnsize,
3224 unsigned char* view,
3225 section_size_type view_size,
3226 std::string* from,
3227 std::string* to) const
3229 // The function starts with a comparison of the stack pointer and a
3230 // field in the TCB. This is followed by a jump.
3232 // cmp %fs:NN,%rsp
3233 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3234 && fnsize > 9)
3236 // We will call __morestack if the carry flag is set after this
3237 // comparison. We turn the comparison into an stc instruction
3238 // and some nops.
3239 view[fnoffset] = '\xf9';
3240 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3242 // lea NN(%rsp),%r10
3243 // lea NN(%rsp),%r11
3244 else if ((this->match_view(view, view_size, fnoffset,
3245 "\x4c\x8d\x94\x24", 4)
3246 || this->match_view(view, view_size, fnoffset,
3247 "\x4c\x8d\x9c\x24", 4))
3248 && fnsize > 8)
3250 // This is loading an offset from the stack pointer for a
3251 // comparison. The offset is negative, so we decrease the
3252 // offset by the amount of space we need for the stack. This
3253 // means we will avoid calling __morestack if there happens to
3254 // be plenty of space on the stack already.
3255 unsigned char* pval = view + fnoffset + 4;
3256 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3257 val -= parameters->options().split_stack_adjust_size();
3258 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3260 else
3262 if (!object->has_no_split_stack())
3263 object->error(_("failed to match split-stack sequence at "
3264 "section %u offset %0zx"),
3265 shndx, static_cast<size_t>(fnoffset));
3266 return;
3269 // We have to change the function so that it calls
3270 // __morestack_non_split instead of __morestack. The former will
3271 // allocate additional stack space.
3272 *from = "__morestack";
3273 *to = "__morestack_non_split";
3276 // The selector for x86_64 object files.
3278 class Target_selector_x86_64 : public Target_selector_freebsd
3280 public:
3281 Target_selector_x86_64()
3282 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3283 "elf64-x86-64-freebsd")
3286 Target*
3287 do_instantiate_target()
3288 { return new Target_x86_64(); }
3292 Target_selector_x86_64 target_selector_x86_64;
3294 } // End anonymous namespace.