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.
409 register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
411 // Force a COPY relocation for a given symbol.
413 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
415 // Apply an incremental relocation.
417 apply_relocation(const Relocate_info<64, false>* relinfo,
418 elfcpp::Elf_types<64>::Elf_Addr r_offset,
420 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
423 elfcpp::Elf_types<64>::Elf_Addr address,
424 section_size_type view_size);
426 // Add a new reloc argument, returning the index in the vector.
428 add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
430 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
431 return this->tlsdesc_reloc_info_.size() - 1;
435 // The class which scans relocations.
440 : issued_non_pic_error_(false)
444 get_reference_flags(unsigned int r_type);
447 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
448 Sized_relobj_file<64, false>* object,
449 unsigned int data_shndx,
450 Output_section* output_section,
451 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
452 const elfcpp::Sym<64, false>& lsym);
455 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
456 Sized_relobj_file<64, false>* object,
457 unsigned int data_shndx,
458 Output_section* output_section,
459 const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
463 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
464 Target_x86_64* target,
465 Sized_relobj_file<64, false>* object,
466 unsigned int data_shndx,
467 Output_section* output_section,
468 const elfcpp::Rela<64, false>& reloc,
470 const elfcpp::Sym<64, false>& lsym);
473 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
474 Target_x86_64* target,
475 Sized_relobj_file<64, false>* object,
476 unsigned int data_shndx,
477 Output_section* output_section,
478 const elfcpp::Rela<64, false>& reloc,
484 unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
487 unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
491 check_non_pic(Relobj*, unsigned int r_type);
494 possible_function_pointer_reloc(unsigned int r_type);
497 reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
498 unsigned int r_type);
500 // Whether we have issued an error about a non-PIC compilation.
501 bool issued_non_pic_error_;
504 // The class which implements relocation.
509 : skip_call_tls_get_addr_(false)
514 if (this->skip_call_tls_get_addr_)
516 // FIXME: This needs to specify the location somehow.
517 gold_error(_("missing expected TLS relocation"));
521 // Do a relocation. Return false if the caller should not issue
522 // any warnings about this relocation.
524 relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
525 size_t relnum, const elfcpp::Rela<64, false>&,
526 unsigned int r_type, const Sized_symbol<64>*,
527 const Symbol_value<64>*,
528 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
532 // Do a TLS relocation.
534 relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
535 size_t relnum, const elfcpp::Rela<64, false>&,
536 unsigned int r_type, const Sized_symbol<64>*,
537 const Symbol_value<64>*,
538 unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
541 // Do a TLS General-Dynamic to Initial-Exec transition.
543 tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
544 Output_segment* tls_segment,
545 const elfcpp::Rela<64, false>&, unsigned int r_type,
546 elfcpp::Elf_types<64>::Elf_Addr value,
548 elfcpp::Elf_types<64>::Elf_Addr,
549 section_size_type view_size);
551 // Do a TLS General-Dynamic to Local-Exec transition.
553 tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
554 Output_segment* tls_segment,
555 const elfcpp::Rela<64, false>&, unsigned int r_type,
556 elfcpp::Elf_types<64>::Elf_Addr value,
558 section_size_type view_size);
560 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
562 tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
563 Output_segment* tls_segment,
564 const elfcpp::Rela<64, false>&, unsigned int r_type,
565 elfcpp::Elf_types<64>::Elf_Addr value,
567 elfcpp::Elf_types<64>::Elf_Addr,
568 section_size_type view_size);
570 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
572 tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
573 Output_segment* tls_segment,
574 const elfcpp::Rela<64, false>&, unsigned int r_type,
575 elfcpp::Elf_types<64>::Elf_Addr value,
577 section_size_type view_size);
579 // Do a TLS Local-Dynamic to Local-Exec transition.
581 tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
582 Output_segment* tls_segment,
583 const elfcpp::Rela<64, false>&, unsigned int r_type,
584 elfcpp::Elf_types<64>::Elf_Addr value,
586 section_size_type view_size);
588 // Do a TLS Initial-Exec to Local-Exec transition.
590 tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
591 Output_segment* tls_segment,
592 const elfcpp::Rela<64, false>&, unsigned int r_type,
593 elfcpp::Elf_types<64>::Elf_Addr value,
595 section_size_type view_size);
597 // This is set if we should skip the next reloc, which should be a
598 // PLT32 reloc against ___tls_get_addr.
599 bool skip_call_tls_get_addr_;
602 // A class which returns the size required for a relocation type,
603 // used while scanning relocs during a relocatable link.
604 class Relocatable_size_for_reloc
608 get_size_for_reloc(unsigned int, Relobj*);
611 // Adjust TLS relocation type based on the options and whether this
612 // is a local symbol.
613 static tls::Tls_optimization
614 optimize_tls_reloc(bool is_final, int r_type);
616 // Get the GOT section, creating it if necessary.
617 Output_data_got<64, false>*
618 got_section(Symbol_table*, Layout*);
620 // Get the GOT PLT section.
622 got_plt_section() const
624 gold_assert(this->got_plt_ != NULL);
625 return this->got_plt_;
628 // Get the GOT section for TLSDESC entries.
629 Output_data_got<64, false>*
630 got_tlsdesc_section() const
632 gold_assert(this->got_tlsdesc_ != NULL);
633 return this->got_tlsdesc_;
636 // Create the PLT section.
638 make_plt_section(Symbol_table* symtab, Layout* layout);
640 // Create a PLT entry for a global symbol.
642 make_plt_entry(Symbol_table*, Layout*, Symbol*);
644 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
646 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
647 Sized_relobj_file<64, false>* relobj,
648 unsigned int local_sym_index);
650 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
652 define_tls_base_symbol(Symbol_table*, Layout*);
654 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
656 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
658 // Create a GOT entry for the TLS module index.
660 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
661 Sized_relobj_file<64, false>* object);
663 // Get the PLT section.
664 Output_data_plt_x86_64*
667 gold_assert(this->plt_ != NULL);
671 // Get the dynamic reloc section, creating it if necessary.
673 rela_dyn_section(Layout*);
675 // Get the section to use for TLSDESC relocations.
677 rela_tlsdesc_section(Layout*) const;
679 // Add a potential copy relocation.
681 copy_reloc(Symbol_table* symtab, Layout* layout,
682 Sized_relobj_file<64, false>* object,
683 unsigned int shndx, Output_section* output_section,
684 Symbol* sym, const elfcpp::Rela<64, false>& reloc)
686 this->copy_relocs_.copy_reloc(symtab, layout,
687 symtab->get_sized_symbol<64>(sym),
688 object, shndx, output_section,
689 reloc, this->rela_dyn_section(layout));
692 // Information about this specific target which we pass to the
693 // general Target structure.
694 static const Target::Target_info x86_64_info;
696 // The types of GOT entries needed for this platform.
697 // These values are exposed to the ABI in an incremental link.
698 // Do not renumber existing values without changing the version
699 // number of the .gnu_incremental_inputs section.
702 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
703 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset
704 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair
705 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair
708 // This type is used as the argument to the target specific
709 // relocation routines. The only target specific reloc is
710 // R_X86_64_TLSDESC against a local symbol.
713 Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
714 : object(a_object), r_sym(a_r_sym)
717 // The object in which the local symbol is defined.
718 Sized_relobj_file<64, false>* object;
719 // The local symbol index in the object.
724 Output_data_got<64, false>* got_;
726 Output_data_plt_x86_64* plt_;
727 // The GOT PLT section.
728 Output_data_space* got_plt_;
729 // The GOT section for TLSDESC relocations.
730 Output_data_got<64, false>* got_tlsdesc_;
731 // The _GLOBAL_OFFSET_TABLE_ symbol.
732 Symbol* global_offset_table_;
733 // The dynamic reloc section.
734 Reloc_section* rela_dyn_;
735 // Relocs saved to avoid a COPY reloc.
736 Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
737 // Space for variables copied with a COPY reloc.
738 Output_data_space* dynbss_;
739 // Offset of the GOT entry for the TLS module index.
740 unsigned int got_mod_index_offset_;
741 // We handle R_X86_64_TLSDESC against a local symbol as a target
742 // specific relocation. Here we store the object and local symbol
743 // index for the relocation.
744 std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
745 // True if the _TLS_MODULE_BASE_ symbol has been defined.
746 bool tls_base_symbol_defined_;
749 const Target::Target_info Target_x86_64::x86_64_info =
752 false, // is_big_endian
753 elfcpp::EM_X86_64, // machine_code
754 false, // has_make_symbol
755 false, // has_resolve
756 true, // has_code_fill
757 true, // is_default_stack_executable
759 "/lib/ld64.so.1", // program interpreter
760 0x400000, // default_text_segment_address
761 0x1000, // abi_pagesize (overridable by -z max-page-size)
762 0x1000, // common_pagesize (overridable by -z common-page-size)
763 elfcpp::SHN_UNDEF, // small_common_shndx
764 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx
765 0, // small_common_section_flags
766 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags
767 NULL, // attributes_section
768 NULL // attributes_vendor
771 // This is called when a new output section is created. This is where
772 // we handle the SHF_X86_64_LARGE.
775 Target_x86_64::do_new_output_section(Output_section* os) const
777 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
778 os->set_is_large_section();
781 // Get the GOT section, creating it if necessary.
783 Output_data_got<64, false>*
784 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
786 if (this->got_ == NULL)
788 gold_assert(symtab != NULL && layout != NULL);
790 this->got_ = new Output_data_got<64, false>();
792 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
794 | elfcpp::SHF_WRITE),
795 this->got_, ORDER_RELRO_LAST,
798 this->got_plt_ = new Output_data_space(8, "** GOT PLT");
799 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
801 | elfcpp::SHF_WRITE),
802 this->got_plt_, ORDER_NON_RELRO_FIRST,
805 // The first three entries are reserved.
806 this->got_plt_->set_current_data_size(3 * 8);
808 // Those bytes can go into the relro segment.
809 layout->increase_relro(3 * 8);
811 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
812 this->global_offset_table_ =
813 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
814 Symbol_table::PREDEFINED,
816 0, 0, elfcpp::STT_OBJECT,
818 elfcpp::STV_HIDDEN, 0,
821 // If there are any TLSDESC relocations, they get GOT entries in
822 // .got.plt after the jump slot entries.
823 this->got_tlsdesc_ = new Output_data_got<64, false>();
824 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
826 | elfcpp::SHF_WRITE),
828 ORDER_NON_RELRO_FIRST, false);
834 // Get the dynamic reloc section, creating it if necessary.
836 Target_x86_64::Reloc_section*
837 Target_x86_64::rela_dyn_section(Layout* layout)
839 if (this->rela_dyn_ == NULL)
841 gold_assert(layout != NULL);
842 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
843 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
844 elfcpp::SHF_ALLOC, this->rela_dyn_,
845 ORDER_DYNAMIC_RELOCS, false);
847 return this->rela_dyn_;
850 // Initialize the PLT section.
853 Output_data_plt_x86_64::init(Symbol_table* symtab, Layout* layout)
855 this->rel_ = new Reloc_section(false);
856 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
857 elfcpp::SHF_ALLOC, this->rel_,
858 ORDER_DYNAMIC_PLT_RELOCS, false);
860 if (parameters->doing_static_link())
862 // A statically linked executable will only have a .rela.plt
863 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
864 // symbols. The library will use these symbols to locate the
865 // IRELATIVE relocs at program startup time.
866 symtab->define_in_output_data("__rela_iplt_start", NULL,
867 Symbol_table::PREDEFINED,
868 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
869 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
871 symtab->define_in_output_data("__rela_iplt_end", NULL,
872 Symbol_table::PREDEFINED,
873 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
874 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
880 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
882 os->set_entsize(plt_entry_size);
885 // Add an entry to the PLT.
888 Output_data_plt_x86_64::add_entry(Symbol* gsym)
890 gold_assert(!gsym->has_plt_offset());
892 unsigned int plt_index;
894 section_offset_type got_offset;
896 if (!this->is_data_size_valid())
898 // Note that when setting the PLT offset we skip the initial
899 // reserved PLT entry.
900 plt_index = this->count_ + 1;
901 plt_offset = plt_index * plt_entry_size;
905 got_offset = (plt_index - 1 + 3) * 8;
906 gold_assert(got_offset == this->got_plt_->current_data_size());
908 // Every PLT entry needs a GOT entry which points back to the PLT
909 // entry (this will be changed by the dynamic linker, normally
910 // lazily when the function is called).
911 this->got_plt_->set_current_data_size(got_offset + 8);
915 // For incremental updates, find an available slot.
916 plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
917 if (plt_offset == -1)
918 gold_fallback(_("out of patch space (PLT);"
919 " relink with --incremental-full"));
921 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
922 // can be calculated from the PLT index, adjusting for the three
923 // reserved entries at the beginning of the GOT.
924 plt_index = plt_offset / plt_entry_size - 1;
925 got_offset = (plt_index - 1 + 3) * 8;
928 gsym->set_plt_offset(plt_offset);
930 // Every PLT entry needs a reloc.
931 this->add_relocation(gsym, got_offset);
933 // Note that we don't need to save the symbol. The contents of the
934 // PLT are independent of which symbols are used. The symbols only
935 // appear in the relocations.
938 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
942 Output_data_plt_x86_64::add_local_ifunc_entry(
943 Sized_relobj_file<64, false>* relobj,
944 unsigned int local_sym_index)
946 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
949 section_offset_type got_offset = this->got_plt_->current_data_size();
951 // Every PLT entry needs a GOT entry which points back to the PLT
953 this->got_plt_->set_current_data_size(got_offset + 8);
955 // Every PLT entry needs a reloc.
956 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
957 elfcpp::R_X86_64_IRELATIVE,
958 this->got_plt_, got_offset, 0);
963 // Add the relocation for a PLT entry.
966 Output_data_plt_x86_64::add_relocation(Symbol* gsym, unsigned int got_offset)
968 if (gsym->type() == elfcpp::STT_GNU_IFUNC
969 && gsym->can_use_relative_reloc(false))
970 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
971 this->got_plt_, got_offset, 0);
974 gsym->set_needs_dynsym_entry();
975 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
980 // Return where the TLSDESC relocations should go, creating it if
981 // necessary. These follow the JUMP_SLOT relocations.
983 Output_data_plt_x86_64::Reloc_section*
984 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
986 if (this->tlsdesc_rel_ == NULL)
988 this->tlsdesc_rel_ = new Reloc_section(false);
989 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
990 elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
991 ORDER_DYNAMIC_PLT_RELOCS, false);
992 gold_assert(this->tlsdesc_rel_->output_section() ==
993 this->rel_->output_section());
995 return this->tlsdesc_rel_;
998 // Set the final size.
1000 Output_data_plt_x86_64::set_final_data_size()
1002 unsigned int count = this->count_;
1003 if (this->has_tlsdesc_entry())
1005 this->set_data_size((count + 1) * plt_entry_size);
1008 // The first entry in the PLT for an executable.
1010 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1012 // From AMD64 ABI Draft 0.98, page 76
1013 0xff, 0x35, // pushq contents of memory address
1014 0, 0, 0, 0, // replaced with address of .got + 8
1015 0xff, 0x25, // jmp indirect
1016 0, 0, 0, 0, // replaced with address of .got + 16
1017 0x90, 0x90, 0x90, 0x90 // noop (x4)
1020 // Subsequent entries in the PLT for an executable.
1022 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1024 // From AMD64 ABI Draft 0.98, page 76
1025 0xff, 0x25, // jmpq indirect
1026 0, 0, 0, 0, // replaced with address of symbol in .got
1027 0x68, // pushq immediate
1028 0, 0, 0, 0, // replaced with offset into relocation table
1029 0xe9, // jmpq relative
1030 0, 0, 0, 0 // replaced with offset to start of .plt
1033 // The reserved TLSDESC entry in the PLT for an executable.
1035 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1037 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1038 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1039 0xff, 0x35, // pushq x(%rip)
1040 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1041 0xff, 0x25, // jmpq *y(%rip)
1042 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1047 // Write out the PLT. This uses the hand-coded instructions above,
1048 // and adjusts them as needed. This is specified by the AMD64 ABI.
1051 Output_data_plt_x86_64::do_write(Output_file* of)
1053 const off_t offset = this->offset();
1054 const section_size_type oview_size =
1055 convert_to_section_size_type(this->data_size());
1056 unsigned char* const oview = of->get_output_view(offset, oview_size);
1058 const off_t got_file_offset = this->got_plt_->offset();
1059 const section_size_type got_size =
1060 convert_to_section_size_type(this->got_plt_->data_size());
1061 unsigned char* const got_view = of->get_output_view(got_file_offset,
1064 unsigned char* pov = oview;
1066 // The base address of the .plt section.
1067 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1068 // The base address of the .got section.
1069 elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1070 // The base address of the PLT portion of the .got section,
1071 // which is where the GOT pointer will point, and where the
1072 // three reserved GOT entries are located.
1073 elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1075 memcpy(pov, first_plt_entry, plt_entry_size);
1076 // We do a jmp relative to the PC at the end of this instruction.
1077 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1079 - (plt_address + 6)));
1080 elfcpp::Swap<32, false>::writeval(pov + 8,
1082 - (plt_address + 12)));
1083 pov += plt_entry_size;
1085 unsigned char* got_pov = got_view;
1087 memset(got_pov, 0, 24);
1090 unsigned int plt_offset = plt_entry_size;
1091 unsigned int got_offset = 24;
1092 const unsigned int count = this->count_;
1093 for (unsigned int plt_index = 0;
1096 pov += plt_entry_size,
1098 plt_offset += plt_entry_size,
1101 // Set and adjust the PLT entry itself.
1102 memcpy(pov, plt_entry, plt_entry_size);
1103 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1104 (got_address + got_offset
1105 - (plt_address + plt_offset
1108 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1109 elfcpp::Swap<32, false>::writeval(pov + 12,
1110 - (plt_offset + plt_entry_size));
1112 // Set the entry in the GOT.
1113 elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1116 if (this->has_tlsdesc_entry())
1118 // Set and adjust the reserved TLSDESC PLT entry.
1119 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1120 memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1121 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1123 - (plt_address + plt_offset
1125 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1127 + tlsdesc_got_offset
1128 - (plt_address + plt_offset
1130 pov += plt_entry_size;
1133 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1134 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1136 of->write_output_view(offset, oview_size, oview);
1137 of->write_output_view(got_file_offset, got_size, got_view);
1140 // Create the PLT section.
1143 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1145 if (this->plt_ == NULL)
1147 // Create the GOT sections first.
1148 this->got_section(symtab, layout);
1150 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1152 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1154 | elfcpp::SHF_EXECINSTR),
1155 this->plt_, ORDER_PLT, false);
1157 // Make the sh_info field of .rela.plt point to .plt.
1158 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1159 rela_plt_os->set_info_section(this->plt_->output_section());
1163 // Return the section for TLSDESC relocations.
1165 Target_x86_64::Reloc_section*
1166 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1168 return this->plt_section()->rela_tlsdesc(layout);
1171 // Create a PLT entry for a global symbol.
1174 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1177 if (gsym->has_plt_offset())
1180 if (this->plt_ == NULL)
1181 this->make_plt_section(symtab, layout);
1183 this->plt_->add_entry(gsym);
1186 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1189 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1190 Sized_relobj_file<64, false>* relobj,
1191 unsigned int local_sym_index)
1193 if (relobj->local_has_plt_offset(local_sym_index))
1195 if (this->plt_ == NULL)
1196 this->make_plt_section(symtab, layout);
1197 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1199 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1202 // Return the number of entries in the PLT.
1205 Target_x86_64::plt_entry_count() const
1207 if (this->plt_ == NULL)
1209 return this->plt_->entry_count();
1212 // Return the offset of the first non-reserved PLT entry.
1215 Target_x86_64::first_plt_entry_offset() const
1217 return Output_data_plt_x86_64::first_plt_entry_offset();
1220 // Return the size of each PLT entry.
1223 Target_x86_64::plt_entry_size() const
1225 return Output_data_plt_x86_64::get_plt_entry_size();
1228 // Create the GOT and PLT sections for an incremental update.
1230 Output_data_got<64, false>*
1231 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1233 unsigned int got_count,
1234 unsigned int plt_count)
1236 gold_assert(this->got_ == NULL);
1238 this->got_ = new Output_data_got<64, false>(got_count * 8);
1239 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1241 | elfcpp::SHF_WRITE),
1242 this->got_, ORDER_RELRO_LAST,
1245 // Add the three reserved entries.
1246 this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1247 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1249 | elfcpp::SHF_WRITE),
1250 this->got_plt_, ORDER_NON_RELRO_FIRST,
1253 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1254 this->global_offset_table_ =
1255 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1256 Symbol_table::PREDEFINED,
1258 0, 0, elfcpp::STT_OBJECT,
1260 elfcpp::STV_HIDDEN, 0,
1263 // If there are any TLSDESC relocations, they get GOT entries in
1264 // .got.plt after the jump slot entries.
1265 // FIXME: Get the count for TLSDESC entries.
1266 this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1267 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1268 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1270 ORDER_NON_RELRO_FIRST, false);
1272 // Create the PLT section.
1273 this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1274 this->got_plt_, plt_count);
1275 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1276 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1277 this->plt_, ORDER_PLT, false);
1279 // Make the sh_info field of .rela.plt point to .plt.
1280 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1281 rela_plt_os->set_info_section(this->plt_->output_section());
1283 // Create the rela_dyn section.
1284 this->rela_dyn_section(layout);
1289 // Reserve a GOT entry for a local symbol, and regenerate any
1290 // necessary dynamic relocations.
1293 Target_x86_64::reserve_local_got_entry(
1294 unsigned int got_index,
1295 Sized_relobj<64, false>* obj,
1297 unsigned int got_type)
1299 unsigned int got_offset = got_index * 8;
1300 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1302 this->got_->reserve_local(got_index, obj, r_sym, got_type);
1305 case GOT_TYPE_STANDARD:
1306 if (parameters->options().output_is_position_independent())
1307 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1308 this->got_, got_offset, 0);
1310 case GOT_TYPE_TLS_OFFSET:
1311 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1312 this->got_, got_offset, 0);
1314 case GOT_TYPE_TLS_PAIR:
1315 this->got_->reserve_slot(got_index + 1);
1316 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1317 this->got_, got_offset, 0);
1319 case GOT_TYPE_TLS_DESC:
1320 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1321 // this->got_->reserve_slot(got_index + 1);
1322 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1323 // this->got_, got_offset, 0);
1330 // Reserve a GOT entry for a global symbol, and regenerate any
1331 // necessary dynamic relocations.
1334 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1335 unsigned int got_type)
1337 unsigned int got_offset = got_index * 8;
1338 Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1340 this->got_->reserve_global(got_index, gsym, got_type);
1343 case GOT_TYPE_STANDARD:
1344 if (!gsym->final_value_is_known())
1346 if (gsym->is_from_dynobj()
1347 || gsym->is_undefined()
1348 || gsym->is_preemptible()
1349 || gsym->type() == elfcpp::STT_GNU_IFUNC)
1350 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1351 this->got_, got_offset, 0);
1353 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1354 this->got_, got_offset, 0);
1357 case GOT_TYPE_TLS_OFFSET:
1358 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1359 this->got_, got_offset, 0);
1361 case GOT_TYPE_TLS_PAIR:
1362 this->got_->reserve_slot(got_index + 1);
1363 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1364 this->got_, got_offset, 0);
1365 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1366 this->got_, got_offset + 8, 0);
1368 case GOT_TYPE_TLS_DESC:
1369 this->got_->reserve_slot(got_index + 1);
1370 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1371 this->got_, got_offset, 0);
1378 // Register an existing PLT entry for a global symbol.
1381 Target_x86_64::register_global_plt_entry(unsigned int plt_index,
1384 gold_assert(this->plt_ != NULL);
1385 gold_assert(!gsym->has_plt_offset());
1387 this->plt_->reserve_slot(plt_index);
1389 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1391 unsigned int got_offset = (plt_index + 3) * 8;
1392 this->plt_->add_relocation(gsym, got_offset);
1395 // Force a COPY relocation for a given symbol.
1398 Target_x86_64::emit_copy_reloc(
1399 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1401 this->copy_relocs_.emit_copy_reloc(symtab,
1402 symtab->get_sized_symbol<64>(sym),
1405 this->rela_dyn_section(NULL));
1408 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1411 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1413 if (this->tls_base_symbol_defined_)
1416 Output_segment* tls_segment = layout->tls_segment();
1417 if (tls_segment != NULL)
1419 bool is_exec = parameters->options().output_is_executable();
1420 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1421 Symbol_table::PREDEFINED,
1425 elfcpp::STV_HIDDEN, 0,
1427 ? Symbol::SEGMENT_END
1428 : Symbol::SEGMENT_START),
1431 this->tls_base_symbol_defined_ = true;
1434 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1437 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1440 if (this->plt_ == NULL)
1441 this->make_plt_section(symtab, layout);
1443 if (!this->plt_->has_tlsdesc_entry())
1445 // Allocate the TLSDESC_GOT entry.
1446 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1447 unsigned int got_offset = got->add_constant(0);
1449 // Allocate the TLSDESC_PLT entry.
1450 this->plt_->reserve_tlsdesc_entry(got_offset);
1454 // Create a GOT entry for the TLS module index.
1457 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1458 Sized_relobj_file<64, false>* object)
1460 if (this->got_mod_index_offset_ == -1U)
1462 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1463 Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1464 Output_data_got<64, false>* got = this->got_section(symtab, layout);
1465 unsigned int got_offset = got->add_constant(0);
1466 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1468 got->add_constant(0);
1469 this->got_mod_index_offset_ = got_offset;
1471 return this->got_mod_index_offset_;
1474 // Optimize the TLS relocation type based on what we know about the
1475 // symbol. IS_FINAL is true if the final address of this symbol is
1476 // known at link time.
1478 tls::Tls_optimization
1479 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1481 // If we are generating a shared library, then we can't do anything
1483 if (parameters->options().shared())
1484 return tls::TLSOPT_NONE;
1488 case elfcpp::R_X86_64_TLSGD:
1489 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1490 case elfcpp::R_X86_64_TLSDESC_CALL:
1491 // These are General-Dynamic which permits fully general TLS
1492 // access. Since we know that we are generating an executable,
1493 // we can convert this to Initial-Exec. If we also know that
1494 // this is a local symbol, we can further switch to Local-Exec.
1496 return tls::TLSOPT_TO_LE;
1497 return tls::TLSOPT_TO_IE;
1499 case elfcpp::R_X86_64_TLSLD:
1500 // This is Local-Dynamic, which refers to a local symbol in the
1501 // dynamic TLS block. Since we know that we generating an
1502 // executable, we can switch to Local-Exec.
1503 return tls::TLSOPT_TO_LE;
1505 case elfcpp::R_X86_64_DTPOFF32:
1506 case elfcpp::R_X86_64_DTPOFF64:
1507 // Another Local-Dynamic reloc.
1508 return tls::TLSOPT_TO_LE;
1510 case elfcpp::R_X86_64_GOTTPOFF:
1511 // These are Initial-Exec relocs which get the thread offset
1512 // from the GOT. If we know that we are linking against the
1513 // local symbol, we can switch to Local-Exec, which links the
1514 // thread offset into the instruction.
1516 return tls::TLSOPT_TO_LE;
1517 return tls::TLSOPT_NONE;
1519 case elfcpp::R_X86_64_TPOFF32:
1520 // When we already have Local-Exec, there is nothing further we
1522 return tls::TLSOPT_NONE;
1529 // Get the Reference_flags for a particular relocation.
1532 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1536 case elfcpp::R_X86_64_NONE:
1537 case elfcpp::R_X86_64_GNU_VTINHERIT:
1538 case elfcpp::R_X86_64_GNU_VTENTRY:
1539 case elfcpp::R_X86_64_GOTPC32:
1540 case elfcpp::R_X86_64_GOTPC64:
1541 // No symbol reference.
1544 case elfcpp::R_X86_64_64:
1545 case elfcpp::R_X86_64_32:
1546 case elfcpp::R_X86_64_32S:
1547 case elfcpp::R_X86_64_16:
1548 case elfcpp::R_X86_64_8:
1549 return Symbol::ABSOLUTE_REF;
1551 case elfcpp::R_X86_64_PC64:
1552 case elfcpp::R_X86_64_PC32:
1553 case elfcpp::R_X86_64_PC16:
1554 case elfcpp::R_X86_64_PC8:
1555 case elfcpp::R_X86_64_GOTOFF64:
1556 return Symbol::RELATIVE_REF;
1558 case elfcpp::R_X86_64_PLT32:
1559 case elfcpp::R_X86_64_PLTOFF64:
1560 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1562 case elfcpp::R_X86_64_GOT64:
1563 case elfcpp::R_X86_64_GOT32:
1564 case elfcpp::R_X86_64_GOTPCREL64:
1565 case elfcpp::R_X86_64_GOTPCREL:
1566 case elfcpp::R_X86_64_GOTPLT64:
1568 return Symbol::ABSOLUTE_REF;
1570 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1571 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1572 case elfcpp::R_X86_64_TLSDESC_CALL:
1573 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1574 case elfcpp::R_X86_64_DTPOFF32:
1575 case elfcpp::R_X86_64_DTPOFF64:
1576 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1577 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1578 return Symbol::TLS_REF;
1580 case elfcpp::R_X86_64_COPY:
1581 case elfcpp::R_X86_64_GLOB_DAT:
1582 case elfcpp::R_X86_64_JUMP_SLOT:
1583 case elfcpp::R_X86_64_RELATIVE:
1584 case elfcpp::R_X86_64_IRELATIVE:
1585 case elfcpp::R_X86_64_TPOFF64:
1586 case elfcpp::R_X86_64_DTPMOD64:
1587 case elfcpp::R_X86_64_TLSDESC:
1588 case elfcpp::R_X86_64_SIZE32:
1589 case elfcpp::R_X86_64_SIZE64:
1591 // Not expected. We will give an error later.
1596 // Report an unsupported relocation against a local symbol.
1599 Target_x86_64::Scan::unsupported_reloc_local(
1600 Sized_relobj_file<64, false>* object,
1601 unsigned int r_type)
1603 gold_error(_("%s: unsupported reloc %u against local symbol"),
1604 object->name().c_str(), r_type);
1607 // We are about to emit a dynamic relocation of type R_TYPE. If the
1608 // dynamic linker does not support it, issue an error. The GNU linker
1609 // only issues a non-PIC error for an allocated read-only section.
1610 // Here we know the section is allocated, but we don't know that it is
1611 // read-only. But we check for all the relocation types which the
1612 // glibc dynamic linker supports, so it seems appropriate to issue an
1613 // error even if the section is not read-only.
1616 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1620 // These are the relocation types supported by glibc for x86_64
1621 // which should always work.
1622 case elfcpp::R_X86_64_RELATIVE:
1623 case elfcpp::R_X86_64_IRELATIVE:
1624 case elfcpp::R_X86_64_GLOB_DAT:
1625 case elfcpp::R_X86_64_JUMP_SLOT:
1626 case elfcpp::R_X86_64_DTPMOD64:
1627 case elfcpp::R_X86_64_DTPOFF64:
1628 case elfcpp::R_X86_64_TPOFF64:
1629 case elfcpp::R_X86_64_64:
1630 case elfcpp::R_X86_64_COPY:
1633 // glibc supports these reloc types, but they can overflow.
1634 case elfcpp::R_X86_64_32:
1635 case elfcpp::R_X86_64_PC32:
1636 if (this->issued_non_pic_error_)
1638 gold_assert(parameters->options().output_is_position_independent());
1639 object->error(_("requires dynamic reloc which may overflow at runtime; "
1640 "recompile with -fPIC"));
1641 this->issued_non_pic_error_ = true;
1645 // This prevents us from issuing more than one error per reloc
1646 // section. But we can still wind up issuing more than one
1647 // error per object file.
1648 if (this->issued_non_pic_error_)
1650 gold_assert(parameters->options().output_is_position_independent());
1651 object->error(_("requires unsupported dynamic reloc; "
1652 "recompile with -fPIC"));
1653 this->issued_non_pic_error_ = true;
1656 case elfcpp::R_X86_64_NONE:
1661 // Return whether we need to make a PLT entry for a relocation of the
1662 // given type against a STT_GNU_IFUNC symbol.
1665 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1666 Sized_relobj_file<64, false>* object,
1667 unsigned int r_type)
1669 int flags = Scan::get_reference_flags(r_type);
1670 if (flags & Symbol::TLS_REF)
1671 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1672 object->name().c_str(), r_type);
1676 // Scan a relocation for a local symbol.
1679 Target_x86_64::Scan::local(Symbol_table* symtab,
1681 Target_x86_64* target,
1682 Sized_relobj_file<64, false>* object,
1683 unsigned int data_shndx,
1684 Output_section* output_section,
1685 const elfcpp::Rela<64, false>& reloc,
1686 unsigned int r_type,
1687 const elfcpp::Sym<64, false>& lsym)
1689 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1690 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1691 && this->reloc_needs_plt_for_ifunc(object, r_type))
1693 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1694 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1699 case elfcpp::R_X86_64_NONE:
1700 case elfcpp::R_X86_64_GNU_VTINHERIT:
1701 case elfcpp::R_X86_64_GNU_VTENTRY:
1704 case elfcpp::R_X86_64_64:
1705 // If building a shared library (or a position-independent
1706 // executable), we need to create a dynamic relocation for this
1707 // location. The relocation applied at link time will apply the
1708 // link-time value, so we flag the location with an
1709 // R_X86_64_RELATIVE relocation so the dynamic loader can
1710 // relocate it easily.
1711 if (parameters->options().output_is_position_independent())
1713 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1714 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1715 rela_dyn->add_local_relative(object, r_sym,
1716 elfcpp::R_X86_64_RELATIVE,
1717 output_section, data_shndx,
1718 reloc.get_r_offset(),
1719 reloc.get_r_addend());
1723 case elfcpp::R_X86_64_32:
1724 case elfcpp::R_X86_64_32S:
1725 case elfcpp::R_X86_64_16:
1726 case elfcpp::R_X86_64_8:
1727 // If building a shared library (or a position-independent
1728 // executable), we need to create a dynamic relocation for this
1729 // location. We can't use an R_X86_64_RELATIVE relocation
1730 // because that is always a 64-bit relocation.
1731 if (parameters->options().output_is_position_independent())
1733 this->check_non_pic(object, r_type);
1735 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1736 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1737 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1738 rela_dyn->add_local(object, r_sym, r_type, output_section,
1739 data_shndx, reloc.get_r_offset(),
1740 reloc.get_r_addend());
1743 gold_assert(lsym.get_st_value() == 0);
1744 unsigned int shndx = lsym.get_st_shndx();
1746 shndx = object->adjust_sym_shndx(r_sym, shndx,
1749 object->error(_("section symbol %u has bad shndx %u"),
1752 rela_dyn->add_local_section(object, shndx,
1753 r_type, output_section,
1754 data_shndx, reloc.get_r_offset(),
1755 reloc.get_r_addend());
1760 case elfcpp::R_X86_64_PC64:
1761 case elfcpp::R_X86_64_PC32:
1762 case elfcpp::R_X86_64_PC16:
1763 case elfcpp::R_X86_64_PC8:
1766 case elfcpp::R_X86_64_PLT32:
1767 // Since we know this is a local symbol, we can handle this as a
1771 case elfcpp::R_X86_64_GOTPC32:
1772 case elfcpp::R_X86_64_GOTOFF64:
1773 case elfcpp::R_X86_64_GOTPC64:
1774 case elfcpp::R_X86_64_PLTOFF64:
1775 // We need a GOT section.
1776 target->got_section(symtab, layout);
1777 // For PLTOFF64, we'd normally want a PLT section, but since we
1778 // know this is a local symbol, no PLT is needed.
1781 case elfcpp::R_X86_64_GOT64:
1782 case elfcpp::R_X86_64_GOT32:
1783 case elfcpp::R_X86_64_GOTPCREL64:
1784 case elfcpp::R_X86_64_GOTPCREL:
1785 case elfcpp::R_X86_64_GOTPLT64:
1787 // The symbol requires a GOT entry.
1788 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1789 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1791 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1792 // lets function pointers compare correctly with shared
1793 // libraries. Otherwise we would need an IRELATIVE reloc.
1795 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1796 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1798 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1801 // If we are generating a shared object, we need to add a
1802 // dynamic relocation for this symbol's GOT entry.
1803 if (parameters->options().output_is_position_independent())
1805 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1806 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1807 if (r_type != elfcpp::R_X86_64_GOT32)
1809 unsigned int got_offset =
1810 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1811 rela_dyn->add_local_relative(object, r_sym,
1812 elfcpp::R_X86_64_RELATIVE,
1813 got, got_offset, 0);
1817 this->check_non_pic(object, r_type);
1819 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1820 rela_dyn->add_local(
1821 object, r_sym, r_type, got,
1822 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1826 // For GOTPLT64, we'd normally want a PLT section, but since
1827 // we know this is a local symbol, no PLT is needed.
1831 case elfcpp::R_X86_64_COPY:
1832 case elfcpp::R_X86_64_GLOB_DAT:
1833 case elfcpp::R_X86_64_JUMP_SLOT:
1834 case elfcpp::R_X86_64_RELATIVE:
1835 case elfcpp::R_X86_64_IRELATIVE:
1836 // These are outstanding tls relocs, which are unexpected when linking
1837 case elfcpp::R_X86_64_TPOFF64:
1838 case elfcpp::R_X86_64_DTPMOD64:
1839 case elfcpp::R_X86_64_TLSDESC:
1840 gold_error(_("%s: unexpected reloc %u in object file"),
1841 object->name().c_str(), r_type);
1844 // These are initial tls relocs, which are expected when linking
1845 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1846 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
1847 case elfcpp::R_X86_64_TLSDESC_CALL:
1848 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1849 case elfcpp::R_X86_64_DTPOFF32:
1850 case elfcpp::R_X86_64_DTPOFF64:
1851 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1852 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1854 bool output_is_shared = parameters->options().shared();
1855 const tls::Tls_optimization optimized_type
1856 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1859 case elfcpp::R_X86_64_TLSGD: // General-dynamic
1860 if (optimized_type == tls::TLSOPT_NONE)
1862 // Create a pair of GOT entries for the module index and
1863 // dtv-relative offset.
1864 Output_data_got<64, false>* got
1865 = target->got_section(symtab, layout);
1866 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1867 unsigned int shndx = lsym.get_st_shndx();
1869 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1871 object->error(_("local symbol %u has bad shndx %u"),
1874 got->add_local_pair_with_rela(object, r_sym,
1877 target->rela_dyn_section(layout),
1878 elfcpp::R_X86_64_DTPMOD64, 0);
1880 else if (optimized_type != tls::TLSOPT_TO_LE)
1881 unsupported_reloc_local(object, r_type);
1884 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1885 target->define_tls_base_symbol(symtab, layout);
1886 if (optimized_type == tls::TLSOPT_NONE)
1888 // Create reserved PLT and GOT entries for the resolver.
1889 target->reserve_tlsdesc_entries(symtab, layout);
1891 // Generate a double GOT entry with an
1892 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1893 // is resolved lazily, so the GOT entry needs to be in
1894 // an area in .got.plt, not .got. Call got_section to
1895 // make sure the section has been created.
1896 target->got_section(symtab, layout);
1897 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1898 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1899 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1901 unsigned int got_offset = got->add_constant(0);
1902 got->add_constant(0);
1903 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1905 Reloc_section* rt = target->rela_tlsdesc_section(layout);
1906 // We store the arguments we need in a vector, and
1907 // use the index into the vector as the parameter
1908 // to pass to the target specific routines.
1909 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1910 void* arg = reinterpret_cast<void*>(intarg);
1911 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1912 got, got_offset, 0);
1915 else if (optimized_type != tls::TLSOPT_TO_LE)
1916 unsupported_reloc_local(object, r_type);
1919 case elfcpp::R_X86_64_TLSDESC_CALL:
1922 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
1923 if (optimized_type == tls::TLSOPT_NONE)
1925 // Create a GOT entry for the module index.
1926 target->got_mod_index_entry(symtab, layout, object);
1928 else if (optimized_type != tls::TLSOPT_TO_LE)
1929 unsupported_reloc_local(object, r_type);
1932 case elfcpp::R_X86_64_DTPOFF32:
1933 case elfcpp::R_X86_64_DTPOFF64:
1936 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1937 layout->set_has_static_tls();
1938 if (optimized_type == tls::TLSOPT_NONE)
1940 // Create a GOT entry for the tp-relative offset.
1941 Output_data_got<64, false>* got
1942 = target->got_section(symtab, layout);
1943 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1944 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1945 target->rela_dyn_section(layout),
1946 elfcpp::R_X86_64_TPOFF64);
1948 else if (optimized_type != tls::TLSOPT_TO_LE)
1949 unsupported_reloc_local(object, r_type);
1952 case elfcpp::R_X86_64_TPOFF32: // Local-exec
1953 layout->set_has_static_tls();
1954 if (output_is_shared)
1955 unsupported_reloc_local(object, r_type);
1964 case elfcpp::R_X86_64_SIZE32:
1965 case elfcpp::R_X86_64_SIZE64:
1967 gold_error(_("%s: unsupported reloc %u against local symbol"),
1968 object->name().c_str(), r_type);
1974 // Report an unsupported relocation against a global symbol.
1977 Target_x86_64::Scan::unsupported_reloc_global(
1978 Sized_relobj_file<64, false>* object,
1979 unsigned int r_type,
1982 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1983 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1986 // Returns true if this relocation type could be that of a function pointer.
1988 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1992 case elfcpp::R_X86_64_64:
1993 case elfcpp::R_X86_64_32:
1994 case elfcpp::R_X86_64_32S:
1995 case elfcpp::R_X86_64_16:
1996 case elfcpp::R_X86_64_8:
1997 case elfcpp::R_X86_64_GOT64:
1998 case elfcpp::R_X86_64_GOT32:
1999 case elfcpp::R_X86_64_GOTPCREL64:
2000 case elfcpp::R_X86_64_GOTPCREL:
2001 case elfcpp::R_X86_64_GOTPLT64:
2009 // For safe ICF, scan a relocation for a local symbol to check if it
2010 // corresponds to a function pointer being taken. In that case mark
2011 // the function whose pointer was taken as not foldable.
2014 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2018 Sized_relobj_file<64, false>* ,
2021 const elfcpp::Rela<64, false>& ,
2022 unsigned int r_type,
2023 const elfcpp::Sym<64, false>&)
2025 // When building a shared library, do not fold any local symbols as it is
2026 // not possible to distinguish pointer taken versus a call by looking at
2027 // the relocation types.
2028 return (parameters->options().shared()
2029 || possible_function_pointer_reloc(r_type));
2032 // For safe ICF, scan a relocation for a global symbol to check if it
2033 // corresponds to a function pointer being taken. In that case mark
2034 // the function whose pointer was taken as not foldable.
2037 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2041 Sized_relobj_file<64, false>* ,
2044 const elfcpp::Rela<64, false>& ,
2045 unsigned int r_type,
2048 // When building a shared library, do not fold symbols whose visibility
2049 // is hidden, internal or protected.
2050 return ((parameters->options().shared()
2051 && (gsym->visibility() == elfcpp::STV_INTERNAL
2052 || gsym->visibility() == elfcpp::STV_PROTECTED
2053 || gsym->visibility() == elfcpp::STV_HIDDEN))
2054 || possible_function_pointer_reloc(r_type));
2057 // Scan a relocation for a global symbol.
2060 Target_x86_64::Scan::global(Symbol_table* symtab,
2062 Target_x86_64* target,
2063 Sized_relobj_file<64, false>* object,
2064 unsigned int data_shndx,
2065 Output_section* output_section,
2066 const elfcpp::Rela<64, false>& reloc,
2067 unsigned int r_type,
2070 // A STT_GNU_IFUNC symbol may require a PLT entry.
2071 if (gsym->type() == elfcpp::STT_GNU_IFUNC
2072 && this->reloc_needs_plt_for_ifunc(object, r_type))
2073 target->make_plt_entry(symtab, layout, gsym);
2077 case elfcpp::R_X86_64_NONE:
2078 case elfcpp::R_X86_64_GNU_VTINHERIT:
2079 case elfcpp::R_X86_64_GNU_VTENTRY:
2082 case elfcpp::R_X86_64_64:
2083 case elfcpp::R_X86_64_32:
2084 case elfcpp::R_X86_64_32S:
2085 case elfcpp::R_X86_64_16:
2086 case elfcpp::R_X86_64_8:
2088 // Make a PLT entry if necessary.
2089 if (gsym->needs_plt_entry())
2091 target->make_plt_entry(symtab, layout, gsym);
2092 // Since this is not a PC-relative relocation, we may be
2093 // taking the address of a function. In that case we need to
2094 // set the entry in the dynamic symbol table to the address of
2096 if (gsym->is_from_dynobj() && !parameters->options().shared())
2097 gsym->set_needs_dynsym_value();
2099 // Make a dynamic relocation if necessary.
2100 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2102 if (gsym->may_need_copy_reloc())
2104 target->copy_reloc(symtab, layout, object,
2105 data_shndx, output_section, gsym, reloc);
2107 else if (r_type == elfcpp::R_X86_64_64
2108 && gsym->type() == elfcpp::STT_GNU_IFUNC
2109 && gsym->can_use_relative_reloc(false)
2110 && !gsym->is_from_dynobj()
2111 && !gsym->is_undefined()
2112 && !gsym->is_preemptible())
2114 // Use an IRELATIVE reloc for a locally defined
2115 // STT_GNU_IFUNC symbol. This makes a function
2116 // address in a PIE executable match the address in a
2117 // shared library that it links against.
2118 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2119 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2120 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2121 output_section, object,
2123 reloc.get_r_offset(),
2124 reloc.get_r_addend());
2126 else if (r_type == elfcpp::R_X86_64_64
2127 && gsym->can_use_relative_reloc(false))
2129 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2130 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2131 output_section, object,
2133 reloc.get_r_offset(),
2134 reloc.get_r_addend());
2138 this->check_non_pic(object, r_type);
2139 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2140 rela_dyn->add_global(gsym, r_type, output_section, object,
2141 data_shndx, reloc.get_r_offset(),
2142 reloc.get_r_addend());
2148 case elfcpp::R_X86_64_PC64:
2149 case elfcpp::R_X86_64_PC32:
2150 case elfcpp::R_X86_64_PC16:
2151 case elfcpp::R_X86_64_PC8:
2153 // Make a PLT entry if necessary.
2154 if (gsym->needs_plt_entry())
2155 target->make_plt_entry(symtab, layout, gsym);
2156 // Make a dynamic relocation if necessary.
2157 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2159 if (gsym->may_need_copy_reloc())
2161 target->copy_reloc(symtab, layout, object,
2162 data_shndx, output_section, gsym, reloc);
2166 this->check_non_pic(object, r_type);
2167 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2168 rela_dyn->add_global(gsym, r_type, output_section, object,
2169 data_shndx, reloc.get_r_offset(),
2170 reloc.get_r_addend());
2176 case elfcpp::R_X86_64_GOT64:
2177 case elfcpp::R_X86_64_GOT32:
2178 case elfcpp::R_X86_64_GOTPCREL64:
2179 case elfcpp::R_X86_64_GOTPCREL:
2180 case elfcpp::R_X86_64_GOTPLT64:
2182 // The symbol requires a GOT entry.
2183 Output_data_got<64, false>* got = target->got_section(symtab, layout);
2184 if (gsym->final_value_is_known())
2186 // For a STT_GNU_IFUNC symbol we want the PLT address.
2187 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2188 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2190 got->add_global(gsym, GOT_TYPE_STANDARD);
2194 // If this symbol is not fully resolved, we need to add a
2195 // dynamic relocation for it.
2196 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2197 if (gsym->is_from_dynobj()
2198 || gsym->is_undefined()
2199 || gsym->is_preemptible()
2200 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2201 && parameters->options().output_is_position_independent()))
2202 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2203 elfcpp::R_X86_64_GLOB_DAT);
2206 // For a STT_GNU_IFUNC symbol we want to write the PLT
2207 // offset into the GOT, so that function pointer
2208 // comparisons work correctly.
2210 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2211 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2214 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2215 // Tell the dynamic linker to use the PLT address
2216 // when resolving relocations.
2217 if (gsym->is_from_dynobj()
2218 && !parameters->options().shared())
2219 gsym->set_needs_dynsym_value();
2223 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2224 rela_dyn->add_global_relative(gsym,
2225 elfcpp::R_X86_64_RELATIVE,
2230 // For GOTPLT64, we also need a PLT entry (but only if the
2231 // symbol is not fully resolved).
2232 if (r_type == elfcpp::R_X86_64_GOTPLT64
2233 && !gsym->final_value_is_known())
2234 target->make_plt_entry(symtab, layout, gsym);
2238 case elfcpp::R_X86_64_PLT32:
2239 // If the symbol is fully resolved, this is just a PC32 reloc.
2240 // Otherwise we need a PLT entry.
2241 if (gsym->final_value_is_known())
2243 // If building a shared library, we can also skip the PLT entry
2244 // if the symbol is defined in the output file and is protected
2246 if (gsym->is_defined()
2247 && !gsym->is_from_dynobj()
2248 && !gsym->is_preemptible())
2250 target->make_plt_entry(symtab, layout, gsym);
2253 case elfcpp::R_X86_64_GOTPC32:
2254 case elfcpp::R_X86_64_GOTOFF64:
2255 case elfcpp::R_X86_64_GOTPC64:
2256 case elfcpp::R_X86_64_PLTOFF64:
2257 // We need a GOT section.
2258 target->got_section(symtab, layout);
2259 // For PLTOFF64, we also need a PLT entry (but only if the
2260 // symbol is not fully resolved).
2261 if (r_type == elfcpp::R_X86_64_PLTOFF64
2262 && !gsym->final_value_is_known())
2263 target->make_plt_entry(symtab, layout, gsym);
2266 case elfcpp::R_X86_64_COPY:
2267 case elfcpp::R_X86_64_GLOB_DAT:
2268 case elfcpp::R_X86_64_JUMP_SLOT:
2269 case elfcpp::R_X86_64_RELATIVE:
2270 case elfcpp::R_X86_64_IRELATIVE:
2271 // These are outstanding tls relocs, which are unexpected when linking
2272 case elfcpp::R_X86_64_TPOFF64:
2273 case elfcpp::R_X86_64_DTPMOD64:
2274 case elfcpp::R_X86_64_TLSDESC:
2275 gold_error(_("%s: unexpected reloc %u in object file"),
2276 object->name().c_str(), r_type);
2279 // These are initial tls relocs, which are expected for global()
2280 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2281 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2282 case elfcpp::R_X86_64_TLSDESC_CALL:
2283 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2284 case elfcpp::R_X86_64_DTPOFF32:
2285 case elfcpp::R_X86_64_DTPOFF64:
2286 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2287 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2289 const bool is_final = gsym->final_value_is_known();
2290 const tls::Tls_optimization optimized_type
2291 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2294 case elfcpp::R_X86_64_TLSGD: // General-dynamic
2295 if (optimized_type == tls::TLSOPT_NONE)
2297 // Create a pair of GOT entries for the module index and
2298 // dtv-relative offset.
2299 Output_data_got<64, false>* got
2300 = target->got_section(symtab, layout);
2301 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2302 target->rela_dyn_section(layout),
2303 elfcpp::R_X86_64_DTPMOD64,
2304 elfcpp::R_X86_64_DTPOFF64);
2306 else if (optimized_type == tls::TLSOPT_TO_IE)
2308 // Create a GOT entry for the tp-relative offset.
2309 Output_data_got<64, false>* got
2310 = target->got_section(symtab, layout);
2311 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2312 target->rela_dyn_section(layout),
2313 elfcpp::R_X86_64_TPOFF64);
2315 else if (optimized_type != tls::TLSOPT_TO_LE)
2316 unsupported_reloc_global(object, r_type, gsym);
2319 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2320 target->define_tls_base_symbol(symtab, layout);
2321 if (optimized_type == tls::TLSOPT_NONE)
2323 // Create reserved PLT and GOT entries for the resolver.
2324 target->reserve_tlsdesc_entries(symtab, layout);
2326 // Create a double GOT entry with an R_X86_64_TLSDESC
2327 // reloc. The R_X86_64_TLSDESC reloc is resolved
2328 // lazily, so the GOT entry needs to be in an area in
2329 // .got.plt, not .got. Call got_section to make sure
2330 // the section has been created.
2331 target->got_section(symtab, layout);
2332 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2333 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2334 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2335 elfcpp::R_X86_64_TLSDESC, 0);
2337 else if (optimized_type == tls::TLSOPT_TO_IE)
2339 // Create a GOT entry for the tp-relative offset.
2340 Output_data_got<64, false>* got
2341 = target->got_section(symtab, layout);
2342 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2343 target->rela_dyn_section(layout),
2344 elfcpp::R_X86_64_TPOFF64);
2346 else if (optimized_type != tls::TLSOPT_TO_LE)
2347 unsupported_reloc_global(object, r_type, gsym);
2350 case elfcpp::R_X86_64_TLSDESC_CALL:
2353 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2354 if (optimized_type == tls::TLSOPT_NONE)
2356 // Create a GOT entry for the module index.
2357 target->got_mod_index_entry(symtab, layout, object);
2359 else if (optimized_type != tls::TLSOPT_TO_LE)
2360 unsupported_reloc_global(object, r_type, gsym);
2363 case elfcpp::R_X86_64_DTPOFF32:
2364 case elfcpp::R_X86_64_DTPOFF64:
2367 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2368 layout->set_has_static_tls();
2369 if (optimized_type == tls::TLSOPT_NONE)
2371 // Create a GOT entry for the tp-relative offset.
2372 Output_data_got<64, false>* got
2373 = target->got_section(symtab, layout);
2374 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2375 target->rela_dyn_section(layout),
2376 elfcpp::R_X86_64_TPOFF64);
2378 else if (optimized_type != tls::TLSOPT_TO_LE)
2379 unsupported_reloc_global(object, r_type, gsym);
2382 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2383 layout->set_has_static_tls();
2384 if (parameters->options().shared())
2385 unsupported_reloc_local(object, r_type);
2394 case elfcpp::R_X86_64_SIZE32:
2395 case elfcpp::R_X86_64_SIZE64:
2397 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2398 object->name().c_str(), r_type,
2399 gsym->demangled_name().c_str());
2405 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2407 Sized_relobj_file<64, false>* object,
2408 unsigned int data_shndx,
2409 unsigned int sh_type,
2410 const unsigned char* prelocs,
2412 Output_section* output_section,
2413 bool needs_special_offset_handling,
2414 size_t local_symbol_count,
2415 const unsigned char* plocal_symbols)
2418 if (sh_type == elfcpp::SHT_REL)
2423 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2424 Target_x86_64::Scan,
2425 Target_x86_64::Relocatable_size_for_reloc>(
2434 needs_special_offset_handling,
2439 // Scan relocations for a section.
2442 Target_x86_64::scan_relocs(Symbol_table* symtab,
2444 Sized_relobj_file<64, false>* object,
2445 unsigned int data_shndx,
2446 unsigned int sh_type,
2447 const unsigned char* prelocs,
2449 Output_section* output_section,
2450 bool needs_special_offset_handling,
2451 size_t local_symbol_count,
2452 const unsigned char* plocal_symbols)
2454 if (sh_type == elfcpp::SHT_REL)
2456 gold_error(_("%s: unsupported REL reloc section"),
2457 object->name().c_str());
2461 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2462 Target_x86_64::Scan>(
2471 needs_special_offset_handling,
2476 // Finalize the sections.
2479 Target_x86_64::do_finalize_sections(
2481 const Input_objects*,
2482 Symbol_table* symtab)
2484 const Reloc_section* rel_plt = (this->plt_ == NULL
2486 : this->plt_->rela_plt());
2487 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2488 this->rela_dyn_, true, false);
2490 // Fill in some more dynamic tags.
2491 Output_data_dynamic* const odyn = layout->dynamic_data();
2494 if (this->plt_ != NULL
2495 && this->plt_->output_section() != NULL
2496 && this->plt_->has_tlsdesc_entry())
2498 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2499 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2500 this->got_->finalize_data_size();
2501 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2502 this->plt_, plt_offset);
2503 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2504 this->got_, got_offset);
2508 // Emit any relocs we saved in an attempt to avoid generating COPY
2510 if (this->copy_relocs_.any_saved_relocs())
2511 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2513 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2514 // the .got.plt section.
2515 Symbol* sym = this->global_offset_table_;
2518 uint64_t data_size = this->got_plt_->current_data_size();
2519 symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2523 // Perform a relocation.
2526 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2527 Target_x86_64* target,
2530 const elfcpp::Rela<64, false>& rela,
2531 unsigned int r_type,
2532 const Sized_symbol<64>* gsym,
2533 const Symbol_value<64>* psymval,
2534 unsigned char* view,
2535 elfcpp::Elf_types<64>::Elf_Addr address,
2536 section_size_type view_size)
2538 if (this->skip_call_tls_get_addr_)
2540 if ((r_type != elfcpp::R_X86_64_PLT32
2541 && r_type != elfcpp::R_X86_64_PC32)
2543 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2545 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2546 _("missing expected TLS relocation"));
2550 this->skip_call_tls_get_addr_ = false;
2555 const Sized_relobj_file<64, false>* object = relinfo->object;
2557 // Pick the value to use for symbols defined in the PLT.
2558 Symbol_value<64> symval;
2560 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2562 symval.set_output_value(target->plt_section()->address()
2563 + gsym->plt_offset());
2566 else if (gsym == NULL && psymval->is_ifunc_symbol())
2568 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2569 if (object->local_has_plt_offset(r_sym))
2571 symval.set_output_value(target->plt_section()->address()
2572 + object->local_plt_offset(r_sym));
2577 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2579 // Get the GOT offset if needed.
2580 // The GOT pointer points to the end of the GOT section.
2581 // We need to subtract the size of the GOT section to get
2582 // the actual offset to use in the relocation.
2583 bool have_got_offset = false;
2584 unsigned int got_offset = 0;
2587 case elfcpp::R_X86_64_GOT32:
2588 case elfcpp::R_X86_64_GOT64:
2589 case elfcpp::R_X86_64_GOTPLT64:
2590 case elfcpp::R_X86_64_GOTPCREL:
2591 case elfcpp::R_X86_64_GOTPCREL64:
2594 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2595 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2599 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2600 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2601 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2602 - target->got_size());
2604 have_got_offset = true;
2613 case elfcpp::R_X86_64_NONE:
2614 case elfcpp::R_X86_64_GNU_VTINHERIT:
2615 case elfcpp::R_X86_64_GNU_VTENTRY:
2618 case elfcpp::R_X86_64_64:
2619 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2622 case elfcpp::R_X86_64_PC64:
2623 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2627 case elfcpp::R_X86_64_32:
2628 // FIXME: we need to verify that value + addend fits into 32 bits:
2629 // uint64_t x = value + addend;
2630 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2631 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2632 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2635 case elfcpp::R_X86_64_32S:
2636 // FIXME: we need to verify that value + addend fits into 32 bits:
2637 // int64_t x = value + addend; // note this quantity is signed!
2638 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2639 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2642 case elfcpp::R_X86_64_PC32:
2643 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2647 case elfcpp::R_X86_64_16:
2648 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2651 case elfcpp::R_X86_64_PC16:
2652 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2656 case elfcpp::R_X86_64_8:
2657 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2660 case elfcpp::R_X86_64_PC8:
2661 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2665 case elfcpp::R_X86_64_PLT32:
2666 gold_assert(gsym == NULL
2667 || gsym->has_plt_offset()
2668 || gsym->final_value_is_known()
2669 || (gsym->is_defined()
2670 && !gsym->is_from_dynobj()
2671 && !gsym->is_preemptible()));
2672 // Note: while this code looks the same as for R_X86_64_PC32, it
2673 // behaves differently because psymval was set to point to
2674 // the PLT entry, rather than the symbol, in Scan::global().
2675 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2679 case elfcpp::R_X86_64_PLTOFF64:
2682 gold_assert(gsym->has_plt_offset()
2683 || gsym->final_value_is_known());
2684 elfcpp::Elf_types<64>::Elf_Addr got_address;
2685 got_address = target->got_section(NULL, NULL)->address();
2686 Relocate_functions<64, false>::rela64(view, object, psymval,
2687 addend - got_address);
2690 case elfcpp::R_X86_64_GOT32:
2691 gold_assert(have_got_offset);
2692 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2695 case elfcpp::R_X86_64_GOTPC32:
2698 elfcpp::Elf_types<64>::Elf_Addr value;
2699 value = target->got_plt_section()->address();
2700 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2704 case elfcpp::R_X86_64_GOT64:
2705 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2706 // Since we always add a PLT entry, this is equivalent.
2707 case elfcpp::R_X86_64_GOTPLT64:
2708 gold_assert(have_got_offset);
2709 Relocate_functions<64, false>::rela64(view, got_offset, addend);
2712 case elfcpp::R_X86_64_GOTPC64:
2715 elfcpp::Elf_types<64>::Elf_Addr value;
2716 value = target->got_plt_section()->address();
2717 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2721 case elfcpp::R_X86_64_GOTOFF64:
2723 elfcpp::Elf_types<64>::Elf_Addr value;
2724 value = (psymval->value(object, 0)
2725 - target->got_plt_section()->address());
2726 Relocate_functions<64, false>::rela64(view, value, addend);
2730 case elfcpp::R_X86_64_GOTPCREL:
2732 gold_assert(have_got_offset);
2733 elfcpp::Elf_types<64>::Elf_Addr value;
2734 value = target->got_plt_section()->address() + got_offset;
2735 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2739 case elfcpp::R_X86_64_GOTPCREL64:
2741 gold_assert(have_got_offset);
2742 elfcpp::Elf_types<64>::Elf_Addr value;
2743 value = target->got_plt_section()->address() + got_offset;
2744 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2748 case elfcpp::R_X86_64_COPY:
2749 case elfcpp::R_X86_64_GLOB_DAT:
2750 case elfcpp::R_X86_64_JUMP_SLOT:
2751 case elfcpp::R_X86_64_RELATIVE:
2752 case elfcpp::R_X86_64_IRELATIVE:
2753 // These are outstanding tls relocs, which are unexpected when linking
2754 case elfcpp::R_X86_64_TPOFF64:
2755 case elfcpp::R_X86_64_DTPMOD64:
2756 case elfcpp::R_X86_64_TLSDESC:
2757 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2758 _("unexpected reloc %u in object file"),
2762 // These are initial tls relocs, which are expected when linking
2763 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2764 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2765 case elfcpp::R_X86_64_TLSDESC_CALL:
2766 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2767 case elfcpp::R_X86_64_DTPOFF32:
2768 case elfcpp::R_X86_64_DTPOFF64:
2769 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2770 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2771 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2772 view, address, view_size);
2775 case elfcpp::R_X86_64_SIZE32:
2776 case elfcpp::R_X86_64_SIZE64:
2778 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2779 _("unsupported reloc %u"),
2787 // Perform a TLS relocation.
2790 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2791 Target_x86_64* target,
2793 const elfcpp::Rela<64, false>& rela,
2794 unsigned int r_type,
2795 const Sized_symbol<64>* gsym,
2796 const Symbol_value<64>* psymval,
2797 unsigned char* view,
2798 elfcpp::Elf_types<64>::Elf_Addr address,
2799 section_size_type view_size)
2801 Output_segment* tls_segment = relinfo->layout->tls_segment();
2803 const Sized_relobj_file<64, false>* object = relinfo->object;
2804 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2805 elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2806 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2808 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2810 const bool is_final = (gsym == NULL
2811 ? !parameters->options().shared()
2812 : gsym->final_value_is_known());
2813 tls::Tls_optimization optimized_type
2814 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2817 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2818 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2820 // If this code sequence is used in a non-executable section,
2821 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2822 // on the assumption that it's being used by itself in a debug
2823 // section. Therefore, in the unlikely event that the code
2824 // sequence appears in a non-executable section, we simply
2825 // leave it unoptimized.
2826 optimized_type = tls::TLSOPT_NONE;
2828 if (optimized_type == tls::TLSOPT_TO_LE)
2830 gold_assert(tls_segment != NULL);
2831 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2832 rela, r_type, value, view,
2838 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2839 ? GOT_TYPE_TLS_OFFSET
2840 : GOT_TYPE_TLS_PAIR);
2841 unsigned int got_offset;
2844 gold_assert(gsym->has_got_offset(got_type));
2845 got_offset = gsym->got_offset(got_type) - target->got_size();
2849 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2850 gold_assert(object->local_has_got_offset(r_sym, got_type));
2851 got_offset = (object->local_got_offset(r_sym, got_type)
2852 - target->got_size());
2854 if (optimized_type == tls::TLSOPT_TO_IE)
2856 gold_assert(tls_segment != NULL);
2857 value = target->got_plt_section()->address() + got_offset;
2858 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2859 value, view, address, view_size);
2862 else if (optimized_type == tls::TLSOPT_NONE)
2864 // Relocate the field with the offset of the pair of GOT
2866 value = target->got_plt_section()->address() + got_offset;
2867 Relocate_functions<64, false>::pcrela32(view, value, addend,
2872 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2873 _("unsupported reloc %u"), r_type);
2876 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2877 case elfcpp::R_X86_64_TLSDESC_CALL:
2878 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2880 // See above comment for R_X86_64_TLSGD.
2881 optimized_type = tls::TLSOPT_NONE;
2883 if (optimized_type == tls::TLSOPT_TO_LE)
2885 gold_assert(tls_segment != NULL);
2886 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2887 rela, r_type, value, view,
2893 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2894 ? GOT_TYPE_TLS_OFFSET
2895 : GOT_TYPE_TLS_DESC);
2896 unsigned int got_offset = 0;
2897 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2898 && optimized_type == tls::TLSOPT_NONE)
2900 // We created GOT entries in the .got.tlsdesc portion of
2901 // the .got.plt section, but the offset stored in the
2902 // symbol is the offset within .got.tlsdesc.
2903 got_offset = (target->got_size()
2904 + target->got_plt_section()->data_size());
2908 gold_assert(gsym->has_got_offset(got_type));
2909 got_offset += gsym->got_offset(got_type) - target->got_size();
2913 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2914 gold_assert(object->local_has_got_offset(r_sym, got_type));
2915 got_offset += (object->local_got_offset(r_sym, got_type)
2916 - target->got_size());
2918 if (optimized_type == tls::TLSOPT_TO_IE)
2920 gold_assert(tls_segment != NULL);
2921 value = target->got_plt_section()->address() + got_offset;
2922 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2923 rela, r_type, value, view, address,
2927 else if (optimized_type == tls::TLSOPT_NONE)
2929 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2931 // Relocate the field with the offset of the pair of GOT
2933 value = target->got_plt_section()->address() + got_offset;
2934 Relocate_functions<64, false>::pcrela32(view, value, addend,
2940 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2941 _("unsupported reloc %u"), r_type);
2944 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2945 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2947 // See above comment for R_X86_64_TLSGD.
2948 optimized_type = tls::TLSOPT_NONE;
2950 if (optimized_type == tls::TLSOPT_TO_LE)
2952 gold_assert(tls_segment != NULL);
2953 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2954 value, view, view_size);
2957 else if (optimized_type == tls::TLSOPT_NONE)
2959 // Relocate the field with the offset of the GOT entry for
2960 // the module index.
2961 unsigned int got_offset;
2962 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2963 - target->got_size());
2964 value = target->got_plt_section()->address() + got_offset;
2965 Relocate_functions<64, false>::pcrela32(view, value, addend,
2969 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2970 _("unsupported reloc %u"), r_type);
2973 case elfcpp::R_X86_64_DTPOFF32:
2974 // This relocation type is used in debugging information.
2975 // In that case we need to not optimize the value. If the
2976 // section is not executable, then we assume we should not
2977 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2978 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2980 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2982 gold_assert(tls_segment != NULL);
2983 value -= tls_segment->memsz();
2985 Relocate_functions<64, false>::rela32(view, value, addend);
2988 case elfcpp::R_X86_64_DTPOFF64:
2989 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2990 if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2992 gold_assert(tls_segment != NULL);
2993 value -= tls_segment->memsz();
2995 Relocate_functions<64, false>::rela64(view, value, addend);
2998 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2999 if (optimized_type == tls::TLSOPT_TO_LE)
3001 gold_assert(tls_segment != NULL);
3002 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3003 rela, r_type, value, view,
3007 else if (optimized_type == tls::TLSOPT_NONE)
3009 // Relocate the field with the offset of the GOT entry for
3010 // the tp-relative offset of the symbol.
3011 unsigned int got_offset;
3014 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3015 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3016 - target->got_size());
3020 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3021 gold_assert(object->local_has_got_offset(r_sym,
3022 GOT_TYPE_TLS_OFFSET));
3023 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3024 - target->got_size());
3026 value = target->got_plt_section()->address() + got_offset;
3027 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3030 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3031 _("unsupported reloc type %u"),
3035 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3036 value -= tls_segment->memsz();
3037 Relocate_functions<64, false>::rela32(view, value, addend);
3042 // Do a relocation in which we convert a TLS General-Dynamic to an
3046 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3049 const elfcpp::Rela<64, false>& rela,
3051 elfcpp::Elf_types<64>::Elf_Addr value,
3052 unsigned char* view,
3053 elfcpp::Elf_types<64>::Elf_Addr address,
3054 section_size_type view_size)
3056 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3057 // .word 0x6666; rex64; call __tls_get_addr
3058 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3060 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3061 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3063 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3064 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3065 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3066 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3068 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3070 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3071 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3073 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3075 this->skip_call_tls_get_addr_ = true;
3078 // Do a relocation in which we convert a TLS General-Dynamic to a
3082 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3084 Output_segment* tls_segment,
3085 const elfcpp::Rela<64, false>& rela,
3087 elfcpp::Elf_types<64>::Elf_Addr value,
3088 unsigned char* view,
3089 section_size_type view_size)
3091 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3092 // .word 0x6666; rex64; call __tls_get_addr
3093 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3095 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3096 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3098 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3099 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3100 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3101 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3103 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3105 value -= tls_segment->memsz();
3106 Relocate_functions<64, false>::rela32(view + 8, value, 0);
3108 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3110 this->skip_call_tls_get_addr_ = true;
3113 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3116 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3117 const Relocate_info<64, false>* relinfo,
3120 const elfcpp::Rela<64, false>& rela,
3121 unsigned int r_type,
3122 elfcpp::Elf_types<64>::Elf_Addr value,
3123 unsigned char* view,
3124 elfcpp::Elf_types<64>::Elf_Addr address,
3125 section_size_type view_size)
3127 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3129 // leaq foo@tlsdesc(%rip), %rax
3130 // ==> movq foo@gottpoff(%rip), %rax
3131 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3132 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3133 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3134 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3136 const elfcpp::Elf_Xword addend = rela.get_r_addend();
3137 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3141 // call *foo@tlscall(%rax)
3143 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3144 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3145 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3146 view[0] == 0xff && view[1] == 0x10);
3152 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3155 Target_x86_64::Relocate::tls_desc_gd_to_le(
3156 const Relocate_info<64, false>* relinfo,
3158 Output_segment* tls_segment,
3159 const elfcpp::Rela<64, false>& rela,
3160 unsigned int r_type,
3161 elfcpp::Elf_types<64>::Elf_Addr value,
3162 unsigned char* view,
3163 section_size_type view_size)
3165 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3167 // leaq foo@tlsdesc(%rip), %rax
3168 // ==> movq foo@tpoff, %rax
3169 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3170 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3171 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3172 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3175 value -= tls_segment->memsz();
3176 Relocate_functions<64, false>::rela32(view, value, 0);
3180 // call *foo@tlscall(%rax)
3182 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3183 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3184 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3185 view[0] == 0xff && view[1] == 0x10);
3192 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3195 const elfcpp::Rela<64, false>& rela,
3197 elfcpp::Elf_types<64>::Elf_Addr,
3198 unsigned char* view,
3199 section_size_type view_size)
3201 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3202 // ... leq foo@dtpoff(%rax),%reg
3203 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3205 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3206 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3208 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3209 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3211 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3213 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3215 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3217 this->skip_call_tls_get_addr_ = true;
3220 // Do a relocation in which we convert a TLS Initial-Exec to a
3224 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3226 Output_segment* tls_segment,
3227 const elfcpp::Rela<64, false>& rela,
3229 elfcpp::Elf_types<64>::Elf_Addr value,
3230 unsigned char* view,
3231 section_size_type view_size)
3233 // We need to examine the opcodes to figure out which instruction we
3236 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3237 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3239 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3240 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3242 unsigned char op1 = view[-3];
3243 unsigned char op2 = view[-2];
3244 unsigned char op3 = view[-1];
3245 unsigned char reg = op3 >> 3;
3253 view[-1] = 0xc0 | reg;
3257 // Special handling for %rsp.
3261 view[-1] = 0xc0 | reg;
3269 view[-1] = 0x80 | reg | (reg << 3);
3272 value -= tls_segment->memsz();
3273 Relocate_functions<64, false>::rela32(view, value, 0);
3276 // Relocate section data.
3279 Target_x86_64::relocate_section(
3280 const Relocate_info<64, false>* relinfo,
3281 unsigned int sh_type,
3282 const unsigned char* prelocs,
3284 Output_section* output_section,
3285 bool needs_special_offset_handling,
3286 unsigned char* view,
3287 elfcpp::Elf_types<64>::Elf_Addr address,
3288 section_size_type view_size,
3289 const Reloc_symbol_changes* reloc_symbol_changes)
3291 gold_assert(sh_type == elfcpp::SHT_RELA);
3293 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3294 Target_x86_64::Relocate>(
3300 needs_special_offset_handling,
3304 reloc_symbol_changes);
3307 // Apply an incremental relocation. Incremental relocations always refer
3308 // to global symbols.
3311 Target_x86_64::apply_relocation(
3312 const Relocate_info<64, false>* relinfo,
3313 elfcpp::Elf_types<64>::Elf_Addr r_offset,
3314 unsigned int r_type,
3315 elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3317 unsigned char* view,
3318 elfcpp::Elf_types<64>::Elf_Addr address,
3319 section_size_type view_size)
3321 gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3333 // Return the size of a relocation while scanning during a relocatable
3337 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3338 unsigned int r_type,
3343 case elfcpp::R_X86_64_NONE:
3344 case elfcpp::R_X86_64_GNU_VTINHERIT:
3345 case elfcpp::R_X86_64_GNU_VTENTRY:
3346 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
3347 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
3348 case elfcpp::R_X86_64_TLSDESC_CALL:
3349 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
3350 case elfcpp::R_X86_64_DTPOFF32:
3351 case elfcpp::R_X86_64_DTPOFF64:
3352 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
3353 case elfcpp::R_X86_64_TPOFF32: // Local-exec
3356 case elfcpp::R_X86_64_64:
3357 case elfcpp::R_X86_64_PC64:
3358 case elfcpp::R_X86_64_GOTOFF64:
3359 case elfcpp::R_X86_64_GOTPC64:
3360 case elfcpp::R_X86_64_PLTOFF64:
3361 case elfcpp::R_X86_64_GOT64:
3362 case elfcpp::R_X86_64_GOTPCREL64:
3363 case elfcpp::R_X86_64_GOTPCREL:
3364 case elfcpp::R_X86_64_GOTPLT64:
3367 case elfcpp::R_X86_64_32:
3368 case elfcpp::R_X86_64_32S:
3369 case elfcpp::R_X86_64_PC32:
3370 case elfcpp::R_X86_64_PLT32:
3371 case elfcpp::R_X86_64_GOTPC32:
3372 case elfcpp::R_X86_64_GOT32:
3375 case elfcpp::R_X86_64_16:
3376 case elfcpp::R_X86_64_PC16:
3379 case elfcpp::R_X86_64_8:
3380 case elfcpp::R_X86_64_PC8:
3383 case elfcpp::R_X86_64_COPY:
3384 case elfcpp::R_X86_64_GLOB_DAT:
3385 case elfcpp::R_X86_64_JUMP_SLOT:
3386 case elfcpp::R_X86_64_RELATIVE:
3387 case elfcpp::R_X86_64_IRELATIVE:
3388 // These are outstanding tls relocs, which are unexpected when linking
3389 case elfcpp::R_X86_64_TPOFF64:
3390 case elfcpp::R_X86_64_DTPMOD64:
3391 case elfcpp::R_X86_64_TLSDESC:
3392 object->error(_("unexpected reloc %u in object file"), r_type);
3395 case elfcpp::R_X86_64_SIZE32:
3396 case elfcpp::R_X86_64_SIZE64:
3398 object->error(_("unsupported reloc %u against local symbol"), r_type);
3403 // Scan the relocs during a relocatable link.
3406 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3408 Sized_relobj_file<64, false>* object,
3409 unsigned int data_shndx,
3410 unsigned int sh_type,
3411 const unsigned char* prelocs,
3413 Output_section* output_section,
3414 bool needs_special_offset_handling,
3415 size_t local_symbol_count,
3416 const unsigned char* plocal_symbols,
3417 Relocatable_relocs* rr)
3419 gold_assert(sh_type == elfcpp::SHT_RELA);
3421 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3422 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3424 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3425 Scan_relocatable_relocs>(
3433 needs_special_offset_handling,
3439 // Relocate a section during a relocatable link.
3442 Target_x86_64::relocate_for_relocatable(
3443 const Relocate_info<64, false>* relinfo,
3444 unsigned int sh_type,
3445 const unsigned char* prelocs,
3447 Output_section* output_section,
3448 off_t offset_in_output_section,
3449 const Relocatable_relocs* rr,
3450 unsigned char* view,
3451 elfcpp::Elf_types<64>::Elf_Addr view_address,
3452 section_size_type view_size,
3453 unsigned char* reloc_view,
3454 section_size_type reloc_view_size)
3456 gold_assert(sh_type == elfcpp::SHT_RELA);
3458 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3463 offset_in_output_section,
3472 // Return the value to use for a dynamic which requires special
3473 // treatment. This is how we support equality comparisons of function
3474 // pointers across shared library boundaries, as described in the
3475 // processor specific ABI supplement.
3478 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3480 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3481 return this->plt_section()->address() + gsym->plt_offset();
3484 // Return a string used to fill a code section with nops to take up
3485 // the specified length.
3488 Target_x86_64::do_code_fill(section_size_type length) const
3492 // Build a jmpq instruction to skip over the bytes.
3493 unsigned char jmp[5];
3495 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3496 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3497 + std::string(length - 5, '\0'));
3500 // Nop sequences of various lengths.
3501 const char nop1[1] = { 0x90 }; // nop
3502 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3503 const char nop3[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3504 const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3505 const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3507 const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3509 const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3511 const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3512 0x00, 0x00, 0x00, 0x00 };
3513 const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3514 0x00, 0x00, 0x00, 0x00,
3516 const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3517 0x84, 0x00, 0x00, 0x00,
3519 const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3520 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3522 const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3523 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3524 0x00, 0x00, 0x00, 0x00 };
3525 const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3526 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3527 0x00, 0x00, 0x00, 0x00,
3529 const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3530 0x66, 0x2e, 0x0f, 0x1f, // data16
3531 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3533 const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3534 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3535 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3538 const char* nops[16] = {
3540 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3541 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3544 return std::string(nops[length], length);
3547 // Return the addend to use for a target specific relocation. The
3548 // only target specific relocation is R_X86_64_TLSDESC for a local
3549 // symbol. We want to set the addend is the offset of the local
3550 // symbol in the TLS segment.
3553 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3556 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3557 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3558 gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3559 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3560 const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3561 gold_assert(psymval->is_tls_symbol());
3562 // The value of a TLS symbol is the offset in the TLS segment.
3563 return psymval->value(ti.object, 0);
3566 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3567 // compiled with -fsplit-stack. The function calls non-split-stack
3568 // code. We have to change the function so that it always ensures
3569 // that it has enough stack space to run some random function.
3572 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3573 section_offset_type fnoffset,
3574 section_size_type fnsize,
3575 unsigned char* view,
3576 section_size_type view_size,
3578 std::string* to) const
3580 // The function starts with a comparison of the stack pointer and a
3581 // field in the TCB. This is followed by a jump.
3584 if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3587 // We will call __morestack if the carry flag is set after this
3588 // comparison. We turn the comparison into an stc instruction
3590 view[fnoffset] = '\xf9';
3591 this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3593 // lea NN(%rsp),%r10
3594 // lea NN(%rsp),%r11
3595 else if ((this->match_view(view, view_size, fnoffset,
3596 "\x4c\x8d\x94\x24", 4)
3597 || this->match_view(view, view_size, fnoffset,
3598 "\x4c\x8d\x9c\x24", 4))
3601 // This is loading an offset from the stack pointer for a
3602 // comparison. The offset is negative, so we decrease the
3603 // offset by the amount of space we need for the stack. This
3604 // means we will avoid calling __morestack if there happens to
3605 // be plenty of space on the stack already.
3606 unsigned char* pval = view + fnoffset + 4;
3607 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3608 val -= parameters->options().split_stack_adjust_size();
3609 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3613 if (!object->has_no_split_stack())
3614 object->error(_("failed to match split-stack sequence at "
3615 "section %u offset %0zx"),
3616 shndx, static_cast<size_t>(fnoffset));
3620 // We have to change the function so that it calls
3621 // __morestack_non_split instead of __morestack. The former will
3622 // allocate additional stack space.
3623 *from = "__morestack";
3624 *to = "__morestack_non_split";
3627 // The selector for x86_64 object files.
3629 class Target_selector_x86_64 : public Target_selector_freebsd
3632 Target_selector_x86_64()
3633 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3634 "elf64-x86-64-freebsd")
3638 do_instantiate_target()
3639 { return new Target_x86_64(); }
3643 Target_selector_x86_64 target_selector_x86_64;
3645 } // End anonymous namespace.