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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
49 // A class to handle the PLT data.
51 class Output_data_plt_x86_64 : public Output_section_data
54 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
56 Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
57 Output_data_got<64, false>* got,
58 Output_data_space* got_plt)
59 : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
60 count_(0), tlsdesc_got_offset_(-1U), free_list_()
61 { this->init(symtab, layout); }
63 Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
64 Output_data_got<64, false>* got,
65 Output_data_space* got_plt,
66 unsigned int plt_count)
67 : Output_section_data((plt_count + 1) * plt_entry_size, 8, false),
68 tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
69 count_(plt_count), tlsdesc_got_offset_(-1U), free_list_()
71 this->init(symtab, layout);
73 // Initialize the free list and reserve the first entry.
74 this->free_list_.init((plt_count + 1) * plt_entry_size, false);
75 this->free_list_.remove(0, plt_entry_size);
78 // Initialize the PLT section.
80 init(Symbol_table* symtab, Layout* layout);
82 // Add an entry to the PLT.
84 add_entry(Symbol* gsym);
86 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
88 add_local_ifunc_entry(Sized_relobj_file<64, false>* relobj,
89 unsigned int local_sym_index);
91 // Add the relocation for a PLT entry.
93 add_relocation(Symbol* gsym, unsigned int got_offset);
95 // Add the reserved TLSDESC_PLT entry to the PLT.
97 reserve_tlsdesc_entry(unsigned int got_offset)
98 { this->tlsdesc_got_offset_ = got_offset; }
100 // Return true if a TLSDESC_PLT entry has been reserved.
102 has_tlsdesc_entry() const
103 { return this->tlsdesc_got_offset_ != -1U; }
105 // Return the GOT offset for the reserved TLSDESC_PLT entry.
107 get_tlsdesc_got_offset() const
108 { return this->tlsdesc_got_offset_; }
110 // Return the offset of the reserved TLSDESC_PLT entry.
112 get_tlsdesc_plt_offset() const
113 { return (this->count_ + 1) * plt_entry_size; }
115 // Return the .rela.plt section data.
118 { return this->rel_; }
120 // Return where the TLSDESC relocations should go.
122 rela_tlsdesc(Layout*);
124 // Return the number of PLT entries.
127 { return this->count_; }
129 // Return the offset of the first non-reserved PLT entry.
131 first_plt_entry_offset()
132 { return plt_entry_size; }
134 // Return the size of a PLT entry.
137 { return plt_entry_size; }
139 // Reserve a slot in the PLT for an existing symbol in an incremental update.
141 reserve_slot(unsigned int plt_index)
143 this->free_list_.remove((plt_index + 1) * plt_entry_size,
144 (plt_index + 2) * plt_entry_size);
149 do_adjust_output_section(Output_section* os);
151 // Write to a map file.
153 do_print_to_mapfile(Mapfile* mapfile) const
154 { mapfile->print_output_data(this, _("** PLT")); }
157 // The size of an entry in the PLT.
158 static const int plt_entry_size = 16;
160 // The first entry in the PLT.
161 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
162 // procedure linkage table for both programs and shared objects."
163 static unsigned char first_plt_entry[plt_entry_size];
165 // Other entries in the PLT for an executable.
166 static unsigned char plt_entry[plt_entry_size];
168 // The reserved TLSDESC entry in the PLT for an executable.
169 static unsigned char tlsdesc_plt_entry[plt_entry_size];
171 // Set the final size.
173 set_final_data_size();
175 // Write out the PLT data.
177 do_write(Output_file*);
179 // The reloc section.
181 // The TLSDESC relocs, if necessary. These must follow the regular
183 Reloc_section* tlsdesc_rel_;
185 Output_data_got<64, false>* got_;
186 // The .got.plt section.
187 Output_data_space* got_plt_;
188 // The number of PLT entries.
190 // Offset of the reserved TLSDESC_GOT entry when needed.
191 unsigned int tlsdesc_got_offset_;
192 // List of available regions within the section, for incremental
194 Free_list free_list_;
197 // The x86_64 target class.
199 // http://www.x86-64.org/documentation/abi.pdf
200 // TLS info comes from
201 // http://people.redhat.com/drepper/tls.pdf
202 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
204 class Target_x86_64 : public Target_freebsd<64, false>
207 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
208 // uses only Elf64_Rela relocation entries with explicit addends."
209 typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
212 : Target_freebsd<64, false>(&x86_64_info),
213 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
214 global_offset_table_(NULL), rela_dyn_(NULL),
215 copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
216 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
217 tls_base_symbol_defined_(false)
220 // This function should be defined in targets that can use relocation
221 // types to determine (implemented in local_reloc_may_be_function_pointer
222 // and global_reloc_may_be_function_pointer)
223 // if a function's pointer is taken. ICF uses this in safe mode to only
224 // fold those functions whose pointer is defintely not taken. For x86_64
225 // pie binaries, safe ICF cannot be done by looking at relocation types.
227 can_check_for_function_pointers() const
228 { return !parameters->options().pie(); }
231 can_icf_inline_merge_sections () const
234 // Hook for a new output section.
236 do_new_output_section(Output_section*) const;
238 // Scan the relocations to look for symbol adjustments.
240 gc_process_relocs(Symbol_table* symtab,
242 Sized_relobj_file<64, false>* object,
243 unsigned int data_shndx,
244 unsigned int sh_type,
245 const unsigned char* prelocs,
247 Output_section* output_section,
248 bool needs_special_offset_handling,
249 size_t local_symbol_count,
250 const unsigned char* plocal_symbols);
252 // Scan the relocations to look for symbol adjustments.
254 scan_relocs(Symbol_table* symtab,
256 Sized_relobj_file<64, false>* object,
257 unsigned int data_shndx,
258 unsigned int sh_type,
259 const unsigned char* prelocs,
261 Output_section* output_section,
262 bool needs_special_offset_handling,
263 size_t local_symbol_count,
264 const unsigned char* plocal_symbols);
266 // Finalize the sections.
268 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
270 // Return the value to use for a dynamic which requires special
273 do_dynsym_value(const Symbol*) const;
275 // Relocate a section.
277 relocate_section(const Relocate_info<64, false>*,
278 unsigned int sh_type,
279 const unsigned char* prelocs,
281 Output_section* output_section,
282 bool needs_special_offset_handling,
284 elfcpp::Elf_types<64>::Elf_Addr view_address,
285 section_size_type view_size,
286 const Reloc_symbol_changes*);
288 // Scan the relocs during a relocatable link.
290 scan_relocatable_relocs(Symbol_table* symtab,
292 Sized_relobj_file<64, false>* object,
293 unsigned int data_shndx,
294 unsigned int sh_type,
295 const unsigned char* prelocs,
297 Output_section* output_section,
298 bool needs_special_offset_handling,
299 size_t local_symbol_count,
300 const unsigned char* plocal_symbols,
301 Relocatable_relocs*);
303 // Relocate a section during a relocatable link.
305 relocate_for_relocatable(const Relocate_info<64, false>*,
306 unsigned int sh_type,
307 const unsigned char* prelocs,
309 Output_section* output_section,
310 off_t offset_in_output_section,
311 const Relocatable_relocs*,
313 elfcpp::Elf_types<64>::Elf_Addr view_address,
314 section_size_type view_size,
315 unsigned char* reloc_view,
316 section_size_type reloc_view_size);
318 // Return a string used to fill a code section with nops.
320 do_code_fill(section_size_type length) const;
322 // Return whether SYM is defined by the ABI.
324 do_is_defined_by_abi(const Symbol* sym) const
325 { return strcmp(sym->name(), "__tls_get_addr") == 0; }
327 // Return the symbol index to use for a target specific relocation.
328 // The only target specific relocation is R_X86_64_TLSDESC for a
329 // local symbol, which is an absolute reloc.
331 do_reloc_symbol_index(void*, unsigned int r_type) const
333 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
337 // Return the addend to use for a target specific relocation.
339 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
341 // Return the PLT section.
343 do_plt_section_for_global(const Symbol*) const
344 { return this->plt_section(); }
347 do_plt_section_for_local(const Relobj*, unsigned int) const
348 { return this->plt_section(); }
350 // Adjust -fsplit-stack code which calls non-split-stack code.
352 do_calls_non_split(Relobj* object, unsigned int shndx,
353 section_offset_type fnoffset, section_size_type fnsize,
354 unsigned char* view, section_size_type view_size,
355 std::string* from, std::string* to) const;
357 // Return the size of the GOT section.
361 gold_assert(this->got_ != NULL);
362 return this->got_->data_size();
365 // Return the number of entries in the GOT.
367 got_entry_count() const
369 if (this->got_ == NULL)
371 return this->got_size() / 8;
374 // Return the number of entries in the PLT.
376 plt_entry_count() const;
378 // Return the offset of the first non-reserved PLT entry.
380 first_plt_entry_offset() const;
382 // Return the size of each PLT entry.
384 plt_entry_size() const;
386 // Create the GOT section for an incremental update.
387 Output_data_got<64, false>*
388 init_got_plt_for_update(Symbol_table* symtab,
390 unsigned int got_count,
391 unsigned int plt_count);
393 // Reserve a GOT entry for a local symbol, and regenerate any
394 // necessary dynamic relocations.
396 reserve_local_got_entry(unsigned int got_index,
397 Sized_relobj<64, false>* obj,
399 unsigned int got_type);
401 // Reserve a GOT entry for a global symbol, and regenerate any
402 // necessary dynamic relocations.
404 reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
405 unsigned int got_type);
407 // Register an existing PLT entry for a global symbol.
408 // A target needs to implement this to support incremental linking.
410 register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
412 // Apply an incremental relocation.
414 apply_relocation(const Relocate_info<64, false>* relinfo,
415 elfcpp::Elf_types<64>::Elf_Addr r_offset,
417 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
420 elfcpp::Elf_types<64>::Elf_Addr address,
421 section_size_type view_size);
423 // Add a new reloc argument, returning the index in the vector.
425 add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
427 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
428 return this->tlsdesc_reloc_info_.size() - 1;
432 // The class which scans relocations.
437 : issued_non_pic_error_(false)
441 get_reference_flags(unsigned int r_type);
444 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
445 Sized_relobj_file<64, false>* object,
446 unsigned int data_shndx,
447 Output_section* output_section,
448 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
449 const elfcpp::Sym<64, false>& lsym);
452 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
453 Sized_relobj_file<64, false>* object,
454 unsigned int data_shndx,
455 Output_section* output_section,
456 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
460 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
461 Target_x86_64* target,
462 Sized_relobj_file<64, false>* object,
463 unsigned int data_shndx,
464 Output_section* output_section,
465 const elfcpp::Rela<64, false>& reloc,
467 const elfcpp::Sym<64, false>& lsym);
470 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
471 Target_x86_64* target,
472 Sized_relobj_file<64, false>* object,
473 unsigned int data_shndx,
474 Output_section* output_section,
475 const elfcpp::Rela<64, false>& reloc,
481 unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
484 unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
488 check_non_pic(Relobj*, unsigned int r_type);
491 possible_function_pointer_reloc(unsigned int r_type);
494 reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
495 unsigned int r_type);
497 // Whether we have issued an error about a non-PIC compilation.
498 bool issued_non_pic_error_;
501 // The class which implements relocation.
506 : skip_call_tls_get_addr_(false)
511 if (this->skip_call_tls_get_addr_)
513 // FIXME: This needs to specify the location somehow.
514 gold_error(_("missing expected TLS relocation"));
518 // Do a relocation. Return false if the caller should not issue
519 // any warnings about this relocation.
521 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
522 size_t relnum, const elfcpp::Rela<64, false>&,
523 unsigned int r_type, const Sized_symbol<64>*,
524 const Symbol_value<64>*,
525 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
529 // Do a TLS relocation.
531 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
532 size_t relnum, const elfcpp::Rela<64, false>&,
533 unsigned int r_type, const Sized_symbol<64>*,
534 const Symbol_value<64>*,
535 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
538 // Do a TLS General-Dynamic to Initial-Exec transition.
540 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
541 Output_segment* tls_segment,
542 const elfcpp::Rela<64, false>&, unsigned int r_type,
543 elfcpp::Elf_types<64>::Elf_Addr value,
545 elfcpp::Elf_types<64>::Elf_Addr,
546 section_size_type view_size);
548 // Do a TLS General-Dynamic to Local-Exec transition.
550 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
551 Output_segment* tls_segment,
552 const elfcpp::Rela<64, false>&, unsigned int r_type,
553 elfcpp::Elf_types<64>::Elf_Addr value,
555 section_size_type view_size);
557 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
559 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
560 Output_segment* tls_segment,
561 const elfcpp::Rela<64, false>&, unsigned int r_type,
562 elfcpp::Elf_types<64>::Elf_Addr value,
564 elfcpp::Elf_types<64>::Elf_Addr,
565 section_size_type view_size);
567 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
569 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
570 Output_segment* tls_segment,
571 const elfcpp::Rela<64, false>&, unsigned int r_type,
572 elfcpp::Elf_types<64>::Elf_Addr value,
574 section_size_type view_size);
576 // Do a TLS Local-Dynamic to Local-Exec transition.
578 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
579 Output_segment* tls_segment,
580 const elfcpp::Rela<64, false>&, unsigned int r_type,
581 elfcpp::Elf_types<64>::Elf_Addr value,
583 section_size_type view_size);
585 // Do a TLS Initial-Exec to Local-Exec transition.
587 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
588 Output_segment* tls_segment,
589 const elfcpp::Rela<64, false>&, unsigned int r_type,
590 elfcpp::Elf_types<64>::Elf_Addr value,
592 section_size_type view_size);
594 // This is set if we should skip the next reloc, which should be a
595 // PLT32 reloc against ___tls_get_addr.
596 bool skip_call_tls_get_addr_;
599 // A class which returns the size required for a relocation type,
600 // used while scanning relocs during a relocatable link.
601 class Relocatable_size_for_reloc
605 get_size_for_reloc(unsigned int, Relobj*);
608 // Adjust TLS relocation type based on the options and whether this
609 // is a local symbol.
610 static tls::Tls_optimization
611 optimize_tls_reloc(bool is_final, int r_type);
613 // Get the GOT section, creating it if necessary.
614 Output_data_got<64, false>*
615 got_section(Symbol_table*, Layout*);
617 // Get the GOT PLT section.
619 got_plt_section() const
621 gold_assert(this->got_plt_ != NULL);
622 return this->got_plt_;
625 // Get the GOT section for TLSDESC entries.
626 Output_data_got<64, false>*
627 got_tlsdesc_section() const
629 gold_assert(this->got_tlsdesc_ != NULL);
630 return this->got_tlsdesc_;
633 // Create the PLT section.
635 make_plt_section(Symbol_table* symtab, Layout* layout);
637 // Create a PLT entry for a global symbol.
639 make_plt_entry(Symbol_table*, Layout*, Symbol*);
641 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
643 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
644 Sized_relobj_file<64, false>* relobj,
645 unsigned int local_sym_index);
647 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
649 define_tls_base_symbol(Symbol_table*, Layout*);
651 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
653 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
655 // Create a GOT entry for the TLS module index.
657 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
658 Sized_relobj_file<64, false>* object);
660 // Get the PLT section.
661 Output_data_plt_x86_64*
664 gold_assert(this->plt_ != NULL);
668 // Get the dynamic reloc section, creating it if necessary.
670 rela_dyn_section(Layout*);
672 // Get the section to use for TLSDESC relocations.
674 rela_tlsdesc_section(Layout*) const;
676 // Add a potential copy relocation.
678 copy_reloc(Symbol_table* symtab, Layout* layout,
679 Sized_relobj_file<64, false>* object,
680 unsigned int shndx, Output_section* output_section,
681 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
683 this->copy_relocs_.copy_reloc(symtab, layout,
684 symtab->get_sized_symbol<64>(sym),
685 object, shndx, output_section,
686 reloc, this->rela_dyn_section(layout));
689 // Information about this specific target which we pass to the
690 // general Target structure.
691 static const Target::Target_info x86_64_info;
693 // The types of GOT entries needed for this platform.
694 // These values are exposed to the ABI in an incremental link.
695 // Do not renumber existing values without changing the version
696 // number of the .gnu_incremental_inputs section.
699 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
700 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
701 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
702 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
705 // This type is used as the argument to the target specific
706 // relocation routines. The only target specific reloc is
707 // R_X86_64_TLSDESC against a local symbol.
710 Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
711 : object(a_object), r_sym(a_r_sym)
714 // The object in which the local symbol is defined.
715 Sized_relobj_file<64, false>* object;
716 // The local symbol index in the object.
721 Output_data_got<64, false>* got_;
723 Output_data_plt_x86_64* plt_;
724 // The GOT PLT section.
725 Output_data_space* got_plt_;
726 // The GOT section for TLSDESC relocations.
727 Output_data_got<64, false>* got_tlsdesc_;
728 // The _GLOBAL_OFFSET_TABLE_ symbol.
729 Symbol* global_offset_table_;
730 // The dynamic reloc section.
731 Reloc_section* rela_dyn_;
732 // Relocs saved to avoid a COPY reloc.
733 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
734 // Space for variables copied with a COPY reloc.
735 Output_data_space* dynbss_;
736 // Offset of the GOT entry for the TLS module index.
737 unsigned int got_mod_index_offset_;
738 // We handle R_X86_64_TLSDESC against a local symbol as a target
739 // specific relocation. Here we store the object and local symbol
740 // index for the relocation.
741 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
742 // True if the _TLS_MODULE_BASE_ symbol has been defined.
743 bool tls_base_symbol_defined_;
746 const Target::Target_info Target_x86_64::x86_64_info =
749 false, // is_big_endian
750 elfcpp::EM_X86_64, // machine_code
751 false, // has_make_symbol
752 false, // has_resolve
753 true, // has_code_fill
754 true, // is_default_stack_executable
756 "/lib/ld64.so.1", // program interpreter
757 0x400000, // default_text_segment_address
758 0x1000, // abi_pagesize (overridable by -z max-page-size)
759 0x1000, // common_pagesize (overridable by -z common-page-size)
760 elfcpp::SHN_UNDEF, // small_common_shndx
761 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
762 0, // small_common_section_flags
763 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
764 NULL, // attributes_section
765 NULL // attributes_vendor
768 // This is called when a new output section is created. This is where
769 // we handle the SHF_X86_64_LARGE.
772 Target_x86_64::do_new_output_section(Output_section* os) const
774 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
775 os->set_is_large_section();
778 // Get the GOT section, creating it if necessary.
780 Output_data_got<64, false>*
781 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
783 if (this->got_ == NULL)
785 gold_assert(symtab != NULL && layout != NULL);
787 this->got_ = new Output_data_got<64, false>();
789 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
791 | elfcpp::SHF_WRITE),
792 this->got_, ORDER_RELRO_LAST,
795 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
796 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
798 | elfcpp::SHF_WRITE),
799 this->got_plt_, ORDER_NON_RELRO_FIRST,
802 // The first three entries are reserved.
803 this->got_plt_->set_current_data_size(3 * 8);
805 // Those bytes can go into the relro segment.
806 layout->increase_relro(3 * 8);
808 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
809 this->global_offset_table_ =
810 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
811 Symbol_table::PREDEFINED,
813 0, 0, elfcpp::STT_OBJECT,
815 elfcpp::STV_HIDDEN, 0,
818 // If there are any TLSDESC relocations, they get GOT entries in
819 // .got.plt after the jump slot entries.
820 this->got_tlsdesc_ = new Output_data_got<64, false>();
821 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
823 | elfcpp::SHF_WRITE),
825 ORDER_NON_RELRO_FIRST, false);
831 // Get the dynamic reloc section, creating it if necessary.
833 Target_x86_64::Reloc_section*
834 Target_x86_64::rela_dyn_section(Layout* layout)
836 if (this->rela_dyn_ == NULL)
838 gold_assert(layout != NULL);
839 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
840 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
841 elfcpp::SHF_ALLOC, this->rela_dyn_,
842 ORDER_DYNAMIC_RELOCS, false);
844 return this->rela_dyn_;
847 // Initialize the PLT section.
850 Output_data_plt_x86_64::init(Symbol_table* symtab, Layout* layout)
852 this->rel_ = new Reloc_section(false);
853 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
854 elfcpp::SHF_ALLOC, this->rel_,
855 ORDER_DYNAMIC_PLT_RELOCS, false);
857 if (parameters->doing_static_link())
859 // A statically linked executable will only have a .rela.plt
860 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
861 // symbols. The library will use these symbols to locate the
862 // IRELATIVE relocs at program startup time.
863 symtab->define_in_output_data("__rela_iplt_start", NULL,
864 Symbol_table::PREDEFINED,
865 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
866 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
868 symtab->define_in_output_data("__rela_iplt_end", NULL,
869 Symbol_table::PREDEFINED,
870 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
871 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
877 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
879 os->set_entsize(plt_entry_size);
882 // Add an entry to the PLT.
885 Output_data_plt_x86_64::add_entry(Symbol* gsym)
887 gold_assert(!gsym->has_plt_offset());
889 unsigned int plt_index;
891 section_offset_type got_offset;
893 if (!this->is_data_size_valid())
895 // Note that when setting the PLT offset we skip the initial
896 // reserved PLT entry.
897 plt_index = this->count_ + 1;
898 plt_offset = plt_index * plt_entry_size;
902 got_offset = (plt_index - 1 + 3) * 8;
903 gold_assert(got_offset == this->got_plt_->current_data_size());
905 // Every PLT entry needs a GOT entry which points back to the PLT
906 // entry (this will be changed by the dynamic linker, normally
907 // lazily when the function is called).
908 this->got_plt_->set_current_data_size(got_offset + 8);
912 // For incremental updates, find an available slot.
913 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
914 if (plt_offset == -1)
915 gold_fatal(_("out of patch space (PLT);"
916 " relink with --incremental-full"));
918 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
919 // can be calculated from the PLT index, adjusting for the three
920 // reserved entries at the beginning of the GOT.
921 plt_index = plt_offset / plt_entry_size - 1;
922 got_offset = (plt_index - 1 + 3) * 8;
925 gsym->set_plt_offset(plt_offset);
927 // Every PLT entry needs a reloc.
928 this->add_relocation(gsym, got_offset);
930 // Note that we don't need to save the symbol. The contents of the
931 // PLT are independent of which symbols are used. The symbols only
932 // appear in the relocations.
935 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
939 Output_data_plt_x86_64::add_local_ifunc_entry(
940 Sized_relobj_file<64, false>* relobj,
941 unsigned int local_sym_index)
943 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
946 section_offset_type got_offset = this->got_plt_->current_data_size();
948 // Every PLT entry needs a GOT entry which points back to the PLT
950 this->got_plt_->set_current_data_size(got_offset + 8);
952 // Every PLT entry needs a reloc.
953 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
954 elfcpp::R_X86_64_IRELATIVE,
955 this->got_plt_, got_offset, 0);
960 // Add the relocation for a PLT entry.
963 Output_data_plt_x86_64::add_relocation(Symbol* gsym, unsigned int got_offset)
965 if (gsym->type() == elfcpp::STT_GNU_IFUNC
966 && gsym->can_use_relative_reloc(false))
967 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
968 this->got_plt_, got_offset, 0);
971 gsym->set_needs_dynsym_entry();
972 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
977 // Return where the TLSDESC relocations should go, creating it if
978 // necessary. These follow the JUMP_SLOT relocations.
980 Output_data_plt_x86_64::Reloc_section*
981 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
983 if (this->tlsdesc_rel_ == NULL)
985 this->tlsdesc_rel_ = new Reloc_section(false);
986 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
987 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
988 ORDER_DYNAMIC_PLT_RELOCS, false);
989 gold_assert(this->tlsdesc_rel_->output_section() ==
990 this->rel_->output_section());
992 return this->tlsdesc_rel_;
995 // Set the final size.
997 Output_data_plt_x86_64::set_final_data_size()
999 unsigned int count = this->count_;
1000 if (this->has_tlsdesc_entry())
1002 this->set_data_size((count + 1) * plt_entry_size);
1005 // The first entry in the PLT for an executable.
1007 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1009 // From AMD64 ABI Draft 0.98, page 76
1010 0xff, 0x35, // pushq contents of memory address
1011 0, 0, 0, 0, // replaced with address of .got + 8
1012 0xff, 0x25, // jmp indirect
1013 0, 0, 0, 0, // replaced with address of .got + 16
1014 0x90, 0x90, 0x90, 0x90 // noop (x4)
1017 // Subsequent entries in the PLT for an executable.
1019 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1021 // From AMD64 ABI Draft 0.98, page 76
1022 0xff, 0x25, // jmpq indirect
1023 0, 0, 0, 0, // replaced with address of symbol in .got
1024 0x68, // pushq immediate
1025 0, 0, 0, 0, // replaced with offset into relocation table
1026 0xe9, // jmpq relative
1027 0, 0, 0, 0 // replaced with offset to start of .plt
1030 // The reserved TLSDESC entry in the PLT for an executable.
1032 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1034 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1035 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1036 0xff, 0x35, // pushq x(%rip)
1037 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1038 0xff, 0x25, // jmpq *y(%rip)
1039 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1044 // Write out the PLT. This uses the hand-coded instructions above,
1045 // and adjusts them as needed. This is specified by the AMD64 ABI.
1048 Output_data_plt_x86_64::do_write(Output_file* of)
1050 const off_t offset = this->offset();
1051 const section_size_type oview_size =
1052 convert_to_section_size_type(this->data_size());
1053 unsigned char* const oview = of->get_output_view(offset, oview_size);
1055 const off_t got_file_offset = this->got_plt_->offset();
1056 const section_size_type got_size =
1057 convert_to_section_size_type(this->got_plt_->data_size());
1058 unsigned char* const got_view = of->get_output_view(got_file_offset,
1061 unsigned char* pov = oview;
1063 // The base address of the .plt section.
1064 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1065 // The base address of the .got section.
1066 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1067 // The base address of the PLT portion of the .got section,
1068 // which is where the GOT pointer will point, and where the
1069 // three reserved GOT entries are located.
1070 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1072 memcpy(pov, first_plt_entry, plt_entry_size);
1073 // We do a jmp relative to the PC at the end of this instruction.
1074 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1076 - (plt_address + 6)));
1077 elfcpp::Swap<32, false>::writeval(pov + 8,
1079 - (plt_address + 12)));
1080 pov += plt_entry_size;
1082 unsigned char* got_pov = got_view;
1084 memset(got_pov, 0, 24);
1087 unsigned int plt_offset = plt_entry_size;
1088 unsigned int got_offset = 24;
1089 const unsigned int count = this->count_;
1090 for (unsigned int plt_index = 0;
1093 pov += plt_entry_size,
1095 plt_offset += plt_entry_size,
1098 // Set and adjust the PLT entry itself.
1099 memcpy(pov, plt_entry, plt_entry_size);
1100 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1101 (got_address + got_offset
1102 - (plt_address + plt_offset
1105 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1106 elfcpp::Swap<32, false>::writeval(pov + 12,
1107 - (plt_offset + plt_entry_size));
1109 // Set the entry in the GOT.
1110 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1113 if (this->has_tlsdesc_entry())
1115 // Set and adjust the reserved TLSDESC PLT entry.
1116 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1117 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1118 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1120 - (plt_address + plt_offset
1122 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1124 + tlsdesc_got_offset
1125 - (plt_address + plt_offset
1127 pov += plt_entry_size;
1130 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1131 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1133 of->write_output_view(offset, oview_size, oview);
1134 of->write_output_view(got_file_offset, got_size, got_view);
1137 // Create the PLT section.
1140 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1142 if (this->plt_ == NULL)
1144 // Create the GOT sections first.
1145 this->got_section(symtab, layout);
1147 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1149 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1151 | elfcpp::SHF_EXECINSTR),
1152 this->plt_, ORDER_PLT, false);
1154 // Make the sh_info field of .rela.plt point to .plt.
1155 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1156 rela_plt_os->set_info_section(this->plt_->output_section());
1160 // Return the section for TLSDESC relocations.
1162 Target_x86_64::Reloc_section*
1163 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1165 return this->plt_section()->rela_tlsdesc(layout);
1168 // Create a PLT entry for a global symbol.
1171 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1174 if (gsym->has_plt_offset())
1177 if (this->plt_ == NULL)
1178 this->make_plt_section(symtab, layout);
1180 this->plt_->add_entry(gsym);
1183 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1186 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1187 Sized_relobj_file<64, false>* relobj,
1188 unsigned int local_sym_index)
1190 if (relobj->local_has_plt_offset(local_sym_index))
1192 if (this->plt_ == NULL)
1193 this->make_plt_section(symtab, layout);
1194 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1196 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1199 // Return the number of entries in the PLT.
1202 Target_x86_64::plt_entry_count() const
1204 if (this->plt_ == NULL)
1206 return this->plt_->entry_count();
1209 // Return the offset of the first non-reserved PLT entry.
1212 Target_x86_64::first_plt_entry_offset() const
1214 return Output_data_plt_x86_64::first_plt_entry_offset();
1217 // Return the size of each PLT entry.
1220 Target_x86_64::plt_entry_size() const
1222 return Output_data_plt_x86_64::get_plt_entry_size();
1225 // Create the GOT and PLT sections for an incremental update.
1227 Output_data_got<64, false>*
1228 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1230 unsigned int got_count,
1231 unsigned int plt_count)
1233 gold_assert(this->got_ == NULL);
1235 this->got_ = new Output_data_got<64, false>(got_count * 8);
1236 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1238 | elfcpp::SHF_WRITE),
1239 this->got_, ORDER_RELRO_LAST,
1242 // Add the three reserved entries.
1243 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1244 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1246 | elfcpp::SHF_WRITE),
1247 this->got_plt_, ORDER_NON_RELRO_FIRST,
1250 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1251 this->global_offset_table_ =
1252 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1253 Symbol_table::PREDEFINED,
1255 0, 0, elfcpp::STT_OBJECT,
1257 elfcpp::STV_HIDDEN, 0,
1260 // If there are any TLSDESC relocations, they get GOT entries in
1261 // .got.plt after the jump slot entries.
1262 // FIXME: Get the count for TLSDESC entries.
1263 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1264 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1265 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1267 ORDER_NON_RELRO_FIRST, false);
1269 // Create the PLT section.
1270 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1271 this->got_plt_, plt_count);
1272 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1273 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1274 this->plt_, ORDER_PLT, false);
1276 // Make the sh_info field of .rela.plt point to .plt.
1277 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1278 rela_plt_os->set_info_section(this->plt_->output_section());
1280 // Create the rela_dyn section.
1281 this->rela_dyn_section(layout);
1286 // Reserve a GOT entry for a local symbol, and regenerate any
1287 // necessary dynamic relocations.
1290 Target_x86_64::reserve_local_got_entry(
1291 unsigned int got_index,
1292 Sized_relobj<64, false>* obj,
1294 unsigned int got_type)
1296 unsigned int got_offset = got_index * 8;
1297 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1299 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1302 case GOT_TYPE_STANDARD:
1303 if (parameters->options().output_is_position_independent())
1304 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1305 this->got_, got_offset, 0);
1307 case GOT_TYPE_TLS_OFFSET:
1308 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1309 this->got_, got_offset, 0);
1311 case GOT_TYPE_TLS_PAIR:
1312 this->got_->reserve_slot(got_index + 1);
1313 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1314 this->got_, got_offset, 0);
1316 case GOT_TYPE_TLS_DESC:
1317 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1318 // this->got_->reserve_slot(got_index + 1);
1319 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1320 // this->got_, got_offset, 0);
1327 // Reserve a GOT entry for a global symbol, and regenerate any
1328 // necessary dynamic relocations.
1331 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1332 unsigned int got_type)
1334 unsigned int got_offset = got_index * 8;
1335 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1337 this->got_->reserve_global(got_index, gsym, got_type);
1340 case GOT_TYPE_STANDARD:
1341 if (!gsym->final_value_is_known())
1343 if (gsym->is_from_dynobj()
1344 || gsym->is_undefined()
1345 || gsym->is_preemptible()
1346 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1347 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1348 this->got_, got_offset, 0);
1350 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1351 this->got_, got_offset, 0);
1354 case GOT_TYPE_TLS_OFFSET:
1355 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1356 this->got_, got_offset, 0);
1358 case GOT_TYPE_TLS_PAIR:
1359 this->got_->reserve_slot(got_index + 1);
1360 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1361 this->got_, got_offset, 0);
1362 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1363 this->got_, got_offset + 8, 0);
1365 case GOT_TYPE_TLS_DESC:
1366 this->got_->reserve_slot(got_index + 1);
1367 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1368 this->got_, got_offset, 0);
1375 // Register an existing PLT entry for a global symbol.
1378 Target_x86_64::register_global_plt_entry(unsigned int plt_index,
1381 gold_assert(this->plt_ != NULL);
1382 gold_assert(!gsym->has_plt_offset());
1384 this->plt_->reserve_slot(plt_index);
1386 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1388 unsigned int got_offset = (plt_index + 3) * 8;
1389 this->plt_->add_relocation(gsym, got_offset);
1392 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1395 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1397 if (this->tls_base_symbol_defined_)
1400 Output_segment* tls_segment = layout->tls_segment();
1401 if (tls_segment != NULL)
1403 bool is_exec = parameters->options().output_is_executable();
1404 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1405 Symbol_table::PREDEFINED,
1409 elfcpp::STV_HIDDEN, 0,
1411 ? Symbol::SEGMENT_END
1412 : Symbol::SEGMENT_START),
1415 this->tls_base_symbol_defined_ = true;
1418 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1421 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1424 if (this->plt_ == NULL)
1425 this->make_plt_section(symtab, layout);
1427 if (!this->plt_->has_tlsdesc_entry())
1429 // Allocate the TLSDESC_GOT entry.
1430 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1431 unsigned int got_offset = got->add_constant(0);
1433 // Allocate the TLSDESC_PLT entry.
1434 this->plt_->reserve_tlsdesc_entry(got_offset);
1438 // Create a GOT entry for the TLS module index.
1441 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1442 Sized_relobj_file<64, false>* object)
1444 if (this->got_mod_index_offset_ == -1U)
1446 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1447 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1448 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1449 unsigned int got_offset = got->add_constant(0);
1450 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1452 got->add_constant(0);
1453 this->got_mod_index_offset_ = got_offset;
1455 return this->got_mod_index_offset_;
1458 // Optimize the TLS relocation type based on what we know about the
1459 // symbol. IS_FINAL is true if the final address of this symbol is
1460 // known at link time.
1462 tls::Tls_optimization
1463 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1465 // If we are generating a shared library, then we can't do anything
1467 if (parameters->options().shared())
1468 return tls::TLSOPT_NONE;
1472 case elfcpp::R_X86_64_TLSGD:
1473 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1474 case elfcpp::R_X86_64_TLSDESC_CALL:
1475 // These are General-Dynamic which permits fully general TLS
1476 // access. Since we know that we are generating an executable,
1477 // we can convert this to Initial-Exec. If we also know that
1478 // this is a local symbol, we can further switch to Local-Exec.
1480 return tls::TLSOPT_TO_LE;
1481 return tls::TLSOPT_TO_IE;
1483 case elfcpp::R_X86_64_TLSLD:
1484 // This is Local-Dynamic, which refers to a local symbol in the
1485 // dynamic TLS block. Since we know that we generating an
1486 // executable, we can switch to Local-Exec.
1487 return tls::TLSOPT_TO_LE;
1489 case elfcpp::R_X86_64_DTPOFF32:
1490 case elfcpp::R_X86_64_DTPOFF64:
1491 // Another Local-Dynamic reloc.
1492 return tls::TLSOPT_TO_LE;
1494 case elfcpp::R_X86_64_GOTTPOFF:
1495 // These are Initial-Exec relocs which get the thread offset
1496 // from the GOT. If we know that we are linking against the
1497 // local symbol, we can switch to Local-Exec, which links the
1498 // thread offset into the instruction.
1500 return tls::TLSOPT_TO_LE;
1501 return tls::TLSOPT_NONE;
1503 case elfcpp::R_X86_64_TPOFF32:
1504 // When we already have Local-Exec, there is nothing further we
1506 return tls::TLSOPT_NONE;
1513 // Get the Reference_flags for a particular relocation.
1516 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1520 case elfcpp::R_X86_64_NONE:
1521 case elfcpp::R_X86_64_GNU_VTINHERIT:
1522 case elfcpp::R_X86_64_GNU_VTENTRY:
1523 case elfcpp::R_X86_64_GOTPC32:
1524 case elfcpp::R_X86_64_GOTPC64:
1525 // No symbol reference.
1528 case elfcpp::R_X86_64_64:
1529 case elfcpp::R_X86_64_32:
1530 case elfcpp::R_X86_64_32S:
1531 case elfcpp::R_X86_64_16:
1532 case elfcpp::R_X86_64_8:
1533 return Symbol::ABSOLUTE_REF;
1535 case elfcpp::R_X86_64_PC64:
1536 case elfcpp::R_X86_64_PC32:
1537 case elfcpp::R_X86_64_PC16:
1538 case elfcpp::R_X86_64_PC8:
1539 case elfcpp::R_X86_64_GOTOFF64:
1540 return Symbol::RELATIVE_REF;
1542 case elfcpp::R_X86_64_PLT32:
1543 case elfcpp::R_X86_64_PLTOFF64:
1544 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1546 case elfcpp::R_X86_64_GOT64:
1547 case elfcpp::R_X86_64_GOT32:
1548 case elfcpp::R_X86_64_GOTPCREL64:
1549 case elfcpp::R_X86_64_GOTPCREL:
1550 case elfcpp::R_X86_64_GOTPLT64:
1552 return Symbol::ABSOLUTE_REF;
1554 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1555 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1556 case elfcpp::R_X86_64_TLSDESC_CALL:
1557 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1558 case elfcpp::R_X86_64_DTPOFF32:
1559 case elfcpp::R_X86_64_DTPOFF64:
1560 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1561 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1562 return Symbol::TLS_REF;
1564 case elfcpp::R_X86_64_COPY:
1565 case elfcpp::R_X86_64_GLOB_DAT:
1566 case elfcpp::R_X86_64_JUMP_SLOT:
1567 case elfcpp::R_X86_64_RELATIVE:
1568 case elfcpp::R_X86_64_IRELATIVE:
1569 case elfcpp::R_X86_64_TPOFF64:
1570 case elfcpp::R_X86_64_DTPMOD64:
1571 case elfcpp::R_X86_64_TLSDESC:
1572 case elfcpp::R_X86_64_SIZE32:
1573 case elfcpp::R_X86_64_SIZE64:
1575 // Not expected. We will give an error later.
1580 // Report an unsupported relocation against a local symbol.
1583 Target_x86_64::Scan::unsupported_reloc_local(
1584 Sized_relobj_file<64, false>* object,
1585 unsigned int r_type)
1587 gold_error(_("%s: unsupported reloc %u against local symbol"),
1588 object->name().c_str(), r_type);
1591 // We are about to emit a dynamic relocation of type R_TYPE. If the
1592 // dynamic linker does not support it, issue an error. The GNU linker
1593 // only issues a non-PIC error for an allocated read-only section.
1594 // Here we know the section is allocated, but we don't know that it is
1595 // read-only. But we check for all the relocation types which the
1596 // glibc dynamic linker supports, so it seems appropriate to issue an
1597 // error even if the section is not read-only.
1600 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1604 // These are the relocation types supported by glibc for x86_64
1605 // which should always work.
1606 case elfcpp::R_X86_64_RELATIVE:
1607 case elfcpp::R_X86_64_IRELATIVE:
1608 case elfcpp::R_X86_64_GLOB_DAT:
1609 case elfcpp::R_X86_64_JUMP_SLOT:
1610 case elfcpp::R_X86_64_DTPMOD64:
1611 case elfcpp::R_X86_64_DTPOFF64:
1612 case elfcpp::R_X86_64_TPOFF64:
1613 case elfcpp::R_X86_64_64:
1614 case elfcpp::R_X86_64_COPY:
1617 // glibc supports these reloc types, but they can overflow.
1618 case elfcpp::R_X86_64_32:
1619 case elfcpp::R_X86_64_PC32:
1620 if (this->issued_non_pic_error_)
1622 gold_assert(parameters->options().output_is_position_independent());
1623 object->error(_("requires dynamic reloc which may overflow at runtime; "
1624 "recompile with -fPIC"));
1625 this->issued_non_pic_error_ = true;
1629 // This prevents us from issuing more than one error per reloc
1630 // section. But we can still wind up issuing more than one
1631 // error per object file.
1632 if (this->issued_non_pic_error_)
1634 gold_assert(parameters->options().output_is_position_independent());
1635 object->error(_("requires unsupported dynamic reloc; "
1636 "recompile with -fPIC"));
1637 this->issued_non_pic_error_ = true;
1640 case elfcpp::R_X86_64_NONE:
1645 // Return whether we need to make a PLT entry for a relocation of the
1646 // given type against a STT_GNU_IFUNC symbol.
1649 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1650 Sized_relobj_file<64, false>* object,
1651 unsigned int r_type)
1653 int flags = Scan::get_reference_flags(r_type);
1654 if (flags & Symbol::TLS_REF)
1655 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1656 object->name().c_str(), r_type);
1660 // Scan a relocation for a local symbol.
1663 Target_x86_64::Scan::local(Symbol_table* symtab,
1665 Target_x86_64* target,
1666 Sized_relobj_file<64, false>* object,
1667 unsigned int data_shndx,
1668 Output_section* output_section,
1669 const elfcpp::Rela<64, false>& reloc,
1670 unsigned int r_type,
1671 const elfcpp::Sym<64, false>& lsym)
1673 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1674 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1675 && this->reloc_needs_plt_for_ifunc(object, r_type))
1677 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1678 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1683 case elfcpp::R_X86_64_NONE:
1684 case elfcpp::R_X86_64_GNU_VTINHERIT:
1685 case elfcpp::R_X86_64_GNU_VTENTRY:
1688 case elfcpp::R_X86_64_64:
1689 // If building a shared library (or a position-independent
1690 // executable), we need to create a dynamic relocation for this
1691 // location. The relocation applied at link time will apply the
1692 // link-time value, so we flag the location with an
1693 // R_X86_64_RELATIVE relocation so the dynamic loader can
1694 // relocate it easily.
1695 if (parameters->options().output_is_position_independent())
1697 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1698 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1699 rela_dyn->add_local_relative(object, r_sym,
1700 elfcpp::R_X86_64_RELATIVE,
1701 output_section, data_shndx,
1702 reloc.get_r_offset(),
1703 reloc.get_r_addend());
1707 case elfcpp::R_X86_64_32:
1708 case elfcpp::R_X86_64_32S:
1709 case elfcpp::R_X86_64_16:
1710 case elfcpp::R_X86_64_8:
1711 // If building a shared library (or a position-independent
1712 // executable), we need to create a dynamic relocation for this
1713 // location. We can't use an R_X86_64_RELATIVE relocation
1714 // because that is always a 64-bit relocation.
1715 if (parameters->options().output_is_position_independent())
1717 this->check_non_pic(object, r_type);
1719 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1720 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1721 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1722 rela_dyn->add_local(object, r_sym, r_type, output_section,
1723 data_shndx, reloc.get_r_offset(),
1724 reloc.get_r_addend());
1727 gold_assert(lsym.get_st_value() == 0);
1728 unsigned int shndx = lsym.get_st_shndx();
1730 shndx = object->adjust_sym_shndx(r_sym, shndx,
1733 object->error(_("section symbol %u has bad shndx %u"),
1736 rela_dyn->add_local_section(object, shndx,
1737 r_type, output_section,
1738 data_shndx, reloc.get_r_offset(),
1739 reloc.get_r_addend());
1744 case elfcpp::R_X86_64_PC64:
1745 case elfcpp::R_X86_64_PC32:
1746 case elfcpp::R_X86_64_PC16:
1747 case elfcpp::R_X86_64_PC8:
1750 case elfcpp::R_X86_64_PLT32:
1751 // Since we know this is a local symbol, we can handle this as a
1755 case elfcpp::R_X86_64_GOTPC32:
1756 case elfcpp::R_X86_64_GOTOFF64:
1757 case elfcpp::R_X86_64_GOTPC64:
1758 case elfcpp::R_X86_64_PLTOFF64:
1759 // We need a GOT section.
1760 target->got_section(symtab, layout);
1761 // For PLTOFF64, we'd normally want a PLT section, but since we
1762 // know this is a local symbol, no PLT is needed.
1765 case elfcpp::R_X86_64_GOT64:
1766 case elfcpp::R_X86_64_GOT32:
1767 case elfcpp::R_X86_64_GOTPCREL64:
1768 case elfcpp::R_X86_64_GOTPCREL:
1769 case elfcpp::R_X86_64_GOTPLT64:
1771 // The symbol requires a GOT entry.
1772 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1773 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1775 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1776 // lets function pointers compare correctly with shared
1777 // libraries. Otherwise we would need an IRELATIVE reloc.
1779 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1780 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1782 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1785 // If we are generating a shared object, we need to add a
1786 // dynamic relocation for this symbol's GOT entry.
1787 if (parameters->options().output_is_position_independent())
1789 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1790 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1791 if (r_type != elfcpp::R_X86_64_GOT32)
1793 unsigned int got_offset =
1794 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1795 rela_dyn->add_local_relative(object, r_sym,
1796 elfcpp::R_X86_64_RELATIVE,
1797 got, got_offset, 0);
1801 this->check_non_pic(object, r_type);
1803 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1804 rela_dyn->add_local(
1805 object, r_sym, r_type, got,
1806 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1810 // For GOTPLT64, we'd normally want a PLT section, but since
1811 // we know this is a local symbol, no PLT is needed.
1815 case elfcpp::R_X86_64_COPY:
1816 case elfcpp::R_X86_64_GLOB_DAT:
1817 case elfcpp::R_X86_64_JUMP_SLOT:
1818 case elfcpp::R_X86_64_RELATIVE:
1819 case elfcpp::R_X86_64_IRELATIVE:
1820 // These are outstanding tls relocs, which are unexpected when linking
1821 case elfcpp::R_X86_64_TPOFF64:
1822 case elfcpp::R_X86_64_DTPMOD64:
1823 case elfcpp::R_X86_64_TLSDESC:
1824 gold_error(_("%s: unexpected reloc %u in object file"),
1825 object->name().c_str(), r_type);
1828 // These are initial tls relocs, which are expected when linking
1829 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1830 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1831 case elfcpp::R_X86_64_TLSDESC_CALL:
1832 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1833 case elfcpp::R_X86_64_DTPOFF32:
1834 case elfcpp::R_X86_64_DTPOFF64:
1835 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1836 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1838 bool output_is_shared = parameters->options().shared();
1839 const tls::Tls_optimization optimized_type
1840 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1843 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1844 if (optimized_type == tls::TLSOPT_NONE)
1846 // Create a pair of GOT entries for the module index and
1847 // dtv-relative offset.
1848 Output_data_got<64, false>* got
1849 = target->got_section(symtab, layout);
1850 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1851 unsigned int shndx = lsym.get_st_shndx();
1853 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1855 object->error(_("local symbol %u has bad shndx %u"),
1858 got->add_local_pair_with_rela(object, r_sym,
1861 target->rela_dyn_section(layout),
1862 elfcpp::R_X86_64_DTPMOD64, 0);
1864 else if (optimized_type != tls::TLSOPT_TO_LE)
1865 unsupported_reloc_local(object, r_type);
1868 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1869 target->define_tls_base_symbol(symtab, layout);
1870 if (optimized_type == tls::TLSOPT_NONE)
1872 // Create reserved PLT and GOT entries for the resolver.
1873 target->reserve_tlsdesc_entries(symtab, layout);
1875 // Generate a double GOT entry with an
1876 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1877 // is resolved lazily, so the GOT entry needs to be in
1878 // an area in .got.plt, not .got. Call got_section to
1879 // make sure the section has been created.
1880 target->got_section(symtab, layout);
1881 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1882 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1883 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1885 unsigned int got_offset = got->add_constant(0);
1886 got->add_constant(0);
1887 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1889 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1890 // We store the arguments we need in a vector, and
1891 // use the index into the vector as the parameter
1892 // to pass to the target specific routines.
1893 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1894 void* arg = reinterpret_cast<void*>(intarg);
1895 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1896 got, got_offset, 0);
1899 else if (optimized_type != tls::TLSOPT_TO_LE)
1900 unsupported_reloc_local(object, r_type);
1903 case elfcpp::R_X86_64_TLSDESC_CALL:
1906 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1907 if (optimized_type == tls::TLSOPT_NONE)
1909 // Create a GOT entry for the module index.
1910 target->got_mod_index_entry(symtab, layout, object);
1912 else if (optimized_type != tls::TLSOPT_TO_LE)
1913 unsupported_reloc_local(object, r_type);
1916 case elfcpp::R_X86_64_DTPOFF32:
1917 case elfcpp::R_X86_64_DTPOFF64:
1920 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1921 layout->set_has_static_tls();
1922 if (optimized_type == tls::TLSOPT_NONE)
1924 // Create a GOT entry for the tp-relative offset.
1925 Output_data_got<64, false>* got
1926 = target->got_section(symtab, layout);
1927 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1928 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1929 target->rela_dyn_section(layout),
1930 elfcpp::R_X86_64_TPOFF64);
1932 else if (optimized_type != tls::TLSOPT_TO_LE)
1933 unsupported_reloc_local(object, r_type);
1936 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1937 layout->set_has_static_tls();
1938 if (output_is_shared)
1939 unsupported_reloc_local(object, r_type);
1948 case elfcpp::R_X86_64_SIZE32:
1949 case elfcpp::R_X86_64_SIZE64:
1951 gold_error(_("%s: unsupported reloc %u against local symbol"),
1952 object->name().c_str(), r_type);
1958 // Report an unsupported relocation against a global symbol.
1961 Target_x86_64::Scan::unsupported_reloc_global(
1962 Sized_relobj_file<64, false>* object,
1963 unsigned int r_type,
1966 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1967 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1970 // Returns true if this relocation type could be that of a function pointer.
1972 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1976 case elfcpp::R_X86_64_64:
1977 case elfcpp::R_X86_64_32:
1978 case elfcpp::R_X86_64_32S:
1979 case elfcpp::R_X86_64_16:
1980 case elfcpp::R_X86_64_8:
1981 case elfcpp::R_X86_64_GOT64:
1982 case elfcpp::R_X86_64_GOT32:
1983 case elfcpp::R_X86_64_GOTPCREL64:
1984 case elfcpp::R_X86_64_GOTPCREL:
1985 case elfcpp::R_X86_64_GOTPLT64:
1993 // For safe ICF, scan a relocation for a local symbol to check if it
1994 // corresponds to a function pointer being taken. In that case mark
1995 // the function whose pointer was taken as not foldable.
1998 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2002 Sized_relobj_file<64, false>* ,
2005 const elfcpp::Rela<64, false>& ,
2006 unsigned int r_type,
2007 const elfcpp::Sym<64, false>&)
2009 // When building a shared library, do not fold any local symbols as it is
2010 // not possible to distinguish pointer taken versus a call by looking at
2011 // the relocation types.
2012 return (parameters->options().shared()
2013 || possible_function_pointer_reloc(r_type));
2016 // For safe ICF, scan a relocation for a global symbol to check if it
2017 // corresponds to a function pointer being taken. In that case mark
2018 // the function whose pointer was taken as not foldable.
2021 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2025 Sized_relobj_file<64, false>* ,
2028 const elfcpp::Rela<64, false>& ,
2029 unsigned int r_type,
2032 // When building a shared library, do not fold symbols whose visibility
2033 // is hidden, internal or protected.
2034 return ((parameters->options().shared()
2035 && (gsym->visibility() == elfcpp::STV_INTERNAL
2036 || gsym->visibility() == elfcpp::STV_PROTECTED
2037 || gsym->visibility() == elfcpp::STV_HIDDEN))
2038 || possible_function_pointer_reloc(r_type));
2041 // Scan a relocation for a global symbol.
2044 Target_x86_64::Scan::global(Symbol_table* symtab,
2046 Target_x86_64* target,
2047 Sized_relobj_file<64, false>* object,
2048 unsigned int data_shndx,
2049 Output_section* output_section,
2050 const elfcpp::Rela<64, false>& reloc,
2051 unsigned int r_type,
2054 // A STT_GNU_IFUNC symbol may require a PLT entry.
2055 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2056 && this->reloc_needs_plt_for_ifunc(object, r_type))
2057 target->make_plt_entry(symtab, layout, gsym);
2061 case elfcpp::R_X86_64_NONE:
2062 case elfcpp::R_X86_64_GNU_VTINHERIT:
2063 case elfcpp::R_X86_64_GNU_VTENTRY:
2066 case elfcpp::R_X86_64_64:
2067 case elfcpp::R_X86_64_32:
2068 case elfcpp::R_X86_64_32S:
2069 case elfcpp::R_X86_64_16:
2070 case elfcpp::R_X86_64_8:
2072 // Make a PLT entry if necessary.
2073 if (gsym->needs_plt_entry())
2075 target->make_plt_entry(symtab, layout, gsym);
2076 // Since this is not a PC-relative relocation, we may be
2077 // taking the address of a function. In that case we need to
2078 // set the entry in the dynamic symbol table to the address of
2080 if (gsym->is_from_dynobj() && !parameters->options().shared())
2081 gsym->set_needs_dynsym_value();
2083 // Make a dynamic relocation if necessary.
2084 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2086 if (gsym->may_need_copy_reloc())
2088 target->copy_reloc(symtab, layout, object,
2089 data_shndx, output_section, gsym, reloc);
2091 else if (r_type == elfcpp::R_X86_64_64
2092 && gsym->type() == elfcpp::STT_GNU_IFUNC
2093 && gsym->can_use_relative_reloc(false)
2094 && !gsym->is_from_dynobj()
2095 && !gsym->is_undefined()
2096 && !gsym->is_preemptible())
2098 // Use an IRELATIVE reloc for a locally defined
2099 // STT_GNU_IFUNC symbol. This makes a function
2100 // address in a PIE executable match the address in a
2101 // shared library that it links against.
2102 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2103 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2104 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2105 output_section, object,
2107 reloc.get_r_offset(),
2108 reloc.get_r_addend());
2110 else if (r_type == elfcpp::R_X86_64_64
2111 && gsym->can_use_relative_reloc(false))
2113 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2114 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2115 output_section, object,
2117 reloc.get_r_offset(),
2118 reloc.get_r_addend());
2122 this->check_non_pic(object, r_type);
2123 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2124 rela_dyn->add_global(gsym, r_type, output_section, object,
2125 data_shndx, reloc.get_r_offset(),
2126 reloc.get_r_addend());
2132 case elfcpp::R_X86_64_PC64:
2133 case elfcpp::R_X86_64_PC32:
2134 case elfcpp::R_X86_64_PC16:
2135 case elfcpp::R_X86_64_PC8:
2137 // Make a PLT entry if necessary.
2138 if (gsym->needs_plt_entry())
2139 target->make_plt_entry(symtab, layout, gsym);
2140 // Make a dynamic relocation if necessary.
2141 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2143 if (gsym->may_need_copy_reloc())
2145 target->copy_reloc(symtab, layout, object,
2146 data_shndx, output_section, gsym, reloc);
2150 this->check_non_pic(object, r_type);
2151 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2152 rela_dyn->add_global(gsym, r_type, output_section, object,
2153 data_shndx, reloc.get_r_offset(),
2154 reloc.get_r_addend());
2160 case elfcpp::R_X86_64_GOT64:
2161 case elfcpp::R_X86_64_GOT32:
2162 case elfcpp::R_X86_64_GOTPCREL64:
2163 case elfcpp::R_X86_64_GOTPCREL:
2164 case elfcpp::R_X86_64_GOTPLT64:
2166 // The symbol requires a GOT entry.
2167 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2168 if (gsym->final_value_is_known())
2170 // For a STT_GNU_IFUNC symbol we want the PLT address.
2171 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2172 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2174 got->add_global(gsym, GOT_TYPE_STANDARD);
2178 // If this symbol is not fully resolved, we need to add a
2179 // dynamic relocation for it.
2180 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2181 if (gsym->is_from_dynobj()
2182 || gsym->is_undefined()
2183 || gsym->is_preemptible()
2184 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2185 && parameters->options().output_is_position_independent()))
2186 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2187 elfcpp::R_X86_64_GLOB_DAT);
2190 // For a STT_GNU_IFUNC symbol we want to write the PLT
2191 // offset into the GOT, so that function pointer
2192 // comparisons work correctly.
2194 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2195 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2198 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2199 // Tell the dynamic linker to use the PLT address
2200 // when resolving relocations.
2201 if (gsym->is_from_dynobj()
2202 && !parameters->options().shared())
2203 gsym->set_needs_dynsym_value();
2207 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2208 rela_dyn->add_global_relative(gsym,
2209 elfcpp::R_X86_64_RELATIVE,
2214 // For GOTPLT64, we also need a PLT entry (but only if the
2215 // symbol is not fully resolved).
2216 if (r_type == elfcpp::R_X86_64_GOTPLT64
2217 && !gsym->final_value_is_known())
2218 target->make_plt_entry(symtab, layout, gsym);
2222 case elfcpp::R_X86_64_PLT32:
2223 // If the symbol is fully resolved, this is just a PC32 reloc.
2224 // Otherwise we need a PLT entry.
2225 if (gsym->final_value_is_known())
2227 // If building a shared library, we can also skip the PLT entry
2228 // if the symbol is defined in the output file and is protected
2230 if (gsym->is_defined()
2231 && !gsym->is_from_dynobj()
2232 && !gsym->is_preemptible())
2234 target->make_plt_entry(symtab, layout, gsym);
2237 case elfcpp::R_X86_64_GOTPC32:
2238 case elfcpp::R_X86_64_GOTOFF64:
2239 case elfcpp::R_X86_64_GOTPC64:
2240 case elfcpp::R_X86_64_PLTOFF64:
2241 // We need a GOT section.
2242 target->got_section(symtab, layout);
2243 // For PLTOFF64, we also need a PLT entry (but only if the
2244 // symbol is not fully resolved).
2245 if (r_type == elfcpp::R_X86_64_PLTOFF64
2246 && !gsym->final_value_is_known())
2247 target->make_plt_entry(symtab, layout, gsym);
2250 case elfcpp::R_X86_64_COPY:
2251 case elfcpp::R_X86_64_GLOB_DAT:
2252 case elfcpp::R_X86_64_JUMP_SLOT:
2253 case elfcpp::R_X86_64_RELATIVE:
2254 case elfcpp::R_X86_64_IRELATIVE:
2255 // These are outstanding tls relocs, which are unexpected when linking
2256 case elfcpp::R_X86_64_TPOFF64:
2257 case elfcpp::R_X86_64_DTPMOD64:
2258 case elfcpp::R_X86_64_TLSDESC:
2259 gold_error(_("%s: unexpected reloc %u in object file"),
2260 object->name().c_str(), r_type);
2263 // These are initial tls relocs, which are expected for global()
2264 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2265 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2266 case elfcpp::R_X86_64_TLSDESC_CALL:
2267 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2268 case elfcpp::R_X86_64_DTPOFF32:
2269 case elfcpp::R_X86_64_DTPOFF64:
2270 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2271 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2273 const bool is_final = gsym->final_value_is_known();
2274 const tls::Tls_optimization optimized_type
2275 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2278 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2279 if (optimized_type == tls::TLSOPT_NONE)
2281 // Create a pair of GOT entries for the module index and
2282 // dtv-relative offset.
2283 Output_data_got<64, false>* got
2284 = target->got_section(symtab, layout);
2285 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2286 target->rela_dyn_section(layout),
2287 elfcpp::R_X86_64_DTPMOD64,
2288 elfcpp::R_X86_64_DTPOFF64);
2290 else if (optimized_type == tls::TLSOPT_TO_IE)
2292 // Create a GOT entry for the tp-relative offset.
2293 Output_data_got<64, false>* got
2294 = target->got_section(symtab, layout);
2295 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2296 target->rela_dyn_section(layout),
2297 elfcpp::R_X86_64_TPOFF64);
2299 else if (optimized_type != tls::TLSOPT_TO_LE)
2300 unsupported_reloc_global(object, r_type, gsym);
2303 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2304 target->define_tls_base_symbol(symtab, layout);
2305 if (optimized_type == tls::TLSOPT_NONE)
2307 // Create reserved PLT and GOT entries for the resolver.
2308 target->reserve_tlsdesc_entries(symtab, layout);
2310 // Create a double GOT entry with an R_X86_64_TLSDESC
2311 // reloc. The R_X86_64_TLSDESC reloc is resolved
2312 // lazily, so the GOT entry needs to be in an area in
2313 // .got.plt, not .got. Call got_section to make sure
2314 // the section has been created.
2315 target->got_section(symtab, layout);
2316 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2317 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2318 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2319 elfcpp::R_X86_64_TLSDESC, 0);
2321 else if (optimized_type == tls::TLSOPT_TO_IE)
2323 // Create a GOT entry for the tp-relative offset.
2324 Output_data_got<64, false>* got
2325 = target->got_section(symtab, layout);
2326 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2327 target->rela_dyn_section(layout),
2328 elfcpp::R_X86_64_TPOFF64);
2330 else if (optimized_type != tls::TLSOPT_TO_LE)
2331 unsupported_reloc_global(object, r_type, gsym);
2334 case elfcpp::R_X86_64_TLSDESC_CALL:
2337 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2338 if (optimized_type == tls::TLSOPT_NONE)
2340 // Create a GOT entry for the module index.
2341 target->got_mod_index_entry(symtab, layout, object);
2343 else if (optimized_type != tls::TLSOPT_TO_LE)
2344 unsupported_reloc_global(object, r_type, gsym);
2347 case elfcpp::R_X86_64_DTPOFF32:
2348 case elfcpp::R_X86_64_DTPOFF64:
2351 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2352 layout->set_has_static_tls();
2353 if (optimized_type == tls::TLSOPT_NONE)
2355 // Create a GOT entry for the tp-relative offset.
2356 Output_data_got<64, false>* got
2357 = target->got_section(symtab, layout);
2358 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2359 target->rela_dyn_section(layout),
2360 elfcpp::R_X86_64_TPOFF64);
2362 else if (optimized_type != tls::TLSOPT_TO_LE)
2363 unsupported_reloc_global(object, r_type, gsym);
2366 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2367 layout->set_has_static_tls();
2368 if (parameters->options().shared())
2369 unsupported_reloc_local(object, r_type);
2378 case elfcpp::R_X86_64_SIZE32:
2379 case elfcpp::R_X86_64_SIZE64:
2381 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2382 object->name().c_str(), r_type,
2383 gsym->demangled_name().c_str());
2389 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2391 Sized_relobj_file<64, false>* object,
2392 unsigned int data_shndx,
2393 unsigned int sh_type,
2394 const unsigned char* prelocs,
2396 Output_section* output_section,
2397 bool needs_special_offset_handling,
2398 size_t local_symbol_count,
2399 const unsigned char* plocal_symbols)
2402 if (sh_type == elfcpp::SHT_REL)
2407 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2408 Target_x86_64::Scan,
2409 Target_x86_64::Relocatable_size_for_reloc>(
2418 needs_special_offset_handling,
2423 // Scan relocations for a section.
2426 Target_x86_64::scan_relocs(Symbol_table* symtab,
2428 Sized_relobj_file<64, false>* object,
2429 unsigned int data_shndx,
2430 unsigned int sh_type,
2431 const unsigned char* prelocs,
2433 Output_section* output_section,
2434 bool needs_special_offset_handling,
2435 size_t local_symbol_count,
2436 const unsigned char* plocal_symbols)
2438 if (sh_type == elfcpp::SHT_REL)
2440 gold_error(_("%s: unsupported REL reloc section"),
2441 object->name().c_str());
2445 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2446 Target_x86_64::Scan>(
2455 needs_special_offset_handling,
2460 // Finalize the sections.
2463 Target_x86_64::do_finalize_sections(
2465 const Input_objects*,
2466 Symbol_table* symtab)
2468 const Reloc_section* rel_plt = (this->plt_ == NULL
2470 : this->plt_->rela_plt());
2471 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2472 this->rela_dyn_, true, false);
2474 // Fill in some more dynamic tags.
2475 Output_data_dynamic* const odyn = layout->dynamic_data();
2478 if (this->plt_ != NULL
2479 && this->plt_->output_section() != NULL
2480 && this->plt_->has_tlsdesc_entry())
2482 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2483 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2484 this->got_->finalize_data_size();
2485 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2486 this->plt_, plt_offset);
2487 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2488 this->got_, got_offset);
2492 // Emit any relocs we saved in an attempt to avoid generating COPY
2494 if (this->copy_relocs_.any_saved_relocs())
2495 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2497 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2498 // the .got.plt section.
2499 Symbol* sym = this->global_offset_table_;
2502 uint64_t data_size = this->got_plt_->current_data_size();
2503 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2507 // Perform a relocation.
2510 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2511 Target_x86_64* target,
2514 const elfcpp::Rela<64, false>& rela,
2515 unsigned int r_type,
2516 const Sized_symbol<64>* gsym,
2517 const Symbol_value<64>* psymval,
2518 unsigned char* view,
2519 elfcpp::Elf_types<64>::Elf_Addr address,
2520 section_size_type view_size)
2522 if (this->skip_call_tls_get_addr_)
2524 if ((r_type != elfcpp::R_X86_64_PLT32
2525 && r_type != elfcpp::R_X86_64_PC32)
2527 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2529 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2530 _("missing expected TLS relocation"));
2534 this->skip_call_tls_get_addr_ = false;
2539 const Sized_relobj_file<64, false>* object = relinfo->object;
2541 // Pick the value to use for symbols defined in the PLT.
2542 Symbol_value<64> symval;
2544 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2546 symval.set_output_value(target->plt_section()->address()
2547 + gsym->plt_offset());
2550 else if (gsym == NULL && psymval->is_ifunc_symbol())
2552 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2553 if (object->local_has_plt_offset(r_sym))
2555 symval.set_output_value(target->plt_section()->address()
2556 + object->local_plt_offset(r_sym));
2561 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2563 // Get the GOT offset if needed.
2564 // The GOT pointer points to the end of the GOT section.
2565 // We need to subtract the size of the GOT section to get
2566 // the actual offset to use in the relocation.
2567 bool have_got_offset = false;
2568 unsigned int got_offset = 0;
2571 case elfcpp::R_X86_64_GOT32:
2572 case elfcpp::R_X86_64_GOT64:
2573 case elfcpp::R_X86_64_GOTPLT64:
2574 case elfcpp::R_X86_64_GOTPCREL:
2575 case elfcpp::R_X86_64_GOTPCREL64:
2578 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2579 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2583 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2584 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2585 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2586 - target->got_size());
2588 have_got_offset = true;
2597 case elfcpp::R_X86_64_NONE:
2598 case elfcpp::R_X86_64_GNU_VTINHERIT:
2599 case elfcpp::R_X86_64_GNU_VTENTRY:
2602 case elfcpp::R_X86_64_64:
2603 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2606 case elfcpp::R_X86_64_PC64:
2607 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2611 case elfcpp::R_X86_64_32:
2612 // FIXME: we need to verify that value + addend fits into 32 bits:
2613 // uint64_t x = value + addend;
2614 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2615 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2616 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2619 case elfcpp::R_X86_64_32S:
2620 // FIXME: we need to verify that value + addend fits into 32 bits:
2621 // int64_t x = value + addend; // note this quantity is signed!
2622 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2623 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2626 case elfcpp::R_X86_64_PC32:
2627 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2631 case elfcpp::R_X86_64_16:
2632 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2635 case elfcpp::R_X86_64_PC16:
2636 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2640 case elfcpp::R_X86_64_8:
2641 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2644 case elfcpp::R_X86_64_PC8:
2645 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2649 case elfcpp::R_X86_64_PLT32:
2650 gold_assert(gsym == NULL
2651 || gsym->has_plt_offset()
2652 || gsym->final_value_is_known()
2653 || (gsym->is_defined()
2654 && !gsym->is_from_dynobj()
2655 && !gsym->is_preemptible()));
2656 // Note: while this code looks the same as for R_X86_64_PC32, it
2657 // behaves differently because psymval was set to point to
2658 // the PLT entry, rather than the symbol, in Scan::global().
2659 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2663 case elfcpp::R_X86_64_PLTOFF64:
2666 gold_assert(gsym->has_plt_offset()
2667 || gsym->final_value_is_known());
2668 elfcpp::Elf_types<64>::Elf_Addr got_address;
2669 got_address = target->got_section(NULL, NULL)->address();
2670 Relocate_functions<64, false>::rela64(view, object, psymval,
2671 addend - got_address);
2674 case elfcpp::R_X86_64_GOT32:
2675 gold_assert(have_got_offset);
2676 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2679 case elfcpp::R_X86_64_GOTPC32:
2682 elfcpp::Elf_types<64>::Elf_Addr value;
2683 value = target->got_plt_section()->address();
2684 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2688 case elfcpp::R_X86_64_GOT64:
2689 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2690 // Since we always add a PLT entry, this is equivalent.
2691 case elfcpp::R_X86_64_GOTPLT64:
2692 gold_assert(have_got_offset);
2693 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2696 case elfcpp::R_X86_64_GOTPC64:
2699 elfcpp::Elf_types<64>::Elf_Addr value;
2700 value = target->got_plt_section()->address();
2701 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2705 case elfcpp::R_X86_64_GOTOFF64:
2707 elfcpp::Elf_types<64>::Elf_Addr value;
2708 value = (psymval->value(object, 0)
2709 - target->got_plt_section()->address());
2710 Relocate_functions<64, false>::rela64(view, value, addend);
2714 case elfcpp::R_X86_64_GOTPCREL:
2716 gold_assert(have_got_offset);
2717 elfcpp::Elf_types<64>::Elf_Addr value;
2718 value = target->got_plt_section()->address() + got_offset;
2719 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2723 case elfcpp::R_X86_64_GOTPCREL64:
2725 gold_assert(have_got_offset);
2726 elfcpp::Elf_types<64>::Elf_Addr value;
2727 value = target->got_plt_section()->address() + got_offset;
2728 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2732 case elfcpp::R_X86_64_COPY:
2733 case elfcpp::R_X86_64_GLOB_DAT:
2734 case elfcpp::R_X86_64_JUMP_SLOT:
2735 case elfcpp::R_X86_64_RELATIVE:
2736 case elfcpp::R_X86_64_IRELATIVE:
2737 // These are outstanding tls relocs, which are unexpected when linking
2738 case elfcpp::R_X86_64_TPOFF64:
2739 case elfcpp::R_X86_64_DTPMOD64:
2740 case elfcpp::R_X86_64_TLSDESC:
2741 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2742 _("unexpected reloc %u in object file"),
2746 // These are initial tls relocs, which are expected when linking
2747 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2748 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2749 case elfcpp::R_X86_64_TLSDESC_CALL:
2750 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2751 case elfcpp::R_X86_64_DTPOFF32:
2752 case elfcpp::R_X86_64_DTPOFF64:
2753 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2754 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2755 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2756 view, address, view_size);
2759 case elfcpp::R_X86_64_SIZE32:
2760 case elfcpp::R_X86_64_SIZE64:
2762 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2763 _("unsupported reloc %u"),
2771 // Perform a TLS relocation.
2774 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2775 Target_x86_64* target,
2777 const elfcpp::Rela<64, false>& rela,
2778 unsigned int r_type,
2779 const Sized_symbol<64>* gsym,
2780 const Symbol_value<64>* psymval,
2781 unsigned char* view,
2782 elfcpp::Elf_types<64>::Elf_Addr address,
2783 section_size_type view_size)
2785 Output_segment* tls_segment = relinfo->layout->tls_segment();
2787 const Sized_relobj_file<64, false>* object = relinfo->object;
2788 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2789 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2790 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2792 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2794 const bool is_final = (gsym == NULL
2795 ? !parameters->options().shared()
2796 : gsym->final_value_is_known());
2797 tls::Tls_optimization optimized_type
2798 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2801 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2802 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2804 // If this code sequence is used in a non-executable section,
2805 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2806 // on the assumption that it's being used by itself in a debug
2807 // section. Therefore, in the unlikely event that the code
2808 // sequence appears in a non-executable section, we simply
2809 // leave it unoptimized.
2810 optimized_type = tls::TLSOPT_NONE;
2812 if (optimized_type == tls::TLSOPT_TO_LE)
2814 gold_assert(tls_segment != NULL);
2815 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2816 rela, r_type, value, view,
2822 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2823 ? GOT_TYPE_TLS_OFFSET
2824 : GOT_TYPE_TLS_PAIR);
2825 unsigned int got_offset;
2828 gold_assert(gsym->has_got_offset(got_type));
2829 got_offset = gsym->got_offset(got_type) - target->got_size();
2833 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2834 gold_assert(object->local_has_got_offset(r_sym, got_type));
2835 got_offset = (object->local_got_offset(r_sym, got_type)
2836 - target->got_size());
2838 if (optimized_type == tls::TLSOPT_TO_IE)
2840 gold_assert(tls_segment != NULL);
2841 value = target->got_plt_section()->address() + got_offset;
2842 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2843 value, view, address, view_size);
2846 else if (optimized_type == tls::TLSOPT_NONE)
2848 // Relocate the field with the offset of the pair of GOT
2850 value = target->got_plt_section()->address() + got_offset;
2851 Relocate_functions<64, false>::pcrela32(view, value, addend,
2856 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2857 _("unsupported reloc %u"), r_type);
2860 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2861 case elfcpp::R_X86_64_TLSDESC_CALL:
2862 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2864 // See above comment for R_X86_64_TLSGD.
2865 optimized_type = tls::TLSOPT_NONE;
2867 if (optimized_type == tls::TLSOPT_TO_LE)
2869 gold_assert(tls_segment != NULL);
2870 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2871 rela, r_type, value, view,
2877 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2878 ? GOT_TYPE_TLS_OFFSET
2879 : GOT_TYPE_TLS_DESC);
2880 unsigned int got_offset = 0;
2881 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2882 && optimized_type == tls::TLSOPT_NONE)
2884 // We created GOT entries in the .got.tlsdesc portion of
2885 // the .got.plt section, but the offset stored in the
2886 // symbol is the offset within .got.tlsdesc.
2887 got_offset = (target->got_size()
2888 + target->got_plt_section()->data_size());
2892 gold_assert(gsym->has_got_offset(got_type));
2893 got_offset += gsym->got_offset(got_type) - target->got_size();
2897 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2898 gold_assert(object->local_has_got_offset(r_sym, got_type));
2899 got_offset += (object->local_got_offset(r_sym, got_type)
2900 - target->got_size());
2902 if (optimized_type == tls::TLSOPT_TO_IE)
2904 gold_assert(tls_segment != NULL);
2905 value = target->got_plt_section()->address() + got_offset;
2906 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2907 rela, r_type, value, view, address,
2911 else if (optimized_type == tls::TLSOPT_NONE)
2913 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2915 // Relocate the field with the offset of the pair of GOT
2917 value = target->got_plt_section()->address() + got_offset;
2918 Relocate_functions<64, false>::pcrela32(view, value, addend,
2924 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2925 _("unsupported reloc %u"), r_type);
2928 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2929 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2931 // See above comment for R_X86_64_TLSGD.
2932 optimized_type = tls::TLSOPT_NONE;
2934 if (optimized_type == tls::TLSOPT_TO_LE)
2936 gold_assert(tls_segment != NULL);
2937 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2938 value, view, view_size);
2941 else if (optimized_type == tls::TLSOPT_NONE)
2943 // Relocate the field with the offset of the GOT entry for
2944 // the module index.
2945 unsigned int got_offset;
2946 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2947 - target->got_size());
2948 value = target->got_plt_section()->address() + got_offset;
2949 Relocate_functions<64, false>::pcrela32(view, value, addend,
2953 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2954 _("unsupported reloc %u"), r_type);
2957 case elfcpp::R_X86_64_DTPOFF32:
2958 // This relocation type is used in debugging information.
2959 // In that case we need to not optimize the value. If the
2960 // section is not executable, then we assume we should not
2961 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2962 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2964 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2966 gold_assert(tls_segment != NULL);
2967 value -= tls_segment->memsz();
2969 Relocate_functions<64, false>::rela32(view, value, addend);
2972 case elfcpp::R_X86_64_DTPOFF64:
2973 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2974 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2976 gold_assert(tls_segment != NULL);
2977 value -= tls_segment->memsz();
2979 Relocate_functions<64, false>::rela64(view, value, addend);
2982 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2983 if (optimized_type == tls::TLSOPT_TO_LE)
2985 gold_assert(tls_segment != NULL);
2986 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2987 rela, r_type, value, view,
2991 else if (optimized_type == tls::TLSOPT_NONE)
2993 // Relocate the field with the offset of the GOT entry for
2994 // the tp-relative offset of the symbol.
2995 unsigned int got_offset;
2998 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2999 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3000 - target->got_size());
3004 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3005 gold_assert(object->local_has_got_offset(r_sym,
3006 GOT_TYPE_TLS_OFFSET));
3007 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3008 - target->got_size());
3010 value = target->got_plt_section()->address() + got_offset;
3011 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3014 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3015 _("unsupported reloc type %u"),
3019 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3020 value -= tls_segment->memsz();
3021 Relocate_functions<64, false>::rela32(view, value, addend);
3026 // Do a relocation in which we convert a TLS General-Dynamic to an
3030 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3033 const elfcpp::Rela<64, false>& rela,
3035 elfcpp::Elf_types<64>::Elf_Addr value,
3036 unsigned char* view,
3037 elfcpp::Elf_types<64>::Elf_Addr address,
3038 section_size_type view_size)
3040 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3041 // .word 0x6666; rex64; call __tls_get_addr
3042 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3044 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3045 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3047 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3048 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3049 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3050 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3052 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3054 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3055 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3057 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3059 this->skip_call_tls_get_addr_ = true;
3062 // Do a relocation in which we convert a TLS General-Dynamic to a
3066 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3068 Output_segment* tls_segment,
3069 const elfcpp::Rela<64, false>& rela,
3071 elfcpp::Elf_types<64>::Elf_Addr value,
3072 unsigned char* view,
3073 section_size_type view_size)
3075 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3076 // .word 0x6666; rex64; call __tls_get_addr
3077 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3079 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3080 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3082 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3083 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3084 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3085 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3087 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3089 value -= tls_segment->memsz();
3090 Relocate_functions<64, false>::rela32(view + 8, value, 0);
3092 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3094 this->skip_call_tls_get_addr_ = true;
3097 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3100 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3101 const Relocate_info<64, false>* relinfo,
3104 const elfcpp::Rela<64, false>& rela,
3105 unsigned int r_type,
3106 elfcpp::Elf_types<64>::Elf_Addr value,
3107 unsigned char* view,
3108 elfcpp::Elf_types<64>::Elf_Addr address,
3109 section_size_type view_size)
3111 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3113 // leaq foo@tlsdesc(%rip), %rax
3114 // ==> movq foo@gottpoff(%rip), %rax
3115 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3116 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3117 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3118 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3120 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3121 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3125 // call *foo@tlscall(%rax)
3127 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3128 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3129 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3130 view[0] == 0xff && view[1] == 0x10);
3136 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3139 Target_x86_64::Relocate::tls_desc_gd_to_le(
3140 const Relocate_info<64, false>* relinfo,
3142 Output_segment* tls_segment,
3143 const elfcpp::Rela<64, false>& rela,
3144 unsigned int r_type,
3145 elfcpp::Elf_types<64>::Elf_Addr value,
3146 unsigned char* view,
3147 section_size_type view_size)
3149 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3151 // leaq foo@tlsdesc(%rip), %rax
3152 // ==> movq foo@tpoff, %rax
3153 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3154 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3155 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3156 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3159 value -= tls_segment->memsz();
3160 Relocate_functions<64, false>::rela32(view, value, 0);
3164 // call *foo@tlscall(%rax)
3166 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3167 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3168 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3169 view[0] == 0xff && view[1] == 0x10);
3176 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3179 const elfcpp::Rela<64, false>& rela,
3181 elfcpp::Elf_types<64>::Elf_Addr,
3182 unsigned char* view,
3183 section_size_type view_size)
3185 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3186 // ... leq foo@dtpoff(%rax),%reg
3187 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3189 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3190 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3192 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3193 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3195 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3197 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3199 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3201 this->skip_call_tls_get_addr_ = true;
3204 // Do a relocation in which we convert a TLS Initial-Exec to a
3208 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3210 Output_segment* tls_segment,
3211 const elfcpp::Rela<64, false>& rela,
3213 elfcpp::Elf_types<64>::Elf_Addr value,
3214 unsigned char* view,
3215 section_size_type view_size)
3217 // We need to examine the opcodes to figure out which instruction we
3220 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3221 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3223 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3224 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3226 unsigned char op1 = view[-3];
3227 unsigned char op2 = view[-2];
3228 unsigned char op3 = view[-1];
3229 unsigned char reg = op3 >> 3;
3237 view[-1] = 0xc0 | reg;
3241 // Special handling for %rsp.
3245 view[-1] = 0xc0 | reg;
3253 view[-1] = 0x80 | reg | (reg << 3);
3256 value -= tls_segment->memsz();
3257 Relocate_functions<64, false>::rela32(view, value, 0);
3260 // Relocate section data.
3263 Target_x86_64::relocate_section(
3264 const Relocate_info<64, false>* relinfo,
3265 unsigned int sh_type,
3266 const unsigned char* prelocs,
3268 Output_section* output_section,
3269 bool needs_special_offset_handling,
3270 unsigned char* view,
3271 elfcpp::Elf_types<64>::Elf_Addr address,
3272 section_size_type view_size,
3273 const Reloc_symbol_changes* reloc_symbol_changes)
3275 gold_assert(sh_type == elfcpp::SHT_RELA);
3277 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3278 Target_x86_64::Relocate>(
3284 needs_special_offset_handling,
3288 reloc_symbol_changes);
3291 // Apply an incremental relocation. Incremental relocations always refer
3292 // to global symbols.
3295 Target_x86_64::apply_relocation(
3296 const Relocate_info<64, false>* relinfo,
3297 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3298 unsigned int r_type,
3299 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3301 unsigned char* view,
3302 elfcpp::Elf_types<64>::Elf_Addr address,
3303 section_size_type view_size)
3305 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3317 // Return the size of a relocation while scanning during a relocatable
3321 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3322 unsigned int r_type,
3327 case elfcpp::R_X86_64_NONE:
3328 case elfcpp::R_X86_64_GNU_VTINHERIT:
3329 case elfcpp::R_X86_64_GNU_VTENTRY:
3330 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3331 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3332 case elfcpp::R_X86_64_TLSDESC_CALL:
3333 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3334 case elfcpp::R_X86_64_DTPOFF32:
3335 case elfcpp::R_X86_64_DTPOFF64:
3336 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3337 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3340 case elfcpp::R_X86_64_64:
3341 case elfcpp::R_X86_64_PC64:
3342 case elfcpp::R_X86_64_GOTOFF64:
3343 case elfcpp::R_X86_64_GOTPC64:
3344 case elfcpp::R_X86_64_PLTOFF64:
3345 case elfcpp::R_X86_64_GOT64:
3346 case elfcpp::R_X86_64_GOTPCREL64:
3347 case elfcpp::R_X86_64_GOTPCREL:
3348 case elfcpp::R_X86_64_GOTPLT64:
3351 case elfcpp::R_X86_64_32:
3352 case elfcpp::R_X86_64_32S:
3353 case elfcpp::R_X86_64_PC32:
3354 case elfcpp::R_X86_64_PLT32:
3355 case elfcpp::R_X86_64_GOTPC32:
3356 case elfcpp::R_X86_64_GOT32:
3359 case elfcpp::R_X86_64_16:
3360 case elfcpp::R_X86_64_PC16:
3363 case elfcpp::R_X86_64_8:
3364 case elfcpp::R_X86_64_PC8:
3367 case elfcpp::R_X86_64_COPY:
3368 case elfcpp::R_X86_64_GLOB_DAT:
3369 case elfcpp::R_X86_64_JUMP_SLOT:
3370 case elfcpp::R_X86_64_RELATIVE:
3371 case elfcpp::R_X86_64_IRELATIVE:
3372 // These are outstanding tls relocs, which are unexpected when linking
3373 case elfcpp::R_X86_64_TPOFF64:
3374 case elfcpp::R_X86_64_DTPMOD64:
3375 case elfcpp::R_X86_64_TLSDESC:
3376 object->error(_("unexpected reloc %u in object file"), r_type);
3379 case elfcpp::R_X86_64_SIZE32:
3380 case elfcpp::R_X86_64_SIZE64:
3382 object->error(_("unsupported reloc %u against local symbol"), r_type);
3387 // Scan the relocs during a relocatable link.
3390 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3392 Sized_relobj_file<64, false>* object,
3393 unsigned int data_shndx,
3394 unsigned int sh_type,
3395 const unsigned char* prelocs,
3397 Output_section* output_section,
3398 bool needs_special_offset_handling,
3399 size_t local_symbol_count,
3400 const unsigned char* plocal_symbols,
3401 Relocatable_relocs* rr)
3403 gold_assert(sh_type == elfcpp::SHT_RELA);
3405 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3406 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3408 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3409 Scan_relocatable_relocs>(
3417 needs_special_offset_handling,
3423 // Relocate a section during a relocatable link.
3426 Target_x86_64::relocate_for_relocatable(
3427 const Relocate_info<64, false>* relinfo,
3428 unsigned int sh_type,
3429 const unsigned char* prelocs,
3431 Output_section* output_section,
3432 off_t offset_in_output_section,
3433 const Relocatable_relocs* rr,
3434 unsigned char* view,
3435 elfcpp::Elf_types<64>::Elf_Addr view_address,
3436 section_size_type view_size,
3437 unsigned char* reloc_view,
3438 section_size_type reloc_view_size)
3440 gold_assert(sh_type == elfcpp::SHT_RELA);
3442 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3447 offset_in_output_section,
3456 // Return the value to use for a dynamic which requires special
3457 // treatment. This is how we support equality comparisons of function
3458 // pointers across shared library boundaries, as described in the
3459 // processor specific ABI supplement.
3462 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3464 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3465 return this->plt_section()->address() + gsym->plt_offset();
3468 // Return a string used to fill a code section with nops to take up
3469 // the specified length.
3472 Target_x86_64::do_code_fill(section_size_type length) const
3476 // Build a jmpq instruction to skip over the bytes.
3477 unsigned char jmp[5];
3479 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3480 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3481 + std::string(length - 5, '\0'));
3484 // Nop sequences of various lengths.
3485 const char nop1[1] = { 0x90 }; // nop
3486 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3487 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3488 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3489 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3491 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3493 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3495 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3496 0x00, 0x00, 0x00, 0x00 };
3497 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3498 0x00, 0x00, 0x00, 0x00,
3500 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3501 0x84, 0x00, 0x00, 0x00,
3503 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3504 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3506 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3507 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3508 0x00, 0x00, 0x00, 0x00 };
3509 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3510 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3511 0x00, 0x00, 0x00, 0x00,
3513 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3514 0x66, 0x2e, 0x0f, 0x1f, // data16
3515 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3517 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3518 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3519 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3522 const char* nops[16] = {
3524 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3525 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3528 return std::string(nops[length], length);
3531 // Return the addend to use for a target specific relocation. The
3532 // only target specific relocation is R_X86_64_TLSDESC for a local
3533 // symbol. We want to set the addend is the offset of the local
3534 // symbol in the TLS segment.
3537 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3540 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3541 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3542 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3543 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3544 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3545 gold_assert(psymval->is_tls_symbol());
3546 // The value of a TLS symbol is the offset in the TLS segment.
3547 return psymval->value(ti.object, 0);
3550 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3551 // compiled with -fsplit-stack. The function calls non-split-stack
3552 // code. We have to change the function so that it always ensures
3553 // that it has enough stack space to run some random function.
3556 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3557 section_offset_type fnoffset,
3558 section_size_type fnsize,
3559 unsigned char* view,
3560 section_size_type view_size,
3562 std::string* to) const
3564 // The function starts with a comparison of the stack pointer and a
3565 // field in the TCB. This is followed by a jump.
3568 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3571 // We will call __morestack if the carry flag is set after this
3572 // comparison. We turn the comparison into an stc instruction
3574 view[fnoffset] = '\xf9';
3575 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3577 // lea NN(%rsp),%r10
3578 // lea NN(%rsp),%r11
3579 else if ((this->match_view(view, view_size, fnoffset,
3580 "\x4c\x8d\x94\x24", 4)
3581 || this->match_view(view, view_size, fnoffset,
3582 "\x4c\x8d\x9c\x24", 4))
3585 // This is loading an offset from the stack pointer for a
3586 // comparison. The offset is negative, so we decrease the
3587 // offset by the amount of space we need for the stack. This
3588 // means we will avoid calling __morestack if there happens to
3589 // be plenty of space on the stack already.
3590 unsigned char* pval = view + fnoffset + 4;
3591 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3592 val -= parameters->options().split_stack_adjust_size();
3593 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3597 if (!object->has_no_split_stack())
3598 object->error(_("failed to match split-stack sequence at "
3599 "section %u offset %0zx"),
3600 shndx, static_cast<size_t>(fnoffset));
3604 // We have to change the function so that it calls
3605 // __morestack_non_split instead of __morestack. The former will
3606 // allocate additional stack space.
3607 *from = "__morestack";
3608 *to = "__morestack_non_split";
3611 // The selector for x86_64 object files.
3613 class Target_selector_x86_64 : public Target_selector_freebsd
3616 Target_selector_x86_64()
3617 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3618 "elf64-x86-64-freebsd")
3622 do_instantiate_target()
3623 { return new Target_x86_64(); }
3627 Target_selector_x86_64 target_selector_x86_64;
3629 } // End anonymous namespace.